|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Tests\SpecTests\ClientSideEncryption; |
| 4 | + |
| 5 | +use MongoDB\BSON\Int64; |
| 6 | +use MongoDB\Client; |
| 7 | +use MongoDB\Driver\WriteConcern; |
| 8 | +use MongoDB\Tests\SpecTests\FunctionalTestCase as BaseFunctionalTestCase; |
| 9 | +use PHPUnit\Framework\Assert; |
| 10 | +use stdClass; |
| 11 | + |
| 12 | +use function explode; |
| 13 | +use function getenv; |
| 14 | +use function is_executable; |
| 15 | +use function is_readable; |
| 16 | +use function sprintf; |
| 17 | +use function strlen; |
| 18 | +use function unserialize; |
| 19 | + |
| 20 | +use const DIRECTORY_SEPARATOR; |
| 21 | +use const PATH_SEPARATOR; |
| 22 | + |
| 23 | +/** |
| 24 | + * Base class for client-side encryption prose tests. |
| 25 | + * |
| 26 | + * @see https://github.com/mongodb/specifications/blob/bc37892f360cab9df4082922384e0f4d4233f6d3/source/client-side-encryption/tests/README.rst |
| 27 | + */ |
| 28 | +abstract class FunctionalTestCase extends BaseFunctionalTestCase |
| 29 | +{ |
| 30 | + public const LOCAL_MASTERKEY = 'Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk'; |
| 31 | + |
| 32 | + public function setUp(): void |
| 33 | + { |
| 34 | + parent::setUp(); |
| 35 | + |
| 36 | + $this->skipIfClientSideEncryptionIsNotSupported(); |
| 37 | + |
| 38 | + if (! static::isCryptSharedLibAvailable() && ! static::isMongocryptdAvailable()) { |
| 39 | + $this->markTestSkipped('Neither crypt_shared nor mongocryptd are available'); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public static function createTestClient(?string $uri = null, array $options = [], array $driverOptions = []): Client |
| 44 | + { |
| 45 | + if (isset($driverOptions['autoEncryption']) && getenv('CRYPT_SHARED_LIB_PATH')) { |
| 46 | + $driverOptions['autoEncryption']['extraOptions']['cryptSharedLibPath'] = getenv('CRYPT_SHARED_LIB_PATH'); |
| 47 | + } |
| 48 | + |
| 49 | + return parent::createTestClient($uri, $options, $driverOptions); |
| 50 | + } |
| 51 | + |
| 52 | + protected static function getAWSCredentials(): array |
| 53 | + { |
| 54 | + return [ |
| 55 | + 'accessKeyId' => static::getEnv('AWS_ACCESS_KEY_ID'), |
| 56 | + 'secretAccessKey' => static::getEnv('AWS_SECRET_ACCESS_KEY'), |
| 57 | + ]; |
| 58 | + } |
| 59 | + |
| 60 | + protected static function insertKeyVaultData(Client $client, ?array $keyVaultData = null): void |
| 61 | + { |
| 62 | + $collection = $client->selectCollection('keyvault', 'datakeys', ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)]); |
| 63 | + $collection->drop(); |
| 64 | + |
| 65 | + if (empty($keyVaultData)) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + $collection->insertMany($keyVaultData); |
| 70 | + } |
| 71 | + |
| 72 | + private function createInt64(string $value): Int64 |
| 73 | + { |
| 74 | + $array = sprintf('a:1:{s:7:"integer";s:%d:"%s";}', strlen($value), $value); |
| 75 | + $int64 = sprintf('C:%d:"%s":%d:{%s}', strlen(Int64::class), Int64::class, strlen($array), $array); |
| 76 | + |
| 77 | + return unserialize($int64); |
| 78 | + } |
| 79 | + |
| 80 | + private function createTestCollection(?stdClass $encryptedFields = null, ?stdClass $jsonSchema = null): void |
| 81 | + { |
| 82 | + $context = $this->getContext(); |
| 83 | + $options = $context->defaultWriteOptions; |
| 84 | + |
| 85 | + if (! empty($encryptedFields)) { |
| 86 | + $options['encryptedFields'] = $encryptedFields; |
| 87 | + } |
| 88 | + |
| 89 | + if (! empty($jsonSchema)) { |
| 90 | + $options['validator'] = ['$jsonSchema' => $jsonSchema]; |
| 91 | + } |
| 92 | + |
| 93 | + $context->getDatabase()->createCollection($context->collectionName, $options); |
| 94 | + } |
| 95 | + |
| 96 | + private static function getEnv(string $name): string |
| 97 | + { |
| 98 | + $value = getenv($name); |
| 99 | + |
| 100 | + if ($value === false) { |
| 101 | + Assert::markTestSkipped(sprintf('Environment variable "%s" is not defined', $name)); |
| 102 | + } |
| 103 | + |
| 104 | + return $value; |
| 105 | + } |
| 106 | + |
| 107 | + private static function isCryptSharedLibAvailable(): bool |
| 108 | + { |
| 109 | + $cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH'); |
| 110 | + |
| 111 | + if ($cryptSharedLibPath === false) { |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + return is_readable($cryptSharedLibPath); |
| 116 | + } |
| 117 | + |
| 118 | + private static function isMongocryptdAvailable(): bool |
| 119 | + { |
| 120 | + $paths = explode(PATH_SEPARATOR, getenv("PATH")); |
| 121 | + |
| 122 | + foreach ($paths as $path) { |
| 123 | + if (is_executable($path . DIRECTORY_SEPARATOR . 'mongocryptd')) { |
| 124 | + return true; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return false; |
| 129 | + } |
| 130 | +} |
0 commit comments