StaffCoder
Kubernetes
15 modules · 60 challengesSign up
Module 7 · Secrets

Consuming Secrets

easySecret ConsumptionEnv & Volume Delivery—No data yet

Problem statement

storefront-api in namespace vault-app needs a database password in an environment variable and a certificate and private key as files on disk. Right now the password is typed straight into the pod manifest and the key material is mounted from a ConfigMap called edge-tls-plaintext - so both are readable by anyone who can run a describe. The Secrets holding the real material are already in the namespace; nothing consumes them yet.

About this exercise

A Secret is only useful once a container can read it, and Kubernetes offers exactly two delivery paths. Both hand the container the decoded value - base64 is how the object is stored, never what the process sees.

  • As an environment variable. Instead of a literal value, the variable names the Secret and the key inside it. The kubelet resolves the reference when the container starts, so the plaintext exists in the container's environment and nowhere in the manifest.
  • As files in a volume. A Secret mounted as a volume becomes one file per key, named after the key, with the decoded bytes as contents - which is the only workable delivery for a certificate or a private key. File permissions matter here: a volume can set the mode its files get, and a private key that is world-readable inside the container is a finding in any audit.

What happens when the reference cannot be resolved is part of the contract, not an accident. A reference to a Secret or key that does not exist holds the container back: it never starts, and the cluster records a reason on the container's status saying so. Marking the same reference optional changes that verdict - the container starts and the variable is simply absent. The first is what you want for a credential the app cannot run without; the second for a value that has a sensible default.

kubectl explain pod.spec.containers.env.valueFrom and kubectl explain pod.spec.volumes.secret describe the fields.

How this project is graded

The grader inspects the live pod specs and then reads values from inside the running containers - what the process actually sees, not what the manifest claims. It also checks the pod that must not start, and the one that must. The namespace is re-seeded before each run, so the plaintext original comes back if you delete it without replacing it.

What you will build

Everything lives in namespace vault-app. Two Secrets are already there: db-credentials with the key password, and edge-tls with the keys tls.crt and tls.key.

1. Pod storefront-api - container api, image busybox:1.36, staying alive, Running:

env DB_PASSWORD        the value of key password in Secret db-credentials
/etc/tls/tls.crt       the value of key tls.crt in Secret edge-tls
/etc/tls/tls.key       the value of key tls.key in Secret edge-tls, mode 0400

No credential may appear as a literal value in its spec, and it must not mount the ConfigMap edge-tls-plaintext. The pod that is there now is the plaintext version; replace it.

2. Pod archive-exporter - container exporter, image busybox:1.36, with:

env ARCHIVE_TOKEN      the value of key token in Secret archive-credentials

That Secret does not exist in the namespace, and you must not create it. This pod is expected never to start.

3. Pod archive-exporter-optional - container exporter, image busybox:1.36, referencing the same missing Secret and key, but declaring that reference optional. This pod must be Running.

4. answers/missing-secret.txt - the reason the cluster recorded on the waiting container of archive-exporter, exactly that word and nothing else.

5. The ConfigMap edge-tls-plaintext must no longer exist in vault-app.

Step 1 - the password the app can read

Replace storefront-api so DB_PASSWORD is resolved from the Secret rather than typed in, and confirm from inside the container that the process sees the password itself.

env_var_delivers_the_decoded_value_inside_the_container should pass

Step 2 - key material as files

Deliver edge-tls into the same pod as files under /etc/tls, with the private key readable only by its owner, and stop mounting the plaintext copy.

mounted_secret_files_carry_the_decoded_material, mounted_key_file_has_the_required_mode and plaintext_delivery_replaced_by_secret_references should pass

Step 3 - when the Secret is not there

Create both exporter pods against a Secret that does not exist. One must be held back by the missing reference; the other must start anyway. Record the reason the cluster gives for the one that is stuck.

missing_required_secret_blocks_startup, startup_failure_reason_recorded and optional_reference_starts_without_the_value should pass

When every test passes

You have:

  • A password delivered to the process without ever appearing in the manifest
  • Certificate and key on disk inside the container, with a private key mode an audit will accept
  • The plaintext copy removed rather than left beside the real thing
  • Proof of what a missing credential does to a container, and what marking it optional changes

You do not have yet:

  • Anything the cluster itself consumes. A registry pull and a TLS listener need Secrets of a declared type, with the exact keys the machinery looks for, and that is the next exercise.

Verify

Click Test after every step.

More in Secrets

See the module
Secret Objects & EncodingMove credentials out of a ConfigMap into Secrets built from literals, files and the plaintext write path, and read them back in both stored and decoded form.—No data yeteasyTyped SecretsBuild the registry and TLS Secret types with the shapes their machinery requires, capture what type validation refuses, and wire an image pull secret onto a pod.—No data yeteasyCredential Hygiene OverhaulMigrate every credential of an inherited namespace into Secrets, deliver them by env and mounted files, mint the typed registry and TLS Secrets, and prove with a scan that no plaintext is left.—No data yeteasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.