Sub topic 15
Going further: PushSecret, ClusterExternalSecret and generators
This chapter assumes you can write an ExternalSecret and a ClusterSecretStore, as covered earlier. It introduces three resources that extend the basic pull model. Each has its own CRD, and some are still at an alpha API version in many ESO releases. Check kubectl api-resources --api-group=external-secrets.io on your cluster before copying manifests.
The three resources at a glance
| Resource | Scope | What it does |
|---|---|---|
| PushSecret | Namespaced | Writes a Kubernetes Secret out to a provider |
| ClusterExternalSecret | Cluster | Creates an ExternalSecret in every matching namespace |
| Generator | Namespaced | Produces values, such as a random password, for an ExternalSecret to use |
PushSecret: Kubernetes to provider
Sometimes the Secret is born in the cluster, for example a token created by another controller. A PushSecret copies selected keys to the provider so other systems can read them.
apiVersion: external-secrets.io/v1alpha1
kind: PushSecret
metadata:
name: push-token
namespace: app
spec:
refreshInterval: 1h
secretStoreRefs:
- name: aws-store
kind: SecretStore
selector:
secret:
name: generated-token
data:
- match:
secretKey: token
remoteRef:
remoteKey: prod/tokenThe store's credentials now need write permission, such as secretsmanager:CreateSecret and PutSecretValue. Use a separate store for pushing, so the read-only store stays read-only.
ClusterExternalSecret: one definition, many namespaces
A ClusterExternalSecret wraps an ExternalSecret spec and stamps it into each namespace that matches a label selector. It works well with a ClusterSecretStore.
apiVersion: external-secrets.io/v1
kind: ClusterExternalSecret
metadata:
name: shared-db
spec:
namespaceSelectors:
- matchLabels:
team: shared
refreshTime: 1m
externalSecretSpec:
refreshInterval: 1h
secretStoreRef:
name: aws-shared
kind: ClusterSecretStore
target:
name: db-credentials
data:
- secretKey: password
remoteRef:
key: prod/db
property: passwordrefreshTime controls how often the controller re-checks which namespaces match. It is separate from refreshInterval, which controls how often values sync. The ClusterSecretStore's conditions still apply. A namespace that matches the selector but is not in the store's allowed list gets an ExternalSecret that fails.
Generators: values created on demand
A generator is a resource that produces data instead of fetching it. The Password generator creates random strings. You reference it from an ExternalSecret through dataFrom and sourceRef.
apiVersion: generators.external-secrets.io/v1alpha1
kind: Password
metadata:
name: app-password
namespace: app
spec:
length: 32
digits: 5
symbols: 5
symbolCharacters: "-_$@"
noUpper: false
allowRepeat: true
---
apiVersion: external-secrets.io/v1
kind: ExternalSecret
metadata:
name: generated-password
namespace: app
spec:
refreshInterval: 0
target:
name: generated-password
dataFrom:
- sourceRef:
generatorRef:
apiVersion: generators.external-secrets.io/v1alpha1
kind: Password
name: app-passwordThe resulting Secret has a password key. Setting refreshInterval: 0 stops the controller from refreshing after the first sync. With a positive interval, every refresh can produce a new password. That is useful for rotation but can break an application that does not expect the change.
Generators can be combined with a PushSecret to create a password in the cluster and store it in the provider. Other generators exist for ECR tokens, GCR tokens and more. Their names and fields vary by release, so check the ESO documentation for your version.
Exercise
A ClusterExternalSecret targets namespaces labelled team=shared, and references a ClusterSecretStore whose conditions list only team-a and team-b. A namespace named team-c carries the label. What happens there, and which field controls how soon a newly labelled namespace gets its ExternalSecret?
Show answer
The ExternalSecret is created in team-c because the label matches, but it fails to sync because the store's conditions do not allow that namespace. The refreshTime field of the ClusterExternalSecret controls how often namespace matching is re-evaluated.