Skip to content

Commit bddafc1

Browse files
authored
fix(connection-form): clear autoEncryption when empty (#3071)
See the comment for `unsetAutoEncryptionIfEmpty`, this otherwise makes connecting fail when fully removing previously configured CSFLE settings for a connection.
1 parent 23b9335 commit bddafc1

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

packages/connection-form/src/utils/csfle-handler.spec.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ describe('csfle-handler', function () {
8585
}).connectionOptions.fleOptions
8686
).to.deep.equal({
8787
storeCredentials: false,
88-
autoEncryption: {},
88+
autoEncryption: undefined,
8989
});
9090
});
9191
});
@@ -124,9 +124,7 @@ describe('csfle-handler', function () {
124124
}).connectionOptions.fleOptions
125125
).to.deep.equal({
126126
storeCredentials: false,
127-
autoEncryption: {
128-
kmsProviders: {},
129-
},
127+
autoEncryption: undefined,
130128
});
131129
});
132130
});
@@ -165,9 +163,7 @@ describe('csfle-handler', function () {
165163
}).connectionOptions.fleOptions
166164
).to.deep.equal({
167165
storeCredentials: false,
168-
autoEncryption: {
169-
tlsOptions: {},
170-
},
166+
autoEncryption: undefined,
171167
});
172168
});
173169
});

packages/connection-form/src/utils/csfle-handler.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import queryParser from 'mongodb-query-parser';
1111

1212
const DEFAULT_FLE_OPTIONS: NonNullable<ConnectionOptions['fleOptions']> = {
1313
storeCredentials: false,
14-
autoEncryption: {},
14+
autoEncryption: undefined,
1515
};
1616

1717
type KeysOfUnion<T> = T extends T ? keyof T : never;
@@ -89,7 +89,7 @@ export function handleUpdateCsfleParam({
8989
fleOptions: {
9090
...DEFAULT_FLE_OPTIONS,
9191
...connectionOptions.fleOptions,
92-
autoEncryption,
92+
autoEncryption: unsetAutoEncryptionIfEmpty(autoEncryption),
9393
},
9494
},
9595
};
@@ -127,10 +127,10 @@ export function handleUpdateCsfleKmsParam({
127127
fleOptions: {
128128
...DEFAULT_FLE_OPTIONS,
129129
...connectionOptions.fleOptions,
130-
autoEncryption: {
130+
autoEncryption: unsetAutoEncryptionIfEmpty({
131131
...autoEncryption,
132132
kmsProviders,
133-
},
133+
}),
134134
},
135135
},
136136
};
@@ -168,15 +168,25 @@ export function handleUpdateCsfleKmsTlsParam({
168168
fleOptions: {
169169
...DEFAULT_FLE_OPTIONS,
170170
...connectionOptions.fleOptions,
171-
autoEncryption: {
171+
autoEncryption: unsetAutoEncryptionIfEmpty({
172172
...autoEncryption,
173173
tlsOptions,
174-
},
174+
}),
175175
},
176176
},
177177
};
178178
}
179179

180+
// The driver creates an AutoEncrypter object if `.autoEncryption` has been set
181+
// as an option, regardless of whether it is filled. Consequently, we need
182+
// to set it to undefined explicitly if the user wants to disable automatic
183+
// CSFLE entirely (indicated by removing all CSFLE options).
184+
export function unsetAutoEncryptionIfEmpty(
185+
o?: AutoEncryptionOptions
186+
): AutoEncryptionOptions | undefined {
187+
return o && hasAnyCsfleOption(o) ? o : undefined;
188+
}
189+
180190
export function hasAnyCsfleOption(o: Readonly<AutoEncryptionOptions>): boolean {
181191
return !!(
182192
o.bypassAutoEncryption ||

packages/data-service/src/connection-options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface ConnectionFleOptions {
3131
/**
3232
* Encryption options passed to the driver verbatim.
3333
*/
34-
autoEncryption: AutoEncryptionOptions;
34+
autoEncryption?: AutoEncryptionOptions;
3535
}
3636

3737
export interface ConnectionSshOptions {

packages/data-service/src/data-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2413,7 +2413,7 @@ export class DataServiceImpl extends EventEmitter implements DataService {
24132413
...fleOptions?.autoEncryption?.encryptedFieldsMap,
24142414
...fleOptions?.autoEncryption?.schemaMap,
24152415
}),
2416-
keyVaultNamespace: fleOptions?.autoEncryption.keyVaultNamespace,
2416+
keyVaultNamespace: fleOptions?.autoEncryption?.keyVaultNamespace,
24172417
kmsProviders,
24182418
};
24192419
}

0 commit comments

Comments
 (0)