From 93c6424e37235006552a063d98e6708716897fbc Mon Sep 17 00:00:00 2001 From: Zombaya Date: Sat, 23 Nov 2024 14:55:46 +0100 Subject: [PATCH] Add new configuration-key to migrate to --- TODO-6.0.md | 4 +- ...rypt.yaml => doctrine_encrypt_bundle.yaml} | 2 +- ...rypt.yaml => doctrine_encrypt_bundle.yaml} | 2 +- ...rypt.yaml => doctrine_encrypt_bundle.yaml} | 2 +- src/AmbtaDoctrineEncryptBundle.php | 10 +++ src/DependencyInjection/Configuration.php | 14 +++- .../DeprecatedDoctrineEncryptExtension.php | 36 +++++++++ .../DoctrineEncryptExtension.php | 38 ++++++--- src/Encryptors/DefuseEncryptor.php | 4 +- src/Encryptors/HaliteEncryptor.php | 4 +- src/Subscribers/DoctrineEncryptSubscriber.php | 2 +- tests/Unit/AmbtaDoctrineEncryptBundleTest.php | 80 +++++++++++++++++++ .../DoctrineEncryptExtensionTest.php | 6 +- tests/Unit/Encryptors/DefuseEncryptorTest.php | 9 ++- tests/Unit/Encryptors/HaliteEncryptorTest.php | 8 +- tests/_data/ambta_doctrine_encrypt.yaml | 4 + tests/_data/doctrine_encrypt_bundle.yaml | 4 + 17 files changed, 197 insertions(+), 32 deletions(-) rename demo/symfony6.x-orm3/config/packages/{ambta_doctrine_encrypt.yaml => doctrine_encrypt_bundle.yaml} (72%) rename demo/symfony6.x/config/packages/{ambta_doctrine_encrypt.yaml => doctrine_encrypt_bundle.yaml} (72%) rename demo/symfony7.x/config/packages/{ambta_doctrine_encrypt.yaml => doctrine_encrypt_bundle.yaml} (72%) create mode 100644 src/DependencyInjection/DeprecatedDoctrineEncryptExtension.php create mode 100644 tests/Unit/AmbtaDoctrineEncryptBundleTest.php create mode 100644 tests/_data/ambta_doctrine_encrypt.yaml create mode 100644 tests/_data/doctrine_encrypt_bundle.yaml diff --git a/TODO-6.0.md b/TODO-6.0.md index 248901ee..796737c4 100644 --- a/TODO-6.0.md +++ b/TODO-6.0.md @@ -1,3 +1,5 @@ # Things we need to do before we can release 6.0 * [ ] Remove \Ambta\DoctrineEncryptBundle\DependencyInjection\DoctrineEncryptExtension::$wrapExceptions -* [ ] Remove wrap_exceptions from configuration of the bundle \ No newline at end of file +* [ ] Remove wrap_exceptions from configuration of the bundle +* [ ] Remove DeprecatedDoctrineEncryptExtension +* [ ] Remove tests and demo using old `ambta_doctrine_encrypt` as configuration-key \ No newline at end of file diff --git a/demo/symfony6.x-orm3/config/packages/ambta_doctrine_encrypt.yaml b/demo/symfony6.x-orm3/config/packages/doctrine_encrypt_bundle.yaml similarity index 72% rename from demo/symfony6.x-orm3/config/packages/ambta_doctrine_encrypt.yaml rename to demo/symfony6.x-orm3/config/packages/doctrine_encrypt_bundle.yaml index ddbd65af..cdb465f0 100644 --- a/demo/symfony6.x-orm3/config/packages/ambta_doctrine_encrypt.yaml +++ b/demo/symfony6.x-orm3/config/packages/doctrine_encrypt_bundle.yaml @@ -1,3 +1,3 @@ -ambta_doctrine_encrypt: +doctrine_encrypt_bundle: enable_secret_generation: false secret: '%env(HALITE_SECRET)%' \ No newline at end of file diff --git a/demo/symfony6.x/config/packages/ambta_doctrine_encrypt.yaml b/demo/symfony6.x/config/packages/doctrine_encrypt_bundle.yaml similarity index 72% rename from demo/symfony6.x/config/packages/ambta_doctrine_encrypt.yaml rename to demo/symfony6.x/config/packages/doctrine_encrypt_bundle.yaml index ddbd65af..cdb465f0 100644 --- a/demo/symfony6.x/config/packages/ambta_doctrine_encrypt.yaml +++ b/demo/symfony6.x/config/packages/doctrine_encrypt_bundle.yaml @@ -1,3 +1,3 @@ -ambta_doctrine_encrypt: +doctrine_encrypt_bundle: enable_secret_generation: false secret: '%env(HALITE_SECRET)%' \ No newline at end of file diff --git a/demo/symfony7.x/config/packages/ambta_doctrine_encrypt.yaml b/demo/symfony7.x/config/packages/doctrine_encrypt_bundle.yaml similarity index 72% rename from demo/symfony7.x/config/packages/ambta_doctrine_encrypt.yaml rename to demo/symfony7.x/config/packages/doctrine_encrypt_bundle.yaml index ddbd65af..cdb465f0 100644 --- a/demo/symfony7.x/config/packages/ambta_doctrine_encrypt.yaml +++ b/demo/symfony7.x/config/packages/doctrine_encrypt_bundle.yaml @@ -1,3 +1,3 @@ -ambta_doctrine_encrypt: +doctrine_encrypt_bundle: enable_secret_generation: false secret: '%env(HALITE_SECRET)%' \ No newline at end of file diff --git a/src/AmbtaDoctrineEncryptBundle.php b/src/AmbtaDoctrineEncryptBundle.php index fad21f6f..ac0cdb86 100644 --- a/src/AmbtaDoctrineEncryptBundle.php +++ b/src/AmbtaDoctrineEncryptBundle.php @@ -2,8 +2,10 @@ namespace Ambta\DoctrineEncryptBundle; +use Ambta\DoctrineEncryptBundle\DependencyInjection\DeprecatedDoctrineEncryptExtension; use Ambta\DoctrineEncryptBundle\DependencyInjection\DoctrineEncryptExtension; use JetBrains\PhpStorm\Pure; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -14,4 +16,12 @@ public function getContainerExtension(): ?ExtensionInterface { return new DoctrineEncryptExtension(); } + + public function build(ContainerBuilder $container) + { + parent::build($container); + + // TODO-6.0 Remove the old extension again + $container->registerExtension(new DeprecatedDoctrineEncryptExtension()); + } } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 0a2f6202..3aae6dee 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -14,15 +14,25 @@ */ class Configuration implements ConfigurationInterface { + /** + * @var string + */ + private $rootName; + + public function __construct($rootName) + { + $this->rootName = $rootName; + } + public function getConfigTreeBuilder(): TreeBuilder { // Create tree builder - $treeBuilder = new TreeBuilder('ambta_doctrine_encrypt'); + $treeBuilder = new TreeBuilder($this->rootName); if (\method_exists($treeBuilder, 'getRootNode')) { $rootNode = $treeBuilder->getRootNode(); } else { // BC layer for symfony/config 4.1 and older - $rootNode = $treeBuilder->root('ambta_doctrine_encrypt'); + $rootNode = $treeBuilder->root($this->rootName); } // Grammar of config tree diff --git a/src/DependencyInjection/DeprecatedDoctrineEncryptExtension.php b/src/DependencyInjection/DeprecatedDoctrineEncryptExtension.php new file mode 100644 index 00000000..ed3bb2bf --- /dev/null +++ b/src/DependencyInjection/DeprecatedDoctrineEncryptExtension.php @@ -0,0 +1,36 @@ + DefuseEncryptor::class, @@ -33,9 +45,7 @@ class DoctrineEncryptExtension extends Extension public function load(array $configs, ContainerBuilder $container): void { - // Create configuration object - $configuration = new Configuration(); - $config = $this->processConfiguration($configuration, $configs); + $config = $this->processConfiguration(new Configuration($this->getAlias()), $configs); // If empty encryptor class, use Halite encryptor if (array_key_exists($config['encryptor_class'], self::SupportedEncryptorClasses)) { @@ -46,11 +56,15 @@ public function load(array $configs, ContainerBuilder $container): void // Set parameters $container->setParameter('ambta_doctrine_encrypt.encryptor_class_name', $config['encryptor_class_full']); - $container->setParameter('ambta_doctrine_encrypt.secret_directory_path', $config['secret_directory_path']); - $container->setParameter('ambta_doctrine_encrypt.enable_secret_generation', $config['enable_secret_generation']); if (isset($config['secret'])) { $container->setParameter('ambta_doctrine_encrypt.secret', $config['secret']); + } else { + $container->setParameter( + 'ambta_doctrine_encrypt.enable_secret_generation', + $config['enable_secret_generation'] + ); + $container->setParameter('ambta_doctrine_encrypt.secret_directory_path', $config['secret_directory_path']); } // Load service file @@ -77,7 +91,11 @@ public function load(array $configs, ContainerBuilder $container): void // PHP 8.x (annotations and attributes) } else { // Doctrine 3.0 - no annotations - if (\Composer\InstalledVersions::satisfies(new \Composer\Semver\VersionParser(), 'doctrine/orm', '^3.0')) { + if (\Composer\InstalledVersions::satisfies( + new \Composer\Semver\VersionParser(), + 'doctrine/orm', + '^3.0' + )) { $loader->load('service_listeners_with_attributes.yml'); } else { $loader->load('services_subscriber_with_annotations_and_attributes.yml'); @@ -90,7 +108,7 @@ public function load(array $configs, ContainerBuilder $container): void // Wrap exceptions if ($config['wrap_exceptions']) { - self::$wrapExceptions = true; + self::wrapExceptions(true); } else { trigger_deprecation( 'doctrineencryptbundle/doctrine-encrypt-bundle', @@ -108,6 +126,6 @@ public function load(array $configs, ContainerBuilder $container): void */ public function getAlias(): string { - return 'ambta_doctrine_encrypt'; + return 'doctrine_encrypt_bundle'; } } diff --git a/src/Encryptors/DefuseEncryptor.php b/src/Encryptors/DefuseEncryptor.php index e8086b3f..1c90abb3 100644 --- a/src/Encryptors/DefuseEncryptor.php +++ b/src/Encryptors/DefuseEncryptor.php @@ -35,7 +35,7 @@ public function encrypt(string $data): string try { return \Defuse\Crypto\Crypto::encryptWithPassword($data, $this->secret); } catch (\Throwable $e) { - if (DoctrineEncryptExtension::$wrapExceptions) { + if (DoctrineEncryptExtension::wrapExceptions()) { throw new UnableToEncryptException($e->getMessage(), $e->getCode(), $e); } throw $e; @@ -51,7 +51,7 @@ public function decrypt(string $data): string try { return \Defuse\Crypto\Crypto::decryptWithPassword($data, $this->secret); } catch (\Throwable $e) { - if (DoctrineEncryptExtension::$wrapExceptions) { + if (DoctrineEncryptExtension::wrapExceptions()) { throw new UnableToDecryptException($e->getMessage(), $e->getCode(), $e); } throw $e; diff --git a/src/Encryptors/HaliteEncryptor.php b/src/Encryptors/HaliteEncryptor.php index 0437ba0c..9cb988c8 100644 --- a/src/Encryptors/HaliteEncryptor.php +++ b/src/Encryptors/HaliteEncryptor.php @@ -40,7 +40,7 @@ public function encrypt(string $data): string try { return Crypto::encrypt(new HiddenString($data), $this->getKey()); } catch (\Throwable $e) { - if (DoctrineEncryptExtension::$wrapExceptions) { + if (DoctrineEncryptExtension::wrapExceptions()) { throw new UnableToEncryptException($e->getMessage(), $e->getCode(), $e); } throw $e; @@ -58,7 +58,7 @@ public function decrypt(string $data): string try { return Crypto::decrypt($data, $this->getKey())->getString(); } catch (\Throwable $e) { - if (DoctrineEncryptExtension::$wrapExceptions) { + if (DoctrineEncryptExtension::wrapExceptions()) { throw new UnableToDecryptException($e->getMessage(), $e->getCode(), $e); } throw $e; diff --git a/src/Subscribers/DoctrineEncryptSubscriber.php b/src/Subscribers/DoctrineEncryptSubscriber.php index 2f99a08c..7b99e699 100644 --- a/src/Subscribers/DoctrineEncryptSubscriber.php +++ b/src/Subscribers/DoctrineEncryptSubscriber.php @@ -310,7 +310,7 @@ public function processFields(object $entity, EntityManagerInterface $entityMana } catch (DoctrineEncryptBundleException $e) { throw $e; } catch (\Throwable $e) { - if (DoctrineEncryptExtension::$wrapExceptions) { + if (DoctrineEncryptExtension::wrapExceptions()) { throw new DoctrineEncryptBundleException('Something went wrong encrypting/decrypting a secret', 0, $e); } throw $e; diff --git a/tests/Unit/AmbtaDoctrineEncryptBundleTest.php b/tests/Unit/AmbtaDoctrineEncryptBundleTest.php new file mode 100644 index 00000000..172fb986 --- /dev/null +++ b/tests/Unit/AmbtaDoctrineEncryptBundleTest.php @@ -0,0 +1,80 @@ + false]) + ); + } + + protected function tearDown(): void + { + parent::tearDown(); + + DoctrineEncryptExtension::wrapExceptions(false); + } + + /** + * @group legacy + */ + public function testContainerIsAbleToConfigFromOldNamespace(): void + { + $container = $this->createContainer(); + + $bundle = new AmbtaDoctrineEncryptBundle(); + + $container->registerExtension($bundle->getContainerExtension()); + $bundle->build($container); + + $yamlLoader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../_data')); + $yamlLoader->load('ambta_doctrine_encrypt.yaml'); + + $container->addCompilerPass(new MergeExtensionConfigurationPass()); + + $this->expectDeprecation('Since doctrineencryptbundle/doctrine-encrypt-bundle 5.4.2: Using `ambta_doctrine_encrypt` as the configuration-key is deprecated and you should replace this with `doctrine_encrypt_bundle`. +Starting from 6.0, only `doctrine_encrypt_bundle` will be supported.'); + + $container->compile(); + + $this->assertTrue($container->hasParameter('ambta_doctrine_encrypt.secret')); + $this->assertEquals('ambta_doctrine_encrypt.yaml', $container->getParameter('ambta_doctrine_encrypt.secret')); + } + + public function testContainerIsAbleToConfigFromNewNamespace(): void + { + $container = $this->createContainer(); + + $bundle = new AmbtaDoctrineEncryptBundle(); + + $container->registerExtension($bundle->getContainerExtension()); + $bundle->build($container); + + $yamlLoader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../_data')); + $yamlLoader->load('doctrine_encrypt_bundle.yaml'); + + $container->addCompilerPass(new MergeExtensionConfigurationPass()); + + $container->compile(); + + $this->assertTrue($container->hasParameter('ambta_doctrine_encrypt.secret')); + $this->assertEquals('doctrine_encrypt_bundle.yaml', $container->getParameter('ambta_doctrine_encrypt.secret')); + } +} diff --git a/tests/Unit/DependencyInjection/DoctrineEncryptExtensionTest.php b/tests/Unit/DependencyInjection/DoctrineEncryptExtensionTest.php index 5447d6f9..6967967f 100644 --- a/tests/Unit/DependencyInjection/DoctrineEncryptExtensionTest.php +++ b/tests/Unit/DependencyInjection/DoctrineEncryptExtensionTest.php @@ -214,7 +214,7 @@ public function testWrapExceptionsTriggersDeprecationWarningWhenNotDefiningTheOp $this->expectDeprecation('Since doctrineencryptbundle/doctrine-encrypt-bundle 5.4.2: Starting from 6.0, all exceptions thrown by this library will be wrapped by \Ambta\DoctrineEncryptBundle\Exception\DoctrineEncryptBundleException or a child-class of it. You can start using these exceptions today by setting \'ambta_doctrine_encrypt.wrap_exceptions\' to TRUE.'); $this->extension->load([$config], $container); - $this->assertFalse(DoctrineEncryptExtension::$wrapExceptions); + $this->assertFalse(DoctrineEncryptExtension::wrapExceptions()); } /** @@ -228,7 +228,7 @@ public function testWrapExceptionsTriggersDeprecationWarningWhenDisabled(): void $this->expectDeprecation('Since doctrineencryptbundle/doctrine-encrypt-bundle 5.4.2: Starting from 6.0, all exceptions thrown by this library will be wrapped by \Ambta\DoctrineEncryptBundle\Exception\DoctrineEncryptBundleException or a child-class of it. You can start using these exceptions today by setting \'ambta_doctrine_encrypt.wrap_exceptions\' to TRUE.'); $this->extension->load([$config], $container); - $this->assertFalse(DoctrineEncryptExtension::$wrapExceptions); + $this->assertFalse(DoctrineEncryptExtension::wrapExceptions()); } /** @@ -240,7 +240,7 @@ public function testWrapExceptionsDoesNotTriggerDeprecationWarningWhenEnabled(): $config = ['wrap_exceptions' => true]; $this->extension->load([$config], $container); - $this->assertTrue(DoctrineEncryptExtension::$wrapExceptions); + $this->assertTrue(DoctrineEncryptExtension::wrapExceptions()); } private function createContainer(): ContainerBuilder diff --git a/tests/Unit/Encryptors/DefuseEncryptorTest.php b/tests/Unit/Encryptors/DefuseEncryptorTest.php index 71933c88..6cba28fc 100644 --- a/tests/Unit/Encryptors/DefuseEncryptorTest.php +++ b/tests/Unit/Encryptors/DefuseEncryptorTest.php @@ -10,17 +10,18 @@ class DefuseEncryptorTest extends TestCase { private const DATA = 'foobar'; + /** @var bool */ private $originalWrapExceptions; protected function setUp(): void { - $this->originalWrapExceptions = DoctrineEncryptExtension::$wrapExceptions; + $this->originalWrapExceptions = DoctrineEncryptExtension::wrapExceptions(); } protected function tearDown(): void { - DoctrineEncryptExtension::$wrapExceptions = $this->originalWrapExceptions; + DoctrineEncryptExtension::wrapExceptions($this->originalWrapExceptions); } public function testEncrypt(): void @@ -40,7 +41,7 @@ public function testEncrypt(): void public function testEncryptorThrowsOwnExceptionWhenExceptionsAreNotWrapped(): void { - DoctrineEncryptExtension::$wrapExceptions = false; + DoctrineEncryptExtension::wrapExceptions(false); try { (new DefuseEncryptor('not-a-valid-key'))->decrypt('foo'); @@ -54,7 +55,7 @@ public function testEncryptorThrowsOwnExceptionWhenExceptionsAreNotWrapped(): vo public function testEncryptorThrowsBundleExceptionWhenExceptionsAreWrapped(): void { - DoctrineEncryptExtension::$wrapExceptions = true; + DoctrineEncryptExtension::wrapExceptions(true); try { (new DefuseEncryptor('not-a-valid-key'))->decrypt('foo'); diff --git a/tests/Unit/Encryptors/HaliteEncryptorTest.php b/tests/Unit/Encryptors/HaliteEncryptorTest.php index 4bd15d4b..b1c58325 100644 --- a/tests/Unit/Encryptors/HaliteEncryptorTest.php +++ b/tests/Unit/Encryptors/HaliteEncryptorTest.php @@ -16,12 +16,12 @@ class HaliteEncryptorTest extends TestCase protected function setUp(): void { - $this->originalWrapExceptions = DoctrineEncryptExtension::$wrapExceptions; + $this->originalWrapExceptions = DoctrineEncryptExtension::wrapExceptions(); } protected function tearDown(): void { - DoctrineEncryptExtension::$wrapExceptions = $this->originalWrapExceptions; + DoctrineEncryptExtension::wrapExceptions($this->originalWrapExceptions); } public function testEncryptExtension(): void @@ -42,7 +42,7 @@ public function testEncryptExtension(): void public function testEncryptorThrowsOwnExceptionWhenExceptionsAreNotWrapped(): void { - DoctrineEncryptExtension::$wrapExceptions = false; + DoctrineEncryptExtension::wrapExceptions(false); try { (new HaliteEncryptor('not-a-valid-key'))->encrypt('foo'); @@ -56,7 +56,7 @@ public function testEncryptorThrowsOwnExceptionWhenExceptionsAreNotWrapped(): vo public function testEncryptorThrowsBundleExceptionWhenExceptionsAreWrapped(): void { - DoctrineEncryptExtension::$wrapExceptions = true; + DoctrineEncryptExtension::wrapExceptions(true); try { (new HaliteEncryptor('not-a-valid-key'))->encrypt('foo'); diff --git a/tests/_data/ambta_doctrine_encrypt.yaml b/tests/_data/ambta_doctrine_encrypt.yaml new file mode 100644 index 00000000..6c7a2f73 --- /dev/null +++ b/tests/_data/ambta_doctrine_encrypt.yaml @@ -0,0 +1,4 @@ +ambta_doctrine_encrypt: + enable_secret_generation: false + secret: 'ambta_doctrine_encrypt.yaml' + wrap_exceptions: true \ No newline at end of file diff --git a/tests/_data/doctrine_encrypt_bundle.yaml b/tests/_data/doctrine_encrypt_bundle.yaml new file mode 100644 index 00000000..49712c1d --- /dev/null +++ b/tests/_data/doctrine_encrypt_bundle.yaml @@ -0,0 +1,4 @@ +doctrine_encrypt_bundle: + enable_secret_generation: false + secret: 'doctrine_encrypt_bundle.yaml' + wrap_exceptions: true