diff --git a/src-new/DependencyInjection/ServiceConfigurator.php b/src-new/DependencyInjection/ServiceConfigurator.php index c89d4c3..271edb1 100644 --- a/src-new/DependencyInjection/ServiceConfigurator.php +++ b/src-new/DependencyInjection/ServiceConfigurator.php @@ -5,7 +5,6 @@ namespace DoctrineEncryptBundle\DoctrineEncryptBundle\DependencyInjection; use Ambta\DoctrineEncryptBundle\DependencyInjection\DoctrineEncryptExtension as AmbtaExtension; -use Ambta\DoctrineEncryptBundle\DependencyInjection\VersionTester; use DoctrineEncryptBundle\DoctrineEncryptBundle\DependencyInjection\DoctrineEncryptExtension as BundleExtension; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; @@ -25,11 +24,8 @@ final class ServiceConfigurator /** @var bool */ private $useNewNames; - /** @var string */ - private $prefix; - /** @var int */ - private $index; + private $nameIndex; private const NAMES = [ 'parameters' => [ @@ -99,11 +95,9 @@ public function __construct( $this->useNewNames = $useNewNames; if ($this->useNewNames) { - $this->prefix = 'doctrine_encrypt_bundle.'; - $this->index = 1; + $this->nameIndex = 1; } else { - $this->prefix = 'ambta_doctrine_encrypt.'; - $this->index = 0; + $this->nameIndex = 0; } } @@ -114,12 +108,12 @@ private function setParameter(ContainerBuilder $container, string $name, $value) private function getParameterName(string $name): string { - return self::NAMES['parameters'][$name][$this->index]; + return self::NAMES['parameters'][$name][$this->nameIndex]; } private function getServiceName(string $name): string { - return self::NAMES['services'][$name][$this->index]; + return self::NAMES['services'][$name][$this->nameIndex]; } private function registerService(ContainerBuilder $container, string $name, string $oldClass, string $newClass): Definition @@ -197,9 +191,14 @@ public function configure(array $config, ContainerBuilder $container): void trigger_deprecation( 'doctrineencryptbundle/doctrine-encrypt-bundle', '5.4.2', - <<useNewNames + ? <<prefix}wrap_exceptions' to TRUE. +You can start using these exceptions today by setting 'ambta_doctrine_encrypt.wrap_exceptions' to TRUE. EOF ); } @@ -309,6 +308,7 @@ private function defineServicesUsingSecretFactory(ContainerBuilder $container): ->setArguments([ new Parameter($this->getParameterName('secret_directory_path')), new Parameter($this->getParameterName('enable_secret_generation')), + new Parameter($this->getParameterName('supported_encryptors')), ]) ; } @@ -322,7 +322,7 @@ private function defineServicesUsingAnnotations(ContainerBuilder $container): vo $container, 'orm_subscriber', \Ambta\DoctrineEncryptBundle\Subscribers\DoctrineEncryptSubscriber::class, - \Ambta\DoctrineEncryptBundle\Subscribers\DoctrineEncryptSubscriber::class + \DoctrineEncryptBundle\DoctrineEncryptBundle\Subscribers\DoctrineEncryptSubscriber::class ) ->setArguments([ new Reference($this->getServiceName('annotation_reader')), @@ -369,7 +369,7 @@ private function defineServicesUsingAnnotationsAndAttributes(ContainerBuilder $c $container, 'attribute_reader', \Ambta\DoctrineEncryptBundle\Mapping\AttributeReader::class, - \DoctrineEncryptBundle\DoctrineEncryptBundle\Mapping\AttributeReader::class, + \DoctrineEncryptBundle\DoctrineEncryptBundle\Mapping\AttributeReader::class ); $this @@ -377,7 +377,7 @@ private function defineServicesUsingAnnotationsAndAttributes(ContainerBuilder $c $container, 'annotation_reader', \Ambta\DoctrineEncryptBundle\Mapping\AttributeAnnotationReader::class, - \DoctrineEncryptBundle\DoctrineEncryptBundle\Mapping\AttributeAnnotationReader::class, + \DoctrineEncryptBundle\DoctrineEncryptBundle\Mapping\AttributeAnnotationReader::class ) ->setArguments([ new Reference($this->getServiceName('attribute_reader')), diff --git a/src/DependencyInjection/VersionTester.php b/src-new/DependencyInjection/VersionTester.php similarity index 87% rename from src/DependencyInjection/VersionTester.php rename to src-new/DependencyInjection/VersionTester.php index f03b5ce..9b46f68 100644 --- a/src/DependencyInjection/VersionTester.php +++ b/src-new/DependencyInjection/VersionTester.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Ambta\DoctrineEncryptBundle\DependencyInjection; +namespace DoctrineEncryptBundle\DoctrineEncryptBundle\DependencyInjection; use Symfony\Component\HttpKernel\Kernel; diff --git a/src/AmbtaDoctrineEncryptBundle.php b/src/AmbtaDoctrineEncryptBundle.php index 9747302..a967ad1 100644 --- a/src/AmbtaDoctrineEncryptBundle.php +++ b/src/AmbtaDoctrineEncryptBundle.php @@ -4,7 +4,7 @@ use Ambta\DoctrineEncryptBundle\DependencyInjection\DeprecatedDoctrineEncryptExtension; use Ambta\DoctrineEncryptBundle\DependencyInjection\DoctrineEncryptExtension; -use Ambta\DoctrineEncryptBundle\DependencyInjection\VersionTester; +use DoctrineEncryptBundle\DoctrineEncryptBundle\DependencyInjection\VersionTester; use JetBrains\PhpStorm\Pure; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; diff --git a/src/Command/AbstractCommand.php b/src/Command/AbstractCommand.php index 261c442..34d45c2 100644 --- a/src/Command/AbstractCommand.php +++ b/src/Command/AbstractCommand.php @@ -48,9 +48,9 @@ public function __construct( array $supportedEncryptors ) { parent::__construct(); - $this->entityManager = $entityManager; - $this->annotationReader = $annotationReader; - $this->subscriber = $subscriber; + $this->entityManager = $entityManager; + $this->annotationReader = $annotationReader; + $this->subscriber = $subscriber; $this->supportedEncryptors = $supportedEncryptors; } diff --git a/src/Command/DoctrineDecryptDatabaseCommand.php b/src/Command/DoctrineDecryptDatabaseCommand.php index 38af594..22cd48f 100644 --- a/src/Command/DoctrineDecryptDatabaseCommand.php +++ b/src/Command/DoctrineDecryptDatabaseCommand.php @@ -2,7 +2,6 @@ namespace Ambta\DoctrineEncryptBundle\Command; -use Ambta\DoctrineEncryptBundle\DependencyInjection\DoctrineEncryptExtension; use Doctrine\ORM\Mapping\ClassMetadataInfo; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Input\InputArgument; diff --git a/src/Command/DoctrineEncryptDatabaseCommand.php b/src/Command/DoctrineEncryptDatabaseCommand.php index 4ed8ab1..e49ad3e 100644 --- a/src/Command/DoctrineEncryptDatabaseCommand.php +++ b/src/Command/DoctrineEncryptDatabaseCommand.php @@ -2,7 +2,6 @@ namespace Ambta\DoctrineEncryptBundle\Command; -use Ambta\DoctrineEncryptBundle\DependencyInjection\DoctrineEncryptExtension; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; diff --git a/src/DependencyInjection/DoctrineEncryptExtension.php b/src/DependencyInjection/DoctrineEncryptExtension.php index fc95d29..ffa9b2c 100644 --- a/src/DependencyInjection/DoctrineEncryptExtension.php +++ b/src/DependencyInjection/DoctrineEncryptExtension.php @@ -5,6 +5,7 @@ use Ambta\DoctrineEncryptBundle\Encryptors\DefuseEncryptor; use Ambta\DoctrineEncryptBundle\Encryptors\HaliteEncryptor; use DoctrineEncryptBundle\DoctrineEncryptBundle\DependencyInjection\ServiceConfigurator; +use DoctrineEncryptBundle\DoctrineEncryptBundle\DependencyInjection\VersionTester; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Extension\Extension; diff --git a/src/Factories/SecretFactory.php b/src/Factories/SecretFactory.php index e368fba..20e4846 100644 --- a/src/Factories/SecretFactory.php +++ b/src/Factories/SecretFactory.php @@ -2,8 +2,6 @@ namespace Ambta\DoctrineEncryptBundle\Factories; -use Ambta\DoctrineEncryptBundle\Encryptors\DefuseEncryptor; -use Ambta\DoctrineEncryptBundle\Encryptors\HaliteEncryptor; use ParagonIE\Halite\KeyFactory; use Symfony\Component\Filesystem\Filesystem; @@ -18,10 +16,17 @@ class SecretFactory */ private $enableSecretCreation; - public function __construct(string $secretDirectory, bool $enableSecretCreation) - { + /** @var array */ + private $supportedEncryptors; + + public function __construct( + string $secretDirectory, + bool $enableSecretCreation, + array $supportedEncryptors + ) { $this->secretDirectory = $secretDirectory; $this->enableSecretCreation = $enableSecretCreation; + $this->supportedEncryptors = $supportedEncryptors; } /** @@ -31,11 +36,11 @@ public function __construct(string $secretDirectory, bool $enableSecretCreation) */ public function getSecret(string $className) { - if (!in_array($className, [DefuseEncryptor::class,HaliteEncryptor::class])) { + if (!in_array($className, $this->supportedEncryptors)) { throw new \RuntimeException(sprintf('Class "%s" is not supported by %s', $className, self::class)); } - if ($className === HaliteEncryptor::class) { + if (str_contains($className, 'HaliteEncryptor')) { $filename = '.Halite.key'; } else { $filename = '.Defuse.key'; @@ -72,11 +77,11 @@ public function getSecret(string $className) */ private function createSecret(string $secretPath, string $className) { - if ($className === HaliteEncryptor::class) { + if (str_contains($className, 'HaliteEncryptor')) { $encryptionKey = KeyFactory::generateEncryptionKey(); KeyFactory::save($encryptionKey, $secretPath); $secret = KeyFactory::export($encryptionKey)->getString(); - } elseif ($className === DefuseEncryptor::class) { + } else { $secret = bin2hex(random_bytes(255)); file_put_contents($secretPath, $secret); } diff --git a/tests-new/Unit/DependencyInjection/DoctrineEncryptExtensionTest.php b/tests-new/Unit/DependencyInjection/DoctrineEncryptExtensionTest.php new file mode 100644 index 0000000..bf56d26 --- /dev/null +++ b/tests-new/Unit/DependencyInjection/DoctrineEncryptExtensionTest.php @@ -0,0 +1,507 @@ +extension = new DoctrineEncryptExtension(new VersionTester()); + $this->temporaryDirectory = sys_get_temp_dir().DIRECTORY_SEPARATOR.sha1(mt_rand()); + mkdir($this->temporaryDirectory); + } + + protected function tearDown(): void + { + unlink($this->temporaryDirectory); + } + + public function testConfigLoadHaliteByDefault(): void + { + $container = $this->createContainer(); + $this->extension->load([[]], $container); + + $this->assertSame(HaliteEncryptor::class, $container->getParameter('doctrine_encrypt.encryptor.class_name')); + } + + public function testConfigLoadHalite(): void + { + $container = $this->createContainer(); + $config = [ + 'encryptor_class' => 'Halite', + ]; + $this->extension->load([$config], $container); + + $this->assertSame(HaliteEncryptor::class, $container->getParameter('doctrine_encrypt.encryptor.class_name')); + } + + public function testConfigLoadDefuse(): void + { + $container = $this->createContainer(); + + $config = [ + 'encryptor_class' => 'Defuse', + ]; + $this->extension->load([$config], $container); + + $this->assertSame(DefuseEncryptor::class, $container->getParameter('doctrine_encrypt.encryptor.class_name')); + } + + public function testConfigLoadCustomEncryptor(): void + { + $container = $this->createContainer(); + $config = [ + 'encryptor_class' => self::class, + ]; + $this->extension->load([$config], $container); + + $this->assertSame(self::class, $container->getParameter('doctrine_encrypt.encryptor.class_name')); + } + + public function testConfigImpossibleToUseSecretAndSecretDirectoryPath(): void + { + $container = $this->createContainer(); + $config = [ + 'secret' => 'my-secret', + 'secret_directory_path' => 'var', + ]; + + $this->expectException(\InvalidArgumentException::class); + + $this->extension->load([$config], $container); + } + + public function testConfigUseSecret(): void + { + $container = $this->createContainer(); + $config = [ + 'secret' => 'my-secret', + ]; + $this->extension->load([$config], $container); + + $this->assertIsString($container->getParameter('doctrine_encrypt.secret')); + $this->assertStringNotContainsString('Halite', $container->getParameter('doctrine_encrypt.secret')); + $this->assertStringNotContainsString('.key', $container->getParameter('doctrine_encrypt.secret')); + $this->assertEquals('my-secret', $container->getParameter('doctrine_encrypt.secret')); + } + + public function testHaliteSecretIsCreatedWhenSecretFileDoesNotExistAndSecretCreationIsEnabled(): void + { + $container = $this->createContainer(); + $config = [ + 'secret_directory_path' => $this->temporaryDirectory, + 'enable_secret_generation' => true, + ]; + $this->extension->load([$config], $container); + + $secretArgument = $container->getDefinition('doctrine_encrypt.encryptor')->getArgument(0); + if ($secretArgument instanceof Expression) { + $actualSecret = $container->resolveServices($secretArgument); + } else { + $actualSecret = $secretArgument; + } + $this->assertIsString($actualSecret); + $actualSecretOnDisk = file_get_contents($this->temporaryDirectory.DIRECTORY_SEPARATOR.'.Halite.key'); + $this->assertEquals($actualSecret, $actualSecretOnDisk); + + try { + KeyFactory::importEncryptionKey(new HiddenString($actualSecret)); + } catch (\Throwable $e) { + $this->fail('Generated key is not valid'); + } + } + + public function testDefuseSecretIsCreatedWhenSecretFileDoesNotExistAndSecretCreationIsEnabled(): void + { + $container = $this->createContainer(); + $config = [ + 'encryptor_class' => 'Defuse', + 'secret_directory_path' => $this->temporaryDirectory, + 'enable_secret_generation' => true, + ]; + $this->extension->load([$config], $container); + + $secretArgument = $container->getDefinition('doctrine_encrypt.encryptor')->getArgument(0); + if ($secretArgument instanceof Expression) { + $actualSecret = $container->resolveServices($secretArgument); + } else { + $actualSecret = $secretArgument; + } + $this->assertIsString($actualSecret); + $actualSecretOnDisk = file_get_contents($this->temporaryDirectory.DIRECTORY_SEPARATOR.'.Defuse.key'); + $this->assertEquals($actualSecret, $actualSecretOnDisk); + + if (strlen(hex2bin($actualSecret)) !== 255) { + $this->fail('Generated key is not valid'); + } + } + + public function testSecretIsNotCreatedWhenSecretFileDoesNotExistAndSecretCreationIsNotEnabled(): void + { + $container = $this->createContainer(); + $config = [ + 'secret_directory_path' => $this->temporaryDirectory, + 'enable_secret_generation' => false, + ]; + $this->extension->load([$config], $container); + + $this->expectException(\RuntimeException::class); + if (method_exists($this, 'expectExceptionMessageMatches')) { + $this->expectExceptionMessageMatches('/DoctrineEncryptBundle: Unable to create secret.*/'); + } elseif (method_exists($this, 'expectExceptionMessageRegExp')) { + $this->expectExceptionMessageRegExp('/DoctrineEncryptBundle: Unable to create secret.*/'); + } else { + $this->markAsRisky('Unable to see if the exception matches the actual message'); + } + + $secretArgument = $container->getDefinition('doctrine_encrypt.encryptor')->getArgument(0); + if ($secretArgument instanceof Expression) { + $container->resolveServices($secretArgument); + } + } + + public function testSecretsAreReadFromFile(): void + { + // Create secret + $expectedSecret = 'my-secret'; + file_put_contents($this->temporaryDirectory.'/.Halite.key', $expectedSecret); + + $container = $this->createContainer(); + $config = [ + 'secret_directory_path' => $this->temporaryDirectory, + 'enable_secret_generation' => false, + ]; + $this->extension->load([$config], $container); + + $secretArgument = $container->getDefinition('doctrine_encrypt.encryptor')->getArgument(0); + if ($secretArgument instanceof Expression) { + $actualSecret = $container->resolveServices($secretArgument); + } else { + $actualSecret = $secretArgument; + } + $this->assertIsString($actualSecret); + $this->assertEquals($expectedSecret, $actualSecret); + } + + /** + * @group legacy + */ + public function testWrapExceptionsTriggersDeprecationWarningWhenNotDefiningTheOption(): void + { + $container = $this->createContainer(); + $config = []; + + $this->expectDeprecation('Since doctrineencryptbundle/doctrine-encrypt-bundle 5.4.2: Starting from 6.0, all exceptions thrown by this library will be wrapped by \DoctrineEncryptBundle\DoctrineEncryptBundle\Exception\DoctrineEncryptBundleException or a child-class of it. +You can start using these exceptions today by setting \'doctrine_encrypt.wrap_exceptions\' to TRUE.'); + $this->extension->load([$config], $container); + $this->assertFalse(DoctrineEncryptExtension::wrapExceptions()); + } + + /** + * @group legacy + */ + public function testWrapExceptionsTriggersDeprecationWarningWhenDisabled(): void + { + $container = $this->createContainer(); + $config = ['wrap_exceptions' => false]; + + $this->expectDeprecation('Since doctrineencryptbundle/doctrine-encrypt-bundle 5.4.2: Starting from 6.0, all exceptions thrown by this library will be wrapped by \DoctrineEncryptBundle\DoctrineEncryptBundle\Exception\DoctrineEncryptBundleException or a child-class of it. +You can start using these exceptions today by setting \'doctrine_encrypt.wrap_exceptions\' to TRUE.'); + $this->extension->load([$config], $container); + $this->assertFalse(DoctrineEncryptExtension::wrapExceptions()); + } + + /** + * @group legacy + */ + public function testWrapExceptionsDoesNotTriggerDeprecationWarningWhenEnabled(): void + { + $container = $this->createContainer(); + $config = ['wrap_exceptions' => true]; + + $this->expectDeprecation(''); + $this->extension->load([$config], $container); + $this->assertTrue(DoctrineEncryptExtension::wrapExceptions()); + } + + /** + * @dataProvider provideConfigLoadsCorrectServicesAndParametersCases + */ + public function testConfigLoadsCorrectServicesAndParameters( + array $config, + array $mockedVersions, + array $expectedParameters, + array $expectedServices, + array $expectedAliases + ): void { + $container = $this->createContainer(); + + // Default from setup + foreach ($container->getParameterBag()->all() as $key => $value) { + $expectedParameters[$key] = $value; + } + foreach ($container->getDefinitions() as $id => $definition) { + $expectedServices[$id] = $definition->getClass(); + } + + $versionTester = $this->createMock(VersionTester::class); + foreach ($mockedVersions as $method => $response) { + $versionTester->method($method)->willReturn($response); + } + + $extension = new DoctrineEncryptExtension($versionTester); + + $extension->load([$config], $container); + + $this->assertEqualsCanonicalizing($expectedParameters, $container->getParameterBag()->all()); + + $expectedServiceIds = array_merge( + array_keys($expectedServices), + array_keys($expectedAliases), + [ + \Psr\Container\ContainerInterface::class, + \Symfony\Component\DependencyInjection\ContainerInterface::class, + ] + ); + + $this->assertEqualsCanonicalizing($expectedServiceIds, $container->getServiceIds()); + + foreach ($expectedServices as $expectedService => $expectedValue) { + $this->assertTrue($container->has($expectedService)); + $this->assertEquals($expectedValue, $container->getDefinition($expectedService)->getClass(), $expectedService); + } + + foreach ($expectedAliases as $expectedAlias => $expectedValue) { + $this->assertTrue($container->has($expectedAlias)); + $this->assertEquals($expectedValue, (string) $container->getAlias($expectedAlias)); + } + + // Mock additional services + $container->set('doctrine.orm.entity_manager', $this->createMock(EntityManagerInterface::class)); + $container->setParameter('kernel.project_dir', ''); + $container->setParameter('kernel.cache_dir', ''); + + // Assert all services are gettable + foreach ($expectedServices as $expectedService => $class) { + $this->assertNotNull($container->get($expectedService)); + } + } + + public static function provideConfigLoadsCorrectServicesAndParametersCases(): iterable + { + yield 'empty-sf5-php7-orm2' => [ + [], + [ + 'isSymfony7OrHigher' => false, + 'isPhp8OrHigher' => false, + 'doctrineOrmIsVersion3' => false, + ], + [ + 'doctrine_encrypt.encryptor.class_name' => HaliteEncryptor::class, + 'doctrine_encrypt.supported_encryptors' => [ + 'halite' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Encryptors\DefuseEncryptor::class, + 'defuse' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Encryptors\HaliteEncryptor::class, + ], + 'doctrine_encrypt.secret.enable_generation' => true, + 'doctrine_encrypt.secret.directory_path' => '%kernel.project_dir%', + ], + [ + 'doctrine_encrypt.command.decrypt_database' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Command\DoctrineDecryptDatabaseCommand::class, + 'doctrine_encrypt.command.encrypt_database' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Command\DoctrineEncryptDatabaseCommand::class, + 'doctrine_encrypt.command.encrypt_status' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Command\DoctrineEncryptStatusCommand::class, + 'doctrine_encrypt.encryptor' => '%doctrine_encrypt.encryptor.class_name%', + 'doctrine_encrypt.secret.factory' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Factories\SecretFactory::class, + 'doctrine_encrypt.orm.subscriber' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Subscribers\DoctrineEncryptSubscriber::class, + ], + [ + 'doctrine_encrypt.annotations.reader' => 'annotations.reader', + ], + ]; + + yield 'secret-sf5-php7-orm2' => [ + [ + 'secret' => '', + ], + [ + 'isSymfony7OrHigher' => false, + 'isPhp8OrHigher' => false, + 'doctrineOrmIsVersion3' => false, + ], + [ + 'doctrine_encrypt.encryptor.class_name' => HaliteEncryptor::class, + 'doctrine_encrypt.supported_encryptors' => [ + 'halite' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Encryptors\DefuseEncryptor::class, + 'defuse' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Encryptors\HaliteEncryptor::class, + ], + 'doctrine_encrypt.secret' => '' + ], + [ + 'doctrine_encrypt.command.decrypt_database' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Command\DoctrineDecryptDatabaseCommand::class, + 'doctrine_encrypt.command.encrypt_database' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Command\DoctrineEncryptDatabaseCommand::class, + 'doctrine_encrypt.command.encrypt_status' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Command\DoctrineEncryptStatusCommand::class, + 'doctrine_encrypt.encryptor' => '%doctrine_encrypt.encryptor.class_name%', + 'doctrine_encrypt.orm.subscriber' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Subscribers\DoctrineEncryptSubscriber::class, + ], + [ + 'doctrine_encrypt.annotations.reader' => 'annotations.reader', + ], + ]; + + yield 'empty-sf5-php8-orm2' => [ + [], + [ + 'isSymfony7OrHigher' => false, + 'isPhp8OrHigher' => true, + 'doctrineOrmIsVersion3' => false, + ], + [ + 'doctrine_encrypt.encryptor.class_name' => HaliteEncryptor::class, + 'doctrine_encrypt.supported_encryptors' => [ + 'halite' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Encryptors\DefuseEncryptor::class, + 'defuse' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Encryptors\HaliteEncryptor::class, + ], + 'doctrine_encrypt.secret.enable_generation' => true, + 'doctrine_encrypt.secret.directory_path' => '%kernel.project_dir%', + ], + [ + 'doctrine_encrypt.command.decrypt_database' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Command\DoctrineDecryptDatabaseCommand::class, + 'doctrine_encrypt.command.encrypt_database' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Command\DoctrineEncryptDatabaseCommand::class, + 'doctrine_encrypt.command.encrypt_status' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Command\DoctrineEncryptStatusCommand::class, + 'doctrine_encrypt.encryptor' => '%doctrine_encrypt.encryptor.class_name%', + 'doctrine_encrypt.secret.factory' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Factories\SecretFactory::class, + 'doctrine_encrypt.orm.subscriber' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Subscribers\DoctrineEncryptSubscriber::class, + 'doctrine_encrypt.attributes.reader' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Mapping\AttributeReader::class, + 'doctrine_encrypt.annotations.reader' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Mapping\AttributeAnnotationReader::class, + ], + [ + ], + ]; + + yield 'empty-sf5-php8-orm3' => [ + [], + [ + 'isSymfony7OrHigher' => false, + 'isPhp8OrHigher' => true, + 'doctrineOrmIsVersion3' => true, + ], + [ + 'doctrine_encrypt.encryptor.class_name' => HaliteEncryptor::class, + 'doctrine_encrypt.supported_encryptors' => [ + 'halite' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Encryptors\DefuseEncryptor::class, + 'defuse' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Encryptors\HaliteEncryptor::class, + ], + 'doctrine_encrypt.secret.enable_generation' => true, + 'doctrine_encrypt.secret.directory_path' => '%kernel.project_dir%', + ], + [ + 'doctrine_encrypt.command.decrypt_database' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Command\DoctrineDecryptDatabaseCommand::class, + 'doctrine_encrypt.command.encrypt_database' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Command\DoctrineEncryptDatabaseCommand::class, + 'doctrine_encrypt.command.encrypt_status' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Command\DoctrineEncryptStatusCommand::class, + 'doctrine_encrypt.encryptor' => '%doctrine_encrypt.encryptor.class_name%', + 'doctrine_encrypt.secret.factory' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Factories\SecretFactory::class, + 'doctrine_encrypt.orm.subscriber' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Subscribers\DoctrineEncryptSubscriber::class, + 'doctrine_encrypt.attributes.reader' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Mapping\AttributeReader::class, + ], + [ + 'doctrine_encrypt.annotations.reader' => 'doctrine_encrypt.attributes.reader', + ], + ]; + + yield 'empty-sf7-php8-orm3' => [ + [], + [ + 'isSymfony7OrHigher' => true, + 'isPhp8OrHigher' => true, + 'doctrineOrmIsVersion3' => true, + ], + [ + 'doctrine_encrypt.encryptor.class_name' => HaliteEncryptor::class, + 'doctrine_encrypt.supported_encryptors' => [ + 'halite' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Encryptors\DefuseEncryptor::class, + 'defuse' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Encryptors\HaliteEncryptor::class, + ], + 'doctrine_encrypt.secret.enable_generation' => true, + 'doctrine_encrypt.secret.directory_path' => '%kernel.project_dir%', + ], + [ + 'doctrine_encrypt.command.decrypt_database' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Command\DoctrineDecryptDatabaseCommand::class, + 'doctrine_encrypt.command.encrypt_database' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Command\DoctrineEncryptDatabaseCommand::class, + 'doctrine_encrypt.command.encrypt_status' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Command\DoctrineEncryptStatusCommand::class, + 'doctrine_encrypt.encryptor' => '%doctrine_encrypt.encryptor.class_name%', + 'doctrine_encrypt.secret.factory' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Factories\SecretFactory::class, + 'doctrine_encrypt.orm.subscriber' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Subscribers\DoctrineEncryptSubscriber::class, + 'doctrine_encrypt.attributes.reader' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Mapping\AttributeReader::class, + ], + [ + 'doctrine_encrypt.annotations.reader' => 'doctrine_encrypt.attributes.reader', + ], + ]; + + yield 'secret-sf7-php8-orm3' => [ + [ + 'secret' => '', + ], + [ + 'isSymfony7OrHigher' => true, + 'isPhp8OrHigher' => true, + 'doctrineOrmIsVersion3' => true, + ], + [ + 'doctrine_encrypt.encryptor.class_name' => HaliteEncryptor::class, + 'doctrine_encrypt.supported_encryptors' => [ + 'halite' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Encryptors\DefuseEncryptor::class, + 'defuse' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Encryptors\HaliteEncryptor::class, + ], + 'doctrine_encrypt.secret' => '', + ], + [ + 'doctrine_encrypt.command.decrypt_database' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Command\DoctrineDecryptDatabaseCommand::class, + 'doctrine_encrypt.command.encrypt_database' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Command\DoctrineEncryptDatabaseCommand::class, + 'doctrine_encrypt.command.encrypt_status' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Command\DoctrineEncryptStatusCommand::class, + 'doctrine_encrypt.encryptor' => '%doctrine_encrypt.encryptor.class_name%', + 'doctrine_encrypt.orm.subscriber' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Subscribers\DoctrineEncryptSubscriber::class, + 'doctrine_encrypt.attributes.reader' => \DoctrineEncryptBundle\DoctrineEncryptBundle\Mapping\AttributeReader::class, + ], + [ + 'doctrine_encrypt.annotations.reader' => 'doctrine_encrypt.attributes.reader', + ], + ]; + } + + private function createContainer(): ContainerBuilder + { + $container = new ContainerBuilder( + new ParameterBag([ + 'kernel.debug' => false, + ]) + ); + + $container->setDefinition('annotations.reader', new Definition(AnnotationReader::class)); + + return $container; + } +} diff --git a/tests/Unit/DependencyInjection/DoctrineEncryptExtensionTest.php b/tests/Unit/DependencyInjection/DoctrineEncryptExtensionTest.php index b9a971a..7a3fb1f 100644 --- a/tests/Unit/DependencyInjection/DoctrineEncryptExtensionTest.php +++ b/tests/Unit/DependencyInjection/DoctrineEncryptExtensionTest.php @@ -3,11 +3,11 @@ namespace Ambta\DoctrineEncryptBundle\Tests\Unit\DependencyInjection; use Ambta\DoctrineEncryptBundle\DependencyInjection\DoctrineEncryptExtension; -use Ambta\DoctrineEncryptBundle\DependencyInjection\VersionTester; use Ambta\DoctrineEncryptBundle\Encryptors\DefuseEncryptor; use Ambta\DoctrineEncryptBundle\Encryptors\HaliteEncryptor; use Doctrine\Common\Annotations\AnnotationReader; use Doctrine\ORM\EntityManagerInterface; +use DoctrineEncryptBundle\DoctrineEncryptBundle\DependencyInjection\VersionTester; use ParagonIE\Halite\KeyFactory; use ParagonIE\HiddenString\HiddenString; use PHPUnit\Framework\TestCase; @@ -282,10 +282,12 @@ public function testConfigLoadsCorrectServicesAndParameters( $expectedServiceIds = array_merge( array_keys($expectedServices), array_keys($expectedAliases), - [ - \Psr\Container\ContainerInterface::class, - \Symfony\Component\DependencyInjection\ContainerInterface::class, - ] + PHP_MAJOR_VERSION < 8 + ? [ + \Psr\Container\ContainerInterface::class, + \Symfony\Component\DependencyInjection\ContainerInterface::class, + ] + : [] ); $this->assertEqualsCanonicalizing($expectedServiceIds, $container->getServiceIds()); @@ -321,10 +323,10 @@ public static function provideConfigLoadsCorrectServicesAndParametersCases(): it 'doctrineOrmIsVersion3' => false, ], [ - 'ambta_doctrine_encrypt.encryptor_class_name' => HaliteEncryptor::class, - 'ambta_doctrine_encrypt.supported_encryptors' => [ - 'halite' => \Ambta\DoctrineEncryptBundle\Encryptors\DefuseEncryptor::class, - 'defuse' => \Ambta\DoctrineEncryptBundle\Encryptors\HaliteEncryptor::class, + 'ambta_doctrine_encrypt.encryptor_class_name' => HaliteEncryptor::class, + 'ambta_doctrine_encrypt.supported_encryptors' => [ + 'halite' => DefuseEncryptor::class, + 'defuse' => HaliteEncryptor::class, ], 'ambta_doctrine_encrypt.enable_secret_generation' => true, 'ambta_doctrine_encrypt.secret_directory_path' => '%kernel.project_dir%', @@ -354,11 +356,11 @@ public static function provideConfigLoadsCorrectServicesAndParametersCases(): it ], [ 'ambta_doctrine_encrypt.encryptor_class_name' => HaliteEncryptor::class, - 'ambta_doctrine_encrypt.supported_encryptors' => [ - 'halite' => \Ambta\DoctrineEncryptBundle\Encryptors\DefuseEncryptor::class, - 'defuse' => \Ambta\DoctrineEncryptBundle\Encryptors\HaliteEncryptor::class, + 'ambta_doctrine_encrypt.supported_encryptors' => [ + 'halite' => DefuseEncryptor::class, + 'defuse' => HaliteEncryptor::class, ], - 'ambta_doctrine_encrypt.secret' => '' + 'ambta_doctrine_encrypt.secret' => '' ], [ 'ambta_doctrine_encrypt.command.decrypt.database' => \Ambta\DoctrineEncryptBundle\Command\DoctrineDecryptDatabaseCommand::class, @@ -381,10 +383,10 @@ public static function provideConfigLoadsCorrectServicesAndParametersCases(): it 'doctrineOrmIsVersion3' => false, ], [ - 'ambta_doctrine_encrypt.encryptor_class_name' => HaliteEncryptor::class, - 'ambta_doctrine_encrypt.supported_encryptors' => [ - 'halite' => \Ambta\DoctrineEncryptBundle\Encryptors\DefuseEncryptor::class, - 'defuse' => \Ambta\DoctrineEncryptBundle\Encryptors\HaliteEncryptor::class, + 'ambta_doctrine_encrypt.encryptor_class_name' => HaliteEncryptor::class, + 'ambta_doctrine_encrypt.supported_encryptors' => [ + 'halite' => DefuseEncryptor::class, + 'defuse' => HaliteEncryptor::class, ], 'ambta_doctrine_encrypt.enable_secret_generation' => true, 'ambta_doctrine_encrypt.secret_directory_path' => '%kernel.project_dir%', @@ -412,10 +414,10 @@ public static function provideConfigLoadsCorrectServicesAndParametersCases(): it 'doctrineOrmIsVersion3' => true, ], [ - 'ambta_doctrine_encrypt.encryptor_class_name' => HaliteEncryptor::class, - 'ambta_doctrine_encrypt.supported_encryptors' => [ - 'halite' => \Ambta\DoctrineEncryptBundle\Encryptors\DefuseEncryptor::class, - 'defuse' => \Ambta\DoctrineEncryptBundle\Encryptors\HaliteEncryptor::class, + 'ambta_doctrine_encrypt.encryptor_class_name' => HaliteEncryptor::class, + 'ambta_doctrine_encrypt.supported_encryptors' => [ + 'halite' => DefuseEncryptor::class, + 'defuse' => HaliteEncryptor::class, ], 'ambta_doctrine_encrypt.enable_secret_generation' => true, 'ambta_doctrine_encrypt.secret_directory_path' => '%kernel.project_dir%', @@ -443,10 +445,10 @@ public static function provideConfigLoadsCorrectServicesAndParametersCases(): it 'doctrineOrmIsVersion3' => true, ], [ - 'ambta_doctrine_encrypt.encryptor_class_name' => HaliteEncryptor::class, - 'ambta_doctrine_encrypt.supported_encryptors' => [ - 'halite' => \Ambta\DoctrineEncryptBundle\Encryptors\DefuseEncryptor::class, - 'defuse' => \Ambta\DoctrineEncryptBundle\Encryptors\HaliteEncryptor::class, + 'ambta_doctrine_encrypt.encryptor_class_name' => HaliteEncryptor::class, + 'ambta_doctrine_encrypt.supported_encryptors' => [ + 'halite' => DefuseEncryptor::class, + 'defuse' => HaliteEncryptor::class, ], 'ambta_doctrine_encrypt.enable_secret_generation' => true, 'ambta_doctrine_encrypt.secret_directory_path' => '%kernel.project_dir%', @@ -477,11 +479,11 @@ public static function provideConfigLoadsCorrectServicesAndParametersCases(): it ], [ 'ambta_doctrine_encrypt.encryptor_class_name' => HaliteEncryptor::class, - 'ambta_doctrine_encrypt.supported_encryptors' => [ - 'halite' => \Ambta\DoctrineEncryptBundle\Encryptors\DefuseEncryptor::class, - 'defuse' => \Ambta\DoctrineEncryptBundle\Encryptors\HaliteEncryptor::class, + 'ambta_doctrine_encrypt.supported_encryptors' => [ + 'halite' => DefuseEncryptor::class, + 'defuse' => HaliteEncryptor::class, ], - 'ambta_doctrine_encrypt.secret' => '', + 'ambta_doctrine_encrypt.secret' => '', ], [ 'ambta_doctrine_encrypt.command.decrypt.database' => \Ambta\DoctrineEncryptBundle\Command\DoctrineDecryptDatabaseCommand::class,