Getting the Lockbox secret value on the GitLab side
Workload Identity Federations allow you to configure a link between external systems and Yandex Cloud via the OpenID Connect
This tutorial shows you as an example how to get the Yandex Lockbox secret value from the GitLab side under a Yandex Cloud service account. Similarly, you can perform any action via the Yandex Cloud CLI, API, or Terraform
To get the Yandex Lockbox secret value under a GitLab account:
If you no longer need the resources you created, delete them.
Create a GitLab project
Create a new GitLab project
Prepare your cloud
Sign up for Yandex Cloud and create a billing account:
- Go to the management console
and log in to Yandex Cloud or create an account if you do not have one yet. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one.
If you have an active billing account, you can go to the cloud page
Learn more about clouds and folders.
Required paid resources
The infrastructure support cost includes a fee for storing a secret and requests to it (see Yandex Lockbox pricing).
Create a workload identity federation
- In the management console
, select the folder you want to create a workload identity federation in. - In the list of services, select Identity and Access Management.
- Go to the Workload identity federations tab.
- Click Create federation.
- In the Issuer value (iss) field, enter the OIDC provider's URL:
https://gitlab.com
. - In the Acceptable Audience values (AUD) field, enter the token recipient:
https://gitlab.com/<group_name>/<gitlab_project_name>
. - In the JWKS address field, enter the URL of the public key list:
https://gitlab.com/oauth/discovery/keys
. - In the Name field, enter a name for the federation, e.g.,
test-iam-federation
. - Click Create.
Note
If using a custom installation of GitLab, replace https://gitlab.com
with the address of your installation in all mentioned values.
Create a custom secret
- In the management console
, select the folder where you want to create a secret. - In the list of services, select Lockbox.
- Click Create secret.
- In the Name field, enter a name for the secret:
MY_SECRET
. - Select Secret type
Custom
. - In the Key field, enter a non-secret ID, e.g.,
secret
. - In the Value field, enter the confidential data you want to store.
- Click Create.
Create a service account
-
Create a service account:
Management console- In the management console
, select the folder where you want to create a service account. - In the list of services, select Identity and Access Management.
- Click Create service account.
- Enter a name for the service account, e.g.,
sa-lockbox
. - Click Create.
- In the management console
-
Assign the
lockbox.payloadViewer
role for the folder to the service account:Management console- On the management console home page
, select a folder. - Go to the Access bindings tab.
- Find the
sa-lockbox
account in the list and click . - Click Edit roles.
- Click
Add role in the dialog that opens and select thelockbox.payloadViewer
role.
- On the management console home page
Link the service account to the federation
- In the management console
, select the folder the service account was created in. - In the list of services, select Identity and Access Management.
- From the list, select the
sa-lockbox
service account. - Go to the Workload identity federations tab.
- Click Link to federation.
- In the Workload identity federation field, select the federation you created earlier.
- In the Subject value (sub) field, specify the external account ID:
project_path:<group_name>/<gitlab_project_name>:ref_type:branch:ref:<repository_branch_name>
. - Click Link.
Configure a GitLab CI/CD script
-
Clone your repository if you have not done so already:
git clone <repository_URL>
-
In a local copy of your repository, create a file named
.gitlab-ci.yml
in the root directory. -
Insert the following code into the
.gitlab-ci.yml
file that will get the value of the Yandex Lockbox secret:stages: - check_token check_token: stage: check_token image: name: ubuntu id_tokens: GITLAB_OIDC_TOKEN: aud: https://gitlab.com/<group_name>/<gitlab_project_name> script: - apt update && apt install curl jq -y # Exchanging the workflow task token for an IAM token of a service account in Yandex Cloud - SA_ID="<service_account_ID>" - >- IAM_TOKEN="$(curl -s -H "Content-Type: application/x-www-form-urlencoded" -d "grant_type=urn:ietf:params:oauth:grant-type:token-exchange&requested_token_type=urn:ietf:params:oauth:token-type:access_token&audience=${SA_ID}&subject_token=${GITLAB_OIDC_TOKEN}&subject_token_type=urn:ietf:params:oauth:token-type:id_token" -X POST https://auth.yandex.cloud/oauth/token | jq -r '.access_token')" # Requesting secret value via the API using an IAM token in Yandex Cloud - SECRET_ID="<secret_ID>" - >- SECRET_DATA=$(curl -sH "Authorization: Bearer ${IAM_TOKEN}" https://payload.lockbox.api.cloud.yandex.net/lockbox/v1/secrets/$SECRET_ID/payload) - echo ${SECRET_DATA} tags: - app-test
Where:
aud
: Token recipient specified when creating the workload identity federation.SA_ID
: Service account ID.SECRET_ID
: Custom secret ID.
-
Save the file and submit the changes to the remote repository:
git add . && git commit -m "Added .gitlab-ci.yml" && git push
Once you submit the code to the repository, the workflow will begin.
-
In GitLab left-hand menu, navigate to Build → Pipelines.
-
Make sure the build's status is Passed. This means that the build was successful.
-
In the GitLab left-hand menu, go to Build → Jobs.
-
Click the last completed task to see the result of executing the script in the log:
{ "entries": [ { "key": "secret", "textValue": "67cH2£?pO********" } ], "versionId": "e6q8isknpcp7********" }
Delete the resources you created
Some resources are not free of charge. Delete the resources you no longer need to avoid paying for them:
- Yandex Lockbox secret
- Service account