Skip to content

Commit

Permalink
feat(create-db-secret): ensure random and secured (#910)
Browse files Browse the repository at this point in the history
* feat(create-db-secret): ensure random and secured

* Update azure-db/bin/create-db-secret

Co-authored-by: Julien Bouquillon <contact@revolunet.com>

* Update azure-db/bin/create-db-secret

Co-authored-by: Julien Bouquillon <contact@revolunet.com>

Co-authored-by: Julien Bouquillon <contact@revolunet.com>
  • Loading branch information
devthejo and Julien Bouquillon committed Nov 23, 2021
1 parent 51b51f0 commit f36a389
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions azure-db/bin/create-db-secret
@@ -0,0 +1,21 @@
#!/bin/bash
set -e


MANDATORY_VARS="K8S_NS PGPASSWORD_SECRET_NAME"
for VAR in $MANDATORY_VARS; do
if [[ -z "${!VAR}" ]]; then
echo "${VAR} environment variable is empty"
exit 1
fi
done

if [ -n "$(kubectl -n $K8S_NS get secret $PGPASSWORD_SECRET_NAME 2>/dev/null)" ]; then
echo "PGPASSWORD secret named '$PGPASSWORD_SECRET_NAME' already exists in namespace '$K8S_NS'"
exit 0
fi

PGPASSWORD=$(node -e "console.log(require('crypto').randomBytes(16).toString('base64').replace(/[^a-z0-9]/gi , ''))")
kubectl -n $K8S_NS create secret generic $PGPASSWORD_SECRET_NAME --from-literal=PGPASSWORD=$PGPASSWORD

echo "PGPASSWORD secret named '$PGPASSWORD_SECRET_NAME' created in namespace '$K8S_NS'"

0 comments on commit f36a389

Please sign in to comment.