Add a New Secret
Introduction
Secrets are used to configure deployments with sensitive data like database login, API keys, and so on.
Secrets live in Azure Key Vault and never in Git. The
External Secrets Operator (ESO), running in the
cluster, copies them into regular Kubernetes Secrets that your pods can use.
We have two Key Vaults, one per trust boundary:
| Key Vault | ClusterSecretStore | Contains | Namespaces allowed to read it |
|---|---|---|---|
kv-pn-apps | kv-pn-apps | Application secrets (DB passwords, API keys, OAuth clients, ...) | The app namespaces listed in the store |
kv-pn-infra | kv-pn-infra | Platform secrets (tunnel token, Flux webhook token, backup key, ...) | cloudflared, flux-system |
ESO reads Key Vault through workload identity federation: no Azure password or key is stored in the cluster.
Whenever there is <something> in the code or in a command, that's a parameter you need to set.
Basics
There are three required steps:
- Add the secret into the Key Vault
- Declare an
ExternalSecretin the app folder - Use the Kubernetes secret in the pod
All the YAML lives in the app folder apps/<namespace>/ of
polinetwork-cd.
Add the secret into the Key Vault
See this section.
Naming convention
Prefix the Key Vault secret name with the app that owns it, e.g. backend-encryption-key.
Key Vault names allow only letters, digits and hyphens.
Declare the ExternalSecret
Check if the app folder already contains an external-secret.yaml. If it does,
just add a new entry.
Otherwise create it:
apiVersion: external-secrets.io/v1
kind: ExternalSecret
metadata:
name: azure-kv # name of the ExternalSecret
spec:
refreshInterval: 1h # how often ESO re-reads Key Vault
secretStoreRef:
kind: ClusterSecretStore
name: kv-pn-apps
target:
name: azure-kv # name of the Kubernetes Secret ESO creates
creationPolicy: Owner
deletionPolicy: Retain # keep the Secret if the Key Vault secret is deleted
data:
- secretKey: <k8s-key> # key inside the Kubernetes Secret
remoteRef:
key: <kv-secret-name> # secret name in Key Vaultand add it to the app's kustomization.yaml:
resources:
- namespace.yaml
- external-secret.yaml
- deployment.yamlThere is no need to set the namespace in the ExternalSecret: Kustomize sets it
from the namespace field of kustomization.yaml.
By convention, our apps call both the ExternalSecret and the resulting Secret
azure-kv, a name inherited from the AKS setup.
Allow the namespace to use the store
A ClusterSecretStore only serves the namespaces listed in its conditions.
If your app is new, add its namespace to the kv-pn-apps store:
apiVersion: external-secrets.io/v1
kind: ClusterSecretStore
metadata:
name: kv-pn-apps
spec:
conditions:
- namespaces:
- auth
- backend
- <namespace>
...Skip this and the ExternalSecret stays NotReady with an error saying the
store doesn't allow the namespace.
Don't add app namespaces to kv-pn-infra. It holds the secrets that run the
platform itself.
Add a key to an existing ExternalSecret
Append an entry under data:
data:
- secretKey: db-pass
remoteRef:
key: postgres-root-password
- secretKey: <k8s-key>
remoteRef:
key: <kv-secret-name>Use the secret in a pod
As an environment variable
containers:
- name: <container-name>
image: <image>
env:
- name: EXAMPLE_SECRET # env variable name
valueFrom:
secretKeyRef:
name: azure-kv # Kubernetes Secret name (target.name)
key: <k8s-key> # secretKey in the ExternalSecretAs a file (deprecated)
Deprecated
Use environment variables instead. Mounting the Secret as a volume was required by the old CSI driver; with ESO it's only extra YAML, and no app does it anymore. Keep it for an app that can only read its configuration from files.
spec:
containers:
- name: <container-name>
image: <image>
volumeMounts:
- name: secrets
mountPath: /mnt/secrets # one file per key in this directory
readOnly: true
volumes:
- name: secrets
secret:
secretName: azure-kvDeploy and check
Open a PR into polinetwork-cd. Once it's merged, Flux applies it. Then, from the node:
# Ready=True and SecretSynced means ESO copied the secret
kubectl get externalsecret -n <namespace>
# The keys in the Kubernetes Secret (values are not printed)
kubectl describe secret azure-kv -n <namespace>Updating a secret value
ESO re-reads Key Vault every refreshInterval (1 hour). To pick up a new value
immediately, force a sync:
kubectl annotate externalsecret azure-kv -n <namespace> \
force-sync="$(date +%s)" --overwriteEnvironment variables are read only when a container starts, so restart the app:
kubectl rollout restart deployment/<deployment-name> -n <namespace>(Secrets mounted as files are updated in place after a short delay, but the app has to re-read them.)
Key Vault management
You need the Azure CLI, logged in with an account that has the Key Vault Secrets Officer role on the vault.
Add or update a secret
You can add (or update) a secret with the following command:
az keyvault secret set --vault-name "kv-pn-apps" --name "<secret-name>" --value "<secret-value>"If the secret value is too long or you don't want to copy/paste in terminal (and leave it in your shell history) you can use a file instead.
-
Paste the secret value into a text file (no extension or ".txt" is valid) on a single line
-
Run the following command
az keyvault secret set --vault-name "kv-pn-apps" --name "<secret-name>" --file "<filename>" -
Delete the file
Delete a secret
Remove it from every ExternalSecret first, otherwise the ExternalSecret turns
NotReady. Then delete it with:
az keyvault secret delete --vault-name "kv-pn-apps" --name "<secret-name>"After the deletion, the secret remains in a Recoverable State for 90 days.
During this period the secret can be recovered with the following command:
az keyvault secret recover --vault-name "kv-pn-apps" --name "<secret-name>"Both vaults have purge protection enabled: a deleted secret can't be purged before its 90 days are over, and its name can't be reused in the meantime unless you recover it.
To update/re-set that secret, during the 90 days retention period, you must recover it first, then run the command to update it
If you try to set a secret that has been deleted within the last 90 days without restoring it first, you get the following error:
(Conflict) Secret <secret-name> is currently in a deleted but recoverable state, and its name cannot be reused; in this state, the secret can only be recovered or purged.
Code: Conflict
Message: Secret <secret-name> is currently in a deleted but recoverable state, and its name cannot be reused; in this state, the secret can only be recovered or purged.
Inner error: {
"code": "ObjectIsDeletedButRecoverable"
}Example
A complete app that reads one secret, my-app-api-key, from kv-pn-apps:
apiVersion: external-secrets.io/v1
kind: ExternalSecret
metadata:
name: azure-kv
spec:
refreshInterval: 1h
secretStoreRef:
kind: ClusterSecretStore
name: kv-pn-apps
target:
name: azure-kv
creationPolicy: Owner
deletionPolicy: Retain
data:
- secretKey: api-key
remoteRef:
key: my-app-api-keyapiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 1
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: ghcr.io/polinetworkorg/my-app:latest
env:
- name: API_KEY
valueFrom:
secretKeyRef:
name: azure-kv
key: api-key
resources:
requests:
cpu: 10m
memory: 64Mi
limits:
memory: 128MiapiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: my-app
resources:
- namespace.yaml
- external-secret.yaml
- deployment.yamlPlus my-app in the kv-pn-apps store conditions, as shown above.
PoliNetwork Docs