From 6911d864ff750168e986a256aec9229d4dc65c0a Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Wed, 12 Jun 2024 15:15:43 -0400 Subject: [PATCH 1/4] PHPC-2401: Support QEv2 range protocol Existing RANGE_PREVIEW constants are deprecated in favor of new RANGE constants introduced in libmongoc 1.28.0 --- src/MongoDB/ClientEncryption.stub.php | 14 +++++++++++++ src/MongoDB/ClientEncryption_arginfo.h | 20 ++++++++++++++++--- .../clientEncryption-constants.phpt | 4 ++++ ...lientEncryption-encryptExpression-001.phpt | 4 ++-- ...ncryption-encryptExpression_error-002.phpt | 4 ++-- 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/MongoDB/ClientEncryption.stub.php b/src/MongoDB/ClientEncryption.stub.php index d64527f27..b39fccfbc 100644 --- a/src/MongoDB/ClientEncryption.stub.php +++ b/src/MongoDB/ClientEncryption.stub.php @@ -34,6 +34,13 @@ final class ClientEncryption public const ALGORITHM_UNINDEXED = UNKNOWN; /** + * @var string + * @cvalue MONGOC_ENCRYPT_ALGORITHM_RANGE + */ + public const ALGORITHM_RANGE = UNKNOWN; + + /** + * @deprecated * @var string * @cvalue MONGOC_ENCRYPT_ALGORITHM_RANGEPREVIEW */ @@ -46,6 +53,13 @@ final class ClientEncryption public const QUERY_TYPE_EQUALITY = UNKNOWN; /** + * @var string + * @cvalue MONGOC_ENCRYPT_QUERY_TYPE_RANGE + */ + public const QUERY_TYPE_RANGE = UNKNOWN; + + /** + * @deprecated * @var string * @cvalue MONGOC_ENCRYPT_QUERY_TYPE_RANGEPREVIEW */ diff --git a/src/MongoDB/ClientEncryption_arginfo.h b/src/MongoDB/ClientEncryption_arginfo.h index 0df229f5b..9e779d592 100644 --- a/src/MongoDB/ClientEncryption_arginfo.h +++ b/src/MongoDB/ClientEncryption_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: ec6f4a230cb96e8985f250b7d716dd86fbc5e0ea */ + * Stub hash: 6b4f3a99f4dd97b08ee22a15985f0ffc4fe34ace */ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_MongoDB_Driver_ClientEncryption___construct, 0, 0, 1) ZEND_ARG_TYPE_INFO(0, options, IS_ARRAY, 0) @@ -193,11 +193,18 @@ static zend_class_entry *register_class_MongoDB_Driver_ClientEncryption(void) zend_declare_class_constant_ex(class_entry, const_ALGORITHM_UNINDEXED_name, &const_ALGORITHM_UNINDEXED_value, ZEND_ACC_PUBLIC, NULL); zend_string_release(const_ALGORITHM_UNINDEXED_name); + zval const_ALGORITHM_RANGE_value; + zend_string *const_ALGORITHM_RANGE_value_str = zend_string_init(MONGOC_ENCRYPT_ALGORITHM_RANGE, strlen(MONGOC_ENCRYPT_ALGORITHM_RANGE), 1); + ZVAL_STR(&const_ALGORITHM_RANGE_value, const_ALGORITHM_RANGE_value_str); + zend_string *const_ALGORITHM_RANGE_name = zend_string_init_interned("ALGORITHM_RANGE", sizeof("ALGORITHM_RANGE") - 1, 1); + zend_declare_class_constant_ex(class_entry, const_ALGORITHM_RANGE_name, &const_ALGORITHM_RANGE_value, ZEND_ACC_PUBLIC, NULL); + zend_string_release(const_ALGORITHM_RANGE_name); + zval const_ALGORITHM_RANGE_PREVIEW_value; zend_string *const_ALGORITHM_RANGE_PREVIEW_value_str = zend_string_init(MONGOC_ENCRYPT_ALGORITHM_RANGEPREVIEW, strlen(MONGOC_ENCRYPT_ALGORITHM_RANGEPREVIEW), 1); ZVAL_STR(&const_ALGORITHM_RANGE_PREVIEW_value, const_ALGORITHM_RANGE_PREVIEW_value_str); zend_string *const_ALGORITHM_RANGE_PREVIEW_name = zend_string_init_interned("ALGORITHM_RANGE_PREVIEW", sizeof("ALGORITHM_RANGE_PREVIEW") - 1, 1); - zend_declare_class_constant_ex(class_entry, const_ALGORITHM_RANGE_PREVIEW_name, &const_ALGORITHM_RANGE_PREVIEW_value, ZEND_ACC_PUBLIC, NULL); + zend_declare_class_constant_ex(class_entry, const_ALGORITHM_RANGE_PREVIEW_name, &const_ALGORITHM_RANGE_PREVIEW_value, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED, NULL); zend_string_release(const_ALGORITHM_RANGE_PREVIEW_name); zval const_QUERY_TYPE_EQUALITY_value; @@ -207,11 +214,18 @@ static zend_class_entry *register_class_MongoDB_Driver_ClientEncryption(void) zend_declare_class_constant_ex(class_entry, const_QUERY_TYPE_EQUALITY_name, &const_QUERY_TYPE_EQUALITY_value, ZEND_ACC_PUBLIC, NULL); zend_string_release(const_QUERY_TYPE_EQUALITY_name); + zval const_QUERY_TYPE_RANGE_value; + zend_string *const_QUERY_TYPE_RANGE_value_str = zend_string_init(MONGOC_ENCRYPT_QUERY_TYPE_RANGE, strlen(MONGOC_ENCRYPT_QUERY_TYPE_RANGE), 1); + ZVAL_STR(&const_QUERY_TYPE_RANGE_value, const_QUERY_TYPE_RANGE_value_str); + zend_string *const_QUERY_TYPE_RANGE_name = zend_string_init_interned("QUERY_TYPE_RANGE", sizeof("QUERY_TYPE_RANGE") - 1, 1); + zend_declare_class_constant_ex(class_entry, const_QUERY_TYPE_RANGE_name, &const_QUERY_TYPE_RANGE_value, ZEND_ACC_PUBLIC, NULL); + zend_string_release(const_QUERY_TYPE_RANGE_name); + zval const_QUERY_TYPE_RANGE_PREVIEW_value; zend_string *const_QUERY_TYPE_RANGE_PREVIEW_value_str = zend_string_init(MONGOC_ENCRYPT_QUERY_TYPE_RANGEPREVIEW, strlen(MONGOC_ENCRYPT_QUERY_TYPE_RANGEPREVIEW), 1); ZVAL_STR(&const_QUERY_TYPE_RANGE_PREVIEW_value, const_QUERY_TYPE_RANGE_PREVIEW_value_str); zend_string *const_QUERY_TYPE_RANGE_PREVIEW_name = zend_string_init_interned("QUERY_TYPE_RANGE_PREVIEW", sizeof("QUERY_TYPE_RANGE_PREVIEW") - 1, 1); - zend_declare_class_constant_ex(class_entry, const_QUERY_TYPE_RANGE_PREVIEW_name, &const_QUERY_TYPE_RANGE_PREVIEW_value, ZEND_ACC_PUBLIC, NULL); + zend_declare_class_constant_ex(class_entry, const_QUERY_TYPE_RANGE_PREVIEW_name, &const_QUERY_TYPE_RANGE_PREVIEW_value, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED, NULL); zend_string_release(const_QUERY_TYPE_RANGE_PREVIEW_name); return class_entry; diff --git a/tests/clientEncryption/clientEncryption-constants.phpt b/tests/clientEncryption/clientEncryption-constants.phpt index f03d462c6..6a86d8ae5 100644 --- a/tests/clientEncryption/clientEncryption-constants.phpt +++ b/tests/clientEncryption/clientEncryption-constants.phpt @@ -7,8 +7,10 @@ var_dump(MongoDB\Driver\ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMIN var_dump(MongoDB\Driver\ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_RANDOM); var_dump(MongoDB\Driver\ClientEncryption::ALGORITHM_INDEXED); var_dump(MongoDB\Driver\ClientEncryption::ALGORITHM_UNINDEXED); +var_dump(MongoDB\Driver\ClientEncryption::ALGORITHM_RANGE); var_dump(MongoDB\Driver\ClientEncryption::ALGORITHM_RANGE_PREVIEW); var_dump(MongoDB\Driver\ClientEncryption::QUERY_TYPE_EQUALITY); +var_dump(MongoDB\Driver\ClientEncryption::QUERY_TYPE_RANGE); var_dump(MongoDB\Driver\ClientEncryption::QUERY_TYPE_RANGE_PREVIEW); ?> @@ -19,7 +21,9 @@ string(43) "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic" string(36) "AEAD_AES_256_CBC_HMAC_SHA_512-Random" string(7) "Indexed" string(9) "Unindexed" +string(5) "Range" string(12) "RangePreview" string(8) "equality" +string(5) "range" string(12) "rangePreview" ===DONE=== diff --git a/tests/clientEncryption/clientEncryption-encryptExpression-001.phpt b/tests/clientEncryption/clientEncryption-encryptExpression-001.phpt index f3d61ee49..04197f3be 100644 --- a/tests/clientEncryption/clientEncryption-encryptExpression-001.phpt +++ b/tests/clientEncryption/clientEncryption-encryptExpression-001.phpt @@ -22,8 +22,8 @@ $keyId = $clientEncryption->createDataKey('local'); $encryptOpts = [ 'keyId' => $keyId, - 'algorithm' => MongoDB\Driver\ClientEncryption::ALGORITHM_RANGE_PREVIEW, - 'queryType' => MongoDB\Driver\ClientEncryption::QUERY_TYPE_RANGE_PREVIEW, + 'algorithm' => MongoDB\Driver\ClientEncryption::ALGORITHM_RANGE, + 'queryType' => MongoDB\Driver\ClientEncryption::QUERY_TYPE_RANGE, 'contentionFactor' => 0, 'rangeOpts' => ['min' => 0, 'max' => 200, 'sparsity' => 1], ]; diff --git a/tests/clientEncryption/clientEncryption-encryptExpression_error-002.phpt b/tests/clientEncryption/clientEncryption-encryptExpression_error-002.phpt index b65da9f2a..941c5b0a2 100644 --- a/tests/clientEncryption/clientEncryption-encryptExpression_error-002.phpt +++ b/tests/clientEncryption/clientEncryption-encryptExpression_error-002.phpt @@ -22,8 +22,8 @@ $keyId = $clientEncryption->createDataKey('local'); $encryptOpts = [ 'keyId' => $keyId, - 'algorithm' => MongoDB\Driver\ClientEncryption::ALGORITHM_RANGE_PREVIEW, - 'queryType' => MongoDB\Driver\ClientEncryption::QUERY_TYPE_RANGE_PREVIEW, + 'algorithm' => MongoDB\Driver\ClientEncryption::ALGORITHM_RANGE, + 'queryType' => MongoDB\Driver\ClientEncryption::QUERY_TYPE_RANGE, 'contentionFactor' => 0, 'rangeOpts' => ['min' => 0, 'max' => 200, 'sparsity' => 1], ]; From a0bf39dd5b99108eb8ddfbc669857adb9bcfd08f Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Thu, 13 Jun 2024 11:59:15 -0400 Subject: [PATCH 2/4] Support trimFactor range option --- src/MongoDB/ClientEncryption.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/MongoDB/ClientEncryption.c b/src/MongoDB/ClientEncryption.c index ee5ef3bd7..8dc85edaa 100644 --- a/src/MongoDB/ClientEncryption.c +++ b/src/MongoDB/ClientEncryption.c @@ -850,6 +850,17 @@ static mongoc_client_encryption_encrypt_range_opts_t* phongo_clientencryption_en return opts; } + if (php_array_existsc(options, "trimFactor")) { + int64_t trimfactor = php_array_fetchc_long(options, "trimFactor"); + + if (trimfactor < 0 || trimfactor > INT32_MAX) { + phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected \"trimFactor\" range option to be a positive 32-bit integer, %" PRId64 " given", trimfactor); + goto cleanup; + } + + mongoc_client_encryption_encrypt_range_opts_set_trim_factor(opts, (int32_t) trimfactor); + } + if (php_array_existsc(options, "sparsity")) { int64_t sparsity = php_array_fetchc_long(options, "sparsity"); From 23d7f518e579b4342cfe7f2b599b27144c1af4bf Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Thu, 13 Jun 2024 12:35:37 -0400 Subject: [PATCH 3/4] Expect deprecation messages for ClientEncryption constants --- tests/clientEncryption/clientEncryption-constants.phpt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/clientEncryption/clientEncryption-constants.phpt b/tests/clientEncryption/clientEncryption-constants.phpt index 6a86d8ae5..cdbaba805 100644 --- a/tests/clientEncryption/clientEncryption-constants.phpt +++ b/tests/clientEncryption/clientEncryption-constants.phpt @@ -16,14 +16,14 @@ var_dump(MongoDB\Driver\ClientEncryption::QUERY_TYPE_RANGE_PREVIEW); ?> ===DONE=== ---EXPECT-- +--EXPECTF-- string(43) "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic" string(36) "AEAD_AES_256_CBC_HMAC_SHA_512-Random" string(7) "Indexed" string(9) "Unindexed" string(5) "Range" -string(12) "RangePreview" +%Astring(12) "RangePreview" string(8) "equality" string(5) "range" -string(12) "rangePreview" +%Astring(12) "rangePreview" ===DONE=== From ae351edcc4873d04178b44c8ce2b8e93e5039480 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Thu, 13 Jun 2024 13:17:27 -0400 Subject: [PATCH 4/4] Specify default value for trimFactor range option This is temporary until sparsity and trimFactor are made optional prior to the GA release for range indexes (PHPC-2403) --- .../clientEncryption-encryptExpression-001.phpt | 2 +- .../clientEncryption-encryptExpression_error-002.phpt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/clientEncryption/clientEncryption-encryptExpression-001.phpt b/tests/clientEncryption/clientEncryption-encryptExpression-001.phpt index 04197f3be..24246fd18 100644 --- a/tests/clientEncryption/clientEncryption-encryptExpression-001.phpt +++ b/tests/clientEncryption/clientEncryption-encryptExpression-001.phpt @@ -25,7 +25,7 @@ $encryptOpts = [ 'algorithm' => MongoDB\Driver\ClientEncryption::ALGORITHM_RANGE, 'queryType' => MongoDB\Driver\ClientEncryption::QUERY_TYPE_RANGE, 'contentionFactor' => 0, - 'rangeOpts' => ['min' => 0, 'max' => 200, 'sparsity' => 1], + 'rangeOpts' => ['min' => 0, 'max' => 200, 'sparsity' => 1, 'trimFactor' => 1], ]; $expr = [ diff --git a/tests/clientEncryption/clientEncryption-encryptExpression_error-002.phpt b/tests/clientEncryption/clientEncryption-encryptExpression_error-002.phpt index 941c5b0a2..e8029bfcd 100644 --- a/tests/clientEncryption/clientEncryption-encryptExpression_error-002.phpt +++ b/tests/clientEncryption/clientEncryption-encryptExpression_error-002.phpt @@ -25,7 +25,7 @@ $encryptOpts = [ 'algorithm' => MongoDB\Driver\ClientEncryption::ALGORITHM_RANGE, 'queryType' => MongoDB\Driver\ClientEncryption::QUERY_TYPE_RANGE, 'contentionFactor' => 0, - 'rangeOpts' => ['min' => 0, 'max' => 200, 'sparsity' => 1], + 'rangeOpts' => ['min' => 0, 'max' => 200, 'sparsity' => 1, 'trimFactor' => 1], ]; echo throws(function() use ($clientEncryption, $encryptOpts) {