Operator which manages Kubernetes Secret Resources created from user defined SopsSecrets
CRs, inspired by Bitnami SealedSecrets and
sops. SopsSecret CR defines multiple
kubernetes Secret resources. It supports managing kubernetes Secrets with
annotations and labels, that allows using these kubernetes secrets as Jenkins Credentials.
The SopsSecret resources can be deployed by Flux GitOps CD and
encrypted using sops for AWS, GCP, Azure or
on-prem hosted kubernetes clusters. Using sops greatly simplifies changing
encrypted files stored in git repository.
| Kubernetes | Sops | Chart | Operator |
|---|---|---|---|
| v1.37.x | v3.13.3 | 0.30.2 | 0.22.5 |
| v1.36.x | v3.13.3 | 0.28.1 | 0.21.2 |
| v1.35.x | v3.12.2 | 0.26.0 | 0.20.2 |
| v1.35.x | v3.12.2 | 0.25.4 | 0.20.1 |
| v1.34.x | v3.11.0 | 0.24.1 | 0.17.3 |
| v1.33.x | v3.10.2 | 0.22.0 | 0.16.0 |
| v1.32.x | v3.9.4 | 0.21.0 | 0.15.0 |
| v1.31.x | v3.9.4 | 0.20.5 | 0.14.3 |
| v1.30.x | v3.9.0 | 0.19.4 | 0.13.3 |
| v1.29.x | v3.8.1 | 0.18.6 | 0.12.6 |
| v1.28.x | v3.8.1 | 0.17.4 | 0.11.4 |
| v1.27.x | v3.7.3 | 0.15.5 | 0.9.5 |
| v1.26.x | v3.7.3 | 0.14.2 | 0.8.2 |
| v1.25.x | v3.7.3 | 0.12.5 | 0.6.4 |
| v1.24.x | v3.7.3 | 0.11.3 | 0.5.3 |
| v1.23.x | v3.7.2 | 0.10.8 | 0.4.8 |
| v1.22.x | v3.7.1 | 0.9.7 | 0.3.7 |
| v1.21.x | v3.7.1 | 0.9.6 | 0.3.6 |
Tool versions (Go, kubebuilder, sops, kustomize, k3d, kubectl, helm, helm-docs, kubeconform, git-chglog, golangci-lint, github-cli) are pinned in mise.toml and installed with mise. Resolved download URLs and checksums for all supported platforms are locked in mise.lock:
mise trust # once per clone
make tools # mise install --locked
make test # unit and envtest integration tests
make help # list all targetsAfter changing a tool version in mise.toml run make tools-lock and commit mise.toml together with mise.lock,
CI fails (mise lock --check) if they are out of sync.
CI workflows use the same mise.toml and mise.lock via jdx/mise-action, the
Dockerfile installs the Go version pinned in them. Local overrides can be placed in mise.local.toml (git ignored).
The chart is published to two places with identical content for every chart version, use either of them:
- Helm repository (GitHub Pages):
helm repo add sops https://isindir.github.io/sops-secrets-operator/
helm upgrade --install sops sops/sops-secrets-operator --namespace sops- OCI registry (quay.io, starting with chart version
0.29.3), e.g. to use a registry pull-through cache:
helm upgrade --install sops oci://quay.io/isindir/charts/sops-secrets-operator --namespace sopsAdd --version <chart version> to pin a chart version with either source. Installation instructions below use the
Helm repository (sops/sops-secrets-operator), with the OCI registry use
oci://quay.io/isindir/charts/sops-secrets-operator instead.
- Create KMS key
- Create AWS Role which can be used by operator to decrypt CR data structure, follow sops documentation
- Deploy CRD:
kubectl apply -f config/crd/bases/isindir.github.com_sopssecrets.yamlNOTE: to grant access to aws for
sops-secret-operator- kiam, kube2iam or IAM roles for service accounts can be used.
- Deploy helm chart:
kubectl create namespace sops
helm repo add sops https://isindir.github.io/sops-secrets-operator/
helm upgrade --install sops sops/sops-secrets-operator --namespace sops- Create age reference
keys.txtfile, create kubernetes secret from it. - Deploy helm chart:
- Use
secretsAsFilesto specify the secret which contains thekeys.txt. - Use
extraEnvand specify mountedkeys.txtpathSOPS_AGE_KEY_FILEenvironment variable.
- Use
See example:
---
secretsAsFiles:
- mountPath: /etc/sops-age-key-file
name: sops-age-key-file
secretName: sops-age-key-file
extraEnv:
- name: SOPS_AGE_KEY_FILE
value: /etc/sops-age-key-file/keysops can use an SSH public key as an age recipient (an SSH recipient), so an existing SSH key pair can be
used instead of a dedicated age key. Only ssh-ed25519 and ssh-rsa keys are supported, and encrypting
for an SSH recipient requires sops 3.10.0 or newer.
- Encrypt for the SSH recipient - the whole public key line, comment included, can be used:
sops encrypt --age "$(cat ~/.ssh/sops_ed25519.pub)" --encrypted-suffix Templates \
jenkins-secrets.yaml > jenkins-secrets.enc.yaml- Or encrypt with a
.sops.yamlcreation rule (SSH and age recipients can be mixed, comma separated):
creation_rules:
- encrypted_suffix: Templates
age: >-
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA... [email protected]- Create kubernetes secret from the matching private key (the SSH identity):
kubectl create secret generic sops-age-ssh-key --namespace sops \
--from-file=key=$HOME/.ssh/sops_ed25519- Deploy helm chart:
- Use
secretsAsFilesto mount the secret which contains the SSH identity. - Use
extraEnvand specify mounted SSH identity path inSOPS_AGE_SSH_PRIVATE_KEY_FILEenvironment variable.
- Use
See example:
---
secretsAsFiles:
- mountPath: /etc/sops-age-ssh-key
name: sops-age-ssh-key
secretName: sops-age-ssh-key
extraEnv:
- name: SOPS_AGE_SSH_PRIVATE_KEY_FILE
value: /etc/sops-age-ssh-key/keyNOTE:
- The SSH identity must not be passphrase protected: sops would prompt for the passphrase on a terminal, which the operator does not have, and decryption fails.
- Instead of a file,
SOPS_AGE_SSH_PRIVATE_KEY_CMDcan be set to a command which prints the (not passphrase protected) SSH identity; the command must exist in the operator image.- sops also always loads
~/.ssh/id_ed25519and~/.ssh/id_rsawhen they exist - do not rely on these in the operator container, setSOPS_AGE_SSH_PRIVATE_KEY_FILEexplicitly.- SSH identities and age identities (
SOPS_AGE_KEY_FILE) can be configured together, the operator tries all of them, so SopsSecrets can be migrated from one to the other gradually.
- Also see: Local testing using age
References:
For instructions on how-to configure PGP keys for operator, see Preparing GPG keys
Then install operator:
kubectl create namespace sops
kubectl apply -f docs/gpg/1.yaml --namespace sops
kubectl apply -f docs/gpg/2.yaml --namespace sops
kubectl apply -f config/crd/bases/isindir.github.com_sopssecrets.yaml
helm repo add sops https://isindir.github.io/sops-secrets-operator/
helm upgrade --install sops sops/sops-secrets-operator \
--namespace sops --set gpg.enabled=true- Create a KeyVault if you don't have one already
- Create a Key in that KeyVault
- Create Service principal with permissions to use the key for Encryption/Decryption
- follow the SOPS documentation
- Either put Tenant ID, Client ID and Client Secret for the Service Principal in your custom values.yaml file or create a Kubernetes Secret with the same information and put the name of that secret in your values.yaml. Enable Azure in the Helm Chart by setting
azure.enabled: truein values.yaml.
cat <<EOF > azure_values.yaml
azure:
enabled: true
tenantId: 6ec4c881-32ee-4340-a456-d6ca65a42193
clientId: 9c325550-b264-4aee-ab6f-719771adda28
clientSecret: 'YOUR_CLIENT_SECRET'
EOF
kubectl create namespace sops
helm repo add sops https://isindir.github.io/sops-secrets-operator/
helm upgrade --install sops sops/sops-secrets-operator \
--namespace sops -f azure_values.yamlcat <<EOF > azure_secret.yaml
kind: Secret
apiVersion: v1
metadata:
name: azure-sp-credentials
type: Opaque
stringData:
clientId: 9c325550-b264-4aee-ab6f-719771adda28
tenantId: 6ec4c881-32ee-4340-a456-d6ca65a42193
clientSecret: 'YOUR_CLIENT_SECRET'
EOF
cat <<EOF > azure_values.yaml
azure:
enabled: true
existingSecret: azure-sp-credentials
EOF
kubectl create namespace sops
kubectl apply -n sops -f azure_secret.yaml
helm repo add sops https://isindir.github.io/sops-secrets-operator/
helm upgrade --install sops sops/sops-secrets-operator \
--namespace sops -f azure_values.yamlPrometheus metrics (with optional prometheus-operator ServiceMonitor or VictoriaMetrics VMServiceScrape) are disabled by default, see chart Metrics documentation.
- create SopsSecret file, for example:
cat >jenkins-secrets.yaml <<EOF
apiVersion: isindir.github.com/v1alpha3
kind: SopsSecret
metadata:
name: example-sopssecret
spec:
# suspend reconciliation of the sops secret object
suspend: false
secretTemplates:
- name: my-secret-name-1
labels:
label1: value1
annotations:
key1: value1
stringData:
data-name0: data-value0
data:
data-name1: ZGF0YS12YWx1ZTE=
- name: jenkins-secret
labels:
"jenkins.io/credentials-type": "usernamePassword"
annotations:
"jenkins.io/credentials-description": "credentials from Kubernetes"
stringData:
username: myUsername
password: 'Pa$$word'
- name: some-token
stringData:
token: Wb4ziZdELkdUf6m6KtNd7iRjjQRvSeJno5meH4NAGHFmpqJyEsekZ2WjX232s4Gj
- name: docker-login
type: 'kubernetes.io/dockerconfigjson'
stringData:
.dockerconfigjson: '{"auths":{"index.docker.io":{"username":"imyuser","password":"mypass","email":"[email protected]","auth":"aW15dXNlcjpteXBhc3M="}}}'
EOF- Encrypt file using
sopsand AWS kms key:
sops encrypt \
--kms 'arn:aws:kms:<region>:<account>:alias/<key-alias-name>' \
--encrypted-suffix='Templates' jenkins-secrets.yaml \
> jenkins-secrets.enc.yamlor
sops encrypt \
--kms 'arn:aws:kms:<region>:<account>:alias/<key-alias-name>' \
--encrypted-regex='^(data)$' jenkins-secrets.yaml \
> jenkins-secrets.enc.yamlNOTE: after using regex
sops --encrypted-regexresulting file may be inapplicable to the kubernetes cluster, use this feature with care
or, to keep the values which are not secret in plain text:
sops encrypt \
--kms 'arn:aws:kms:<region>:<account>:alias/<key-alias-name>' \
--unencrypted-regex='^(apiVersion|kind|metadata|status|name|LOG_LEVEL|PORT)$' jenkins-secrets.yaml \
> jenkins-secrets.enc.yamlsops --unencrypted-regex encrypts all the values, except the values with a key that matches the
regex. Use this option to keep configuration which is not secret readable in git, and to show the
changes to that configuration in a diff. A new key is encrypted by default.
NOTE: the regex must also match
apiVersion,kind,metadata,statusandname. Kubernetes adds fields to the resource when it stores the resource, for examplemetadata.uidandmetadata.resourceVersion. These fields are in plain text. If the regex does not match them, the operator tries to decrypt them, and the reconciliation fails.
A worked example. One application usually needs both secrets and configuration which is not
secret. This SopsSecret holds five environment variables for one deployment:
---
apiVersion: isindir.github.com/v1alpha3
kind: SopsSecret
metadata:
name: my-app
spec:
secretTemplates:
- name: my-app-env
stringData:
LOG_LEVEL: debug
WEB_CONCURRENCY: "4"
OTEL_EXPORTER_OTLP_ENDPOINT: https://otel.example.com:4318
DATABASE_URL: postgres://app:[email protected]:5432/app
OPENAI_API_KEY: sk-example-not-a-real-keyPut the rule in .sops.yaml, so that the allowlist is in one file which the team reviews, and
so that nobody has to repeat the flag:
---
creation_rules:
- path_regex: \.enc\.yaml$
unencrypted_regex: '^(apiVersion|kind|metadata|status|name|LOG_LEVEL|WEB_CONCURRENCY|OTEL_EXPORTER_OTLP_ENDPOINT)$'
kms: arn:aws:kms:<region>:<account>:alias/<key-alias-name>After sops encrypt, the first three values stay readable and the last two are cipher text. A
change to WEB_CONCURRENCY is then visible in the diff, and DATABASE_URL and OPENAI_API_KEY
stay protected. A new key which the regex does not match is encrypted, so the default is safe.
Values in stringData must stay quoted when they look like a number or a boolean, for example
WEB_CONCURRENCY: "4". Kubernetes only accepts strings in stringData.
See config/age-test-key/05-raw-test-secrets-unencrypted-regex.yaml and
config/age-test-key/05-test-secrets-unencrypted-regex.yaml for the same example before and
after encryption.
- Encrypt file using
sopsand GCP KMS key:
sops encrypt \
--gcp-kms 'projects/<project-name>/locations/<location>/keyRings/<keyring-name>/cryptoKeys/<key-name>' \
--encrypted-suffix='Templates' jenkins-secrets.yaml \
> jenkins-secrets.enc.yaml- Encrypt file using
sopsand Azure Keyvault key:
sops encrypt \
--azure-kv 'https://<vault-url>/keys/<key-name>/<key-version>' \
--encrypted-suffix='Templates' jenkins-secrets.yaml \
> jenkins-secrets.enc.yaml- Encrypt file using
sopsand PGP key:
sops encrypt \
--pgp '<pgp-finger-print>' \
--encrypted-suffix='Templates' jenkins-secrets.yaml \
> jenkins-secrets.enc.yamlNote: Multiple keys can be used to encrypt secrets. At the time of decryption access to one of these is needed. For more information see
sopsdocumentation.
Note: Files encrypted with
sops --mac-only-encryptedare supported. The resultingsops.mac_only_encrypted: truemetadata field is recognised by the CRD, so such resources pass strict server-side field validation when applied withkubectl applyorhelm upgrade.
If there is a need to re-own existing Secrets by SopsSecret, following annotation should
be added to the target kubernetes native secret:
---
metadata:
annotations:
"sopssecret/managed": "true"previously not managed secret will be replaced by
SopsSecretowned at the next rescheduled reconciliation event.
Please see document here: SopsSecret API and Operator Upgrade
Mozilla Public License Version 2.0
sops-secrets-operatoris not using standardsopslibrary decryption interface function, modified upstream function is used to decrypt data which ignoresencsignature field insopsmetadata. This means if some encrypted fields are removed or changed to plain text - it still will be able to decrypt the resource.This is due to the fact that when Kubernetes resource is applied it is always mutated by Kubernetes, for example resource version is generated and added to the resource. But any mutation invalidatessopsmetadataencfield and standard decryption function fails.sops-secrets-operatorby design is not wrapping encrypted object to some field in spec. This was deliberate decision for the simplicity of the operations - ability to directly encrypt the wholeSopsSecretresource usingsopscli. This causes side effects like: if the user of the k8s cluster (which runssops-secrets-operator) has RBAC access to read secrets in some namespace - it allows directly applying encryptedSopsSecretresource to that namespaces and getting access to the secret material. This operator was only designed to protect access to the secret material from git repository.sops-secrets-operatoris not strictly following Kubernetes OpenAPI naming conventions. This is due to the fact thatsopsgenerates substructures in encrypted file with incompatible to OpenAPI names (containing underscore symbols, where it should belowerCamelCasefor OpenAPI compatibility).
Projects and tools inspired development of sops-secrets-operator:
- sops
- kube2iam
- kiam - in ABANDONED mode now
- Flux GitOps CD - flux supports
sopsout of the box - Jenkins Configuration as Code
- Bitnami SealedSecrets
- kubebuilder
- Sops Operator (Peak Scale)
- Kubernetes external secrets
- Vault Secrets Operator
- Sealed Secrets
- Secrets Store CSI driver
- Kamus
- Tesoro
- Sops Operator (Craftypath)