Skip to content

Commit 4998992

Browse files
convert KMS providers to TS
1 parent d301690 commit 4998992

File tree

12 files changed

+453
-221
lines changed

12 files changed

+453
-221
lines changed

package-lock.json

Lines changed: 93 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
"eslint-plugin-simple-import-sort": "^10.0.0",
9191
"eslint-plugin-tsdoc": "^0.2.17",
9292
"express": "^4.18.2",
93+
"gcp-metadata": "^5.2.0",
9394
"js-yaml": "^4.1.0",
9495
"mocha": "^10.2.0",
9596
"mocha-sinon": "^2.1.2",

src/client-side-encryption/errors.ts

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,77 @@
1+
import { type Document } from '../bson';
2+
13
/**
2-
* @class
4+
* @public
35
* An error indicating that something went wrong specifically with MongoDB Client Encryption
46
*/
57
export class MongoCryptError extends Error {
6-
constructor(message, options = {}) {
8+
cause?: Error | string;
9+
constructor(message: string, options: { cause?: Error | string } = {}) {
710
super(message);
811
if (options.cause != null) {
912
this.cause = options.cause;
1013
}
1114
}
1215

13-
get name() {
16+
override get name() {
1417
return 'MongoCryptError';
1518
}
1619
}
1720

1821
/**
19-
* @class
22+
* @public
2023
* An error indicating that `ClientEncryption.createEncryptedCollection()` failed to create data keys
2124
*/
2225
export class MongoCryptCreateDataKeyError extends MongoCryptError {
26+
// TODO: type encrypted fields
27+
encryptedFields: Document;
2328
constructor({ encryptedFields, cause }) {
2429
super(`Unable to complete creating data keys: ${cause.message}`, { cause });
2530
this.encryptedFields = encryptedFields;
2631
}
2732

28-
get name() {
33+
override get name() {
2934
return 'MongoCryptCreateDataKeyError';
3035
}
3136
}
3237

3338
/**
34-
* @class
39+
* @public
3540
* An error indicating that `ClientEncryption.createEncryptedCollection()` failed to create a collection
3641
*/
3742
export class MongoCryptCreateEncryptedCollectionError extends MongoCryptError {
43+
// TODO: type encrypted fields
44+
encryptedFields: Document;
3845
constructor({ encryptedFields, cause }) {
3946
super(`Unable to create collection: ${cause.message}`, { cause });
4047
this.encryptedFields = encryptedFields;
4148
}
4249

43-
get name() {
50+
override get name() {
4451
return 'MongoCryptCreateEncryptedCollectionError';
4552
}
4653
}
4754

4855
/**
49-
* @class
56+
* @public
5057
* An error indicating that mongodb-client-encryption failed to auto-refresh Azure KMS credentials.
5158
*/
5259
export class MongoCryptAzureKMSRequestError extends MongoCryptError {
53-
/**
54-
* @param {string} message
55-
* @param {object | undefined} body
56-
*/
57-
constructor(message, body) {
60+
/** The body of the http response that failed, if present. */
61+
body?: Document;
62+
constructor(message: string, body?: Document) {
5863
super(message);
5964
this.body = body;
6065
}
66+
67+
override get name(): string {
68+
return 'MongoCryptAzureKMSRequestError';
69+
}
6170
}
6271

63-
export class MongoCryptKMSRequestNetworkTimeoutError extends MongoCryptError {}
72+
/** @public */
73+
export class MongoCryptKMSRequestNetworkTimeoutError extends MongoCryptError {
74+
override get name(): string {
75+
return 'MongoCryptKMSRequestNetworkTimeoutError';
76+
}
77+
}
Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
let awsCredentialProviders = null;
2-
/** @ignore */
3-
export async function loadAWSCredentials(kmsProviders) {
4-
if (awsCredentialProviders == null) {
5-
try {
6-
// Ensure you always wrap an optional require in the try block NODE-3199
7-
awsCredentialProviders = require('@aws-sdk/credential-providers');
8-
// eslint-disable-next-line no-empty
9-
} catch {}
10-
}
1+
import { getAwsCredentialProvider } from '../../deps';
2+
import { type KMSProviders } from '.';
3+
4+
/**
5+
* @internal
6+
*/
7+
export async function loadAWSCredentials(kmsProviders: KMSProviders): Promise<KMSProviders> {
8+
const credentialProvider = getAwsCredentialProvider();
119

12-
if (awsCredentialProviders != null) {
13-
const { fromNodeProviderChain } = awsCredentialProviders;
14-
const provider = fromNodeProviderChain();
15-
// The state machine is the only place calling this so it will
16-
// catch if there is a rejection here.
17-
const aws = await provider();
18-
return { ...kmsProviders, aws };
10+
if ('kModuleError' in credentialProvider) {
11+
return kmsProviders;
1912
}
2013

21-
return kmsProviders;
14+
const { fromNodeProviderChain } = credentialProvider;
15+
const provider = fromNodeProviderChain();
16+
// The state machine is the only place calling this so it will
17+
// catch if there is a rejection here.
18+
const aws = await provider();
19+
// TODO - figure out the type mismatch here
20+
return { ...kmsProviders, aws };
2221
}

0 commit comments

Comments
 (0)