Skip to content

Commit e200808

Browse files
Use existing KMS conveniences
1 parent 4e098c0 commit e200808

File tree

1 file changed

+30
-53
lines changed

1 file changed

+30
-53
lines changed

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

Lines changed: 30 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6244,18 +6244,11 @@ test_auto_datakeys (void *unused)
62446244
}
62456245

62466246
static void
6247-
_do_cec_test (void (*test) (const char *kmsProvider, const bson_t *masterKey))
6247+
_do_cec_test (void (*test) (const char *kmsProvider))
62486248
{
62496249
// Run the test using the "local" key:
6250-
test ("local", NULL);
6251-
// Run the test using an AWS key:
6252-
bsonBuildDecl (masterKey,
6253-
kv ("region", cstr ("us-east-1")),
6254-
kv ("key",
6255-
cstr ("arn:aws:kms:us-east-1:579766882180:key/"
6256-
"89fcc2c4-08b0-4bd9-9f25-e30687b580d0")));
6257-
test ("aws", &masterKey);
6258-
bson_destroy (&masterKey);
6250+
test ("local");
6251+
test ("aws");
62596252
}
62606253

62616254
// Declare a createEncryptedCollection test case (See usage below)
@@ -6268,13 +6261,11 @@ _do_cec_test (void (*test) (const char *kmsProvider, const bson_t *masterKey))
62686261
} \
62696262
static void name##_impl (__VA_ARGS__)
62706263

6271-
CEC_TEST (test_create_encrypted_collection_simple,
6272-
const char *kmsProvider,
6273-
const bson_t *opt_masterKey)
6264+
CEC_TEST (test_create_encrypted_collection_simple, const char *kmsProvider)
62746265
{
62756266
bson_error_t error = {0};
62766267
mongoc_client_t *const client = test_framework_new_default_client ();
6277-
bson_t *const kmsProviders = _make_kms_providers (false, true);
6268+
bson_t *const kmsProviders = _make_kms_providers (true, true);
62786269

62796270
const char *const dbName = "cec-test-db";
62806271

@@ -6315,17 +6306,13 @@ CEC_TEST (test_create_encrypted_collection_simple,
63156306
kv ("bsonType", cstr ("string")),
63166307
kv ("keyId", null)))))));
63176308
mongoc_database_t *const db = mongoc_client_get_database (client, dbName);
6309+
bson_t *const mkey = _make_kms_masterkey (kmsProvider);
63186310
mongoc_collection_t *const coll =
6319-
mongoc_client_encryption_create_encrypted_collection (ce,
6320-
db,
6321-
"test-coll",
6322-
&ccOpts,
6323-
NULL,
6324-
kmsProvider,
6325-
opt_masterKey,
6326-
&error);
6311+
mongoc_client_encryption_create_encrypted_collection (
6312+
ce, db, "test-coll", &ccOpts, NULL, kmsProvider, mkey, &error);
63276313
ASSERT_OR_PRINT (coll, error);
63286314
bson_destroy (&ccOpts);
6315+
bson_destroy (mkey);
63296316

63306317
bsonBuildDecl (doc, kv ("ssn", cstr ("123-45-6789")));
63316318
const bool okay =
@@ -6350,11 +6337,10 @@ test_create_encrypted_collection_no_encryptedFields_helper (
63506337
mongoc_client_t *client,
63516338
const char *dbName,
63526339
const char *collName,
6353-
const char *kmsProvider,
6354-
const bson_t *const opt_masterKey)
6340+
const char *kmsProvider)
63556341
{
63566342
bson_error_t error = {0};
6357-
bson_t *const kmsProviders = _make_kms_providers (false, true);
6343+
bson_t *const kmsProviders = _make_kms_providers (true, true);
63586344

63596345
// Drop prior data
63606346
{
@@ -6388,14 +6374,16 @@ test_create_encrypted_collection_no_encryptedFields_helper (
63886374
// Create the encrypted collection
63896375
bsonBuildDecl (ccOpts, do());
63906376
mongoc_database_t *const db = mongoc_client_get_database (client, dbName);
6377+
bson_t *const mkey = _make_kms_masterkey (kmsProvider);
63916378
mongoc_collection_t *const coll =
63926379
mongoc_client_encryption_create_encrypted_collection (
6393-
ce, db, collName, &ccOpts, NULL, kmsProvider, opt_masterKey, &error);
6380+
ce, db, collName, &ccOpts, NULL, kmsProvider, mkey, &error);
63946381
ASSERT_ERROR_CONTAINS (error,
63956382
MONGOC_ERROR_COMMAND,
63966383
MONGOC_ERROR_COMMAND_INVALID_ARG,
63976384
"No 'encryptedFields' are defined");
63986385
bson_destroy (&ccOpts);
6386+
bson_destroy (mkey);
63996387

64006388
bson_destroy (kmsProviders);
64016389
mongoc_collection_destroy (coll);
@@ -6405,8 +6393,7 @@ test_create_encrypted_collection_no_encryptedFields_helper (
64056393
}
64066394

64076395
CEC_TEST (test_create_encrypted_collection_no_encryptedFields,
6408-
const char *kmsProvider,
6409-
const bson_t *const opt_masterKey)
6396+
const char *kmsProvider)
64106397
{
64116398
const char *dbName = "cec-test-db";
64126399
const char *collName = "test-coll";
@@ -6415,7 +6402,7 @@ CEC_TEST (test_create_encrypted_collection_no_encryptedFields,
64156402
{
64166403
mongoc_client_t *const client = test_framework_new_default_client ();
64176404
test_create_encrypted_collection_no_encryptedFields_helper (
6418-
client, dbName, collName, kmsProvider, opt_masterKey);
6405+
client, dbName, collName, kmsProvider);
64196406
mongoc_client_destroy (client);
64206407
}
64216408

@@ -6427,7 +6414,7 @@ CEC_TEST (test_create_encrypted_collection_no_encryptedFields,
64276414
mongoc_auto_encryption_opts_t *aeOpts =
64286415
mongoc_auto_encryption_opts_new ();
64296416
bson_t *const kmsProviders =
6430-
_make_kms_providers (false /* with aws */, true /* with local */);
6417+
_make_kms_providers (true /* with aws */, true /* with local */);
64316418
char *namespace = bson_strdup_printf ("%s.%s", dbName, collName);
64326419
bson_t *encryptedFieldsMap =
64336420
tmp_bson ("{'%s': {'fields': []}}", namespace);
@@ -6442,7 +6429,7 @@ CEC_TEST (test_create_encrypted_collection_no_encryptedFields,
64426429
mongoc_client_enable_auto_encryption (client, aeOpts, &error), error);
64436430

64446431
test_create_encrypted_collection_no_encryptedFields_helper (
6445-
client, dbName, collName, kmsProvider, opt_masterKey);
6432+
client, dbName, collName, kmsProvider);
64466433

64476434
bson_free (namespace);
64486435
bson_destroy (kmsProviders);
@@ -6452,12 +6439,11 @@ CEC_TEST (test_create_encrypted_collection_no_encryptedFields,
64526439
}
64536440

64546441
CEC_TEST (test_create_encrypted_collection_bad_keyId,
6455-
const char *const kmsProvider,
6456-
const bson_t *const opt_masterKey)
6442+
const char *const kmsProvider)
64576443
{
64586444
bson_error_t error = {0};
64596445
mongoc_client_t *const client = test_framework_new_default_client ();
6460-
bson_t *const kmsProviders = _make_kms_providers (false, true);
6446+
bson_t *const kmsProviders = _make_kms_providers (true, true);
64616447

64626448
const char *const dbName = "cec-test-db";
64636449

@@ -6498,20 +6484,16 @@ CEC_TEST (test_create_encrypted_collection_bad_keyId,
64986484
kv ("bsonType", cstr ("string")),
64996485
kv ("keyId", bool (true))))))));
65006486
mongoc_database_t *const db = mongoc_client_get_database (client, dbName);
6487+
bson_t *const mkey = _make_kms_masterkey (kmsProvider);
65016488
mongoc_collection_t *const coll =
6502-
mongoc_client_encryption_create_encrypted_collection (ce,
6503-
db,
6504-
"test-coll",
6505-
&ccOpts,
6506-
NULL,
6507-
kmsProvider,
6508-
opt_masterKey,
6509-
&error);
6489+
mongoc_client_encryption_create_encrypted_collection (
6490+
ce, db, "test-coll", &ccOpts, NULL, kmsProvider, mkey, &error);
65106491
ASSERT_ERROR_CONTAINS (error,
65116492
MONGOC_ERROR_QUERY,
65126493
MONGOC_ERROR_PROTOCOL_INVALID_REPLY,
65136494
"create.encryptedFields.fields.keyId");
65146495
bson_destroy (&ccOpts);
6496+
bson_destroy (mkey);
65156497

65166498
bson_destroy (kmsProviders);
65176499
mongoc_collection_destroy (coll);
@@ -6523,12 +6505,11 @@ CEC_TEST (test_create_encrypted_collection_bad_keyId,
65236505

65246506
// Implements Prose Test 21. Case: 4.
65256507
CEC_TEST (test_create_encrypted_collection_insert,
6526-
const char *const kmsProvider,
6527-
const bson_t *const opt_masterKey)
6508+
const char *const kmsProvider)
65286509
{
65296510
bson_error_t error = {0};
65306511
mongoc_client_t *const client = test_framework_new_default_client ();
6531-
bson_t *const kmsProviders = _make_kms_providers (false, true);
6512+
bson_t *const kmsProviders = _make_kms_providers (true, true);
65326513

65336514
const char *const dbName = "cec-test-db";
65346515

@@ -6570,17 +6551,13 @@ CEC_TEST (test_create_encrypted_collection_insert,
65706551
kv ("keyId", null)))))));
65716552
mongoc_database_t *const db = mongoc_client_get_database (client, dbName);
65726553
bson_t new_opts;
6554+
bson_t *const mkey = _make_kms_masterkey (kmsProvider);
65736555
mongoc_collection_t *const coll =
6574-
mongoc_client_encryption_create_encrypted_collection (ce,
6575-
db,
6576-
"testing1",
6577-
&ccOpts,
6578-
&new_opts,
6579-
kmsProvider,
6580-
opt_masterKey,
6581-
&error);
6556+
mongoc_client_encryption_create_encrypted_collection (
6557+
ce, db, "testing1", &ccOpts, &new_opts, kmsProvider, mkey, &error);
65826558
ASSERT_OR_PRINT (coll, error);
65836559
bson_destroy (&ccOpts);
6560+
bson_destroy (mkey);
65846561

65856562
// Extract the encryption key ID that was generated by
65866563
// CreateEncryptedCollection:

0 commit comments

Comments
 (0)