Skip to content

refactor(NODE-5953): move promisifying of randomBytes to utils #3999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/cmap/auth/mongodb_aws.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import * as crypto from 'crypto';
import * as process from 'process';
import { promisify } from 'util';

import type { Binary, BSONSerializeOptions } from '../../bson';
import * as BSON from '../../bson';
Expand All @@ -11,7 +9,7 @@ import {
MongoMissingCredentialsError,
MongoRuntimeError
} from '../../error';
import { ByteUtils, maxWireVersion, ns, request } from '../../utils';
import { ByteUtils, maxWireVersion, ns, randomBytes, request } from '../../utils';
import { type AuthContext, AuthProvider } from './auth_provider';
import { MongoCredentials } from './mongo_credentials';
import { AuthMechanism } from './providers';
Expand Down Expand Up @@ -59,11 +57,9 @@ interface AWSSaslContinuePayload {
export class MongoDBAWS extends AuthProvider {
static credentialProvider: ReturnType<typeof getAwsCredentialProvider>;
provider?: () => Promise<AWSCredentials>;
randomBytesAsync: (size: number) => Promise<Buffer>;

constructor() {
super();
this.randomBytesAsync = promisify(crypto.randomBytes);
MongoDBAWS.credentialProvider ??= getAwsCredentialProvider();

let { AWS_STS_REGIONAL_ENDPOINTS = '', AWS_REGION = '' } = process.env;
Expand Down Expand Up @@ -131,7 +127,7 @@ export class MongoDBAWS extends AuthProvider {
: undefined;

const db = credentials.source;
const nonce = await this.randomBytesAsync(32);
const nonce = await randomBytes(32);

const saslStart = {
saslStart: 1,
Expand Down
8 changes: 3 additions & 5 deletions src/cmap/auth/scram.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { saslprep } from '@mongodb-js/saslprep';
import * as crypto from 'crypto';
import { promisify } from 'util';

import { Binary, type Document } from '../../bson';
import {
MongoInvalidArgumentError,
MongoMissingCredentialsError,
MongoRuntimeError
} from '../../error';
import { ns } from '../../utils';
import { ns, randomBytes } from '../../utils';
import type { HandshakeDocument } from '../connect';
import { type AuthContext, AuthProvider } from './auth_provider';
import type { MongoCredentials } from './mongo_credentials';
Expand All @@ -18,11 +17,10 @@ type CryptoMethod = 'sha1' | 'sha256';

class ScramSHA extends AuthProvider {
cryptoMethod: CryptoMethod;
randomBytesAsync: (size: number) => Promise<Buffer>;

constructor(cryptoMethod: CryptoMethod) {
super();
this.cryptoMethod = cryptoMethod || 'sha1';
this.randomBytesAsync = promisify(crypto.randomBytes);
}

override async prepare(
Expand All @@ -35,7 +33,7 @@ class ScramSHA extends AuthProvider {
throw new MongoMissingCredentialsError('AuthContext must provide credentials.');
}

const nonce = await this.randomBytesAsync(24);
const nonce = await randomBytes(24);
// store the nonce for later use
authContext.nonce = nonce;

Expand Down
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as http from 'http';
import { clearTimeout, setTimeout } from 'timers';
import * as url from 'url';
import { URL } from 'url';
import { promisify } from 'util';

import { type Document, ObjectId, resolveBSONOptions } from './bson';
import type { Connection } from './cmap/connection';
Expand Down Expand Up @@ -1292,3 +1293,5 @@ export function promiseWithResolvers<T>() {
});
return { promise, resolve, reject } as const;
}

export const randomBytes = promisify(crypto.randomBytes);