forked from Cross-PLN-Technical-Working-Group/adpn-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadpn-stash-do
executable file
·190 lines (153 loc) · 7.05 KB
/
adpn-stash-do
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
#!/bin/bash
#
# adpn-stash-do: script to handle adpn stash commands
#
# @version 2021.0910
SCRIPTPATH="$(which "$0")"
SCRIPTPATH="$( readlink --canonicalize "${SCRIPTPATH}" )"
SCRIPTDIR="$(dirname "${SCRIPTPATH}")"
SCRIPT="$(basename "${SCRIPTPATH}")"
source "${SCRIPTDIR}/adpn-define-aliases"
__USAGE__="Usage: ${SCRIPT} [--version] [--help] <CMD> [<ARGS>]"
__HELP__="[${SCRIPT}] Try '${SCRIPT} help' for more information."
__DOC__="""${__USAGE__}
--version Display the version of the script
--help Display these usage notes
The most commonly used ${SCRIPT} commands are:
get Retrieve the value of a chosen property from temporary encrypted stash
post Assign a new value to a chosen property in temporary encrypted stash
open Set up a new temporary encrypted stash & output variable assignments needed to access it
close Delete a temporary encrypted stash & output variable assignments to erase keys
The open and close commands are typically used within an eval structure, for example:
eval \$( ${SCRIPT} open )
eval \$( ${SCRIPT} open --if-needed )
eval \$( ${SCRIPT} close )
Exit codes:
0 = success (successful operation and expected result)
1-254 = error in executing command
255 = command not supported
"""
##########################################################################################
### COMMAND LINE: loop through switches ##################################################
##########################################################################################
declare -a _CMDLINE ; _CMDLINE=("$0")
declare -a SWITCHFILES ; SWITCHFILES=()
if [[ -r "${CONFFILE}" ]] ; then
SWITCHFILES+=(${CONFFILE})
fi
CMDLINETXT=$(mktemp)
until [[ "$#" -lt 1 ]] ; do
_CMDLINE+=("$1")
printf "%s\n" "$1" >> "${CMDLINETXT}"
shift
done
SWITCHFILES+=(${CMDLINETXT})
adpn_command_line "${SWITCHFILES[@]}"
rm "${CMDLINETXT}"
##########################################################################################
### SCRIPT: DETERMINE COMMAND, THEN EXECUTE PIPELINE ####################################
##########################################################################################
EXITCODE=0
CMD="${_ARGV[1]}"
adpn_set_display_settings # V, VV, Q, QQ, DBG, DBGLEVEL, DDBG, SCRIPT_CMD_NAME / @see adpn-define-aliases
adpn_script_handle_version_or_help
declare -a ADPN_SECRETS_ARGS=( "${_ARGV[@]:2}" )
ADPN_SECRET_COUPLE_KEY=""
ADPN_JSON_TEMPLATE=""
case "${CMD}" in
"open")
declare -a ADPN_STASH_SETTINGS_SHELL=()
ADPN_STASH_SETTINGS_SHELL+=( "export ADPN_STASH_FILE='%(file)s'" )
ADPN_STASH_SETTINGS_SHELL+=( "export ADPN_STASH_KEY='%(\$json)s'" )
if [[ -z "${_PARAM[if-needed]}" || -z "${ADPN_STASH_KEY}" || -z "${ADPN_STASH_FILE}" || ! -e "${ADPN_STASH_FILE}" ]] ; then
ADPN_STASH_JSON="$( printf '%s\n' '{}' | adpn-do-stash.py --put )"
ADPN_STASH_FILE="$( printf '%s' "${ADPN_STASH_JSON}" | adpn-json.py --key=file )"
ADPN_STASH_KEY="$( printf '%s' "${ADPN_STASH_JSON}" )"
[[ -z "${_PARAM[keep]}" ]] && ADPN_STASH_SETTINGS_SHELL+=( "ADPN_STASH_CLOSE='%(file)s'" )
else
ADPN_STASH_KEY_JSON=$( printf "%s" "${ADPN_STASH_KEY}" )
ADPN_STASH_FILE_JSON=$( printf '{"file": "%s"}' "${ADPN_STASH_FILE}" )
ADPN_STASH_CACHED_JSON='{ "cached": 1 }'
ADPN_STASH_JSON=$( printf '%s\n%s\n%s' "${ADPN_STASH_KEY_JSON}" "${ADPN_STASH_FILE_JSON}" "${ADPN_STASH_CACHED_JSON}" | adpn-json.py --cascade --output=application/json )
fi
[[ -z "${QQ}" ]] && ADPN_STASH_SETTINGS_SHELL+=( "declare -p ADPN_STASH_FILE 1>&2" )
ADPN_STASH_SETTINGS="$( join_by "; " "${ADPN_STASH_SETTINGS_SHELL[@]}" )"
[[ -z "${_SWITCHES[output]}" ]] && ADPN_JSON_TEMPLATE=$( printf -- "--template=%s" "${ADPN_STASH_SETTINGS}" ) || ADPN_JSON_TEMPLATE=""
[[ -z "${_SWITCHES[output]}" ]] && _SWITCHES[output]="--output=text/plain"
;;
"close")
if [[ -z "${_SWITCHES[if-needed]}" || -n "${_PARAM[if-needed]}" ]] ; then
declare -a ADPN_STASH_CLEANUP=() # FIXME: fill from log file
for FILE in "${ADPN_STASH_FILE}" "${ADPN_STASH_CLEANUP[@]}" ; do
[[ -z "${QQ}" ]] && RM="rm -v" || RM="rm"
[[ -w "${FILE}" ]] && ${RM} "${FILE}" 1>&2
done
unset ADPN_STASH_JSON
[[ -z "${_SWITCHES[output]}" ]] && printf "unset ADPN_STASH_FILE;\nunset ADPN_STASH_KEY;\nunset ADPN_STASH_CLOSE;\n"
fi
;;
""|"get")
if [[ -n "${_PARAM[key]}" ]] ; then
ADPN_SECRET_COUPLE_KEY="${_PARAM[key]}"
else
ADPN_SECRET_COUPLE_KEY="${ADPN_SECRETS_ARGS[0]}"
ADPN_SECRETS_ARGS=( "${ADPN_SECRETS_ARGS[@]:1}" )
fi
ADPN_STASH_JSON="$( printf "%s\n" "${ADPN_STASH_KEY}" | adpn-do-stash.py --get )"
EXITCODE="$?"
;;
"put")
ADPN_STASH_OUTPUT="$( printf "%s\n%s" "${ADPN_STASH_KEY}" "${ADPN_SECRETS_ARGS[@]}" | adpn-json.py --cascade --output=application/json | adpn-do-stash.py --put )"
ADPN_STASH_JSON="{}"
if [[ -r "${ADPN_STASH_FILE}" ]] ; then
ADPN_STASH_JSON="$( printf "%s" "${ADPN_STASH_KEY}" | adpn-do-stash.py --get )"
fi
;;
"post")
if [[ -n "${_PARAM[key]}" ]] ; then
ADPN_SECRET_COUPLE_KEY="${_PARAM[key]}"
else
ADPN_SECRET_COUPLE_KEY="${ADPN_SECRETS_ARGS[0]}"
ADPN_SECRETS_ARGS=( "${ADPN_SECRETS_ARGS[@]:1}" )
fi
if [[ -n "${_PARAM[value]}" ]] ; then
ADPN_SECRET_COUPLE_VALUE="${_PARAM[value]}"
elif [[ "${#ADPN_SECRETS_ARGS[@]}" -gt 0 ]] ; then
ADPN_SECRET_COUPLE_VALUE="${ADPN_SECRETS_ARGS[0]}"
ADPN_SECRETS_ARGS=( "${ADPN_SECRETS_ARGS[@]:1}" )
elif [[ -n "${ADPN_SECRET_COUPLE_KEY}" ]] ; then
ADPN_SECRET_COUPLE_VALUE="$( cat - )"
fi
declare -a JSON_OVERLAYS=( "${ADPN_STASH_KEY}" )
if [[ -r "${ADPN_STASH_FILE}" ]] ; then
JSON_OVERLAYS+=( "$( printf "%s" "${ADPN_STASH_KEY}" | adpn-do-stash.py --get )" )
fi
JSON_OVERLAYS+=( "$( adpn-json.py --key="${ADPN_SECRET_COUPLE_KEY}" --value="${ADPN_SECRET_COUPLE_VALUE}" )" )
ADPN_STASH_OUTPUT="$( printf '%s\n' "${JSON_OVERLAYS[@]}" | adpn-json.py --cascade --output=application/json | adpn-do-stash.py --put )"
ADPN_STASH_JSON="{}"
if [[ -r "${ADPN_STASH_FILE}" ]] ; then
ADPN_STASH_JSON="$( printf '%s' "${ADPN_STASH_KEY}" | adpn-do-stash.py )"
fi
;;
"version"|"help")
EXITCODE=0
;;
*)
printf "[%s:%d] '%s' command not understood.\n" "${SCRIPT_CMD_NAME}" "${LINENO}" "${SUBCMD}" 1>&2
printf "${__HELP__}" 1>&2
EXITCODE=255
;;
esac
if [[ "${EXITCODE}" -gt 0 ]] ; then
printf "[%s:%d] adpn-do-stash.py request FAILED: (req='%s', file='%s', error=%d)\n" "${SCRIPT_CMD_NAME}" "${LINENO}" "${CMD}" "${ADPN_STASH_FILE}" "${EXITCODE}" 1>&2
elif [[ -n "${ADPN_STASH_JSON}" ]] ; then
declare -a ADPN_JSON_ARGS=( )
[[ -n "${ADPN_SECRET_COUPLE_KEY}" ]] && ADPN_JSON_ARGS+=( "--key=${ADPN_SECRET_COUPLE_KEY}" )
[[ -n "${ADPN_JSON_TEMPLATE}" ]] && ADPN_JSON_ARGS+=( "${ADPN_JSON_TEMPLATE}" )
[[ -n "${_SWITCHES[output]}" ]] && ADPN_JSON_ARGS+=( "${_SWITCHES[output]}" )
printf "%s" "${ADPN_STASH_JSON}" | adpn-json.py "${ADPN_JSON_ARGS[@]}"
fi
##########################################################################################
### CLEANUP: exit with settled exit code. ################################################
##########################################################################################
exit ${EXITCODE}