Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/cli-repl/src/cli-repl.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { MongoshInternalError } from '@mongosh/errors';
import { bson } from '@mongosh/service-provider-core';
import { once } from 'events';
import { promises as fs } from 'fs';
import http from 'http';
import path from 'path';
import { Duplex, PassThrough } from 'stream';
import { promisify } from 'util';
import { eventually } from '../../../testing/eventually';
import { MongodSetup, skipIfServerVersion, startTestServer } from '../../../testing/integration-testing-hooks';
import { expect, fakeTTYProps, readReplLogfile, tick, useTmpdir, waitBus, waitCompletion, waitEval } from '../test/repl-helpers';
import { eventually } from '../../../testing/eventually';
import CliRepl, { CliReplOptions } from './cli-repl';
import { CliReplErrors } from './error-codes';
import { bson } from '@mongosh/service-provider-core';
const { EJSON } = bson;

const delay = promisify(setTimeout);
Expand Down
27 changes: 25 additions & 2 deletions packages/cli-repl/test/e2e-direct.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ describe('e2e direct connection', () => {
shell.assertContainsOutput(`me: '${await rs0.hostport()}'`);
shell.assertContainsOutput(`setName: '${replSetId}'`);
});

await shell.executeLine('use admin');
await shell.executeLine("db.createUser({ user: 'anna', pwd: 'pwd', roles: [] })");
shell.assertContainsOutput('ok: 1');

dbname = `test-${Date.now()}-${(Math.random() * 100000) | 0}`;
await shell.executeLine(`use ${dbname}`);
await shell.executeLine('db.testcollection.insertOne({})');
Expand Down Expand Up @@ -173,7 +178,7 @@ describe('e2e direct connection', () => {
shell.assertContainsOutput(`me: '${await rs0.hostport()}'`);
shell.assertContainsOutput(`setName: '${replSetId}'`);
});
it('when specifying multiple seeds', async() => {
it('when specifying multiple seeds in a connection string', async() => {
const connectionString = 'mongodb://' + await rs2.hostport() + ',' + await rs1.hostport() + ',' + await rs0.hostport();
const shell = TestShell.start({ args: [connectionString] });
await shell.waitForPrompt();
Expand All @@ -182,7 +187,18 @@ describe('e2e direct connection', () => {
shell.assertContainsOutput(`me: '${await rs0.hostport()}'`);
shell.assertContainsOutput(`setName: '${replSetId}'`);
});
it('when specifying multiple seeds through --host', async() => {
it('when specifying multiple seeds without replset through --host', async() => {
const hostlist = await rs2.hostport() + ',' + await rs1.hostport() + ',' + await rs0.hostport();
const shell = TestShell.start({ args: ['--host', hostlist] });
await shell.waitForPrompt();
await shell.executeLine('db.isMaster()');
await shell.executeLine('({ dbname: db.getName() })');
shell.assertContainsOutput('ismaster: true');
shell.assertContainsOutput(`me: '${await rs0.hostport()}'`);
shell.assertContainsOutput(`setName: '${replSetId}'`);
shell.assertContainsOutput("dbname: 'test'");
});
it('when specifying multiple seeds with replset through --host', async() => {
const hostlist = replSetId + '/' + await rs2.hostport() + ',' + await rs1.hostport() + ',' + await rs0.hostport();
const shell = TestShell.start({ args: ['--host', hostlist] });
await shell.waitForPrompt();
Expand Down Expand Up @@ -230,6 +246,13 @@ describe('e2e direct connection', () => {
shell.assertContainsOutput('db.testcollection');
});
});
it('can authenticate when specifying multiple seeds with replset through --host', async() => {
const hostlist = replSetId + '/' + await rs2.hostport() + ',' + await rs1.hostport() + ',' + await rs0.hostport();
const shell = TestShell.start({ args: ['--host', hostlist, '--username', 'anna', '--password', 'pwd'] });
await shell.waitForPrompt();
await shell.executeLine('db.runCommand({connectionStatus: 1})');
shell.assertContainsOutput("user: 'anna'");
});
});

describe('fail-fast connections', () => {
Expand Down
41 changes: 28 additions & 13 deletions packages/service-provider-core/src/uri-generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,13 +399,13 @@ describe('uri-generator.generate-uri', () => {
});

context('when the --host option contains invalid characters', () => {
const options = { host: 'a,b,c' };
const options = { host: 'a$b,c' };

it('returns the uri', () => {
try {
generateUri(options);
} catch (e) {
expect(e.message).to.contain('The --host argument contains an invalid character: ,');
expect(e.message).to.contain('The --host argument contains an invalid character: $');
expect(e).to.be.instanceOf(MongoshInvalidInputError);
expect(e.code).to.equal(CommonErrors.InvalidArgument);
return;
Expand All @@ -414,20 +414,35 @@ describe('uri-generator.generate-uri', () => {
});
});

context('when the --host option contains a replica set', () => {
it('returns a URI for the hosts and ports specified in --host', () => {
const options = { host: 'replsetname/host1:123,host2,host3:456,' };
expect(generateUri(options)).to.equal('mongodb://host1:123,host2,host3:456/test?replicaSet=replsetname');
context('when the --host option contains a seed list', () => {
context('without a replica set', () => {
it('returns a URI for the hosts and ports specified in --host', () => {
const options = { host: 'host1:123,host2,host3:456,' };
expect(generateUri(options)).to.equal('mongodb://host1:123,host2,host3:456/');
});
it('returns a URI for the hosts and ports specified in --host and database name', () => {
const options = {
host: 'host1:123,host2,host3:456,',
connectionSpecifier: 'admin'
};
expect(generateUri(options)).to.equal('mongodb://host1:123,host2,host3:456/admin');
});
});
context('with a replica set', () => {
it('returns a URI for the hosts and ports specified in --host', () => {
const options = { host: 'replsetname/host1:123,host2,host3:456,' };
expect(generateUri(options)).to.equal('mongodb://host1:123,host2,host3:456/?replicaSet=replsetname');
});

it('returns a URI for the hosts and ports specified in --host and database name', () => {
const options = { host: 'replsetname/host1:123,host2,host3:456', connectionSpecifier: 'admin' };
expect(generateUri(options)).to.equal('mongodb://host1:123,host2,host3:456/admin?replicaSet=replsetname');
});
it('returns a URI for the hosts and ports specified in --host and database name', () => {
const options = { host: 'replsetname/host1:123,host2,host3:456', connectionSpecifier: 'admin' };
expect(generateUri(options)).to.equal('mongodb://host1:123,host2,host3:456/admin?replicaSet=replsetname');
});

it('returns a URI for the hosts and ports specified in --host and database name with escaped chars', () => {
const options = { host: 'replsetname/host1:123,host2,host3:456', connectionSpecifier: 'admin?foo=bar' };
expect(generateUri(options)).to.equal('mongodb://host1:123,host2,host3:456/admin%3Ffoo%3Dbar?replicaSet=replsetname');
it('returns a URI for the hosts and ports specified in --host and database name with escaped chars', () => {
const options = { host: 'replsetname/host1:123,host2,host3:456', connectionSpecifier: 'admin?foo=bar' };
expect(generateUri(options)).to.equal('mongodb://host1:123,host2,host3:456/admin%3Ffoo%3Dbar?replicaSet=replsetname');
});
});
});
});
17 changes: 15 additions & 2 deletions packages/service-provider-core/src/uri-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,28 @@ function generateUriNormalized(options: CliOptions): ConnectionString {
// If the --host argument contains /, it has the format
// <replSetName>/<hostname1><:port>,<hostname2><:port>,<...>
const replSetHostMatch = (options.host ?? '').match(
/^(?<replSetName>[^/]+)\/(?<hosts>([A-Za-z0-9.-]+(:\d+)?,?)+)$/);
/^(?<replSetName>[^/]+)\/(?<hosts>([A-Za-z0-9.-]+(:\d+)?,?)+)$/
);
if (replSetHostMatch) {
const { replSetName, hosts } = replSetHostMatch.groups as { replSetName: string, hosts: string };
const connectionString = new ConnectionString(`${Scheme.Mongo}replacemeHost/${encodeURIComponent(uri ?? DEFAULT_DB)}`);
const connectionString = new ConnectionString(`${Scheme.Mongo}replacemeHost/${encodeURIComponent(uri || '')}`);
connectionString.hosts = hosts.split(',').filter(host => host.trim());
connectionString.searchParams.set('replicaSet', replSetName);
return addShellConnectionStringParameters(connectionString);
}

// If the --host argument contains multiple hosts as a seed list
// we directly do not do additional host/port parsing
const seedList = (options.host ?? '').match(
/^(?<hosts>([A-Za-z0-9.-]+(:\d+)?,?)+)$/
);
if (seedList && options.host?.includes(',')) {
const { hosts } = seedList.groups as { hosts: string };
const connectionString = new ConnectionString(`${Scheme.Mongo}replacemeHost/${encodeURIComponent(uri || '')}`);
connectionString.hosts = hosts.split(',').filter(host => host.trim());
return addShellConnectionStringParameters(connectionString);
}

// There is no URI provided, use default 127.0.0.1:27017
if (!uri) {
return new ConnectionString(`${Scheme.Mongo}${generateHost(options)}:${generatePort(options)}/?directConnection=true`);
Expand Down