From f0f584442e70c6fad41c07a72eb2e9c3f530d6aa Mon Sep 17 00:00:00 2001 From: Alena Khineika Date: Thu, 22 Feb 2024 15:57:47 +0100 Subject: [PATCH] refactor(NODE-5953): move promisifying of randomBytes to utils --- src/cmap/auth/mongodb_aws.ts | 8 ++------ src/cmap/auth/scram.ts | 8 +++----- src/utils.ts | 3 +++ 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/cmap/auth/mongodb_aws.ts b/src/cmap/auth/mongodb_aws.ts index b6676656ccf..dae97056b2c 100644 --- a/src/cmap/auth/mongodb_aws.ts +++ b/src/cmap/auth/mongodb_aws.ts @@ -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'; @@ -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'; @@ -59,11 +57,9 @@ interface AWSSaslContinuePayload { export class MongoDBAWS extends AuthProvider { static credentialProvider: ReturnType; provider?: () => Promise; - randomBytesAsync: (size: number) => Promise; constructor() { super(); - this.randomBytesAsync = promisify(crypto.randomBytes); MongoDBAWS.credentialProvider ??= getAwsCredentialProvider(); let { AWS_STS_REGIONAL_ENDPOINTS = '', AWS_REGION = '' } = process.env; @@ -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, diff --git a/src/cmap/auth/scram.ts b/src/cmap/auth/scram.ts index ba18e0f5c7e..95858f9fef7 100644 --- a/src/cmap/auth/scram.ts +++ b/src/cmap/auth/scram.ts @@ -1,6 +1,5 @@ import { saslprep } from '@mongodb-js/saslprep'; import * as crypto from 'crypto'; -import { promisify } from 'util'; import { Binary, type Document } from '../../bson'; import { @@ -8,7 +7,7 @@ import { 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'; @@ -18,11 +17,10 @@ type CryptoMethod = 'sha1' | 'sha256'; class ScramSHA extends AuthProvider { cryptoMethod: CryptoMethod; - randomBytesAsync: (size: number) => Promise; + constructor(cryptoMethod: CryptoMethod) { super(); this.cryptoMethod = cryptoMethod || 'sha1'; - this.randomBytesAsync = promisify(crypto.randomBytes); } override async prepare( @@ -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; diff --git a/src/utils.ts b/src/utils.ts index 173de9053a5..8020d508f83 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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'; @@ -1292,3 +1293,5 @@ export function promiseWithResolvers() { }); return { promise, resolve, reject } as const; } + +export const randomBytes = promisify(crypto.randomBytes);