4 SITE_NAMESPACE="${SITE_NAMESPACE:-metal3}"
7 gpg --with-colons --list-secret-keys $1 | awk -F: '/fpr/ {print $10;exit}'
10 function create_gpg_key {
13 # Create an rsa4096 key that does not expire
14 gpg --batch --full-generate-key <<EOF
21 Name-Real: ${key_name}
25 function export_gpg_private_key {
26 gpg --export-secret-keys --armor "$(_gpg_key_fp $1)"
29 function sops_encrypt {
31 local -r yaml_dir=$(dirname ${yaml})
34 local -r key_fp=$(_gpg_key_fp ${key_name})
36 local site_dir=${yaml_dir}
37 if [[ $# -eq 3 ]]; then
41 # Commit the public key to the repository so that team members who
42 # clone the repo can encrypt new files
43 echo "Creating ${yaml_dir}/sops.pub.asc with public key used to encrypt secrets"
44 gpg --export --armor "${key_fp}" >${site_dir}/sops.pub.asc
46 # Add .sops.yaml so users won't have to worry about specifying the
47 # proper key for the target cluster or namespace
48 echo "Creating ${site_dir}/.sops.yaml SOPS configuration file"
49 encrypted_regex="(bmcPassword|ca-key.pem|decryptionSecret|hashedPassword|emcoPassword|rootPassword)"
50 cat <<EOF > ${site_dir}/.sops.yaml
53 encrypted_regex: ^${encrypted_regex}$
57 if [[ $(grep -c $(echo ${encrypted_regex} | sed -e 's/(/\\(/g' -e 's/|/\\|/g' -e 's/)/\\)/') ${yaml}) -ne 0 ]]; then
58 sops --encrypt --in-place --config=${site_dir}/.sops.yaml ${yaml}
62 function sops_decrypt {
64 local -r yaml_dir=$(dirname ${yaml})
65 local site_dir=${yaml_dir}
66 if [[ $# -eq 2 ]]; then
70 if [[ $(grep -c "^sops:" ${yaml}) -ne 0 ]]; then
71 sops --decrypt --in-place --config=${site_dir}/.sops.yaml ${yaml}
75 function flux_site_source_name {
78 echo $(basename ${url})-${branch}
81 function flux_site_kustomization_name {
85 echo $(flux_site_source_name ${url} ${branch})-site-$(basename ${path})
88 function flux_create_site {
94 local -r source_name=$(flux_site_source_name ${url} ${branch})
95 local -r kustomization_name=$(flux_site_kustomization_name ${url} ${branch} ${path})
96 local -r key_fp=$(gpg --with-colons --list-secret-keys ${key_name} | awk -F: '/fpr/ {print $10;exit}')
97 local -r secret_name="${key_name}-sops-gpg"
99 flux create source git ${source_name} --url=${url} --branch=${branch}
100 gpg --export-secret-keys --armor "$(_gpg_key_fp ${key_name})" |
101 kubectl -n flux-system create secret generic ${secret_name} --from-file=sops.asc=/dev/stdin --dry-run=client -o yaml |
103 kubectl create namespace ${SITE_NAMESPACE} --dry-run=client -o yaml | kubectl apply -f -
104 flux create kustomization ${kustomization_name} --target-namespace=${SITE_NAMESPACE} --path=${path} --source=GitRepository/${source_name} --prune=true \
105 --decryption-provider=sops --decryption-secret=${secret_name}