euberdeveloper / mongoback

The most powerful npm module to export and make backups of your MongoDB
https://mongoback.euber.dev
GNU General Public License v3.0
8 stars 1 forks source link

Port # is used when srv is true #23

Closed Daxcor69 closed 1 year ago

Daxcor69 commented 1 year ago

When SRV = true, the port designation should be disabled. If I leave port out of the config I get 27017, if I put undefined it puts the colon and no port number. If I put false then it puts :false.

Here is my options config:

{
  host: 'cluster0.sdfddqmlkssffdsdsn.mongodb.net',
  replicaSetName: 'atlas-7ksdwebssdddhrhalbt-shard-0',
  srv: true,
  username: 'blah',
  password: 'blah',
  outDir: '/backup/2023-2-8-18-23-33',
  authenticationDatabase: 'found',
  authenticationMechanism: 'SCRAM-SHA-1',
  ssl: true,
  sslAllowInvalidCertificates: true,
  all: true
}

Here is the error:

 uri: 'mongodb+srv://blah:blah@cluster0.kysfkiehfkdikdkdkdkfnkhzn.mongodb.net:27017/?authSource=found&authMechanism=SCRAM-SHA-1',
    options: {
      replicaSet: 'atlas-7kadfkjfsjfsueyuhgmbvkfhgidydoswlgulbt-shard-0',
      ssl: true,
      tlsAllowInvalidCertificates: true
    },
    triggerError: MongoParseError: mongodb+srv URI cannot have port number
Daxcor69 commented 1 year ago

This example doesn't make sense

This will add srv (mongodb+srv//myhost:8080):
const { mongoExport } = require('mongoback');
const options = { 
    host: 'myhost',
    port: 8080,
    srv: true,
    all: true 
};
euberdeveloper commented 1 year ago

I am taking a look into that, thanks for the issue

Daxcor69 commented 1 year ago

I don't know typescript, but I would think a conditional here:

function getMongoConnectionUri(options: ConnectionOptions): string {
    let uri = options.uri;

    if (!uri) {
        const protocol = options.srv ? 'mongodb+srv' : 'mongodb';
        const host = Array.isArray(options.host)
            ? `${options.host.map(({ host, port }) => `${host}:${port}`, '').join(',')}`
            : `${options.host ?? ''}:${options.port ?? ''}`;
        const auth = options.username
            ? options.password
                ? `${options.username}:${options.password}@`
                : `${options.username}@`
            : '';
        const opt = getOptionsString(options);
        uri = `${protocol}://${auth}${host}${opt}`;
    }

    return uri;
}

to create a different protocol and host based on srv

euberdeveloper commented 1 year ago

Exactly, I did some commits referring this issue, this is the last one. Now I am fixing the github action that runs the tests, after that I can publish the new version and close the issue

Daxcor69 commented 1 year ago

wow thank you for doing this right away, that is really cool. thank you for the project it is really helpful.

euberdeveloper commented 1 year ago

Thanks! I was chilling right now, so I saw the notificaiton email, so no problem. If you like the project feel free to add a star

Daxcor69 commented 1 year ago

Hello,

I think there is still an issue with SRV and ports. Here is the output of the backup. You will see that it does indeed connect to Mongo Atlas service. Gets the first collection to export and the spinner goes and then after a timeout it errors out with an dns/tcp lookup. If you look it is still showing a port number.

foundry
  ⣯ products2023-02-09T20:39:24.806+0000        could not connect to server: server selection error: server selection timeout, current topology: { Type: Single, Servers: [{ Addr: cluster0.<redacted>.mongodb.net:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: dial tcp: lookup cluster0.<redacted>.mongodb.net: No address associated with hostname }, ] }
  ✖ products
euberdeveloper commented 1 year ago

Do you have the Uri from the log? (Of course without pwd)

Daxcor69 commented 1 year ago

not sure how to get that, but here is the options object

{
  host: 'cluster0.<redacted>.mongodb.net',
  replicaSetName: 'atlas-<redacted>-shard-0',
  srv: true,
  username: 'backup',
  password: '<redacted>',
  outDir: '/backup/2023-2-10-0-15-50',
  authenticationDatabase: 'found',
  authenticationMechanism: 'SCRAM-SHA-1',
  ssl: true,
  tlsInsecure: true,
  all: true,
  outType: 'flat',
  readPreference: 'primary',
  realtimeLog: true
}

Because it does connect on the initial part, it does NOT throw an error with the uri. Only after it times out on the export of the collection does it throw the error. Is there a way to print the fully constructed uri ?

euberdeveloper commented 1 year ago

You are right, the part regarding the first connection is different from the one in which the collections are exported (this)

Daxcor69 commented 1 year ago

Sorry to be a PIA, but I am glad that I was able to help make it better. ;) Never got to do that before. Thanks for the quick response.

euberdeveloper commented 1 year ago

No problem, I am happy that I fixed those bugs. Now the new version is released

Daxcor69 commented 1 year ago

Sorry same issue remains, Here is my package.json

{
  "name": "mongo_job",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "mongoback": "^3.0.5",
    "winston": "^3.8.2",
    "winston-loki": "^6.0.6"
  }
}

Here is the output


{
  host: 'cluster0.redact.mongodb.net',
  replicaSetName: 'atlas-redact-shard-0',
  srv: true,
  username: 'backup',
  password: 'redact',
  outDir: '/backup/2023-2-10-20-58-12',
  authenticationDatabase: 'found',
  authenticationMechanism: 'SCRAM-SHA-1',
  ssl: true,
  tlsInsecure: true,
  all: true,
  outType: 'flat',
  readPreference: 'primary',
  realtimeLog: true
}
foundry
  ⣯ products2023-02-10T20:58:43.214+0000        could not connect to server: server selection error: server selection timeout, current topology: { Type: Single, Servers: [{ Addr: cluster0.redact.mongodb.net:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: dial tcp: lookup cluster0.redact.mongodb.net: No address associated with hostname }, ] }
  ✖ products
  ⣽ messageswaitings^C2023-02-10T20:58:46.530+0000      signal 'interrupt' received; forcefully terminating
  ✖ messageswaitings
Daxcor69 commented 1 year ago

I uninstalled 3.0.4 and then installed 3.0.5. fyi

euberdeveloper commented 1 year ago

A note:

image

tlsInsecure seems not to be in the available options. Could also be that it is an option of mongoexport added after that I developed the module and I could add it if needed, but actually I did not find it here.

Another note:

I do not know if there are other uses for that, but when there is srv I think that replicaSetName is not more needed. For what I know, srv is a kind of shortcut that was added in order to not have to write all the hosts + the replica set name every time. On mongoatlas indeed it is suggested in the connection uri only when you select "Connect with my application" and select very old versions of mongo.

I think I solved the issue, until now I didn't try the mongoexport for real with mongo atlas:

In case it does not work, I think that specifying directly the uri could be a good solution, but please report me eventual errors

I think that now it will work

Daxcor69 commented 1 year ago

OK the changes you made did the trick. In your docs, there is this

sslAllowInvalidCertificates: Default value: false. Bypasses the validation checks for server certificates and allows the use of invalid certificates. When using the allowInvalidCertificates setting, MongoDB logs as a warning the use of the invalid certificate.

When I run this, I got a deprecation warning and was told to use tlsInsecure. It looks like I didn't need this for Atlas, however I could see someone needed this if they host their own with an unsigned cert.

Again thank you for your help. Brad

euberdeveloper commented 1 year ago

I found that here: https://www.mongodb.com/docs/database-tools/release-notes/database-tools-changelog/#100.2.0-changelog

But it seems like it's not documented