Skip to content

Commit

Permalink
cache AWS credential provider
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Mar 26, 2024
1 parent 00a4755 commit 7c72a03
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
3 changes: 2 additions & 1 deletion src/client-side-encryption/auto_encrypter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ export class AutoEncrypter {
* fields were decrypted.
*/
[kDecorateResult] = false;
_credentialProvider: KMSCredentialProvider;
/** @internal */
private _credentialProvider: KMSCredentialProvider;

/** @internal */
static getMongoCrypt(): MongoCryptConstructor {
Expand Down
2 changes: 1 addition & 1 deletion src/client-side-encryption/client_encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class ClientEncryption {
/** @internal */
_tlsOptions: CSFLEKMSTlsOptions;
/** @internal */
_credentialProvider: KMSCredentialProvider;
private _credentialProvider: KMSCredentialProvider;
/** @internal */
_mongoCrypt: MongoCrypt;

Expand Down
25 changes: 0 additions & 25 deletions src/client-side-encryption/providers/aws.ts

This file was deleted.

28 changes: 26 additions & 2 deletions src/client-side-encryption/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { loadAWSCredentials } from './aws';
import { AWSSDKCredentialProvider } from '../../cmap/auth/aws_temporary_credentials';
import { loadAzureCredentials } from './azure';
import { loadGCPCredentials } from './gcp';

Expand Down Expand Up @@ -145,8 +145,17 @@ export function isEmptyCredentials(

/**
* @internal
*
* A class that fetchs KMS credentials on-demand during client encryption. This class is instantiated
* per client encryption or auto encrypter and caches the AWS credential provider, if AWS is being used.
*/
export class KMSCredentialProvider {
private _awsCredentialProvider?: AWSSDKCredentialProvider;
private get awsCredentialProvider(): AWSSDKCredentialProvider {
this._awsCredentialProvider ??= new AWSSDKCredentialProvider();
return this._awsCredentialProvider;
}

constructor(private readonly kmsProviders: KMSProviders) {}

/**
Expand All @@ -158,7 +167,22 @@ export class KMSCredentialProvider {
let finalKMSProviders = this.kmsProviders;

if (isEmptyCredentials('aws', this.kmsProviders)) {
finalKMSProviders = await loadAWSCredentials(finalKMSProviders);
// We shouldn't ever receive a response from the AWS SDK that doesn't have these
// fields. However, TS says these fields are optional. We provide empty strings
// and let libmongocrypt error if we're unable to fetch the required keys.
const {
SecretAccessKey = '',
Token = '',
AccessKeyId = ''
} = await this.awsCredentialProvider.getCredentials();
finalKMSProviders = {
...this.kmsProviders,
aws: {
secretAccessKey: SecretAccessKey,
sessionToken: Token,
accessKeyId: AccessKeyId
}
};
}

if (isEmptyCredentials('gcp', this.kmsProviders)) {
Expand Down

0 comments on commit 7c72a03

Please sign in to comment.