-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to install secrets with aws ssm
For launch template
- Loading branch information
1 parent
0ac62b0
commit 79071e0
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
: "${SECRETS_NAMESPACE:?}" | ||
|
||
here="$(cd "$(dirname "$0")" && pwd)" | ||
repo_root="$(cd "$here/.." && pwd)" | ||
env_file="$repo_root/.env" | ||
# These could probably be arguments instead | ||
secret_names=( | ||
github_api_token | ||
github_oauth_id | ||
github_oauth_secret | ||
postgres_server | ||
postgres_db | ||
postgres_password | ||
postgres_user | ||
) | ||
readonly here repo_root env_file secret_names | ||
|
||
ends-with() { [[ ${#2} -le ${#1} && "${1:$((${#1}-${#2}))}" == "$2" ]]; } | ||
starts-with() { [[ ${#2} -le ${#1} && "${1:0:${#2}}" == "$2" ]]; } | ||
|
||
get-secrets() { | ||
aws ssm get-parameters \ | ||
--with-decryption \ | ||
--names "$@" \ | ||
--output text \ | ||
--query 'Parameters[*].[Name, Value]' | ||
} | ||
|
||
if ends-with "$SECRETS_NAMESPACE" "/"; then | ||
SECRETS_NAMESPACE="${SECRETS_NAMESPACE:0:$((${#SECRETS_NAMESPACE}-1))}" | ||
fi | ||
|
||
names=() | ||
for secret_name in "${secret_names[@]}"; do | ||
# Assume fully qualified secret name if it starts with / | ||
if starts-with "$secret_name" "/"; then | ||
names+=("$secret_name") | ||
else | ||
names+=("$SECRETS_NAMESPACE/$secret_name") | ||
fi | ||
done | ||
|
||
while IFS=$'\t' read -r secret_name secret_value; do | ||
echo -n "$secret_name" | \ | ||
awk -F/ '{ $0=toupper($NF); gsub(/-/, "_"); printf $0 }' | ||
echo -n = | ||
echo "$secret_value" | ||
done < <(get-secrets "${names[@]}") >> "$env_file" |