From 168637a8a91c59ea6d2c946e6cc3bb11626e06fd Mon Sep 17 00:00:00 2001 From: Sam Cook Date: Mon, 12 Feb 2018 09:56:01 +0000 Subject: [PATCH 1/3] Wrap variables in double quotes As recommended by shellcheck wrap variables in double-quotes to avoid issues with spaces and other characters. --- bin/kubedecode | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/kubedecode b/bin/kubedecode index 286ec73..7502038 100755 --- a/bin/kubedecode +++ b/bin/kubedecode @@ -3,8 +3,8 @@ SECRET="$1" NAMESPACE="$2" -for row in $(echo $(kubectl get secret $SECRET -o json -n $NAMESPACE) | jq -c '.data | to_entries[]'); do - KEY=$(echo $row | jq -r '.key') - DECODED=$(echo $row | jq -r '.value' | base64 --decode) - echo $KEY: $DECODED +for row in $(kubectl get secret "${SECRET}" -o json -n "${NAMESPACE}" | jq -c '.data | to_entries[]'); do + KEY=$(echo "${row}" | jq -r '.key') + DECODED=$(echo "${row}" | jq -r '.value' | base64 --decode) + echo "${KEY}: ${DECODED}" done From 8771953d4cbee7d9201b1a875966c2674369f7f0 Mon Sep 17 00:00:00 2001 From: Sam Cook Date: Mon, 12 Feb 2018 10:14:16 +0000 Subject: [PATCH 2/3] Add guard to namespace If the namespace is not set use the current context's default instead. --- bin/kubedecode | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bin/kubedecode b/bin/kubedecode index 7502038..7747e93 100755 --- a/bin/kubedecode +++ b/bin/kubedecode @@ -1,7 +1,13 @@ #!/bin/bash SECRET="$1" -NAMESPACE="$2" + +if [[ -z "${2// }" ]]; then + NAMESPACE=$(kubectl config get-contexts --no-headers | tr -s ' ' | cut -d ' ' -f5) + echo "No namespace set, using default: ${NAMESPACE}" +else + NAMESPACE="$2" +fi for row in $(kubectl get secret "${SECRET}" -o json -n "${NAMESPACE}" | jq -c '.data | to_entries[]'); do KEY=$(echo "${row}" | jq -r '.key') From 573b089a3afed43cba84768ed57a3971ce329334 Mon Sep 17 00:00:00 2001 From: Sam Cook Date: Mon, 12 Feb 2018 10:15:01 +0000 Subject: [PATCH 3/3] Add usage and guard to secret If a secret is not passed raise an error and print usage. --- bin/kubedecode | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/bin/kubedecode b/bin/kubedecode index 7747e93..70d185a 100755 --- a/bin/kubedecode +++ b/bin/kubedecode @@ -1,5 +1,22 @@ #!/bin/bash +function usage(){ + echo -e "usage: ${0} SECRET [NAMESPACE]\n" + echo -e "SECRET The secret to decode" + echo -e "NAMESPACE The namespace to retrieve the secret from (optional," + echo -e " defaults to the namespace of the current context)." +} + +if [ "${1}" == "-h" ]; then + usage + exit +fi + +if [[ -z "${1// }" ]]; then + echo "ERROR: No secret set." + usage + exit 1 +fi SECRET="$1" if [[ -z "${2// }" ]]; then