Skip to content

Commit 03fcb6b

Browse files
authored
PHPC-2099: crypt_shared testing (#1333)
* Use non-breaking space in OS axis labels * Revise titles and Manager construction in autoEncryption tests * Define CSFLE_KEY_VAULT_NS and CSFLE_LOCAL_KEY constants The value for CSFLE_KEY_VAULT_NS is based on the example from PHPLIB-826. This was not required but helps makes all tests consistent and will make it easier if we need to add functionality to a helper to drop the key vault collection before a test. Using CSFLE_LOCAL_KEY allows removal of a duplicated string literal in various CSFLE tests. Although the new constant wasn't required for all tests (empty strings worked fine to satisfy option validation), using a constant helps ensure consistency across the test suite. Also use create_test_manager() in more places when basic.inc is included. The remaining instances of direct Manager construction should only be in tests where basic.inc isn't used.
1 parent dec2ed2 commit 03fcb6b

24 files changed

+191
-59
lines changed

.evergreen/config.yml

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,18 @@ functions:
466466
${DRIVERS_TOOLS}/.evergreen/run-load-balancer.sh stop
467467
fi
468468
469+
"fetch crypt_shared":
470+
- command: shell.exec
471+
params:
472+
script: |
473+
# TODO: Specify same version provisioned by download-mongodb.sh (see: DRIVERS-2355)
474+
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
475+
- command: expansions.update
476+
params:
477+
updates:
478+
- key: client_side_encryption_crypt_shared_lib_path
479+
value: ${DRIVERS_TOOLS}/.evergreen/csfle/mongo_crypt_v1.so
480+
469481
pre:
470482
- func: "fetch source"
471483
- func: "prepare resources"
@@ -585,6 +597,17 @@ tasks:
585597
vars:
586598
TESTS: "tests/atlas.phpt"
587599

600+
- name: "test-crypt_shared"
601+
commands:
602+
- func: "compile driver"
603+
- func: "bootstrap mongo-orchestration"
604+
vars:
605+
TOPOLOGY: "replica_set"
606+
- func: "fetch crypt_shared"
607+
- func: "run tests"
608+
vars:
609+
CRYPT_SHARED_LIB_PATH: "${client_side_encryption_crypt_shared_lib_path}"
610+
588611
- name: "test-loadBalanced"
589612
tags: ["loadbalanced"]
590613
commands:
@@ -1107,10 +1130,10 @@ axes:
11071130
display_name: "RHEL 7.1 Power 8"
11081131
run_on: rhel71-power8-build
11091132
- id: rhel72-zseries
1110-
display_name: "RHEL 7.2 zSeries"
1133+
display_name: "RHEL 7.2 zSeries"
11111134
run_on: rhel72-zseries-build
11121135
- id: ubuntu1804-arm64
1113-
display_name: "Ubuntu 18.04 ARM64"
1136+
display_name: "Ubuntu 18.04 ARM64"
11141137
run_on: ubuntu1804-arm64-test
11151138
# Pending installation of PHP toolchain on macOS hosts (see: PHPC-869)
11161139
# - id: macos-1014
@@ -1233,3 +1256,10 @@ buildvariants:
12331256
display_name: "Load balanced - ${mongodb-versions}"
12341257
tasks:
12351258
- name: "test-loadBalanced"
1259+
1260+
# CSFLE crypt_shared is available from MongoDB 6.0+
1261+
- matrix_name: "test-csfle-crypt_shared"
1262+
matrix_spec: { "os": "debian11", "mongodb-versions": "6.0", "php-edge-versions": "latest-stable" }
1263+
display_name: "CSFLE crypt_shared - ${mongodb-versions}"
1264+
tasks:
1265+
- name: "test-crypt_shared"

CONTRIBUTING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ The test suite references the following environment variables:
111111
which will then be specified as the `serverApi` driver option for
112112
[`MongoDB\Driver\Manager`](https://www.php.net/manual/en/class.mongodb-driver-manager.php)
113113
objects created by the test suite.
114+
* `CRYPT_SHARED_LIB_PATH`: If defined, this value will be used to set the
115+
`cryptSharedLibPath` autoEncryption driver option for
116+
[`MongoDB\Driver\Manager`](https://www.php.net/manual/en/class.mongodb-driver-manager.php)
117+
objects created by the test suite.
114118

115119
### Mongo Orchestration
116120

tests/clientEncryption/clientEncryption-createDataKey-001.phpt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ MongoDB\Driver\ClientEncryption::createDataKey()
66
<?php skip_if_not_live(); ?>
77
--FILE--
88
<?php
9-
require_once __DIR__ . "/../utils/basic.inc";
109

11-
$key = base64_decode('Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk');
10+
require_once __DIR__ . "/../utils/basic.inc";
1211

1312
$manager = create_test_manager();
14-
$clientEncryption = $manager->createClientEncryption(['keyVaultNamespace' => 'default.keys', 'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary($key, 0)]]]);
13+
14+
$clientEncryption = $manager->createClientEncryption([
15+
'keyVaultNamespace' => CSFLE_KEY_VAULT_NS,
16+
'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary(CSFLE_LOCAL_KEY, 0)]],
17+
]);
1518

1619
var_dump($clientEncryption->createDataKey('local'));
1720

tests/clientEncryption/clientEncryption-createDataKey_error-001.phpt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ MongoDB\Driver\ClientEncryption::createDataKey() with invalid keyAltNames
66
<?php skip_if_not_live(); ?>
77
--FILE--
88
<?php
9-
require_once __DIR__ . "/../utils/basic.inc";
109

11-
$key = base64_decode('Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk');
10+
require_once __DIR__ . "/../utils/basic.inc";
1211

1312
$tests = [
1413
['keyAltNames' => 'foo'],
@@ -17,7 +16,10 @@ $tests = [
1716
];
1817

1918
$manager = create_test_manager();
20-
$clientEncryption = $manager->createClientEncryption(['keyVaultNamespace' => 'default.keys', 'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary($key, 0)]]]);
19+
$clientEncryption = $manager->createClientEncryption([
20+
'keyVaultNamespace' => CSFLE_KEY_VAULT_NS,
21+
'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary(CSFLE_LOCAL_KEY, 0)]],
22+
]);
2123

2224
foreach ($tests as $opts) {
2325
echo throws(function () use ($clientEncryption, $opts) {

tests/clientEncryption/clientEncryption-ctor-001.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ MongoDB\Driver\ClientEncryption::__construct()
66
--FILE--
77
<?php
88

9-
$key = base64_decode('Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk');
9+
require_once __DIR__ . "/../utils/basic.inc";
1010

1111
$clientEncryption = new MongoDB\Driver\ClientEncryption([
12-
'keyVaultClient' => new MongoDB\Driver\Manager(),
13-
'keyVaultNamespace' => 'default.keys',
14-
'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary($key, 0)]]
12+
'keyVaultClient' => create_test_manager(),
13+
'keyVaultNamespace' => CSFLE_KEY_VAULT_NS,
14+
'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary(CSFLE_LOCAL_KEY, 0)]],
1515
]);
1616

1717
var_dump($clientEncryption);

tests/clientEncryption/clientEncryption-ctor_error-002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ $tests = [
1919
[
2020
'keyVaultNamespace' => 'not_a_namespace',
2121
// keyVaultNamespace requires a valid kmsProviders option
22-
'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary('', 0)]],
22+
'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary(CSFLE_LOCAL_KEY, 0)]],
2323
] + $baseOptions,
2424
['kmsProviders' => 'not_an_array_or_object'] + $baseOptions,
2525
['tlsOptions' => 'not_an_array_or_object'] + $baseOptions,

tests/clientEncryption/clientEncryption-decrypt-001.phpt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@ MongoDB\Driver\ClientEncryption::decrypt()
77
<?php skip_if_not_server_storage_engine('wiredTiger'); ?>
88
--FILE--
99
<?php
10-
require_once __DIR__ . "/../utils/basic.inc";
1110

12-
$key = base64_decode('Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk');
11+
require_once __DIR__ . "/../utils/basic.inc";
1312

1413
$manager = create_test_manager();
15-
$clientEncryption = $manager->createClientEncryption(['keyVaultNamespace' => 'default.keys', 'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary($key, 0)]]]);
1614

17-
$key = $clientEncryption->createDataKey('local');
15+
$clientEncryption = $manager->createClientEncryption([
16+
'keyVaultNamespace' => CSFLE_KEY_VAULT_NS,
17+
'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary(CSFLE_LOCAL_KEY, 0)]],
18+
]);
19+
20+
$keyId = $clientEncryption->createDataKey('local');
1821

19-
$encrypted = $clientEncryption->encrypt('top-secret', ['keyId' => $key, 'algorithm' => MongoDB\Driver\ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC]);
22+
$encrypted = $clientEncryption->encrypt('top-secret', ['keyId' => $keyId, 'algorithm' => MongoDB\Driver\ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC]);
2023
var_dump($clientEncryption->decrypt($encrypted));
2124

2225
?>

tests/clientEncryption/clientEncryption-encrypt-001.phpt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@ MongoDB\Driver\ClientEncryption::encrypt()
77
<?php skip_if_not_server_storage_engine('wiredTiger'); ?>
88
--FILE--
99
<?php
10-
require_once __DIR__ . "/../utils/basic.inc";
1110

12-
$key = base64_decode('Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk');
11+
require_once __DIR__ . "/../utils/basic.inc";
1312

1413
$manager = create_test_manager();
15-
$clientEncryption = $manager->createClientEncryption(['keyVaultNamespace' => 'default.keys', 'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary($key, 0)]]]);
1614

17-
$key = $clientEncryption->createDataKey('local');
15+
$clientEncryption = $manager->createClientEncryption([
16+
'keyVaultNamespace' => CSFLE_KEY_VAULT_NS,
17+
'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary(CSFLE_LOCAL_KEY, 0)]],
18+
]);
19+
20+
$keyId = $clientEncryption->createDataKey('local');
1821

19-
var_dump($clientEncryption->encrypt('top-secret', ['keyId' => $key, 'algorithm' => MongoDB\Driver\ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC]));
22+
var_dump($clientEncryption->encrypt('top-secret', ['keyId' => $keyId, 'algorithm' => MongoDB\Driver\ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC]));
2023

2124
?>
2225
===DONE===

tests/cursor/bug1529-001.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class CommandLogger implements MongoDB\Driver\Monitoring\CommandSubscriber
4646
$keyVaultClient = create_test_manager(URI, [], ['disableClientPersistence' => true]);
4747
$autoEncryptionOpts = [
4848
'keyVaultClient' => $keyVaultClient,
49-
'keyVaultNamespace' => 'default.keys',
50-
'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary(str_repeat('0', 96), 0)]],
49+
'keyVaultNamespace' => CSFLE_KEY_VAULT_NS,
50+
'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary(CSFLE_LOCAL_KEY, 0)]],
5151
];
5252

5353
$manager = create_test_manager(URI, [], ['autoEncryption' => $autoEncryptionOpts, 'disableClientPersistence' => true]);

tests/manager/manager-createClientEncryption-001.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ MongoDB\Driver\Manager::createClientEncryption()
66
--FILE--
77
<?php
88

9-
$key = base64_decode('Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk');
9+
require_once __DIR__ . '/../utils/basic.inc';
1010

11-
$manager = new MongoDB\Driver\Manager();
11+
$manager = create_test_manager();
1212

1313
$clientEncryption = $manager->createClientEncryption([
14-
'keyVaultNamespace' => 'default.keys',
15-
'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary($key, 0)]]
14+
'keyVaultNamespace' => CSFLE_KEY_VAULT_NS,
15+
'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary(CSFLE_LOCAL_KEY, 0)]]
1616
]);
1717

1818
var_dump($clientEncryption);

tests/manager/manager-createClientEncryption-error-002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ $tests = [
1313
[
1414
'keyVaultNamespace' => 'not_a_namespace',
1515
// keyVaultNamespace requires a valid kmsProviders option
16-
'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary('', 0)]],
16+
'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary(CSFLE_LOCAL_KEY, 0)]],
1717
],
1818
['kmsProviders' => 'not_an_array_or_object'],
1919
['tlsOptions' => 'not_an_array_or_object'],

tests/manager/manager-ctor-auto_encryption-001.phpt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
--TEST--
2-
MongoDB\Driver\Manager::__construct(): auto encryption options
2+
MongoDB\Driver\Manager::__construct(): autoEncryption options
33
--SKIPIF--
44
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
55
<?php skip_if_appveyor(); /* AppVeyor does not have mongocryptd installed */ ?>
66
<?php skip_if_not_libmongocrypt(); ?>
77
--FILE--
88
<?php
99

10+
require_once __DIR__ . '/../utils/basic.inc';
11+
1012
$baseOptions = [
11-
'keyVaultNamespace' => 'admin.dataKeys',
12-
'kmsProviders' => ['aws' => (object) ['accessKeyId' => 'abc', 'secretAccessKey' => 'def']]
13+
'keyVaultNamespace' => CSFLE_KEY_VAULT_NS,
14+
'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary(CSFLE_LOCAL_KEY, 0)]],
1315
];
1416

1517
$tests = [
1618
[],
1719
['bypassAutoEncryption' => true],
1820
['bypassQueryAnalysis' => true],
19-
['keyVaultClient' => new MongoDB\Driver\Manager()],
21+
['keyVaultClient' => create_test_manager()],
2022
['schemaMap' => [
2123
'default.default' => [
2224
'properties' => [
@@ -42,7 +44,7 @@ $tests = [
4244
];
4345

4446
foreach ($tests as $autoEncryptionOptions) {
45-
$manager = new MongoDB\Driver\Manager(null, [], ['autoEncryption' => $autoEncryptionOptions + $baseOptions]);
47+
create_test_manager(null, [], ['autoEncryption' => $autoEncryptionOptions + $baseOptions]);
4648
}
4749

4850
?>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::__construct(): crypt_shared is required
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_libmongocrypt(); ?>
6+
<?php skip_if_no_crypt_shared(); ?>
7+
--FILE--
8+
<?php
9+
10+
require_once __DIR__ . '/../utils/basic.inc';
11+
12+
$autoEncryptionOptions = [
13+
'keyVaultNamespace' => CSFLE_KEY_VAULT_NS,
14+
'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary(CSFLE_LOCAL_KEY, 0)]],
15+
'extraOptions' => ['cryptSharedLibRequired' => true],
16+
];
17+
18+
create_test_manager(null, [], ['autoEncryption' => $autoEncryptionOptions]);
19+
20+
?>
21+
===DONE===
22+
<?php exit(0); ?>
23+
--EXPECT--
24+
===DONE===

tests/manager/manager-ctor-auto_encryption-error-001.phpt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
--TEST--
2-
MongoDB\Driver\Manager::__construct(): incomplete auto encryption options
2+
MongoDB\Driver\Manager::__construct(): incomplete autoEncryption options
33
--SKIPIF--
44
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
55
<?php skip_if_not_libmongocrypt(); ?>
66
--FILE--
77
<?php
8-
98
require_once __DIR__ . '/../utils/basic.inc';
109

1110
$tests = [
1211
[],
13-
['keyVaultNamespace' => 'admin.keys'],
12+
['keyVaultNamespace' => CSFLE_KEY_VAULT_NS],
1413
];
1514

16-
foreach ($tests as $driverOptions) {
17-
echo throws(function() use ($driverOptions) {
18-
$manager = create_test_manager(null, [], ['autoEncryption' => $driverOptions]);
15+
foreach ($tests as $autoEncryptionOptions) {
16+
echo throws(function() use ($autoEncryptionOptions) {
17+
create_test_manager(null, [], ['autoEncryption' => $autoEncryptionOptions]);
1918
}, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n\n";
2019
}
2120

tests/manager/manager-ctor-auto_encryption-error-002.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
MongoDB\Driver\Manager::__construct(): auto encryption when compiling without libmongocrypt
2+
MongoDB\Driver\Manager::__construct(): autoEncryption when compiling without libmongocrypt
33
--SKIPIF--
44
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
55
<?php skip_if_libmongocrypt(); ?>
@@ -9,7 +9,7 @@ MongoDB\Driver\Manager::__construct(): auto encryption when compiling without li
99
require_once __DIR__ . '/../utils/basic.inc';
1010

1111
echo throws(function () {
12-
$manager = create_test_manager(null, [], ['autoEncryption' => []]);
12+
create_test_manager(null, [], ['autoEncryption' => []]);
1313
}, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n";
1414

1515
?>

tests/manager/manager-ctor-auto_encryption-error-003.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
MongoDB\Driver\Manager::__construct(): invalid option types
2+
MongoDB\Driver\Manager::__construct(): invalid types in autoEncryption options
33
--SKIPIF--
44
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
55
<?php skip_if_not_libmongocrypt(); ?>
@@ -15,17 +15,17 @@ $tests = [
1515
[
1616
'keyVaultNamespace' => 'not_a_namespace',
1717
// keyVaultNamespace requires a valid kmsProviders option
18-
'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary('', 0)]],
18+
'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary(CSFLE_LOCAL_KEY, 0)]],
1919
],
2020
['kmsProviders' => 'not_an_array_or_object'],
2121
['schemaMap' => 'not_an_array_or_object'],
2222
['tlsOptions' => 'not_an_array_or_object'],
2323
['extraOptions' => 'not_an_array_or_object'],
2424
];
2525

26-
foreach ($tests as $test) {
27-
echo throws(function() use ($test) {
28-
$manager = create_test_manager(null, [], ['autoEncryption' => $test]);
26+
foreach ($tests as $autoEncryptionOptions) {
27+
echo throws(function() use ($autoEncryptionOptions) {
28+
create_test_manager(null, [], ['autoEncryption' => $autoEncryptionOptions]);
2929
}, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n\n";
3030
}
3131

0 commit comments

Comments
 (0)