From 828f20f5b6b878626924e9e3c8199c6a0871df9d Mon Sep 17 00:00:00 2001 From: Muhammad Atif Ali Date: Mon, 9 Oct 2023 17:01:23 +0300 Subject: [PATCH] test --- vault/main.tf | 19 ++++++++++++++++--- vault/run.sh | 48 ++++++++++-------------------------------------- 2 files changed, 26 insertions(+), 41 deletions(-) diff --git a/vault/main.tf b/vault/main.tf index a939305..7cedfbf 100644 --- a/vault/main.tf +++ b/vault/main.tf @@ -27,8 +27,21 @@ variable "vault_auth_id" { } variable "secrets" { - type = map(list(string)) - description = "A map of secret lists to set as environment variables. Each secret list is a list of paths to secrets to set as environment variables." + type = map(map(string)) + description = </dev/null; then - echo "jq is not installed. Please install jq to automatically set the secrets." - echo "You can manually set the secrets by using the following command in your workspace:" - echo "vault kv get " - exit 0 -fi - -echo "${SECRETS}" -# Decode the JSON string to a temporary file -echo "${SECRETS}" | jq '.' > temp.json - -# Iterate through the keys and values in the JSON file -for key in $(jq -r 'keys[]' temp.json); do - path=$(echo $key | tr -d \") - # Fetch the secrets from Vault - secrets=$(vault kv get -format=json $path) - # Get the array of secret names from the JSON file - sceret_names=$(jq -r ".$key[]" temp.json) - # Convert the list of environment variables to an array - IFS=', ' read -r -a sceret_array <<< "$sceret_names" - # Set the environment variables with the secret values - for secret_name in "$${sceret_array[@]}"; do - # Remove quotes from the variable name - secret_name=$(echo $secret_name | tr -d \") - secret_value=$(echo $secrets | jq -r ".data.data.$secret_name") - export $secret_name=$secret_value +printf "šŸ” Fetching secrets ...\n\n" +for key in $(echo "${SECRETS}" | jq -r "keys[]" ); do + secrets=$(echo "${SECRETS}" | jq -r ".$key.secrets[]") + file=$(echo "${SECRETS}" | jq -r ".$key.file") + printf "Fetching secrets from $${key} ...\n" + for secret in $${secrets}; do + value=$(vault kv get -format=json $${key} | jq -r ".data.data.$${secret}") + printf "$${secret}=$${value}\n" >> $${file} done + printf "\n" done - -# Remove the temporary file -rm temp.json - -