-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
helm.sh
275 lines (220 loc) · 8.39 KB
/
helm.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env sh
set -euf
# shellcheck source=scripts/commands/decrypt.sh
. "${SCRIPT_DIR}/commands/decrypt.sh"
helm_command_usage() {
cat <<EOF
helm secrets [ OPTIONS ] $1
This is a wrapper for "helm [command]". It will detect -f and
--values options, and decrypt any encrypted *.yaml files before running "helm
[command]".
Example:
$ helm secrets <HELM SECRETS OPTIONS> upgrade <HELM UPGRADE OPTIONS>
$ helm secrets <HELM SECRETS OPTIONS> lint <HELM LINT OPTIONS>
Typical usage:
$ helm secrets upgrade i1 stable/nginx-ingress -f values.test.yaml -f secrets.test.yaml
$ helm secrets -b vals lint ./my-chart -f values.test.yaml -f secrets.test.yaml
EOF
}
decrypted_file_list_dir=$(_mktemp -d)
_trap_hook() {
if [ -d "${decrypted_file_list_dir}" ]; then
set +f # Enable globbing
for file in "${decrypted_file_list_dir}"/*.file; do
set -f # Disable globbing
if [ -e "$file" ]; then # Make sure it isn't an empty match, in case of no files
decrypted_file=$(cat "$file")
rm -- "$(printf '%s' "$decrypted_file")"
if [ "${QUIET}" = "false" ]; then
printf "[helm-secrets] Removed: %s\n" "$decrypted_file" >&2
fi
fi
done
rm -rf "${decrypted_file_list_dir}"
fi
}
helm_wrapper() {
argc=$#
j=0
while [ $j -lt $argc ]; do
case "$1" in
--set | --set=?* | --set-string | --set-string=?* | --set-json | --set-json=?*)
_1="${1}"
case "${_1}" in
--set=* | --set-string=* | --set-json=*)
literals="${_1#*=}"
set -- "$@" "${_1%%=*}"
;;
*)
literals="${2}"
set -- "$@" "$1"
shift
j=$((j + 1))
;;
esac
decrypted_literals=""
IFS=","
_literal=""
set_list=false
for literal in ${literals}; do
unset IFS
opt_prefix=""
case "${literal}" in
*\\)
_literal="${literal}"
continue
;;
esac
if [ "${_literal}" != "" ]; then
literal="${_literal},${literal}"
_literal=""
fi
if [ "${set_list}" = "false" ]; then
opt_prefix="${literal%%=*}"
if [ "$opt_prefix" != "" ]; then
opt_prefix="${opt_prefix}="
fi
literal="${literal#*=}"
fi
case "${literal}" in
\\\{*) ;;
*\\\}) ;;
\{*)
set_list=true
;;
*\})
set_list=false
;;
esac
# Force secret backend
if [ "${literal#*!}" != "${literal}" ]; then
if is_secret_backend "${literal%%\!*}"; then
load_secret_backend "${literal%%\!*}"
literal="${literal#*!}"
else
load_secret_backend "${DEFAULT_SECRET_BACKEND}"
fi
else
load_secret_backend "${DEFAULT_SECRET_BACKEND}"
fi
if ! decrypted_literal=$(backend_decrypt_literal "${literal}"); then
fatal 'Unable to decrypt literal value %s' "${literal}"
fi
if [ "${decrypted_literal}" = "${literal}" ]; then
decrypted_literals="${decrypted_literals}${opt_prefix}${decrypted_literal},"
else
decrypted_literals="${decrypted_literals}${opt_prefix}$(printf '%s' "${decrypted_literal}" | sed -e 's/\\/\\\\/g' | sed -e 's/,/\\,/g'),"
fi
done
set -- "$@" "${decrypted_literals%*,}"
;;
-f | --values | --values=?* | --set-file | --set-file=?*)
_1="${1}"
case "${_1}" in
--values=* | --set-file=*)
files="${_1#*=}"
set -- "$@" "${_1%%=*}"
;;
*)
files="${2}"
set -- "$@" "$1"
shift
j=$((j + 1))
;;
esac
decrypted_files=""
IFS='
'
for file in $(printf '%s' "${files}" | sed -E 's/([^\\]),/\1\n/g'); do
unset IFS
case "$_1" in
-f | --values | --values=?*)
double_escape_need=0
sops_type="yaml"
opt_prefix=""
;;
--set-file | --set-file=?*)
double_escape_need=1
sops_type="auto"
opt_prefix="${file%%=*}="
file="${file#*=}"
;;
esac
# Ignore error on files beginning with ?
if [ "${file##\?}" != "${file}" ]; then
file="${file##\?}"
IGNORE_MISSING_VALUES=true
fi
# Force secret backend
if [ "${file#*!}" != "${file}" ]; then
if is_secret_backend "${file%%\!*}"; then
load_secret_backend "${file%%\!*}"
file="${file#*!}"
else
load_secret_backend "${DEFAULT_SECRET_BACKEND}"
fi
else
load_secret_backend "${DEFAULT_SECRET_BACKEND}"
fi
if ! real_file=$(_file_get "${file}"); then
if [ "${IGNORE_MISSING_VALUES}" = "true" ]; then
real_file="$(_mktemp)"
else
fatal 'File does not exist: %s' "${file}"
fi
fi
file_dec="$(_file_dec_name "${real_file}")"
if [ -f "${file_dec}" ]; then
decrypted_files="${decrypted_files}${opt_prefix}$(_helm_winpath "${file_dec}" "${double_escape_need}"),"
if [ "${QUIET}" = "false" ]; then
log 'Decrypt skipped: %s' "${file}"
fi
else
if decrypt_helper "${real_file}" "${sops_type}"; then
printf '%s' "${file_dec}" >"${decrypted_file_list_dir}/${j}.file"
if [ "${QUIET}" = "false" ]; then
log 'Decrypt: %s' "${file}"
fi
decrypted_files="${decrypted_files}${opt_prefix}$(_helm_winpath "${file_dec}" "${double_escape_need}"),"
else
decrypted_files="${decrypted_files}${opt_prefix}$(_helm_winpath "${real_file}" "${double_escape_need}"),"
fi
fi
done
set -- "$@" "${decrypted_files%*,}"
;;
*)
if [ -d "$1" ] || [ -f "$1" ]; then
set -- "$@" "$(_helm_winpath "${1}")"
else
set -- "$@" "$1"
fi
;;
esac
shift
j=$((j + 1))
done
if [ "${EVALUATE_TEMPLATES}" = "true" ]; then
set -- "$@" "--post-renderer" "${HELM_BIN}"
if [ "${HELM_DEBUG:-}" = "1" ] || [ "${HELM_DEBUG:-}" = "true" ] || [ -n "${HELM_SECRETS_DEBUG+x}" ]; then
set -- "$@" "--post-renderer-args" "--debug"
fi
set -- "$@" "--post-renderer-args" "secrets"
set -- "$@" "--post-renderer-args" "--backend" "--post-renderer-args" "${SECRET_BACKEND}"
if [ "${SECRET_BACKEND_ARGS}" != "" ]; then
set -- "$@" "--post-renderer-args" "--backend-args" "--post-renderer-args" "${SECRET_BACKEND_ARGS}"
fi
if [ "${EVALUATE_TEMPLATES_DECODE_SECRETS}" = "true" ]; then
set -- "$@" "--post-renderer-args" "--evaluate-templates-decode-secrets"
fi
set -- "$@" "--post-renderer-args" "post-renderer"
fi
"${HELM_BIN}" ${TILLER_HOST:+--host "$TILLER_HOST"} "$@"
}
helm_command() {
if [ $# -lt 2 ] || is_help "$2"; then
helm_command_usage "${1:-"[helm command]"}"
return
fi
helm_wrapper "$@"
}