Skip to content

Instantly share code, notes, and snippets.

@curx
Created September 14, 2023 06:21
Show Gist options
  • Select an option

  • Save curx/dd65dfd81428424e516dc4ccab6ba613 to your computer and use it in GitHub Desktop.

Select an option

Save curx/dd65dfd81428424e516dc4ccab6ba613 to your computer and use it in GitHub Desktop.
helper for sops for fluxcd
#!/usr/bin/env bash
## desc: create sops key for fluxcd and more
## author: Thorsten Schifferdecker https://github.com/curx
## license: Apache 2.0
## variables
HASH="$(xxd -l5 -ps /dev/urandom)"
# the keyname and comment
KEY_NAME="${1:-iac-$HASH}"
KEY_COMMENT="sops decrypter for ${KEY_NAME}"
echo "$0 - create sops key"
# create key
gpg --batch --full-generate-key <<EOF
%no-protection
Key-Type: 1
Key-Length: 4096
Subkey-Type: 1
Subkey-Length: 4096
Expire-Date: 0
Name-Comment: $KEY_COMMENT
Name-Real: $KEY_NAME
EOF
# export sops gnupg key
gpg --export-secret-keys --armor ${KEY_NAME} | \
tee ${KEY_NAME}.priv.asc | \
kubectl create secret generic sops-key \
--namespace=flux-system \
--from-file=sops.asc=/dev/stdin \
--dry-run=client -o yaml > ./secret-sops-key.yaml
# export the public
gpg --export --armor ${KEY_NAME} \
> ./sops.pub.asc
# get fingerprint
KEY_FINGERPRINT="$(gpg --show-keys --with-colons ./sops.pub.asc | awk -F':' '/^fpr/ { print $10 }' | head -1)"
# remove gpg-dir
# gpg --delete-secret-and-public-key ${KEYNAME}
# create sops config
cat <<EOF > ./.sops.yaml
creation_rules:
- path_regex: .*.yaml
encrypted_regex: ^(data|stringData)$
pgp: ${KEY_FINGERPRINT}
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment