Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 37 ยท Security and Operators

Service Accounts and Secrets in OpenShift

Give pods and pipelines an identity with service accounts, create and mount secrets, pull from private registries, use short-lived tokens and avoid leaking credentials.

People log in with users and groups. Programs use service accounts: namespaced identities that pods run as, that pipelines authenticate with, and that RBAC and SCCs can target. Alongside them, secrets hold sensitive values such as passwords, API keys and TLS certificates. Together they answer two questions: who is this workload and which credentials may it use.

The commands below are read-only examples; the runnable parts are Python stand-ins that demonstrate encoding, redaction and token expiry.

Service accounts

Every project starts with three: default, builder (used by builds) and deployer. Pods use default unless you say otherwise.

Terminal
oc create serviceaccount reports-bot -n shop
oc get sa -n shop
oc adm policy add-role-to-user view -z reports-bot -n shop
oc set serviceaccount deployment/reports reports-bot

The identity of a service account in RBAC is system:serviceaccount:shop:reports-bot. Bind roles to it just like to a user (see RBAC).

Modern clusters do not create long-lived token secrets automatically. Pods get a bound, short-lived token projected into /var/run/secrets/kubernetes.io/serviceaccount/token, rotated by the kubelet. For an external tool, request a token explicitly:

Terminal
oc create token reports-bot -n shop --duration=1h

The token expires, which is exactly what you want. Avoid creating permanent token secrets unless an old tool has no alternative.

Secrets

A secret is a key/value object, stored base64-encoded in etcd. Base64 is encoding, not encryption. Anyone allowed to get secrets can read the values, so RBAC on secrets matters, and you can additionally enable etcd encryption at rest on the cluster.

Terminal
oc create secret generic db-credentials \
  --from-literal=username=shop \
  --from-literal=password='s3cret-Passw0rd'

oc create secret generic app-config --from-file=config.json
oc create secret tls shop-tls --cert=tls.crt --key=tls.key
oc get secret db-credentials -o yaml

Types you meet often: Opaque (generic), kubernetes.io/tls, kubernetes.io/dockerconfigjson (registry login), kubernetes.io/basic-auth and kubernetes.io/ssh-auth.

Python Stand-in: base64 is not encryption

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Using secrets in pods

As environment variables:

YAML
env:
  - name: DB_PASSWORD
    valueFrom:
      secretKeyRef:
        name: db-credentials
        key: password

Or as files (preferred for certificates, and updated automatically when the secret changes, while env vars need a restart):

YAML
volumes:
  - name: creds
    secret:
      secretName: db-credentials
containers:
  - name: app
    volumeMounts:
      - name: creds
        mountPath: /etc/creds
        readOnly: true

Shortcut from the CLI: oc set env deployment/shop-api --from=secret/db-credentials --prefix=DB_, and oc set volume deployment/shop-api --add --secret-name=db-credentials --mount-path=/etc/creds.

Never put secrets in container images, ConfigMaps, Git repositories or build logs. A common team pattern is to keep encrypted secrets in Git with a tool such as Sealed Secrets, or to sync them from an external vault through the External Secrets Operator or Secrets Store CSI driver, so the cluster object is a copy and the source of truth is elsewhere.

Pulling from a private registry

Terminal
oc create secret docker-registry regcred \
  --docker-server=registry.example.com \
  --docker-username=ci --docker-password="$REG_PASSWORD"

oc secrets link default regcred --for=pull

oc secrets link ... --for=pull adds the secret to the service account's imagePullSecrets, so every pod using it can pull. Without it, pods end in ImagePullBackOff.

Python Stand-in: keep secrets out of logs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Token lifetimes

The bound token of a pod is tied to the pod: when the pod is deleted, the token becomes invalid, and the audience and expiry are enforced. The model below shows why short lifetimes reduce damage when a token leaks.

Python Stand-in: token expiry

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Good habits

  • One service account per application, named for what it does.
  • Give it only the RBAC it needs; default should have none.
  • Set automountServiceAccountToken: false on pods that never call the API.
  • Rotate passwords; changing a secret does not restart pods that read env vars, so run oc rollout restart.
  • Restrict get secrets with view (which excludes secrets) rather than edit.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which statement about Secrets is true?

    Base64 is just encoding; anyone with get access can decode, so protect secrets with RBAC and encryption at rest.
  2. 2. How do you let every pod using the default service account pull from a private registry?

    Linking the pull secret to the service account adds it to imagePullSecrets.
  3. 3. What is the main benefit of bound, projected service account tokens?

    Short-lived tokens limit the damage if one leaks.
  4. 4. A secret used through environment variables was updated. What must you do?

    Environment variables are read at container start; mounted files are refreshed automatically.

Summary

  • Service accounts are identities for workloads; bind RBAC and SCCs to them.
  • Tokens are short-lived and bound to pods; request external ones with oc create token.
  • Secrets are base64, not encrypted; limit who can read them.
  • Mount as files or inject as env vars; keep them out of images, Git and logs.
  • Link pull secrets to service accounts for private registries.

Next: Network policies