KAHIBARO Bookshelves
Discord Login Register

10.2. Secrets

Types of Secrets in OpenShift

In OpenShift, Secret objects are Kubernetes secrets with some OpenShift-specific integrations and usage patterns. At a high level they all store key–value data, but are used for different purposes.

Opaque (generic) secrets

  • Type: Opaque
  • Use for: Arbitrary key–value data that doesn’t fit a more specific type, such as application API keys or custom tokens.
  • Created from:
    • Literal values
    • Files
    • --from-env-file style configuration

Example:

oc create secret generic app-credentials \
  --from-literal=api-key='my-api-key' \
  --from-literal=api-secret='my-secret'

Opaque is often the default when no type is specified.

Docker registry (image pull) secrets

  • Types:
    • kubernetes.io/dockerconfigjson
    • kubernetes.io/dockercfg (legacy)
  • Use for: Authenticating to container registries (e.g. private Quay, Docker Hub, internal registries).
  • Integrated with:
    • ServiceAccounts’ imagePullSecrets
    • Cluster-wide or project-level image pull configuration

Example (create from Docker credentials):

oc create secret docker-registry pull-secret \
  --docker-server=quay.io \
  --docker-username=myuser \
  --docker-password='mypassword' \
  --docker-email=myuser@example.com

TLS secrets

  • Type: kubernetes.io/tls
  • Use for: Storing X.509 certificates and private keys, typically used by:
    • Routes (HTTPS/TLS termination)
    • Ingress controllers
    • Applications that need server certificates

Required keys:

  • tls.crt – certificate (and chain, if needed)
  • tls.key – private key

Example:

oc create secret tls my-tls-secret \
  --cert=server.crt \
  --key=server.key

Routes can reference this secret for edge or re-encrypt termination.

Basic authentication secrets

  • Type: kubernetes.io/basic-auth
  • Use for: Username/password pairs, often for external services.
  • Standard keys:
    • username
    • password

Example:

oc create secret generic db-basic-auth \
  --type=kubernetes.io/basic-auth \
  --from-literal=username=dbuser \
  --from-literal=password='S3cureP@ss'

SSH auth secrets

  • Type: kubernetes.io/ssh-auth
  • Use for: Private SSH keys (for Git over SSH, or other SSH-based integrations).
  • Key:
    • ssh-privatekey

Example:

oc create secret generic git-ssh-key \
  --type=kubernetes.io/ssh-auth \
  --from-file=ssh-privatekey=id_rsa

Service account tokens (service-account-token)

  • Type: kubernetes.io/service-account-token
  • Automatically managed by Kubernetes/OpenShift.
  • Use for: Credentials that Pods use to authenticate to the API server.
  • You normally do not create these manually; they are bound to a ServiceAccount.

Creating Secrets in OpenShift

OpenShift provides multiple ways to create secrets, via CLI, web console, and manifests.

Using `oc create secret`

The CLI provides subcommands for common secret types:

  • Generic / Opaque:
  oc create secret generic app-secret \
    --from-literal=token='12345' \
    --from-file=config.yaml=./config.yaml
  • Docker registry:
  oc create secret docker-registry registry-creds \
    --docker-server=registry.example.com \
    --docker-username=user \
    --docker-password='pass' \
    --docker-email=user@example.com
  • TLS:
  oc create secret tls tls-cert \
    --cert=./tls.crt \
    --key=./tls.key

From environment files

You can create a secret from a file in KEY=VALUE format:

cat > secret.env <<EOF
DB_USER=myuser
DB_PASS=mypass
EOF
oc create secret generic db-credentials \
  --from-env-file=secret.env

Using YAML manifests

Secrets are Kubernetes API objects. They can be declared in YAML and applied with oc apply -f.

Example Opaque secret:

apiVersion: v1
kind: Secret
metadata:
  name: app-credentials
type: Opaque
data:
  api-key: bXktYXBpLWtleQ==
  api-secret: bXktc2VjcmV0

Key points:

  • data values must be base64-encoded.
  • stringData (write-only) can be used for clear text; the API server will encode to data.

Example using stringData:

apiVersion: v1
kind: Secret
metadata:
  name: app-credentials
type: Opaque
stringData:
  api-key: my-api-key
  api-secret: my-secret

Using the OpenShift web console

Typical workflow:

  1. Navigate to Workloads → Secrets in the developer or admin perspective.
  2. Click Create and choose the secret type (e.g., Key/value, TLS, Image pull secret).
  3. Fill in the key/value pairs or upload files.
  4. Select the Project/Namespace where the secret will live.
  5. Save and then reference the secret from Deployments, BuildConfigs, or Routes.

The console guides you through type-specific fields (e.g., server URL for registry secrets, TLS cert/key upload, etc.).

Using Secrets in Pods

Once a secret exists, it must be explicitly referenced by workloads. Common integration patterns are environment variables, mounted volumes, and image pull secrets.

Exposing secrets as environment variables

Useful for small pieces of configuration (e.g. credentials) read by the application at startup.

From a single secret key

YAML snippet:

env:
  - name: DB_USER
    valueFrom:
      secretKeyRef:
        name: db-credentials
        key: DB_USER
  - name: DB_PASS
    valueFrom:
      secretKeyRef:
        name: db-credentials
        key: DB_PASS

Using oc set env:

oc set env deployment/myapp \
  DB_USER_SECRET_FROM=secretKeyRef:name=db-credentials,key=DB_USER

From all keys in a secret

Exports every key in a secret as an environment variable with the same name:

envFrom:
  - secretRef:
      name: app-credentials

Mounting secrets as volumes

This is the recommended approach for files (certificates, SSH keys, config files, etc.) and for apps that periodically reload secrets.

YAML snippet:

volumes:
  - name: certs
    secret:
      secretName: my-tls-secret
containers:
  - name: web
    image: myimage
    volumeMounts:
      - name: certs
        mountPath: /etc/tls
        readOnly: true

Each key in the secret will appear as a file in the mount directory (/etc/tls/tls.crt, /etc/tls/tls.key, etc. for TLS secrets).

Mapping secret keys to specific file paths

You can customize file names and mount only selected keys:

volumes:
  - name: ssh-key
    secret:
      secretName: git-ssh-key
      items:
        - key: ssh-privatekey
          path: id_rsa
containers:
  - name: builder
    image: my-builder
    volumeMounts:
      - name: ssh-key
        mountPath: /root/.ssh
        readOnly: true

Image pull secrets

When pulling images from private registries, reference a Docker registry secret so the kubelet can authenticate.

On a ServiceAccount

Attach the secret to a ServiceAccount used by Pods:

oc secrets link my-serviceaccount registry-creds --for=pull

Or directly in YAML:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: my-serviceaccount
imagePullSecrets:
  - name: registry-creds

Any Pod using this ServiceAccount will automatically use the pull secret.

On a Pod/Deployment

You can also define imagePullSecrets directly:

spec:
  imagePullSecrets:
    - name: registry-creds

In OpenShift, the project’s default builder and default ServiceAccounts often already reference secrets for the internal registry.

Secrets in Common OpenShift Workflows

With Routes and TLS termination

Routes can reference TLS secrets for HTTPS:

  • For edge or re-encrypt termination, you usually:
    1. Create a TLS secret with tls.crt and tls.key.
    2. Reference it in the Route under spec.tls.

Example:

apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: my-secure-route
spec:
  host: app.example.com
  to:
    kind: Service
    name: my-service
  tls:
    termination: edge
    key: |
      -----BEGIN PRIVATE KEY-----
      ...
    certificate: |
      -----BEGIN CERTIFICATE-----
      ...

Alternatively, the console allows you to select a pre-created TLS secret.

With Builds and Source-to-Image (S2I)

Build configurations often need secrets for:

  • Pulling base images from private registries.
  • Accessing private Git repositories.

Typical patterns:

  • Use sourceSecret in a BuildConfig for Git SSH keys:
  spec:
    source:
      type: Git
      git:
        uri: git@github.com:org/repo.git
      sourceSecret:
        name: git-ssh-key
  • Use pullSecret for private image registries:
  spec:
    strategy:
      sourceStrategy:
        from:
          kind: ImageStreamTag
          name: 'my-base-image:latest'
        pullSecret:
          name: registry-creds

With Deployments and StatefulSets

When defining workloads:

  • Reference secrets for:
    • Database credentials
    • API tokens
    • File-based certificates/keys
  • Use envFrom or volumeMounts accordingly.

Operations teams often standardize secret names and keys across projects to keep manifests portable (e.g., always DB_USER, DB_PASSWORD, SSL_CERT, SSL_KEY).

Namespaces, Access Control, and Secrets

Secrets are namespaced resources:

  • A secret in one project/namespace is not visible in another.
  • Access is controlled via RBAC (covered elsewhere), but for secrets:
    • get, list, and especially view access can expose sensitive data.
    • It’s common to restrict who can get or edit secrets.

Impact:

  • Separate applications with different trust boundaries should be in different projects with separate secrets.
  • Shared secrets across projects must be deliberately duplicated or managed by an external secrets manager.

When using oc:

  • oc get secret shows metadata only.
  • oc get secret my-secret -o yaml exposes encoded data.
  • oc extract secret/my-secret can write secret values to local files.

Treat local copies with the same care as the original secret.

Lifecycle and Rotation of Secrets

Updating a secret

Updating secret data can be done via:

  • oc edit secret my-secret
  • oc apply -f updated-secret.yaml
  • oc create secret ... --dry-run=client -o yaml | oc apply -f -

By default, Pods will not automatically restart when the secret changes. How updates are propagated depends on how you use the secret:

  • As environment variables:
    • Values are fixed at Pod start.
    • You must roll out a new Pod (e.g., oc rollout restart deployment/myapp) to pick up changes.
  • As a mounted volume:
    • The Secret volume is updated by the kubelet.
    • Many applications still need a restart or reload to apply new values.
    • Some applications watch files for changes and reload automatically.

Secret rotation strategies

Common patterns:

  • Use short-lived credentials where possible (e.g., tokens with limited TTL).
  • Schedule rotations (manually or automated) for:
    • Certificates before expiry.
    • Passwords and keys according to policy.
  • Combine secret updates with:
    • Rolling Deployment updates.
    • Health checks to ensure new credentials work.

In OpenShift, Operators and controllers often manage their own secrets and rotation (for example, for platform components); application teams typically manage application-level secrets.

Security Considerations Specific to Secrets

Secrets are not a complete security solution by themselves; they are one building block.

Key points in OpenShift:

  • Secrets are only base64-encoded at rest in the etcd data store. Real confidentiality at rest requires:
    • etcd encryption (cluster-level feature).
    • Protecting etcd and control plane access.
  • Secret environment variables are visible in:
    • Process environments inside the container.
    • Some debugging tools, if not restricted.
  • Mounted secret volumes:
    • Are readable by the container user according to volume permissions.
    • Should be mounted readOnly: true where possible.

Additional practices:

  • Avoid placing secret values in:
    • Logs
    • Labels, annotations, or names
    • Application responses or error messages
  • Prefer:
    • Narrow RBAC rules for secrets resource.
    • Different secrets for different components, even if values initially match.
    • Using stringData in manifests only when necessary, and keeping those manifests secure.

Working with Secrets in `oc`

Some useful CLI commands in day-to-day work:

  • List secrets:
  oc get secrets
  • View a specific secret (encoded):
  oc get secret app-credentials -o yaml
  • Decode a specific key:
  oc get secret app-credentials -o jsonpath='{.data.api-key}' | base64 -d
  • Extract all secret data to files:
  oc extract secret/app-credentials --to=-
  # or to a directory
  oc extract secret/app-credentials --to=./secrets
  • Patch a secret (e.g., change a key):
  oc patch secret app-credentials \
    -p='{"stringData":{"api-key":"new-api-key"}}'

Use these tools carefully and avoid leaving sensitive data in shell history or shared terminals.

Views: 0

Comments

Please login to add a comment.

Don't have an account? Register now!

Where to Move

Move chapter:

  • ☰ 1. Course Overview
    • ☰ 1.1. What is OpenShift
      • ☰ 1.2. Why OpenShift is used in modern IT and cloud environments
        • ☰ 1.3. OpenShift vs Kubernetes
          • ☰ 1.4. Typical OpenShift use cases
            • ☰ 1.5. Structure and goals of the course
            • ☰ 2. Containers and Cloud-Native Fundamentals
              • ☰ 2.1. What is a container
                • ☰ 2.2. Containers vs virtual machines
                  • ☰ 2.3. Introduction to Docker and container images
                    • ☰ 2.4. Container registries
                      • ☰ 2.5. Basics of cloud-native applications
                      • ☰ 3. Kubernetes Fundamentals
                        • ☰ 3.1. What is Kubernetes
                          • ☰ 3.2. Kubernetes architecture
                            • ☰ 3.3. Control plane components
                              • ☰ 3.4. Worker node components
                                • ☰ 3.5. Kubernetes objects and resources
                                  • ☰ 3.6. Declarative configuration model
                                  • ☰ 4. OpenShift Architecture
                                    • ☰ 4.1. Overview of OpenShift architecture
                                      • ☰ 4.2. Control plane in OpenShift
                                        • ☰ 4.3. Nodes and machine roles
                                          • ☰ 4.4. OpenShift Operators
                                            • ☰ 4.5. OpenShift networking model
                                              • ☰ 4.6. OpenShift storage model
                                              • ☰ 5. OpenShift Installation and Deployment Models
                                                • ☰ 5.1. OpenShift deployment options
                                                  • ☰ 5.2. Installer-Provisioned Infrastructure
                                                    • ☰ 5.3. User-Provisioned Infrastructure
                                                      • ☰ 5.4. Managed OpenShift services
                                                        • ☰ 5.5. Single-node OpenShift
                                                          • ☰ 5.6. Cluster lifecycle management
                                                          • ☰ 6. Accessing and Using OpenShift
                                                            • ☰ 6.1. OpenShift web console
                                                              • ☰ 6.2. OpenShift CLI (oc)
                                                                • ☰ 6.3. Authentication and authorization
                                                                  • ☰ 6.4. Projects and namespaces
                                                                    • ☰ 6.5. Resource quotas and limits
                                                                    • ☰ 7. Building and Deploying Applications
                                                                      • ☰ 7.1. Application deployment concepts
                                                                        • ☰ 7.2. Source-to-Image (S2I)
                                                                          • ☰ 7.3. Container-based deployments
                                                                            • ☰ 7.4. DeploymentConfigs and Deployments
                                                                              • ☰ 7.5. Rolling updates and rollbacks
                                                                              • ☰ 8. Networking in OpenShift
                                                                                • ☰ 8.1. Services and service discovery
                                                                                  • ☰ 8.2. Routes and ingress
                                                                                    • ☰ 8.3. Network policies
                                                                                      • ☰ 8.4. Load balancing
                                                                                        • ☰ 8.5. External access to applications
                                                                                        • ☰ 9. Storage in OpenShift
                                                                                          • ☰ 9.1. Persistent storage concepts
                                                                                            • ☰ 9.2. Persistent Volumes
                                                                                              • ☰ 9.3. Persistent Volume Claims
                                                                                                • ☰ 9.4. StorageClasses
                                                                                                  • ☰ 9.5. Dynamic provisioning
                                                                                                    • ☰ 9.6. Stateful applications
                                                                                                    • ☰ 10. Configuration and Secrets Management
                                                                                                      • ☰ 10.1. ConfigMaps
                                                                                                        • ☰ 10.2. Secrets
                                                                                                          • ☰ 10.3. Environment variables
                                                                                                            • ☰ 10.4. Managing sensitive data
                                                                                                              • ☰ 10.5. Best practices for configuration
                                                                                                              • ☰ 11. Scaling and High Availability
                                                                                                                • ☰ 11.1. Horizontal pod autoscaling
                                                                                                                  • ☰ 11.2. Vertical scaling
                                                                                                                    • ☰ 11.3. Self-healing and pod restarts
                                                                                                                      • ☰ 11.4. High availability concepts
                                                                                                                        • ☰ 11.5. Multi-zone and multi-region clusters
                                                                                                                        • ☰ 12. Monitoring, Logging, and Observability
                                                                                                                          • ☰ 12.1. Built-in monitoring stack
                                                                                                                            • ☰ 12.2. Metrics and alerts
                                                                                                                              • ☰ 12.3. Logging architecture
                                                                                                                                • ☰ 12.4. Distributed tracing
                                                                                                                                  • ☰ 12.5. Troubleshooting applications
                                                                                                                                  • ☰ 13. Security in OpenShift
                                                                                                                                    • ☰ 13.1. Security model overview
                                                                                                                                      • ☰ 13.2. Role-Based Access Control
                                                                                                                                        • ☰ 13.3. Security Context Constraints
                                                                                                                                          • ☰ 13.4. Image security and scanning
                                                                                                                                            • ☰ 13.5. Network security
                                                                                                                                              • ☰ 13.6. Compliance and auditing
                                                                                                                                              • ☰ 14. CI/CD and DevOps with OpenShift
                                                                                                                                                • ☰ 14.1. DevOps concepts
                                                                                                                                                  • ☰ 14.2. CI/CD pipelines overview
                                                                                                                                                    • ☰ 14.3. OpenShift Pipelines (Tekton)
                                                                                                                                                      • ☰ 14.4. GitOps with Argo CD
                                                                                                                                                        • ☰ 14.5. Integrating OpenShift with external CI tools
                                                                                                                                                        • ☰ 15. Operators and Platform Services
                                                                                                                                                          • ☰ 15.1. What are Operators
                                                                                                                                                            • ☰ 15.2. Operator Lifecycle Manager
                                                                                                                                                              • ☰ 15.3. Installing and managing Operators
                                                                                                                                                                • ☰ 15.4. Common platform Operators
                                                                                                                                                                  • ☰ 15.5. Custom Operators
                                                                                                                                                                  • ☰ 16. Upgrades, Maintenance, and Operations
                                                                                                                                                                    • ☰ 16.1. OpenShift upgrade process
                                                                                                                                                                      • ☰ 16.2. Backup and restore
                                                                                                                                                                        • ☰ 16.3. Node maintenance
                                                                                                                                                                          • ☰ 16.4. Cluster troubleshooting
                                                                                                                                                                            • ☰ 16.5. Capacity planning
                                                                                                                                                                            • ☰ 17. OpenShift on HPC and Specialized Workloads
                                                                                                                                                                              • ☰ 17.1. Running batch workloads on OpenShift
                                                                                                                                                                                • ☰ 17.2. OpenShift and GPUs
                                                                                                                                                                                  • ☰ 17.3. MPI and parallel workloads in containers
                                                                                                                                                                                    • ☰ 17.4. Hybrid HPC and cloud workflows
                                                                                                                                                                                    • ☰ 18. OpenShift in Practice
                                                                                                                                                                                      • ☰ 18.1. Typical OpenShift workflows
                                                                                                                                                                                        • ☰ 18.2. Development to production lifecycle
                                                                                                                                                                                          • ☰ 18.3. Case studies from industry
                                                                                                                                                                                            • ☰ 18.4. Common pitfalls and best practices
                                                                                                                                                                                            • ☰ 19. Future Directions and Ecosystem
                                                                                                                                                                                              • ☰ 19.1. Kubernetes ecosystem overview
                                                                                                                                                                                                • ☰ 19.2. OpenShift roadmap
                                                                                                                                                                                                  • ☰ 19.3. Serverless and event-driven workloads
                                                                                                                                                                                                    • ☰ 19.4. AI and data platforms on OpenShift
                                                                                                                                                                                                    • ☰ 20. Final Project and Hands-On Exercises
                                                                                                                                                                                                      • ☰ 20.1. Deploying a containerized application
                                                                                                                                                                                                        • ☰ 20.2. Implementing CI/CD with OpenShift
                                                                                                                                                                                                          • ☰ 20.3. Securing and scaling an application
                                                                                                                                                                                                            • ☰ 20.4. Monitoring and troubleshooting report