Getting the Yandex Lockbox secret value on the custom Kubernetes installation 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 Kubernetes side under a Yandex Cloud service account. Similarly, you can perform any action via the Yandex Cloud CLI, API, or Terraform.
Note
This tutorial demonstrates an example of integrating a custom Kubernetes installation with a workload identity federation. See the Managed Service for Kubernetes integration guide in Accessing the Yandex Cloud API from a Managed Service for Kubernetes cluster using an Identity and Access Management workload identity federation
To get the Yandex Lockbox secret value under a Kubernetes account:
If you no longer need the resources you created, delete them.
Prepare the Kubernetes cluster
-
Create a new Kubernetes cluster or use an existing one.
Make sure your cluster meets the following requirements:
- You are using Kubernetes 1.20 or higher.
Earlier Kubernetes versions use a differentServiceAccounttoken format incompatible with the instructions provided here. - You have configured
kube-apiserverto support projection ofServiceAccounttokens .
Note
This tutorial applies to both public and private Kubernetes clusters.
- You are using Kubernetes 1.20 or higher.
-
Create a manifest named
service-account.yamlwith the following contents:apiVersion: v1 kind: ServiceAccount metadata: name: wlif -
Apply the manifest:
kubectl apply -f service-account.yaml -
To check the
ServiceAccountresource you created, run this command:kubectl describe serviceaccount wlifResult:
Name: wlif Namespace: defaultYou will need the
NameandNamespacefield values to further configure integration on the Yandex Cloud side. -
Create a manifest named
pod.yamlwith the following contents:apiVersion: v1 kind: Pod metadata: name: test-wlif spec: containers: - image: nginx name: nginx volumeMounts: - mountPath: /var/run/secrets/tokens name: sa-token serviceAccountName: wlif volumes: - name: sa-token projected: sources: - serviceAccountToken: path: sa-token expirationSeconds: 7200 audience: ycaudIn the
audiencefield, specify the token recipient, e.g.,ycaud. -
Apply the manifest:
kubectl apply -f pod.yaml -
Make sure the pod you created has the
RUNNINGstatus:kubectl get pod test-wlifResult:
NAME READY STATUS RESTARTS AGE test-wlif 1/1 Running 0 1m -
Get
URL Issuerfor your Kubernetes cluster:kubectl get --raw /.well-known/openid-configuration | jq -r .issuer -
Create a file with a public key set (JWKS):
kubectl get --raw /openid/v1/jwks > cluster-jwks.json
Get your cloud ready
Sign up for Yandex Cloud and create a billing account:
- Navigate to the management console
and log in to Yandex Cloud or create a new account. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVEorTRIAL_ACTIVEstatus. If you do not have a billing account, create one and link a cloud to it.
If you have an active billing account, you can navigate to the cloud page
Learn more about clouds and folders here.
Required paid resources
The infrastructure support costs include:
- Fee for storing the secret and requests to the secret (see Yandex Lockbox pricing).
- Fee for data storage in a bucket and data operations (see Object Storage pricing).
Upload a file with a public key set to a public bucket
-
Create a public bucket:
Management console- In the management console
, select the folder where you want to create a bucket. - Select Object Storage.
- In the top panel, click Create bucket.
- On the bucket creation page:
-
Enter the bucket name consistent with the naming conventions.
-
In the Read objects field, select
For all. -
Click Create bucket to complete the operation.
-
- In the management console
-
Upload the
cluster-jwks.jsonfile you created earlier into the bucket:Management console- Go to the new bucket.
- In the left-hand panel, select
Objects. - Click Upload objects.
- In the top panel, click
Upload. - In the window that opens, select the
cluster-jwks.jsonfile and click Open. - Click Upload.
- Refresh the page.
-
Get a link to the uploaded file:
Management console- Click the file name.
- Click Get link in the top-right corner.
- Copy the link.
Create a workload identity federation
- In the management console
, select the folder where you want to create a workload identity federation. - In the list of services, select Identity and Access Management.
- In the left-hand panel, select
Workload identity federations. - Click Create federation.
- In the Issuer value (iss) field, enter
URL Issuerfor your Kubernetes cluster, which you got earlier, e.g.,https://kubernetes.default.svc.cluster.local. - In the Acceptable Audience values (AUD) field, enter the token recipient you specified when creating the pod, e.g.,
ycaud. - In the JWKS address field, enter a link to the
cluster-jwks.jsonfile in the bucket. - In the Name field, enter a name for the federation, e.g.,
test-iam-federation. - Click Create.
Create a custom secret
- In the management console
, select the folder where you want to create your secret. - In the list of services, select Lockbox.
- Click Create secret.
- In the Name field, enter a name for the secret:
MY_SECRET. - Select
CustomSecret type. - 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.payloadViewerrole for the folder to the service account:Management console- On the management console home page
, select a folder. - Navigate to the Access bindings tab.
- Find the
sa-lockboxaccount in the list and click . - Click Edit roles.
- In the dialog that opens, click
Add role and select thelockbox.payloadViewerrole.
- 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.
-
In the list of service accounts, select
sa-lockbox. -
Navigate 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:
system:serviceaccount:<namespace>:<account_name>.Where:
namespace: TheNamespacefield value in the output of thekubectl describe serviceaccount <account_name>command, e.g.,default.account_name: Name of the account you created, e.g.,wlif.
-
Click Link.
Configure a scenario in Kubernetes
-
Fill in the variables:
SA_ID="<service_account_ID>" SECRET_ID="<secret_ID>"Where:
SA_ID: Service account ID.SECRET_ID: Custom secret ID.
-
Get the Kubernetes service account token:
SA_TOKEN=`kubectl exec -it test-wlif -- cat /var/run/secrets/tokens/sa-token` -
Exchange the Kubernetes service account token for the IAM token of the Yandex Cloud service account:
IAMTOKEN=$(curl -sH "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=$SA_TOKEN&subject_token_type=urn:ietf:params:oauth:token-type:id_token" -X POST https://auth.yandex.cloud/oauth/token | jq -r '.access_token') -
Request the secret via the API using the Yandex Cloud IAM token:
SECRET_DATA=$(curl -sH "Authorization: Bearer ${IAMTOKEN}" https://payload.lockbox.api.cloud.yandex.net/lockbox/v1/secrets/$SECRET_ID/payload) echo ${SECRET_DATA}Result:
{ "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: