Skip to content

Commit e24a7a2

Browse files
[CDRIVER-4568] Limit options available for createEncryptedCollection (#1218)
* Modify create-encrypted-collection to only accept a masterKey option * Parameterise CEC tests: - Run each test against a "local" KMS provider - Run each test against an "aws" KMS provider
1 parent 2979cca commit e24a7a2

File tree

4 files changed

+80
-51
lines changed

4 files changed

+80
-51
lines changed

src/libmongoc/doc/mongoc_client_encryption_create_encrypted_collection.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Synopsis
1616
const bson_t *in_options,
1717
bson_t *out_options,
1818
const char *kms_provider,
19-
const mongoc_client_encryption_datakey_opts_t *dk_opts,
19+
const bson_t *opt_masterKey,
2020
bson_error_t *error)
2121
BSON_GNUC_WARN_UNUSED_RESULT;
2222
@@ -47,8 +47,9 @@ Parameters
4747
must be destroyed by the caller. If ``NULL``, has no effect.
4848
* ``kms_provider``: The name of the KMS provider to use for generating new data
4949
encryption keys for encrypted fields within the collection.
50-
* ``dk_opts``: Additional options to be used when creating data encryption keys
51-
for the collection.
50+
* ``opt_masterKey``: If provided, used as the masterkey option when data
51+
encryption keys need to be created. (See:
52+
:doc:`mongoc_client_encryption_datakey_opts_set_masterkey`)
5253
* ``error``: Optional output parameter pointing to storage for a
5354
:symbol:`bson_error_t`. If an error occurs, will be initialized with error
5455
information.

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ mongoc_client_encryption_create_encrypted_collection (
974974
const bson_t *in_options,
975975
bson_t *out_options,
976976
const char *const kms_provider,
977-
const mongoc_client_encryption_datakey_opts_t *dk_opts,
977+
const bson_t *opt_masterkey,
978978
bson_error_t *error)
979979
{
980980
BSON_UNUSED (enc);
@@ -983,7 +983,7 @@ mongoc_client_encryption_create_encrypted_collection (
983983
BSON_UNUSED (in_options);
984984
BSON_UNUSED (out_options);
985985
BSON_UNUSED (kms_provider);
986-
BSON_UNUSED (dk_opts);
986+
BSON_UNUSED (opt_masterkey);
987987

988988
_disabled_error (error);
989989
return NULL;
@@ -2932,16 +2932,16 @@ mongoc_client_encryption_create_encrypted_collection (
29322932
const bson_t *in_options,
29332933
bson_t *out_options,
29342934
const char *const kms_provider,
2935-
const mongoc_client_encryption_datakey_opts_t *dk_opts,
2935+
const bson_t *opt_masterkey,
29362936
bson_error_t *error)
29372937
{
29382938
BSON_ASSERT_PARAM (enc);
29392939
BSON_ASSERT_PARAM (database);
29402940
BSON_ASSERT_PARAM (name);
29412941
BSON_ASSERT_PARAM (in_options);
29422942
BSON_ASSERT (out_options || true);
2943+
BSON_ASSERT (opt_masterkey || true);
29432944
BSON_ASSERT_PARAM (kms_provider);
2944-
BSON_ASSERT_PARAM (dk_opts);
29452945
BSON_ASSERT (error || true);
29462946

29472947
mongoc_collection_t *ret = NULL;
@@ -2950,6 +2950,13 @@ mongoc_client_encryption_create_encrypted_collection (
29502950
bson_t new_encryptedFields = BSON_INITIALIZER;
29512951
bson_t local_new_options = BSON_INITIALIZER;
29522952

2953+
mongoc_client_encryption_datakey_opts_t *dk_opts =
2954+
mongoc_client_encryption_datakey_opts_new ();
2955+
if (opt_masterkey) {
2956+
mongoc_client_encryption_datakey_opts_set_masterkey (dk_opts,
2957+
opt_masterkey);
2958+
}
2959+
29532960
if (!out_options) {
29542961
// We'll use our own storage for the new options
29552962
out_options = &local_new_options;
@@ -3036,6 +3043,7 @@ mongoc_client_encryption_create_encrypted_collection (
30363043
done:
30373044
bson_destroy (&new_encryptedFields);
30383045
bson_destroy (&in_encryptedFields);
3046+
mongoc_client_encryption_datakey_opts_destroy (dk_opts);
30393047
// Destroy the local options, which may or may not have been used. If unused,
30403048
// the new options are now owned by the caller and this is a no-op.
30413049
bson_destroy (&local_new_options);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ mongoc_client_encryption_create_encrypted_collection (
328328
const bson_t *in_options,
329329
bson_t *opt_out_options,
330330
const char *const kms_provider,
331-
const mongoc_client_encryption_datakey_opts_t *dk_opts,
331+
const bson_t *opt_masterkey,
332332
bson_error_t *error) BSON_GNUC_WARN_UNUSED_RESULT;
333333

334334
BSON_END_DECLS

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

Lines changed: 63 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6218,10 +6218,9 @@ test_auto_datakeys (void *unused)
62186218
require (
62196219
keyWithType ("0", doc), //
62206220
parse (require (allOf (key ("keyId"), strEqual ("keepme")), nop))),
6221-
require (
6222-
keyWithType ("1", doc),
6223-
parse (require (allOf (keyWithType ("keyId", int32)),
6224-
do (ASSERT_CMPINT32 (bsonAs (int32), ==, 42))))));
6221+
require (keyWithType ("1", doc),
6222+
parse (require (allOf (keyWithType ("keyId", int32)),
6223+
do(ASSERT_CMPINT32 (bsonAs (int32), ==, 42))))));
62256224
ASSERT (bsonParseError == NULL);
62266225
bson_destroy (&out_fields);
62276226

@@ -6245,12 +6244,28 @@ test_auto_datakeys (void *unused)
62456244
}
62466245

62476246
static void
6248-
test_create_encrypted_collection_simple (void *unused)
6247+
_do_cec_test (void (*test) (const char *kmsProvider))
6248+
{
6249+
test ("local");
6250+
test ("aws");
6251+
}
6252+
6253+
// Declare a createEncryptedCollection test case (See usage below)
6254+
#define CEC_TEST(name, ...) \
6255+
static void name##_impl (__VA_ARGS__); \
6256+
static void name (void *unused) \
6257+
{ \
6258+
BSON_UNUSED (unused); \
6259+
_do_cec_test (name##_impl); \
6260+
} \
6261+
static void name##_impl (__VA_ARGS__)
6262+
6263+
CEC_TEST (test_create_encrypted_collection_simple, const char *kmsProvider)
62496264
{
6250-
BSON_UNUSED (unused);
62516265
bson_error_t error = {0};
62526266
mongoc_client_t *const client = test_framework_new_default_client ();
6253-
bson_t *const kmsProviders = _make_kms_providers (false, true);
6267+
bson_t *const kmsProviders = _make_kms_providers (true, true);
6268+
bson_t *const tlsOptions = _make_tls_opts ();
62546269

62556270
const char *const dbName = "cec-test-db";
62566271

@@ -6275,6 +6290,7 @@ test_create_encrypted_collection_simple (void *unused)
62756290
mongoc_client_encryption_opts_t *const ceOpts =
62766291
mongoc_client_encryption_opts_new ();
62776292
mongoc_client_encryption_opts_set_kms_providers (ceOpts, kmsProviders);
6293+
mongoc_client_encryption_opts_set_tls_opts (ceOpts, tlsOptions);
62786294
mongoc_client_encryption_opts_set_keyvault_namespace (
62796295
ceOpts, "keyvault", "datakeys");
62806296
mongoc_client_encryption_opts_set_keyvault_client (ceOpts, client);
@@ -6291,13 +6307,13 @@ test_create_encrypted_collection_simple (void *unused)
62916307
kv ("bsonType", cstr ("string")),
62926308
kv ("keyId", null)))))));
62936309
mongoc_database_t *const db = mongoc_client_get_database (client, dbName);
6294-
mongoc_client_encryption_datakey_opts_t *const dkOpts =
6295-
mongoc_client_encryption_datakey_opts_new ();
6310+
bson_t *const mkey = _make_kms_masterkey (kmsProvider);
62966311
mongoc_collection_t *const coll =
62976312
mongoc_client_encryption_create_encrypted_collection (
6298-
ce, db, "test-coll", &ccOpts, NULL, "local", dkOpts, &error);
6313+
ce, db, "test-coll", &ccOpts, NULL, kmsProvider, mkey, &error);
62996314
ASSERT_OR_PRINT (coll, error);
63006315
bson_destroy (&ccOpts);
6316+
bson_destroy (mkey);
63016317

63026318
bsonBuildDecl (doc, kv ("ssn", cstr ("123-45-6789")));
63036319
const bool okay =
@@ -6310,7 +6326,7 @@ test_create_encrypted_collection_simple (void *unused)
63106326
bson_destroy (&doc);
63116327

63126328
bson_destroy (kmsProviders);
6313-
mongoc_client_encryption_datakey_opts_destroy (dkOpts);
6329+
bson_destroy (tlsOptions);
63146330
mongoc_collection_destroy (coll);
63156331
mongoc_database_drop (db, &error);
63166332
mongoc_database_destroy (db);
@@ -6320,10 +6336,14 @@ test_create_encrypted_collection_simple (void *unused)
63206336

63216337
static void
63226338
test_create_encrypted_collection_no_encryptedFields_helper (
6323-
mongoc_client_t *client, const char *dbName, const char *collName)
6339+
mongoc_client_t *client,
6340+
const char *dbName,
6341+
const char *collName,
6342+
const char *kmsProvider)
63246343
{
63256344
bson_error_t error = {0};
6326-
bson_t *const kmsProviders = _make_kms_providers (false, true);
6345+
bson_t *const kmsProviders = _make_kms_providers (true, true);
6346+
bson_t *const tlsOptions = _make_tls_opts ();
63276347

63286348
// Drop prior data
63296349
{
@@ -6346,6 +6366,7 @@ test_create_encrypted_collection_no_encryptedFields_helper (
63466366
mongoc_client_encryption_opts_t *const ceOpts =
63476367
mongoc_client_encryption_opts_new ();
63486368
mongoc_client_encryption_opts_set_kms_providers (ceOpts, kmsProviders);
6369+
mongoc_client_encryption_opts_set_tls_opts (ceOpts, tlsOptions);
63496370
mongoc_client_encryption_opts_set_keyvault_namespace (
63506371
ceOpts, "keyvault", "datakeys");
63516372
mongoc_client_encryption_opts_set_keyvault_client (ceOpts, client);
@@ -6355,41 +6376,38 @@ test_create_encrypted_collection_no_encryptedFields_helper (
63556376
ASSERT_OR_PRINT (ce, error);
63566377

63576378
// Create the encrypted collection
6358-
bsonBuildDecl (ccOpts, do ());
6379+
bsonBuildDecl (ccOpts, do());
63596380
mongoc_database_t *const db = mongoc_client_get_database (client, dbName);
6360-
mongoc_client_encryption_datakey_opts_t *const dkOpts =
6361-
mongoc_client_encryption_datakey_opts_new ();
6381+
bson_t *const mkey = _make_kms_masterkey (kmsProvider);
63626382
mongoc_collection_t *const coll =
63636383
mongoc_client_encryption_create_encrypted_collection (
6364-
ce, db, collName, &ccOpts, NULL, "local", dkOpts, &error);
6384+
ce, db, collName, &ccOpts, NULL, kmsProvider, mkey, &error);
63656385
ASSERT_ERROR_CONTAINS (error,
63666386
MONGOC_ERROR_COMMAND,
63676387
MONGOC_ERROR_COMMAND_INVALID_ARG,
63686388
"No 'encryptedFields' are defined");
63696389
bson_destroy (&ccOpts);
6390+
bson_destroy (mkey);
63706391

63716392
bson_destroy (kmsProviders);
6372-
mongoc_client_encryption_datakey_opts_destroy (dkOpts);
6393+
bson_destroy (tlsOptions);
63736394
mongoc_collection_destroy (coll);
63746395
mongoc_database_drop (db, &error);
63756396
mongoc_database_destroy (db);
63766397
mongoc_client_encryption_destroy (ce);
63776398
}
63786399

6379-
6380-
static void
6381-
test_create_encrypted_collection_no_encryptedFields (void *unused)
6400+
CEC_TEST (test_create_encrypted_collection_no_encryptedFields,
6401+
const char *kmsProvider)
63826402
{
6383-
BSON_UNUSED (unused);
6384-
63856403
const char *dbName = "cec-test-db";
63866404
const char *collName = "test-coll";
63876405

63886406
// Test with a default client.
63896407
{
63906408
mongoc_client_t *const client = test_framework_new_default_client ();
63916409
test_create_encrypted_collection_no_encryptedFields_helper (
6392-
client, dbName, collName);
6410+
client, dbName, collName, kmsProvider);
63936411
mongoc_client_destroy (client);
63946412
}
63956413

@@ -6401,7 +6419,7 @@ test_create_encrypted_collection_no_encryptedFields (void *unused)
64016419
mongoc_auto_encryption_opts_t *aeOpts =
64026420
mongoc_auto_encryption_opts_new ();
64036421
bson_t *const kmsProviders =
6404-
_make_kms_providers (false /* with aws */, true /* with local */);
6422+
_make_kms_providers (true /* with aws */, true /* with local */);
64056423
char *namespace = bson_strdup_printf ("%s.%s", dbName, collName);
64066424
bson_t *encryptedFieldsMap =
64076425
tmp_bson ("{'%s': {'fields': []}}", namespace);
@@ -6416,7 +6434,7 @@ test_create_encrypted_collection_no_encryptedFields (void *unused)
64166434
mongoc_client_enable_auto_encryption (client, aeOpts, &error), error);
64176435

64186436
test_create_encrypted_collection_no_encryptedFields_helper (
6419-
client, dbName, collName);
6437+
client, dbName, collName, kmsProvider);
64206438

64216439
bson_free (namespace);
64226440
bson_destroy (kmsProviders);
@@ -6425,13 +6443,13 @@ test_create_encrypted_collection_no_encryptedFields (void *unused)
64256443
}
64266444
}
64276445

6428-
static void
6429-
test_create_encrypted_collection_bad_keyId (void *unused)
6446+
CEC_TEST (test_create_encrypted_collection_bad_keyId,
6447+
const char *const kmsProvider)
64306448
{
6431-
BSON_UNUSED (unused);
64326449
bson_error_t error = {0};
64336450
mongoc_client_t *const client = test_framework_new_default_client ();
6434-
bson_t *const kmsProviders = _make_kms_providers (false, true);
6451+
bson_t *const kmsProviders = _make_kms_providers (true, true);
6452+
bson_t *const tlsOptions = _make_tls_opts ();
64356453

64366454
const char *const dbName = "cec-test-db";
64376455

@@ -6456,6 +6474,7 @@ test_create_encrypted_collection_bad_keyId (void *unused)
64566474
mongoc_client_encryption_opts_t *const ceOpts =
64576475
mongoc_client_encryption_opts_new ();
64586476
mongoc_client_encryption_opts_set_kms_providers (ceOpts, kmsProviders);
6477+
mongoc_client_encryption_opts_set_tls_opts (ceOpts, tlsOptions);
64596478
mongoc_client_encryption_opts_set_keyvault_namespace (
64606479
ceOpts, "keyvault", "datakeys");
64616480
mongoc_client_encryption_opts_set_keyvault_client (ceOpts, client);
@@ -6472,19 +6491,19 @@ test_create_encrypted_collection_bad_keyId (void *unused)
64726491
kv ("bsonType", cstr ("string")),
64736492
kv ("keyId", bool (true))))))));
64746493
mongoc_database_t *const db = mongoc_client_get_database (client, dbName);
6475-
mongoc_client_encryption_datakey_opts_t *const dkOpts =
6476-
mongoc_client_encryption_datakey_opts_new ();
6494+
bson_t *const mkey = _make_kms_masterkey (kmsProvider);
64776495
mongoc_collection_t *const coll =
64786496
mongoc_client_encryption_create_encrypted_collection (
6479-
ce, db, "test-coll", &ccOpts, NULL, "local", dkOpts, &error);
6497+
ce, db, "test-coll", &ccOpts, NULL, kmsProvider, mkey, &error);
64806498
ASSERT_ERROR_CONTAINS (error,
64816499
MONGOC_ERROR_QUERY,
64826500
MONGOC_ERROR_PROTOCOL_INVALID_REPLY,
64836501
"create.encryptedFields.fields.keyId");
64846502
bson_destroy (&ccOpts);
6503+
bson_destroy (mkey);
64856504

64866505
bson_destroy (kmsProviders);
6487-
mongoc_client_encryption_datakey_opts_destroy (dkOpts);
6506+
bson_destroy (tlsOptions);
64886507
mongoc_collection_destroy (coll);
64896508
mongoc_database_drop (db, &error);
64906509
mongoc_database_destroy (db);
@@ -6493,13 +6512,13 @@ test_create_encrypted_collection_bad_keyId (void *unused)
64936512
}
64946513

64956514
// Implements Prose Test 21. Case: 4.
6496-
static void
6497-
test_create_encrypted_collection_insert (void *unused)
6515+
CEC_TEST (test_create_encrypted_collection_insert,
6516+
const char *const kmsProvider)
64986517
{
6499-
BSON_UNUSED (unused);
65006518
bson_error_t error = {0};
65016519
mongoc_client_t *const client = test_framework_new_default_client ();
6502-
bson_t *const kmsProviders = _make_kms_providers (false, true);
6520+
bson_t *const kmsProviders = _make_kms_providers (true, true);
6521+
bson_t *const tlsOptions = _make_tls_opts ();
65036522

65046523
const char *const dbName = "cec-test-db";
65056524

@@ -6524,6 +6543,7 @@ test_create_encrypted_collection_insert (void *unused)
65246543
mongoc_client_encryption_opts_t *const ceOpts =
65256544
mongoc_client_encryption_opts_new ();
65266545
mongoc_client_encryption_opts_set_kms_providers (ceOpts, kmsProviders);
6546+
mongoc_client_encryption_opts_set_tls_opts (ceOpts, tlsOptions);
65276547
mongoc_client_encryption_opts_set_keyvault_namespace (
65286548
ceOpts, "keyvault", "datakeys");
65296549
mongoc_client_encryption_opts_set_keyvault_client (ceOpts, client);
@@ -6540,14 +6560,14 @@ test_create_encrypted_collection_insert (void *unused)
65406560
kv ("bsonType", cstr ("string")),
65416561
kv ("keyId", null)))))));
65426562
mongoc_database_t *const db = mongoc_client_get_database (client, dbName);
6543-
mongoc_client_encryption_datakey_opts_t *const dkOpts =
6544-
mongoc_client_encryption_datakey_opts_new ();
65456563
bson_t new_opts;
6564+
bson_t *const mkey = _make_kms_masterkey (kmsProvider);
65466565
mongoc_collection_t *const coll =
65476566
mongoc_client_encryption_create_encrypted_collection (
6548-
ce, db, "testing1", &ccOpts, &new_opts, "local", dkOpts, &error);
6567+
ce, db, "testing1", &ccOpts, &new_opts, kmsProvider, mkey, &error);
65496568
ASSERT_OR_PRINT (coll, error);
65506569
bson_destroy (&ccOpts);
6570+
bson_destroy (mkey);
65516571

65526572
// Extract the encryption key ID that was generated by
65536573
// CreateEncryptedCollection:
@@ -6561,7 +6581,7 @@ test_create_encrypted_collection_insert (void *unused)
65616581
visitEach (require (type (doc)),
65626582
parse (require (key ("keyId"),
65636583
require (type (binary)),
6564-
do ({
6584+
do({
65656585
bson_value_copy (
65666586
bson_iter_value (
65676587
(bson_iter_t *) &bsonVisitIter),
@@ -6598,7 +6618,7 @@ test_create_encrypted_collection_insert (void *unused)
65986618
bson_destroy (&doc);
65996619
bson_value_destroy (&ciphertext);
66006620
bson_destroy (kmsProviders);
6601-
mongoc_client_encryption_datakey_opts_destroy (dkOpts);
6621+
bson_destroy (tlsOptions);
66026622
mongoc_collection_destroy (coll);
66036623
mongoc_database_drop (db, &error);
66046624
mongoc_database_destroy (db);

0 commit comments

Comments
 (0)