-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.sh
executable file
·141 lines (118 loc) · 3.86 KB
/
client.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/bash
set -eu
COMMAND="$1"
shift
set -xo pipefail
export VAULT_ADDR="https://127.0.0.1:8200"
export VAULT_CACERT="${VAULT_CACERT:-certs/vault-ca.pem}"
NAME="enclaive-redis-sgx"
MEASUREMENT="5cd731b2990478b4542eb9f9f362f3e8de8845fa2e19146737f11ded92298a66"
SECRET='{"environment": {}, "files": {"/dev/attestation/keys/data":"c1ydwRokay1R4xZ3mPwd1w==","/dev/attestation/keys/logs":"nKz4dRYLWQBhkW9bzs6HQw=="}, "argv": []}'
KEY_TYPE="ec"
KEY_BITS="256"
DEPLOYMENT_DOMAIN="${ENCLAIVE_NAMESPACE}.svc.cluster.local"
function enable() {
vault secrets enable \
-path=sgx-app kv-v2 \
|| echo "Already enabled kvv2 at sgx-app"
vault auth enable \
-path=sgx-auth vault-plugin-auth-sgx \
|| echo "Already enabled sgx-auth"
vault secrets enable \
-path=sgx-pki-root pki \
|| echo "Already enabled sgx-pki-root"
vault secrets enable \
-path=sgx-pki pki \
|| echo "Already enabled sgx-pki"
VAULT_DOMAIN="enclaive-vault-sgx.${DEPLOYMENT_DOMAIN}"
# configure ca
vault write -format=json \
sgx-pki-root/config/urls \
issuing_certificates="https://${VAULT_DOMAIN}/v1/sgx-pki-root/ca" \
crl_distribution_points="https://${VAULT_DOMAIN}/v1/sgx-pki-root/crl"
vault write -format=json \
sgx-pki/config/urls \
issuing_certificates="https://${VAULT_DOMAIN}/v1/sgx-pki/ca" \
crl_distribution_points="https://${VAULT_DOMAIN}/v1/sgx-pki/crl"
# allow higher ttl
vault secrets tune \
-max-lease-ttl=87600h sgx-pki-root
vault secrets tune \
-max-lease-ttl=43800h sgx-pki
# generate ca
vault write -format=json \
sgx-pki-root/root/generate/internal \
ttl=87600h \
key_type="${KEY_TYPE}" \
key_bits="${KEY_BITS}" \
common_name="${VAULT_DOMAIN} Root Authority"
vault write -format=json \
sgx-pki/intermediate/generate/internal \
ttl=43800h \
key_type="${KEY_TYPE}" \
key_bits="${KEY_BITS}" \
common_name="${VAULT_DOMAIN} Intermediate Authority" \
| tee /dev/stderr \
| jq -r '.data.csr' > certs/sgx-ca-intermediate.csr
vault write -format=json \
sgx-pki-root/root/sign-intermediate \
csr=@certs/sgx-ca-intermediate.csr \
format=pem_bundle \
ttl=43800h \
| tee /dev/stderr \
| jq -r '.data.certificate' > certs/sgx-ca.pem
vault write -format=json \
sgx-pki/intermediate/set-signed \
certificate=@certs/sgx-ca.pem
rm certs/sgx-ca-intermediate.csr
# generate external client cert
vault write -format=json \
sgx-pki/roles/"client.sgx.enclaive" \
allowed_domains="client.sgx.enclaive" \
allow_bare_domains=true \
allow_subdomains=false \
allow_localhost=false \
ttl=8760h \
key_type="${KEY_TYPE}" \
key_bits="${KEY_BITS}"
vault write -format=json \
sgx-pki/issue/"client.sgx.enclaive" \
common_name="client.sgx.enclaive" \
| jq '.data' \
> certs/sgx-client.json
jq -r '.certificate' certs/sgx-client.json > certs/sgx-cert.pem
jq -r '.private_key' certs/sgx-client.json > certs/sgx-key.pem
chmod 0600 certs/sgx-key.pem
rm certs/sgx-client.json
}
function create() {
# register enclave
vault write -format=json \
auth/sgx-auth/enclave/"${NAME}" mrenclave="${MEASUREMENT}"
# store secret
vault kv put -format=json \
-mount=sgx-app "${NAME}" provision="${SECRET}"
APP_DOMAIN="${NAME}.${DEPLOYMENT_DOMAIN}"
# create a pki role
vault write -format=json \
sgx-pki/roles/"${APP_DOMAIN}" \
allowed_domains="${APP_DOMAIN}" \
allow_bare_domains=true \
allow_subdomains=true \
allow_localhost=false \
ttl=8760h \
key_type="${KEY_TYPE}" \
key_bits="${KEY_BITS}"
# allow access to secret and cert issuing
vault policy write \
sgx-app/"${NAME}" - \
< <(env -i NAME="${NAME}" ROLE="${APP_DOMAIN}" envsubst < vault.sgx.policy.template)
}
case "${COMMAND}" in
enable) enable ;;
create) create ;;
*)
echo "Unknown command: ${COMMAND}"
exit 1
;;
esac