Tag 6: Shamir – Key Groups
Ziel: Du verschlüsselst ein Notfall-Geheimnis so, dass es nur zwei von drei Schlüsselhaltern gemeinsam öffnen können, ersetzt einen verlorenen Schlüssel und kennst die Grenzen von
updatekeys. · Dauer: ca. 60 min · Voraussetzung: Tag 5
Briefing
- Bisher reichte ein passender Schlüssel. Mit Key Groups teilt sops den Datenschlüssel nach Shamir in Anteile auf: ein Anteil pro Gruppe.
shamir_threshold: 2heißt: Zwei Gruppen müssen ihren Anteil liefern. Innerhalb einer Gruppe reicht irgendein Schlüssel.- Einsatz: Root-Passwörter, Unseal-Keys, Notfallzugänge – „Vier-Augen-Prinzip“ in Kryptografie statt in einer Richtlinie.
Datenschlüssel ──Shamir 2 von 3──┬─ Anteil 1 ─► Gruppe 0: anna (+ später ich)
├─ Anteil 2 ─► Gruppe 1: ben
└─ Anteil 3 ─► Gruppe 2: cleo
Entschlüsseln: 2 beliebige Anteile ─► Datenschlüssel ─► Werte
Drill 1: Drei Schlüsselhalter und eine Regel
# auf argo
mkdir -p ~/sops/tag06 && cd ~/sops/tag06
for n in anna ben cleo; do age-keygen -o $n.key 2>&1; done
A=$(age-keygen -y anna.key); B=$(age-keygen -y ben.key); C=$(age-keygen -y cleo.key)
cd ~/sops/repo
cat >> .sops.yaml <<EOF
- path_regex: ^tresor/.*\.yaml$
shamir_threshold: 2
key_groups:
- age: [$A]
- age: [$B]
- age: [$C]
EOF
tail -6 .sops.yaml | cut -c1-60Erwartet:
Public key: age14w75l5awdfnwylp6nrvrm92f037t3wdcg6chualy5tjrfqseqq8q85wa4n
Public key: age1z32l8zxdjjgc97375gfzvtf74976lh373e6fpagzyp58du7865useagh79
Public key: age1s4a2qerngelfdr3eg4j4quz9w2xzfvgans2u7dy2u69vehqdn9ssa3yuef
- path_regex: ^tresor/.*\.yaml$
shamir_threshold: 2
key_groups:
- age: [age14w75l5awdfnwylp6nrvrm92f037t3wdcg6chualy5t
- age: [age1z32l8zxdjjgc97375gfzvtf74976lh373e6fpagzyp
- age: [age1s4a2qerngelfdr3eg4j4quz9w2xzfvgans2u7dy2u6
key_groups ersetzt in einer Regel das einfache
age:. In einer Gruppe dürfen verschiedene Schlüsselarten
gemischt stehen (age, PGP, Transit aus Tag 7).
Fortschritt speichern? Kostenlos registrieren
Drill 2: Verschlüsseln und ansehen
# auf argo
cd ~/sops/repo
mkdir -p tresor
printf 'root_password: lab-root-123\n' > tresor/notfall.yaml
sops encrypt -i tresor/notfall.yaml
yq '.sops | keys' tresor/notfall.yaml
yq '.sops.shamir_threshold, (.sops.key_groups | length)' tresor/notfall.yaml
yq '.sops.key_groups[].age[].recipient' tresor/notfall.yaml | cut -c1-30Erwartet:
- key_groups
- lastmodified
- mac
- shamir_threshold
- unencrypted_suffix
- version
2
3
age14w75l5awdfnwylp6nrvrm92f03
age1z32l8zxdjjgc97375gfzvtf749
age1s4a2qerngelfdr3eg4j4quz9w2
Statt age: direkt unter sops: steht jetzt
key_groups. Jeder enc-Eintrag enthält nicht
mehr den ganzen Datenschlüssel, sondern nur einen Anteil.
Fortschritt speichern? Kostenlos registrieren
Drill 3: Ein Schlüssel reicht nicht
# auf argo
cd ~/sops/repo
SOPS_AGE_KEY_FILE=~/sops/tag06/anna.key sops decrypt tresor/notfall.yaml 2>&1 \
| grep -E '^Group|^ age1' | cut -c1-40
SOPS_AGE_KEY_FILE=~/sops/tag06/anna.key sops decrypt tresor/notfall.yaml 2>&1 \
| tail -4Erwartet:
Group 0: SUCCESS
Group 1: FAILED
age1z32l8zxdjjgc97375gfzvtf74976lh37
Group 2: FAILED
age1s4a2qerngelfdr3eg4j4quz9w2xzfvga
2, but only 1 part(s) were successfully recovered, one for each successful
key group. In order for SOPS to recover the file, at least 2 groups have to
be successful. In order for a group to be successful, decryption has to
succeed with any of the keys in that key group.
Die Meldung ist ungewöhnlich hilfreich: Schwellwert, Zahl der erfolgreichen Gruppen und die Regel stehen da. Anna allein kommt nicht weiter – auch nicht du mit deinem eigenen Schlüssel, der in keiner Gruppe steht.
Fortschritt speichern? Kostenlos registrieren
Drill 4: Zwei aus drei
Eine Identity-Datei darf mehrere Schlüssel enthalten – im echten
Leben säßen Anna und Cleo zusammen, oder einer reicht dem anderen seinen
Anteil über einen keyservice (Tag 8).
# auf argo
cd ~/sops/repo
cat ~/sops/tag06/anna.key ~/sops/tag06/cleo.key > ~/sops/tag06/anna-cleo.txt
SOPS_AGE_KEY_FILE=~/sops/tag06/anna-cleo.txt sops decrypt tresor/notfall.yaml
SOPS_AGE_KEY="$(grep -h AGE-SECRET ~/sops/tag06/ben.key ~/sops/tag06/cleo.key)" \
sops decrypt tresor/notfall.yamlErwartet:
root_password: lab-root-123
root_password: lab-root-123
SOPS_AGE_KEY nimmt ebenfalls mehrere Zeilen. Jede
Kombination aus zwei der drei Gruppen öffnet die Datei.
Fortschritt speichern? Kostenlos registrieren
Drill 5: Eine Gruppe mit zwei Mitgliedern
Du sollst Annas Rolle vertreten dürfen: Du kommst in ihre Gruppe. Dann zählt dein Schlüssel als Anteil 1 – nicht als zusätzlicher Anteil.
# auf argo
cd ~/sops/repo
ICH=$(age-keygen -y ~/.config/sops/age/keys.txt)
yq -i '(.creation_rules[] | select(.path_regex == "^tresor/.*\\.yaml$")
| .key_groups[0].age) += ["'$ICH'"]' .sops.yaml
SOPS_AGE_KEY_FILE=~/sops/tag06/anna-cleo.txt sops updatekeys -y \
tresor/notfall.yaml 2>&1 | grep -vE '^20'
yq '.sops.key_groups[0].age[].recipient' tresor/notfall.yaml | cut -c1-30
SOPS_AGE_KEY_FILE=~/sops/tag06/ben.key sops decrypt tresor/notfall.yamlErwartet:
The following changes will be made to the file's groups:
shamir_threshold: 2
Group 1
age14w75l5awdfnwylp6nrvrm92f037t3wdcg6chualy5tjrfqseqq8q85wa4n
+++ age1rnz4tqupd0f3lm4kfs564cfgys5j80497rk7ajzn2ekxh3d2g3uqdtdnv2
Group 2
age1z32l8zxdjjgc97375gfzvtf74976lh373e6fpagzyp58du7865useagh79
Group 3
age1s4a2qerngelfdr3eg4j4quz9w2xzfvgans2u7dy2u69vehqdn9ssa3yuef
age14w75l5awdfnwylp6nrvrm92f03
age1rnz4tqupd0f3lm4kfs564cfgys
root_password: lab-root-123
updatekeys braucht bei Key Groups zwei
Anteile, um den Datenschlüssel neu zu verteilen – deshalb Anna und Cleo.
Danach reichen du (aus keys.txt, Gruppe 0) und Ben (Gruppe
1). Die Gruppen zählt die Ausgabe von updatekeys ab 1, die
Fehlermeldung beim Entschlüsseln ab 0.
Fortschritt speichern? Kostenlos registrieren
Drill 6: Ein Schlüssel ist verloren
Cleos Laptop ist weg. Mit zwei von drei Anteilen ist das kein Notfall: Dora übernimmt Cleos Gruppe.
# auf argo
cd ~/sops/repo
age-keygen -o ~/sops/tag06/dora.key 2>&1
C=$(age-keygen -y ~/sops/tag06/cleo.key); D=$(age-keygen -y ~/sops/tag06/dora.key)
sed -i "s/$C/$D/" .sops.yaml
SOPS_AGE_KEY_FILE=~/sops/tag06/ben.key sops updatekeys -y tresor/notfall.yaml 2>&1 \
| grep -E '^(Group 3|\+\+\+|---)'
SOPS_AGE_KEY_FILE=~/sops/tag06/ben.key sops rotate -i tresor/notfall.yaml
cat ~/sops/tag06/ben.key ~/sops/tag06/dora.key > ~/sops/tag06/ben-dora.txt
XDG_CONFIG_HOME=/nirgends SOPS_AGE_KEY_FILE=~/sops/tag06/ben-dora.txt \
sops decrypt tresor/notfall.yamlErwartet:
Public key: age1fylg8z028cgs2z47pnntwrwhu2fsc8nklzcfy2r7ckknvm9z2gfshkr0ej
Group 3
+++ age1fylg8z028cgs2z47pnntwrwhu2fsc8nklzcfy2r7ckknvm9z2gfshkr0ej
--- age1s4a2qerngelfdr3eg4j4quz9w2xzfvgans2u7dy2u69vehqdn9ssa3yuef
root_password: lab-root-123
Wer findet Cleos Laptop? Mit Cleos Schlüssel allein ist nichts
anzufangen, und nach rotate passen ihre alten Anteile auch
nicht mehr zum neuen Datenschlüssel. Das Geheimnis selbst wechselst du
trotzdem, wenn der Verlust ein Diebstahl war (Tag 5).
# auf argo
cd ~/sops/repo
git add .sops.yaml tresor
git commit -q -m "tag06: Notfall-Tresor 2 von 3" && git push -q
git log --oneline | head -1Erwartet:
556364c tag06: Notfall-Tresor 2 von 3
Fortschritt speichern? Kostenlos registrieren
Übung: Speed-Drills
- Zeig Schwellwert und Zahl der Gruppen von
tresor/notfall.yaml. Zeitziel: 15 s - Öffne die Datei mit dir und Dora. Zeitziel: 30 s
Lösung
# auf argo
cd ~/sops/repo
yq '.sops.shamir_threshold, (.sops.key_groups | length)' tresor/notfall.yaml
SOPS_AGE_KEY_FILE=~/sops/tag06/dora.key sops decrypt tresor/notfall.yamlErwartet:
2
3
root_password: lab-root-123
Dein Schlüssel kommt automatisch aus keys.txt dazu.
Fortschritt speichern? Kostenlos registrieren
Kontrollpunkt
# auf argo
cd ~/sops/repo && git status -sb
SOPS_AGE_KEY_FILE=~/sops/tag06/anna.key sops decrypt tresor/notfall.yaml 2>&1 \
| grep -c SUCCESSErwartet:
## main...origin/main
1
Anna allein (plus dein Schlüssel – beide in Gruppe 0) bringt nur eine erfolgreiche Gruppe zusammen.
Challenge
Die Revision verlangt „drei von drei“. Setze
shamir_threshold in .sops.yaml auf 3, bringe
tresor/notfall.yaml auf den neuen Schwellwert und weise
nach, dass Ben und Dora allein nicht mehr reichen. Stelle danach den
alten Zustand (2 von 3) wieder her.
Hinweis: updatekeys gleicht nur die
Schlüssel ab, nicht den Schwellwert – prüfe, was es meldet.
Lösung
# auf argo
cd ~/sops/repo
yq -i '(.creation_rules[] | select(.path_regex == "^tresor/.*\\.yaml$")
| .shamir_threshold) = 3' .sops.yaml
SOPS_AGE_KEY_FILE=~/sops/tag06/ben.key sops updatekeys -y tresor/notfall.yaml \
2>&1 | cut -c21-
yq '.sops.shamir_threshold' tresor/notfall.yaml
SOPS_AGE_KEY_FILE=~/sops/tag06/ben.key sops decrypt tresor/notfall.yaml \
| sops encrypt --filename-override tresor/notfall.yaml /dev/stdin > neu.yaml
mv neu.yaml tresor/notfall.yaml
yq '.sops.shamir_threshold' tresor/notfall.yaml
XDG_CONFIG_HOME=/nirgends SOPS_AGE_KEY_FILE=~/sops/tag06/ben-dora.txt \
sops decrypt tresor/notfall.yaml 2>&1 | grep -A1 Recovery
git checkout .sops.yaml tresor/notfall.yamlErwartet:
Syncing keys for file /home/ubuntu/sops/repo/tresor/notfall.yaml
File /home/ubuntu/sops/repo/tresor/notfall.yaml already up to date
2
3
Recovery failed because the file was encrypted with a Shamir threshold of
3, but only 2 part(s) were successfully recovered, one for each successful
Updated 2 paths from the index
Der Schwellwert steht in der Datei und ändert sich nur beim
Neuverschlüsseln – decrypt | encrypt mit
--filename-override, damit die Regel für
tresor/ greift. Ein updatekeys meldet „already
up to date“ und lässt dich im Glauben, alles sei erledigt.
Fortschritt speichern? Kostenlos registrieren
Interview-Notiz
- Wie erzwingst du ein Vier-Augen-Prinzip für ein Geheimnis?
sops Key Groups mit
shamir_threshold: Der Datenschlüssel wird geteilt, jede Gruppe hält einen Anteil, erst die Mindestzahl Anteile setzt ihn wieder zusammen. - Was passiert, wenn ein Schlüsselhalter ausfällt? Solange
genug Gruppen übrig sind, nichts; seine Gruppe wird per
.sops.yaml+updatekeysneu besetzt, danachrotate.
Enterprise-Notiz
- Jede Gruppe mit mindestens zwei Mitgliedern oder einem KMS-Schlüssel plus Person besetzen, sonst fällt mit einem Urlaub eine ganze Gruppe aus.
- Shamir ist für wenige, selten geöffnete Notfall-Dateien gedacht; für den Alltagsbetrieb (GitOps) ist ein einzelner Cluster-Schlüssel üblich.
- Schwellwert-Änderungen und Gruppenwechsel im Change-Prozess
dokumentieren und per
yq '.sops.shamir_threshold'in der CI prüfen –updatekeysübernimmt den Schwellwert nicht.