Skip to content

Commit

Permalink
feat(cli-repl): pass through sspiHostnameCanonicalization to driver M…
Browse files Browse the repository at this point in the history
…ONGOSH-856 (#1231)

Pass down `sspiHostnameCanonicalization` to the driver as-is
and let the driver do the validation. We still map `true` and
`false` from strings to booleans and an empty string to `undefined`,
since the driver would otherwise reject those values.
  • Loading branch information
addaleax authored Mar 17, 2022
1 parent 1467075 commit 48137a1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
25 changes: 15 additions & 10 deletions packages/cli-repl/src/arg-mapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ describe('arg-mapper.mapCliToDriver', () => {

it('is not mapped to authMechanismProperties', () => {
expect(mapCliToDriver(cliOptions)).to.deep.equal({
authMechanismProperties: {
CANONICALIZE_HOST_NAME: 'none'
},
driverInfo: {
name: 'mongosh',
version: packageJSON.version
Expand All @@ -334,7 +337,7 @@ describe('arg-mapper.mapCliToDriver', () => {
it('is mapped to authMechanismProperties', () => {
expect(mapCliToDriver(cliOptions)).to.deep.equal({
authMechanismProperties: {
gssapiCanonicalizeHostName: 'true'
CANONICALIZE_HOST_NAME: 'forward'
},
driverInfo: {
name: 'mongosh',
Expand All @@ -344,17 +347,19 @@ describe('arg-mapper.mapCliToDriver', () => {
});
});

context('with a value of forwardAndReverse', () => {
const cliOptions: CliOptions = { sspiHostnameCanonicalization: 'forwardAndReverse' };
context('with a value of true', () => {
const cliOptions: CliOptions = { sspiHostnameCanonicalization: 'true' };

it('is mapped to authMechanismProperties', () => {
try {
mapCliToDriver(cliOptions);
} catch (e) {
expect(e.message).to.contain('forwardAndReverse is not supported');
return;
}
expect.fail('expected error');
expect(mapCliToDriver(cliOptions)).to.deep.equal({
authMechanismProperties: {
CANONICALIZE_HOST_NAME: true
},
driverInfo: {
name: 'mongosh',
version: packageJSON.version
}
});
});
});
});
Expand Down
19 changes: 9 additions & 10 deletions packages/cli-repl/src/arg-mapper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CommonErrors, MongoshInvalidInputError, MongoshUnimplementedError } from '@mongosh/errors';
import { MongoshInvalidInputError, MongoshUnimplementedError } from '@mongosh/errors';
import { CliOptions, DevtoolsConnectOptions } from '@mongosh/service-provider-server';
import setValue from 'lodash.set';

Expand All @@ -15,7 +15,7 @@ const MAPPINGS = {
awsIamSessionToken: 'authMechanismProperties.AWS_SESSION_TOKEN',
gssapiServiceName: 'authMechanismProperties.SERVICE_NAME',
sspiRealmOverride: 'authMechanismProperties.SERVICE_REALM',
sspiHostnameCanonicalization: { opt: 'authMechanismProperties.gssapiCanonicalizeHostName', fun: mapSspiHostnameCanonicalization },
sspiHostnameCanonicalization: { opt: 'authMechanismProperties.CANONICALIZE_HOST_NAME', fun: mapGSSAPIHostnameCanonicalization },
authenticationDatabase: 'authSource',
authenticationMechanism: 'authMechanism',
keyVaultNamespace: 'autoEncryption.keyVaultNamespace',
Expand Down Expand Up @@ -125,17 +125,16 @@ function getCertificateExporter(): TlsCertificateExporter | undefined {
return undefined;
}

function mapSspiHostnameCanonicalization(value: string): string | undefined {
if (!value || value === 'none') {
function mapGSSAPIHostnameCanonicalization(value: string): string | boolean | undefined {
// Here for backwards compatibility reasons -- ideally, users should always
// just either not specify this, or use none/forward/forwardAndReverse.
if (value === '') {
return undefined;
}
if (value === 'forward') {
return 'true';
if (value === 'true' || value === 'false') {
return value === 'true';
}
throw new MongoshInvalidInputError(
`--sspiHostnameCanonicalization value ${value} is not supported`,
CommonErrors.InvalidArgument
);
return value;
}

export default mapCliToDriver;

0 comments on commit 48137a1

Please sign in to comment.