-
-
Notifications
You must be signed in to change notification settings - Fork 515
[Encryption] Set master key when creating an encrypted collection #2780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
GromNaN
merged 8 commits into
doctrine:feature/queryable-encryption
from
GromNaN:master-key
Jun 13, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bc343a4
Set master key when creating an encrypted collection
GromNaN ab9731e
Accept multiple KMS providers if the "kmsProvider" option is set
GromNaN e0ac1ef
Check if the kmsProvider is in the list of kmsProviders
GromNaN 0203523
Cleanup autoEncryption options in driver options
GromNaN 8f9cfc8
Unsealed AutoEncryptionOptions array shape
GromNaN ba240da
Split MasterKey and KmsProvider options to a distinct config
GromNaN dd4d297
Convert exceptions to static methods
GromNaN 5e9bac2
Use kmsProvider "type" instead of "name"
GromNaN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,8 @@ | |
| use InvalidArgumentException; | ||
| use Jean85\PrettyVersions; | ||
| use LogicException; | ||
| use MongoDB\Client; | ||
| use MongoDB\Driver\Manager; | ||
| use MongoDB\Driver\WriteConcern; | ||
| use ProxyManager\Configuration as ProxyManagerConfiguration; | ||
| use ProxyManager\Factory\LazyLoadingGhostFactory; | ||
|
|
@@ -35,12 +37,11 @@ | |
| use ReflectionClass; | ||
| use Throwable; | ||
|
|
||
| use function array_diff_key; | ||
| use function array_intersect_key; | ||
| use function array_key_exists; | ||
| use function array_key_first; | ||
| use function class_exists; | ||
| use function count; | ||
| use function interface_exists; | ||
| use function is_array; | ||
| use function is_string; | ||
| use function trigger_deprecation; | ||
| use function trim; | ||
|
|
@@ -56,11 +57,7 @@ | |
| * $dm = DocumentManager::create(new Connection(), $config); | ||
| * | ||
| * @phpstan-import-type CommitOptions from UnitOfWork | ||
| * @phpstan-type AutoEncryptionOptions array{ | ||
| * keyVaultNamespace: string, | ||
| * kmsProviders: array<string, array<string, string>>, | ||
| * tlsOptions?: array{kmip: array{tlsCAFile: string, tlsCertificateKeyFile: string}}, | ||
| * } | ||
| * @phpstan-type KmsProvider array{type: string, ...} | ||
| */ | ||
| class Configuration | ||
| { | ||
|
|
@@ -133,7 +130,9 @@ class Configuration | |
| * proxyDir?: string, | ||
| * proxyNamespace?: string, | ||
| * repositoryFactory?: RepositoryFactory, | ||
| * autoEncryption?: AutoEncryptionOptions, | ||
| * kmsProvider?: KmsProvider, | ||
| * defaultMasterKey?: array<string, mixed>|null, | ||
| * autoEncryption?: array<string, mixed>, | ||
| * } | ||
| */ | ||
| private array $attributes = []; | ||
|
|
@@ -163,13 +162,34 @@ public function getDriverOptions(): array | |
| ], | ||
| ]; | ||
|
|
||
| if (isset($this->attributes['autoEncryption'])) { | ||
| $driverOptions['autoEncryption'] = $this->attributes['autoEncryption']; | ||
| if (isset($this->attributes['kmsProvider'])) { | ||
| $driverOptions['autoEncryption'] = $this->getAutoEncryptionOptions(); | ||
| } | ||
|
|
||
| return $driverOptions; | ||
| } | ||
|
|
||
| /** | ||
| * Get options to create a ClientEncryption instance. | ||
| * | ||
| * @see https://www.php.net/manual/en/mongodb-driver-clientencryption.construct.php | ||
| * | ||
| * @return array{keyVaultClient?: Client|Manager, keyVaultNamespace: string, kmsProviders: array<string, mixed>, tlsOptions?: array<string, mixed>} | ||
| */ | ||
| public function getClientEncryptionOptions(): array | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| { | ||
| if (! isset($this->attributes['kmsProvider'])) { | ||
| throw ConfigurationException::clientEncryptionOptionsNotSet(); | ||
| } | ||
|
|
||
| return array_intersect_key($this->getAutoEncryptionOptions(), [ | ||
| 'keyVaultClient' => 1, | ||
| 'keyVaultNamespace' => 1, | ||
| 'kmsProviders' => 1, | ||
| 'tlsOptions' => 1, | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * Adds a namespace under a certain alias. | ||
| */ | ||
|
|
@@ -688,47 +708,72 @@ public function isLazyGhostObjectEnabled(): bool | |
| } | ||
|
|
||
| /** | ||
| * Set the options for auto-encryption. | ||
| * Set the KMS provider to use for auto-encryption. The name of the KMS provider | ||
| * must be specified in the 'type' key of the array. | ||
| * | ||
| * @see https://www.php.net/manual/en/mongodb-driver-clientencryption.construct.php | ||
| * | ||
| * @phpstan-param AutoEncryptionOptions $options | ||
| * | ||
| * @throws InvalidArgumentException If the options are invalid. | ||
| * @param KmsProvider $kmsProvider | ||
| */ | ||
| public function setAutoEncryption(array $options): void | ||
| public function setKmsProvider(array $kmsProvider): void | ||
| { | ||
| if (! isset($options['keyVaultNamespace']) || ! is_string($options['keyVaultNamespace'])) { | ||
| throw new InvalidArgumentException('The "keyVaultNamespace" option is required.'); | ||
| if (! isset($kmsProvider['type'])) { | ||
| throw ConfigurationException::kmsProviderTypeRequired(); | ||
| } | ||
|
|
||
| // @todo Throw en exception if multiple KMS providers are defined. This is not supported yet and would require a setting for the KMS provider to use when creating a new collection | ||
| if (! isset($options['kmsProviders']) || ! is_array($options['kmsProviders']) || count($options['kmsProviders']) < 1) { | ||
| throw new InvalidArgumentException('The "kmsProviders" option is required.'); | ||
| if (! is_string($kmsProvider['type'])) { | ||
| throw ConfigurationException::kmsProviderTypeMustBeString(); | ||
| } | ||
|
|
||
| $this->attributes['autoEncryption'] = $options; | ||
| $this->attributes['kmsProvider'] = $kmsProvider; | ||
| } | ||
|
|
||
| /** | ||
| * Get the options for auto-encryption. | ||
| * Set the default master key to use when creating encrypted collections. | ||
| * | ||
| * @see https://www.php.net/manual/en/mongodb-driver-clientencryption.construct.php | ||
| * @param array<string, mixed>|null $masterKey | ||
| */ | ||
| public function setDefaultMasterKey(?array $masterKey): void | ||
| { | ||
| $this->attributes['defaultMasterKey'] = $masterKey; | ||
| } | ||
|
|
||
| /** | ||
| * Set the options for auto-encryption. | ||
| * | ||
| * @see https://www.php.net/manual/en/mongodb-driver-manager.construct.php | ||
| * | ||
| * @phpstan-return AutoEncryptionOptions | ||
| * @param array{ keyVaultClient?: Client|Manager, keyVaultNamespace?: string, tlsOptions?: array<string, mixed>, schemaMap?: array<string, mixed>, encryptedFieldsMap?: array<string, mixed>, extraOptions?: array<string, mixed>} $options | ||
| */ | ||
| public function getAutoEncryption(): ?array | ||
| public function setAutoEncryption(array $options): void | ||
| { | ||
| return $this->attributes['autoEncryption'] ?? null; | ||
| if (isset($options['kmsProviders'])) { | ||
| throw ConfigurationException::kmsProvidersOptionMustUseSetter(); | ||
| } | ||
|
|
||
| $this->attributes['autoEncryption'] = $options; | ||
| } | ||
|
|
||
| public function getKmsProvider(): ?string | ||
| /** | ||
| * Get the default KMS provider name used when creating encrypted collections. | ||
| */ | ||
| public function getDefaultKmsProvider(): ?string | ||
| { | ||
| if (! isset($this->attributes['autoEncryption'])) { | ||
| return $this->attributes['kmsProvider']['type'] ?? null; | ||
| } | ||
|
|
||
| /** | ||
| * Get the default master key used when creating encrypted collections. | ||
| * | ||
| * @return array<string, mixed>|null | ||
| */ | ||
| public function getDefaultMasterKey(): ?array | ||
| { | ||
| if (! isset($this->attributes['kmsProvider']) || $this->attributes['kmsProvider']['type'] === 'local') { | ||
| return null; | ||
| } | ||
|
|
||
| return array_key_first($this->attributes['autoEncryption']['kmsProviders']); | ||
| return $this->attributes['defaultMasterKey'] ?? throw ConfigurationException::masterKeyRequired($this->attributes['kmsProvider']['type']); | ||
| } | ||
|
|
||
| private static function getVersion(): string | ||
|
|
@@ -743,6 +788,16 @@ private static function getVersion(): string | |
|
|
||
| return self::$version; | ||
| } | ||
|
|
||
| /** @return array<string, mixed> */ | ||
| private function getAutoEncryptionOptions(): array | ||
| { | ||
| return [ | ||
| 'kmsProviders' => [$this->attributes['kmsProvider']['type'] => array_diff_key($this->attributes['kmsProvider'], ['type' => 0])], | ||
| 'keyVaultNamespace' => $this->getDefaultDB() . '.datakeys', | ||
| ...$this->attributes['autoEncryption'] ?? [], | ||
| ]; | ||
| } | ||
| } | ||
|
|
||
| interface_exists(MappingDriver::class); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I extracted the
kmsProvideranddefaultMasterKeyfrom theautoEncryptionas they are not part of the driver options.