8000 Fix invalid option sslmode · symfony/symfony@cea413e · GitHub
[go: up one dir, main page]

Skip to content

Commit cea413e

Browse files
committed
Fix invalid option sslmode
1 parent ea25825 commit cea413e

File tree

2 files changed

+58
-15
lines changed

2 files changed

+58
-15
lines changed

src/Symfony/Component/Messenger/Bridge/AmazonSqs/Tests/Transport/ConnectionTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,24 @@ public function testFromDsnWithCustomEndpoint()
9797
);
9898
}
9999

100+
public function testFromDsnWithSslMode()
101+
{
102+
$httpClient = $this->getMockBuilder(HttpClientInterface::class)->getMock();
103+
$this->assertEquals(
104+
new Connection(['queue_name' => 'queue'], new SqsClient(['region' => 'eu-west-1', 'endpoint' => 'http://localhost', 'accessKeyId' => null, 'accessKeySecret' => null], null, $httpClient)),
105+
Connection::fromDsn('sqs://localhost/queue?sslmode=disable', [], $httpClient)
106+
);
107+
}
108+
109+
public function testFromDsnWithSslModeOnDefault()
110+
{
111+
$httpClient = $this->getMockBuilder(HttpClientInterface::class)->getMock();
112+
$this->assertEquals(
113+
new Connection(['queue_name' => 'queue'], new SqsClient(['region' => 'eu-west-1', 'accessKeyId' => null, 'accessKeySecret' => null], null, $httpClient)),
114+
Connection::fromDsn('sqs://default/queue?sslmode=disable', [], $httpClient)
115+
);
116+
}
117+
100118
public function testFromDsnWithCustomEndpointAndPort()
101119
{
102120
$httpClient = $this->getMockBuilder(HttpClientInterface::class)->getMock();
@@ -149,6 +167,31 @@ public function testFromDsnWithAccountAndEndpointOption()
149167
);
150168
}
151169

170+
public function testFromDsnWithInvalidQueryString()
171+
{
172+
$this->expectException(\InvalidArgumentException::class);
173+
$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].');
174+
175+
Connection::fromDsn('sqs://default?foo=foo');
176+
}
177+
178+
public function testFromDsnWithInvalidOption()
179+
{
180+
$this->expectException(\InvalidArgumentException::class);
181+
$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].');
182+
183+
Connection::fromDsn('sqs://default', ['bar' => 'bar']);
184+
}
185+
186+
public function testFromDsnWithInvalidQueryStringAndOption()
187+
{
188+
$this->expectException(\InvalidArgumentException::class);
189+
$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].');
190+
191+
Connection::fromDsn('sqs://default?foo=foo', ['bar' => 'bar']);
192+
}
193+
194+
152195
public function testKeepGettingPendingMessages()
153196
{
154197
$client = $this->createMock(SqsClient::class);

src/Symfony/Component/Messenger/Bridge/AmazonSqs/Transport/Connection.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,20 @@ public static function fromDsn(string $dsn, array $options = [], HttpClientInter
9494
if (isset($parsedUrl['query'])) {
9595
parse_str($parsedUrl['query'], $query);
9696
}
97-
$options = $query + $options + self::DEFAULT_OPTIONS;
97+
$mergedOptions = $query + $options + self::DEFAULT_OPTIONS;
9898
$configuration = [
99-
'buffer_size' => (int) $options['buffer_size'],
100-
'wait_time' => (int) $options['wait_time'],
101-
'poll_timeout' => $options['poll_timeout'],
102-
'visibility_timeout' => $options['visibility_timeout'],
103-
'auto_setup' => (bool) $options['auto_setup'],
104-
'queue_name' => (string) $options['queue_name'],
99+
'buffer_size' => (int) $mergedOptions['buffer_size'],
100+
'wait_time' => (int) $mergedOptions['wait_time'],
101+
'poll_timeout' => $mergedOptions['poll_timeout'],
102+
'visibility_timeout' => $mergedOptions['visibility_timeout'],
103+
'auto_setup' => (bool) $mergedOptions['auto_setup'],
104+
'queue_name' => (string) $mergedOptions['queue_name'],
105105
];
106106

107107
$clientConfiguration = [
108-
'region' => $options['region'],
109-
'accessKeyId' => urldecode($parsedUrl['user'] ?? '') ?: $options['access_key'] ?? self::DEFAULT_OPTIONS['access_key'],
110-
'accessKeySecret' => urldecode($parsedUrl['pass'] ?? '') ?: $options['secret_key'] ?? self::DEFAULT_OPTIONS['secret_key'],
108+
'region' => $mergedOptions['region'],
109+
'accessKeyId' => urldecode($parsedUrl['user'] ?? '') ?: $mergedOptions['access_key'] ?? self::DEFAULT_OPTIONS['access_key'],
110+
'accessKeySecret' => urldecode($parsedUrl['pass'] ?? '') ?: $mergedOptions['secret_key'] ?? self::DEFAULT_OPTIONS['secret_key'],
111111
];
112112
unset($query['region']);
113113

@@ -116,21 +116,21 @@ public static function fromDsn(string $dsn, array $options = [], HttpClientInter
116116
if (preg_match(';^sqs\.([^\.]++)\.amazonaws\.com$;', $parsedUrl['host'], $matches)) {
117117
$clientConfiguration['region'] = $matches[1];
118118
}
119-
unset($query['sslmode']);
120-
} elseif (self::DEFAULT_OPTIONS['endpoint'] !== $options['endpoint'] ?? self::DEFAULT_OPTIONS['endpoint']) {
121-
$clientConfiguration['endpoint'] = $options['endpoint'];
119+
} elseif (self::DEFAULT_OPTIONS['endpoint'] !== $mergedOptions['endpoint'] ?? self::DEFAULT_OPTIONS['endpoint']) {
120+
$clientConfiguration['endpoint'] = $mergedOptions['endpoint'];
122121
}
122+
unset($query['sslmode']);
123123

124124
$parsedPath = explode('/', ltrim($parsedUrl['path'] ?? '/', '/'));
125125
if (\count($parsedPath) > 0 && !empty($queueName = end($parsedPath))) {
126126
$configuration['queue_name'] = $queueName;
127127
}
128-
$configuration['account'] = 2 === \count($parsedPath) ? $parsedPath[0] : $options['account'] ?? self::DEFAULT_OPTIONS['account'];
128+
$configuration['account'] = 2 === \count($parsedPath) ? $parsedPath[0] : $mergedOptions['account'] ?? self::DEFAULT_OPTIONS['account'];
129129

130130
// check for extra keys in options
131131
$optionsExtraKeys = array_diff(array_keys($options), array_keys(self::DEFAULT_OPTIONS));
132132
if (0 < \count($optionsExtraKeys)) {
133-
throw new InvalidArgumentException(sprintf('Unknown option found : [%s]. Allowed options are [%s].', implode(', ', $optionsExtraKeys), implode(', ', array_keys(self::DEFAULT_OPTIONS))));
133+
throw new InvalidArgumentException(sprintf('Unknown option found: [%s]. Allowed options are [%s].', implode(', ', $optionsExtraKeys), implode(', ', array_keys(self::DEFAULT_OPTIONS))));
134134
}
135135

136136
// check for extra keys in options

0 commit comments

Comments
 (0)
0