Skip to content

Commit 18cf71f

Browse files
CDRIVER-4424: Additional error handling tests for CEC (#1147)
Also fixes: Not catching missing `encryptedFields` when attempting to create an encrypted collection
1 parent 6072b69 commit 18cf71f

File tree

2 files changed

+163
-4
lines changed

2 files changed

+163
-4
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2764,6 +2764,16 @@ mongoc_client_encryption_create_encrypted_collection (
27642764
goto done;
27652765
}
27662766

2767+
if (bson_empty (&in_encryptedFields)) {
2768+
bson_set_error (error,
2769+
MONGOC_ERROR_COMMAND,
2770+
MONGOC_ERROR_COMMAND_INVALID_ARG,
2771+
"No 'encryptedFields' are defined for the creation of "
2772+
"the '%s' collection",
2773+
name);
2774+
goto done;
2775+
}
2776+
27672777
// Add the keyIds to the encryptedFields.
27682778
// Context for the creation of new datakeys:
27692779
struct cec_context ctx = {

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

Lines changed: 153 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5477,7 +5477,7 @@ test_auto_datakeys (void *unused)
54775477
}
54785478

54795479
static void
5480-
test_create_encrypted_collection (void *unused)
5480+
test_create_encrypted_collection_simple (void *unused)
54815481
{
54825482
BSON_UNUSED (unused);
54835483
bson_error_t error = {0};
@@ -5508,7 +5508,7 @@ test_create_encrypted_collection (void *unused)
55085508
mongoc_client_encryption_opts_new ();
55095509
mongoc_client_encryption_opts_set_kms_providers (ceOpts, kmsProviders);
55105510
mongoc_client_encryption_opts_set_keyvault_namespace (
5511-
ceOpts, "keyvaule", "datakeys");
5511+
ceOpts, "keyvault", "datakeys");
55125512
mongoc_client_encryption_opts_set_keyvault_client (ceOpts, client);
55135513
mongoc_client_encryption_t *const ce =
55145514
mongoc_client_encryption_new (ceOpts, &error);
@@ -5550,6 +5550,135 @@ test_create_encrypted_collection (void *unused)
55505550
mongoc_client_destroy (client);
55515551
}
55525552

5553+
static void
5554+
test_create_encrypted_collection_no_encryptedFields (void *unused)
5555+
{
5556+
BSON_UNUSED (unused);
5557+
bson_error_t error = {0};
5558+
mongoc_client_t *const client = test_framework_new_default_client ();
5559+
bson_t *const kmsProviders = _make_kms_providers (false, true);
5560+
5561+
const char *const dbName = "cec-test-db";
5562+
5563+
// Drop prior data
5564+
{
5565+
mongoc_collection_t *const coll =
5566+
mongoc_client_get_collection (client, "keyvault", "datakeys");
5567+
if (coll) {
5568+
mongoc_collection_drop (coll, &error);
5569+
bool okay =
5570+
error.code == 0 || strstr (error.message, "ns not found") != NULL;
5571+
ASSERT_OR_PRINT (okay, error);
5572+
}
5573+
mongoc_collection_destroy (coll);
5574+
5575+
mongoc_database_t *const db = mongoc_client_get_database (client, dbName);
5576+
ASSERT_OR_PRINT (mongoc_database_drop (db, &error), error);
5577+
mongoc_database_destroy (db);
5578+
}
5579+
5580+
// Create a CE
5581+
mongoc_client_encryption_opts_t *const ceOpts =
5582+
mongoc_client_encryption_opts_new ();
5583+
mongoc_client_encryption_opts_set_kms_providers (ceOpts, kmsProviders);
5584+
mongoc_client_encryption_opts_set_keyvault_namespace (
5585+
ceOpts, "keyvault", "datakeys");
5586+
mongoc_client_encryption_opts_set_keyvault_client (ceOpts, client);
5587+
mongoc_client_encryption_t *const ce =
5588+
mongoc_client_encryption_new (ceOpts, &error);
5589+
mongoc_client_encryption_opts_destroy (ceOpts);
5590+
ASSERT_OR_PRINT (ce, error);
5591+
5592+
// Create the encrypted collection
5593+
bsonBuildDecl (ccOpts);
5594+
mongoc_database_t *const db = mongoc_client_get_database (client, dbName);
5595+
mongoc_client_encryption_datakey_opts_t *const dkOpts =
5596+
mongoc_client_encryption_datakey_opts_new ();
5597+
mongoc_collection_t *const coll =
5598+
mongoc_client_encryption_create_encrypted_collection (
5599+
ce, db, "test-coll", &ccOpts, NULL, "local", dkOpts, &error);
5600+
ASSERT_ERROR_CONTAINS (error,
5601+
MONGOC_ERROR_COMMAND,
5602+
MONGOC_ERROR_COMMAND_INVALID_ARG,
5603+
"No 'encryptedFields' are defined");
5604+
bson_destroy (&ccOpts);
5605+
5606+
bson_destroy (kmsProviders);
5607+
mongoc_client_encryption_datakey_opts_destroy (dkOpts);
5608+
mongoc_collection_destroy (coll);
5609+
mongoc_database_drop (db, &error);
5610+
mongoc_database_destroy (db);
5611+
mongoc_client_encryption_destroy (ce);
5612+
mongoc_client_destroy (client);
5613+
}
5614+
5615+
static void
5616+
test_create_encrypted_collection_bad_keyId (void *unused)
5617+
{
5618+
BSON_UNUSED (unused);
5619+
bson_error_t error = {0};
5620+
mongoc_client_t *const client = test_framework_new_default_client ();
5621+
bson_t *const kmsProviders = _make_kms_providers (false, true);
5622+
5623+
const char *const dbName = "cec-test-db";
5624+
5625+
// Drop prior data
5626+
{
5627+
mongoc_collection_t *const coll =
5628+
mongoc_client_get_collection (client, "keyvault", "datakeys");
5629+
if (coll) {
5630+
mongoc_collection_drop (coll, &error);
5631+
bool okay =
5632+
error.code == 0 || strstr (error.message, "ns not found") != NULL;
5633+
ASSERT_OR_PRINT (okay, error);
5634+
}
5635+
mongoc_collection_destroy (coll);
5636+
5637+
mongoc_database_t *const db = mongoc_client_get_database (client, dbName);
5638+
ASSERT_OR_PRINT (mongoc_database_drop (db, &error), error);
5639+
mongoc_database_destroy (db);
5640+
}
5641+
5642+
// Create a CE
5643+
mongoc_client_encryption_opts_t *const ceOpts =
5644+
mongoc_client_encryption_opts_new ();
5645+
mongoc_client_encryption_opts_set_kms_providers (ceOpts, kmsProviders);
5646+
mongoc_client_encryption_opts_set_keyvault_namespace (
5647+
ceOpts, "keyvault", "datakeys");
5648+
mongoc_client_encryption_opts_set_keyvault_client (ceOpts, client);
5649+
mongoc_client_encryption_t *const ce =
5650+
mongoc_client_encryption_new (ceOpts, &error);
5651+
mongoc_client_encryption_opts_destroy (ceOpts);
5652+
ASSERT_OR_PRINT (ce, error);
5653+
5654+
// Create the encrypted collection
5655+
bsonBuildDecl (ccOpts,
5656+
kv ("encryptedFields",
5657+
doc (kv ("fields",
5658+
array (doc (kv ("path", cstr ("ssn")),
5659+
kv ("bsonType", cstr ("string")),
5660+
kv ("keyId", bool (true))))))));
5661+
mongoc_database_t *const db = mongoc_client_get_database (client, dbName);
5662+
mongoc_client_encryption_datakey_opts_t *const dkOpts =
5663+
mongoc_client_encryption_datakey_opts_new ();
5664+
mongoc_collection_t *const coll =
5665+
mongoc_client_encryption_create_encrypted_collection (
5666+
ce, db, "test-coll", &ccOpts, NULL, "local", dkOpts, &error);
5667+
ASSERT_ERROR_CONTAINS (error,
5668+
MONGOC_ERROR_QUERY,
5669+
MONGOC_ERROR_PROTOCOL_INVALID_REPLY,
5670+
"create.encryptedFields.fields.keyId");
5671+
bson_destroy (&ccOpts);
5672+
5673+
bson_destroy (kmsProviders);
5674+
mongoc_client_encryption_datakey_opts_destroy (dkOpts);
5675+
mongoc_collection_destroy (coll);
5676+
mongoc_database_drop (db, &error);
5677+
mongoc_database_destroy (db);
5678+
mongoc_client_encryption_destroy (ce);
5679+
mongoc_client_destroy (client);
5680+
}
5681+
55535682
void
55545683
test_client_side_encryption_install (TestSuite *suite)
55555684
{
@@ -5858,9 +5987,29 @@ test_client_side_encryption_install (TestSuite *suite)
58585987
NULL,
58595988
NULL);
58605989

5990+
TestSuite_AddFull (
5991+
suite,
5992+
"/client_side_encryption/createEncryptedCollection/simple",
5993+
test_create_encrypted_collection_simple,
5994+
NULL,
5995+
NULL,
5996+
test_framework_skip_if_no_client_side_encryption,
5997+
test_framework_skip_if_max_wire_version_less_than_17,
5998+
test_framework_skip_if_single);
5999+
6000+
TestSuite_AddFull (suite,
6001+
"/client_side_encryption/createEncryptedCollection/"
6002+
"missing-encryptedFields",
6003+
test_create_encrypted_collection_no_encryptedFields,
6004+
NULL,
6005+
NULL,
6006+
test_framework_skip_if_no_client_side_encryption,
6007+
test_framework_skip_if_max_wire_version_less_than_17,
6008+
test_framework_skip_if_single);
58616009
TestSuite_AddFull (suite,
5862-
"/client_side_encryption/createEncryptedCollection",
5863-
test_create_encrypted_collection,
6010+
"/client_side_encryption/createEncryptedCollection/"
6011+
"bad-keyId",
6012+
test_create_encrypted_collection_bad_keyId,
58646013
NULL,
58656014
NULL,
58666015
test_framework_skip_if_no_client_side_encryption,

0 commit comments

Comments
 (0)