Skip to content

Commit 265810a

Browse files
authored
PHPLIB-891: Run CSFLE tests with crypt_shared (#943)
* Ensure TESTS var is passed through to run-tests.sh This likely dates back to d1b18dd
1 parent 809223b commit 265810a

File tree

4 files changed

+90
-5
lines changed

4 files changed

+90
-5
lines changed

.evergreen/config.yml

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,9 @@ functions:
246246
export KMS_ENDPOINT_REQUIRE_CLIENT_CERT="${client_side_encryption_kms_endpoint_require_client_cert}"
247247
export KMS_TLS_CA_FILE="${client_side_encryption_kms_tls_ca_file}"
248248
export KMS_TLS_CERTIFICATE_KEY_FILE="${client_side_encryption_kms_tls_certificate_key_file}"
249+
export CRYPT_SHARED_LIB_PATH="${client_side_encryption_crypt_shared_lib_path}"
249250
export PATH="${PHP_PATH}/bin:$PATH"
250-
API_VERSION=${API_VERSION} PHP_VERSION=${PHP_VERSION} AUTH=${AUTH} SSL=${SSL} MONGODB_URI="${MONGODB_URI}" sh ${PROJECT_DIRECTORY}/.evergreen/run-tests.sh
251+
API_VERSION=${API_VERSION} PHP_VERSION=${PHP_VERSION} TESTS=${TESTS} AUTH=${AUTH} SSL=${SSL} MONGODB_URI="${MONGODB_URI}" sh ${PROJECT_DIRECTORY}/.evergreen/run-tests.sh
251252
252253
"run atlas data lake test":
253254
- command: shell.exec
@@ -377,6 +378,18 @@ functions:
377378
- key: client_side_encryption_kmip_endpoint
378379
value: localhost:5698
379380

381+
"fetch crypt_shared":
382+
- command: shell.exec
383+
params:
384+
script: |
385+
# TODO: Specify same version provisioned by download-mongodb.sh (see: DRIVERS-2355)
386+
python3 ${DRIVERS_TOOLS}/.evergreen/mongodl.py --component crypt_shared --version latest --only "**/mongo_crypt_v1.so" --out ${DRIVERS_TOOLS}/.evergreen/csfle --strip-path-components 1
387+
- command: expansions.update
388+
params:
389+
updates:
390+
- key: client_side_encryption_crypt_shared_lib_path
391+
value: ${DRIVERS_TOOLS}/.evergreen/csfle/mongo_crypt_v1.so
392+
380393
pre:
381394
- func: "fetch source"
382395
- func: "prepare resources"
@@ -507,6 +520,18 @@ tasks:
507520
MONGODB_URI: "${SINGLE_MONGOS_LB_URI}"
508521
SSL: "yes"
509522
# Note: "stop load balancer" will be called from "post"
523+
524+
- name: "test-crypt_shared"
525+
commands:
526+
- func: "bootstrap mongo-orchestration"
527+
vars:
528+
TOPOLOGY: "replica_set"
529+
- func: "start kms servers"
530+
- func: "fetch crypt_shared"
531+
- func: "run tests"
532+
vars:
533+
TESTS: "csfle"
534+
510535
# }}}
511536

512537

@@ -766,3 +791,10 @@ buildvariants:
766791
display_name: "Load balanced - ${mongodb-versions}"
767792
tasks:
768793
- name: "test-loadBalanced"
794+
795+
# CSFLE crypt_shared is available from MongoDB 6.0+
796+
- matrix_name: "test-csfle-crypt_shared"
797+
matrix_spec: { "os": "debian11", "mongodb-versions": "6.0", "php-edge-versions": "latest-stable", "driver-versions": "latest-stable" }
798+
display_name: "CSFLE crypt_shared - ${mongodb-versions}"
799+
tasks:
800+
- name: "test-crypt_shared"

.evergreen/run-tests.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ case "$TESTS" in
6767
php vendor/bin/simple-phpunit --configuration phpunit.evergreen.xml --testsuite "Atlas Data Lake Test Suite" $PHPUNIT_OPTS
6868
;;
6969

70+
csfle)
71+
php vendor/bin/simple-phpunit --configuration phpunit.evergreen.xml --group csfle $PHPUNIT_OPTS
72+
;;
73+
7074
versioned-api)
7175
php vendor/bin/simple-phpunit --configuration phpunit.evergreen.xml --group versioned-api $PHPUNIT_OPTS
7276
;;

tests/SpecTests/ClientSideEncryptionSpecTest.php

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use function glob;
3232
use function in_array;
3333
use function is_executable;
34+
use function is_readable;
3435
use function iterator_to_array;
3536
use function json_decode;
3637
use function sprintf;
@@ -46,6 +47,7 @@
4647
* Client-side encryption spec tests.
4748
*
4849
* @see https://github.com/mongodb/specifications/tree/master/source/client-side-encryption
50+
* @group csfle
4951
*/
5052
class ClientSideEncryptionSpecTest extends FunctionalTestCase
5153
{
@@ -65,7 +67,10 @@ public function setUp(): void
6567
parent::setUp();
6668

6769
$this->skipIfClientSideEncryptionIsNotSupported();
68-
$this->skipIfLocalMongocryptdIsUnavailable();
70+
71+
if (! static::isCryptSharedLibAvailable() && ! static::isMongocryptdAvailable()) {
72+
$this->markTestSkipped('Neither crypt_shared nor mongocryptd are available');
73+
}
6974
}
7075

7176
/**
@@ -79,6 +84,15 @@ public static function assertCommandMatches(stdClass $expected, stdClass $actual
7984
static::assertDocumentsMatch($expected, $actual);
8085
}
8186

87+
public static function createTestClient(?string $uri = null, array $options = [], array $driverOptions = []): Client
88+
{
89+
if (isset($driverOptions['autoEncryption']) && getenv('CRYPT_SHARED_LIB_PATH')) {
90+
$driverOptions['autoEncryption']['extraOptions']['cryptSharedLibPath'] = getenv('CRYPT_SHARED_LIB_PATH');
91+
}
92+
93+
return parent::createTestClient($uri, $options, $driverOptions);
94+
}
95+
8296
/**
8397
* Execute an individual test case from the specification.
8498
*
@@ -825,6 +839,14 @@ static function (self $test, ClientEncryption $clientEncryption, ClientEncryptio
825839
*/
826840
public function testBypassSpawningMongocryptdViaBypassSpawn(): void
827841
{
842+
/* If crypt_shared is available it will likely already have been loaded
843+
* by a previous test so there is no way to prevent it from being used.
844+
* Since CSFLE prefers crypt_shared to mongocryptd there is reason to
845+
* run any of the "bypass spawning" tests (see also: MONGOCRYPT-421). */
846+
if (static::isCryptSharedLibAvailable()) {
847+
$this->markTestSkipped('Bypass spawning of mongocryptd cannot be tested when crypt_shared is available');
848+
}
849+
828850
$autoEncryptionOpts = [
829851
'keyVaultNamespace' => 'keyvault.datakeys',
830852
'kmsProviders' => [
@@ -840,6 +862,7 @@ public function testBypassSpawningMongocryptdViaBypassSpawn(): void
840862
],
841863
];
842864

865+
// Disable adding cryptSharedLibPath, as it may interfere with this test
843866
$clientEncrypted = static::createTestClient(null, [], ['autoEncryption' => $autoEncryptionOpts]);
844867

845868
try {
@@ -860,6 +883,10 @@ public function testBypassSpawningMongocryptdViaBypassSpawn(): void
860883
*/
861884
public function testBypassSpawningMongocryptdViaBypassAutoEncryption(): void
862885
{
886+
if (static::isCryptSharedLibAvailable()) {
887+
$this->markTestSkipped('Bypass spawning of mongocryptd cannot be tested when crypt_shared is available');
888+
}
889+
863890
$autoEncryptionOpts = [
864891
'keyVaultNamespace' => 'keyvault.datakeys',
865892
'kmsProviders' => [
@@ -871,6 +898,7 @@ public function testBypassSpawningMongocryptdViaBypassAutoEncryption(): void
871898
],
872899
];
873900

901+
// Disable adding cryptSharedLibPath, as it may interfere with this test
874902
$clientEncrypted = static::createTestClient(null, [], ['autoEncryption' => $autoEncryptionOpts]);
875903

876904
$clientEncrypted->selectCollection('db', 'coll')->insertOne(['unencrypted' => 'test']);
@@ -888,6 +916,10 @@ public function testBypassSpawningMongocryptdViaBypassAutoEncryption(): void
888916
*/
889917
public function testBypassSpawningMongocryptdViaBypassQueryAnalysis(): void
890918
{
919+
if (static::isCryptSharedLibAvailable()) {
920+
$this->markTestSkipped('Bypass spawning of mongocryptd cannot be tested when crypt_shared is available');
921+
}
922+
891923
$autoEncryptionOpts = [
892924
'keyVaultNamespace' => 'keyvault.datakeys',
893925
'kmsProviders' => [
@@ -899,6 +931,7 @@ public function testBypassSpawningMongocryptdViaBypassQueryAnalysis(): void
899931
],
900932
];
901933

934+
// Disable adding cryptSharedLibPath, as it may interfere with this test
902935
$clientEncrypted = static::createTestClient(null, [], ['autoEncryption' => $autoEncryptionOpts]);
903936

904937
$clientEncrypted->selectCollection('db', 'coll')->insertOne(['unencrypted' => 'test']);
@@ -1525,16 +1558,27 @@ private function prepareEncryptedFieldsMap(stdClass $encryptedFieldsMap): stdCla
15251558
return $encryptedFieldsMap;
15261559
}
15271560

1528-
private function skipIfLocalMongocryptdIsUnavailable(): void
1561+
private static function isCryptSharedLibAvailable(): bool
1562+
{
1563+
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');
1564+
1565+
if ($cryptSharedLibPath === false) {
1566+
return false;
1567+
}
1568+
1569+
return is_readable($cryptSharedLibPath);
1570+
}
1571+
1572+
private static function isMongocryptdAvailable(): bool
15291573
{
15301574
$paths = explode(PATH_SEPARATOR, getenv("PATH"));
15311575

15321576
foreach ($paths as $path) {
15331577
if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) {
1534-
return;
1578+
return true;
15351579
}
15361580
}
15371581

1538-
$this->markTestSkipped('Mongocryptd is not available on the localhost');
1582+
return false;
15391583
}
15401584
}

tests/SpecTests/Context.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ public static function fromClientSideEncryption(stdClass $test, $databaseName, $
107107

108108
$autoEncryptionOptions['tlsOptions']->kmip = self::getKmsTlsOptions();
109109
}
110+
111+
// Intentionally ignore empty values for CRYPT_SHARED_LIB_PATH
112+
if (getenv('CRYPT_SHARED_LIB_PATH')) {
113+
$autoEncryptionOptions['extraOptions']['cryptSharedLibPath'] = getenv('CRYPT_SHARED_LIB_PATH');
114+
}
110115
}
111116

112117
if (isset($test->outcome->collection->name)) {

0 commit comments

Comments
 (0)