Created
November 8, 2021 18:27
-
-
Save JacobJohansen/cbfb65cf9a651303eec7f92293049550 to your computer and use it in GitHub Desktop.
shell to get db password
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
#!/bin/sh | |
relative_dir=`perl -e 'use Cwd "realpath";$pwd = realpath(shift); $pwd =~ s/\/[^\/]*$//; print $pwd' $0` | |
GREEN='\033[0;32m' | |
NC='\033[0m' # No Color | |
RDSHOST="" | |
RDSUSER="reader" | |
PROFILE_POST_FIX="ro" | |
case $2 in | |
writer) | |
RDSUSER="writer" | |
PROFILE_POST_FIX="rw" | |
;; | |
*) | |
RDSUSER="reader" | |
PROFILE_POST_FIX="ro" | |
;; | |
esac | |
case $1 in | |
dev) | |
RDSHOST="rds.url.aws" | |
;; | |
pre-prod) | |
RDSHOST="" | |
;; | |
prod) | |
RDSHOST="" | |
;; | |
esac | |
export AWS_PROFILE="$1-$PROFILE_POST_FIX" | |
export AWS_CONFIG_FILE="$relative_dir/awsconfig" | |
echo "getting credentials for ${GREEN}$1${NC} for the user ${GREEN}$RDSUSER${NC}" | |
PGPASSWORD="$(aws rds generate-db-auth-token --hostname $RDSHOST --port 5432 --username $RDSUSER)" | |
echo "" | |
if [[ "$PGPASSWORD" != *"X-Amz-Credential"* ]]; then | |
echo "$PGPASSWORD" | |
fi | |
if [[ "$PGPASSWORD" == *"X-Amz-Credential"* ]]; then | |
echo "${GREEN}DB-URL:${NC} $RDSHOST" | |
echo "$PGPASSWORD" | pbcopy | |
echo "" | |
echo "${GREEN}Password Token has been copied to clip board${NC}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#RDS #IAM