Skip to content

Commit 687baea

Browse files
committed
feat!: Integrate MongoOptions parser into driver
MongoOptions introduce a well typed HostAddress type that tracks the correct shape of a host's address depending on its type. The MongoClientOptions type inherit from node's TLS and Socket options and use our allow list of options to maintain a connection between the types and programmatic processing of options. The hierarchy of options from MongoClient > Topology > Server > ConnectionPool > Connection have been corrected and clarified for responsibilities. NODE-2704
1 parent 16c1574 commit 687baea

File tree

84 files changed

+1525
-2502
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+1525
-2502
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
docs
22
lib
3+
test/disabled

package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"@types/bl": "^2.1.0",
3838
"@types/bson": "^4.0.2",
3939
"@types/kerberos": "^1.1.0",
40+
"@types/mocha": "^8.2.0",
4041
"@types/node": "^14.6.4",
4142
"@types/saslprep": "^1.0.0",
4243
"@typescript-eslint/eslint-plugin": "^3.10.0",

src/bson.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import type { SerializeOptions } from 'bson';
3737
* BSON Serialization options.
3838
* @public
3939
*/
40-
export interface BSONSerializeOptions extends SerializeOptions {
40+
export interface BSONSerializeOptions extends Omit<SerializeOptions, 'index'> {
4141
/** Return document results as raw BSON buffers */
4242
fieldsAsRaw?: { [key: string]: boolean };
4343
/** Promotes BSON values to native types where possible, set to false to only receive wrapper types */

src/cmap/auth/defaultAuthProviders.ts

+9-17
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,12 @@ export const AuthMechanism = {
2121
/** @public */
2222
export type AuthMechanismId = typeof AuthMechanism[keyof typeof AuthMechanism];
2323

24-
export const AUTH_PROVIDERS = {
25-
[AuthMechanism.MONGODB_AWS]: new MongoDBAWS(),
26-
[AuthMechanism.MONGODB_CR]: new MongoCR(),
27-
[AuthMechanism.MONGODB_GSSAPI]: new GSSAPI(),
28-
[AuthMechanism.MONGODB_PLAIN]: new Plain(),
29-
[AuthMechanism.MONGODB_SCRAM_SHA1]: new ScramSHA1(),
30-
[AuthMechanism.MONGODB_SCRAM_SHA256]: new ScramSHA256(),
31-
[AuthMechanism.MONGODB_X509]: new X509()
32-
};
33-
34-
// TODO: We can make auth mechanism more functional since we pass around a context object
35-
// and we improve the the typing here to use the enum, the current issue is that the mechanism is
36-
// 'default' until resolved maybe we can do that resolution here and make the this strictly
37-
// AuthMechanism -> AuthProviders
38-
export function defaultAuthProviders(): Record<string, AuthProvider> {
39-
return AUTH_PROVIDERS;
40-
}
24+
export const AUTH_PROVIDERS = new Map<AuthMechanismId | string, AuthProvider>([
25+
[AuthMechanism.MONGODB_AWS, new MongoDBAWS()],
26+
[AuthMechanism.MONGODB_CR, new MongoCR()],
27+
[AuthMechanism.MONGODB_GSSAPI, new GSSAPI()],
28+
[AuthMechanism.MONGODB_PLAIN, new Plain()],
29+
[AuthMechanism.MONGODB_SCRAM_SHA1, new ScramSHA1()],
30+
[AuthMechanism.MONGODB_SCRAM_SHA256, new ScramSHA256()],
31+
[AuthMechanism.MONGODB_X509, new X509()]
32+
]);

src/cmap/auth/gssapi.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,10 @@ export class GSSAPI extends AuthProvider {
6060
}
6161
}
6262
function makeKerberosClient(authContext: AuthContext, callback: Callback<KerberosClient>): void {
63-
const { host, port } = authContext.options;
63+
const { hostAddress } = authContext.options;
6464
const { credentials } = authContext;
65-
if (!host || !port || !credentials) {
66-
return callback(
67-
new MongoError(
68-
`Connection must specify: ${host ? 'host' : ''}, ${port ? 'port' : ''}, ${
69-
credentials ? 'host' : 'credentials'
70-
}.`
71-
)
72-
);
65+
if (!hostAddress || typeof hostAddress.host !== 'string' || !credentials) {
66+
return callback(new MongoError('Connection must have host and port and credentials defined.'));
7367
}
7468

7569
if ('kModuleError' in Kerberos) {
@@ -83,7 +77,7 @@ function makeKerberosClient(authContext: AuthContext, callback: Callback<Kerbero
8377
'mongodb';
8478

8579
performGssapiCanonicalizeHostName(
86-
host,
80+
hostAddress.host,
8781
mechanismProperties as MechanismProperties,
8882
(err?: Error | MongoError, host?: string) => {
8983
if (err) return callback(err);

src/cmap/auth/mongo_credentials.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,15 @@ export class MongoCredentials {
147147
}
148148

149149
static merge(
150-
creds: MongoCredentials,
150+
creds: MongoCredentials | undefined,
151151
options: Partial<MongoCredentialsOptions>
152152
): MongoCredentials {
153153
return new MongoCredentials({
154-
username: options.username ?? creds.username,
155-
password: options.password ?? creds.password,
156-
mechanism: options.mechanism ?? creds.mechanism,
157-
mechanismProperties: options.mechanismProperties ?? creds.mechanismProperties,
158-
source: options.source ?? creds.source ?? options.db
154+
username: options.username ?? creds?.username ?? '',
155+
password: options.password ?? creds?.password ?? '',
156+
mechanism: options.mechanism ?? creds?.mechanism ?? AuthMechanism.MONGODB_DEFAULT,
157+
mechanismProperties: options.mechanismProperties ?? creds?.mechanismProperties ?? {},
158+
source: options.source ?? options.db ?? creds?.source ?? 'admin'
159159
});
160160
}
161161
}

src/cmap/auth/mongodb_aws.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class MongoDBAWS extends AuthProvider {
4545

4646
if (credentials.username == null) {
4747
makeTempCredentials(credentials, (err, tempCredentials) => {
48-
if (err) return callback(err);
48+
if (err || !tempCredentials) return callback(err);
4949

5050
authContext.credentials = tempCredentials;
5151
this.auth(authContext, callback);

0 commit comments

Comments
 (0)