testcontainers-node icon indicating copy to clipboard operation
testcontainers-node copied to clipboard

@testcontainers/mongodb - how can creds to require be passed?

Open osher opened this issue 7 months ago • 6 comments

I cannot find an example for it in the docs.

I tried to

const mongo = new MongoDBContainer()
      .withEnvironment({ 
         MONGO_INITDB_ROOT_USERNAME: dbUsername, 
         MONGO_INITDB_ROOT_PASSWORD: dbPassword,
      })
      .start();

but it appears that mongo.getConnectionString() does not include the creds...

osher avatar Apr 15 '25 17:04 osher

btw, my workaround currenty is:

const mongo = new GenericContainer('mongo:4.0.1')
        .withExposedPorts(27017)
        .withEnvironment({ MONGO_INITDB_ROOT_USERNAME: dbUsername })
        .withEnvironment({ MONGO_INITDB_ROOT_PASSWORD: dbPassword })
        .start(),

and I'm composing the connection string myself using mongo.getMappedPort(27017) ...

osher avatar Apr 15 '25 17:04 osher

Seems the MongoDB module does not support custom creds. A PR would be welcome. The implementation could be similar to https://github.com/testcontainers/testcontainers-node/blob/main/packages/modules/mysql/src/mysql-container.ts#L40-L41

cristianrgreco avatar Apr 15 '25 19:04 cristianrgreco

I tested simple implementation with username & password, but that actually fails due to replSet option set in constructor. It seems the current commands for setting up replication wont work if authentication is enabled. Did not find fast fix for that one. Adding username & password to the evalcommands seemed to result in authentication failing. Unsure why, could be race condition from user being created vs the command being run,

Removing replication from constructor would also be a breaking change. Originally it would've probably been better to create the MongoDB container with replication as an option, not default behaviour.

Might come back to this issue later, just FYI for others considering solving it.

stscoundrel avatar Apr 19 '25 16:04 stscoundrel

I wondered how this was implemented in TestContainers of other languages.

Mixed support:

  • Python supports credentials, but no replication.
  • Java supports replication, but no credentials.

Both supported:

  • Go supports both, using keyfile
  • .NET supports both, using keyfile

All in all, somewhat unfortunate that different language versions have such different implementations.

Considering that all that support both are using keyfiles, the authentication error I faced might've been about that instead of user creation race condition.

I tried adding keyfiles, but still faced auth problem. I also added wait for auth and that seemed to solve the issue. Will open PR about this once cleaned up.

stscoundrel avatar Apr 20 '25 08:04 stscoundrel

To complicate the matter a bit more, it seems my earlier working solution only worked on my local docker, not on CI 😅 In hindsight, that may have been a false positive originally, as I've struggled to replicate the working poc since.

I tried quite a few approaches, but getting the auth to work with replication seems to be oddly complex. To quote official docs for MongoDB:

Authentication in MongoDB is fairly complex

It would appear that I got the following done:

  • Create the user via the env variables (cant see why this would fail)
  • Create the keyfile & set it up in container (had some troubles with permissions originally, but that error seems to be gone now)

But where it fails is actually authenticating using those credentials. For example, simply running the command for setting up replication. The auth seems to fail without much of a descriptive error.

I feel I've wasted enough time with this for now. Might get back to it at some later time, but currently seems like getting both auth and replication to work using the current container seems oddly flaky.

stscoundrel avatar Apr 21 '25 07:04 stscoundrel

This might not be a solution that the issue author wants to hear, but I use a GenericContainer for Mongodb. This makes running newer version and using credentials a lot easier, if you don't need replication.

prenaissance avatar May 13 '25 16:05 prenaissance

Don't give up. There's a limit to how far one can get with workarounds...

osher avatar Jul 09 '25 08:07 osher

Released in 11.4.0. Thanks @digital88

cristianrgreco avatar Jul 23 '25 13:07 cristianrgreco