Skip to content

Commit 1841a0e

Browse files
authored
CDRIVER-4369 Add FLE 2 API to AutoEncryptionOpts (#989)
* do not return error on "ns not found" error in drop with encryptedFields * use assert_match_bson, not ASSERT(match_bson * pass encrypted_fields_map to libmongocrypt * add mongoc_auto_encryption_opts_set_bypass_query_analysis * require libmongocrypt 1.5.0 * update csfle test dependency to 6.0.0-rc4 * do not spawn mongocryptd if bypassQueryAnalysis is true
1 parent b6db935 commit 1841a0e

29 files changed

+3565
-64
lines changed

src/libmongoc/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,10 @@ elseif (NOT ENABLE_CLIENT_SIDE_ENCRYPTION STREQUAL OFF)
441441
find_package (mongocrypt QUIET)
442442
endif ()
443443

444-
if (mongocrypt_FOUND AND "${mongocrypt_VERSION}" VERSION_LESS 1.3.0)
444+
if (mongocrypt_FOUND AND "${mongocrypt_VERSION}" VERSION_LESS 1.5.0)
445445
message ("-- libmongocrypt found at ${LIBMONGOCRYPT_LIBRARY}")
446446
message ("-- libmongocrypt version ${mongocrypt_VERSION} found")
447-
message ("-- libmongocrypt version 1.3.0 is required to enable Client-Side Field Level Encryption Support.")
447+
message ("-- libmongocrypt version 1.5.0 is required to enable Client-Side Field Level Encryption Support.")
448448
set (REQUIRED_MONGOCRYPT_VERSION_FOUND OFF)
449449
elseif (mongocrypt_FOUND)
450450
set (REQUIRED_MONGOCRYPT_VERSION_FOUND ON)
@@ -877,7 +877,7 @@ if (MONGOC_TEST_USE_CSFLE)
877877
COMMAND
878878
"${_PYTHON3_EXE}" -u "${mongo-c-driver_SOURCE_DIR}/build/mongodl.py"
879879
--component csfle
880-
--version 5.3.1
880+
--version 6.0.0-rc4
881881
--edition enterprise
882882
--out "${CMAKE_CURRENT_BINARY_DIR}"
883883
--only "**/mongo_csfle_v1.*"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
:man_page: mongoc_auto_encryption_opts_set_bypass_query_analysis
2+
3+
mongoc_auto_encryption_opts_set_bypass_query_analysis()
4+
=======================================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
void
12+
mongoc_auto_encryption_opts_set_bypass_query_analysis (
13+
mongoc_auto_encryption_opts_t *opts, bool bypass_query_analysis);
14+
15+
16+
Parameters
17+
----------
18+
19+
* ``opts``: The :symbol:`mongoc_auto_encryption_opts_t`
20+
* ``bypass_query_analysis``: A boolean.
21+
22+
23+
``bypass_query_analysis`` disables automatic analysis of outgoing commands.
24+
``bypass_query_analysis`` is useful for encrypting indexed fields without the ``csfle`` shared library or ``mongocryptd`` process.
25+
Set ``bypass_query_analysis`` to true to use explicit encryption on indexed fields.
26+
27+
.. seealso::
28+
29+
| :symbol:`mongoc_client_enable_auto_encryption()`
30+
31+
| The guide for :doc:`Using Client-Side Field Level Encryption <using_client_side_encryption>`
32+

src/libmongoc/doc/mongoc_auto_encryption_opts_t.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ Synopsis
3636
mongoc_auto_encryption_opts_set_extra
3737
mongoc_auto_encryption_opts_set_tls_opts
3838
mongoc_auto_encryption_opts_set_encrypted_fields_map
39+
mongoc_auto_encryption_opts_set_bypass_query_analysis
3940

src/libmongoc/doc/mongoc_collection_drop_with_opts.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ If the collection does not exist, the server responds with an "ns not found" err
5959
6060
In MongoDB 3.0 and older, the "ns not found" error code is the generic MONGOC_ERROR_QUERY_FAILURE; in this case check whether the error message is equal to the string "ns not found".
6161

62+
The ``encryptedFields`` document in ``opts`` may be used to create a collection used for :doc:`Using Client-Side Field Level Encryption <using_client_side_encryption>`. If ``encryptedFields`` is specifed, the "ns not found" error is not returned.
63+
6264
Errors
6365
------
6466

src/libmongoc/src/mongoc/mongoc-client-side-encryption.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct _mongoc_auto_encryption_opts_t {
4444
bson_t *schema_map;
4545
bson_t *encrypted_fields_map;
4646
bool bypass_auto_encryption;
47+
bool bypass_query_analysis;
4748
bson_t *extra;
4849
};
4950

@@ -181,6 +182,16 @@ mongoc_auto_encryption_opts_set_bypass_auto_encryption (
181182
opts->bypass_auto_encryption = bypass_auto_encryption;
182183
}
183184

185+
void
186+
mongoc_auto_encryption_opts_set_bypass_query_analysis (
187+
mongoc_auto_encryption_opts_t *opts, bool bypass_query_analysis)
188+
{
189+
if (!opts) {
190+
return;
191+
}
192+
opts->bypass_query_analysis = bypass_query_analysis;
193+
}
194+
184195
void
185196
mongoc_auto_encryption_opts_set_extra (mongoc_auto_encryption_opts_t *opts,
186197
const bson_t *extra)
@@ -1331,18 +1342,22 @@ _mongoc_cse_client_enable_auto_encryption (mongoc_client_t *client,
13311342
client->topology->crypt =
13321343
_mongoc_crypt_new (opts->kms_providers,
13331344
opts->schema_map,
1345+
opts->encrypted_fields_map,
13341346
opts->tls_opts,
13351347
client->topology->csfle_override_path,
13361348
client->topology->csfle_required,
13371349
opts->bypass_auto_encryption,
1350+
opts->bypass_query_analysis,
13381351
error);
13391352
if (!client->topology->crypt) {
13401353
GOTO (fail);
13411354
}
13421355

13431356
client->topology->bypass_auto_encryption = opts->bypass_auto_encryption;
1357+
client->topology->bypass_query_analysis = opts->bypass_query_analysis;
13441358

1345-
if (!client->topology->bypass_auto_encryption) {
1359+
if (!client->topology->bypass_auto_encryption &&
1360+
!client->topology->bypass_query_analysis) {
13461361
if (!client->topology->mongocryptd_bypass_spawn) {
13471362
if (!_spawn_mongocryptd (client->topology->mongocryptd_spawn_path,
13481363
client->topology->mongocryptd_spawn_args,
@@ -1485,18 +1500,21 @@ _mongoc_cse_client_pool_enable_auto_encryption (
14851500

14861501
topology->crypt = _mongoc_crypt_new (opts->kms_providers,
14871502
opts->schema_map,
1503+
opts->encrypted_fields_map,
14881504
opts->tls_opts,
14891505
topology->csfle_override_path,
14901506
topology->csfle_required,
14911507
opts->bypass_auto_encryption,
1508+
opts->bypass_query_analysis,
14921509
error);
14931510
if (!topology->crypt) {
14941511
GOTO (fail);
14951512
}
14961513

14971514
topology->bypass_auto_encryption = opts->bypass_auto_encryption;
1515+
topology->bypass_query_analysis = opts->bypass_query_analysis;
14981516

1499-
if (!topology->bypass_auto_encryption) {
1517+
if (!topology->bypass_auto_encryption && !topology->bypass_query_analysis) {
15001518
if (!topology->mongocryptd_bypass_spawn) {
15011519
if (!_spawn_mongocryptd (topology->mongocryptd_spawn_path,
15021520
topology->mongocryptd_spawn_args,
@@ -1584,10 +1602,12 @@ mongoc_client_encryption_new (mongoc_client_encryption_opts_t *opts,
15841602
client_encryption->crypt =
15851603
_mongoc_crypt_new (opts->kms_providers,
15861604
NULL /* schema_map */,
1605+
NULL /* encrypted_fields_map */,
15871606
opts->tls_opts,
15881607
NULL /* No csfle path */,
15891608
false /* csfle not requried */,
15901609
true, /* bypassAutoEncryption (We are explicit) */
1610+
false /* bypass_query_analysis. Not applicable. */,
15911611
error);
15921612
if (!client_encryption->crypt) {
15931613
goto fail;

src/libmongoc/src/mongoc/mongoc-client-side-encryption.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ MONGOC_EXPORT (void)
7272
mongoc_auto_encryption_opts_set_bypass_auto_encryption (
7373
mongoc_auto_encryption_opts_t *opts, bool bypass_auto_encryption);
7474

75+
MONGOC_EXPORT (void)
76+
mongoc_auto_encryption_opts_set_bypass_query_analysis (
77+
mongoc_auto_encryption_opts_t *opts, bool bypass_query_analysis);
78+
7579
MONGOC_EXPORT (void)
7680
mongoc_auto_encryption_opts_set_extra (mongoc_auto_encryption_opts_t *opts,
7781
const bson_t *extra);

src/libmongoc/src/mongoc/mongoc-collection.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,11 @@ drop_with_opts_with_encryptedFields (mongoc_collection_t *collection,
10671067

10681068
/* Drop data collection. */
10691069
if (!drop_with_opts (collection, opts, error)) {
1070-
goto fail;
1070+
if (error->code == MONGOC_SERVER_ERR_NS_NOT_FOUND) {
1071+
memset (error, 0, sizeof (bson_error_t));
1072+
} else {
1073+
goto fail;
1074+
}
10711075
}
10721076

10731077
/* Drop ESC collection. */
@@ -1080,7 +1084,11 @@ drop_with_opts_with_encryptedFields (mongoc_collection_t *collection,
10801084
escCollection = mongoc_client_get_collection (
10811085
collection->client, collection->db, escName);
10821086
if (!drop_with_opts (escCollection, NULL /* opts */, error)) {
1083-
goto fail;
1087+
if (error->code == MONGOC_SERVER_ERR_NS_NOT_FOUND) {
1088+
memset (error, 0, sizeof (bson_error_t));
1089+
} else {
1090+
goto fail;
1091+
}
10841092
}
10851093

10861094
/* Drop ECC collection. */
@@ -1093,7 +1101,11 @@ drop_with_opts_with_encryptedFields (mongoc_collection_t *collection,
10931101
eccCollection = mongoc_client_get_collection (
10941102
collection->client, collection->db, eccName);
10951103
if (!drop_with_opts (eccCollection, NULL /* opts */, error)) {
1096-
goto fail;
1104+
if (error->code == MONGOC_SERVER_ERR_NS_NOT_FOUND) {
1105+
memset (error, 0, sizeof (bson_error_t));
1106+
} else {
1107+
goto fail;
1108+
}
10971109
}
10981110

10991111
/* Drop ECOC collection. */
@@ -1106,7 +1118,11 @@ drop_with_opts_with_encryptedFields (mongoc_collection_t *collection,
11061118
ecocCollection = mongoc_client_get_collection (
11071119
collection->client, collection->db, ecocName);
11081120
if (!drop_with_opts (ecocCollection, NULL /* opts */, error)) {
1109-
goto fail;
1121+
if (error->code == MONGOC_SERVER_ERR_NS_NOT_FOUND) {
1122+
memset (error, 0, sizeof (bson_error_t));
1123+
} else {
1124+
goto fail;
1125+
}
11101126
}
11111127

11121128
ok = true;

src/libmongoc/src/mongoc/mongoc-crypt-private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ Creates a new handle into libmongocrypt.
3636
_mongoc_crypt_t *
3737
_mongoc_crypt_new (const bson_t *kms_providers,
3838
const bson_t *schema_map,
39+
const bson_t *encrypted_fields_map,
3940
const bson_t *tls_opts,
4041
const char *csfle_override_path,
4142
bool csfle_required,
4243
bool bypass_auto_encryption,
44+
bool bypass_query_analysis,
4345
bson_error_t *error);
4446

4547
void

src/libmongoc/src/mongoc/mongoc-crypt.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,15 +913,18 @@ _parse_all_tls_opts (_mongoc_crypt_t *crypt,
913913
_mongoc_crypt_t *
914914
_mongoc_crypt_new (const bson_t *kms_providers,
915915
const bson_t *schema_map,
916+
const bson_t *encrypted_fields_map,
916917
const bson_t *tls_opts,
917918
const char *csfle_override_path,
918919
bool csfle_required,
919920
bool bypass_auto_encryption,
921+
bool bypass_query_analysis,
920922
bson_error_t *error)
921923
{
922924
_mongoc_crypt_t *crypt;
923925
mongocrypt_binary_t *local_masterkey_bin = NULL;
924926
mongocrypt_binary_t *schema_map_bin = NULL;
927+
mongocrypt_binary_t *encrypted_fields_map_bin = NULL;
925928
mongocrypt_binary_t *kms_providers_bin = NULL;
926929
bool success = false;
927930

@@ -952,6 +955,17 @@ _mongoc_crypt_new (const bson_t *kms_providers,
952955
}
953956
}
954957

958+
if (encrypted_fields_map) {
959+
encrypted_fields_map_bin = mongocrypt_binary_new_from_data (
960+
(uint8_t *) bson_get_data (encrypted_fields_map),
961+
encrypted_fields_map->len);
962+
if (!mongocrypt_setopt_encrypted_field_config_map (
963+
crypt->handle, encrypted_fields_map_bin)) {
964+
_crypt_check_error (crypt->handle, error, true);
965+
goto fail;
966+
}
967+
}
968+
955969
if (!bypass_auto_encryption) {
956970
mongocrypt_setopt_append_csfle_search_path (crypt->handle, "$SYSTEM");
957971
if (!_crypt_check_error (crypt->handle, error, false)) {
@@ -967,6 +981,13 @@ _mongoc_crypt_new (const bson_t *kms_providers,
967981
}
968982
}
969983

984+
if (bypass_query_analysis) {
985+
mongocrypt_setopt_bypass_query_analysis (crypt->handle);
986+
if (!_crypt_check_error (crypt->handle, error, false)) {
987+
goto fail;
988+
}
989+
}
990+
970991
if (!mongocrypt_init (crypt->handle)) {
971992
_crypt_check_error (crypt->handle, error, true);
972993
goto fail;
@@ -994,6 +1015,7 @@ _mongoc_crypt_new (const bson_t *kms_providers,
9941015
success = true;
9951016
fail:
9961017
mongocrypt_binary_destroy (local_masterkey_bin);
1018+
mongocrypt_binary_destroy (encrypted_fields_map_bin);
9971019
mongocrypt_binary_destroy (schema_map_bin);
9981020
mongocrypt_binary_destroy (kms_providers_bin);
9991021

src/libmongoc/src/mongoc/mongoc-error-private.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ typedef enum {
5050
MONGOC_SERVER_ERR_STALECONFIG = 13388,
5151
MONGOC_SERVER_ERR_NOTPRIMARYNOSECONDARYOK = 13435,
5252
MONGOC_SERVER_ERR_NOTPRIMARYORSECONDARY = 13436,
53-
MONGOC_SERVER_ERR_LEGACYNOTPRIMARY = 10058
53+
MONGOC_SERVER_ERR_LEGACYNOTPRIMARY = 10058,
54+
MONGOC_SERVER_ERR_NS_NOT_FOUND = 26
5455
} mongoc_server_err_t;
5556

5657
mongoc_read_err_type_t

src/libmongoc/src/mongoc/mongoc-topology-private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ typedef struct _mongoc_topology_t {
185185
bool mongocryptd_bypass_spawn;
186186
char *mongocryptd_spawn_path;
187187
bson_t *mongocryptd_spawn_args;
188+
bool bypass_query_analysis;
188189
#endif
189190

190191
// Corresponds to extraOptions.csflePath

0 commit comments

Comments
 (0)