Skip to content

Commit

Permalink
test(NODE-2939): update enum and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Feb 14, 2022
1 parent 3c6d586 commit 20c8d55
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 60 deletions.
14 changes: 7 additions & 7 deletions src/cmap/auth/gssapi.ts
Expand Up @@ -13,7 +13,7 @@ import { Callback, ns } from '../../utils';
import { AuthContext, AuthProvider } from './auth_provider';

/** @public */
export const CanonicalizationProperties = Object.freeze({
export const CanonicalizationValues = Object.freeze({
on: true,
off: false,
none: 'none',
Expand All @@ -22,13 +22,13 @@ export const CanonicalizationProperties = Object.freeze({
} as const);

/** @public */
export type CanonicalizationProperties =
typeof CanonicalizationProperties[keyof typeof CanonicalizationProperties];
export type CanonicalizationValues =
typeof CanonicalizationValues[keyof typeof CanonicalizationValues];

type MechanismProperties = {
/** @deprecated use `CANONICALIZE_HOST_NAME` instead */
gssapiCanonicalizeHostName?: boolean;
CANONICALIZE_HOST_NAME?: CanonicalizationProperties;
CANONICALIZE_HOST_NAME?: CanonicalizationValues;
SERVICE_HOST?: string;
SERVICE_NAME?: string;
SERVICE_REALM?: string;
Expand Down Expand Up @@ -193,14 +193,14 @@ function performGssapiCanonicalizeHostName(
callback: Callback<string>
): void {
const mode = mechanismProperties.CANONICALIZE_HOST_NAME;
if (!mode || mode === CanonicalizationProperties.none) {
if (!mode || mode === CanonicalizationValues.none) {
return callback(undefined, host);
}

// If forward and reverse or true
if (
mode === CanonicalizationProperties.on ||
mode === CanonicalizationProperties.forwardAndReverse
mode === CanonicalizationValues.on ||
mode === CanonicalizationValues.forwardAndReverse
) {
// Perform the lookup of the ip address.
dns.lookup(host, (error, address) => {
Expand Down
6 changes: 3 additions & 3 deletions src/cmap/auth/mongo_credentials.ts
Expand Up @@ -2,7 +2,7 @@
import type { Document } from '../../bson';
import { MongoAPIError, MongoMissingCredentialsError } from '../../error';
import { emitWarningOnce } from '../../utils';
import { CanonicalizationProperties } from './gssapi';
import { CanonicalizationValues } from './gssapi';
import { AUTH_MECHS_AUTH_SRC_EXTERNAL, AuthMechanism } from './providers';

// https://github.com/mongodb/specifications/blob/master/source/auth/auth.rst
Expand Down Expand Up @@ -31,7 +31,7 @@ export interface AuthMechanismProperties extends Document {
SERVICE_HOST?: string;
SERVICE_NAME?: string;
SERVICE_REALM?: string;
CANONICALIZE_HOST_NAME?: CanonicalizationProperties;
CANONICALIZE_HOST_NAME?: CanonicalizationValues;
AWS_SESSION_TOKEN?: string;
}

Expand Down Expand Up @@ -170,7 +170,7 @@ export class MongoCredentials {
}

const canonicalization = this.mechanismProperties.CANONICALIZE_HOST_NAME ?? false;
if (!Object.values(CanonicalizationProperties).includes(canonicalization)) {
if (!Object.values(CanonicalizationValues).includes(canonicalization)) {
throw new MongoAPIError(`Invalid CANONICALIZE_HOST_NAME value: ${canonicalization}`);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -176,7 +176,7 @@ export type {
ResumeToken,
UpdateDescription
} from './change_stream';
export type { CanonicalizationProperties } from './cmap/auth/gssapi';
export type { CanonicalizationValues } from './cmap/auth/gssapi';
export type {
AuthMechanismProperties,
MongoCredentials,
Expand Down
75 changes: 26 additions & 49 deletions test/manual/kerberos.test.js
Expand Up @@ -88,34 +88,39 @@ describe('Kerberos', function () {
}
});

context('when the value is true', function () {
it('successfully authenticates', function (done) {
const client = new MongoClient(
`${krb5Uri}&authMechanismProperties=SERVICE_NAME:mongodb,CANONICALIZE_HOST_NAME:true&maxPoolSize=1`
);
client.connect(function (err, client) {
if (err) return done(err);
expect(dns.resolveCname).to.be.calledOnce;
verifyKerberosAuthentication(client, done);
for (const option of [true, 'forward']) {
context(`when the value is ${option}`, function () {
it('authenticates with a forward cname lookup', function (done) {
const client = new MongoClient(
`${krb5Uri}&authMechanismProperties=SERVICE_NAME:mongodb,CANONICALIZE_HOST_NAME:${option}&maxPoolSize=1`
);
client.connect(function (err, client) {
if (err) return done(err);
expect(dns.resolveCname).to.be.calledOnce;
verifyKerberosAuthentication(client, done);
});
});
});
});
}

context('when the value is forward', function () {
it('successfully authenticates', function (done) {
const client = new MongoClient(
`${krb5Uri}&authMechanismProperties=SERVICE_NAME:mongodb,CANONICALIZE_HOST_NAME:forward&maxPoolSize=1`
);
client.connect(function (err, client) {
if (err) return done(err);
expect(dns.resolveCname).to.be.calledOnce;
verifyKerberosAuthentication(client, done);
for (const option of [false, 'none']) {
context(`when the value is ${option}`, function () {
it('authenticates with no dns lookups', function (done) {
const client = new MongoClient(
`${krb5Uri}&authMechanismProperties=SERVICE_NAME:mongodb,CANONICALIZE_HOST_NAME:${option}&maxPoolSize=1`
);
client.connect(function (err, client) {
if (err) return done(err);
expect(dns.resolveCname).to.not.be.called;
expect(dns.lookup).to.not.be.called;
verifyKerberosAuthentication(client, done);
});
});
});
});
}

context('when the value is forwardAndReverse', function () {
it('successfully authenticates', function (done) {
it('authenticates with a forward dns lookup and a reverse ptr lookup', function (done) {
const client = new MongoClient(
`${krb5Uri}&authMechanismProperties=SERVICE_NAME:mongodb,CANONICALIZE_HOST_NAME:forwardAndReverse&maxPoolSize=1`
);
Expand All @@ -127,34 +132,6 @@ describe('Kerberos', function () {
});
});
});

context('when the value is false', function () {
it('successfully authenticates', function (done) {
const client = new MongoClient(
`${krb5Uri}&authMechanismProperties=SERVICE_NAME:mongodb,CANONICALIZE_HOST_NAME:false&maxPoolSize=1`
);
client.connect(function (err, client) {
if (err) return done(err);
expect(dns.resolveCname).to.not.be.calledOnce;
expect(dns.lookup).to.not.be.calledOnce;
verifyKerberosAuthentication(client, done);
});
});
});

context('when the value is none', function () {
it('successfully authenticates', function (done) {
const client = new MongoClient(
`${krb5Uri}&authMechanismProperties=SERVICE_NAME:mongodb,CANONICALIZE_HOST_NAME:none&maxPoolSize=1`
);
client.connect(function (err, client) {
if (err) return done(err);
expect(dns.resolveCname).to.not.be.calledOnce;
expect(dns.lookup).to.not.be.calledOnce;
verifyKerberosAuthentication(client, done);
});
});
});
});

// Unskip this test when a proper setup is available - see NODE-3060
Expand Down

0 comments on commit 20c8d55

Please sign in to comment.