|
20 | 20 | /* _mongoc_host_list_from_string_with_err */
|
21 | 21 | #include "mongoc/mongoc-host-list-private.h"
|
22 | 22 |
|
| 23 | +/* MONGOC_SERVER_ERR_NS_NOT_FOUND */ |
| 24 | +#include "mongoc/mongoc-error-private.h" |
| 25 | + |
23 | 26 | #include "mongoc/mongoc-uri.h"
|
24 | 27 |
|
25 | 28 | static void
|
@@ -3197,6 +3200,156 @@ test_kms_tls_options_extra_rejected (void *unused)
|
3197 | 3200 | mongoc_client_destroy (keyvault_client);
|
3198 | 3201 | }
|
3199 | 3202 |
|
| 3203 | +/* ee_fixture is a fixture for the Explicit Encryption prose test. */ |
| 3204 | +typedef struct { |
| 3205 | + bson_value_t key1ID; |
| 3206 | + mongoc_client_t *keyVaultClient; |
| 3207 | + mongoc_client_encryption_t *clientEncryption; |
| 3208 | + mongoc_client_t *encryptedClient; |
| 3209 | +} ee_fixture; |
| 3210 | + |
| 3211 | +static ee_fixture * |
| 3212 | +explicit_encryption_setup (void) |
| 3213 | +{ |
| 3214 | + ee_fixture *eef = (ee_fixture *) bson_malloc0 (sizeof (ee_fixture)); |
| 3215 | + bson_t *encryptedFields = get_bson_from_json_file ( |
| 3216 | + "./src/libmongoc/tests/client_side_encryption_prose/explicit_encryption/" |
| 3217 | + "encryptedFields.json"); |
| 3218 | + bson_t *key1Document = get_bson_from_json_file ( |
| 3219 | + "./src/libmongoc/tests/client_side_encryption_prose/explicit_encryption/" |
| 3220 | + "key1-document.json"); |
| 3221 | + mongoc_client_t *setupClient = test_framework_new_default_client (); |
| 3222 | + |
| 3223 | + |
| 3224 | + /* Read the ``"_id"`` field of ``key1Document`` as ``key1ID``. */ |
| 3225 | + { |
| 3226 | + bson_iter_t iter; |
| 3227 | + const bson_value_t *value; |
| 3228 | + |
| 3229 | + ASSERT (bson_iter_init_find (&iter, key1Document, "_id")); |
| 3230 | + value = bson_iter_value (&iter); |
| 3231 | + bson_value_copy (value, &eef->key1ID); |
| 3232 | + } |
| 3233 | + |
| 3234 | + /* Drop and create the collection ``db.explicit_encryption`` using |
| 3235 | + * ``encryptedFields`` as an option. */ |
| 3236 | + { |
| 3237 | + mongoc_database_t *db = mongoc_client_get_database (setupClient, "db"); |
| 3238 | + mongoc_collection_t *coll = |
| 3239 | + mongoc_database_get_collection (db, "explicit_encryption"); |
| 3240 | + bson_error_t error; |
| 3241 | + bson_t *opts; |
| 3242 | + |
| 3243 | + opts = BCON_NEW ("encryptedFields", BCON_DOCUMENT (encryptedFields)); |
| 3244 | + |
| 3245 | + if (!mongoc_collection_drop_with_opts (coll, opts, &error)) { |
| 3246 | + if (error.code != MONGOC_SERVER_ERR_NS_NOT_FOUND) { |
| 3247 | + test_error ("unexpected error in drop: %s", error.message); |
| 3248 | + } |
| 3249 | + } |
| 3250 | + mongoc_collection_destroy (coll); |
| 3251 | + |
| 3252 | + coll = mongoc_database_create_collection ( |
| 3253 | + db, "explicit_encryption", opts, &error); |
| 3254 | + ASSERT_OR_PRINT (coll, error); |
| 3255 | + |
| 3256 | + mongoc_collection_destroy (coll); |
| 3257 | + bson_destroy (opts); |
| 3258 | + mongoc_database_destroy (db); |
| 3259 | + } |
| 3260 | + |
| 3261 | + /* Drop and create the collection ``keyvault.datakeys``. */ |
| 3262 | + { |
| 3263 | + mongoc_database_t *db = |
| 3264 | + mongoc_client_get_database (setupClient, "keyvault"); |
| 3265 | + mongoc_collection_t *coll = |
| 3266 | + mongoc_database_get_collection (db, "datakeys"); |
| 3267 | + bson_error_t error; |
| 3268 | + |
| 3269 | + if (!mongoc_collection_drop (coll, &error)) { |
| 3270 | + if (error.code != MONGOC_SERVER_ERR_NS_NOT_FOUND) { |
| 3271 | + test_error ("unexpected error in drop: %s", error.message); |
| 3272 | + } |
| 3273 | + } |
| 3274 | + mongoc_collection_destroy (coll); |
| 3275 | + |
| 3276 | + coll = mongoc_database_create_collection ( |
| 3277 | + db, "datakeys", NULL /* opts */, &error); |
| 3278 | + ASSERT_OR_PRINT (coll, error); |
| 3279 | + |
| 3280 | + mongoc_collection_destroy (coll); |
| 3281 | + mongoc_database_destroy (db); |
| 3282 | + } |
| 3283 | + |
| 3284 | + eef->keyVaultClient = test_framework_new_default_client (); |
| 3285 | + |
| 3286 | + /* Create a ClientEncryption object named ``clientEncryption`` */ |
| 3287 | + { |
| 3288 | + mongoc_client_encryption_opts_t *ceOpts = |
| 3289 | + mongoc_client_encryption_opts_new (); |
| 3290 | + bson_t *kms_providers = _make_local_kms_provider (NULL); |
| 3291 | + bson_error_t error; |
| 3292 | + |
| 3293 | + mongoc_client_encryption_opts_set_keyvault_client (ceOpts, |
| 3294 | + eef->keyVaultClient); |
| 3295 | + mongoc_client_encryption_opts_set_keyvault_namespace ( |
| 3296 | + ceOpts, "keyvault", "datakeys"); |
| 3297 | + mongoc_client_encryption_opts_set_kms_providers (ceOpts, kms_providers); |
| 3298 | + |
| 3299 | + eef->clientEncryption = mongoc_client_encryption_new (ceOpts, &error); |
| 3300 | + ASSERT_OR_PRINT (eef->clientEncryption, error); |
| 3301 | + |
| 3302 | + bson_destroy (kms_providers); |
| 3303 | + mongoc_client_encryption_opts_destroy (ceOpts); |
| 3304 | + } |
| 3305 | + |
| 3306 | + /* Create a MongoClient named ``encryptedClient``. */ |
| 3307 | + { |
| 3308 | + mongoc_auto_encryption_opts_t *aeOpts = |
| 3309 | + mongoc_auto_encryption_opts_new (); |
| 3310 | + bson_t *kms_providers = _make_local_kms_provider (NULL); |
| 3311 | + bson_error_t error; |
| 3312 | + |
| 3313 | + mongoc_auto_encryption_opts_set_keyvault_namespace ( |
| 3314 | + aeOpts, "keyvault", "datakeys"); |
| 3315 | + mongoc_auto_encryption_opts_set_kms_providers (aeOpts, kms_providers); |
| 3316 | + mongoc_auto_encryption_opts_set_bypass_query_analysis (aeOpts, true); |
| 3317 | + eef->encryptedClient = test_framework_new_default_client (); |
| 3318 | + ASSERT_OR_PRINT (mongoc_client_enable_auto_encryption ( |
| 3319 | + eef->encryptedClient, aeOpts, &error), |
| 3320 | + error); |
| 3321 | + |
| 3322 | + bson_destroy (kms_providers); |
| 3323 | + mongoc_auto_encryption_opts_destroy (aeOpts); |
| 3324 | + } |
| 3325 | + |
| 3326 | + mongoc_client_destroy (setupClient); |
| 3327 | + bson_destroy (key1Document); |
| 3328 | + bson_destroy (encryptedFields); |
| 3329 | + return eef; |
| 3330 | +} |
| 3331 | + |
| 3332 | +static void |
| 3333 | +explicit_encryption_destroy (ee_fixture *eef) |
| 3334 | +{ |
| 3335 | + if (!eef) { |
| 3336 | + return; |
| 3337 | + } |
| 3338 | + |
| 3339 | + mongoc_client_destroy (eef->encryptedClient); |
| 3340 | + mongoc_client_encryption_destroy (eef->clientEncryption); |
| 3341 | + mongoc_client_destroy (eef->keyVaultClient); |
| 3342 | + bson_value_destroy (&eef->key1ID); |
| 3343 | + bson_free (eef); |
| 3344 | +} |
| 3345 | + |
| 3346 | +static void |
| 3347 | +test_explicit_encryption (void *unused) |
| 3348 | +{ |
| 3349 | + ee_fixture *eef = explicit_encryption_setup (); |
| 3350 | + explicit_encryption_destroy (eef); |
| 3351 | +} |
| 3352 | + |
3200 | 3353 | void
|
3201 | 3354 | test_client_side_encryption_install (TestSuite *suite)
|
3202 | 3355 | {
|
@@ -3343,4 +3496,13 @@ test_client_side_encryption_install (TestSuite *suite)
|
3343 | 3496 | NULL,
|
3344 | 3497 | NULL,
|
3345 | 3498 | test_framework_skip_if_no_client_side_encryption);
|
| 3499 | + |
| 3500 | + TestSuite_AddFull (suite, |
| 3501 | + "/client_side_encryption/explicit_encryption", |
| 3502 | + test_explicit_encryption, |
| 3503 | + NULL /* dtor */, |
| 3504 | + NULL /* ctx */, |
| 3505 | + test_framework_skip_if_no_client_side_encryption, |
| 3506 | + test_framework_skip_if_max_wire_version_less_than_17, |
| 3507 | + test_framework_skip_if_single); |
3346 | 3508 | }
|
0 commit comments