Skip to content

Commit 03d96f1

Browse files
committed
fix: add documentation and secret::clear
Signed-off-by: Kevin O'Donnell <kevin@blockchaintp.com>
1 parent 308ec03 commit 03d96f1

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

bash/secret.sh

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@ source "$(dirname "${BASH_SOURCE[0]}")/includer.sh"
2525

2626
declare -g -A SECRETS
2727
declare -g -A SECRETS_FILES
28+
declare -g -a SECRET_TMPFILES
2829

2930
function secret::register_env {
31+
@doc Register a secret under the provided name
32+
@arg _1_ the secret name
33+
@arg _2_ optional - the name of a different env var containing the secret val
34+
set +x
3035
local varName=${1:?}
3136
local targetVar=$2
3237
if [ -z "$targetVar" ]; then
@@ -39,6 +44,10 @@ function secret::register_env {
3944
}
4045

4146
function secret::register_file {
47+
@doc Register a secret in the specified file under the provided name
48+
@arg _1_ the secret name
49+
@arg _2_ the file containing the secret
50+
set +x
4251
local varName=${1:?}
4352
local file=${2:?}
4453
SECRETS[$varName]="file"
@@ -76,37 +85,52 @@ function secret::exists {
7685
}
7786

7887
function secret::must_exist {
88+
@doc Verify a secret exists or exit with error
89+
@arg _1_ name of the secret
7990
local secretName=${1:?}
8091
if ! secret::exists "$secretName"; then
8192
error::exit "No such secret $secretName"
8293
fi
8394
}
8495

8596
function secret::as_file {
97+
@doc Render the named secret as a temporary file and return the name
98+
@arg _1_ name of the secret
99+
set +x
86100
local secretName=${1:?}
87101
secret::must_exist "$secretName"
88102
case "${SECRETS[$secretName]}" in
89103
environment)
90-
secret::env_as_file "$secretName"
104+
_env_as_file "$secretName"
91105
;;
92106
file)
93-
secret::file_as_file "$secretName"
107+
_file_as_file "$secretName"
94108
;;
95109
*)
96110
return 1
97111
;;
98112
esac
99113
}
100114

101-
function secret::file_as_file {
115+
function _file_as_file {
116+
set +x
102117
local secretName=${1:?}
103118
printf "%s" "${SECRETS_FILES[$secretName]}"
104119
}
105120

106-
function secret::env_as_file {
121+
function _env_as_file {
122+
set +x
107123
local secretName=${1:?}
108124
local tmpFile
109125
tmpFile=$(mktemp)
126+
SECRET_TMPFILES+=("$tmpFile")
110127
(printenv "$secretName") >"$tmpFile"
111128
echo "$tmpFile"
112129
}
130+
131+
function secret::clear {
132+
@doc "Clear secret temprary files."
133+
if [ -n "${SECRET_TMPFILES[0]}" ]; then
134+
rm -f "${SECRET_TMPFILES[@]}"
135+
fi
136+
}

0 commit comments

Comments
 (0)