Skip to content

Commit

Permalink
Fix SymfonyContainer when both Credential and Cache are defined (#1764)
Browse files Browse the repository at this point in the history
  • Loading branch information
jderusse authored Sep 18, 2024
1 parent dc845bd commit 5e43502
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 19 additions & 17 deletions src/DependencyInjection/AsyncAwsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
94 changes: 94 additions & 0 deletions tests/Functional/BundleInitializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions tests/Functional/Resources/config/issue-1758/cache.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
framework:
cache:
pools:
test:
adapter: cache.adapter.apcu

async_aws:
credential_provider_cache: test
1 change: 1 addition & 0 deletions tests/Functional/Resources/config/issue-1758/empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
async_aws:
5 changes: 5 additions & 0 deletions tests/Functional/Resources/config/issue-1758/provider.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
async_aws:
credential_provider: AsyncAws\Core\Credentials\InstanceProvider
credential_provider_cache: null
services:
AsyncAws\Core\Credentials\InstanceProvider: ~
10 changes: 10 additions & 0 deletions tests/Functional/Resources/config/issue-1758/provider_cache.yaml
Original file line number Diff line number Diff line change
@@ -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: ~

0 comments on commit 5e43502

Please sign in to comment.