Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove duplicated authSource from url COMPASS-4940 #2342

Merged
merged 1 commit into from
Jul 12, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/connection-model/lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,12 @@ function encodeURIComponentRFC3986(str) {
});
}

function setAuthSourceToExternal(url) {
const uri = new ConnectionString(url);
uri.searchParams.set('authSource', '$external');
return uri.toString();
}

/**
* Adds auth info to URL. The connection model builds two URLs.
* driverUrl - for the driver with the password included.
Expand Down Expand Up @@ -472,7 +478,7 @@ function addAuthToUrl({ url, isPasswordProtected }) {
url = url.replace('AUTH_TOKEN', authField, 1);

if (includes(['LDAP', 'KERBEROS', 'X509'], this.authStrategy)) {
url = `${url}&authSource=$external`;
url = setAuthSourceToExternal(url);
}

if (this.authStrategy === 'KERBEROS' && this.kerberosCanonicalizeHostname) {
Expand Down
28 changes: 14 additions & 14 deletions packages/connection-model/test/build-uri.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ describe('Connection model builder', () => {
});

expect(c.driverUrl).to.be.equal(
'mongodb://testing@localhost:27017/?authMechanism=MONGODB-X509&readPreference=primary&ssl=true&authSource=$external'
'mongodb://testing@localhost:27017/?authMechanism=MONGODB-X509&readPreference=primary&ssl=true&authSource=%24external'
);
expect(c.driverOptions).to.deep.equal(options);

Expand Down Expand Up @@ -302,7 +302,7 @@ describe('Connection model builder', () => {
expect(c.driverUrl).to.be.equal(
[
'mongodb://user%40-azMPk%5D%263Wt%29iP_9C%3APMQ%3D:user%40-azMPk%5D%263Wt%29iP_9C%3APMQ%3D',
'@localhost:27017/?authMechanism=PLAIN&readPreference=primary&ssl=false&authSource=$external'
'@localhost:27017/?authMechanism=PLAIN&readPreference=primary&ssl=false&authSource=%24external'
].join('')
);

Expand All @@ -319,7 +319,7 @@ describe('Connection model builder', () => {
expect(c.driverUrl).to.be.equal(
[
'mongodb://user%40-azMPk%5D%263Wt%29iP_9C%3APMQ%3D',
'@localhost:27017/?authMechanism=GSSAPI&readPreference=primary&ssl=false&authSource=$external'
'@localhost:27017/?authMechanism=GSSAPI&readPreference=primary&ssl=false&authSource=%24external'
].join('')
);

Expand All @@ -339,7 +339,7 @@ describe('Connection model builder', () => {
expect(c.driverUrl).to.be.equal(
[
'mongodb://user%40-azMPk%5D%263Wt%29iP_9C%3APMQ%3D',
'@localhost:27017/?authMechanism=GSSAPI&readPreference=primary&ssl=false&authSource=$external&authMechanismProperties=CANONICALIZE_HOST_NAME:true'
'@localhost:27017/?authMechanism=GSSAPI&readPreference=primary&ssl=false&authSource=%24external&authMechanismProperties=CANONICALIZE_HOST_NAME:true'
].join('')
);

Expand Down Expand Up @@ -371,10 +371,10 @@ describe('Connection model builder', () => {
const c = new Connection(attrs);

expect(c.driverUrl).to.be.equal(
'mongodb://ldap-user:ldap-password@localhost:27017/?authMechanism=PLAIN&readPreference=primary&ssl=false&authSource=$external'
'mongodb://ldap-user:ldap-password@localhost:27017/?authMechanism=PLAIN&readPreference=primary&ssl=false&authSource=%24external'
);
expect(c.safeUrl).to.be.equal(
'mongodb://ldap-user:*****@localhost:27017/?authMechanism=PLAIN&readPreference=primary&ssl=false&authSource=$external'
'mongodb://ldap-user:*****@localhost:27017/?authMechanism=PLAIN&readPreference=primary&ssl=false&authSource=%24external'
);

Connection.from(c.driverUrl, (error) => {
Expand All @@ -390,7 +390,7 @@ describe('Connection model builder', () => {
c.ldapPassword = 'ldap-password';

expect(c.driverUrl).to.be.equal(
'mongodb://ldap-user:ldap-password@localhost:27017/?authMechanism=PLAIN&readPreference=primary&ssl=false&authSource=$external'
'mongodb://ldap-user:ldap-password@localhost:27017/?authMechanism=PLAIN&readPreference=primary&ssl=false&authSource=%24external'
);

Connection.from(c.driverUrl, (error) => {
Expand All @@ -409,7 +409,7 @@ describe('Connection model builder', () => {

expect(c.driverAuthMechanism).to.be.equal('PLAIN');
expect(c.driverUrl).to.be.equal(
'mongodb://arlo:w%40of@localhost:27017/ldap?authMechanism=PLAIN&readPreference=primary&ssl=false&authSource=$external'
'mongodb://arlo:w%40of@localhost:27017/ldap?authMechanism=PLAIN&readPreference=primary&ssl=false&authSource=%24external'
);

Connection.from(c.driverUrl, (error) => {
Expand All @@ -429,7 +429,7 @@ describe('Connection model builder', () => {

expect(c.driverAuthMechanism).to.be.equal('PLAIN');
expect(c.driverUrl).to.be.equal(
'mongodb://arlo%40t.co:woof@localhost:27017/ldap?authMechanism=PLAIN&readPreference=primary&ssl=false&authSource=$external'
'mongodb://arlo%40t.co:woof@localhost:27017/ldap?authMechanism=PLAIN&readPreference=primary&ssl=false&authSource=%24external'
);

Connection.from(c.driverUrl, (error) => {
Expand All @@ -449,7 +449,7 @@ describe('Connection model builder', () => {
expect(c.driverUrl).to.be.equal(
'mongodb://CN%3Dclient%2COU%3Dkerneluser%3Ainfo%2CO%3D10Gen%2CL%3DNew%20York%20City' +
'%2CST%3DNew%20York%2CC%3DUS@localhost:27017/' +
'?authMechanism=MONGODB-X509&readPreference=primary&ssl=false&authSource=$external'
'?authMechanism=MONGODB-X509&readPreference=primary&ssl=false&authSource=%24external'
);

Connection.from(c.driverUrl, (error) => {
Expand All @@ -466,7 +466,7 @@ describe('Connection model builder', () => {
expect(c.driverAuthMechanism).to.be.equal('MONGODB-X509');
expect(c.driverUrl).to.be.equal(
'mongodb://localhost:27017/?authMechanism=MONGODB-X509' +
'&readPreference=primary&ssl=false&authSource=$external'
'&readPreference=primary&ssl=false&authSource=%24external'
);

Connection.from(c.driverUrl, (error) => {
Expand Down Expand Up @@ -737,7 +737,7 @@ describe('Connection model builder', () => {
});

expect(c.driverUrl).to.be.equal(
'mongodb://lucas%40kerb.mongodb.parts@localhost:27017/?authMechanism=GSSAPI&readPreference=primary&ssl=false&authSource=$external'
'mongodb://lucas%40kerb.mongodb.parts@localhost:27017/?authMechanism=GSSAPI&readPreference=primary&ssl=false&authSource=%24external'
);

Connection.from(c.driverUrl, (error) => {
Expand Down Expand Up @@ -786,10 +786,10 @@ describe('Connection model builder', () => {
const c = new Connection(attrs);

expect(c.driverUrl).to.be.equal(
'mongodb://alena%40test.test@localhost:27017/?authMechanism=GSSAPI&readPreference=primary&ssl=false&authSource=$external'
'mongodb://alena%40test.test@localhost:27017/?authMechanism=GSSAPI&readPreference=primary&ssl=false&authSource=%24external'
);
expect(c.safeUrl).to.be.equal(
'mongodb://alena%40test.test@localhost:27017/?authMechanism=GSSAPI&readPreference=primary&ssl=false&authSource=$external'
'mongodb://alena%40test.test@localhost:27017/?authMechanism=GSSAPI&readPreference=primary&ssl=false&authSource=%24external'
);

Connection.from(c.driverUrl, (error) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/connection-model/test/parse-and-build-uri.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ const tests = [
expectedConnectionString:
'mongodb://%40rlo@localhost:27017/?' +
'authMechanism=GSSAPI&readPreference=primary&' +
'authSource=%24external&directConnection=true&ssl=false&authSource=$external'
'authSource=%24external&directConnection=true&ssl=false'
},
{
description: 'with authMechanismProperties and gssapiServiceName',
connectionString:
'mongodb://%40rlo@localhost:27017/?' +
'authMechanism=GSSAPI&readPreference=primary&' +
'authSource=%24external&authMechanismProperties=CANONICALIZE_HOST_NAME%3Atrue&' +
'gssapiCanonicalizeHostName=true&directConnection=true&ssl=false&authSource=$external'
'gssapiCanonicalizeHostName=true&directConnection=true&ssl=false'
},
{
description:
Expand Down