Skip to content

Commit 244c635

Browse files
committed
Improve documentation for kubernetes.io/enforce-mountable-secrets annotation on ServiceAccount
1 parent f214c48 commit 244c635

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

content/en/docs/concepts/configuration/secret.md

+7
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,13 @@ There may be Secrets for several Pods on the same node. However, only the
677677
Secrets that a Pod requests are potentially visible within its containers.
678678
Therefore, one Pod does not have access to the Secrets of another Pod.
679679

680+
### Configure least-privilege access to Secrets
681+
682+
To enhance the security measures around Secrets, Kubernetes provides a mechanism: you can
683+
annotate a ServiceAccount as `kubernetes.io/enforce-mountable-secrets: "true"`.
684+
685+
For more information, you can refer to the [documentation about this annotation](/docs/concepts/security/service-accounts/#enforce-mountable-secrets).
686+
680687
{{< warning >}}
681688
Any containers that run with `privileged: true` on a node can access all
682689
Secrets used on that node.

content/en/docs/concepts/security/secrets-good-practices.md

+6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ recommendations include:
6262
* Implement audit rules that alert on specific events, such as concurrent
6363
reading of multiple Secrets by a single user
6464

65+
#### Additional ServiceAccount annotations for Secret management
66+
67+
You can also use the `kubernetes.io/enforce-mountable-secrets` annotation on
68+
a ServiceAccount to enforce specific rules on how Secrets are used in a Pod.
69+
For more details, see the [documentation on this annotation](/docs/reference/labels-annotations-taints/#enforce-mountable-secrets).
70+
6571
### Improve etcd management policies
6672

6773
Consider wiping or shredding the durable storage used by `etcd` once it is

content/en/docs/concepts/security/service-accounts.md

+30
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,36 @@ or using a custom mechanism such as an [authentication webhook](/docs/reference/
196196
You can also use TokenRequest to obtain short-lived tokens for your external application.
197197
{{< /note >}}
198198

199+
### Restricting access to Secrets {#enforce-mountable-secrets}
200+
201+
Kubernetes provides an annotation called `kubernetes.io/enforce-mountable-secrets`
202+
that you can add to your ServiceAccounts. When this annotation is applied,
203+
the ServiceAccount's secrets can only be mounted on specified types of resources,
204+
enhancing the security posture of your cluster.
205+
206+
You can add the annotation to a ServiceAccount using a manifest:
207+
208+
```yaml
209+
apiVersion: v1
210+
kind: ServiceAccount
211+
metadata:
212+
annotations:
213+
kubernetes.io/enforce-mountable-secrets: "true"
214+
name: my-serviceaccount
215+
namespace: my-namespace
216+
```
217+
When this annotation is set to "true", the Kubernetes control plane ensures that
218+
the Secrets from this ServiceAccount are subject to certain mounting restrictions.
219+
220+
1. The name of each Secret that is mounted as a volume in a Pod must appear in the `secrets` field of the
221+
Pod's ServiceAccount.
222+
1. The name of each Secret referenced using `envFrom` in a Pod must also appear in the `secrets`
223+
field of the Pod's ServiceAccount.
224+
1. The name of each Secret referenced using `imagePullSecrets` in a Pod must also appear in the `secrets`
225+
field of the Pod's ServiceAccount.
226+
227+
By understanding and enforcing these restrictions, cluster administrators can maintain a tighter security profile and ensure that secrets are accessed only by the appropriate resources.
228+
199229
## Authenticating service account credentials {#authenticating-credentials}
200230

201231
ServiceAccounts use signed

content/en/docs/reference/access-authn-authz/admission-controllers.md

+4
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,10 @@ The Kubernetes project strongly recommends enabling this admission controller.
845845
You should enable this admission controller if you intend to make any use of Kubernetes
846846
`ServiceAccount` objects.
847847

848+
Regarding the annotation `kubernetes.io/enforce-mountable-secrets`: While the annotation's name suggests it only concerns the mounting of Secrets,
849+
its enforcement also extends to other ways Secrets are used in the context of a Pod.
850+
Therefore, it is crucial to ensure that all the referenced secrets are correctly specified in the ServiceAccount.
851+
848852
### StorageObjectInUseProtection
849853

850854
**Type**: Mutating.

content/en/docs/reference/labels-annotations-taints/_index.md

+17-2
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,23 @@ Example: `kubernetes.io/enforce-mountable-secrets: "true"`
548548
Used on: ServiceAccount
549549

550550
The value for this annotation must be **true** to take effect.
551-
This annotation indicates that Pods running as this ServiceAccount may only reference
552-
Secret API objects specified in the ServiceAccount's `secrets` field.
551+
When you set this annotation to "true", Kubernetes enforces the following rules for
552+
Pods running as this ServiceAccount:
553+
554+
1. Secrets mounted as volumes must be listed in the ServiceAccount's `secrets` field.
555+
1. Secrets referenced in `envFrom` for containers (including sidecar containers and init containers)
556+
must also be listed in the ServiceAccount's secrets field.
557+
If any container in a Pod references a Secret not listed in the ServiceAccount's `secrets` field
558+
(and even if the reference is marked as `optional`), then the Pod will fail to start,
559+
and an error indicating the non-compliant secret reference will be generated.
560+
1. Secrets referenced in a Pod's `imagePullSecrets` must be present in the
561+
ServiceAccount's `imagePullSecrets` field, the Pod will fail to start,
562+
and an error indicating the non-compliant image pull secret reference will be generated.
563+
564+
When you create or update a Pod, these rules are checked. If a Pod doesn't follow them, it won't start and you'll see an error message.
565+
If a Pod is already running and you change the `kubernetes.io/enforce-mountable-secrets` annotation
566+
to true, or you edit the associated ServiceAccount to remove the reference to a Secret
567+
that the Pod is already using, the Pod continues to run.
553568

554569
### node.kubernetes.io/exclude-from-external-load-balancers
555570

0 commit comments

Comments
 (0)