Secret handling and access
What a Secret protects — and what it does not
A Kubernetes Secret is a namespaced API object intended for sensitive values, such as a database credential or TLS private key. Unlike a ConfigMap, the Secret resource has specific handling and security guidance. But the resource kind alone does not guarantee encryption at rest, restricted access, or safe application behavior. A person who can read a Secret may obtain its underlying value; treat all representations as sensitive. The exercises here use the public word demo, never a real credential.
Understand data and stringData
data contains base64 representations of bytes. Base64 is reversible encoding, not encryption. The optional stringData field accepts plaintext strings and the API server merges them into data; when both provide a key, stringData takes precedence. stringData is a write-only convenience and has limitations with server-side apply. An Opaque Secret is a general-purpose type; other types such as kubernetes.io/tls expect specific keys. A single Secret has a 1 MiB size limit.
apiVersion: v1kind: Secretmetadata: name: book-demo namespace: book-labtype: Opaquedata: demo-value: ZGVtbw==The four public letters demo encode to ZGVtbw==. No key is required to decode them, so anyone who sees that text can recover the example value. This fixture is not a credential and must not be used as one.
Give only the intended container a reference
A container can receive a selected value with env[].valueFrom.secretKeyRef, a set of eligible values with envFrom.secretRef, or a read-only volume using volumes[].secret.secretName. Keep required references explicit and avoid distributing more values than needed. An application can read a mounted file without having direct API permission to list Secrets. This is a distinction between the kubelet delivering configured data and an application calling the API itself.
apiVersion: v1kind: Podmetadata: name: book-worker namespace: book-labspec: automountServiceAccountToken: false containers: - name: worker image: example.invalid/bytemind/worker:offline volumeMounts: - name: demo-input mountPath: /run/demo-input readOnly: true resources: requests: cpu: 10m memory: 16Mi limits: cpu: 100m memory: 64Mi - name: helper image: example.invalid/bytemind/helper:offline resources: requests: cpu: 10m memory: 16Mi limits: cpu: 100m memory: 64Mi volumes: - name: demo-input secret: secretName: book-demo items: - key: demo-value path: demo-valueOnly worker has a direct mount for /run/demo-input/demo-value. The helper has no direct mount. This reduces unnecessary exposure; it does not establish a strong isolation boundary between mutually untrusted containers in the same Pod. The fixture uses placeholder images and is not deployable.
Apply defense in depth
Use least privilege for read, list and watch operations on Secrets. Permissions to create workloads in a namespace can also enable indirect access to its Secrets, so evaluate workload-creation privileges alongside direct reads. Configure and verify encryption at rest for the Secret API; upstream Kubernetes does not encrypt persisted Secret values by default. Protect transport, backups, logs, nodes and application memory as well. A base64 manifest or plaintext stringData in version control is a disclosure, not a secret manager. Prefer an approved secure delivery and rotation process; external secret managers are a later topic. Do not dump Secret YAML, log environment variables or paste credentials into support tickets.
Practice: reason about access without exposing values
Inspect book-demo.yaml, book-worker.yaml and mount-access.json. Identify which container directly receives the example file and explain what would change if the helper also mounted the volume. Classify the following as separate controls: base64 encoding, storage encryption, authorization and preventing application logs from containing secrets. If a real value is accidentally committed, follow the incident process and revoke or rotate it; deleting the file alone is insufficient. Advanced RBAC and rotation are covered later.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Secret handling and access — knowledge check
Apply the lesson concepts to original offline scenarios.
What does ZGVtbw== demonstrate in the example?
What does stringData allow when writing a Secret?
Which container has a direct demo-input mount in book-worker.yaml?
Which control protects persisted Secret data against storage disclosure?
An actual credential was committed to Git. What is the appropriate response?