After setting up WIF, you must grant the WIF principal access to the secrets in Secret Manager.
After setting up WIF, you must grant the WIF principal access to the secrets in Secret Manager.
1. In GCP Console, go to **IAM & Admin > IAM**.
1. In GCP Console, go to **IAM & Admin > IAM**.
1. Select **GRANT ACCESS** to grant access to the principal set created through the WIF provider. For example,
1. Select **GRANT ACCESS** to grant access to the principal set created through the WIF provider. The external identity takes the format:
to grant IAM access to the principal matching the project with ID `123`, add
a new principal like: `principalSet://iam.googleapis.com/projects/[PROJECT_NUMBER]/locations/global/workloadIdentityPools/[POOL_ID]/attribute.gitlab_project_id/[PROJECT_ID]`.
- `PROJECT_NUMBER` is your Google Cloud project number (not ID) which can be found in the [Project's dashboard](https://console.cloud.google.com/home/dashboard)
- `POOL_ID` is the ID (not name) of the Workload Identity Pool created in the first section, for example `gitlab-pool`
- `GITLAB_PROJECT_ID` is the GitLab project ID that you can copy in your project top-right three dots (**{ellipsis_v}**) menu (click **Copy project ID: XXX**)
1. Assign the role **Secret Manager Secret Accessor**.
1. Assign the role **Secret Manager Secret Accessor**.
1. (Optional) Select **IAM condition (Optional)** to add an IAM condition.
1. (Optional) Select **IAM condition (Optional)** to add an IAM condition.
Under **Condition Builder**, you can add conditions. For example, you could add two `AND` conditions:
Under **Condition Builder**, you can add conditions. For example, you could add two `AND` conditions:
...
@@ -72,14 +85,20 @@ accessing secrets with names starting with the project name.
...
@@ -72,14 +85,20 @@ accessing secrets with names starting with the project name.
## Configure GitLab CI/CD to use GCP Secret Manager secrets
## Configure GitLab CI/CD to use GCP Secret Manager secrets
You can use secrets stored in GCP Secret Manager in CI/CD jobs by defining them with the `gcp_secret_manager` keyword:
You must [add these CI/CD variables](../variables/index.md#for-a-project) to provide details about your GCP Secret Manager:
-`GCP_PROJECT_NUMBER`: The GCP [Project Number](https://cloud.google.com/resource-manager/docs/creating-managing-projects)
-`GCP_WORKLOAD_IDENTITY_FEDERATION_POOL_ID`: The WIF Pool ID (e.g `gitlab-pool`)
-`GCP_WORKLOAD_IDENTITY_FEDERATION_PROVIDER_ID`: The WIF Provider ID (e.g `gitlab-provider`)
Then you can use secrets stored in GCP Secret Manager in CI/CD jobs by defining them with the `gcp_secret_manager` keyword:
```yaml
```yaml
job_using_gcp_sm:
job_using_gcp_sm:
id_tokens:
id_tokens:
GCP_ID_TOKEN:
GCP_ID_TOKEN:
# `aud` must match the audience defined in the WIF Identity Pool.
# `aud` must match the audience defined in the WIF Identity Pool.
You must also [add these CI/CD variables](../variables/index.md#for-a-project) to provide details about your GCP Secret Manager:
NOTE:
A merge request branch name that's too long can fail a job with the following error if the length of [the `assertion.sub` attribute](id_token_authentication.md#token-payload) is longer than 127 characters:
-`GCP_PROJECT_NUMBER`: The GCP [Project Number](https://cloud.google.com/resource-manager/docs/creating-managing-projects)
```plaintext
-`GCP_WORKLOAD_IDENTITY_FEDERATION_POOL_ID`: The WIF Pool ID (e.g `gitlab-pool`)
ERROR: Job failed (system failure): resolving secrets: failed to exchange sts token: googleapi: got HTTP response code 400 with body: {"error":"invalid_request","error_description":"The size of mapped attribute google.subject exceeds the 127 bytes limit. Either modify your attribute mapping or the incoming assertion to produce a mapped attribute that is less than 127 bytes."}
-`GCP_WORKLOAD_IDENTITY_FEDERATION_PROVIDER_ID`: The WIF Provider ID (e.g `gitlab-provider`)
```
For instance, for a `gitlab-org/gitlab` branch, the payload would be `project_path:gitlab-org/gitlab:ref_type:branch:ref:{branch_name}` which means the branch name shouldn't be longer than 76 characters.