Skip to content
Hands-on IT IT Drills

Day 3: File formats and partial encryption

Goal: You encrypt JSON, dotenv, INI and binary files, put a Kubernetes Secret into Git so that both Kubernetes and humans can read it, and know everything the MAC protects. · Duration: approx. 60 min · Prerequisite: Day 2

Briefing

  • sops detects the format by the file extension: .yaml/.yml, .json, .env, .ini. Everything else is binary – the whole file becomes a single value.
  • The default is: every value is encrypted. For Kubernetes manifests that is wrong, because apiVersion and kind would then be ENC[…] too.
  • With --encrypted-regex, --unencrypted-regex, --encrypted-suffix and --unencrypted-suffix you decide which keys get encrypted.
  • By default the MAC covers the plaintext values too.
Format Extension Metadata lives in
YAML / JSON .yaml, .json key sops
dotenv .env lines sops_…=
INI .ini section [sops]
binary everything else JSON with data and sops

Drill 1: JSON

# on argo
mkdir -p ~/sops/tag03 && cd ~/sops/tag03
ICH=$(age-keygen -y ~/.config/sops/age/keys.txt)
printf '{"db": {"user": "notes", "password": "lab-db-123"}, "replicas": 2}\n' \
  > app.json
sops encrypt --age $ICH app.json > app.enc.json
jq -c '.db' app.enc.json | cut -c1-80
jq -r '.sops | keys | join(",")' app.enc.json
sops decrypt app.enc.json

Expected (abridged):

{"user":"ENC[AES256_GCM,data:tvzM/Mg=,iv:UaK7bjIQ1jcN5VXyK1/SJEP8EktFn6ARxqt+picPo
age,lastmodified,mac,unencrypted_suffix,version
{
    "db": {
        "user": "notes",
        "password": "lab-db-123"
    },
    "replicas": 2
}

The result is valid JSON – every tool that reads JSON can handle the file. sops indents JSON output with tabs.

Save your progress? Sign up for free

Drill 2: dotenv

# on argo
cd ~/sops/tag03
ICH=$(age-keygen -y ~/.config/sops/age/keys.txt)
printf 'DB_USER=notes\nDB_PASSWORD=lab-db-123\n# Kommentar\nAPI_KEY=lab-key-123\n' \
  > app.env
sops encrypt --age $ICH app.env > app.enc.env
cut -c1-70 app.enc.env
sops decrypt app.enc.env

Expected (abridged):

DB_USER=ENC[AES256_GCM,data:YUAeRZg=,iv:4PvR0O2Ch8MX0DrJOAonUHFmFCSphp
DB_PASSWORD=ENC[AES256_GCM,data:Dlz0eu0wxhSmaA==,iv:aeesIqbYJy99pA/KAO
#ENC[AES256_GCM,data:K75BvcftNiyLfw==,iv:HjKRBkUOhG1MKaExOTIfDTF3SXhgJ
API_KEY=ENC[AES256_GCM,data:PezolV7zPm7vBHY=,iv:RiLrIt1sFdHLGjU5P7klF4
sops_age__list_0__map_enc=-----BEGIN AGE ENCRYPTED FILE-----\nYWdlLWVu
sops_age__list_0__map_recipient=age126j96yd6te036uya83l76w0afmlaj9vyyl
sops_lastmodified=2026-11-20T16:19:37Z
sops_mac=ENC[AES256_GCM,data:63U64qO/yHukE4DRGVylDrP46RXWCwOnQVlGPGJpJ
sops_unencrypted_suffix=_unencrypted
sops_version=3.13.3
DB_USER=notes
DB_PASSWORD=lab-db-123
# Kommentar
API_KEY=lab-key-123

dotenv is flat: the structure of the sops block is translated into key names such as sops_age__list_0__map_enc. Variable names starting with sops_ are therefore off-limits in dotenv files.

Save your progress? Sign up for free

Drill 3: INI

# on argo
cd ~/sops/tag03
ICH=$(age-keygen -y ~/.config/sops/age/keys.txt)
printf '[db]\nuser = notes\npassword = lab-db-123\n\n[api]\nkey = lab-key-123\n' \
  > app.ini
sops encrypt --age $ICH app.ini > app.enc.ini
cut -c1-70 app.enc.ini | head -12

Expected (abridged):

[db]
user     = ENC[AES256_GCM,data:MUEhFzg=,iv:5bOQNpJgEUuGcWfzVucBsWpPeS9
password = ENC[AES256_GCM,data:uIq7ggyA2dyu9Q==,iv:DHZLwehtA99sclAE/hw

[api]
key = ENC[AES256_GCM,data:8JwsKoRMnElqi34=,iv:ULlHt2HqwVlYTiezwMMry+tG

[sops]
age__list_0__map_enc       = -----BEGIN AGE ENCRYPTED FILE-----\nYWdlL
age__list_0__map_recipient = age126j96yd6te036uya83l76w0afmlaj9vyyluxr
lastmodified               = 2026-11-20T16:19:37Z
mac                        = ENC[AES256_GCM,data:25FLqRfd7+bzkx8+lP8U0

Sections are preserved, sops aligns the equals signs.

Save your progress? Sign up for free

Drill 4: Binary files and wrong extensions

A private key, a keystore, a certificate bundle – not everything is YAML.

# on argo
cd ~/sops/tag03
ICH=$(age-keygen -y ~/.config/sops/age/keys.txt)
head -c 32 /dev/urandom > tls.key.bin
sha256sum tls.key.bin | cut -c1-20
sops encrypt --age $ICH tls.key.bin > tls.key.bin.enc
jq -r 'keys | join(",")' tls.key.bin.enc
sops decrypt tls.key.bin.enc | sha256sum | cut -c1-20

Expected:

b4a860095c46c9cc5940
data,sops
b4a860095c46c9cc5940

The checksum is different for you (random data), but the same before and after. The whole file sits base64-encoded and encrypted in the field data, the wrapper is JSON. Decryption returns the original file byte for byte.

It gets tricky when the extension lies: sops treats a dotenv file named app.txt as binary – then --encrypted-regex does not help either, and exec-env (Day 8) finds no variables.

# on argo
cd ~/sops/tag03
ICH=$(age-keygen -y ~/.config/sops/age/keys.txt)
cp app.env app.txt
sops encrypt --age $ICH app.txt | head -2 | cut -c1-40
sops encrypt --age $ICH --input-type dotenv --output-type dotenv app.txt \
  | head -2 | cut -c1-40

Expected:

{
    "data": "ENC[AES256_GCM,data:dJqXZMG
DB_USER=ENC[AES256_GCM,data:aK9UOSA=,iv
DB_PASSWORD=ENC[AES256_GCM,data:loMtTG5

Save your progress? Sign up for free

Drill 5: A Kubernetes Secret

# on argo
cd ~/sops/tag03
ICH=$(age-keygen -y ~/.config/sops/age/keys.txt)
kubectl create secret generic notes-db -n notes --from-literal=password=lab-db-123 \
  --from-literal=user=notes --dry-run=client -o yaml > secret.yaml
cat secret.yaml
sops encrypt --age $ICH secret.yaml | head -9 | cut -c1-60

Expected:

apiVersion: v1
data:
  password: bGFiLWRiLTEyMw==
  user: bm90ZXM=
kind: Secret
metadata:
  name: notes-db
  namespace: notes
apiVersion: ENC[AES256_GCM,data:yig=,iv:BPOTXVVBtDpEvmIZvHQm
data:
    password: ENC[AES256_GCM,data:9jmgiihVRoRQXp80KJ50DQ==,i
    user: ENC[AES256_GCM,data:vDOIOZteW0k=,iv:EXTpHgNVtyRnL5
kind: ENC[AES256_GCM,data:Jas9+224,iv:ivl+IR7fblQ+y9psvYe1hb
metadata:
    name: ENC[AES256_GCM,data:5p4O21censU=,iv:CdnXSHbqhQtloCBSM
    namespace: ENC[AES256_GCM,data:BvrfHTc=,iv:2al6/MkPOJeYbSqaA

Base64 in data is not encryption (bGFiLWRiLTEyMw== is lab-db-123). And fully encrypted, nobody knows any more that this is a Secret named notes-db – neither a human in review nor Kustomize or Flux. Only the values should disappear:

# on argo
cd ~/sops/tag03
ICH=$(age-keygen -y ~/.config/sops/age/keys.txt)
sops encrypt --age $ICH --encrypted-regex '^(data|stringData)$' secret.yaml \
  > secret.enc.yaml
grep -v '^ \{8,\}' secret.enc.yaml | cut -c1-70

Expected:

apiVersion: v1
data:
    password: ENC[AES256_GCM,data:bjhTuZ68EMdXS2dRORMjFg==,iv:JbIY3N33
    user: ENC[AES256_GCM,data:g1EXKsm1WQo=,iv:g5bUDs8iEQkN0+fAJH6DHPt8
kind: Secret
metadata:
    name: notes-db
    namespace: notes
sops:
    age:
    encrypted_regex: ^(data|stringData)$
    lastmodified: "2026-11-20T16:19:51Z"
    mac: ENC[AES256_GCM,data:cwZGVZvTL9lN5xAwPVgipTaNT6T5HIjSDgCxqrkbt
    version: 3.13.3

encrypted_regex applies to key names: everything under data and stringData gets encrypted, the rest stays readable. The rule is stored in the sops block so that later changes with sops edit apply it again.

Save your progress? Sign up for free

Drill 6: Into the cluster – but decrypted

Break it: Apply the encrypted file directly.

# on argo
cd ~/sops/tag03
kubectl create namespace notes
kubectl apply -f secret.enc.yaml 2>&1 | cut -c1-88

Expected (abridged):

namespace/notes created
Error from server (BadRequest): error when creating "secret.enc.yaml": Secret in versi

Investigate:

# on argo
cd ~/sops/tag03
kubectl apply -f secret.enc.yaml 2>&1 | grep -o 'cannot be handled.*'

Expected:

cannot be handled as a Secret: illegal base64 data at input byte 3

Cause: Kubernetes expects base64 in data. ENC[AES2… is not valid base64 (the square bracket at position 3). With stringData it would be worse: the ciphertext would be stored as the password without complaint. And Kubernetes does not know the block sops: at all.

Fix and verify:

# on argo
cd ~/sops/tag03
sops decrypt secret.enc.yaml | kubectl apply -f -
kubectl -n notes get secret notes-db -o jsonpath='{.data.password}' | base64 -d; echo

Expected:

secret/notes-db created
lab-db-123

The plaintext flows through a pipe straight to kubectl, without a file. From Day 10 on, Flux and Argo CD do exactly that for you.

Save your progress? Sign up for free

Drill 7: Suffixes instead of regex

For your own configuration files, a naming convention is often more readable than a regex.

# on argo
cd ~/sops/tag03
ICH=$(age-keygen -y ~/.config/sops/age/keys.txt)
printf 'db:\n  host_unencrypted: postgres.notes.svc\n  password: lab-db-123\n' \
  > suffix.yaml
sops encrypt --age $ICH suffix.yaml | head -3 | cut -c1-60
printf 'db:\n  host: postgres.notes.svc\n  password_enc: lab-db-123\n' > suffix2.yaml
sops encrypt --age $ICH --encrypted-suffix _enc suffix2.yaml | head -3 | cut -c1-60

Expected:

db:
    host_unencrypted: postgres.notes.svc
    password: ENC[AES256_GCM,data:yO7kkmH560uG1g==,iv:FeA5YM
db:
    host: postgres.notes.svc
    password_enc: ENC[AES256_GCM,data:dZrB58543ElSWA==,iv:IC
Option Effect
--unencrypted-suffix _unencrypted default: everything except *_unencrypted
--encrypted-suffix _enc encrypt only *_enc
--unencrypted-regex REGEX everything except matching keys
--encrypted-regex REGEX only matching keys (and everything below them)

Exactly one of these options applies per file; sops rejects several at once.

Save your progress? Sign up for free

Drill 8: The MAC protects the plaintext too

Break it: During review, someone “just quickly” changes the namespace – a plaintext field.

# on argo
cd ~/sops/tag03
cp secret.enc.yaml umgezogen.yaml
sed -i 's/namespace: notes/namespace: prod/' umgezogen.yaml

Symptom:

# on argo
cd ~/sops/tag03
sops decrypt umgezogen.yaml 2>&1 | cut -c1-60

Expected (abridged):

MAC mismatch. File has 21C0FDDCD6AADA7383A6DD306348120D2C68F

Cause: The MAC is computed over all values, including the unencrypted ones. Whoever changes namespace without using sops breaks the checksum. That is intentional: otherwise an attacker could quietly redirect an encrypted Secret into another namespace.

Fix: Always make changes through sops – sops set or sops edit. If the damage is already done and the change is wanted, you decrypt once with --ignore-mac and encrypt again:

# on argo
cd ~/sops/tag03
sops decrypt --ignore-mac umgezogen.yaml > /dev/null && echo lesbar
sops set umgezogen.yaml '["metadata"]["namespace"]' '"prod"' 2>&1 | cut -c1-30

Expected (lesbar means “readable”):

lesbar
MAC mismatch. File has 21C0FDD

sops set checks the MAC first too. That leaves only the way through the plaintext:

# on argo
cd ~/sops/tag03
sops decrypt --ignore-mac umgezogen.yaml \
  | sops encrypt --filename-override umgezogen.yaml \
    --age $(age-keygen -y ~/.config/sops/age/keys.txt) \
    --encrypted-regex '^(data|stringData)$' /dev/stdin > neu.yaml
mv neu.yaml umgezogen.yaml
sops decrypt umgezogen.yaml | grep namespace

Verify – Expected:

    namespace: prod

--filename-override tells sops which format standard input has. When to limit the MAC to the encrypted values (mac_only_encrypted) is shown on Day 4.

Save your progress? Sign up for free

Exercise: Speed Drills

  1. Encrypt a Secret notes-api with stringData (key: lab-key-123) so that only stringData is encrypted, and apply it. Target time: 60 s
  2. Show the plaintext of the INI file. Target time: 10 s

Solution

# on argo
cd ~/sops/tag03
cat > str.yaml <<'EOF'
apiVersion: v1
kind: Secret
metadata: {name: notes-api, namespace: notes}
stringData:
  key: lab-key-123
EOF
sops encrypt --age $(age-keygen -y ~/.config/sops/age/keys.txt) \
  --encrypted-regex '^(data|stringData)$' str.yaml > str.enc.yaml
sops decrypt str.enc.yaml | kubectl apply -f -
kubectl -n notes get secret notes-api -o jsonpath='{.data.key}' | base64 -d; echo
sops decrypt app.enc.ini

Expected:

secret/notes-api created
lab-key-123
[db]
user     = notes
password = lab-db-123

[api]
key = lab-key-123

Save your progress? Sign up for free

Checkpoint

# on argo
cd ~/sops/tag03
for f in app.enc.json app.enc.env app.enc.ini tls.key.bin.enc secret.enc.yaml; do
  sops filestatus $f; done
kubectl -n notes get secrets

Expected:

{"encrypted":true}
{"encrypted":true}
{"encrypted":true}
{"encrypted":true}
{"encrypted":true}
NAME        TYPE     DATA   AGE
notes-api   Opaque   1      40s
notes-db    Opaque   2      3m

Challenge

From the encrypted dotenv file app.enc.env, create a Secret notes-env in the namespace notes with the three variables as keys – without the plaintext landing on disk.

Hint: kubectl create secret generic knows --from-env-file, and Bash can pass a command’s output as a file.

Solution

# on argo
cd ~/sops/tag03
kubectl -n notes create secret generic notes-env \
  --from-env-file=<(sops decrypt app.enc.env) --dry-run=client -o yaml \
  | kubectl apply -f -
kubectl -n notes get secret notes-env -o jsonpath='{.data}' | jq -r 'keys[]'

Expected:

secret/notes-env created
API_KEY
DB_PASSWORD
DB_USER

<(…) is a process substitution: kubectl reads from a pipe under /dev/fd/ and ignores the comment line.

Save your progress? Sign up for free

Cleanup

# on argo
kubectl delete namespace notes
cd ~/sops/tag03
rm -f app.json app.env app.ini app.txt tls.key.bin secret.yaml str.yaml \
  suffix.yaml suffix2.yaml
ls -1

Expected:

namespace "notes" deleted
app.enc.env
app.enc.ini
app.enc.json
secret.enc.yaml
str.enc.yaml
tls.key.bin.enc
umgezogen.yaml

The encrypted practice files may stay; no later day needs anything from ~/sops/tag03, and the namespace notes is gone again.

Interview Note

  • How do you encrypt a Kubernetes Secret with sops? Only data/stringData (encrypted_regex: ^(data|stringData)$), so that apiVersion, kind and metadata stay readable; what gets applied is the decrypted content.
  • Why does the MAC fail although only a plaintext field was changed? By default the MAC covers all values, so that encrypted values cannot be moved unnoticed into another context.

Enterprise Note

  • Set a format rule per repo (e.g. only YAML, only *.enc.yaml); otherwise a wrong extension quickly turns into a binary file nobody can review.
  • Binary files (keystores, certificate bundles) are opaque in a diff – better store them as PEM text in YAML.
  • encrypted_regex belongs in .sops.yaml (Day 4), not in the developers’ hands.