2022-05-24 16:26:05 +00:00
|
|
|
#####################################################################
|
2022-06-22 18:17:19 +00:00
|
|
|
|
2023-02-22 01:44:27 +00:00
|
|
|
DEPENDENCIES+=(
|
|
|
|
docker
|
|
|
|
)
|
|
|
|
|
|
|
|
REQUIRED_ENV+=(
|
|
|
|
AWS_ACCOUNT
|
|
|
|
AWS_REGION
|
|
|
|
)
|
|
|
|
|
|
|
|
use cloud/aws/cli
|
|
|
|
|
|
|
|
#####################################################################
|
|
|
|
|
|
|
|
RDS__SELECT_DATABASE() {
|
|
|
|
local DATABASES=$(_RDS__GET_AVAILABLE_DATABASES)
|
|
|
|
[ ! $DATABASES ] && FAIL 1 'no databases available'
|
|
|
|
|
|
|
|
local ID=$(\
|
|
|
|
echo $DATABASES | jq -r '.instance + " @ " + .cluster' \
|
|
|
|
| FZF 'select a database (instance@cluster)' \
|
|
|
|
)
|
|
|
|
[ ! $ID ] && ABORT
|
|
|
|
|
|
|
|
local INSTANCE=$(echo $ID | sed 's/ @ .*$//')
|
|
|
|
local CLUSTER=$(echo $ID | sed 's/^.* @ //')
|
|
|
|
|
|
|
|
echo $DATABASES | jq "select (.instance == \"$INSTANCE\" and .cluster == \"$CLUSTER\")"
|
|
|
|
}
|
|
|
|
|
|
|
|
_RDS__GET_AVAILABLE_DATABASES() {
|
|
|
|
AWS rds describe-db-instances \
|
|
|
|
| jq -r '.[] | .[] | {
|
|
|
|
instance: .DBInstanceIdentifier,
|
|
|
|
cluster: .DBClusterIdentifier,
|
|
|
|
type: .Engine,
|
|
|
|
host: .Endpoint.Address,
|
|
|
|
port: .Endpoint.Port,
|
|
|
|
user: .MasterUsername,
|
|
|
|
database: .DBName
|
|
|
|
}'
|
|
|
|
}
|
|
|
|
|
|
|
|
RDS__GET_DATABASE_CREDENTIALS() {
|
2022-08-01 21:53:40 +00:00
|
|
|
local PRINT_PASSWORD=0
|
2022-08-16 00:30:37 +00:00
|
|
|
local ERRORS=0
|
2022-06-22 18:17:19 +00:00
|
|
|
|
2022-08-01 21:53:40 +00:00
|
|
|
while [[ $# -gt 0 ]]
|
2022-06-22 18:17:19 +00:00
|
|
|
do
|
2022-08-01 21:53:40 +00:00
|
|
|
case $1 in
|
|
|
|
--print-password ) PRINT_PASSWORD=1 ;;
|
|
|
|
* )
|
2023-02-22 01:44:27 +00:00
|
|
|
WARNING "unrecognized argument $1"
|
2022-08-16 00:30:37 +00:00
|
|
|
ERRORS+=1
|
2022-08-01 21:53:40 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift 1
|
2022-06-22 18:17:19 +00:00
|
|
|
done
|
2022-08-16 00:30:37 +00:00
|
|
|
|
2023-02-22 01:44:27 +00:00
|
|
|
CHECK_ERRORS
|
2022-08-01 21:53:40 +00:00
|
|
|
|
|
|
|
##########################################
|
|
|
|
|
2023-02-22 01:44:27 +00:00
|
|
|
local DATABASE=$(RDS__SELECT_DATABASE)
|
|
|
|
[ ! $DATABASE ] && ABORT
|
2022-08-01 21:53:40 +00:00
|
|
|
|
|
|
|
DB_HOST="$(echo $DATABASE | jq -r '.host')"
|
2023-02-22 01:44:27 +00:00
|
|
|
[ ! $DB_HOST ] && { ERROR 'unable to find host'; return 2; }
|
2022-08-01 21:53:40 +00:00
|
|
|
|
|
|
|
DB_PORT="$(echo $DATABASE | jq -r '.port')"
|
|
|
|
[ ! $DB_PORT ] && DB_PORT=5432
|
|
|
|
[[ $DB_PORT =~ ^null$ ]] && DB_PORT=5432
|
|
|
|
|
|
|
|
##########################################
|
|
|
|
|
|
|
|
local AUTH_METHOD=$(\
|
|
|
|
echo "iam\nsecretsmanager\nuser-input" \
|
2023-02-22 01:44:27 +00:00
|
|
|
| FZF 'select an authentication method' \
|
2022-08-01 21:53:40 +00:00
|
|
|
)
|
2023-02-22 01:44:27 +00:00
|
|
|
[ ! $AUTH_METHOD ] && ABORT
|
2022-08-01 21:53:40 +00:00
|
|
|
|
|
|
|
case $AUTH_METHOD in
|
2023-02-22 01:44:27 +00:00
|
|
|
iam ) _RDS_AUTH__iam ;;
|
|
|
|
secretsmanager ) _RDS_AUTH__secretsmanager ;;
|
|
|
|
user-input ) _RDS_AUTH__userinput ;;
|
2022-08-01 21:53:40 +00:00
|
|
|
esac
|
|
|
|
|
2023-02-22 01:44:27 +00:00
|
|
|
STATUS
|
|
|
|
STATUS "host : $DB_HOST"
|
|
|
|
STATUS "type : $DB_TYPE"
|
|
|
|
STATUS "port : $DB_PORT"
|
|
|
|
STATUS "database : $DB_NAME"
|
|
|
|
STATUS "username : $DB_USER"
|
|
|
|
[[ $PRINT_PASSWORD -eq 1 ]] && STATUS "password : $DB_PASS"
|
|
|
|
STATUS
|
2022-08-01 21:53:40 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 01:44:27 +00:00
|
|
|
_RDS_AUTH__iam() {
|
2022-08-01 21:53:40 +00:00
|
|
|
DB_PASS=$(\
|
2023-02-22 01:44:27 +00:00
|
|
|
AWS rds generate-db-auth-token \
|
2022-08-01 21:53:40 +00:00
|
|
|
--hostname $DB_HOST \
|
|
|
|
--port $DB_PORT \
|
|
|
|
--username $DB_USER \
|
|
|
|
)
|
|
|
|
}
|
2022-06-22 18:17:19 +00:00
|
|
|
|
2023-02-22 01:44:27 +00:00
|
|
|
_RDS_AUTH__secretsmanager() {
|
|
|
|
local CREDENTIALS=$(_RDS__GET_SECRETSMANAGER_CREDENTIALS)
|
2022-08-01 21:53:40 +00:00
|
|
|
echo $CREDENTIALS | jq -e '.pass' >/dev/null 2>&1 \
|
|
|
|
&& DB_PASS="'$(echo $CREDENTIALS | jq -r '.pass' | sed "s/'/'\"'\"'/g")'"
|
|
|
|
|
|
|
|
echo $CREDENTIALS | jq -e '.password' >/dev/null 2>&1 \
|
|
|
|
&& DB_PASS="'$(echo $CREDENTIALS | jq -r '.password' | sed "s/'/'\"'\"'/g")'"
|
|
|
|
|
|
|
|
echo $CREDENTIALS | jq -e '.user' >/dev/null 2>&1 \
|
|
|
|
&& DB_USER=$(echo $CREDENTIALS | jq -r '.user')
|
|
|
|
|
|
|
|
echo $CREDENTIALS | jq -e '.username' >/dev/null 2>&1 \
|
|
|
|
&& DB_USER=$(echo $CREDENTIALS | jq -r '.username')
|
|
|
|
|
|
|
|
echo $CREDENTIALS | jq -e '.name' >/dev/null 2>&1 \
|
|
|
|
&& DB_NAME=$(echo $CREDENTIALS | jq -r '.name')
|
|
|
|
|
|
|
|
echo $CREDENTIALS | jq -e '.dbname' >/dev/null 2>&1 \
|
|
|
|
&& DB_NAME=$(echo $CREDENTIALS | jq -r '.dbname')
|
2022-06-22 18:17:19 +00:00
|
|
|
}
|
2022-08-01 21:53:40 +00:00
|
|
|
|
2023-02-22 01:44:27 +00:00
|
|
|
_RDS__GET_SECRETSMANAGER_CREDENTIALS() {
|
2022-08-01 21:53:40 +00:00
|
|
|
local ID=$(\
|
2023-02-22 01:44:27 +00:00
|
|
|
AWS secretsmanager list-secrets \
|
2022-08-01 21:53:40 +00:00
|
|
|
| jq -r '.[] | .[] | .Name' \
|
2023-02-22 01:44:27 +00:00
|
|
|
| FZF 'select a secret' \
|
2022-08-01 21:53:40 +00:00
|
|
|
)
|
|
|
|
[ ! $ID ] && return 1
|
|
|
|
|
2023-02-22 01:44:27 +00:00
|
|
|
AWS secretsmanager get-secret-value --secret-id "$ID" \
|
2022-08-01 21:53:40 +00:00
|
|
|
| jq -r '.SecretString' | jq
|
|
|
|
}
|
|
|
|
|