Created
September 14, 2023 06:21
-
-
Save curx/dd65dfd81428424e516dc4ccab6ba613 to your computer and use it in GitHub Desktop.
helper for sops for fluxcd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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