Skip to content

Commit

Permalink
add ssm compare script
Browse files Browse the repository at this point in the history
  • Loading branch information
jay-87 committed Dec 26, 2024
1 parent a30c88e commit d902040
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ FROM amazonlinux:latest

# Install the AWS CLI
RUN yum update -y && \
yum install -y aws-cli nc nano vim
yum install -y aws-cli nc nano vim jq

COPY ./check-connections.sh /usr/local/bin/check-connections.sh
COPY ./ssm-compare.sh /usr/local/bin/ssm-compare.sh

RUN chmod +x /usr/local/bin/check-connections.sh
RUN chmod +x /usr/local/bin/ssm-compare.sh
53 changes: 53 additions & 0 deletions ssm-compare.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash
PREFIX=""

while getopts n:p: flag
do
case "${flag}" in
n) APP_NAME=${OPTARG};;
p) PREFIX=${OPTARG};;
esac
done

usage() {
echo "Usage: $0 -n <APP_NAME> (-p <PREFIX>)"
echo " <APP_NAME> must be inputed."
echo " <PREFIX> is optional."
exit 1;
}

if [ -z "$APP_NAME" ]; then
usage
fi

################### FUNCTIONS ###################
function vault_get_keys() {
# get keys from env of container
env | grep "^${PREFIX}" | cut -d '=' -f 1 | sed 's/^/"/;s/$/"/'
}

function ssm_get_path() {
AWS_REGION="ap-northeast-1" # Tokyo region
params=$(aws ssm get-parameters-by-path --path /$APP_NAME --region $AWS_REGION --with-decryption | jq -r ".Parameters[] | select(.Name | startswith(\"/$APP_NAME/${PREFIX}\")) | .Name")
echo "total:" $(wc -l <<< "$params")
echo "$params" | tr ' ' '\n' | cut -d '/' -f 3 | sed 's/^/"/;s/$/"/'

SSM_VARS=($(echo $params | tr ' ' '\n' | cut -d '/' -f 3 | sed 's/^/"/;s/$/"/'))

VAULT_VARS=($(vault_get_keys))
echo "compare ssm and vault..."
for expected_var in "${SSM_VARS[@]}"; do
found=false
for vault_var in "${VAULT_VARS[@]}"; do
if [[ "$expected_var" == "$vault_var" ]]; then
found=true
break
fi
done
if [[ "$found" == false ]]; then
echo "Missing: $expected_var"
fi
done
}

ssm_get_path

0 comments on commit d902040

Please sign in to comment.