Last active
June 27, 2024 11:38
-
-
Save judy2k/56e0574688df87cf96fbc7cc12a8b62d to your computer and use it in GitHub Desktop.
A script to generate a user for a MongoDB Atlas project.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Create a database user with a random password, and print out a MongoDB connection string for that database. | |
# | |
# Requires: python >= 3.6, jq, and atlas-cli | |
set -e | |
CLUSTER='Sandbox' | |
create_user() { | |
#username=$(fictionary -m 8 -x 8) | |
username="$1" | |
database="$2" | |
password=$(python3 -c 'import secrets; print(secrets.token_urlsafe(12))') | |
atlas dbuser create --username "${username}" --password "${password}" --role "readWrite@${database}" | |
complete_uri "${username}" "${password}" "${database}" | |
} | |
complete_uri() { | |
username="$1" | |
password="$2" | |
database="$3" | |
base_uri=$(atlas clusters connectionStrings describe ${CLUSTER} -o json | jq -r .standardSrv | sed "s#//#//${username}:${password}@#g") | |
uri="${base_uri}/${database}" | |
echo "Connection string copied to pasteboard." >&2 | |
echo "${uri}" | pbcopy | |
echo "${uri}" | |
} | |
main() { | |
case $# in | |
2) | |
username="$1" | |
database="$2" | |
create_user "${username}" "${database}" | |
;; | |
*) | |
echo 'Usage:' $(basename $0) 'USERNAME DBNAME' >&2 | |
;; | |
esac | |
} | |
# create_user | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment