Skip to content

Commit 40f748b

Browse files
kevinAlbseramongodb
andcommitted
CDRIVER-4498 return error if masterKey is set, but provider is not set (#1259)
* add CSE Prose Test 16: Case 2 * return error if `provider` is not set when `master_key` is set * Simplify assertion of provider Co-authored-by: Ezra Chung <[email protected]> * update commented test name to match specification * use empty doc for masterKey to match specification * use ASSERT_WITH_MSG * clarify in docs that current master key is used when provider is NULL --------- Co-authored-by: Ezra Chung <[email protected]>
1 parent 86e23fc commit 40f748b

File tree

4 files changed

+96
-3
lines changed

4 files changed

+96
-3
lines changed

src/libmongoc/doc/mongoc_client_encryption_rewrap_many_datakey.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ A ``NULL`` argument for ``filter`` is equivalent to being given an empty
2424
document (match all).
2525

2626
If ``provider`` is ``NULL``, rewraps matching data keys with their current KMS
27-
provider.
27+
provider and master key.
2828

2929
If ``provider`` is not ``NULL``, rewraps matching data keys with the new KMS
3030
provider as described by ``master_key``. The ``master_key`` document must

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

+9
Original file line numberDiff line numberDiff line change
@@ -2085,6 +2085,15 @@ mongoc_client_encryption_rewrap_many_datakey (
20852085

20862086
bson_reinit (bulk_write_result);
20872087

2088+
if (master_key && !provider) {
2089+
bson_set_error (
2090+
error,
2091+
MONGOC_ERROR_CLIENT,
2092+
MONGOC_ERROR_CLIENT_INVALID_ENCRYPTION_ARG,
2093+
"expected 'provider' to be set to identify type of 'master_key'");
2094+
GOTO (fail);
2095+
}
2096+
20882097
if (!_mongoc_crypt_rewrap_many_datakey (client_encryption->crypt,
20892098
client_encryption->keyvault_coll,
20902099
filter,

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

+3
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,9 @@ _mongoc_crypt_rewrap_many_datakey (_mongoc_crypt_t *crypt,
15841584
mongocrypt_binary_t *filter_bin = NULL;
15851585
bool ret = false;
15861586

1587+
// Caller must ensure `provider` is provided alongside `master_key`.
1588+
BSON_ASSERT (!master_key || provider);
1589+
15871590
bson_init (doc_out);
15881591
state_machine = _state_machine_new (crypt);
15891592
state_machine->keyvault_coll = keyvault_coll;

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

+83-2
Original file line numberDiff line numberDiff line change
@@ -4910,7 +4910,7 @@ _test_rewrap_with_separate_client_encryption (const char *src_provider,
49104910
mongoc_client_destroy (src_client);
49114911
}
49124912

4913-
/* Prose Test 16: Rewrap with separate ClientEncryption */
4913+
/* Prose Test 16: Case 1: Rewrap with separate ClientEncryption */
49144914
static void
49154915
test_rewrap_with_separate_client_encryption (void *unused)
49164916
{
@@ -4934,6 +4934,80 @@ test_rewrap_with_separate_client_encryption (void *unused)
49344934
}
49354935
}
49364936

4937+
/* Prose Test 16: Case 2: RewrapManyDataKeyOpts.provider is not optional. */
4938+
static void
4939+
test_rewrap_without_provider (void *unused)
4940+
{
4941+
BSON_UNUSED (unused);
4942+
4943+
mongoc_uri_t *const uri = test_framework_get_uri ();
4944+
mongoc_client_encryption_opts_t *const ce_opts =
4945+
mongoc_client_encryption_opts_new ();
4946+
mongoc_client_t *const key_vault_client =
4947+
test_framework_client_new_from_uri (uri, NULL);
4948+
4949+
bson_error_t error = {0};
4950+
4951+
BSON_ASSERT (uri);
4952+
BSON_ASSERT (ce_opts);
4953+
BSON_ASSERT (key_vault_client);
4954+
4955+
test_framework_set_ssl_opts (key_vault_client);
4956+
4957+
{
4958+
mongoc_client_encryption_opts_set_keyvault_client (ce_opts,
4959+
key_vault_client);
4960+
mongoc_client_encryption_opts_set_keyvault_namespace (
4961+
ce_opts, "keyvault", "datakeys");
4962+
4963+
{
4964+
bson_t *const kms_providers = _make_kms_providers (true, true);
4965+
BSON_ASSERT (kms_providers);
4966+
mongoc_client_encryption_opts_set_kms_providers (ce_opts,
4967+
kms_providers);
4968+
bson_destroy (kms_providers);
4969+
}
4970+
4971+
{
4972+
bson_t *const tls_opts = _make_tls_opts ();
4973+
BSON_ASSERT (tls_opts);
4974+
mongoc_client_encryption_opts_set_tls_opts (ce_opts, tls_opts);
4975+
bson_destroy (tls_opts);
4976+
}
4977+
}
4978+
4979+
// 1. Create a ClientEncryption object named clientEncryption with these
4980+
// options: (see ce_opts).
4981+
mongoc_client_encryption_t *clientEncryption =
4982+
mongoc_client_encryption_new (ce_opts, &error);
4983+
ASSERT_OR_PRINT (clientEncryption, error);
4984+
4985+
// 2. Call ``clientEncryption.rewrapManyDataKey`` with an empty ``filter``
4986+
// and these options: (see below).
4987+
{
4988+
bool ok =
4989+
mongoc_client_encryption_rewrap_many_datakey (clientEncryption,
4990+
NULL /* filter */,
4991+
NULL /* kms_provider */,
4992+
tmp_bson ("{}"),
4993+
NULL /* result */,
4994+
&error);
4995+
// Assert an error is returned from the driver suggesting that the
4996+
// ``provider`` option is required.
4997+
ASSERT_WITH_MSG (!ok, "expected error, but got success");
4998+
ASSERT_ERROR_CONTAINS (
4999+
error,
5000+
MONGOC_ERROR_CLIENT,
5001+
MONGOC_ERROR_CLIENT_INVALID_ENCRYPTION_ARG,
5002+
"expected 'provider' to be set to identify type of 'master_key'");
5003+
}
5004+
5005+
mongoc_client_encryption_destroy (clientEncryption);
5006+
mongoc_client_encryption_opts_destroy (ce_opts);
5007+
mongoc_uri_destroy (uri);
5008+
mongoc_client_destroy (key_vault_client);
5009+
}
5010+
49375011
/* test_qe_docs_example tests the documentation example requested in
49385012
* CDRIVER-4379. */
49395013
static void
@@ -5534,13 +5608,20 @@ test_client_side_encryption_install (TestSuite *suite)
55345608
test_framework_skip_if_no_client_side_encryption,
55355609
test_framework_skip_if_max_wire_version_less_than_8);
55365610
TestSuite_AddFull (suite,
5537-
"/client_side_encryption/prose_test_16",
5611+
"/client_side_encryption/prose_test_16/case1",
55385612
test_rewrap_with_separate_client_encryption,
55395613
NULL,
55405614
NULL,
55415615
test_framework_skip_if_no_client_side_encryption,
55425616
test_framework_skip_if_max_wire_version_less_than_8,
55435617
test_framework_skip_if_slow);
5618+
TestSuite_AddFull (suite,
5619+
"/client_side_encryption/prose_test_16/case2",
5620+
test_rewrap_without_provider,
5621+
NULL,
5622+
NULL,
5623+
test_framework_skip_if_no_client_side_encryption,
5624+
test_framework_skip_if_max_wire_version_less_than_8);
55445625

55455626
/* Other, C driver specific, tests. */
55465627
TestSuite_AddFull (suite,

0 commit comments

Comments
 (0)