Identities and Credentials¶
A TerraformClusterIdentity is a cluster-scoped object that names a Secret of cloud credentials and the namespaces allowed to use it. Every TerraformCluster, TerraformMachine and TerraformMachinePool needs one, directly or by inheriting its cluster’s, before it can run a Job. (An object can also name a Secret in its own namespace instead; see Use a Secret in the object’s namespace.) This page covers creating an identity, choosing which namespaces it allows, referencing it from each kind, rotating and revoking its credentials, and deleting it. For the trust an identity’s credentials carry once mounted into a Job, see Security model; for the mirror’s lifecycle, see Credentials.
Before you begin
- Cluster-scoped access to create
TerraformClusterIdentityobjects, and namespace access to create the Secret it names. getaccess to that Secret: creating an identity, or pointing an existing one at a different Secret, checks it.- A
TerraformCluster,TerraformMachineorTerraformMachinePoolto reference the identity from, or a namespace to create one in.
How credentials reach a Job¶
An identity’s credentials never reach a Job directly. Into each allowed namespace where an object uses the identity, the controller mirrors the source Secret as captf-creds-<identity> and keeps it in sync. Each Job that resolves to this identity gets that mirror both ways:
envFrom, so every key becomes an environment variable — the convention most provider SDKs (AWS_*,ARM_*,GOOGLE_*, …) read. A key starting withTF_orKUBE_never reaches the module’s environment, exceptTF_IN_AUTOMATION,TF_INPUTandKUBE_NAMESPACE, which the Job sets itself and which a Secret key cannot override; see Runtime environment for why.- A read-only file mount at
/var/run/captf/credentials/<key>, one file per key, for file-based authentication (GOOGLE_APPLICATION_CREDENTIALS, a kubeconfig, a PEM key). TheTF_andKUBE_filtering above applies only to the environment: a matching key still appears as a file.
The runner ServiceAccount that a Job runs as can itself read every Secret in its namespace; the identity mechanism controls which credentials a Job carries, not what its ServiceAccount could otherwise reach — see RBAC. The mirror Secret itself, its lifecycle and its sensitivity are cataloged in Secrets; its labels and annotations are listed in Annotations and labels, and the MirrorCreated/MirrorRemoved events in Events.
Create the credentials Secret¶
Create a plain Opaque Secret with one key per credential your module’s providers read:
apiVersion: v1
kind: Secret
metadata:
name: <identity-name>
namespace: <credentials-namespace>
type: Opaque
stringData:
AWS_ACCESS_KEY_ID: <value>
AWS_SECRET_ACCESS_KEY: <value>
<identity-name>— reused below as the identity’s own name; the two do not have to match, but it keeps the pair easy to find.<credentials-namespace>— any namespace the identity’s creator can read Secrets in. It is commonly a provider or platform namespace, separate from the tenant namespaces that use the identity.
The keys are exactly what reaches every Job that resolves to this identity: name them for what your module’s providers expect, not for CAPTF.
Create the identity¶
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1
kind: TerraformClusterIdentity
metadata:
name: <identity-name>
spec:
type: Secret
secretRef:
name: <identity-name>
namespace: <credentials-namespace>
requiredKeys:
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
allowedNamespaces:
list:
- <tenant-namespace>
type says how the credentials are supplied. Secret, the only type, takes them from secretRef, and is what an unset type means.
requiredKeys is optional. It lists the keys the Secret must hold: when one is missing, the identity is Ready=False with reason CredentialsIncomplete, and objects that use it start no Job instead of failing in the cloud with a confusing authentication error. Only key names are checked, never values, so it works the same for every cloud.
allowedNamespaces decides which namespaces may use the identity:
| Value | Meaning |
|---|---|
| unset | No namespace may use it |
list | Exactly the named namespaces |
selector | Namespaces whose labels match |
selector: {} (empty selector) | Every namespace, including one that does not exist yet |
list and selector both set | The union of the two |
{} (empty object) | Rejected: write selector: {} for every namespace instead |
CAPTF reads the namespace’s labels straight from the API server, not from a cache, so a label change applies at once. If the selector is invalid, or the namespace no longer exists, the namespace is denied; only selector: {} allows a namespace that does not exist. An invalid selector is a denial, not a retry.
An empty selector reads as “every namespace” rather than “no namespace”, so allowedNamespaces: {} is rejected outright rather than treated as one or the other. list accepts up to 100 namespace names; selector follows the usual label selector rules. Full field details, including size limits, are in TerraformClusterIdentity.
Creating the identity, and any later change to spec.secretRef or to spec.allowedNamespaces, widening or narrowing, runs a SubjectAccessReview for the requesting user to get the named Secret, and the request is rejected if they may not. This stops a role that may manage identities but not read Secrets from pointing one at a Secret it cannot read, or from granting more namespaces access to one, and having the controller mirror it somewhere that role can. Updates that change neither field, such as to labels or annotations, are not re-checked.
Reference it¶
An identity is used through identityRef, which names it by name (and, for a namespace-local Secret, kind: Secret; see below):
TerraformCluster:spec.identityRefis required and mutable — changing it re-resolves the cluster’s credentials on its next reconcile.TerraformClusterdefaults for machines and pools:spec.defaults.identityRef, mutable, is the identity a machine or pool without its ownidentityRefuses; unset, they fall back further to the cluster’s ownspec.identityRef. See Kinds for howspec.defaultsinheritance works, and Templates and ClusterClass for aTerraformClusterTemplatethat leavesidentityReffor a ClusterClass patch.TerraformMachine:spec.identityRefis optional, and immutable once the object is created — including from unset to a value. Change it by replacing the machine (aMachineDeploymentor control-plane rollout), not by editing it in place. A machine created with noidentityRefkeeps falling back to its cluster’s current default orspec.identityRef, so changing those still changes which credentials such a machine uses.TerraformMachinePool:spec.identityRefis optional and mutable, with the same fallback as a machine.
The fallback chain is the same for both kinds: an object’s own identityRef, else spec.defaults.identityRef, else the cluster’s spec.identityRef.
identityRef.name must resolve to an identity that exists and allows the object’s namespace; the Conditions reference has every reason IdentityAllowed and CredentialsMirrored can carry.
Confirm it worked¶
kubectl get terraformclusteridentity <identity-name>
kubectl describe terraformcluster <cluster-name> -n <tenant-namespace>
The identity’s own Ready condition is True/SecretFound once its Secret exists with every key in requiredKeys, False/SecretNotFound when it does not exist, False/CredentialsIncomplete when a required key is missing, and status.namespaces lists every namespace currently holding a mirror of it. On the object that references it, IdentityAllowed and CredentialsMirrored turn True once the namespace is allowed and the mirror is in place; until then no Job for that object starts.
Use a Secret in the object’s namespace¶
When the credentials belong to the tenant and no platform team needs to gate them, skip the identity and name a Secret from the object’s own namespace:
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1
kind: TerraformCluster
metadata:
name: demo
namespace: team-a
spec:
identityRef:
kind: Secret
name: team-a-aws
kind is TerraformClusterIdentity when unset, or Secret. A Secret reference:
- is read from the namespace of the object that sets it, never another;
- reaches the Job exactly as an identity’s mirror does (
envFromand files under/var/run/captf/credentials/), with the sameTF_andKUBE_rules; - is not mirrored, and there is no
allowedNamespacesrule orSubjectAccessReview:IdentityAllowedisTruewith reasonLocalSecret, orFalsewithSecretNotFoundwhile the Secret is missing; - is not checked against
requiredKeys(that lives on the identity); - works in
spec.identityRef, inspec.defaults.identityRefof aTerraformCluster, and in a machine’s or pool’s ownidentityRef, with the fallback chain above. A machine’sidentityRefstays immutable; - is picked up by the next Job after you change the Secret. The Secret is not watched, so a Secret that was missing is noticed on the object’s next retry (about every 30 seconds while credentials are the blocker).
Deleting the object does not touch the Secret. An immutable kind (TerraformMachine) records the Secret’s name and kind when it applies, so its destroy keeps using it even if the cluster’s default later changes.
Anyone who can write the identityRef can pick the Secret
The Secret is not gated by a platform object, so whoever may create or edit a Terraform* object in the namespace chooses which of its Secrets becomes the Job’s credentials. A module can already read every Secret in its namespace, so this adds no new reach, but it removes the platform team’s say. Use a TerraformClusterIdentity for credentials you hand out. See Multi-tenancy.
Chain roles with the credentials mount¶
An identity’s Secret is not limited to access keys. Because every key also appears as a file under /var/run/captf/credentials/<key> and as an environment variable, you can ship a cloud SDK config that has the Job assume a role or impersonate a service account. CAPTF does nothing special for it: the Secret is delivered, and the provider SDK in the module does the chaining. It works when the module’s provider block uses the default credential chain and sets no credentials itself, as the reference modules do.
Keep the long-lived base credential in a file, not as an environment variable: environment credentials take precedence over a profile, so a plain AWS_ACCESS_KEY_ID key would stop the profile from being used.
apiVersion: v1
kind: Secret
metadata:
name: aws-assume
namespace: <credentials-namespace>
type: Opaque
stringData:
AWS_CONFIG_FILE: /var/run/captf/credentials/config
AWS_SHARED_CREDENTIALS_FILE: /var/run/captf/credentials/credentials
AWS_PROFILE: target
credentials: |
[base]
aws_access_key_id = <value>
aws_secret_access_key = <value>
config: |
[profile target]
role_arn = arn:aws:iam::<account-id>:role/<role-name>
source_profile = base
role_session_name = captf
The AWS_* keys become environment variables that point the SDK at the two files; credentials and config are the files. The role’s trust policy must allow the base principal. Set requiredKeys to ["credentials", "config"] so a Secret missing either one is caught early.
apiVersion: v1
kind: Secret
metadata:
name: gcp-impersonate
namespace: <credentials-namespace>
type: Opaque
stringData:
GOOGLE_APPLICATION_CREDENTIALS: /var/run/captf/credentials/source.json
GOOGLE_IMPERSONATE_SERVICE_ACCOUNT: <target>@<project>.iam.gserviceaccount.com
source.json: |
{ "type": "service_account", ... }
The Google provider reads GOOGLE_IMPERSONATE_SERVICE_ACCOUNT and calls the target through the source key. The source account needs roles/iam.serviceAccountTokenCreator on the target.
Neither has an assume-role chain the identity Secret can express. For Azure, use one service principal per subscription or tenant (the ARM_* keys); a provider that supports auxiliary tenants takes its own setting, which is a module variable, not part of the identity. For OCI, use an API key per target (OCI_* keys, or a config file with a profile, as for AWS). Whether a given module honors a file-based config depends on its provider block: check the module’s page under Cloud modules.
Two cautions. The base credential is still mounted in the Job and is as exposed as any other (see Security model), so scope it to the one permission that matters, the right to assume the role. And the assumed role’s session expires (an hour by default for AWS); a Job longer than that needs a role with a longer maximum session duration, and role_duration_seconds in the profile.
Rotate credentials¶
-
Edit the credentials Secret’s data in place, keeping its name and namespace. The controller cannot watch that Secret, so this is not picked up at once: the mirror in each allowed namespace is rewritten the next time an object that uses the identity reconciles for any reason, and at the latest within one
--sync-period(10 minutes by default). A Job created after that reconcile gets the new values through itsenvFrom. A Job already running gets them too, but only in its file mount: the kubelet resyncs that volume from the mirror on its own schedule, even for a pod that started before the rotation; whether a long-running provider process re-reads a changed file is up to the provider. Only the environment is fixed for the life of the pod.A Job already running when you rotate keeps the old credentials in its environment for its whole run: the old credential must stay valid until every mirror has picked up the rotation and the longest
activeDeadlineSecondsany in-flight Job could still run for has passed (default 3600 seconds; see Deadlines and lock waits), not just until you edited the Secret.Every mirror carries a
captf.io/source-hashannotation of the source Secret’s data at the time it was last written; it changes whenever the mirror is rewritten, so watching it move off the value it held before the edit confirms that namespace’s mirror has picked up the rotation, without comparing credential values directly:kubectl get secret captf-creds-<identity-name> -n <tenant-namespace> \ -o jsonpath='{.metadata.annotations.captf\.io/source-hash}'Repeat for every namespace
status.namespaceslists, or diff the mirror’sdataagainst the source Secret’sdatadirectly if you want to confirm the values themselves rather than only that a rewrite happened. Only once every mirror has moved past its pre-rotation hash is the old credential safe to revoke. - Point the identity at a different Secret, by editingspec.secretRef. This is a change to the identity object itself, so it is watched: every object that uses the identity reconciles at once and refreshes its mirror. It also re-runs theSubjectAccessReview, so the user making the change needsgetaccess to the new Secret.
Revoke access¶
Editing spec.allowedNamespaces to drop a namespace is a change to the identity object, so every object of that namespace using the identity reconciles at once: the controller starts no new Job for it, leaves any infrastructure it already created alone, and deletes the mirror Secret in that namespace, whatever objects still reference it.
Revoking a namespace also blocks destroy
Deleting such an object waits, reporting that the destroy is waiting for the identity to allow the namespace again, until access is restored.
Delete an identity¶
Deletion is refused while:
- a
TerraformCluster,TerraformMachineorTerraformMachinePool, in any namespace, currently resolves to this identity through its ownidentityRefor a cluster’s fallback — an object being deleted still counts, since its destroy still needs the credentials; or status.namespacesstill lists a namespace holding a mirror of it.
The rejection names one object still using it, or every namespace still holding a mirror. Switching an object’s identityRef away from this identity does not by itself release its mirror in that namespace: the mirror keeps that object as an owner until the object is deleted, so status.namespaces keeps listing the namespace, and the identity cannot be deleted, until it is. If nothing resolves to the identity there anymore, deleting the mirror Secret directly also releases the namespace: it is only a copy, so this does not touch the source Secret, and the identity’s status catches up as soon as the deletion is seen.
Deleting an identity keeps its Secret
Deleting an identity never deletes the Secret it names: that Secret belongs to you, and stays behind for you to remove or reuse.
Move considerations¶
TerraformClusterIdentity moves with clusterctl move, but its credentials Secret deliberately does not: see the move runbook for why, and the procedure for copying it to the target cluster yourself.