|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright 2021 Blockchain Technology Partners |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http:#www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# ------------------------------------------------------------------------------ |
| 16 | + |
| 17 | +# shellcheck source=includer.sh |
| 18 | +source "$(dirname "${BASH_SOURCE[0]}")/includer.sh" |
| 19 | + |
| 20 | +@include doc |
| 21 | +@include error |
| 22 | +@include exec |
| 23 | + |
| 24 | +@package secret |
| 25 | + |
| 26 | +declare -g -A SECRETS |
| 27 | +declare -g -A SECRETS_FILES |
| 28 | + |
| 29 | +function secret::register_env { |
| 30 | + local varName=${1:?} |
| 31 | + local targetVar=$2 |
| 32 | + if [ -z "$targetVar" ]; then |
| 33 | + SECRETS[$varName]="environment" |
| 34 | + declare -g "$varName=${!varName}" |
| 35 | + else |
| 36 | + SECRETS[$varName]="environment" |
| 37 | + declare -g -n "$varName=${targetVar}" |
| 38 | + fi |
| 39 | +} |
| 40 | + |
| 41 | +function secret::register_file { |
| 42 | + local varName=${1:?} |
| 43 | + local file=${2:?} |
| 44 | + SECRETS[$varName]="file" |
| 45 | + SECRETS_FILES[$varName]="$file" |
| 46 | + declare -g "$varName=$(cat "${SECRETS_FILES[$varName]}")" |
| 47 | +} |
| 48 | + |
| 49 | +function secret::exists { |
| 50 | + @doc Check if secret exists. |
| 51 | + @arg _1_ name of the secret |
| 52 | + local secretName=${1:?} |
| 53 | + if [ -n "${SECRETS[$secretName]}" ]; then |
| 54 | + case "${SECRETS[$secretName]}" in |
| 55 | + environment) |
| 56 | + if [ -n "${!secretName}" ]; then |
| 57 | + return 0 |
| 58 | + else |
| 59 | + return 1 |
| 60 | + fi |
| 61 | + ;; |
| 62 | + file) |
| 63 | + if [ -r "${SECRETS_FILES[$secretName]}" ]; then |
| 64 | + return 0 |
| 65 | + else |
| 66 | + return 1 |
| 67 | + fi |
| 68 | + ;; |
| 69 | + *) |
| 70 | + return 1 |
| 71 | + ;; |
| 72 | + esac |
| 73 | + else |
| 74 | + return 1 |
| 75 | + fi |
| 76 | +} |
| 77 | + |
| 78 | +function secret::must_exist { |
| 79 | + local secretName=${1:?} |
| 80 | + if ! secret::exists "$secretName"; then |
| 81 | + error::exit "No such secret $secretName" |
| 82 | + fi |
| 83 | +} |
| 84 | + |
| 85 | +function secret::as_file { |
| 86 | + local secretName=${1:?} |
| 87 | + secret::must_exist "$secretName" |
| 88 | + case "${SECRETS[$secretName]}" in |
| 89 | + environment) |
| 90 | + secret::env_as_file "$secretName" |
| 91 | + ;; |
| 92 | + file) |
| 93 | + secret::file_as_file "$secretName" |
| 94 | + ;; |
| 95 | + *) |
| 96 | + return 1 |
| 97 | + ;; |
| 98 | + esac |
| 99 | +} |
| 100 | + |
| 101 | +function secret::file_as_file { |
| 102 | + local secretName=${1:?} |
| 103 | + printf "%s" "${SECRETS_FILES[$secretName]}" |
| 104 | +} |
| 105 | + |
| 106 | +function secret::env_as_file { |
| 107 | + local secretName=${1:?} |
| 108 | + local tmpFile |
| 109 | + tmpFile=$(mktemp) |
| 110 | + (printenv "$secretName") >"$tmpFile" |
| 111 | + echo "$tmpFile" |
| 112 | +} |
0 commit comments