Hulubina
Hulubina

Reputation: 163

MongoDB code:18 error Authentification failed

This is the error :

{
    "errorResponse": {
        "ok": 0,
        "errmsg": "Authentication failed.",
        "code": 18,
        "operationTime": {
            "$timestamp": "7415236078489042945"
        }
    },
    "ok": 0,
    "code": 18,
    "operationTime": {
        "$timestamp": "7415236078489042945"
    },
    "connectionGeneration": 0
}

This is the connection url:

mongodb://user:pass@databaseservicedatabasexxxxx.cluster-xxxxx.us-east-1.docdb.amazonaws.com:27017/?replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false

I tried to update the Amazon Document DB certificate to

rds-ca-rsa2048-g1

But it is not working, I updated the mongoose to ^8.6.2 ,the lambda function is using node 18. An example is :

 const connection = await mongoose
    .createConnection(CONNECTION_URL, {
      dbName: DB_NAME,
    })
    .asPromise();

It stopped working, one month ago was working well.

Another file if it helps but I don't think:

export class DatabaseService extends Construct {
  public readonly instance: DatabaseCluster;
  public readonly connectionUrl: string;
  constructor(scope: Construct, id: string, vpc: Vpc) {
    super(scope, id);

    const databasePasswordSecret = new Secret(this, "Database Password", {
      secretName: getStandarizedName(["database", "secret"]),
      generateSecretString: {
        excludePunctuation: true,
        excludeCharacters: "/¥'%:;{}",
      },
    });

    const parameterGroup = new ClusterParameterGroup(
      this,
      getStandarizedName(["parameter", "group"]),
      {
        family: DOCDB_FAMILY,
        parameters: {
          tls: "disabled",
        },
      }
    );

    const securityGroup = new SecurityGroup(this, "SecurityGroup", {
      vpc,
      allowAllOutbound: true,
    });

    securityGroup.addIngressRule(
      Peer.anyIpv4(),
      Port.tcp(27017),
      "Allow connections from deployed services"
    );

    this.instance = new DatabaseCluster(this, "Database", {
      masterUser: {
        username: env.DATABSE_MASTER_USERNAME,
        password: SecretValue.secretsManager(databasePasswordSecret.secretArn),
      },
      instanceType: isProduction
        ? PRODUCTION_INSTANCE_TYPE
        : DEVELOPMENT_INSTANCE_TYPE,
      vpc,
      vpcSubnets: {
        subnetType: SubnetType.PRIVATE_ISOLATED,
      },
      instances: env.DATABASE_NODES_COUNT,
      parameterGroup,
      deletionProtection: isProduction,
      storageEncrypted: true,
      securityGroup,
    });

    new StringParameter(this, getStandarizedName(["db", "url"]), {
      stringValue: this.instance.clusterEndpoint.socketAddress,
      parameterName: getStandarizedName(["db", "url"]),
    });

    this.connectionUrl = `${MONGODB_PREFIX}${
      env.DATABSE_MASTER_USERNAME
    }:${databasePasswordSecret.secretValue.unsafeUnwrap()}@${
      this.instance.clusterEndpoint.socketAddress
    }${MONGODB_SUFFIX}`;
  }
}

Thank you!!!

Upvotes: 0

Views: 72

Answers (1)

Wernfried Domscheit
Wernfried Domscheit

Reputation: 59436

The connection string format is like this:

mongodb://<username>:<password>@databaseservicedatabasexxxxx.cluster-xxxxx.us-east-1.docdb.amazonaws.com:27017/...

If you don't like to put the password into connection string due to security concerns, then you can use

mongodb://databaseservicedatabasexxxxx.cluster-xxxxx.us-east-1.docdb.amazonaws.com:27017/...

> db.auth(<username>, <password>)

Please note, above answer applies to MongoDB, AWS DocumentDB could be different. Also be aware of this: Authentication failure while trying to save to mongodb

Upvotes: 0

Related Questions