w9jds / firebase-action

GitHub Action for interacting with Firebase
MIT License
927 stars 200 forks source link

Use jq to validate a json instead of checking base64 encoded string #120

Closed banyan closed 3 years ago

banyan commented 3 years ago

Ref: https://github.com/w9jds/firebase-action/issues/119

set -e

BASE64_PATTERN="([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)"

base64_1=`echo 'foo' | base64` # Zm9vCg==
base64_2=`echo 'fooo' | base64` # Zm9vbwo=
base64_3=`echo 'foooo' | base64` # Zm9vb28K

if [[ $base64_1 =~ $BASE64_PATTERN ]]; then
    echo "matched base64_1"
fi

if [[ $base64_2 =~ $BASE64_PATTERN ]]; then
    echo "matched base64_2"
fi

if [[ $base64_3 =~ $BASE64_PATTERN ]]; then
    echo "matched base64_3"
fi
❯ sh test.sh
matched base64_1
matched base64_2

current BASE64_PATTERN doesn't match when = is missing. How about validating if given string is json or not?

❯ GCP_SA_KEY=`cat gcp_sa_key.json` sh a.sh
Storing GCP_SA_KEY in /opt/gcp_key.json
❯ GCP_SA_KEY=`cat gcp_sa_key.json | base64` sh a.sh
Storing the decoded GCP_SA_KEY in /opt/gcp_key.json
w9jds commented 3 years ago

Testing now, but looks like a better solution.