|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Collect secrets from AWS SSM Parameter Store and output as environment variable exports. |
| 4 | + |
| 5 | +# Colors |
| 6 | +C_RESET='\033[0m' |
| 7 | +C_RED='\033[1;31m' |
| 8 | +C_GREEN='\033[1;32m' |
| 9 | +C_YELLOW='\033[1;33m' |
| 10 | + |
| 11 | +# Logs |
| 12 | +PREFIX_INFO="${C_GREEN}[INFO]${C_RESET} [$(date +%d-%m\ %T)]" |
| 13 | +PREFIX_WARN="${C_YELLOW}[WARN]${C_RESET} [$(date +%d-%m\ %T)]" |
| 14 | +PREFIX_CRIT="${C_RED}[CRIT]${C_RESET} [$(date +%d-%m\ %T)]" |
| 15 | + |
| 16 | +# Print help message |
| 17 | +function usage { |
| 18 | + echo "Usage: $0 [-h] -p PRODUCT -o OUTPUT" |
| 19 | + echo |
| 20 | + echo "CLI to collect secrets from AWS SSM Parameter Store |
| 21 | +and output as environment variable exports" |
| 22 | + echo |
| 23 | + echo "Optional arguments:" |
| 24 | + echo " -h Show this help message and exit" |
| 25 | + echo " -p Product tag (moonstream, spire, brood, drones)" |
| 26 | + echo " -o Output file name environment variables export to" |
| 27 | +} |
| 28 | + |
| 29 | +product_flag="" |
| 30 | +output_flag="" |
| 31 | +verbose_flag="false" |
| 32 | + |
| 33 | +while getopts 'p:o:v' flag; do |
| 34 | + case "${flag}" in |
| 35 | + p) product_flag="${OPTARG}" ;; |
| 36 | + o) output_flag="${OPTARG}" ;; |
| 37 | + h) usage |
| 38 | + exit 1 ;; |
| 39 | + v) verbose_flag="true" ;; |
| 40 | + *) usage |
| 41 | + exit 1 ;; |
| 42 | + esac |
| 43 | +done |
| 44 | + |
| 45 | +# Log messages |
| 46 | +function verbose { |
| 47 | + if [ "${verbose_flag}" == "true" ]; then |
| 48 | + echo -e "$1" |
| 49 | + fi |
| 50 | +} |
| 51 | + |
| 52 | +# Product flag should be specified |
| 53 | +# TODO(kompotkot): Extend script to work with few product at once |
| 54 | +if [ -z "${product_flag}" ]; then |
| 55 | + verbose "${PREFIX_CRIT} Please specify product tag" |
| 56 | + usage |
| 57 | + exit 1 |
| 58 | +fi |
| 59 | + |
| 60 | +verbose "${PREFIX_INFO} Retrieving deployment parameters with tag ${C_GREEN}Product:${product_flag}${C_RESET}" |
| 61 | +ENV_PARAMETERS=$(aws ssm describe-parameters \ |
| 62 | + --parameter-filters Key=tag:Product,Values=${product_flag} \ |
| 63 | + | jq -r .Parameters[].Name) |
| 64 | +if [ -z "${ENV_PARAMETERS}" ]; then |
| 65 | + verbose "${PREFIX_CRIT} There no parameters for provided product tag" |
| 66 | + exit 1 |
| 67 | +fi |
| 68 | + |
| 69 | +verbose "${PREFIX_INFO} Retrieving parameters values" |
| 70 | +ENV_PARAMETERS_VALUES=$(aws ssm get-parameters \ |
| 71 | + --names ${ENV_PARAMETERS} \ |
| 72 | + --query "Parameters[*].{Name:Name,Value:Value}") |
| 73 | +ENV_PARAMETERS_VALUES_LENGTH=$(echo ${ENV_PARAMETERS_VALUES} | jq length) |
| 74 | +verbose "${PREFIX_INFO} Extracted ${ENV_PARAMETERS_VALUES_LENGTH} parameters" |
| 75 | +for i in $(seq 0 $((${ENV_PARAMETERS_VALUES_LENGTH} - 1))); do |
| 76 | + param_key=$(echo ${ENV_PARAMETERS_VALUES} | jq -r .[$i].Name) |
| 77 | + param_value=$(echo ${ENV_PARAMETERS_VALUES} | jq .[$i].Value) |
| 78 | + if [ -z "${output_flag}" ]; then |
| 79 | + echo "${param_key}=${param_value}" |
| 80 | + else |
| 81 | + echo "${param_key}=${param_value}" >> "${output_flag}" |
| 82 | + fi |
| 83 | +done |
0 commit comments