From afbd51b368651b732e033303f91dbc87e6365bf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Deruss=C3=A9?= Date: Fri, 24 Jul 2020 12:11:03 +0200 Subject: [PATCH] Fix invalid option sslmode --- .../Tests/Transport/ConnectionTest.php | 42 +++++++++++++++++++ .../Bridge/AmazonSqs/Transport/Connection.php | 27 ++++++------ 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/src/Symfony/Component/Messenger/Bridge/AmazonSqs/Tests/Transport/ConnectionTest.php b/src/Symfony/Component/Messenger/Bridge/AmazonSqs/Tests/Transport/ConnectionTest.php index 447988ec8b..8d1ccdfe44 100644 --- a/src/Symfony/Component/Messenger/Bridge/AmazonSqs/Tests/Transport/ConnectionTest.php +++ b/src/Symfony/Component/Messenger/Bridge/AmazonSqs/Tests/Transport/ConnectionTest.php @@ -97,6 +97,24 @@ class ConnectionTest extends TestCase ); } + public function testFromDsnWithSslMode() + { + $httpClient = $this->getMockBuilder(HttpClientInterface::class)->getMock(); + $this->assertEquals( + new Connection(['queue_name' => 'queue'], new SqsClient(['region' => 'eu-west-1', 'endpoint' => 'http://localhost', 'accessKeyId' => null, 'accessKeySecret' => null], null, $httpClient)), + Connection::fromDsn('sqs://localhost/queue?sslmode=disable', [], $httpClient) + ); + } + + public function testFromDsnWithSslModeOnDefault() + { + $httpClient = $this->getMockBuilder(HttpClientInterface::class)->getMock(); + $this->assertEquals( + new Connection(['queue_name' => 'queue'], new SqsClient(['region' => 'eu-west-1', 'accessKeyId' => null, 'accessKeySecret' => null], null, $httpClient)), + Connection::fromDsn('sqs://default/queue?sslmode=disable', [], $httpClient) + ); + } + public function testFromDsnWithCustomEndpointAndPort() { $httpClient = $this->getMockBuilder(HttpClientInterface::class)->getMock(); @@ -149,6 +167,30 @@ class ConnectionTest extends TestCase ); } + public function testFromDsnWithInvalidQueryString() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Unknown option found in DSN: [foo]. Allowed options are [buffer_size, wait_time, poll_timeout, visibility_timeout, auto_setup, access_key, secret_key, endpoint, region, queue_name, account, sslmode].'); + + Connection::fromDsn('sqs://default?foo=foo'); + } + + public function testFromDsnWithInvalidOption() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Unknown option found: [bar]. Allowed options are [buffer_size, wait_time, poll_timeout, visibility_timeout, auto_setup, access_key, secret_key, endpoint, region, queue_name, account, sslmode].'); + + Connection::fromDsn('sqs://default', ['bar' => 'bar']); + } + + public function testFromDsnWithInvalidQueryStringAndOption() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Unknown option found: [bar]. Allowed options are [buffer_size, wait_time, poll_timeout, visibility_timeout, auto_setup, access_key, secret_key, endpoint, region, queue_name, account, sslmode].'); + + Connection::fromDsn('sqs://default?foo=foo', ['bar' => 'bar']); + } + public function testKeepGettingPendingMessages() { $client = $this->createMock(SqsClient::class); diff --git a/src/Symfony/Component/Messenger/Bridge/AmazonSqs/Transport/Connection.php b/src/Symfony/Component/Messenger/Bridge/AmazonSqs/Transport/Connection.php index 29bb1006fe..da72a795da 100644 --- a/src/Symfony/Component/Messenger/Bridge/AmazonSqs/Transport/Connection.php +++ b/src/Symfony/Component/Messenger/Bridge/AmazonSqs/Transport/Connection.php @@ -44,6 +44,7 @@ class Connection 'region' => 'eu-west-1', 'queue_name' => 'messages', 'account' => null, + 'sslmode' => null, ]; private $configuration; @@ -94,6 +95,19 @@ class Connection if (isset($parsedUrl['query'])) { parse_str($parsedUrl['query'], $query); } + + // check for extra keys in options + $optionsExtraKeys = array_diff(array_keys($options), array_keys(self::DEFAULT_OPTIONS)); + if (0 < \count($optionsExtraKeys)) { + throw new InvalidArgumentException(sprintf('Unknown option found: [%s]. Allowed options are [%s].', implode(', ', $optionsExtraKeys), implode(', ', array_keys(self::DEFAULT_OPTIONS)))); + } + + // check for extra keys in options + $queryExtraKeys = array_diff(array_keys($query), array_keys(self::DEFAULT_OPTIONS)); + if (0 < \count($queryExtraKeys)) { + throw new InvalidArgumentException(sprintf('Unknown option found in DSN: [%s]. Allowed options are [%s].', implode(', ', $queryExtraKeys), implode(', ', array_keys(self::DEFAULT_OPTIONS)))); + } + $options = $query + $options + self::DEFAULT_OPTIONS; $configuration = [ 'buffer_size' => (int) $options['buffer_size'], @@ -116,7 +130,6 @@ class Connection if (preg_match(';^sqs\.([^\.]++)\.amazonaws\.com$;', $parsedUrl['host'], $matches)) { $clientConfiguration['region'] = $matches[1]; } - unset($query['sslmode']); } elseif (self::DEFAULT_OPTIONS['endpoint'] !== $options['endpoint'] ?? self::DEFAULT_OPTIONS['endpoint']) { $clientConfiguration['endpoint'] = $options['endpoint']; } @@ -127,18 +140,6 @@ class Connection } $configuration['account'] = 2 === \count($parsedPath) ? $parsedPath[0] : $options['account'] ?? self::DEFAULT_OPTIONS['account']; - // check for extra keys in options - $optionsExtraKeys = array_diff(array_keys($options), array_keys(self::DEFAULT_OPTIONS)); - if (0 < \count($optionsExtraKeys)) { - throw new InvalidArgumentException(sprintf('Unknown option found : [%s]. Allowed options are [%s].', implode(', ', $optionsExtraKeys), implode(', ', array_keys(self::DEFAULT_OPTIONS)))); - } - - // check for extra keys in options - $queryExtraKeys = array_diff(array_keys($query), array_keys(self::DEFAULT_OPTIONS)); - if (0 < \count($queryExtraKeys)) { - throw new InvalidArgumentException(sprintf('Unknown option found in DSN: [%s]. Allowed options are [%s].', implode(', ', $queryExtraKeys), implode(', ', array_keys(self::DEFAULT_OPTIONS)))); - } - return new self($configuration, new SqsClient($clientConfiguration, null, $client)); }