@@ -47,6 +47,33 @@ export interface ClientSideFieldLevelEncryptionOptions {
47
47
bypassQueryAnalysis ?: boolean ;
48
48
}
49
49
50
+ type MasterKey = AWSEncryptionKeyOptions | AzureEncryptionKeyOptions | GCPEncryptionKeyOptions ;
51
+ type AltNames = string [ ] ;
52
+
53
+ type DataKeyEncryptionKeyOptions = {
54
+ masterKey ?: MasterKey ;
55
+ keyAltNames ?: AltNames ;
56
+ keyMaterial ?: Buffer | BinaryType
57
+ } ;
58
+
59
+ type MasterKeyOrAltNamesOrDataKeyOptions = MasterKey | DataKeyEncryptionKeyOptions | AltNames | string ;
60
+
61
+ const isDataKeyEncryptionKeyOptions = ( options ?: MasterKeyOrAltNamesOrDataKeyOptions ) : options is DataKeyEncryptionKeyOptions => {
62
+ return (
63
+ ! Array . isArray ( options ) &&
64
+ typeof options === 'object' &&
65
+ ( 'masterKey' in options || 'keyAltNames' in options || 'keyMaterial' in options )
66
+ ) ;
67
+ } ;
68
+
69
+ const isMasterKey = ( options ?: MasterKeyOrAltNamesOrDataKeyOptions ) : options is MasterKey => {
70
+ return (
71
+ ! Array . isArray ( options ) &&
72
+ typeof options === 'object' &&
73
+ ! isDataKeyEncryptionKeyOptions ( options )
74
+ ) ;
75
+ } ;
76
+
50
77
@shellApiClassDefault
51
78
@classPlatforms ( [ ReplPlatform . CLI ] )
52
79
export class ClientEncryption extends ShellApiWithMongoClass {
@@ -80,15 +107,24 @@ export class ClientEncryption extends ShellApiWithMongoClass {
80
107
async encrypt (
81
108
encryptionId : BinaryType ,
82
109
value : any ,
83
- encryptionAlgorithm : ClientEncryptionEncryptOptions [ 'algorithm' ]
110
+ algorithmOrEncryptionOptions : ClientEncryptionEncryptOptions [ 'algorithm' ] | ClientEncryptionEncryptOptions
84
111
) : Promise < BinaryType > {
85
- assertArgsDefinedType ( [ encryptionId , value , encryptionAlgorithm ] , [ true , true , true ] , 'ClientEncryption.encrypt' ) ;
112
+ let encryptionOptions : ClientEncryptionEncryptOptions ;
113
+ if ( typeof algorithmOrEncryptionOptions === 'object' ) {
114
+ encryptionOptions = {
115
+ keyId : encryptionId ,
116
+ ...algorithmOrEncryptionOptions
117
+ } ;
118
+ } else {
119
+ encryptionOptions = {
120
+ keyId : encryptionId ,
121
+ algorithm : algorithmOrEncryptionOptions
122
+ } ;
123
+ }
124
+ assertArgsDefinedType ( [ encryptionId , value , encryptionOptions ] , [ true , true , true ] , 'ClientEncryption.encrypt' ) ;
86
125
return await this . _libmongocrypt . encrypt (
87
126
value ,
88
- {
89
- keyId : encryptionId ,
90
- algorithm : encryptionAlgorithm
91
- }
127
+ encryptionOptions
92
128
) ;
93
129
}
94
130
@@ -160,29 +196,46 @@ export class KeyVault extends ShellApiWithMongoClass {
160
196
161
197
createKey ( kms : 'local' , keyAltNames ?: string [ ] ) : Promise < BinaryType >
162
198
createKey ( kms : ClientEncryptionDataKeyProvider , legacyMasterKey : string , keyAltNames ?: string [ ] ) : Promise < BinaryType >
163
- createKey ( kms : ClientEncryptionDataKeyProvider , options : AWSEncryptionKeyOptions | AzureEncryptionKeyOptions | GCPEncryptionKeyOptions | undefined ) : Promise < BinaryType >
164
- createKey ( kms : ClientEncryptionDataKeyProvider , options : AWSEncryptionKeyOptions | AzureEncryptionKeyOptions | GCPEncryptionKeyOptions | undefined , keyAltNames : string [ ] ) : Promise < BinaryType >
199
+ createKey ( kms : ClientEncryptionDataKeyProvider , options : MasterKey | DataKeyEncryptionKeyOptions | undefined ) : Promise < BinaryType >
200
+ createKey ( kms : ClientEncryptionDataKeyProvider , options : MasterKey | DataKeyEncryptionKeyOptions | undefined , keyAltNames : string [ ] ) : Promise < BinaryType >
165
201
@returnsPromise
166
202
@apiVersions ( [ 1 ] )
203
+ // eslint-disable-next-line complexity
167
204
async createKey (
168
205
kms : ClientEncryptionDataKeyProvider ,
169
- masterKeyOrAltNames ?: AWSEncryptionKeyOptions | AzureEncryptionKeyOptions | GCPEncryptionKeyOptions | string | undefined | string [ ] ,
170
- keyAltNames ?: string [ ]
206
+ masterKeyOrAltNamesOrDataKeyOptions ?: MasterKeyOrAltNamesOrDataKeyOptions ,
207
+ legacyKeyAltNames ?: string [ ]
171
208
) : Promise < BinaryType > {
209
+ let masterKey : MasterKey | undefined ;
210
+ let keyAltNames ;
211
+ let keyMaterial ;
212
+
213
+ if ( isDataKeyEncryptionKeyOptions ( masterKeyOrAltNamesOrDataKeyOptions ) ) {
214
+ masterKey = masterKeyOrAltNamesOrDataKeyOptions ?. masterKey ;
215
+ keyAltNames = masterKeyOrAltNamesOrDataKeyOptions ?. keyAltNames ;
216
+ keyMaterial = masterKeyOrAltNamesOrDataKeyOptions ?. keyMaterial ;
217
+ } else if ( isMasterKey ( masterKeyOrAltNamesOrDataKeyOptions ) ) {
218
+ masterKey = masterKeyOrAltNamesOrDataKeyOptions ;
219
+ }
220
+
221
+ if ( legacyKeyAltNames ) {
222
+ keyAltNames = legacyKeyAltNames ;
223
+ }
224
+
172
225
assertArgsDefinedType ( [ kms ] , [ true ] , 'KeyVault.createKey' ) ;
173
226
174
- if ( typeof masterKeyOrAltNames === 'string' ) {
175
- if ( kms === 'local' && masterKeyOrAltNames === '' ) {
227
+ if ( typeof masterKeyOrAltNamesOrDataKeyOptions === 'string' ) {
228
+ if ( kms === 'local' && masterKeyOrAltNamesOrDataKeyOptions === '' ) {
176
229
// allowed in the old shell - even enforced prior to 4.2.3
177
230
// https://docs.mongodb.com/manual/reference/method/KeyVault.createKey/
178
- masterKeyOrAltNames = undefined ;
231
+ masterKey = undefined ;
179
232
} else {
180
233
throw new MongoshInvalidInputError (
181
234
'KeyVault.createKey does not support providing masterKey as string anymore. For AWS please use createKey("aws", { region: ..., key: ... })' ,
182
235
CommonErrors . Deprecated
183
236
) ;
184
237
}
185
- } else if ( Array . isArray ( masterKeyOrAltNames ) ) {
238
+ } else if ( Array . isArray ( masterKeyOrAltNamesOrDataKeyOptions ) ) {
186
239
// old signature - one could immediately provide an array of key alt names
187
240
// not documented but visible in code: https://github.com/mongodb/mongo/blob/eb2b72cf9c0269f086223d499ac9be8a270d268c/src/mongo/shell/keyvault.js#L19
188
241
if ( kms !== 'local' ) {
@@ -198,22 +251,22 @@ export class KeyVault extends ShellApiWithMongoClass {
198
251
) ;
199
252
}
200
253
201
- keyAltNames = masterKeyOrAltNames ;
202
- masterKeyOrAltNames = undefined ;
254
+ keyAltNames = masterKeyOrAltNamesOrDataKeyOptions ;
255
+ masterKey = undefined ;
203
256
}
204
257
}
205
258
206
259
let options : ClientEncryptionCreateDataKeyProviderOptions | undefined ;
207
- if ( masterKeyOrAltNames ) {
208
- options = {
209
- masterKey : masterKeyOrAltNames as ClientEncryptionCreateDataKeyProviderOptions [ 'masterKey' ]
210
- } ;
260
+
261
+ if ( masterKey ) {
262
+ options = { ...( options ?? { } ) , masterKey } ;
211
263
}
212
264
if ( keyAltNames ) {
213
- options = {
214
- ...( options ?? { } ) ,
215
- keyAltNames
216
- } ;
265
+ options = { ...( options ?? { } ) , keyAltNames } ;
266
+ }
267
+ if ( keyMaterial ) {
268
+ // @ts -expect-error waiting for driver release
269
+ options = { ...( options ?? { } ) , keyMaterial } ;
217
270
}
218
271
219
272
return await this . _clientEncryption . _libmongocrypt . createDataKey ( kms , options as ClientEncryptionCreateDataKeyProviderOptions ) ;
0 commit comments