forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: mongodb replicaset should work with auth (testcontainers#2847)
* added custom entrypoint to setup keyfile with proper user permissions * added testcases for replicaset with auth * implementation to support replicaset with auth * removed unnecessary files * cleanup * added autodetection for user:group and entrypoint * added tests for replica set and auth for different images * renamed entrypoint to differentiate between custom entrypoint of testcontainers * code cleanup * renamed newly added tests * fixed names of tests to use slash separated options * fix: lint --------- Co-authored-by: Manuel de la Peña <[email protected]>
- Loading branch information
1 parent
b7511cd
commit 11eb809
Showing
4 changed files
with
218 additions
and
34 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package mongodb | ||
|
||
import "fmt" | ||
|
||
// mongoCli is cli to interact with MongoDB. If username and password are provided | ||
// it will use credentials to authenticate. | ||
type mongoCli struct { | ||
mongoshBaseCmd string | ||
mongoBaseCmd string | ||
} | ||
|
||
func newMongoCli(username string, password string) mongoCli { | ||
authArgs := "" | ||
if username != "" && password != "" { | ||
authArgs = fmt.Sprintf("--username %s --password %s", username, password) | ||
} | ||
|
||
return mongoCli{ | ||
mongoshBaseCmd: fmt.Sprintf("mongosh %s --quiet", authArgs), | ||
mongoBaseCmd: fmt.Sprintf("mongo %s --quiet", authArgs), | ||
} | ||
} | ||
|
||
func (m mongoCli) eval(command string, args ...any) []string { | ||
command = "\"" + fmt.Sprintf(command, args...) + "\"" | ||
|
||
return []string{ | ||
"sh", | ||
"-c", | ||
m.mongoshBaseCmd + " --eval " + command + " || " + m.mongoBaseCmd + " --eval " + command, | ||
} | ||
} |
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,32 @@ | ||
#!/bin/bash | ||
|
||
set -Eeuo pipefail | ||
|
||
# detect mongo user and group | ||
function get_user_group() { | ||
user_group=$(cut -d: -f1,5 /etc/passwd | grep mongo) | ||
echo "${user_group}" | ||
} | ||
|
||
# detect the entrypoint | ||
function get_entrypoint() { | ||
entrypoint=$(find /usr/local/bin -name 'docker-entrypoint.*') | ||
if [[ "${entrypoint}" == *.py ]]; then | ||
entrypoint="python3 ${entrypoint}" | ||
else | ||
entrypoint="exec ${entrypoint}" | ||
fi | ||
echo "${entrypoint}" | ||
} | ||
|
||
ENTRYPOINT=$(get_entrypoint) | ||
MONGO_USER_GROUP=$(get_user_group) | ||
|
||
# Create the keyfile | ||
openssl rand -base64 756 > "${MONGO_KEYFILE}" | ||
|
||
# Set the permissions and ownership of the keyfile | ||
chown "${MONGO_USER_GROUP}" "${MONGO_KEYFILE}" | ||
chmod 400 "${MONGO_KEYFILE}" | ||
|
||
${ENTRYPOINT} "$@" |