Skip to content

Delivering Secrets to Kubernetes Workloads with Workload Identity and CSI

Interactive Diagram

새 탭 ↗

As soon as you start declaring deployments with GitOps, the same question appears. Where do the database password and the OAuth client secret live? The fastest answer is a Kubernetes Secret manifest committed to the repository, and at that moment the lifetime of the secret value becomes the lifetime of Git history.

If Splitting Platform and Application Delivery Across Two Argo CD Instances drew the reconciliation boundary, this article covers the credentials that cross it. The short version: secret delivery is not one problem but four separable claims, and putting values in a manifest hands all four to Git and cluster administrators at once.

For a workload to read database-password from a vault, four different things must be true.

  • Ownership: who creates and rotates the value. When IaC creates it rather than a person, the value never passes through anyone’s terminal.
  • Workload identity: how a running Pod proves what it is, without a static credential.
  • Authorization: which vault entries that identity may read. This axis is distinct from cluster administration rights.
  • Injection: what shape the value takes inside the process, and when it is refreshed.

kubectl create secret collapses all four. The owner becomes whoever ran the command, the workload’s identity is replaced by namespace access, authorization shrinks to Kubernetes RBAC alone, and injection becomes a base64 string in etcd. Separating these four axes at Runtime is the whole subject of this article.

The first axis, ownership, is settled in Terraform. The vault itself, a randomly generated password, and the database that consumes it are created in one apply.

resource "random_password" "orders_db_admin" {
length = 32
special = true
}
resource "azurerm_key_vault_secret" "orders_db_admin" {
name = "database-password"
key_vault_id = module.vault.keyvault_id
value = random_password.orders_db_admin.result
}
resource "azurerm_mssql_server" "orders" {
name = "sql-orders-dev"
administrator_login_password = azurerm_key_vault_secret.orders_db_admin.value
}

What matters here is not random_password but the direction of reference. Because the database resource references the vault entry, the vault becomes the single source of the value. Build it the other way (create the database first and copy the password into the vault afterwards) and you now have two truths that will diverge the first time you rotate.

The vault is created with RBAC authorization mode, soft delete, and network restrictions. Up to this point the secret value exists only inside Terraform state, and not one character of it enters the GitOps repository.

A federated credential names one service account in one namespace

Section titled ā€œA federated credential names one service account in one namespaceā€

The second axis, workload identity, is solved by a single federated credential. It trusts the cluster’s OIDC issuer, but not any Pod in that cluster; only one service account in one namespace.

Sequence in which the kubelet projects a service account token and the workload exchanges it for an Azure AD access token to reach Azure resources Source: Azure Workload Identity docs, Introduction, Azure Workload Identity authors (Microsoft), MIT

resource "azurerm_federated_identity_credential" "orders_app" {
name = "cred-k8s-orders-app"
resource_group_name = var.resource_group_name
parent_id = var.workload_identity_id
issuer = module.kubernetes.aks_cluster_issuer_url
subject = "system:serviceaccount:orders:kv-service-account"
audience = ["api://AzureADTokenExchange"]
}

The subject string is the real security boundary of this design. system:serviceaccount:orders:kv-service-account accepts only a projected token issued to kv-service-account in the orders namespace. Creating a service account with the same name in payments produces a different subject and the token exchange is refused. In other words, the ability to create a namespace does not become the ability to read secrets.

The third axis, authorization, is created by granting that identity only a Key Vault Secrets User level role on the vault. Write permission stays with the separate identity used by the IaC pipeline. If readers and writers share one identity, a single compromised workload makes the entire vault writable.

The fourth axis, injection, begins inside the cluster. SecretProviderClass is committed to the GitOps repository, but it contains only secret names and vault coordinates.

Pod creation flows through the kubelet to the Secrets Store CSI Driver and provider, which fetch values from the external secrets store Source: Secrets Store CSI Driver docs, Concepts, The Kubernetes Authors, Apache-2.0

apiVersion: secrets-store.csi.x-k8s.io/v1
kind: SecretProviderClass
metadata:
name: orders-secrets
namespace: orders
spec:
provider: azure
parameters:
usePodIdentity: "false"
tenantId: <tenant-id>
clientID: <workload-identity-client-id>
keyvaultName: platform-secrets
objects: |
array:
- |
objectName: database-password
objectType: secret
objectVersion: ""
- |
objectName: oauth-client-secret
objectType: secret
objectVersion: ""
- |
objectName: signing-key
objectType: secret
objectVersion: ""
# secretObjects:
# - secretName: orders-db-secret
# type: Opaque
# data:
# - key: DATABASE_PASSWORD
# objectName: database-password

usePodIdentity: "false" declares that this uses Workload Identity rather than the older Pod Identity mechanism. objectVersion: "" means always read the latest version, and that choice directly affects the rotation behaviour discussed below.

The commented-out secretObjects block deserves attention. Enabling it makes the CSI driver synchronise mounted values into a real Kubernetes Secret object. Left disabled, the values exist only as files and never appear in etcd. This is an explicit trade of convenience against exposure, not an oversight.

The Pod selects its identity through a label and serviceAccountName

Section titled ā€œThe Pod selects its identity through a label and serviceAccountNameā€

The Deployment overlay needs three things: a pod label that activates the Workload Identity webhook, a serviceAccountName matching the federated subject, and the CSI volume.

apiVersion: apps/v1
kind: Deployment
metadata:
name: orders-api
spec:
template:
metadata:
labels:
app: orders-api
azure.workload.identity/use: "true"
spec:
serviceAccountName: kv-service-account
containers:
- name: orders-api
image: registry.example.com/orders-api:<immutable-tag>
volumeMounts:
- name: platform-secrets
mountPath: /mnt/secrets-store
readOnly: true
volumes:
- name: platform-secrets
csi:
driver: secrets-store.csi.k8s.io
readOnly: true
volumeAttributes:
secretProviderClass: orders-secrets

The label is what makes the webhook inject the projected service account token volume and the identity environment variables. The serviceAccountName is what makes that token’s subject match the federated credential. Omit either and the mount fails with an authentication error, and because it fails at Pod start, it does not fail silently, which is a feature rather than a defect.

PlantUML Diagram

The critical property is that no solid line (no actual value) ever crosses the GitOps boundary. GitOps declares only what should be read; what it is exists only at Runtime.

Whether to enable secretObjects is not a matter of taste but a trade-off.

AspectFile mount onlySynchronised into a Kubernetes Secret
Environment variable compatibilityNone. The application must read filesYes. secretKeyRef wires straight into existing 12-factor apps
Where the value is storedOnly in the Pod’s tmpfs volumeAdditionally stored in etcd
Exposure scopeThat Pod’s filesystemEvery principal with Secret read rights in the namespace
Rotation behaviourFile contents refresh on the driver’s rotation intervalThe Secret object refreshes too, but environment variables keep the old value until the process restarts
Application change requiredRead a file, or point config at a pathNone
Secret object lifetimeNonePersists until the last referencing Pod is gone, and is easily orphaned
Blast radiusSmallGrows to namespace scope

The practical guidance is this. Make file mount the default for new services and let the config loading layer accept a file path. For legacy applications that only support environment variables, use secretObjects synchronisation, but narrow the Secret read RBAC in that namespace separately and document that rotation requires a restart.

This structure has many parts, and each fails with a different symptom. You need to be able to work backwards from symptom to cause.

Failure causeObserved symptomWhere to check
Namespace or service account name mismatch in the federated subjectPod stuck in ContainerCreating with a token exchange rejection in mount eventsCompare the credential’s subject string against the real namespace/SA character by character
Missing pod label azure.workload.identity/useNo projected token or identity environment variables are injected, so authentication failsWhether the injected volume and environment variables exist in the Pod spec
Missing serviceAccountNameThe default service account is used and the subject does not matchThe Pod’s effective service account
No vault read role on the identityAuthentication succeeds but the mount fails with a permission errorRole assignments on the vault
Typo in a SecretProviderClass objectNameOne bad object fails the entire mountThe actual list of secret names in the vault
Vault network restrictions block the clusterMount fails as a timeoutVault network rules and private endpoint
CSI driver addon not installedThe volume plugin cannot be foundCluster addon status
Application still uses the old value after rotationAppears as an authentication error, not a mount failureWhether the file is re-read, and the restart policy if environment variables are used

The last row is the most commonly misread. ā€œThe CSI driver refreshes the fileā€ and ā€œthe application uses the new valueā€ are different statements. For an application that reads the file once at startup, rotation is not automatic. A rotation design must explicitly answer what picks up the new value, and the answer is usually either a file watch or a controlled rolling restart.

The structure described here was validated on the development cluster aks-platform-dev-01, and it is not automatically a production standard.

AxisDevelopment choiceAdditionally required in production
Vault access pathPublic endpoint allowed to simplify initial setupRestricted to a private endpoint and network rules
Identity separationSeveral namespaces easily share a similar-purpose identitySeparate identity and role per namespace or service
RotationManual, only when neededScheduled rotation plus automated post-rotation verification
AuditingDefault vault logsRetain vault access logs separately and Alert on anomalous access
Secret synchronisationTempting to use secretObjects for convenienceFile mount by default, exceptions only by documented approval

Treat secret delivery as one problem and the answer is always ā€œstore the value somewhere.ā€ Split it into ownership, workload identity, authorization, and injection, and each axis can be owned by a different tool. IaC creates the value, a federated credential names exactly one service account in one namespace, a vault role bounds the read scope, and a CSI volume places files only at Runtime.

The result is a GitOps repository that holds references and policy but no values. The next article, GitOps-Deployed Observability Pipelines: From Sampling to Grafana, applies the same declarative boundary to Traces, Metrics, and Logs.