Skip to main content

Overview

The Secret class represents a Kubernetes Secret, which is used to store and manage sensitive information such as passwords, OAuth tokens, SSH keys, and TLS certificates. Secrets are similar to ConfigMaps but are specifically intended for confidential data.

Class Definition

API Version: v1

Constructor

Secret accepts all parameters from NamespacedResource, plus:
str | None
Secret name
str | None
Namespace where the secret will be created
DynamicClient | None
Dynamic client for cluster connection
str | None
AWS access key ID (convenience parameter for AWS credentials)
str | None
AWS secret key (convenience parameter for AWS credentials)
str | None
Htpasswd data (convenience parameter for basic auth)
dict | None
Dictionary of base64-encoded data
dict | None
Dictionary of plain-text data (will be automatically base64-encoded)
str | None
Secret type (e.g., Opaque, kubernetes.io/service-account-token, kubernetes.io/dockercfg, kubernetes.io/tls)
bool
default:"True"
Whether to delete the secret on cleanup
str | None
Path to YAML file for the secret
int
default:"TIMEOUT_4MINUTES"
Timeout for delete operations

Properties

certificate_not_after

Returns the certificate expiration date from the annotation auth.openshift.io/certificate-not-after.

certificate_not_before

Returns the certificate start date from the annotation auth.openshift.io/certificate-not-before.

keys_to_hash

Returns keys that should be hashed in logs: ["data", "stringData"]

Inherited Methods

Secret inherits all methods from NamespacedResource and Resource.

Examples

Creating a Simple Opaque Secret

Creating a Secret with Base64-Encoded Data

Creating a Docker Registry Secret

Creating a TLS Secret

Creating an SSH Key Secret

Creating an AWS Credentials Secret

Creating an htpasswd Secret

Using Secret in a Pod (Volume Mount)

Using Secret in a Pod (Environment Variables)

Listing Secrets

Using Context Manager

Creating from YAML

Secret Types

Opaque (Default)

Arbitrary user-defined data. Most common type for generic secrets.

kubernetes.io/service-account-token

Service account token secret.

kubernetes.io/dockercfg

Serialized ~/.dockercfg file for Docker registry authentication.

kubernetes.io/dockerconfigjson

Serialized ~/.docker/config.json file for Docker registry authentication.

kubernetes.io/basic-auth

Credentials for basic authentication with username and password keys.

kubernetes.io/ssh-auth

Credentials for SSH authentication with ssh-privatekey key.

kubernetes.io/tls

TLS certificate and key with tls.crt and tls.key keys.

bootstrap.kubernetes.io/token

Bootstrap token data.

Security Best Practices

Access Control

  • Use RBAC to restrict access to secrets
  • Follow the principle of least privilege
  • Separate secrets by namespace

Encryption

  • Enable encryption at rest in etcd
  • Use external secret management systems (e.g., HashiCorp Vault, AWS Secrets Manager)

Secret Rotation

  • Regularly rotate secrets
  • Consider using external secret operators for automatic rotation

Avoid Hardcoding

  • Never commit secrets to source control
  • Use tools like git-secrets or pre-commit hooks

Immutability

  • Consider making secrets immutable for production environments
  • Recreate secrets instead of updating them

See Also