8000 [Messenger] Fix invalid option sslmode in AmazonSqs bridge by jderusse · Pull Request #37654 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] Fix invalid option sslmode in AmazonSqs bridge #37654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ public function testFromDsnWithCustomEndpoint()
);
}

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();
Expand Down Expand Up @@ -149,6 +167,30 @@ public function testFromDsnWithAccountAndEndpointOption()
);
}

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Connection
'region' => 'eu-west-1',
'queue_name' => 'messages',
'account' => null,
'sslmode' => null,
];

private $configuration;
Expand Down Expand Up @@ -94,6 +95,19 @@ public static function fromDsn(string $dsn, array $options = [], HttpClientInter
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));
Copy link
Member Author
@jderusse jderusse Jul 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved the checks before the merge of $options = $query + $options to distinguish errors in option and dsn.
maybe it doesn't worth it, and we should just keep the first check on 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'],
Expand All @@ -116,7 +130,6 @@ public static function fromDsn(string $dsn, array $options = [], HttpClientInter
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'];
}
Expand All @@ -127,18 +140,6 @@ public static function fromDsn(string $dsn, array $options = [], HttpClientInter
}
$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));
}

Expand Down
0