-
Notifications
You must be signed in to change notification settings - Fork 29
/
deploy-secrets.sh
executable file
·164 lines (158 loc) · 6.71 KB
/
deploy-secrets.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/bash
# use the bash_colors.sh file
found_colors="./tools/bash_colors.sh"
up_found_colors="../tools/bash_colors.sh"
if [[ "${DISABLE_COLORS}" == "" ]] && [[ "${found_colors}" != "" ]] && [[ -e ${found_colors} ]]; then
. ${found_colors}
elif [[ "${DISABLE_COLORS}" == "" ]] && [[ "${up_found_colors}" != "" ]] && [[ -e ${up_found_colors} ]]; then
. ${up_found_colors}
else
inf() {
echo "$@"
}
anmt() {
echo "$@"
}
good() {
echo "$@"
}
err() {
echo "$@"
}
critical() {
echo "$@"
}
warn() {
echo "$@"
}
fi
secret_prefix="tls-"
should_cleanup_before_startup=0
for i in "$@"
do
if [[ "${i}" == "-r" ]] || [[ "${i}" == "r" ]] || [[ "${i}" == "reload" ]]; then
should_cleanup_before_startup=1
fi
done
use_path="."
if [[ ! -e create-x509s.yml ]]; then
use_path="./ansible"
fi
anmt "----------------------------------------------------------------------------------"
anmt "deploying secrets"
inf ""
file_count=$(ls ${use_path}/ssl/ | grep "_key.pem" | wc -l)
if [[ "${file_count}" == "0" ]]; then
files=$(ls ${use_path}/secrets/ | grep "${secret_prefix}*")
inf "importing default secrets: ${files}"
for i in ${files}; do
base_secret_name=$(echo ${i} | sed -e 's/.yml//g')
secret_name="${base_secret_name}"
yml_file=${use_path}/secrets/${secret_name}.yml
inf " - checking yaml_file=${yml_file} for secret: ${secret_name}"
if [[ -e ${yml_file} ]]; then
if [[ "${should_cleanup_before_startup}" == "1" ]]; then
test_exists=$(kubectl get secrets --ignore-not-found | grep ${secret_name} | wc -l)
if [[ "${test_exists}" != "0" ]]; then
inf " - deleting previous secret: ${secret_name}"
kubectl delete secret ${secret_name}
fi
fi
inf " - importing default secret ${secret_name} from ${yml_file}"
kubectl apply -f ${yml_file}
if [[ $? -ne 0 ]]; then
inf ""
err "Failed import default secret ${secret_name} from ${yml_file} with: kubectl kubectl apply -f ${yml_file}"
inf ""
inf "If you want to reload all the keys use:"
inf "./ansible/deploy-secrets.sh -r"
inf ""
exit 1
fi
else
err " - no action taken for secret: ${secret_name} - did not find yaml_file=${yml_file}"
fi
done
else
files=$(ls ${use_path}/ssl/ | grep "_key.pem" | grep -v "_private")
inf "importing secrets: ${files}"
for i in ${files}; do
key_file=${use_path}/ssl/${i}
cert_file=$(echo "${key_file}" | sed -e 's/_key.pem/_cert.pem/g')
base_secret_name=$(echo ${i} | sed -e 's/_key.pem//g' | sed -e 's/_server//g')
secret_name="${secret_prefix}${base_secret_name}"
yml_file=${use_path}/secrets/${secret_name}.yml
namespace="default"
if [[ -e ${key_file} ]] && [[ -e ${cert_file} ]]; then
inf " - found key=${key_file} cert=${cert_file}"
if [[ "${should_cleanup_before_startup}" == "1" ]]; then
test_exists=$(kubectl get secrets --ignore-not-found | grep ${secret_name} | wc -l)
if [[ "${test_exists}" != "0" ]]; then
inf " - deleting previous secret: ${secret_name}"
kubectl delete secret ${secret_name}
fi
fi
if [[ "${secret_name}" == "tls-s3" ]];then
namespace="rook-ceph"
if [[ "${should_cleanup_before_startup}" == "1" ]]; then
test_exists=$(kubectl get secrets -n ${namespace} --ignore-not-found | grep ${secret_name} | wc -l)
if [[ "${test_exists}" != "0" ]]; then
inf " - deleting previous secret -n ${namespace}: ${secret_name}"
kubectl delete secret -n ${namespace} ${secret_name}
fi
fi
inf " - kubectl create secret tls ${secret_name} --cert=${cert_file} --key=${key_file} --namespace=${namespace}"
kubectl create secret tls ${secret_name} --cert=${cert_file} --key=${key_file} --namespace=${namespace}
namespace="default"
else
namespace="default"
fi
inf " - kubectl create secret tls ${secret_name} --cert=${cert_file} --key=${key_file} --namespace=${namespace}"
kubectl create secret tls ${secret_name} --cert=${cert_file} --key=${key_file} --namespace=${namespace}
if [[ $? -ne 0 ]]; then
inf ""
err "Failed - kubectl create secret tls ${secret_name} --cert=${cert_file} --key=${key_file} --namespace=${namesapce}"
inf ""
inf "If you want to reload all the keys use:"
inf "./ansible/deploy-secrets.sh -r"
inf ""
exit 1
else
inf " - creating ${secret_name} yaml file: ${yml_file}"
kubectl get secret ${secret_name} -o yaml > ${yml_file}
if [[ ! -e ${yml_file} ]]; then
inf ""
err "Failed to output secret to yaml file: kubectl get secret ${secret_name} -o yaml > ${yml_file}"
inf ""
exit 1
fi
fi
else
inf " - checking yaml_file=${yml_file} for secret: ${secret_name}"
if [[ -e ${yml_file} ]]; then
if [[ "${should_cleanup_before_startup}" == "1" ]]; then
test_exists=$(kubectl get secrets --ignore-not-found | grep ${secret_name} | wc -l)
if [[ "${test_exists}" != "0" ]]; then
inf " - deleting previous secret: ${secret_name}"
kubectl delete secret ${secret_name}
fi
fi
inf " - importing default secret ${secret_name} from ${yml_file}"
kubectl apply -f ${yml_file}
if [[ $? -ne 0 ]]; then
inf ""
err "Failed import default secret ${secret_name} from ${yml_file} with: kubectl kubectl apply -f ${yml_file}"
inf ""
inf "If you want to reload all the keys use:"
inf "./ansible/deploy-secrets.sh -r"
inf ""
exit 1
fi
else
inf " - no action taken for secret: ${secret_name} - no (key_file=${key_file} and cert_file=${cert_file}) and no yaml_file=${yml_file} found"
fi
fi
inf ""
done
fi
good "done deploying: secrets"