متقدم
البحث فيأدلة التثبيت
طريقة البحث
  • الصق رسالة الخطأ بين علامتي تنصيص، مثل "permission denied"، لتجد جدول الحل.
  • اختر مجالًا وجزءًا من الدليل (التثبيت، أول الإعدادات، الاستخدام اليومي، الأمان، الحلول) لتصل مباشرة إلى الخطوة الصحيحة.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

الدليل 73SOPS · Security

Install and set up SOPS (encrypted secrets in Git)

SOPS (Secrets OPerationS) encrypts the values in YAML, JSON, ENV and INI files and leaves the keys readable — so you can keep secrets in Git, review changes in pull requests, and decrypt only where needed (your laptop, CI, Flux or Argo CD). It works with age keys, PGP and cloud KMS (AWS, Azure, Google, Vault). This guide installs SOPS and age, writes a .sops.yaml with rules per folder, encrypts and edits files, uses them in CI and Kubernetes, and covers key rotation and fixes.

  • 10 الدروس
  • 11 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

73.1

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.

ItemValue
Programsops (from github.com/getsops/sops/releases; also .rpm/.deb)
Keysage (age-keygen), or KMS / PGP
Rules.sops.yaml in the repository root
Your age key~/.config/sops/age/keys.txt (or SOPS_AGE_KEY / SOPS_AGE_KEY_FILE)
FormatsYAML, JSON, dotenv, INI, binary

الأوامر في هذا الدرس

73.2

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.)

73.3

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.txt

Rules in the repo: which files, which fields, which recipients. Production secrets get different (fewer) recipients than dev:

.sops.yaml
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
PartWhy
path_regexDifferent recipients per environment or folder.
encrypted_regex: ^(data|stringData)$Only Secret values are encrypted; names and metadata stay readable for review.
Several recipientsEach person/cluster/CI decrypts with its own key; remove one person without changing others.
kmsA cloud KMS key as a recipient — access by IAM, with audit logs.

73.4

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:

echo '*.enc.yaml diff=sopsdiffer' >> .gitattributes
git config diff.sopsdiffer.textconv "sops decrypt"

73.5

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); then sops decrypt works in the job.
  • Flux: native support — store the age key in a Secret (sops-age) and set decryption: {provider: sops} on the Kustomization (see the Flux guide).
  • Argo CD: with a plugin (KSOPS or helm-secrets) in the repo server.

الأوامر في هذا الدرس

73.6

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:

sops updatekeys k8s/prod/db-secret.yaml
sops rotate -i k8s/prod/db-secret.yaml

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 .env files per environment, reviewed in pull requests.
  • Terraform variables (.tfvars) with secrets — decrypted only in the pipeline.

الأوامر في هذا الدرس

73.7

Make it safe

Security checklist:

  • Private keys never in Git; keys.txt mode 600; CI keys as masked secrets.
  • Separate recipients (and KMS keys) for prod and non-prod.
  • encrypted_regex so only secret fields are encrypted — but check that no secret is outside them.
  • Never commit a decrypted file; add patterns like *.dec.yaml to .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.

73.8

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).

الأوامر في هذا الدرس

73.9

Fix common problems

ProblemCauseFix
error loading config: no matching creation rules foundThe 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 fileNone 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 mismatchThe encrypted file was changed by hand.Use sops edit; restore the file from Git and edit again.
Values not encryptedFields do not match encrypted_regex.Adjust the regex (for example add password).
Unreadable diffs in pull requestsEncrypted values change every time.Use the sopsdiffer textconv locally; review keys and metadata in the PR.

الأوامر في هذا الدرس

73.10

Next steps

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.

الأوامر في هذا الدرس

كل الأوامر في هذا الفصل