Skip to content

Commit 05cf04b

Browse files
docs(NODE-5423): export all public FLE types and classes (#3794)
Co-authored-by: Durran Jordan <durran@gmail.com>
1 parent fee8d3e commit 05cf04b

File tree

9 files changed

+113
-45
lines changed

9 files changed

+113
-45
lines changed

src/client-side-encryption/auto_encrypter.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ import * as cryptoCallbacks from './crypto_callbacks';
1414
import { MongoCryptInvalidArgumentError } from './errors';
1515
import { MongocryptdManager } from './mongocryptd_manager';
1616
import { type KMSProviders, refreshKMSCredentials } from './providers';
17-
import {
18-
type CSFLEKMSTlsOptions,
19-
StateMachine,
20-
type StateMachineExecutable
21-
} from './state_machine';
17+
import { type CSFLEKMSTlsOptions, StateMachine } from './state_machine';
2218

2319
/** @public */
2420
export interface AutoEncryptionOptions {
@@ -229,7 +225,7 @@ const kDecoratedKeys = Symbol.for('@@mdb.decryptedKeys');
229225
* @internal An internal class to be used by the driver for auto encryption
230226
* **NOTE**: Not meant to be instantiated directly, this is for internal use only.
231227
*/
232-
export class AutoEncrypter implements StateMachineExecutable {
228+
export class AutoEncrypter {
233229
_client: MongoClient;
234230
_bypassEncryption: boolean;
235231
_keyVaultNamespace: string;

src/client-side-encryption/client_encryption.ts

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ import {
2424
MongoCryptCreateEncryptedCollectionError,
2525
MongoCryptInvalidArgumentError
2626
} from './errors';
27-
import { type KMSProvider, type KMSProviders, refreshKMSCredentials } from './providers/index';
2827
import {
29-
type CSFLEKMSTlsOptions,
30-
StateMachine,
31-
type StateMachineExecutable
32-
} from './state_machine';
28+
type ClientEncryptionDataKeyProvider,
29+
type KMSProviders,
30+
refreshKMSCredentials
31+
} from './providers/index';
32+
import { type CSFLEKMSTlsOptions, StateMachine } from './state_machine';
3333

3434
/**
35+
* @public
3536
* The schema for a DataKey in the key vault collection.
3637
*/
3738
export interface DataKey {
@@ -46,14 +47,21 @@ export interface DataKey {
4647
}
4748

4849
/**
50+
* @public
4951
* The public interface for explicit in-use encryption
5052
*/
51-
export class ClientEncryption implements StateMachineExecutable {
53+
export class ClientEncryption {
54+
/** @internal */
5255
_client: MongoClient;
56+
/** @internal */
5357
_keyVaultNamespace: string;
58+
/** @internal */
5459
_keyVaultClient: MongoClient;
60+
/** @internal */
5561
_proxyOptions: ProxyOptions;
62+
/** @internal */
5663
_tlsOptions: CSFLEKMSTlsOptions;
64+
/** @internal */
5765
_kmsProviders: KMSProviders;
5866

5967
/** @internal */
@@ -165,7 +173,7 @@ export class ClientEncryption implements StateMachineExecutable {
165173
* ```
166174
*/
167175
createDataKey(
168-
provider: KMSProvider,
176+
provider: ClientEncryptionDataKeyProvider,
169177
options?: ClientEncryptionCreateDataKeyProviderOptions,
170178
callback?: Callback<DataKey>
171179
) {
@@ -268,7 +276,10 @@ export class ClientEncryption implements StateMachineExecutable {
268276
* }
269277
* ```
270278
*/
271-
async rewrapManyDataKey(filter: Filter<DataKey>, options: RewrapManyDataKeyOptions) {
279+
async rewrapManyDataKey(
280+
filter: Filter<DataKey>,
281+
options: ClientEncryptionRewrapManyDataKeyProviderOptions
282+
) {
272283
let keyEncryptionKeyBson = undefined;
273284
if (options) {
274285
const keyEncryptionKey = Object.assign({ provider: options.provider }, options.masterKey);
@@ -533,7 +544,7 @@ export class ClientEncryption implements StateMachineExecutable {
533544
db: Db,
534545
name: string,
535546
options: {
536-
provider: KMSProvider;
547+
provider: ClientEncryptionDataKeyProvider;
537548
createCollectionOptions: Omit<CreateCollectionOptions, 'encryptedFields'> & {
538549
encryptedFields: Document;
539550
};
@@ -569,7 +580,7 @@ export class ClientEncryption implements StateMachineExecutable {
569580
(result): result is PromiseRejectedResult => result.status === 'rejected'
570581
);
571582
if (rejection != null) {
572-
throw new MongoCryptCreateDataKeyError({ encryptedFields, cause: rejection.reason });
583+
throw new MongoCryptCreateDataKeyError(encryptedFields, { cause: rejection.reason });
573584
}
574585
}
575586

@@ -580,7 +591,7 @@ export class ClientEncryption implements StateMachineExecutable {
580591
});
581592
return { collection, encryptedFields };
582593
} catch (cause) {
583-
throw new MongoCryptCreateEncryptedCollectionError({ encryptedFields, cause });
594+
throw new MongoCryptCreateEncryptedCollectionError(encryptedFields, { cause });
584595
}
585596
}
586597

@@ -703,6 +714,7 @@ export class ClientEncryption implements StateMachineExecutable {
703714
}
704715

705716
/**
717+
* @internal
706718
* Ask the user for KMS credentials.
707719
*
708720
* This returns anything that looks like the kmsProviders original input
@@ -718,6 +730,7 @@ export class ClientEncryption implements StateMachineExecutable {
718730
}
719731

720732
/**
733+
* @internal
721734
* A helper that perform explicit encryption of values and expressions.
722735
* Explicitly encrypt a provided value. Note that either `options.keyId` or `options.keyAltName` must
723736
* be specified. Specifying both `options.keyId` and `options.keyAltName` is considered an error.
@@ -780,6 +793,7 @@ export class ClientEncryption implements StateMachineExecutable {
780793
}
781794

782795
/**
796+
* @public
783797
* Options to provide when encrypting data.
784798
*/
785799
export interface ClientEncryptionEncryptOptions {
@@ -817,9 +831,12 @@ export interface ClientEncryptionEncryptOptions {
817831
rangeOptions?: RangeOptions;
818832
}
819833

820-
/** @experimental */
821-
export interface RewrapManyDataKeyOptions {
822-
provider: KMSProvider;
834+
/**
835+
* @public
836+
* @experimental
837+
*/
838+
export interface ClientEncryptionRewrapManyDataKeyProviderOptions {
839+
provider: ClientEncryptionDataKeyProvider;
823840
masterKey?:
824841
| AWSEncryptionKeyOptions
825842
| AzureEncryptionKeyOptions
@@ -828,6 +845,7 @@ export interface RewrapManyDataKeyOptions {
828845
}
829846

830847
/**
848+
* @public
831849
* Additional settings to provide when creating a new `ClientEncryption` instance.
832850
*/
833851
export interface ClientEncryptionOptions {
@@ -858,6 +876,7 @@ export interface ClientEncryptionOptions {
858876
}
859877

860878
/**
879+
* @public
861880
* Configuration options for making an AWS encryption key
862881
*/
863882
export interface AWSEncryptionKeyOptions {
@@ -878,6 +897,7 @@ export interface AWSEncryptionKeyOptions {
878897
}
879898

880899
/**
900+
* @public
881901
* Configuration options for making an AWS encryption key
882902
*/
883903
export interface GCPEncryptionKeyOptions {
@@ -913,6 +933,7 @@ export interface GCPEncryptionKeyOptions {
913933
}
914934

915935
/**
936+
* @public
916937
* Configuration options for making an Azure encryption key
917938
*/
918939
export interface AzureEncryptionKeyOptions {
@@ -933,6 +954,7 @@ export interface AzureEncryptionKeyOptions {
933954
}
934955

935956
/**
957+
* @public
936958
* Options to provide when creating a new data key.
937959
*/
938960
export interface ClientEncryptionCreateDataKeyProviderOptions {
@@ -955,35 +977,43 @@ export interface ClientEncryptionCreateDataKeyProviderOptions {
955977
keyMaterial?: Buffer | Binary;
956978
}
957979

958-
/** @experimental */
959-
export interface RewrapManyDataKeyOptions {
960-
provider: KMSProvider;
980+
/**
981+
* @public
982+
* @experimental
983+
*/
984+
export interface ClientEncryptionRewrapManyDataKeyProviderOptions {
985+
provider: ClientEncryptionDataKeyProvider;
961986
masterKey?:
962987
| AWSEncryptionKeyOptions
963988
| AzureEncryptionKeyOptions
964989
| GCPEncryptionKeyOptions
965990
| undefined;
966991
}
967992

968-
/** @experimental */
993+
/**
994+
* @public
995+
* @experimental
996+
*/
969997
export interface ClientEncryptionRewrapManyDataKeyResult {
970998
/** The result of rewrapping data keys. If unset, no keys matched the filter. */
971999
bulkWriteResult?: BulkWriteResult;
9721000
}
9731001

9741002
/**
1003+
* @public
9751004
* RangeOptions specifies index options for a Queryable Encryption field supporting "rangePreview" queries.
9761005
* min, max, sparsity, and range must match the values set in the encryptedFields of the destination collection.
9771006
* For double and decimal128, min/max/precision must all be set, or all be unset.
9781007
*/
979-
interface RangeOptions {
1008+
export interface RangeOptions {
9801009
min?: any;
9811010
max?: any;
9821011
sparsity: Long;
9831012
precision?: number;
9841013
}
9851014

9861015
/**
1016+
* @public
9871017
* Options to provide when encrypting data.
9881018
*/
9891019
export interface ClientEncryptionEncryptOptions {

src/client-side-encryption/errors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class MongoCryptInvalidArgumentError extends MongoCryptError {
3737
export class MongoCryptCreateDataKeyError extends MongoCryptError {
3838
encryptedFields: Document;
3939
/** @internal */
40-
constructor({ encryptedFields, cause }: { encryptedFields: Document; cause: Error }) {
40+
constructor(encryptedFields: Document, { cause }: { cause: Error }) {
4141
super(`Unable to complete creating data keys: ${cause.message}`, { cause });
4242
this.encryptedFields = encryptedFields;
4343
}
@@ -54,7 +54,7 @@ export class MongoCryptCreateDataKeyError extends MongoCryptError {
5454
export class MongoCryptCreateEncryptedCollectionError extends MongoCryptError {
5555
encryptedFields: Document;
5656
/** @internal */
57-
constructor({ encryptedFields, cause }: { encryptedFields: Document; cause: Error }) {
57+
constructor(encryptedFields: Document, { cause }: { cause: Error }) {
5858
super(`Unable to create collection: ${cause.message}`, { cause });
5959
this.encryptedFields = encryptedFields;
6060
}

src/client-side-encryption/providers/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { loadGCPCredentials } from './gcp';
55
/**
66
* @public
77
*/
8-
export type KMSProvider = 'aws' | 'azure' | 'gcp' | 'local';
8+
export type ClientEncryptionDataKeyProvider = 'aws' | 'azure' | 'gcp' | 'local' | 'kmip';
99

1010
/**
1111
* @public
@@ -132,7 +132,10 @@ export interface KMSProviders {
132132
*
133133
* @internal - exposed for testing purposes only
134134
*/
135-
export function isEmptyCredentials(providerName: KMSProvider, kmsProviders: KMSProviders): boolean {
135+
export function isEmptyCredentials(
136+
providerName: ClientEncryptionDataKeyProvider,
137+
kmsProviders: KMSProviders
138+
): boolean {
136139
const provider = kmsProviders[providerName];
137140
if (provider == null) {
138141
return false;

src/client-side-encryption/state_machine.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { BufferPool, type Callback, MongoDBCollectionNamespace } from '../utils'
1919
import { type DataKey } from './client_encryption';
2020
import { MongoCryptError } from './errors';
2121
import { type MongocryptdManager } from './mongocryptd_manager';
22-
import { type KMSProvider, type KMSProviders } from './providers';
22+
import { type ClientEncryptionDataKeyProvider, type KMSProviders } from './providers';
2323

2424
let socks: SocksLib | null = null;
2525
function loadSocks(): SocksLib {
@@ -97,19 +97,21 @@ declare module 'mongodb-client-encryption' {
9797
* - tlsAllowInvalidCertificates
9898
* - tlsAllowInvalidHostnames
9999
* - tlsInsecure
100+
*
101+
* These options are not included in the type, and are ignored if provided.
100102
*/
101-
export type CSFLETlsOptions = Pick<
103+
export type ClientEncryptionTlsOptions = Pick<
102104
MongoClientOptions,
103105
'tlsCAFile' | 'tlsCertificateKeyFile' | 'tlsCertificateKeyFilePassword'
104106
>;
105107

106108
/** @public */
107109
export type CSFLEKMSTlsOptions = {
108-
aws?: CSFLETlsOptions;
109-
gcp?: CSFLETlsOptions;
110-
kmip?: CSFLETlsOptions;
111-
local?: CSFLETlsOptions;
112-
azure?: CSFLETlsOptions;
110+
aws?: ClientEncryptionTlsOptions;
111+
gcp?: ClientEncryptionTlsOptions;
112+
kmip?: ClientEncryptionTlsOptions;
113+
local?: ClientEncryptionTlsOptions;
114+
azure?: ClientEncryptionTlsOptions;
113115
};
114116

115117
/**
@@ -122,14 +124,14 @@ export type CSFLEKMSTlsOptions = {
122124
export interface StateMachineExecutable {
123125
_keyVaultNamespace: string;
124126
_keyVaultClient: MongoClient;
127+
askForKMSCredentials: () => Promise<KMSProviders>;
125128

126129
/** only used for auto encryption */
127130
_metaDataClient?: MongoClient;
128131
/** only used for auto encryption */
129132
_mongocryptdClient?: MongoClient;
130133
/** only used for auto encryption */
131134
_mongocryptdManager?: MongocryptdManager;
132-
askForKMSCredentials: () => Promise<KMSProviders>;
133135
}
134136

135137
export type StateMachineOptions = {
@@ -402,7 +404,7 @@ export class StateMachine {
402404

403405
const tlsOptions = this.options.tlsOptions;
404406
if (tlsOptions) {
405-
const kmsProvider = request.kmsProvider as KMSProvider;
407+
const kmsProvider = request.kmsProvider as ClientEncryptionDataKeyProvider;
406408
const providerTlsOptions = tlsOptions[kmsProvider];
407409
if (providerTlsOptions) {
408410
const error = this.validateTlsOptions(kmsProvider, providerTlsOptions);
@@ -441,7 +443,10 @@ export class StateMachine {
441443
*
442444
* @returns An error if any option is invalid.
443445
*/
444-
validateTlsOptions(kmsProvider: string, tlsOptions: CSFLETlsOptions): MongoCryptError | void {
446+
validateTlsOptions(
447+
kmsProvider: string,
448+
tlsOptions: ClientEncryptionTlsOptions
449+
): MongoCryptError | void {
445450
const tlsOptionNames = Object.keys(tlsOptions);
446451
for (const option of INSECURE_TLS_OPTIONS) {
447452
if (tlsOptionNames.includes(option)) {
@@ -456,7 +461,7 @@ export class StateMachine {
456461
* @param tlsOptions - The client TLS options for the provider.
457462
* @param options - The existing connection options.
458463
*/
459-
setTlsOptions(tlsOptions: CSFLETlsOptions, options: tls.ConnectionOptions) {
464+
setTlsOptions(tlsOptions: ClientEncryptionTlsOptions, options: tls.ConnectionOptions) {
460465
if (tlsOptions.tlsCertificateKeyFile) {
461466
const cert = fs.readFileSync(tlsOptions.tlsCertificateKeyFile);
462467
options.cert = options.key = cert;

0 commit comments

Comments
 (0)