@@ -15,6 +15,7 @@ COMMENT
1515# set -eou pipefail
1616
1717ASSETS_PATH=${ASSETS_PATH:-/ usr/ share/ extras/ }
18+ MTLS_CERT_PATH=${MTLS_CERT_PATH:-/ etc/ ssl/ mongodb/ ca.pem}
1819
1920MONGODB_DATABASES=(
2021 " archive"
@@ -37,7 +38,7 @@ disableMongoTelemetry() {
3738
3839waitForMongoDB () {
3940 while true ; do
40- status=$( mongosh ${MONGODB_ROOT_URI} --eval " db.adminCommand('ping')" 2>&1 )
41+ status=$( mongosh ${MONGODB_ROOT_URI} ${MONGO_URI_EXTRA_PARAMS} --eval " db.adminCommand('ping')" 2>&1 )
4142
4243 echo -e " MongoDB status:\n$status "
4344 if $( echo $status | grep ' ok: 1' -q) ; then
@@ -54,12 +55,22 @@ parseMongoURI() {
5455 local parameters=" $( echo $1 | grep ' ?' | cut -d ' ?' -f2) " ; if [[ -n $parameters ]]; then parameters=" ?${parameters} " ; fi
5556 local url=" $( echo ${1/ $proto / } ) "
5657 local userpass=" $( echo $url | grep @ | cut -d@ -f1) "
57- local hostport=" $( echo $url | sed s/$userpass // | sed " s/\/\?$parameters //" | sed -re " s/\/\?|@//g" | sed ' s/\/$//' ) "
58+ if [[ -z $userpass ]]; then
59+ local hostport=" $( echo $url | sed " s/\/\?$parameters //" | sed -re " s/\/\?|@//g" | sed ' s/\/$//' ) "
60+ MONGO_URI=" $proto$hostport /${MONGODB_DATABASE} $parameters "
61+ else
62+ local hostport=" $( echo $url | sed s/$userpass // | sed " s/\/\?$parameters //" | sed -re " s/\/\?|@//g" | sed ' s/\/$//' ) "
63+ MONGODB_PASSWORD=" $( echo $userpass | grep : | cut -d: -f2) "
64+ MONGODB_USER=" $( echo $userpass | grep : | cut -d: -f1) "
65+ MONGO_URI=" $proto$userpass @$hostport /${MONGODB_DATABASE} $parameters "
66+ fi
67+
68+ if [[ -z $MONGODB_ROOT_OPTIONS ]]; then
69+ MONGODB_ROOT_URI=" $proto ${MONGODB_ROOT_USER} :${MONGODB_ROOT_PASSWORD} @$hostport /admin$parameters "
70+ else
71+ MONGODB_ROOT_URI=" $proto ${MONGODB_ROOT_USER} :${MONGODB_ROOT_PASSWORD} @$hostport /admin?${MONGODB_ROOT_OPTIONS} "
72+ fi
5873
59- MONGODB_PASSWORD=" $( echo $userpass | grep : | cut -d: -f2) "
60- MONGODB_USER=" $( echo $userpass | grep : | cut -d: -f1) "
61- MONGO_URI=" $proto$userpass @$hostport /${MONGODB_DATABASE} $parameters "
62- MONGODB_ROOT_URI=" $proto ${MONGODB_ROOT_USER} :${MONGODB_ROOT_PASSWORD} @$hostport /admin$parameters "
6374}
6475
6576getMongoVersion () {
@@ -68,6 +79,14 @@ getMongoVersion() {
6879
6980parseMongoURI $MONGO_URI
7081
82+ if [[ -s ${MTLS_CERT_PATH} ]]; then
83+ MONGO_URI_EXTRA_PARAMS=" --tls --tlsCertificateKeyFile ${MTLS_CERT_PATH} --tlsAllowInvalidHostnames --tlsAllowInvalidCertificates"
84+ MONGOIMPORT_EXTRA_PARAMS=" --ssl --sslPEMKeyFile ${MTLS_CERT_PATH} --sslAllowInvalidHostnames --sslAllowInvalidCertificates"
85+ else
86+ MONGO_URI_EXTRA_PARAMS=" "
87+ MONGOIMPORT_EXTRA_PARAMS=" "
88+ fi
89+
7190disableMongoTelemetry
7291
7392waitForMongoDB
@@ -76,15 +95,15 @@ getMongoVersion
7695
7796for MONGODB_DATABASE in ${MONGODB_DATABASES[@]} ; do
7897 waitForMongoDB
79- mongosh ${MONGODB_ROOT_URI} --eval " db.getSiblingDB(\" ${MONGODB_DATABASE} \" ).createUser({user: \" ${MONGODB_USER} \" , pwd: \" ${MONGODB_PASSWORD} \" , roles: [\" readWrite\" ]})" 2>&1 || true
98+ mongosh ${MONGODB_ROOT_URI} ${MONGO_URI_EXTRA_PARAMS} --eval " db.getSiblingDB(\" ${MONGODB_DATABASE} \" ).createUser({user: \" ${MONGODB_USER} \" , pwd: \" ${MONGODB_PASSWORD} \" , roles: [\" readWrite\" ]})" 2>&1 || true
8099 waitForMongoDB
81- mongosh ${MONGODB_ROOT_URI} --eval " db.getSiblingDB(\" ${MONGODB_DATABASE} \" ).changeUserPassword(\" ${MONGODB_USER} \" ,\" ${MONGODB_PASSWORD} \" )" 2>&1 || true
100+ mongosh ${MONGODB_ROOT_URI} ${MONGO_URI_EXTRA_PARAMS} --eval " db.getSiblingDB(\" ${MONGODB_DATABASE} \" ).changeUserPassword(\" ${MONGODB_USER} \" ,\" ${MONGODB_PASSWORD} \" )" 2>&1 || true
82101done
83102
84- mongosh ${MONGODB_ROOT_URI} --eval " db.getSiblingDB(\" codefresh\" ).grantRolesToUser( \" ${MONGODB_USER} \" , [ { role: \" readWrite\" , db: \" pipeline-manager\" } ] )" 2>&1 || true
85- mongosh ${MONGODB_ROOT_URI} --eval " db.getSiblingDB(\" codefresh\" ).grantRolesToUser( \" ${MONGODB_USER} \" , [ { role: \" readWrite\" , db: \" platform-analytics-postgres\" } ] )" 2>&1 || true
86- mongosh ${MONGODB_ROOT_URI} --eval " db.getSiblingDB(\" codefresh\" ).changeUserPassword(\" ${MONGODB_USER} \" ,\" ${MONGODB_PASSWORD} \" )" 2>&1 || true
103+ mongosh ${MONGODB_ROOT_URI} ${MONGO_URI_EXTRA_PARAMS} --eval " db.getSiblingDB(\" codefresh\" ).grantRolesToUser( \" ${MONGODB_USER} \" , [ { role: \" readWrite\" , db: \" pipeline-manager\" } ] )" 2>&1 || true
104+ mongosh ${MONGODB_ROOT_URI} ${MONGO_URI_EXTRA_PARAMS} --eval " db.getSiblingDB(\" codefresh\" ).grantRolesToUser( \" ${MONGODB_USER} \" , [ { role: \" readWrite\" , db: \" platform-analytics-postgres\" } ] )" 2>&1 || true
105+ mongosh ${MONGODB_ROOT_URI} ${MONGO_URI_EXTRA_PARAMS} --eval " db.getSiblingDB(\" codefresh\" ).changeUserPassword(\" ${MONGODB_USER} \" ,\" ${MONGODB_PASSWORD} \" )" 2>&1 || true
87106
88- mongoimport --uri ${MONGO_URI} --collection idps --type json --legacy --file ${ASSETS_PATH} idps.json
89- mongoimport --uri ${MONGO_URI} --collection accounts --type json --legacy --file ${ASSETS_PATH} accounts.json
90- mongoimport --uri ${MONGO_URI} --collection users --type json --legacy --file ${ASSETS_PATH} users.json
107+ mongoimport --uri ${MONGO_URI} ${MONGOIMPORT_EXTRA_PARAMS} --collection idps --type json --legacy --file ${ASSETS_PATH} idps.json
108+ mongoimport --uri ${MONGO_URI} ${MONGOIMPORT_EXTRA_PARAMS} --collection accounts --type json --legacy --file ${ASSETS_PATH} accounts.json
109+ mongoimport --uri ${MONGO_URI} ${MONGOIMPORT_EXTRA_PARAMS} --collection users --type json --legacy --file ${ASSETS_PATH} users.json
0 commit comments