Want to know more about this blog?

Tom de Brouwer will happily answer all your questions.

By Tom de Brouwer, Merapar Software Architect.

In a series of blog posts we will focus on some of the best practices we use within Merapar to evolve our DevOps practices we have built around the GCP platform. Why? Because we think it’s fun to share knowledge and to learn from others in the industry.


We come across customers who’s preferred Git repository tool and pipelines are hosted by Gitlab.com. Where we will quickly try to deploy from Gitlab runners hosted within the GCP environment, there are always some bootstrap projects required which from a dependency point cannot use the gitlab runners hosted on GCP. The easiest example, there has to be a pipeline which deploys the gitlab runner itself.

Back in the days, we would use a service account key, securely stored in Gitlab. Where the industry has learned that we need to rotate these frequently. This work is cumbersome, and often gets forgotten when the project is in maintenance mode.


A solution which is coming up, and please note that Gitlab states as of December 2022 this feature is available in Alpha and not ready for production, is to use OpenID connect with GCP workload identity federation. 

This is a mouth full of hype words, what it means is that each job running on a Gitlab runner hosted by gitlab.com has an JSON Web Token (JWT) injected via an environment variable. 

Trust should be given to a JWT token signed by Gitlab.com including certain assertions, and as such, in exchange of a JWT token a short lived service account key can be created.

There are 2 parts to this setup, a part within GCP to setup the trust and associated service account. And a second part within the build pipeline steps.

This is depicted in the picture below:

GCP Setup

Let’s look into the required Terraform setup with which we can define the required setup on GCP.
First we will define some variables to be used when creating the other resources

We should create a service account, and provide permissions to create tokens:

Additionally, you want to provide the necessary rights to the service account for the actual deployments.
We are as well required to create an identity pool and an identity pool provider:

Note that in the Workload Identity pool provider attribute condition you can create additional assertions which are valid for your use case. As always within the cloud, having additional assertions here promotes a least privileges and security in depth principle. For more information see https://docs.gitlab.com/ee/ci/cloud_services/google_cloud/ 

And as a last step, we need to glue the service account and workload identity together using a service account IAM binding:

Members can be scoped down using additional attribute referencing from attributes, see https://cloud.google.com/iam/docs/workload-identity-federation#impersonation

Gitlab runner

In order to use the policies associated with the service account, we need to execute a few actions on Gitlab, see the commands below.

When all goes fine, the last gcloud command should have logged you in into the project. When things are misconfigured, you can verify the audit logging of the GCP project containing the identity pool for error messages, these will point you into a solution direction.

While doing so, the JWT is sent to GCP, GCP verifies that the JWT is signed correctly with the public key. The public key is exposed to GCP using the openid-connect configuration as can be seen here: https://gitlab.com/.well-known/openid-configuration. The JWT is then after validating the assertions (e.g. project limitations) exchanged for a short lived service account key.

By using this solution, you can completely move away from managed service account keys in gitlab. We have been using this solution for almost a year and did not have any issues with it although it's still in alfa. We are not using it for any production critical pipelines.

Keep a look out for our next knowledge share blog - Keeping your terraform DRY using terragrunt!