Skip to content

Commit

Permalink
fix(NODE-2939): change canonicalization to enum
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Feb 11, 2022
1 parent 447a49c commit 3c6d586
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
26 changes: 16 additions & 10 deletions src/cmap/auth/gssapi.ts
Expand Up @@ -13,16 +13,17 @@ import { Callback, ns } from '../../utils';
import { AuthContext, AuthProvider } from './auth_provider';

/** @public */
export const CANONICALIZATION_VALUES = [
true,
false,
'none',
'forward',
'forwardAndReverse'
] as const;
export const CanonicalizationProperties = Object.freeze({
on: true,
off: false,
none: 'none',
forward: 'forward',
forwardAndReverse: 'forwardAndReverse'
} as const);

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

type MechanismProperties = {
/** @deprecated use `CANONICALIZE_HOST_NAME` instead */
Expand Down Expand Up @@ -192,10 +193,15 @@ function performGssapiCanonicalizeHostName(
callback: Callback<string>
): void {
const mode = mechanismProperties.CANONICALIZE_HOST_NAME;
if (!mode || mode === 'none') return callback(undefined, host);
if (!mode || mode === CanonicalizationProperties.none) {
return callback(undefined, host);
}

// If forward and reverse or true
if (mode === true || mode === 'forwardAndReverse') {
if (
mode === CanonicalizationProperties.on ||
mode === CanonicalizationProperties.forwardAndReverse
) {
// Perform the lookup of the ip address.
dns.lookup(host, (error, address) => {
// No ip found, return the error.
Expand Down
4 changes: 2 additions & 2 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 { CANONICALIZATION_VALUES, CanonicalizationProperties } from './gssapi';
import { CanonicalizationProperties } 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 @@ -170,7 +170,7 @@ export class MongoCredentials {
}

const canonicalization = this.mechanismProperties.CANONICALIZE_HOST_NAME ?? false;
if (!CANONICALIZATION_VALUES.includes(canonicalization)) {
if (!Object.values(CanonicalizationProperties).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 { CANONICALIZATION_VALUES, CanonicalizationProperties } from './cmap/auth/gssapi';
export type { CanonicalizationProperties } from './cmap/auth/gssapi';
export type {
AuthMechanismProperties,
MongoCredentials,
Expand Down
2 changes: 1 addition & 1 deletion test/manual/kerberos.test.js
Expand Up @@ -121,7 +121,7 @@ describe('Kerberos', function () {
);
client.connect(function (err, client) {
if (err) return done(err);
expect(dns.lookup).to.be.calledOnce;
expect(dns.lookup).to.be.called;
expect(dns.resolvePtr).to.be.calledOnce;
verifyKerberosAuthentication(client, done);
});
Expand Down

0 comments on commit 3c6d586

Please sign in to comment.