083757006a1b725d2a123f7c223922591d62abe5
[icn.git] / deploy / site / common.sh
1 #!/usr/bin/env bash
2 set -eu -o pipefail
3
4 SITE_NAMESPACE="${SITE_NAMESPACE:-metal3}"
5
6 function _gpg_key_fp {
7     gpg --with-colons --list-secret-keys $1 | awk -F: '/fpr/ {print $10;exit}'
8 }
9
10 function create_gpg_key {
11     local -r key_name=$1
12
13     # Create an rsa4096 key that does not expire
14     gpg --batch --full-generate-key <<EOF
15 %no-protection
16 Key-Type: 1
17 Key-Length: 4096
18 Subkey-Type: 1
19 Subkey-Length: 4096
20 Expire-Date: 0
21 Name-Real: ${key_name}
22 EOF
23 }
24
25 function export_gpg_private_key {
26     gpg --export-secret-keys --armor "$(_gpg_key_fp $1)"
27 }
28
29 function sops_encrypt {
30     local -r yaml=$1
31     local -r key_name=$2
32
33     local -r yaml_dir=$(dirname ${yaml})
34     local -r key_fp=$(_gpg_key_fp ${key_name})
35
36     # Commit the public key to the repository so that team members who
37     # clone the repo can encrypt new files
38     echo "Creating ${yaml_dir}/sops.pub.asc with public key used to encrypt secrets"
39     gpg --export --armor "${key_fp}" >${yaml_dir}/sops.pub.asc
40
41     # Add .sops.yaml so users won't have to worry about specifying the
42     # proper key for the target cluster or namespace
43     echo "Creating ${yaml_dir}/.sops.yaml SOPS configuration file"
44     cat <<EOF > ${yaml_dir}/.sops.yaml
45 creation_rules:
46   - path_regex: .*.yaml
47     encrypted_regex: ^(bmcPassword|decryptionSecret|hashedPassword|emcoPassword|rootPassword)$
48     pgp: ${key_fp}
49 EOF
50
51     sops --encrypt --in-place --config=${yaml_dir}/.sops.yaml ${yaml}
52 }
53
54 function sops_decrypt {
55     local -r yaml=$1
56
57     local -r yaml_dir=$(dirname ${yaml})
58     sops --decrypt --in-place --config=${yaml_dir}/.sops.yaml ${yaml}
59 }
60
61 function flux_site_source_name {
62     local -r url=$1
63     local -r branch=$2
64     echo $(basename ${url})-${branch}
65 }
66
67 function flux_site_kustomization_name {
68     local -r url=$1
69     local -r branch=$2
70     local -r path=$3
71     echo $(flux_site_source_name ${url} ${branch})-site-$(basename ${path})
72 }
73
74 function flux_create_site {
75     local -r url=$1
76     local -r branch=$2
77     local -r path=$3
78     local -r key_name=$4
79
80     local -r source_name=$(flux_site_source_name ${url} ${branch})
81     local -r kustomization_name=$(flux_site_kustomization_name ${url} ${branch} ${path})
82     local -r key_fp=$(gpg --with-colons --list-secret-keys ${key_name} | awk -F: '/fpr/ {print $10;exit}')
83     local -r secret_name="${key_name}-sops-gpg"
84
85     flux create source git ${source_name} --url=${url} --branch=${branch}
86     gpg --export-secret-keys --armor "$(_gpg_key_fp ${key_name})" |
87         kubectl -n flux-system create secret generic ${secret_name} --from-file=sops.asc=/dev/stdin --dry-run=client -o yaml |
88         kubectl apply -f -
89     kubectl create namespace ${SITE_NAMESPACE} --dry-run=client -o yaml | kubectl apply -f -
90     flux create kustomization ${kustomization_name} --target-namespace=${SITE_NAMESPACE} --path=${path} --source=GitRepository/${source_name} --prune=true \
91          --decryption-provider=sops --decryption-secret=${secret_name}
92 }