gke using workload identity to access secret manager

This is the configuration for our project where we need the cluster name, project name, project number and secret name which looks something like (projects/1092851139093/secrets/first-api-key)

In this approach we are using direct federation approach where we don't do service account impersonation and annotation. So we do not create service account. The permission are direct to the service account and namespace. 

You can use this approach if you like. 

export PROJECT_ID=project-f54eacb9-b4e5-40b9-b0c
export PROJECT_NUMBER=1092851139093
export CLUSTER=autopilot-cluster-1
export LOCATION=us-central1        # <-- change to your cluster's region
export NAMESPACE=default
export KSA=secret-reader-ksa
export SECRET=first-api-key
gcloud config set project $PROJECT_ID

We need to enable secret manager service API service if you haven't done so 


gcloud services enable container.googleapis.com secretmanager.googleapis.com

Enable secret manager component for your cluster



gcloud container clusters update $CLUSTER \
  --location=$LOCATION \
  --enable-secret-manager

gcloud container clusters get-credentials $CLUSTER --location=$LOCATION


And let's see if we have our secret driver deployed to our cluster



kubectl get csidriver secrets-store-gke.csi.k8s.io


Let's create our service account and then enable workload identity there 


kubectl create serviceaccount $KSA -n $NAMESPACE

gcloud secrets add-iam-policy-binding $SECRET \
  --project=$PROJECT_ID \
  --role=roles/secretmanager.secretAccessor \
  --member="principal://iam.googleapis.com/projects/
$PROJECT_NUMBER/locations/global/workloadIdentityPools/
$PROJECT_ID.svc.id.goog/subject/ns/$NAMESPACE/sa/$KSA"



if you have more secret you may need to do it separately for example 


gcloud secrets add-iam-policy-binding second-api-key \
--project=$PROJECT_ID \
--role=roles/secretmanager.secretAccessor \
--member="principal://iam.googleapis.com/projects/$PROJECT_NUMBER
/locations/global/workloadIdentityPools/$PROJECT_ID.svc.id.goog/subject/ns/$NAMESPACE/
sa/$KSA"



If you want to grant access to all the secret you can do this - grant access to entire secret manager. 

gcloud projects add-iam-policy-binding $PROJECT_ID \
  --role=roles/secretmanager.secretAccessor \
  --member="principal://iam.googleapis.com/projects/$PROJECT_NUMBER/locations/global/workloadIdentityPools/$PROJECT_ID.svc.id.goog/subject/ns/$NAMESPACE/sa/$KSA" \
  --condition='title=api-keys-only,expression=resource.name.startsWith("projects/'$PROJECT_NUMBER'/secrets/") && resource.name.contains("-api-key")'
 

And let's deploy our yaml resources and mount our secret to the pod.


cat <<EOF | kubectl apply -f -
apiVersion: secrets-store.csi.x-k8s.io/v1
kind: SecretProviderClass
metadata:
  name: app-secrets
  namespace: $NAMESPACE
spec:
  provider: gke
  parameters:
    secrets: |
      - resourceName: "projects/$PROJECT_NUMBER/secrets/$SECRET/versions/latest"
        path: "first-api-key.txt"
---
apiVersion: v1
kind: Pod
metadata:
  name: secret-test-pod
  namespace: $NAMESPACE
spec:
  serviceAccountName: $KSA
  containers:
  - name: app
    image: busybox:1.36
    command: ["sh", "-c", "sleep 3600"]
    volumeMounts:
    - name: secrets-vol
      mountPath: /var/secrets
      readOnly: true
  volumes:
  - name: secrets-vol
    csi:
      driver: secrets-store-gke.csi.k8s.io
      readOnly: true
      volumeAttributes:
        secretProviderClass: app-secrets
EOF

And as you can see, we are able to mount our secret and I have validated the content to be the same as what we stored in secret manager.



















Comments

Popular posts from this blog

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20

llama cpp running it in google colab