Skip to content

[Encryption] Improve diagnostic command #898

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

Open
wants to merge 18 commits into
base: feature/queryable-encryption
Choose a base branch
from

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Jun 18, 2025

From GromNaN#1

This improves the output of the diagnostics command and adds a command that prints the encrypted fields map for use in configuration.

Output:

❯ bin/console doctrine:mongodb:connection:diagnostic 

MongoDB Encryption Diagnostics
==============================

 PHP Environment
 * MongoDB extension loaded: Yes
 * MongoDB extension version: 1.21.1
 * MongoDB extension supports libmongocrypt: Yes
 * MongoDB library version: 2.1.0

 mongocryptd information
 * mongocryptd path: /Users/jerome/.local/bin/mongocryptd
 * mongocryptd version: mongocrypt version v8.0.8

Connection: default
-------------------

 Server Information
 * Server Version: 8.0.8
 * Topology: Replica Set

 Auto Encryption Configuration
 * Auto Encryption Enabled: Yes
 * Key Vault Namespace: symfony.datakeys
 * Key Count: 0

                                                                                                         
 [OK] System looks ok for encryption support.                                                            
                                                                                                         

In case of incorrect setup:

MongoDB Encryption Diagnostics
==============================

 PHP Environment
 * MongoDB extension loaded: Yes
 * MongoDB extension version: 2.1.0
 * MongoDB extension supports libmongocrypt: Yes
 * MongoDB library version: 2.1.0

 mongocryptd information
 * mongocryptd: not found

Connection: default
-------------------

 Server Information
 * Server Version: 8.0.8
 * Topology: Standalone

                                                                                                         
 [WARNING] This topology does not support encryption.                                                    
                                                                                                         

 Auto Encryption Configuration

                                                                                                         
 [ERROR] Failed to retrieve auto encryption information: mongocryptd error: No suitable servers found:   
         `serverSelectionTimeoutMS` expired: [connection refused calling hello on 'localhost:27020'].    
         Topology type: Single:                                                                          
                                                                                                         

Connection: other
-----------------

 Server Information
 * Server Version: 8.0.8
 * Topology: Standalone

                                                                                                         
 [WARNING] This topology does not support encryption.                                                    
                                                                                                         

 Auto Encryption Configuration
 Auto encryption is not enabled for this connection.

                                                                                                         
 [WARNING] Not all requirements for encryption support are met. Please check the diagnostics above.      

@GromNaN GromNaN changed the base branch from 5.4.x to feature/queryable-encryption June 18, 2025 09:32
@GromNaN GromNaN force-pushed the improve-diagnostic-command branch from f75dee1 to 30a90d3 Compare June 19, 2025 20:17
@GromNaN GromNaN requested a review from alcaeus June 19, 2025 20:56
@GromNaN GromNaN marked this pull request as ready for review June 19, 2025 20:56
} catch (Throwable $exception) {
$io->error('Could not retrieve auto encryption info: ' . $exception->getMessage());
}
$configOk = $this->printAndCheckConnectionDiagnostic($name, $diagnostic, $io) && $configOk;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When multiple connections are configured, but they don't have all auto encryption enabled, then the conclusion says there is an issue. We should return "OK" when auto encryption is not configured on a connection?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's a good point. Indeed, if encryption wasn't configured on a connection, we should print an info line and mark it as "ok".

@GromNaN GromNaN force-pushed the improve-diagnostic-command branch from 9f8a963 to b4d1412 Compare June 19, 2025 21:37
@GromNaN GromNaN mentioned this pull request Jun 19, 2025
5 tasks
alcaeus and others added 14 commits June 23, 2025 09:00
commit b494ea1
Author: Andreas Braun <[email protected]>
Date:   Thu Jun 12 13:36:37 2025 +0200

    Add command to dump encrypted fields map

commit a74e799
Author: Andreas Braun <[email protected]>
Date:   Thu Jun 12 13:10:11 2025 +0200

    Extract connection diagnostics into separate print method

commit 69d205d
Author: Andreas Braun <[email protected]>
Date:   Wed Jun 11 14:58:49 2025 +0200

    Extract encryption diagnostic to separate class

commit 1c1b2e2
Author: Andreas Braun <[email protected]>
Date:   Wed Jun 11 14:41:50 2025 +0200

    Improve connection diagnostics output

commit f19bc06
Merge: e24dd48 e09b2a2
Author: Jérôme Tamarelle <[email protected]>
Date:   Thu Jun 12 10:04:33 2025 +0200

    Merge remote-tracking branch 'upstream/5.4.x' into fix-kms-xsd-docs-alignment

commit e24dd48
Author: Jérôme Tamarelle <[email protected]>
Date:   Wed Jun 11 11:36:16 2025 +0200

    Add AutoEncryptionInfo to the diagnostic command

commit ea7b50f
Author: Jérôme Tamarelle <[email protected]>
Date:   Tue May 27 18:24:46 2025 +0200

    Introduce connection:diagnotic command

commit 60cc9c2
Author: Jérôme Tamarelle <[email protected]>
Date:   Mon May 26 18:21:32 2025 +0000

    Add autoEncryption configuration to the client
Improve connection diagnostics output
Comment on lines 507 to 515
if (isset($driverOptions['autoEncryption']['extraOptions']['cryptSharedLibPath'])) {
$fs = new Filesystem();
$cryptSharedLibPath = $driverOptions['autoEncryption']['extraOptions']['cryptSharedLibPath'];

// If it's not an absolute path, resolve it relative to the project root
if (! $fs->isAbsolutePath($cryptSharedLibPath)) {
$driverOptions['autoEncryption']['extraOptions']['cryptSharedLibPath'] = realpath($cryptSharedLibPath);
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alcaeus I'm reverting this part. In Symfony we never convert relative paths to absolute.

I'll add documentation using %kernel.project_dir% instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good - I wasn't quite sure if the kernel parameter was resolved properly. If that works, I prefer that over changing the path in the extension.

{
public function __construct(
private readonly ServiceProviderInterface $diagnostics,
private readonly EncryptionDiagnostic $encryptionDiagnostic = new EncryptionDiagnostic(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't declare a service for EncryptionDiagnostic. Only leverage the default value from the constructor.

@GromNaN GromNaN force-pushed the improve-diagnostic-command branch from b4d1412 to e192526 Compare June 23, 2025 08:11
@GromNaN GromNaN force-pushed the improve-diagnostic-command branch from 381dfba to 6b300d7 Compare June 23, 2025 13:50
@GromNaN GromNaN force-pushed the improve-diagnostic-command branch from 6b300d7 to cd2a184 Compare June 23, 2025 13:51
@alcaeus alcaeus requested a review from paulinevos June 25, 2025 15:23
@paulinevos
Copy link

There's a lot of commits in this. Does it need to be rebased?

@GromNaN
Copy link
Member Author

GromNaN commented Jun 26, 2025

There's a lot of commits in this. Does it need to be rebased?

It will be squashed on merge. All the commits represent the iterations on this PR.

Copy link

@paulinevos paulinevos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some small comments, otherwise seems fine

'Key Vault Namespace: ' . $autoEncryptionInfo['keyVaultNamespace'],
'Key Count: ' . $autoEncryptionInfo['keyCount'],
]);
} catch (RuntimeException $e) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not catch something broader? I'm guessing there could be other exception types that we'd want to catch here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only want to catch driver errors due to the command that is run. I don't expect other kind of exception to occurs, so if there is an other error I don't want to catch it, but I want the regular error handler to display it (with the backtrace).

Queryable Encryption (QE) allows you to run queries on encrypted fields. To use QE, you may need to provide an ``encryptedFieldsMap`` or use a schema map, depending on your driver and use case.
You can configure which fields are encrypted in each collection by specifying the
``autoEncryption.encryptedFieldsMap`` option in the connection configuration.
This setting **recommended** for improved security and performance.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing is after this setting

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This setting **recommended** for improved security and performance.
This setting is **recommended** for improved security and performance.

@@ -44,7 +44,6 @@
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Filesystem\Filesystem;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use fixup to add this commit to either:

  • whichever commit introduced the CS errors if that commit is part of this PR, or
  • fix it up to the other fix CS commit you already have

Not a big deal not but makes reviewing a little easier to go commit by commit

private function printAndCheckConnectionDiagnostic(string $name, ConnectionDiagnostic $diagnostic, SymfonyStyle $io): bool
{
$io->section(sprintf('Connection: %s', $name));

$configOk = $this->printAndCheckServerInfo($io, $diagnostic);
$this->printAutoEncryptionConfiguration($io, $diagnostic);
$enabled = $this->printAutoEncryptionConfiguration($io, $diagnostic);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename this to autoEncryptionEnabled?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants