2023-07-12 04:46:12 +00:00
|
|
|
#! /usr/bin/env bash
|
|
|
|
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
|
|
|
|
USER_HOME=$HOME
|
|
|
|
[[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")"
|
|
|
|
. ${SHRC_D:-$SCRIPT_DIR}/01_util.functions
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
JSON=0
|
2023-08-27 19:00:11 +00:00
|
|
|
if [[ " $* " =~ " --help " ]]; then
|
|
|
|
cat <<USAGE
|
2023-07-12 04:46:12 +00:00
|
|
|
Usage:
|
|
|
|
./tcdbinfo.sh --help
|
2023-08-27 19:00:11 +00:00
|
|
|
sudo ./tcdbinfo.sh [--json|--cols=<columns>] [--force] [app1] [app...]
|
2023-07-12 04:46:12 +00:00
|
|
|
|
|
|
|
Description:
|
|
|
|
When running the script (as root) without any arguments, it will list
|
|
|
|
all TrueChart apps with database credential secrets.
|
|
|
|
|
|
|
|
The scope can be narrowed by passing app name arguments.
|
|
|
|
|
|
|
|
The FIRST argument, however, will affect the output:
|
|
|
|
--help - Display this usage text.
|
|
|
|
--json - Output all fields in JSON format
|
|
|
|
--cols=<columns> - Where <columns> can be empty, a preset name or
|
|
|
|
a comma delimited list of columns.
|
|
|
|
|
|
|
|
An empty string will show the default preset.
|
|
|
|
|
|
|
|
Presets:
|
|
|
|
default - The default preset
|
|
|
|
safe - Similar to the default, but without
|
|
|
|
the password column
|
|
|
|
debug - Full connection URL with password
|
|
|
|
masked with asterisks ('******')
|
|
|
|
|
|
|
|
Columns:
|
|
|
|
name - App name, preceding with ix-
|
|
|
|
raw_url - DB connection URL as it is stored
|
|
|
|
in kubernetes secrets.
|
|
|
|
This can only be used within the
|
|
|
|
same pod
|
|
|
|
url - Fully formed URL.
|
|
|
|
safeurl - Same as url, with password masked.
|
|
|
|
protocol - Connection protocol.
|
|
|
|
username - DB Username
|
|
|
|
password - Password
|
|
|
|
pwd_len - Password length
|
|
|
|
host - Hostname
|
|
|
|
port - Conection Port
|
|
|
|
dbname - DB name
|
|
|
|
|
|
|
|
When '--cols' is not specified, default behavious can be overridden
|
|
|
|
by exporting the TCDBCOLS envrionment variable.
|
|
|
|
|
|
|
|
--cols takes presedence, and overrides any behavior dictated by the
|
|
|
|
environment variable
|
|
|
|
|
|
|
|
USAGE
|
2023-08-27 19:00:11 +00:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
ARGS=()
|
|
|
|
ARGS_MODE=1
|
|
|
|
ALL=0
|
|
|
|
while [[ -n "$1" ]]; do
|
|
|
|
if [[ "$ARGS_MODE" -eq 1 && "$1" =~ ^-- ]]; then
|
|
|
|
case "$1" in
|
|
|
|
"--json")
|
|
|
|
JSON=1
|
|
|
|
;;
|
|
|
|
"--cols"*)
|
|
|
|
[[ "$1" =~ ^--cols= ]] || shift
|
|
|
|
TCDBCOLS="${1#--cols=}"
|
|
|
|
;;
|
|
|
|
"--force")
|
|
|
|
ALL=1
|
|
|
|
;;
|
|
|
|
"--")
|
|
|
|
ARGS_MODE='--'
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
ARGS=("${ARGS[@]}" "$1")
|
|
|
|
esac
|
|
|
|
else
|
|
|
|
ARGS=("${ARGS[@]}" "$1")
|
2023-07-12 03:21:15 +00:00
|
|
|
fi
|
2023-07-12 04:46:12 +00:00
|
|
|
shift
|
2023-08-27 19:00:11 +00:00
|
|
|
done
|
|
|
|
|
2023-07-12 04:46:12 +00:00
|
|
|
TCDBCOLS="${TCDBCOLS:-default}"
|
|
|
|
case "$TCDBCOLS" in
|
|
|
|
'default' )
|
|
|
|
TCDBCOLS='name,protocol,username,password,pwd_len,host,port,dbname'
|
|
|
|
;;
|
|
|
|
'safe' )
|
|
|
|
TCDBCOLS='name,protocol,username,pwd_len,host,port,dbname'
|
|
|
|
;;
|
|
|
|
'debug' )
|
|
|
|
TCDBCOLS='name,safeurl'
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
require_root
|
|
|
|
|
|
|
|
QUERY_NAMESPACE=' -A'
|
2023-08-27 19:00:11 +00:00
|
|
|
[[ ${#ARGS[@]} -eq 0 ]] || QUERY_NAMESPACE=$( printf -- ' --namespace=ix-%s' "${ARGS[@]}" )
|
2023-07-12 04:46:12 +00:00
|
|
|
|
|
|
|
jqcode='
|
2023-08-27 19:00:11 +00:00
|
|
|
.items[] | select(.metadata.name|test("(dbcreds|cnpg-main-urls|-superuser)$$"))
|
2023-07-12 04:46:12 +00:00
|
|
|
| {
|
|
|
|
"name": .metadata.namespace,
|
2023-08-27 19:00:11 +00:00
|
|
|
"app": (
|
|
|
|
if .metadata.labels."app.kubernetes.io/instance" != null then
|
|
|
|
.metadata.labels."app.kubernetes.io/instance"
|
|
|
|
else
|
|
|
|
.metadata.labels."cnpg.io/cluster"
|
|
|
|
end
|
|
|
|
),
|
|
|
|
"url": (
|
|
|
|
if .data.url != null then
|
|
|
|
.data.url | @base64d
|
|
|
|
elif .data.std != null then
|
|
|
|
.data.std | @base64d
|
|
|
|
else {
|
|
|
|
"protocol": "",
|
|
|
|
"username": .data.username | @base64d,
|
|
|
|
"password": .data.password | @base64d,
|
|
|
|
"passwordlen": .data.password | @base64d | length,
|
|
|
|
"host": "",
|
|
|
|
"port": "",
|
|
|
|
"dbname": ""
|
|
|
|
} end)
|
|
|
|
,"raw": .
|
|
|
|
} | {
|
|
|
|
"name": .name,
|
|
|
|
"app": .app,
|
|
|
|
"url": (
|
|
|
|
if (.url|type) == "object" then
|
|
|
|
""
|
|
|
|
else
|
|
|
|
.url
|
|
|
|
end
|
|
|
|
),
|
2023-07-12 04:46:12 +00:00
|
|
|
"data": (
|
2023-08-27 19:00:11 +00:00
|
|
|
if (.url|type) == "object" then
|
|
|
|
.url
|
|
|
|
else
|
|
|
|
.url |
|
|
|
|
match("(.*)://(.+):(.+)@([^:]+)(:(\\d+))?/(.*)$") | .captures | {
|
|
|
|
"protocol": .[0].string,
|
|
|
|
"username": .[1].string,
|
|
|
|
"password": .[2].string,
|
|
|
|
"passwordlen": .[2].string | length,
|
|
|
|
"host": .[3].string,
|
|
|
|
"safeport": .[4].string,
|
|
|
|
"port": .[5].string,
|
|
|
|
"dbname": .[6].string,
|
|
|
|
}
|
|
|
|
end
|
2023-07-12 04:46:12 +00:00
|
|
|
)
|
|
|
|
} | {
|
|
|
|
"name": .name,
|
|
|
|
"raw_url": .url,
|
|
|
|
"url": "\(.data.protocol)://\(.data.username)@\(.data.password):\(.data.host).\(.name).svc.cluster.local\(.data.safeport)/\(.data.dbname)",
|
|
|
|
"safeurl": "\(.data.protocol)://\(.data.username)@*******:\(.data.host).\(.name).svc.cluster.local\(.data.safeport)/\(.data.dbname)",
|
|
|
|
"protocol": .data.protocol,
|
|
|
|
"username": .data.username,
|
|
|
|
"password": .data.password,
|
|
|
|
"pwd_len": .data.passwordlen,
|
|
|
|
"host": "\(.data.host).\(.name).svc.cluster.local",
|
|
|
|
"port": .data.port,
|
|
|
|
"dbname": .data.dbname
|
|
|
|
}
|
|
|
|
'
|
|
|
|
|
2023-08-27 19:00:11 +00:00
|
|
|
[[ "$ALL" -eq 1 ]] || jqcode="$jqcode | select( .raw_url != \"\" )"
|
|
|
|
|
2023-07-12 04:46:12 +00:00
|
|
|
json_results=$(
|
|
|
|
<<<"${QUERY_NAMESPACE}" \
|
|
|
|
xargs -n1 k3s kubectl \
|
|
|
|
get secrets \
|
|
|
|
--output json \
|
|
|
|
| jq "$jqcode"
|
|
|
|
)
|
|
|
|
|
|
|
|
[[ "$JSON" -eq 1 ]] && echo "$json_results" && exit 0
|
|
|
|
|
|
|
|
JQ_COLS="[$( <<<"\"$TCDBCOLS\"" \
|
|
|
|
jq -r '. | split(",") | map( "\"\(.)\"" ) | join(",")'
|
|
|
|
)]"
|
|
|
|
JQ_COLS_REGEX="$( <<<"$JQ_COLS" \
|
|
|
|
jq -r '. | map ( ".\(.)" ) | join(",")'
|
|
|
|
)"
|
|
|
|
|
2023-07-12 04:53:28 +00:00
|
|
|
[[ -z "$NOHEAD" ]] || JQ_COLS=
|
|
|
|
|
2023-07-12 04:46:12 +00:00
|
|
|
jqcode='
|
|
|
|
['"${JQ_COLS^^}"'] + [.[] |
|
|
|
|
['"$JQ_COLS_REGEX"']
|
|
|
|
] | .[] | join("|")
|
|
|
|
'
|
|
|
|
|
|
|
|
<<<"$json_results" jq -s '.' | jq -r "$jqcode" | column -t -s "|"
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
|