This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbws-env.sh
executable file
·331 lines (273 loc) · 7.42 KB
/
bws-env.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env bash
# This script was generated by bashly 1.0.7 (https://bashly.dannyb.co)
# Modifying it manually is not recommended
# :wrapper.bash3_bouncer
if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
printf "bash version 4 or higher is required\n" >&2
exit 1
fi
# :command.master_script
# :command.root_command
root_command() {
# src/root_command.sh
# shellcheck disable=SC2154
if [[ ${#other_args[@]} -eq 0 ]]; then
${0} --help
exit 0
fi
# echo "# this file is located in 'src/root_command.sh'"
# echo "# you can edit it freely and regenerate (it will not be overwritten)"
# inspect_args
# exit 0
bws_command() {
# shellcheck disable=SC2086
# we want to split args
bws "${@}" ${args[--project]:-}
}
if [[ -z "${BWS_ACCESS_TOKEN:-}" || -n "${args[--interactive]:-}" ]]; then
read -r -s -p "Paste your access token: " BWS_ACCESS_TOKEN
echo
export BWS_ACCESS_TOKEN
fi
secrets_json=$(bws_command secret list | jq '[.[] | {key: .key, value: .value}]')
for row in $(echo "${secrets_json}" | jq -r '.[] | @base64'); do
_jq() {
echo "${row}" | base64 --decode | jq -r "${1}"
}
key=$(_jq '.key')
value=$(_jq '.value')
export "${key}"="${value}"
done
# shellcheck disable=SC2154
bash -c "${other_args[0]}"
}
# :command.version_command
version_command() {
echo "$version"
}
# :command.usage
bws_env.sh_usage() {
if [[ -n $long_usage ]]; then
printf "bws-env.sh - Inject secrets from Bitwarden into a command.\n"
echo
else
printf "bws-env.sh - Inject secrets from Bitwarden into a command.\n"
echo
fi
printf "%s\n" "$(bold "Usage:")"
printf " bws-env.sh [OPTIONS] [COMMAND...]\n"
printf " bws-env.sh --help | -h\n"
printf " bws-env.sh --version | -v\n"
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "$(bold "Options:")"
# :command.usage_flags
# :flag.usage
printf " %s\n" "$(magenta "--interactive, -i")"
printf " Interactively supply a BWS_ACCESS_TOKEN\n"
echo
# :flag.usage
printf " %s\n" "$(magenta "--project, -p PROJECT")"
printf " Project ID to use for secrets\n"
echo
# :command.usage_fixed_flags
printf " %s\n" "$(magenta "--help, -h")"
printf " Show this help\n"
echo
printf " %s\n" "$(magenta "--version, -v")"
printf " Show version number\n"
echo
# :command.usage_args
printf "%s\n" "$(bold "Arguments:")"
echo " COMMAND..."
printf " The command to run with secrets injected\n"
echo
# :command.usage_environment_variables
printf "%s\n" "$(bold "Environment Variables:")"
# :environment_variable.usage
printf " %s\n" "$(cyan "BWS_ACCESS_TOKEN")"
printf " The access token used for Bitwarden Secrets Manager\n"
echo
# :command.usage_examples
printf "%s\n" "$(bold "Examples:")"
printf " ${0##*/} 'echo \$SOME_VAR_FROM_BITWARDEN'\n"
printf " ${0##*/} 'docker-compose up -d'\n"
printf " ${0##*/} 'npm run start'\n"
echo
fi
}
# :command.normalize_input
normalize_input() {
local arg flags
while [[ $# -gt 0 ]]; do
arg="$1"
if [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
input+=("${BASH_REMATCH[1]}")
input+=("${BASH_REMATCH[2]}")
elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
input+=("${BASH_REMATCH[1]}")
input+=("${BASH_REMATCH[2]}")
elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
flags="${BASH_REMATCH[1]}"
for ((i = 0; i < ${#flags}; i++)); do
input+=("-${flags:i:1}")
done
else
input+=("$arg")
fi
shift
done
}
# :command.inspect_args
inspect_args() {
if ((${#args[@]})); then
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
echo args:
for k in "${sorted_keys[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
else
echo args: none
fi
if ((${#other_args[@]})); then
echo
echo other_args:
echo "- \${other_args[*]} = ${other_args[*]}"
for i in "${!other_args[@]}"; do
echo "- \${other_args[$i]} = ${other_args[$i]}"
done
fi
if ((${#deps[@]})); then
readarray -t sorted_keys < <(printf '%s\n' "${!deps[@]}" | sort)
echo
echo deps:
for k in "${sorted_keys[@]}"; do echo "- \${deps[$k]} = ${deps[$k]}"; done
fi
}
# :command.user_lib
# src/lib/colors.sh
print_in_color() {
local color="$1"
shift
if [[ -z ${NO_COLOR+x} ]]; then
printf "$color%b\e[0m\n" "$*"
else
printf "%b\n" "$*"
fi
}
red() { print_in_color "\e[31m" "$*"; }
green() { print_in_color "\e[32m" "$*"; }
yellow() { print_in_color "\e[33m" "$*"; }
blue() { print_in_color "\e[34m" "$*"; }
magenta() { print_in_color "\e[35m" "$*"; }
cyan() { print_in_color "\e[36m" "$*"; }
bold() { print_in_color "\e[1m" "$*"; }
underlined() { print_in_color "\e[4m" "$*"; }
red_bold() { print_in_color "\e[1;31m" "$*"; }
green_bold() { print_in_color "\e[1;32m" "$*"; }
yellow_bold() { print_in_color "\e[1;33m" "$*"; }
blue_bold() { print_in_color "\e[1;34m" "$*"; }
magenta_bold() { print_in_color "\e[1;35m" "$*"; }
cyan_bold() { print_in_color "\e[1;36m" "$*"; }
red_underlined() { print_in_color "\e[4;31m" "$*"; }
green_underlined() { print_in_color "\e[4;32m" "$*"; }
yellow_underlined() { print_in_color "\e[4;33m" "$*"; }
blue_underlined() { print_in_color "\e[4;34m" "$*"; }
magenta_underlined() { print_in_color "\e[4;35m" "$*"; }
cyan_underlined() { print_in_color "\e[4;36m" "$*"; }
# :command.command_functions
# :command.parse_requirements
parse_requirements() {
# :command.fixed_flags_filter
while [[ $# -gt 0 ]]; do
case "${1:-}" in
--version | -v)
version_command
exit
;;
--help | -h)
long_usage=yes
bws_env.sh_usage
exit
;;
*)
break
;;
esac
done
# :command.dependencies_filter
if command -v bws >/dev/null 2>&1; then
deps['bws']="$(command -v bws | head -n1)"
else
printf "missing dependency: bws\n" >&2
printf "%s\n" "https://github.com/bitwarden/sdk/releases/latest" >&2
exit 1
fi
if command -v jq >/dev/null 2>&1; then
deps['jq']="$(command -v jq | head -n1)"
else
printf "missing dependency: jq\n" >&2
printf "%s\n" "https://command-not-found.com/jq" >&2
exit 1
fi
# :command.command_filter
action="root"
# :command.parse_requirements_while
while [[ $# -gt 0 ]]; do
key="$1"
case "$key" in
# :flag.case
--interactive | -i)
# :flag.case_no_arg
args['--interactive']=1
shift
;;
# :flag.case
--project | -p)
# :flag.case_arg
if [[ -n ${2+x} ]]; then
args['--project']="$2"
shift
shift
else
printf "%s\n" "--project requires an argument: --project, -p PROJECT" >&2
exit 1
fi
;;
--)
shift
other_args+=("$@")
break
;;
-?*)
other_args+=("$1")
shift
;;
*)
# :command.parse_requirements_case
# :command.parse_requirements_case_catch_all
other_args+=("$1")
shift
;;
esac
done
}
# :command.initialize
initialize() {
version="0.1.0"
long_usage=''
set -e
}
# :command.run
run() {
declare -A args=()
declare -A deps=()
declare -a other_args=()
declare -a input=()
normalize_input "$@"
parse_requirements "${input[@]}"
case "$action" in
"root") root_command ;;
esac
}
initialize
run "$@"