diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9b90e3520..06bc413ac 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -91,18 +91,24 @@ It will install `goimports`, `goreleaser`, `golangci-lint` and `reviewdog`. The testing sandbox starts `n` MongoDB instances as follows: -- 3 Instances for shard 1 at ports 17001, 17002, 17003 -- 3 instances for shard 2 at ports 17004, 17005, 17006 +- 3 Instances for shard 1 at ports 17001, 17002, 17003 (with no authentication) +- 3 instances for shard 2 at ports 17004, 17005, 17006 (with authentication enabled) - 3 config servers at ports 17007, 17008, 17009 - 1 mongos server at port 17000 - 1 stand alone instance at port 27017 -All instances are currently running without user and password so for example, to connect to the **mongos** you can just use: +To connect to the **mongos** on shard 1, you can use: ``` mongo mongodb://127.0.0.1:17001/admin ``` +To connect to the **mongos** on shard 2 (with authentication enabled), you can use: + +``` +mongo mongodb://admin:admin@127.0.0.1:17001/admin +``` + The sandbox can be started using the provided Makefile using: `make test-cluster` and it can be stopped using `make test-cluster-clean`. ### Running tests diff --git a/Makefile b/Makefile index 25866ddcd..882fd593a 100644 --- a/Makefile +++ b/Makefile @@ -104,7 +104,7 @@ test-race: env ## Run all tests with race flag. go test -race -v -timeout 30s ./... test-cluster: env ## Starts MongoDB test cluster. Use env var TEST_MONGODB_IMAGE to set flavor and version. Example: TEST_MONGODB_IMAGE=mongo:3.6 make test-cluster - docker compose up -d + docker compose up --build -d test-cluster-clean: env ## Stops MongoDB test cluster. docker compose down --remove-orphans diff --git a/docker-compose.yml b/docker-compose.yml index b779c2b40..411c0964a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -63,37 +63,52 @@ services: mongo-2-2: container_name: "mongo-2-2" - image: ${TEST_MONGODB_IMAGE:-mongo:4.2} + build: + dockerfile: ./docker/mongodb-auth.dockerfile + args: + TEST_MONGODB_IMAGE: ${TEST_MONGODB_IMAGE} + environment: + - MONGO_INITDB_ROOT_USERNAME=${TEST_MONGODB_USERNAME:-admin} + - MONGO_INITDB_ROOT_PASSWORD=${TEST_MONGODB_PASSWORD:-admin} ports: - "${TEST_MONGODB_S2_PRIMARY_PORT:-17004}:27017" - command: mongod --replSet rs2 --shardsvr --port 27017 --oplogSize 16 + command: mongod --replSet rs2 --port 27017 --oplogSize 16 --auth --keyFile=/opt/keyfile networks: - rs2 mongo-2-3: container_name: "mongo-2-3" - image: ${TEST_MONGODB_IMAGE:-mongo:4.2} + build: + dockerfile: ./docker/mongodb-auth.dockerfile + args: + TEST_MONGODB_IMAGE: ${TEST_MONGODB_IMAGE} ports: - "${TEST_MONGODB_S2_SECONDARY1_PORT:-17005}:27017" - command: mongod --replSet rs2 --shardsvr --port 27017 --oplogSize 16 + command: mongod --replSet rs2 --port 27017 --oplogSize 16 --auth --keyFile=/opt/keyfile networks: - rs2 mongo-2-1: container_name: "mongo-2-1" - image: ${TEST_MONGODB_IMAGE:-mongo:4.2} + build: + dockerfile: ./docker/mongodb-auth.dockerfile + args: + TEST_MONGODB_IMAGE: ${TEST_MONGODB_IMAGE} ports: - "${TEST_MONGODB_S2_SECONDARY2_PORT:-17006}:27017" - command: mongod --replSet rs2 --shardsvr --port 27017 --oplogSize 16 + command: mongod --replSet rs2 --port 27017 --oplogSize 16 --auth --keyFile=/opt/keyfile networks: - rs2 mongo-2-arbiter: container_name: "mongo-2-arbiter" - image: ${TEST_MONGODB_IMAGE:-mongo:4.2} + build: + dockerfile: ./docker/mongodb-auth.dockerfile + args: + TEST_MONGODB_IMAGE: ${TEST_MONGODB_IMAGE} ports: - "${TEST_MONGODB_S2_ARBITER:-17012}:27017" - command: mongod --replSet rs1 --shardsvr --port 27017 --oplogSize 16 + command: mongod --replSet rs2 --port 27017 --oplogSize 16 --auth --keyFile=/opt/keyfile networks: - rs2 @@ -114,6 +129,8 @@ services: - ARBITER=mongo-2-arbiter - RS=rs2 - VERSION=${TEST_MONGODB_IMAGE} + - MONGO_INITDB_ROOT_USERNAME=${TEST_MONGODB_USERNAME:-admin} + - MONGO_INITDB_ROOT_PASSWORD=${TEST_MONGODB_PASSWORD:-admin} entrypoint: [ "/scripts/setup.sh" ] networks: - rs2 diff --git a/docker/mongodb-auth.dockerfile b/docker/mongodb-auth.dockerfile new file mode 100644 index 000000000..4c15257ca --- /dev/null +++ b/docker/mongodb-auth.dockerfile @@ -0,0 +1,6 @@ +ARG TEST_MONGODB_IMAGE=mongo:4.2 +FROM ${TEST_MONGODB_IMAGE} +USER root +COPY docker/secret/keyfile /opt/keyfile +RUN chown mongodb /opt/keyfile && chmod 400 /opt/keyfile && mkdir -p /home/mongodb/ && chown mongodb /home/mongodb +USER mongodb diff --git a/docker/scripts/setup.sh b/docker/scripts/setup.sh index c432bdcbe..f263462cb 100755 --- a/docker/scripts/setup.sh +++ b/docker/scripts/setup.sh @@ -4,6 +4,7 @@ MONGODB_CLIENT="mongosh --quiet" PARSED=(${VERSION//:/ }) MONGODB_VERSION=${PARSED[1]} MONGODB_VENDOR=${PARSED[0]} + if [ "`echo ${MONGODB_VERSION} | cut -c 1`" = "4" ]; then MONGODB_CLIENT="mongo" fi @@ -17,6 +18,9 @@ mongodb2=`getent hosts ${MONGO2} | awk '{ print $1 }'` mongodb3=`getent hosts ${MONGO3} | awk '{ print $1 }'` arbiter=`getent hosts ${ARBITER} | awk '{ print $1 }'` +username=${MONGO_INITDB_ROOT_USERNAME} +password=${MONGO_INITDB_ROOT_PASSWORD} + port=${PORT:-27017} echo "Waiting for startup.." @@ -60,7 +64,11 @@ EOF function general_servers() { echo "setup servers on ${MONGO1}(${mongodb1}:${port})" - ${MONGODB_CLIENT} --host ${mongodb1}:${port} <