This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathintermediate-initialize.sh
61 lines (33 loc) · 1.64 KB
/
intermediate-initialize.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env bash
set -o pipefail
set -e
set -x
ca_wrapped_token=$1
vault_token=$2
VAULT_HOME=${VAULT_HOME:-/opt/vault}
# unwrap the token from the ca
export VAULT_CACERT="${VAULT_HOME}/config/keys/server.ca.pem"
ca_token=$(vault unwrap -address="https://ca.$(hostname -d):8200" -field=token "${ca_wrapped_token}")
# configure the intermediate and generate an intermediate csr
export VAULT_ADDR="https://$(hostname -f):8200"
export VAULT_TOKEN=${vault_token}
mount=pki-ica
csr="${VAULT_HOME}/config/keys/intermediate.csr.pem"
vault mount "-path=${mount}" pki
vault mount-tune -max-lease-ttl=87600h "${mount}"
vault write -field=csr "${mount}/intermediate/generate/internal" "common_name=$(hostname -f)" ttl=8760h "ip_sans=$(hostname -i)" > "${csr}"
# sign the intermediate csr
export VAULT_ADDR="https://ca.$(hostname -d):8200"
export VAULT_TOKEN=${ca_token}
certificate_bundle="${VAULT_HOME}/config/keys/intermediate-bundle.json"
certificate="${VAULT_HOME}/config/keys/intermediate.cert.pem"
issuing_ca="${VAULT_HOME}/config/keys/intermediate.issuing_ca.pem"
ca_chain="${VAULT_HOME}/config/keys/intermediate.ca_chain.pem"
vault write -format=json "pki-ca/root/sign-intermediate" "csr=@${csr}" use_csr_values=true format=pem_bundle > "${certificate_bundle}"
jq -r '.data.certificate' "${certificate_bundle}" > "${certificate}"
jq -r '.data.issuing_ca' "${certificate_bundle}" > "${issuing_ca}"
jq -r '.data.ca_chain' "${certificate_bundle}" > "${ca_chain}"
# set the signed certificate
export VAULT_ADDR="https://$(hostname -f):8200"
export VAULT_TOKEN=${vault_token}
vault write "${mount}/intermediate/set-signed" "certificate=@${certificate}"