Skip to content

Commit

Permalink
fix(NODE-5801): use more specific key typing for multiple KMS provide…
Browse files Browse the repository at this point in the history
…r support (#4146)

Co-authored-by: Bailey Pearson <bailey.pearson@mongodb.com>
Co-authored-by: Neal Beeken <neal.beeken@mongodb.com>
  • Loading branch information
3 people committed Jun 14, 2024
1 parent 3ed6a2a commit 465ffd9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 21 deletions.
6 changes: 3 additions & 3 deletions .evergreen/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3493,9 +3493,9 @@ tasks:
- {key: TS_VERSION, value: current}
- func: install dependencies
- func: check types
- name: check-types-typescript-4.1.6
- name: check-types-typescript-4.4
tags:
- check-types-typescript-4.1.6
- check-types-typescript-4.4
- typescript-compilation
commands:
- command: expansions.update
Expand All @@ -3504,7 +3504,7 @@ tasks:
updates:
- {key: NODE_LTS_VERSION, value: '16'}
- {key: NPM_VERSION, value: '9'}
- {key: TS_VERSION, value: 4.1.6}
- {key: TS_VERSION, value: '4.4'}
- func: install dependencies
- func: check types
- name: download-and-merge-coverage
Expand Down
7 changes: 4 additions & 3 deletions .evergreen/generate_evergreen_tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,10 @@ SINGLETON_TASKS.push(
);

function* makeTypescriptTasks() {
for (const TS_VERSION of ['next', 'current', '4.1.6']) {
// 4.1.6 can consume the public API but not compile the driver
if (TS_VERSION !== '4.1.6' && TS_VERSION !== 'next') {
for (const TS_VERSION of ['next', 'current', '4.4']) {
// We don't compile on next, because compilation errors are likely. We do expect
// that the drivers types continue to work with next though.
if (TS_VERSION !== '4.4' && TS_VERSION !== 'next') {
yield {
name: `compile-driver-typescript-${TS_VERSION}`,
tags: [`compile-driver-typescript-${TS_VERSION}`, 'typescript-compilation'],
Expand Down
2 changes: 1 addition & 1 deletion .evergreen/run-typescript.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export TSC="./node_modules/typescript/bin/tsc"
export TS_VERSION=$(get_ts_version)

# On old versions of TS we need to put the node types back to 18.11.19
npm install --no-save --force typescript@"$TS_VERSION" "$(if [[ $TS_VERSION == '4.1.6' ]]; then echo "@types/node@18.11.19"; else echo ""; fi)"
npm install --no-save --force typescript@"$TS_VERSION" "$(if [[ $TS_VERSION == '4.4' ]]; then echo "@types/node@18.11.19"; else echo ""; fi)"

echo "Typescript $($TSC -v)"

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ The official [MongoDB](https://www.mongodb.com/) driver for Node.js.
The GitHub release contains a detached signature file for the NPM package (named
`mongodb-X.Y.Z.tgz.sig`).

The following command returns the link npm package.
The following command returns the link npm package.
```shell
npm view mongodb@vX.Y.Z dist.tarball
npm view mongodb@vX.Y.Z dist.tarball
```

Using the result of the above command, a `curl` command can return the official npm package for the release.
Expand Down Expand Up @@ -81,7 +81,7 @@ The following table describes add-on component version compatibility for the Nod

#### Typescript Version

We recommend using the latest version of typescript, however we currently ensure the driver's public types compile against `typescript@4.1.6`.
We recommend using the latest version of typescript, however we currently ensure the driver's public types compile against `typescript@4.4.0`.
This is the lowest typescript version guaranteed to work with our driver: older versions may or may not work - use at your own risk.
Since typescript [does not restrict breaking changes to major versions](https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes), we consider this support best effort.
If you run into any unexpected compiler failures against our supported TypeScript versions, please let us know by filing an issue on our [JIRA](https://jira.mongodb.org/browse/NODE).
Expand Down
15 changes: 6 additions & 9 deletions src/client-side-encryption/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { loadGCPCredentials } from './gcp';
* `aws:<name>`, `gcp:<name>`, `local:<name>`, `kmip:<name>`, `azure:<name>`
* where `name` is an alphanumeric string, underscores allowed.
*/
export type ClientEncryptionDataKeyProvider = string;
export type ClientEncryptionDataKeyProvider = keyof KMSProviders;

/** @public */
export interface AWSKMSProviderConfiguration {
Expand Down Expand Up @@ -122,34 +122,31 @@ export interface KMSProviders {
* Configuration options for using 'aws' as your KMS provider
*/
aws?: AWSKMSProviderConfiguration | Record<string, never>;
[key: `aws:${string}`]: AWSKMSProviderConfiguration;

/**
* Configuration options for using 'local' as your KMS provider
*/
local?: LocalKMSProviderConfiguration;
[key: `local:${string}`]: LocalKMSProviderConfiguration;

/**
* Configuration options for using 'kmip' as your KMS provider
*/
kmip?: KMIPKMSProviderConfiguration;
[key: `kmip:${string}`]: KMIPKMSProviderConfiguration;

/**
* Configuration options for using 'azure' as your KMS provider
*/
azure?: AzureKMSProviderConfiguration | Record<string, never>;
[key: `azure:${string}`]: AzureKMSProviderConfiguration;

/**
* Configuration options for using 'gcp' as your KMS provider
*/
gcp?: GCPKMSProviderConfiguration | Record<string, never>;

[key: string]:
| AWSKMSProviderConfiguration
| LocalKMSProviderConfiguration
| KMIPKMSProviderConfiguration
| AzureKMSProviderConfiguration
| GCPKMSProviderConfiguration
| undefined;
[key: `gcp:${string}`]: GCPKMSProviderConfiguration;
}

/**
Expand Down
24 changes: 22 additions & 2 deletions test/types/client-side-encryption.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expectAssignable, expectError, expectType } from 'tsd';
import { expectAssignable, expectError, expectNotAssignable, expectType } from 'tsd';

import type {
AWSEncryptionKeyOptions,
Expand All @@ -9,13 +9,14 @@ import type {
KMSProviders,
RangeOptions
} from '../..';
import type { ClientEncryptionDataKeyProvider } from '../mongodb';

type RequiredCreateEncryptedCollectionSettings = Parameters<
ClientEncryption['createEncryptedCollection']
>[2];

expectError<RequiredCreateEncryptedCollectionSettings>({});
expectAssignable<RequiredCreateEncryptedCollectionSettings>({
expectError<RequiredCreateEncryptedCollectionSettings>({
provider: 'blah!',
createCollectionOptions: { encryptedFields: {} }
});
Expand All @@ -32,6 +33,10 @@ expectAssignable<RequiredCreateEncryptedCollectionSettings>({
provider: 'aws',
createCollectionOptions: { encryptedFields: {} }
});
expectAssignable<RequiredCreateEncryptedCollectionSettings>({
provider: 'aws:namedprovider',
createCollectionOptions: { encryptedFields: {} }
});
expectAssignable<RequiredCreateEncryptedCollectionSettings>({
provider: 'aws',
createCollectionOptions: { encryptedFields: {} },
Expand Down Expand Up @@ -83,3 +88,18 @@ expectAssignable<RequiredCreateEncryptedCollectionSettings>({
// automatic
expectAssignable<KMSProviders['gcp']>({});
}

{
expectAssignable<ClientEncryptionDataKeyProvider>('aws');
expectAssignable<ClientEncryptionDataKeyProvider>('gcp');
expectAssignable<ClientEncryptionDataKeyProvider>('azure');
expectAssignable<ClientEncryptionDataKeyProvider>('local');
expectAssignable<ClientEncryptionDataKeyProvider>('kmip');
expectAssignable<ClientEncryptionDataKeyProvider>('aws:named');
expectAssignable<ClientEncryptionDataKeyProvider>('gcp:named');
expectAssignable<ClientEncryptionDataKeyProvider>('azure:named');
expectAssignable<ClientEncryptionDataKeyProvider>('local:named');
expectAssignable<ClientEncryptionDataKeyProvider>('kmip:named');

expectNotAssignable<ClientEncryptionDataKeyProvider>('arbitrary string');
}

0 comments on commit 465ffd9

Please sign in to comment.