-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Require authentication for clients #11
Open
geek
wants to merge
6
commits into
autopilotpattern:master
Choose a base branch
from
geek:auth
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
|
||
if [ -d "/data/db/_mongosetup" ]; then | ||
echo "/data/db/_mongosetup exists, mongo already setup, exiting" | ||
exit 0 | ||
fi | ||
|
||
echo "Start MongoDB without access control and only for local connections" | ||
mongod --fork --bind_ip 127.0.0.1 --logpath /dev/stdout | ||
|
||
echo "Create the user administrator." | ||
# The createUser will error if the user already exists. | ||
mongo admin --eval "db.createUser({ user: '${MONGO_USER}', pwd: '${MONGO_PASSWORD}', roles: [ { role: 'dbAdminAnyDatabase', db: 'admin' }, { role: 'clusterAdmin', db: 'admin' } ] });" | ||
|
||
|
||
echo "Shutdown the MongoDB service." | ||
mongod --shutdown | ||
|
||
|
||
echo "Creating keyFile for replication." | ||
echo -e ${MONGO_KEY} > /etc/mongod.key | ||
chmod 400 /etc/mongod.key | ||
|
||
echo "Create directory to designate that setup is complete." | ||
mkdir -p /data/db/_mongosetup |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
security: | ||
keyFile: "/etc/mongod.key" | ||
authorization: "enabled" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
#!/bin/bash | ||
set -e -o pipefail | ||
|
||
help() { | ||
echo | ||
echo 'Usage ./setup.sh ~/path/to/MANTA_PRIVATE_KEY ~/path/to/MONGO_KEYFILE' | ||
echo | ||
echo 'Checks that your Triton and Docker environment is sane and configures' | ||
echo 'an environment file to use.' | ||
echo | ||
echo 'MANTA_PRIVATE_KEY is the filesystem path to an SSH private key' | ||
echo 'used to connect to Manta for the database backups.' | ||
echo | ||
echo 'MONGO_KEYFILE is the filesystem path to a file that contains a secret' | ||
echo 'value between 6 and 1024 characters for authenticating replica members' | ||
echo | ||
echo 'Additional details must be configured in the _env file, but this script will properly' | ||
echo 'encode the SSH key details for use with this MongoDB image.' | ||
echo | ||
} | ||
|
||
|
||
# populated by `check` function whenever we're using Triton | ||
TRITON_USER= | ||
TRITON_DC= | ||
TRITON_ACCOUNT= | ||
|
||
# --------------------------------------------------- | ||
# Top-level commands | ||
|
||
# Check for correct configuration and setup _env file | ||
envcheck() { | ||
|
||
if [ -z "$1" ]; then | ||
tput rev # reverse | ||
tput bold # bold | ||
echo 'Please provide a path to a SSH private key to access Manta.' | ||
tput sgr0 # clear | ||
|
||
help | ||
exit 1 | ||
fi | ||
|
||
if [ ! -f "$1" ]; then | ||
tput rev # reverse | ||
tput bold # bold | ||
echo 'SSH private key for Manta is unreadable.' | ||
tput sgr0 # clear | ||
|
||
help | ||
exit 1 | ||
fi | ||
|
||
if [ -z "$2" ]; then | ||
tput rev # reverse | ||
tput bold # bold | ||
echo 'Please provide a path to a key file for MongoDB replica members.' | ||
tput sgr0 # clear | ||
|
||
help | ||
exit 1 | ||
fi | ||
|
||
if [ ! -f "$2" ]; then | ||
tput rev # reverse | ||
tput bold # bold | ||
echo 'MongoDB replica key file is unreadable.' | ||
tput sgr0 # clear | ||
|
||
help | ||
exit 1 | ||
fi | ||
|
||
# Assign args to named vars | ||
MANTA_PRIVATE_KEY_PATH=$1 | ||
MONGO_KEYFILE_PATH=$2 | ||
|
||
command -v docker >/dev/null 2>&1 || { | ||
echo | ||
tput rev # reverse | ||
tput bold # bold | ||
echo 'Docker is required, but does not appear to be installed.' | ||
tput sgr0 # clear | ||
echo 'See https://docs.joyent.com/public-cloud/api-access/docker' | ||
exit 1 | ||
} | ||
command -v json >/dev/null 2>&1 || { | ||
echo | ||
tput rev # reverse | ||
tput bold # bold | ||
echo 'Error! JSON CLI tool is required, but does not appear to be installed.' | ||
tput sgr0 # clear | ||
echo 'See https://apidocs.joyent.com/cloudapi/#getting-started' | ||
exit 1 | ||
} | ||
|
||
command -v triton >/dev/null 2>&1 || { | ||
echo | ||
tput rev # reverse | ||
tput bold # bold | ||
echo 'Error! Joyent Triton CLI is required, but does not appear to be installed.' | ||
tput sgr0 # clear | ||
echo 'See https://www.joyent.com/blog/introducing-the-triton-command-line-tool' | ||
exit 1 | ||
} | ||
|
||
# make sure Docker client is pointed to the same place as the Triton client | ||
local docker_user=$(docker info 2>&1 | awk -F": " '/SDCAccount:/{print $2}') | ||
local docker_dc=$(echo $DOCKER_HOST | awk -F"/" '{print $3}' | awk -F'.' '{print $1}') | ||
TRITON_USER=$(triton profile get | awk -F": " '/account:/{print $2}') | ||
TRITON_DC=$(triton profile get | awk -F"/" '/url:/{print $3}' | awk -F'.' '{print $1}') | ||
TRITON_ACCOUNT=$(triton account get | awk -F": " '/id:/{print $2}') | ||
if [ ! "$docker_user" = "$TRITON_USER" ] || [ ! "$docker_dc" = "$TRITON_DC" ]; then | ||
echo | ||
tput rev # reverse | ||
tput bold # bold | ||
echo 'Error! The Triton CLI configuration does not match the Docker CLI configuration.' | ||
tput sgr0 # clear | ||
echo | ||
echo "Docker user: ${docker_user}" | ||
echo "Triton user: ${TRITON_USER}" | ||
echo "Docker data center: ${docker_dc}" | ||
echo "Triton data center: ${TRITON_DC}" | ||
exit 1 | ||
fi | ||
|
||
local triton_cns_enabled=$(triton account get | awk -F": " '/cns/{print $2}') | ||
if [ ! "true" == "$triton_cns_enabled" ]; then | ||
echo | ||
tput rev # reverse | ||
tput bold # bold | ||
echo 'Error! Triton CNS is required and not enabled.' | ||
tput sgr0 # clear | ||
echo | ||
exit 1 | ||
fi | ||
|
||
# setup environment file | ||
if [ ! -f "_env" ]; then | ||
echo '# Environment variables for MongoDB service' > _env | ||
echo 'MONGO_USER=dbuser' >> _env | ||
echo 'MONGO_PASSWORD='$(cat /dev/urandom | LC_ALL=C tr -dc 'A-Za-z0-9' | head -c 7) >> _env | ||
echo MONGO_KEY=$(cut -c1-100 ${MONGO_KEYFILE_PATH} | tr '\n' '1' | tr '-' '1') >> _env | ||
echo >> _env | ||
|
||
echo '# Environment variables for backups to Manta' >> _env | ||
echo 'MANTA_URL=https://us-east.manta.joyent.com' >> _env | ||
echo 'MANTA_BUCKET= # an existing Manta bucket' >> _env | ||
echo 'MANTA_USER= # a user with access to that bucket' >> _env | ||
echo 'MANTA_SUBUSER=' >> _env | ||
echo 'MANTA_ROLE=' >> _env | ||
|
||
# MANTA_KEY_ID must be the md5 formatted key fingerprint. A SHA256 will result in errors. | ||
set +o pipefail | ||
# The -E option was added to ssh-keygen recently; if it doesn't work, then | ||
# assume we're using an older version of ssh-keygen that only outputs MD5 fingerprints | ||
ssh-keygen -yl -E md5 -f ${MANTA_PRIVATE_KEY_PATH} > /dev/null 2>&1 | ||
if [ $? -eq 0 ]; then | ||
echo MANTA_KEY_ID=$(ssh-keygen -yl -E md5 -f ${MANTA_PRIVATE_KEY_PATH} | awk '{print substr($2,5)}') >> _env | ||
else | ||
echo MANTA_KEY_ID=$(ssh-keygen -yl -f ${MANTA_PRIVATE_KEY_PATH} | awk '{print $2}') >> _env | ||
fi | ||
set -o pipefail | ||
|
||
# munge the private key so that we can pass it into an env var sanely | ||
# and then unmunge it in our startup script | ||
echo MANTA_PRIVATE_KEY=$(cat ${MANTA_PRIVATE_KEY_PATH} | tr '\n' '#') >> _env | ||
echo >> _env | ||
|
||
echo '# Consul discovery via Triton CNS' >> _env | ||
echo CONSUL=mongodb-consul.svc.${TRITON_ACCOUNT}.${TRITON_DC}.cns.joyent.com >> _env | ||
echo >> _env | ||
|
||
echo 'Edit the _env file with your desired MONGO_* and MANTA_* config' | ||
else | ||
echo 'Existing _env file found, exiting' | ||
exit | ||
fi | ||
} | ||
|
||
get_root_password() { | ||
echo $(docker logs ${COMPOSE_PROJECT_NAME:-mongodb}_mongodb_1 2>&1 | \ | ||
awk '/Generated root password/{print $NF}' | \ | ||
awk '{$1=$1};1' | ||
) | pbcopy | ||
} | ||
|
||
|
||
|
||
# --------------------------------------------------- | ||
# parse arguments | ||
|
||
# Get function list | ||
funcs=($(declare -F -p | cut -d " " -f 3)) | ||
|
||
until | ||
if [ ! -z "$1" ]; then | ||
# check if the first arg is a function in this file, or use a default | ||
if [[ " ${funcs[@]} " =~ " $1 " ]]; then | ||
cmd=$1 | ||
shift 1 | ||
else | ||
cmd="envcheck" | ||
fi | ||
|
||
$cmd "$@" | ||
if [ $? == 127 ]; then | ||
help | ||
fi | ||
|
||
exit | ||
else | ||
help | ||
fi | ||
do | ||
echo | ||
done |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to have a
set -e
here so that failures of individual lines will cause the script to stop.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated