From 5e4350204c331328260ec998b854fbd9ad314baa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Deruss=C3=A9?= Date: Wed, 18 Sep 2024 14:18:55 +0200 Subject: [PATCH] Fix SymfonyContainer when both Credential and Cache are defined (#1764) --- CHANGELOG.md | 4 + src/DependencyInjection/AsyncAwsExtension.php | 36 +++---- tests/Functional/BundleInitializationTest.php | 94 +++++++++++++++++++ .../Resources/config/issue-1758/cache.yaml | 8 ++ .../Resources/config/issue-1758/empty.yaml | 1 + .../Resources/config/issue-1758/provider.yaml | 5 + .../config/issue-1758/provider_cache.yaml | 10 ++ 7 files changed, 141 insertions(+), 17 deletions(-) create mode 100644 tests/Functional/Resources/config/issue-1758/cache.yaml create mode 100644 tests/Functional/Resources/config/issue-1758/empty.yaml create mode 100644 tests/Functional/Resources/config/issue-1758/provider.yaml create mode 100644 tests/Functional/Resources/config/issue-1758/provider_cache.yaml diff --git a/CHANGELOG.md b/CHANGELOG.md index fc785ae..4075e37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ - Enable compiler optimization for the `sprintf` function. +### Fixed + +- Fix bundle configuration with both `credential_provider` and `credential_provider_cache`. + ## 1.12.2 ### Changed diff --git a/src/DependencyInjection/AsyncAwsExtension.php b/src/DependencyInjection/AsyncAwsExtension.php index 782afea..2ea9aa6 100644 --- a/src/DependencyInjection/AsyncAwsExtension.php +++ b/src/DependencyInjection/AsyncAwsExtension.php @@ -164,32 +164,34 @@ private function addServiceDefinition(ContainerBuilder $container, string $name, } } - // If no credential provider is specified, lets configured a credentials provider with cache. - $hasCacheClasses = class_exists(SymfonyCacheProvider::class) && interface_exists(CacheInterface::class); $credentialServiceId = $config['credential_provider']; - if (null === $credentialServiceId && null !== $config['credential_provider_cache'] && $hasCacheClasses) { + $credentialProviderCache = class_exists(SymfonyCacheProvider::class) && interface_exists(CacheInterface::class) ? $config['credential_provider_cache'] : null; + + if (null === $credentialServiceId) { $credentialServiceId = 'async_aws.credential'; if (!$container->hasDefinition($credentialServiceId)) { $container->register($credentialServiceId, CredentialProvider::class) ->setFactory([ChainProvider::class, 'createDefaultChain']) ->setArguments([$httpClient, $logger]) ->addTag('monolog.logger', ['channel' => 'async_aws']); + } + } - $container->register('async_aws.credential.cache', SymfonyCacheProvider::class) - ->setDecoratedService($credentialServiceId) - ->setArguments([ - new Reference('async_aws.credential.cache.inner'), - new Reference($config['credential_provider_cache']), - $logger, - ]) - ->addTag('monolog.logger', ['channel' => 'async_aws']); + if (null !== $credentialProviderCache) { + $container->register($credentialServiceId . '.cache', SymfonyCacheProvider::class) + ->setDecoratedService($credentialServiceId) + ->setArguments([ + new Reference($credentialServiceId . '.cache.inner'), + new Reference($credentialProviderCache), + $logger, + ]) + ->addTag('monolog.logger', ['channel' => 'async_aws']); - $container->register('async_aws.credential.memory', CacheProvider::class) - ->setDecoratedService($credentialServiceId) - ->setArguments([ - new Reference('async_aws.credential.memory.inner'), - ]); - } + $container->register($credentialServiceId . '.memory', CacheProvider::class) + ->setDecoratedService($credentialServiceId) + ->setArguments([ + new Reference($credentialServiceId . '.memory.inner'), + ]); } $definition = new Definition($clientClass); diff --git a/tests/Functional/BundleInitializationTest.php b/tests/Functional/BundleInitializationTest.php index 8f4ad4f..0fe522a 100644 --- a/tests/Functional/BundleInitializationTest.php +++ b/tests/Functional/BundleInitializationTest.php @@ -4,6 +4,10 @@ namespace AsyncAws\Symfony\Bundle\Tests\Functional; +use AsyncAws\Core\AbstractApi; +use AsyncAws\Core\Credentials\CacheProvider; +use AsyncAws\Core\Credentials\InstanceProvider; +use AsyncAws\Core\Credentials\SymfonyCacheProvider; use AsyncAws\S3\S3Client; use AsyncAws\Ses\SesClient; use AsyncAws\Sns\SnsClient; @@ -14,6 +18,7 @@ use Nyholm\BundleTest\TestKernel; use Symfony\Bundle\FrameworkBundle\EventListener\ConsoleProfilerListener; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Symfony\Component\Cache\Adapter\ApcuAdapter; use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; use Symfony\Component\HttpKernel\KernelInterface; @@ -152,6 +157,95 @@ public function testIssue793() self::assertSame('./docker/dynamodb/credentials', $x->getConfiguration()->get('sharedCredentialsFile')); } + public function testIssue1758Empty() + { + $this->bootWithConfig([ + 'issue-1758/empty.yaml', + ]); + + self::assertServiceExists('async_aws.client.s3', S3Client::class); + self::assertServiceExists('async_aws.credential', CacheProvider::class); + self::assertServiceExists('async_aws.credential.memory', CacheProvider::class); + self::assertServiceExists('async_aws.credential.cache', SymfonyCacheProvider::class); + } + + public function testIssue1758Cache() + { + $this->bootWithConfig([ + 'issue-1758/cache.yaml', + ]); + + self::assertServiceExists('async_aws.client.s3', S3Client::class); + self::assertServiceExists('async_aws.credential', CacheProvider::class); + self::assertServiceExists('async_aws.credential.memory', CacheProvider::class); + self::assertServiceExists('async_aws.credential.cache', SymfonyCacheProvider::class); + + $container = self::$kernel->getContainer(); + $cache = $container->get('async_aws.credential.cache'); + + $r = new \ReflectionObject($cache); + $p = $r->getProperty('cache'); + $p->setAccessible(true); + + $adapter = $p->getValue($cache); + self::assertInstanceOf(ApcuAdapter::class, $adapter); + } + + public function testIssue1758Provider() + { + $this->bootWithConfig([ + 'issue-1758/provider.yaml', + ]); + + self::assertServiceExists('async_aws.client.s3', S3Client::class); + + $container = self::$kernel->getContainer(); + $client = $container->get(S3Client::class); + + $r = new \ReflectionClass(AbstractApi::class); + $p = $r->getProperty('credentialProvider'); + $p->setAccessible(true); + + $credentialProvider = $p->getValue($client); + self::assertInstanceOf(InstanceProvider::class, $credentialProvider); + } + + public function testIssue1758ProviderAndCache() + { + $this->bootWithConfig([ + 'issue-1758/provider_cache.yaml', + ]); + + self::assertServiceExists('async_aws.client.s3', S3Client::class); + self::assertServiceExists(InstanceProvider::class . '.memory', CacheProvider::class); + self::assertServiceExists(InstanceProvider::class . '.cache', SymfonyCacheProvider::class); + + $container = self::$kernel->getContainer(); + $client = $container->get(S3Client::class); + + $r = new \ReflectionClass(AbstractApi::class); + $p = $r->getProperty('credentialProvider'); + $p->setAccessible(true); + + $credentialProvider = $p->getValue($client); + self::assertInstanceOf(CacheProvider::class, $credentialProvider); + + $cache = $container->get(InstanceProvider::class . '.cache'); + + $r = new \ReflectionObject($cache); + $p = $r->getProperty('cache'); + $p->setAccessible(true); + + $adapter = $p->getValue($cache); + self::assertInstanceOf(ApcuAdapter::class, $adapter); + + $p = $r->getProperty('decorated'); + $p->setAccessible(true); + + $decorated = $p->getValue($cache); + self::assertInstanceOf(InstanceProvider::class, $decorated); + } + protected static function getKernelClass(): string { return TestKernel::class; diff --git a/tests/Functional/Resources/config/issue-1758/cache.yaml b/tests/Functional/Resources/config/issue-1758/cache.yaml new file mode 100644 index 0000000..c82e895 --- /dev/null +++ b/tests/Functional/Resources/config/issue-1758/cache.yaml @@ -0,0 +1,8 @@ +framework: + cache: + pools: + test: + adapter: cache.adapter.apcu + +async_aws: + credential_provider_cache: test diff --git a/tests/Functional/Resources/config/issue-1758/empty.yaml b/tests/Functional/Resources/config/issue-1758/empty.yaml new file mode 100644 index 0000000..1cdd8d3 --- /dev/null +++ b/tests/Functional/Resources/config/issue-1758/empty.yaml @@ -0,0 +1 @@ +async_aws: diff --git a/tests/Functional/Resources/config/issue-1758/provider.yaml b/tests/Functional/Resources/config/issue-1758/provider.yaml new file mode 100644 index 0000000..798ea86 --- /dev/null +++ b/tests/Functional/Resources/config/issue-1758/provider.yaml @@ -0,0 +1,5 @@ +async_aws: + credential_provider: AsyncAws\Core\Credentials\InstanceProvider + credential_provider_cache: null +services: + AsyncAws\Core\Credentials\InstanceProvider: ~ diff --git a/tests/Functional/Resources/config/issue-1758/provider_cache.yaml b/tests/Functional/Resources/config/issue-1758/provider_cache.yaml new file mode 100644 index 0000000..319737b --- /dev/null +++ b/tests/Functional/Resources/config/issue-1758/provider_cache.yaml @@ -0,0 +1,10 @@ +framework: + cache: + pools: + test: + adapter: cache.adapter.apcu +async_aws: + credential_provider: AsyncAws\Core\Credentials\InstanceProvider + credential_provider_cache: test +services: + AsyncAws\Core\Credentials\InstanceProvider: ~