Skip to content

Commit

Permalink
feat!: Integrate MongoOptions parser into driver (#2680)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nbbeeken authored Jan 8, 2021
1 parent 16c1574 commit b1bdb06
Show file tree
Hide file tree
Showing 84 changed files with 1,559 additions and 2,514 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
docs
lib
test/disabled
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@types/bl": "^2.1.0",
"@types/bson": "^4.0.2",
"@types/kerberos": "^1.1.0",
"@types/mocha": "^8.2.0",
"@types/node": "^14.6.4",
"@types/saslprep": "^1.0.0",
"@typescript-eslint/eslint-plugin": "^3.10.0",
Expand Down
2 changes: 1 addition & 1 deletion src/bson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import type { SerializeOptions } from 'bson';
* BSON Serialization options.
* @public
*/
export interface BSONSerializeOptions extends SerializeOptions {
export interface BSONSerializeOptions extends Omit<SerializeOptions, 'index'> {
/** Return document results as raw BSON buffers */
fieldsAsRaw?: { [key: string]: boolean };
/** Promotes BSON values to native types where possible, set to false to only receive wrapper types */
Expand Down
26 changes: 9 additions & 17 deletions src/cmap/auth/defaultAuthProviders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,12 @@ export const AuthMechanism = {
/** @public */
export type AuthMechanismId = typeof AuthMechanism[keyof typeof AuthMechanism];

export const AUTH_PROVIDERS = {
[AuthMechanism.MONGODB_AWS]: new MongoDBAWS(),
[AuthMechanism.MONGODB_CR]: new MongoCR(),
[AuthMechanism.MONGODB_GSSAPI]: new GSSAPI(),
[AuthMechanism.MONGODB_PLAIN]: new Plain(),
[AuthMechanism.MONGODB_SCRAM_SHA1]: new ScramSHA1(),
[AuthMechanism.MONGODB_SCRAM_SHA256]: new ScramSHA256(),
[AuthMechanism.MONGODB_X509]: new X509()
};

// TODO: We can make auth mechanism more functional since we pass around a context object
// and we improve the the typing here to use the enum, the current issue is that the mechanism is
// 'default' until resolved maybe we can do that resolution here and make the this strictly
// AuthMechanism -> AuthProviders
export function defaultAuthProviders(): Record<string, AuthProvider> {
return AUTH_PROVIDERS;
}
export const AUTH_PROVIDERS = new Map<AuthMechanismId | string, AuthProvider>([
[AuthMechanism.MONGODB_AWS, new MongoDBAWS()],
[AuthMechanism.MONGODB_CR, new MongoCR()],
[AuthMechanism.MONGODB_GSSAPI, new GSSAPI()],
[AuthMechanism.MONGODB_PLAIN, new Plain()],
[AuthMechanism.MONGODB_SCRAM_SHA1, new ScramSHA1()],
[AuthMechanism.MONGODB_SCRAM_SHA256, new ScramSHA256()],
[AuthMechanism.MONGODB_X509, new X509()]
]);
14 changes: 4 additions & 10 deletions src/cmap/auth/gssapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,10 @@ export class GSSAPI extends AuthProvider {
}
}
function makeKerberosClient(authContext: AuthContext, callback: Callback<KerberosClient>): void {
const { host, port } = authContext.options;
const { hostAddress } = authContext.options;
const { credentials } = authContext;
if (!host || !port || !credentials) {
return callback(
new MongoError(
`Connection must specify: ${host ? 'host' : ''}, ${port ? 'port' : ''}, ${
credentials ? 'host' : 'credentials'
}.`
)
);
if (!hostAddress || typeof hostAddress.host !== 'string' || !credentials) {
return callback(new MongoError('Connection must have host and port and credentials defined.'));
}

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

performGssapiCanonicalizeHostName(
host,
hostAddress.host,
mechanismProperties as MechanismProperties,
(err?: Error | MongoError, host?: string) => {
if (err) return callback(err);
Expand Down
12 changes: 6 additions & 6 deletions src/cmap/auth/mongo_credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ export class MongoCredentials {
}

static merge(
creds: MongoCredentials,
creds: MongoCredentials | undefined,
options: Partial<MongoCredentialsOptions>
): MongoCredentials {
return new MongoCredentials({
username: options.username ?? creds.username,
password: options.password ?? creds.password,
mechanism: options.mechanism ?? creds.mechanism,
mechanismProperties: options.mechanismProperties ?? creds.mechanismProperties,
source: options.source ?? creds.source ?? options.db
username: options.username ?? creds?.username ?? '',
password: options.password ?? creds?.password ?? '',
mechanism: options.mechanism ?? creds?.mechanism ?? AuthMechanism.MONGODB_DEFAULT,
mechanismProperties: options.mechanismProperties ?? creds?.mechanismProperties ?? {},
source: options.source ?? options.db ?? creds?.source ?? 'admin'
});
}
}
2 changes: 1 addition & 1 deletion src/cmap/auth/mongodb_aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class MongoDBAWS extends AuthProvider {

if (credentials.username == null) {
makeTempCredentials(credentials, (err, tempCredentials) => {
if (err) return callback(err);
if (err || !tempCredentials) return callback(err);

authContext.credentials = tempCredentials;
this.auth(authContext, callback);
Expand Down
Loading

0 comments on commit b1bdb06

Please sign in to comment.