Skip to content

Commit beb7f95

Browse files
committed
Use MongoDB auth from env vars if available
If MONGODB_USER and MONGODB_PASS are set, they will be used to authenticate to the MongoDB server. If they are not set, then a connection request with no authentication will be sent (as per the existing behavior). This allows us to deploy this change, then set MONGODB_USER and MONGODB_PASS later and have those changes picked up without a redeployment. Note that a corresponding change to LfMerge will also be needed.
1 parent 9479df3 commit beb7f95

File tree

5 files changed

+16
-2
lines changed

5 files changed

+16
-2
lines changed

docker-compose.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ services:
5656
- ENVIRONMENT=development
5757
- WEBSITE=localhost
5858
- DATABASE=scriptureforge
59-
- MONGODB_CONN=mongodb://lf-db:27017
59+
- MONGODB_CONN=mongodb://lf-db:27017&authSource=admin
60+
- MONGODB_USER=admin
61+
- MONGODB_PASS=pass
6062
- MAIL_HOST=mail
6163
- GOOGLE_CLIENT_ID=bogus-development-token
6264
- GOOGLE_CLIENT_SECRET=bogus-development-token

scripts/scriptsConfig.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@
1010

1111
define("DATABASE", Env::requireEnv("DATABASE"));
1212
define("MONGODB_CONN", Env::requireEnv("MONGODB_CONN"));
13+
define("MONGODB_USER", Env::get("MONGODB_USER"));
14+
define("MONGODB_PASS", Env::get("MONGODB_PASS"));
1315
define("BCRYPT_COST", 7);

src/Api/Model/Shared/Mapper/MongoStore.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ public static function connect($databaseName)
1818
if (static::$_mongoClient == null) {
1919
// MongoDB Client that will unserialize everything as PHP Arrays consistent with the legacy driver (which our code was built on)
2020
// see http://mongodb.github.io/mongo-php-library/classes/client/#example
21+
$options = [];
22+
if (defined(MONGODB_USER) && defined(MONGODB_PASS)) {
23+
if (MONGODB_USER != null && MONGODB_PASS != null) {
24+
$options = [ 'username' => MONGODB_USER, 'password' => MONGODB_PASS ];
25+
}
26+
}
2127
static::$_mongoClient = new Client(
2228
MONGODB_CONN,
23-
[],
29+
$options,
2430
["typeMap" => ["root" => "array", "document" => "array", "array" => "array"]]
2531
);
2632
}

src/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
define("ENVIRONMENT", Env::requireEnv("ENVIRONMENT"));
66
define("DATABASE", Env::requireEnv("DATABASE"));
77
define("MONGODB_CONN", Env::requireEnv("MONGODB_CONN"));
8+
define("MONGODB_USER", Env::get("MONGODB_USER"));
9+
define("MONGODB_PASS", Env::get("MONGODB_PASS"));
810
define("LANGUAGE_DEPOT_API_TOKEN", Env::requireEnv("LANGUAGE_DEPOT_API_TOKEN"));
911

1012
define("BCRYPT_COST", 7);

test/php/TestConfig.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
define("DATABASE", Env::requireEnv("DATABASE"));
2121
define("MONGODB_CONN", Env::requireEnv("MONGODB_CONN"));
22+
define("MONGODB_USER", Env::get("MONGODB_USER"));
23+
define("MONGODB_PASS", Env::get("MONGODB_PASS"));
2224
define("SF_TESTPROJECT", "Test Project");
2325
define("SF_TESTPROJECTCODE", "testcode1");
2426
define("SF_TESTPROJECT2", "Test Project2");

0 commit comments

Comments
 (0)