Set authSource in MongoDB Docker Compose connection details

When authentication credentials are provided in environment variables
for the official MongoDB Docker image, the authentication source must
also be set to the `admin` database.

Fixes gh-35777
This commit is contained in:
Scott Frederick
2023-06-08 16:20:57 -05:00
parent 409eb055bb
commit 8f31a5772e
2 changed files with 11 additions and 2 deletions

View File

@@ -30,6 +30,7 @@ import org.springframework.boot.docker.compose.service.connection.DockerComposeC
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
*/
class MongoDockerComposeConnectionDetailsFactory extends DockerComposeConnectionDetailsFactory<MongoConnectionDetails> {
@@ -73,6 +74,9 @@ class MongoDockerComposeConnectionDetailsFactory extends DockerComposeConnection
builder.append(service.ports().get(MONGODB_PORT));
builder.append("/");
builder.append((environment.getDatabase() != null) ? environment.getDatabase() : "test");
if (environment.getUsername() != null) {
builder.append("?authSource=admin");
}
return new ConnectionString(builder.toString());
}