Getting the Yandex Lockbox secret value on the Kubernetes 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
Warning
Currently, you cannot set up a link between a workload identity federation and a Yandex Managed Service for Kubernetes cluster. Only custom Kubernetes installations are supported.
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 differentServiceAccount
token format incompatible with the instructions provided here. - You have configured
kube-apiserver
to support projection ofServiceAccount
tokens .
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.yaml
with the following contents:apiVersion: v1 kind: ServiceAccount metadata: name: wlif
-
Apply the manifest:
kubectl apply -f service-account.yaml
-
To check the
ServiceAccount
resource you created, run this command:kubectl describe serviceaccount wlif
Result:
Name: wlif Namespace: default
You will need the
Name
andNamespace
field values to further configure integration on the Yandex Cloud side. -
Create a manifest named
pod.yaml
with 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: ycaud
In the
audience
field, specify the token recipient, e.g.,ycaud
. -
Apply the manifest:
kubectl apply -f pod.yaml
-
Make sure the pod you created has the
RUNNING
status:kubectl get pod test-wlif
Result:
NAME READY STATUS RESTARTS AGE test-wlif 1/1 Running 0 1m
-
Get
URL Issuer
for 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:
- 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 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 a name for the bucket according to the naming requirements.
-
In the Object read access field, select
Public
. -
Click Create bucket to complete the operation.
-
- In the management console
-
Upload the
cluster-jwks.json
file 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.json
file 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 Issuer
for 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.json
file 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 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. - From 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. - Navigate 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.
-
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
: TheNamespace
field 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: