Skip to content

Commit f5c76f3

Browse files
authored
feat(NODE-3351): use hostname canonicalization (#3122)
1 parent a7a3b99 commit f5c76f3

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

src/cmap/auth/gssapi.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import { Callback, ns } from '../../utils';
1313
import { AuthContext, AuthProvider } from './auth_provider';
1414

1515
type MechanismProperties = {
16+
/** @deprecated use `CANONICALIZE_HOST_NAME` instead */
1617
gssapiCanonicalizeHostName?: boolean;
18+
CANONICALIZE_HOST_NAME?: boolean;
1719
SERVICE_NAME?: string;
1820
SERVICE_REALM?: string;
1921
};
@@ -174,7 +176,7 @@ function performGssapiCanonicalizeHostName(
174176
mechanismProperties: MechanismProperties,
175177
callback: Callback<string>
176178
): void {
177-
if (!mechanismProperties.gssapiCanonicalizeHostName) return callback(undefined, host);
179+
if (!mechanismProperties.CANONICALIZE_HOST_NAME) return callback(undefined, host);
178180

179181
// Attempt to resolve the host name
180182
dns.resolveCname(host, (err, r) => {

src/cmap/auth/mongo_credentials.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Resolves the default auth mechanism according to
2-
32
import type { Document } from '../../bson';
43
import { MongoAPIError, MongoMissingCredentialsError } from '../../error';
4+
import { emitWarningOnce } from '../../utils';
55
import { AUTH_MECHS_AUTH_SRC_EXTERNAL, AuthMechanism } from './providers';
66

77
// https://github.com/mongodb/specifications/blob/master/source/auth/auth.rst
@@ -89,6 +89,14 @@ export class MongoCredentials {
8989
}
9090
}
9191

92+
if ('gssapiCanonicalizeHostName' in this.mechanismProperties) {
93+
emitWarningOnce(
94+
'gssapiCanonicalizeHostName is deprecated. Please use CANONICALIZE_HOST_NAME instead.'
95+
);
96+
this.mechanismProperties.CANONICALIZE_HOST_NAME =
97+
this.mechanismProperties.gssapiCanonicalizeHostName;
98+
}
99+
92100
Object.freeze(this.mechanismProperties);
93101
Object.freeze(this);
94102
}

test/manual/kerberos.test.js

+35
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
'use strict';
22
const { MongoClient } = require('../../src');
33
const chai = require('chai');
4+
const sinon = require('sinon');
5+
const dns = require('dns');
46

57
const expect = chai.expect;
8+
chai.use(require('sinon-chai'));
69

710
function verifyKerberosAuthentication(client, done) {
811
client
@@ -23,6 +26,16 @@ function verifyKerberosAuthentication(client, done) {
2326
}
2427

2528
describe('Kerberos', function () {
29+
const sandbox = sinon.createSandbox();
30+
31+
beforeEach(function () {
32+
sandbox.spy(dns);
33+
});
34+
35+
afterEach(function () {
36+
sandbox.restore();
37+
});
38+
2639
if (process.env.MONGODB_URI == null) {
2740
console.error('skipping Kerberos tests, MONGODB_URI environment variable is not defined');
2841
return;
@@ -51,6 +64,28 @@ describe('Kerberos', function () {
5164
});
5265
});
5366

67+
it('validate that gssapiCanonicalizeHostName can be passed in', function (done) {
68+
const client = new MongoClient(
69+
`${krb5Uri}&authMechanismProperties=SERVICE_NAME:mongodb,gssapiCanonicalizeHostName:true&maxPoolSize=1`
70+
);
71+
client.connect(function (err, client) {
72+
if (err) return done(err);
73+
expect(dns.resolveCname).to.be.calledOnce;
74+
verifyKerberosAuthentication(client, done);
75+
});
76+
});
77+
78+
it('validate that CANONICALIZE_HOST_NAME can be passed in', function (done) {
79+
const client = new MongoClient(
80+
`${krb5Uri}&authMechanismProperties=SERVICE_NAME:mongodb,CANONICALIZE_HOST_NAME:true&maxPoolSize=1`
81+
);
82+
client.connect(function (err, client) {
83+
if (err) return done(err);
84+
expect(dns.resolveCname).to.be.calledOnce;
85+
verifyKerberosAuthentication(client, done);
86+
});
87+
});
88+
5489
// Unskip this test when a proper setup is available - see NODE-3060
5590
it.skip('validate that SERVICE_REALM and CANONICALIZE_HOST_NAME can be passed in', function (done) {
5691
const client = new MongoClient(

0 commit comments

Comments
 (0)