Skip to content

Commit

Permalink
refactor(NODE-5953): move promisifying of randomBytes to utils (#3999)
Browse files Browse the repository at this point in the history
  • Loading branch information
alenakhineika committed Feb 22, 2024
1 parent 46b7bbb commit 17952d2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
8 changes: 2 additions & 6 deletions 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';
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
@@ -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
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);

0 comments on commit 17952d2

Please sign in to comment.