Question

Apps and MongoDB managed connection with Mongoose Fails

Hi, I’m trying to port our dockerized app wrote in NestJS and using Mongoose to app platform. Currently I’m facing errors when connecting to managed MongoDB database.

I’ve tested locally using the params mongo.CA_CERT and mongo.DATABASE_URL passed to the app changing the connection string to ${mongo.DATABASE_URL}&tlsCAFile=ca-certificate.crt and saving the certificate as ca-certificate.crt in the root of the project, and everything works correctly.

When I do this on app platform using this configuration:

databases:
  
  - name: mongo
    engine: MONGODB
    production: true
    cluster_name: ....
    db_name: ....

envs:
 - key: DATABASE_URI
    value: ${mongo.DATABASE_URL}
    scope: RUN_TIME
    type: GENERAL

  - key: DATABASE_CERT
    value: ${mongo.CA_CERT}
    scope: RUN_TIME
    type: GENERAL

And using as CMD an executable file with this commands

echo "Database connection string"
echo $DATABASE_URI
echo "Creating cert file..."
echo "${DATABASE_CERT}" > ca-certificate.crt
echo "Created file ca-certificate.crt"
cat ca-certificate.crt
echo "Starting seeding..."
npm run console:prod -- seeder .....
echo "Seeding done."
echo "Starting Server..." 
node dist/apps/manager-api/main.js

Everyting is printed correctly and the server starts… but cannot connect to the database.

Also I tryied to use the run_command to avoid to put the logic for the cert inside the docker container, but even using something like echo "${DATABASE_CERT}" > ca-certificate.crt && node dist/apps/manager-api/main.js fails with a Deploy Error: Non-Zero Exit Code and no logs…

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

OK, for the run_command part I think that it’s an issue with docker and how it manages commands… probably I should pass it as an array

For the Database I think I found a bug in the system. In the last few days I’ve done some heavy testing destroying and recreating apps, and I think something broke in the db management. When deploying an app, a user is created in the DB. This user is the same in the connection string and in the dashboard of the database section, but accessing directly the DB shows an user with a different name, blocking the connection.

This is the logs of the failed connection from DB

{
	"timestamp" : ISODate("2022-03-18T12:31:11.795+01:00"),
	"severity" : "Info",
	"component" : "ACCESS",
	"id" : 20249,
	"context" : "conn498842",
	"message" : "Authentication failed",
	"duration_ms" : undefined,
	"namespace" : undefined,
	"operation" : undefined,
	"appName" : undefined,
	"attr" : {
		"mechanism" : "SCRAM-SHA-1",
		"speculative" : false,
		"principalName" : "doapps-6b6eaa87-acec-4251-993f-b9eadce72666",
		"authenticationDatabase" : "admin",
		"remote" : "138.68.80.26:16636",
		"extraInfo" : {
			
		},
		"error" : "UserNotFound: Could not find user \"doapps-6b6eaa87-acec-4251-993f-b9eadce72666\" for db \"admin\""
	}
}

This is the only user starting with doapps on the DB at the moment

{
	"_id" : "admin.doapps-16c909fa-d26f-4b74-9d2e-a6483e762b63",
	"user" : "doapps-16c909fa-d26f-4b74-9d2e-a6483e762b63",
	"db" : "admin",
	...
}

I’ve tried eleminating everything from the user to the collection, but the bug remained.

I’ve istantiated a new DB and everything works correctly now.

This issue can create serious damage, also because it seems to be impossible to add or remove users without using your dashboard…