Serge Bazanski | c6fd662 | 2018-11-01 22:39:01 +0100 | [diff] [blame] | 1 | # source me to have all the nice things |
| 2 | |
| 3 | if [ "$0" == "$BASH_SOURCE" ]; then |
| 4 | echo "You should be sourcing this." |
| 5 | exit 1 |
| 6 | fi |
| 7 | |
| 8 | hscloud_root="$( cd "$(dirname "$BASH_SOURCE")"; pwd -P )" |
| 9 | |
| 10 | hscloud-dc() { |
| 11 | ( cd "$hscloud_root" && docker-compose -f "docker/docker-compose.yml" "$@" ) |
| 12 | } |
| 13 | |
| 14 | hscloud-pki-dev() { |
| 15 | ( |
| 16 | set -e |
| 17 | |
| 18 | cd "$hscloud_root" |
| 19 | rm -rf docker/pki |
| 20 | |
| 21 | cp -rv go/pki/dev-certs docker/pki |
| 22 | cd docker/pki |
| 23 | bash gen.sh m6220-proxy arista-proxy cmc-proxy topo client |
| 24 | ls *pem |
| 25 | ) |
| 26 | } |
| 27 | |
Sergiusz Bazanski | 52c8718 | 2019-01-12 22:30:41 +0100 | [diff] [blame] | 28 | # Generate a per-node certificate remotely on the node. |
| 29 | hscloud-node-remote-cert() { |
Serge Bazanski | a5be0d8 | 2018-12-23 01:35:07 +0100 | [diff] [blame] | 30 | ( |
| 31 | set -e |
Sergiusz Bazanski | 52c8718 | 2019-01-12 22:30:41 +0100 | [diff] [blame] | 32 | if [ -z "$1" ] || [ -z "$2" ] || [ -x "$3" ]; then |
| 33 | echo >&2 "Usage: hscloud-node-remote-cert node.fqdn.com certname subj" |
Serge Bazanski | a5be0d8 | 2018-12-23 01:35:07 +0100 | [diff] [blame] | 34 | exit 1 |
| 35 | fi |
| 36 | fqdn="$1" |
Sergiusz Bazanski | 52c8718 | 2019-01-12 22:30:41 +0100 | [diff] [blame] | 37 | certname="$2" |
| 38 | subj="$3" |
| 39 | |
| 40 | echo "Node: ${fqdn}; Cert: ${certname}" |
Serge Bazanski | a5be0d8 | 2018-12-23 01:35:07 +0100 | [diff] [blame] | 41 | |
| 42 | echo "Checking node livenes..." |
| 43 | ssh root@$fqdn uname -a |
| 44 | |
| 45 | echo "Checking if node already has key..." |
Sergiusz Bazanski | 52c8718 | 2019-01-12 22:30:41 +0100 | [diff] [blame] | 46 | ssh root@$fqdn stat /opt/hscloud/${certname}.key || ( |
Serge Bazanski | a5be0d8 | 2018-12-23 01:35:07 +0100 | [diff] [blame] | 47 | echo "Generating key..." |
| 48 | ssh root@$fqdn -- mkdir -p /opt/hscloud |
Sergiusz Bazanski | 52c8718 | 2019-01-12 22:30:41 +0100 | [diff] [blame] | 49 | ssh root@$fqdn -- nix-shell -p openssl --command "\"openssl genrsa -out /opt/hscloud/${certname}.key 4096\"" |
| 50 | ssh root@$fqdn -- chmod 400 /opt/hscloud/${certname}.key |
Serge Bazanski | a5be0d8 | 2018-12-23 01:35:07 +0100 | [diff] [blame] | 51 | ) |
| 52 | |
| 53 | echo "Checking if node already has cert..." |
Sergiusz Bazanski | 52c8718 | 2019-01-12 22:30:41 +0100 | [diff] [blame] | 54 | ssh root@$fqdn stat /opt/hscloud/${certname}.crt && exit 0 |
Serge Bazanski | a5be0d8 | 2018-12-23 01:35:07 +0100 | [diff] [blame] | 55 | echo "No cert, will generate..." |
| 56 | |
| 57 | cd "$hscloud_root" |
| 58 | secrets="$hscloud_root/secrets" |
Sergiusz Bazanski | 711c4a9 | 2019-01-13 00:02:10 +0100 | [diff] [blame^] | 59 | ca="$secrets/ca.key" |
Serge Bazanski | a5be0d8 | 2018-12-23 01:35:07 +0100 | [diff] [blame] | 60 | [ ! -f "$ca" ] && ( scripts/secretstore decrypt "$secrets/cipher/ca.key" > $ca ) |
| 61 | |
Sergiusz Bazanski | 52c8718 | 2019-01-12 22:30:41 +0100 | [diff] [blame] | 62 | ssh root@$fqdn -- nix-shell -p openssl --command "\"openssl req -new -key /opt/hscloud/${certname}.key -out /opt/hscloud/${certname}.csr -subj '${subj}'\"" |
| 63 | scp root@$fqdn:/opt/hscloud/${certname}.csr ${fqdn}-${certname}.csr |
| 64 | openssl x509 -req -in ${fqdn}-${certname}.csr -CA data/ca.crt -CAkey "$ca" -CAcreateserial -out "data/${fqdn}-${certname}.crt" |
Serge Bazanski | a5be0d8 | 2018-12-23 01:35:07 +0100 | [diff] [blame] | 65 | |
Sergiusz Bazanski | 52c8718 | 2019-01-12 22:30:41 +0100 | [diff] [blame] | 66 | scp "data/${fqdn}-${certname}.crt" root@$fqdn:/opt/hscloud/${certname}.crt |
Serge Bazanski | a5be0d8 | 2018-12-23 01:35:07 +0100 | [diff] [blame] | 67 | scp "data/ca.crt" root@$fqdn:/opt/hscloud/ca.crt |
Sergiusz Bazanski | 52c8718 | 2019-01-12 22:30:41 +0100 | [diff] [blame] | 68 | ssh root@$fqdn -- chmod 444 /opt/hscloud/${certname}.crt /opt/hscloud/ca.crt |
| 69 | rm ${fqdn}-${certname}.csr |
| 70 | ) |
| 71 | } |
| 72 | |
| 73 | # Generate locally (if not present) a shared certificate, and upload it to the node |
| 74 | hscloud-node-shared-cert() { |
| 75 | ( |
| 76 | set -e |
| 77 | if [ -z "$1" ] || [ -z "$2" ] || [ -x "$3" ]; then |
| 78 | echo >&2 "Usage: hscloud-node-shared-cert node.fqdn.com certname subj" |
| 79 | exit 1 |
| 80 | fi |
| 81 | fqdn="$1" |
| 82 | certname="$2" |
| 83 | subj="$3" |
| 84 | |
| 85 | cd "$hscloud_root" |
| 86 | secrets="$hscloud_root/secrets" |
| 87 | keyfile="$secrets/$certname.key" |
| 88 | cert="$hscloud_root/data/$certname.crt" |
| 89 | csr="$hscloud_root/data/$certname.csr" |
Sergiusz Bazanski | 711c4a9 | 2019-01-13 00:02:10 +0100 | [diff] [blame^] | 90 | ca="$secrets/ca.key" |
Sergiusz Bazanski | 52c8718 | 2019-01-12 22:30:41 +0100 | [diff] [blame] | 91 | [ ! -f "$ca" ] && ( scripts/secretstore decrypt "$secrets/cipher/ca.key" > $ca ) |
| 92 | |
| 93 | echo "Checking if key exists..." |
| 94 | if [ ! -f "$keyfile" ]; then |
| 95 | echo "No key, trying to decrypt..." |
| 96 | if ! scripts/secretstore decrypt "$secrets/cipher/$certname.key" > "$keyfile" ; then |
| 97 | echo "No encrypted key, generating..." |
| 98 | openssl genrsa -out $keyfile 4096 |
| 99 | echo "Encrypting..." |
| 100 | scripts/secretstore encrypt "$keyfile" > "$secrets/cipher/$certname.key" |
| 101 | fi |
| 102 | fi |
| 103 | |
| 104 | echo "Checking if cert exists..." |
| 105 | if [ ! -f "$cert" ]; then |
| 106 | echo "No cert, generating..." |
| 107 | rm -f "${csr}" |
| 108 | openssl req -new -key "${keyfile}" -out "${csr}" -subj "${subj}" |
| 109 | openssl x509 -req -in "${csr}" -CA data/ca.crt -CAkey "$ca" -CAcreateserial -out "${cert}" |
| 110 | fi |
| 111 | |
| 112 | echo "Copying certificate to node..." |
| 113 | scp "${cert}" root@$fqdn:/opt/hscloud/${certname}.crt |
| 114 | scp "${keyfile}" root@$fqdn:/opt/hscloud/${certname}.key |
| 115 | ssh root@$fqdn -- chmod 444 /opt/hscloud/${certname}.crt |
| 116 | ssh root@$fqdn -- chmod 400 /opt/hscloud/${certname}.key |
| 117 | ) |
| 118 | } |
| 119 | |
| 120 | hscloud-node-certs() { |
| 121 | ( |
| 122 | set -e |
| 123 | |
| 124 | if [ -z "$1" ]; then |
| 125 | echo >&2 "Usage: hscloud-node-certs node.fqdn.com" |
| 126 | exit 1 |
| 127 | fi |
| 128 | fqdn="$1" |
| 129 | |
| 130 | hscloud-node-remote-cert ${fqdn} node "/C=PL/ST=Mazowieckie/L=Mazowieckie/O=Stowarzyszenie Warszawski Hackerspace/OU=Node Bootstrap Certificate/CN=\"$fqdn\"" |
| 131 | hscloud-node-remote-cert ${fqdn} kube-node "/C=PL/ST=Mazowieckie/L=Mazowieckie/O=system:nodes/OU=Kubernetes Node Certificate/CN=system:node:\"$fqdn\"" |
| 132 | for component in controller-manager proxy scheduler; do |
| 133 | hscloud-node-shared-cert ${fqdn} kube-${component} "/C=PL/ST=Mazowieckie/L=Mazowieckie/O=system:kube-${component}/OU=Kubernetes Component ${component}/CN=system:kube-${component}" |
| 134 | done |
Sergiusz Bazanski | ee7c1aa | 2019-01-12 23:56:17 +0100 | [diff] [blame] | 135 | hscloud-node-shared-cert ${fqdn} kube-apiserver "/C=PL/ST=Mazowieckie/L=Mazowieckie/O=Kubernetes API/CN=k0.hswaw.net" |
| 136 | hscloud-node-shared-cert ${fqdn} kube-serviceaccounts "/C=PL/ST=Mazowieckie/L=Mazowieckie/O=Kubernetes Service Accounts/CN=service-accounts" |
Serge Bazanski | a5be0d8 | 2018-12-23 01:35:07 +0100 | [diff] [blame] | 137 | ) |
| 138 | } |
| 139 | |
Serge Bazanski | c6fd662 | 2018-11-01 22:39:01 +0100 | [diff] [blame] | 140 | echo "Now playing:" |
| 141 | echo " hscloud-dc - run docker-compose" |
| 142 | echo " hscloud-pki-dev - generate dev PKI certs" |
Serge Bazanski | a5be0d8 | 2018-12-23 01:35:07 +0100 | [diff] [blame] | 143 | echo " hscloud-node-push-certs - push a node cert to the node" |
Serge Bazanski | 9ec50e3 | 2018-12-23 01:40:28 +0100 | [diff] [blame] | 144 | echo "" |