هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
Know it before you install
SOPS makes a random data key per file, encrypts the values with it (AES-256-GCM), and encrypts the data key for every recipient (age public keys, KMS keys). Anyone with one of the recipient private keys can decrypt. A MAC protects the whole file from changes.
Install on RHEL and Ubuntu
dnf -y install age
apt-get install -y age
SVER=v3.11.0
curl -fsSLo sops https://github.com/getsops/sops/releases/download/$SVER/sops-$SVER.linux.amd64
install -m 0755 sops /usr/local/bin/sops
sops --version(age on RHEL comes from EPEL: dnf -y install epel-release first.)
First configuration: keys and rules
Make your personal age key (once per person / per CI system):
mkdir -p ~/.config/sops/age
age-keygen -o ~/.config/sops/age/keys.txt
Public key: age1qyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqs3290gq
chmod 600 ~/.config/sops/age/keys.txtRules in the repo: which files, which fields, which recipients. Production secrets get different (fewer) recipients than dev:
creation_rules:
- path_regex: k8s/prod/.*\.ya?ml$
encrypted_regex: ^(data|stringData)$
age: >-
age1prodcluster0000000000000000000000000000000000000000000000,
age1alice00000000000000000000000000000000000000000000000000
kms: arn:aws:kms:eu-central-1:123456789012:alias/sops-prod
- path_regex: k8s/dev/.*\.ya?ml$
encrypted_regex: ^(data|stringData)$
age: >-
age1devcluster00000000000000000000000000000000000000000000000,
age1alice00000000000000000000000000000000000000000000000000,
age1bob000000000000000000000000000000000000000000000000000000
- path_regex: \.env\.enc$
age: age1alice00000000000000000000000000000000000000000000000000| Part | Why |
|---|---|
path_regex | Different recipients per environment or folder. |
encrypted_regex: ^(data|stringData)$ | Only Secret values are encrypted; names and metadata stay readable for review. |
| Several recipients | Each person/cluster/CI decrypts with its own key; remove one person without changing others. |
kms | A cloud KMS key as a recipient — access by IAM, with audit logs. |
Encrypt, edit and decrypt
kubectl create secret generic db --from-literal=password='S3cret-long' --dry-run=client -o yaml > k8s/prod/db-secret.yaml
sops encrypt -i k8s/prod/db-secret.yaml
cat k8s/prod/db-secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: db
data:
password: ENC[AES256_GCM,data:3Jf…,iv:…,tag:…,type:str]
sops:
age:
- recipient: age1prodcluster…
…
sops edit k8s/prod/db-secret.yaml
sops decrypt k8s/prod/db-secret.yaml | kubectl apply -f -
sops decrypt --extract '["data"]["password"]' k8s/prod/db-secret.yaml
sops exec-env .env.enc './deploy.sh'sops edit opens the decrypted file in your editor and encrypts it again when you save. exec-env gives the values to one command as environment variables — they never touch the disk.
Readable diffs in Git:
In CI and Kubernetes
- CI: store the CI system's age private key as a masked secret
SOPS_AGE_KEY(or give the job a cloud role with KMS decrypt); thensops decryptworks in the job. - Flux: native support — store the age key in a Secret (
sops-age) and setdecryption: {provider: sops}on the Kustomization (see the Flux guide). - Argo CD: with a plugin (KSOPS or helm-secrets) in the repo server.
Rotate keys and remove people
When someone leaves: remove their key from .sops.yaml, re-encrypt the data key for the new recipient list, and rotate the secrets they could read:
updatekeys changes who can decrypt; rotate makes a new data key. The secret values must still be changed at the source (new DB password) — the person may have copied them.
Real uses at work:
- Kubernetes Secrets in GitOps repos (Flux, Argo CD) without plain text in Git.
- App config and
.envfiles per environment, reviewed in pull requests. - Terraform variables (
.tfvars) with secrets — decrypted only in the pipeline.
Make it safe
Security checklist:
- Private keys never in Git;
keys.txtmode600; CI keys as masked secrets. - Separate recipients (and KMS keys) for prod and non-prod.
encrypted_regexso only secret fields are encrypted — but check that no secret is outside them.- Never commit a decrypted file; add patterns like
*.dec.yamlto.gitignore. - Rotate the real secret values when access changes; keep a backup of at least one admin key or KMS access.
- Combine with Gitleaks to catch plain-text secrets.
Update and remove
Download the new binary and replace /usr/local/bin/sops. Old encrypted files stay readable by newer versions. Remove: rm /usr/local/bin/sops (keep your keys if files still need decrypting).
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
error loading config: no matching creation rules found | The file path does not match any path_regex. | Fix the regex or the file name; run from the repo root. |
Failed to get the data key required to decrypt the SOPS file | None of your keys is a recipient (or KMS access missing). | Check SOPS_AGE_KEY_FILE/keys.txt; ask someone to add your key and run updatekeys. |
MAC mismatch | The encrypted file was changed by hand. | Use sops edit; restore the file from Git and edit again. |
| Values not encrypted | Fields do not match encrypted_regex. | Adjust the regex (for example add password). |
| Unreadable diffs in pull requests | Encrypted values change every time. | Use the sopsdiffer textconv locally; review keys and metadata in the PR. |
Next steps
- Quick how-tos: Create a Kubernetes Secret safely, Encrypt a file with GPG or age, Encrypt Kubernetes secrets in Git.
- Command page: sops.
- At work: SOPS — encrypted secrets in Git, age — simple file encryption.
- GitOps with SOPS: Flux CD, GitOps secrets. A central secret store: Vault.
1What does SOPS encrypt in a YAML file — the keys, the values, or both?
Only the values (or the ones matching encrypted_regex); the keys stay readable.
2A teammate leaves. Is sops updatekeys enough?
No — it stops future decryption, but you must also change the real secret values they could read.