Skip to content

Instantly share code, notes, and snippets.

@devtanc
Last active October 25, 2017 04:08
Show Gist options
  • Save devtanc/f2bf1783f5497a0b886c659ccd931123 to your computer and use it in GitHub Desktop.
Save devtanc/f2bf1783f5497a0b886c659ccd931123 to your computer and use it in GitHub Desktop.
Credstash Loader Loader
#!/bin/bash
# Loads credstah credentials from a file
# Pre-req:
# Need to have credstash installed and setup from: https://github.com/fugue/credstash
# Tables for credentials need to be named `credentials-<stage>`
#
# Stage defaults to 'development'
#
# File Format Spec
# -----------------
# stage <stage> - Applies the given stage to all lines following (blank value resets to 'development')
# prefix <prefix> - Applies the given prefix to all lines following (blank value resets to '')
# <key> <value> - Specifies a given key/value pair (settigs for stage and prefix will be applied)
#
# <value> CANNOT contain whitespace characters
#
# e.g.
# ----begin file----
# stage staging
# prefix App.subkey
#
# key1 valueforkey1
# key2 valueforkey2
# stage
# prefix
# key3 valueforkey3
# -----end file-----
#
# will yield the following [<stage> <key> <value>] entries
# staging App.subkey.key1 valueforkey1
# staging App.subkey.key2 valueforkey2
# development key3 valueforkey3
filename=$1
prefix=""
stage="development"
# Check that file exists
if [ ! -e "$filename" ]; then
echo "File not found: [$filename]"
exit 1
fi
function credstash_put {
# Default stage to development
stage='development'
# If the env is stage, set the stage, key, and value to 'staging', $2, and $3 respectively
if [ $1 = 'stage' ] || \
[ $1 = 'staging' ]; then
stage='staging'
key=$2
value=$3
# If the env is prod, set the stage, key, and value to 'production', $2, and $3 respectively
elif [ $1 = 'prod' ] || \
[ $1 = 'production' ]; then
stage='production'
key=$2
value=$3
# If the env is dev, set the stage, key, and value to 'development', $2, and $3 respectively
elif [ $1 = 'dev' ] || \
[ $1 = 'development' ]; then
stage='development'
key=$2
value=$3
# Otherwise set them to $1 and $2 and default to 'development'
else
key=$1
value=$2
fi
echo 'Table: credentials-'$stage
echo 'Key: '$key
echo 'Value: '$value
credstash -t credentials-"$stage" put -k alias/credstash-"$stage" -a "$key" "$value"
echo ""
}
function process_line {
key=$(echo "$1" | cut -d' ' -f1)
value=$(echo "$1" | cut -d' ' -f2-)
# Handle 'prefix' lines
# If the line has a second word, then $prefix will be set to that
# Otherwise $prefix will be empty
if [ $key == "prefix" ]; then
echo "Prefix: [$value]"
echo ""
prefix=$value
continue
fi
# Handle 'stage' lines
# If the line has a second word, then $stage will be set to that
# Otherwise $stage will be set to 'development'
if [ $key == "stage" ]; then
echo "Stage: [$value]"
echo ""
if [ ! -z "$value" ]; then
stage=$value
else
stage="development"
fi
continue
fi
# Put credentials into credstash (with prefix, if set)
if [ ! -z $prefix ]; then
credstash_put $stage $prefix"."$key $value
else
credstash_put $stage $key $value
fi
}
while read line; do
# Skip blank lines
[ -z "$line" ] && continue
process_line "$line"
done <$filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment