Skip to content

Commit 307b3f8

Browse files
committed
chore: comments
1 parent a093422 commit 307b3f8

File tree

3 files changed

+80
-66
lines changed

3 files changed

+80
-66
lines changed

src/cmap/auth/aws_temporary_credentials.ts

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,33 @@ export interface AWSTempCredentials {
1919
/** @public **/
2020
export type AWSCredentialProvider = () => Promise<AWSCredentials>;
2121

22-
/**
23-
* @internal
24-
*
25-
* Fetches temporary AWS credentials.
26-
*/
27-
export abstract class AWSTemporaryCredentialProvider {
28-
abstract getCredentials(): Promise<AWSTempCredentials>;
29-
private static _awsSDK: ReturnType<typeof getAwsCredentialProvider>;
30-
static get awsSDK() {
31-
AWSTemporaryCredentialProvider._awsSDK ??= getAwsCredentialProvider();
32-
return AWSTemporaryCredentialProvider._awsSDK;
33-
}
34-
35-
static get isAWSSDKInstalled(): boolean {
36-
return !('kModuleError' in AWSTemporaryCredentialProvider.awsSDK);
37-
}
38-
}
39-
4022
/** @internal */
41-
export class AWSSDKCredentialProvider extends AWSTemporaryCredentialProvider {
23+
export class AWSSDKCredentialProvider {
24+
private static _awsSDK: ReturnType<typeof getAwsCredentialProvider>;
4225
private _provider?: AWSCredentialProvider;
4326

4427
/**
4528
* Create the SDK credentials provider.
4629
* @param credentialsProvider - The credentials provider.
4730
*/
4831
constructor(credentialsProvider?: AWSCredentialProvider) {
49-
super();
50-
5132
if (credentialsProvider) {
5233
this._provider = credentialsProvider;
5334
}
5435
}
5536

37+
static get awsSDK() {
38+
AWSSDKCredentialProvider._awsSDK ??= getAwsCredentialProvider();
39+
return AWSSDKCredentialProvider._awsSDK;
40+
}
41+
5642
/**
5743
* The AWS SDK caches credentials automatically and handles refresh when the credentials have expired.
5844
* To ensure this occurs, we need to cache the `provider` returned by the AWS sdk and re-use it when fetching credentials.
5945
*/
6046
private get provider(): () => Promise<AWSCredentials> {
61-
if ('kModuleError' in AWSTemporaryCredentialProvider.awsSDK) {
62-
throw AWSTemporaryCredentialProvider.awsSDK.kModuleError;
47+
if ('kModuleError' in AWSSDKCredentialProvider.awsSDK) {
48+
throw AWSSDKCredentialProvider.awsSDK.kModuleError;
6349
}
6450
if (this._provider) {
6551
return this._provider;
@@ -107,15 +93,15 @@ export class AWSSDKCredentialProvider extends AWSTemporaryCredentialProvider {
10793

10894
this._provider =
10995
awsRegionSettingsExist && useRegionalSts
110-
? AWSTemporaryCredentialProvider.awsSDK.fromNodeProviderChain({
96+
? AWSSDKCredentialProvider.awsSDK.fromNodeProviderChain({
11197
clientConfig: { region: AWS_REGION }
11298
})
113-
: AWSTemporaryCredentialProvider.awsSDK.fromNodeProviderChain();
99+
: AWSSDKCredentialProvider.awsSDK.fromNodeProviderChain();
114100

115101
return this._provider;
116102
}
117103

118-
override async getCredentials(): Promise<AWSTempCredentials> {
104+
async getCredentials(): Promise<AWSTempCredentials> {
119105
/*
120106
* Creates a credential provider that will attempt to find credentials from the
121107
* following sources (listed in order of precedence):

src/cmap/auth/mongodb_aws.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import { type AuthContext, AuthProvider } from './auth_provider';
1111
import {
1212
type AWSCredentialProvider,
1313
AWSSDKCredentialProvider,
14-
type AWSTempCredentials,
15-
type AWSTemporaryCredentialProvider
14+
type AWSTempCredentials
1615
} from './aws_temporary_credentials';
1716
import { MongoCredentials } from './mongo_credentials';
1817
import { AuthMechanism } from './providers';
@@ -33,13 +32,10 @@ interface AWSSaslContinuePayload {
3332
}
3433

3534
export class MongoDBAWS extends AuthProvider {
36-
private credentialFetcher: AWSTemporaryCredentialProvider;
37-
private credentialProvider?: AWSCredentialProvider;
35+
private credentialFetcher: AWSSDKCredentialProvider;
3836

3937
constructor(credentialProvider?: AWSCredentialProvider) {
4038
super();
41-
42-
this.credentialProvider = credentialProvider;
4339
this.credentialFetcher = new AWSSDKCredentialProvider(credentialProvider);
4440
}
4541

@@ -159,7 +155,7 @@ export class MongoDBAWS extends AuthProvider {
159155

160156
async function makeTempCredentials(
161157
credentials: MongoCredentials,
162-
awsCredentialFetcher: AWSTemporaryCredentialProvider
158+
awsCredentialFetcher: AWSSDKCredentialProvider
163159
): Promise<MongoCredentials> {
164160
function makeMongoCredentialsFromAWSTemp(creds: AWSTempCredentials) {
165161
// The AWS session token (creds.Token) may or may not be set.

test/integration/auth/mongodb_aws.test.ts

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as sinon from 'sinon';
88
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
99
import { refreshKMSCredentials } from '../../../src/client-side-encryption/providers';
1010
import {
11-
AWSTemporaryCredentialProvider,
11+
AWSSDKCredentialProvider,
1212
type CommandOptions,
1313
Connection,
1414
type Document,
@@ -41,7 +41,7 @@ describe('MONGODB-AWS', function () {
4141

4242
context('when the AWS SDK is not present', function () {
4343
beforeEach(function () {
44-
AWSTemporaryCredentialProvider.awsSDK['kModuleError'] = new MongoMissingDependencyError(
44+
AWSSDKCredentialProvider.awsSDK['kModuleError'] = new MongoMissingDependencyError(
4545
'Missing dependency @aws-sdk/credential-providers',
4646
{
4747
cause: new Error(),
@@ -51,7 +51,7 @@ describe('MONGODB-AWS', function () {
5151
});
5252

5353
afterEach(function () {
54-
delete AWSTemporaryCredentialProvider.awsSDK['kModuleError'];
54+
delete AWSSDKCredentialProvider.awsSDK['kModuleError'];
5555
});
5656

5757
describe('when attempting AWS auth', function () {
@@ -176,7 +176,7 @@ describe('MONGODB-AWS', function () {
176176
});
177177

178178
it('authenticates with a user provided credentials provider', async function () {
179-
const credentialProvider = AWSTemporaryCredentialProvider.awsSDK;
179+
const credentialProvider = AWSSDKCredentialProvider.awsSDK;
180180
const provider = async () => {
181181
providerCount++;
182182
return await credentialProvider.fromNodeProviderChain().apply();
@@ -389,7 +389,7 @@ describe('MONGODB-AWS', function () {
389389
return this.skip();
390390
}
391391

392-
credentialProvider = AWSTemporaryCredentialProvider.awsSDK;
392+
credentialProvider = AWSSDKCredentialProvider.awsSDK;
393393

394394
storedEnv = process.env;
395395
if (test.env.AWS_STS_REGIONAL_ENDPOINTS === undefined) {
@@ -461,46 +461,78 @@ describe('MONGODB-AWS', function () {
461461
});
462462

463463
describe('AWS KMS Credential Fetching', function () {
464-
context('when a credential provider is not provided', function () {
465-
it('KMS credentials are successfully fetched.', async function () {
466-
const { aws } = await refreshKMSCredentials({ aws: {} });
464+
context('when the AWS SDK is not installed', function () {
465+
beforeEach(function () {
466+
AWSSDKCredentialProvider.awsSDK['kModuleError'] = new MongoMissingDependencyError(
467+
'Missing dependency @aws-sdk/credential-providers',
468+
{
469+
cause: new Error(),
470+
dependencyName: '@aws-sdk/credential-providers'
471+
}
472+
);
473+
});
467474

468-
expect(aws).to.have.property('accessKeyId');
469-
expect(aws).to.have.property('secretAccessKey');
475+
afterEach(function () {
476+
delete AWSSDKCredentialProvider.awsSDK['kModuleError'];
477+
});
478+
479+
it('fetching AWS KMS credentials throws an error', async function () {
480+
const result = await refreshKMSCredentials({ aws: {} }).catch(e => e);
481+
482+
// TODO(NODE-7046): Remove branch when removing support for AWS credentials in URI.
483+
// The drivers tools scripts put the credentials in the URI currently for some environments,
484+
// this will need to change when doing the DRIVERS-3131 work.
485+
if (!client.options.credentials.username) {
486+
expect(result).to.be.instanceof(MongoAWSError);
487+
expect(result.message).to.match(/credential-providers/);
488+
} else {
489+
expect(result).to.equal(0);
490+
}
470491
});
471492
});
472493

473-
context('when a credential provider is provided', function () {
474-
let credentialProvider;
475-
let providerCount = 0;
494+
context('when the AWS SDK is installed', function () {
495+
context('when a credential provider is not provided', function () {
496+
it('KMS credentials are successfully fetched.', async function () {
497+
const { aws } = await refreshKMSCredentials({ aws: {} });
476498

477-
beforeEach(function () {
478-
const provider = AWSTemporaryCredentialProvider.awsSDK;
479-
credentialProvider = async () => {
480-
providerCount++;
481-
return await provider.fromNodeProviderChain().apply();
482-
};
499+
expect(aws).to.have.property('accessKeyId');
500+
expect(aws).to.have.property('secretAccessKey');
501+
});
483502
});
484503

485-
it('KMS credentials are successfully fetched.', async function () {
486-
const { aws } = await refreshKMSCredentials({ aws: {} }, { aws: credentialProvider });
504+
context('when a credential provider is provided', function () {
505+
let credentialProvider;
506+
let providerCount = 0;
487507

488-
expect(aws).to.have.property('accessKeyId');
489-
expect(aws).to.have.property('secretAccessKey');
490-
expect(providerCount).to.be.greaterThan(0);
508+
beforeEach(function () {
509+
const provider = AWSSDKCredentialProvider.awsSDK;
510+
credentialProvider = async () => {
511+
providerCount++;
512+
return await provider.fromNodeProviderChain().apply();
513+
};
514+
});
515+
516+
it('KMS credentials are successfully fetched.', async function () {
517+
const { aws } = await refreshKMSCredentials({ aws: {} }, { aws: credentialProvider });
518+
519+
expect(aws).to.have.property('accessKeyId');
520+
expect(aws).to.have.property('secretAccessKey');
521+
expect(providerCount).to.be.greaterThan(0);
522+
});
491523
});
492-
});
493524

494-
it('does not return any extra keys for the `aws` credential provider', async function () {
495-
const { aws } = await refreshKMSCredentials({ aws: {} });
525+
it('does not return any extra keys for the `aws` credential provider', async function () {
526+
const { aws } = await refreshKMSCredentials({ aws: {} });
496527

497-
const keys = new Set(Object.keys(aws ?? {}));
498-
const allowedKeys = ['accessKeyId', 'secretAccessKey', 'sessionToken'];
528+
const keys = new Set(Object.keys(aws ?? {}));
529+
const allowedKeys = ['accessKeyId', 'secretAccessKey', 'sessionToken'];
499530

500-
expect(
501-
Array.from(setDifference(keys, allowedKeys)),
502-
'received an unexpected key in the response refreshing KMS credentials'
503-
).to.deep.equal([]);
531+
expect(
532+
Array.from(setDifference(keys, allowedKeys)),
533+
'received an unexpected key in the response refreshing KMS credentials'
534+
).to.deep.equal([]);
535+
});
504536
});
505537
});
506538
});

0 commit comments

Comments
 (0)