Skip to content

SQS Transport - Add support for AWS profiles. #1008

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 2 commits into from
Jan 8, 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
2 changes: 1 addition & 1 deletion pkg/snsqs/Tests/SnsQsProducerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testCouldBeConstructedWithRequiredArguments()
public function testShouldThrowIfMessageIsInvalidType()
{
$this->expectException(InvalidMessageException::class);
$this->expectExceptionMessage('The message must be an instance of Enqueue\SnsQs\SnsQsMessage but it is Double\Message\P1');
$this->expectExceptionMessage('The message must be an instance of Enqueue\SnsQs\SnsQsMessage but it is Double\Message\P4');

$producer = new SnsQsProducer($this->createSnsContextMock(), $this->createSqsContextMock());

Expand Down
14 changes: 9 additions & 5 deletions pkg/sqs/SqsConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class SqsConnectionFactory implements ConnectionFactory
* 'retries' => 3, (int, default=int(3)) Configures the maximum number of allowed retries for a client (pass 0 to disable retries).
* 'version' => '2012-11-05', (string, required) The version of the webservice to utilize
* 'lazy' => true, Enable lazy connection (boolean)
* 'endpoint' => null (string, default=null) The full URI of the webservice. This is only required when connecting to a custom endpoint e.g. localstack
* 'endpoint' => null, (string, default=null) The full URI of the webservice. This is only required when connecting to a custom endpoint e.g. localstack
* 'profile' => null, (string, default=null) The name of an AWS profile to used, if provided the SDK will attempt to read associated credentials from the ~/.aws/credentials file.
* 'queue_owner_aws_account_id' The AWS account ID of the account that created the queue.
* ].
*
Expand Down Expand Up @@ -92,6 +93,10 @@ private function establishConnection(): SqsClient
$config['endpoint'] = $this->config['endpoint'];
}

if (isset($this->config['profile'])) {
$config['profile'] = $this->config['profile'];
}

if ($this->config['key'] && $this->config['secret']) {
$config['credentials'] = [
'key' => $this->config['key'],
Expand Down Expand Up @@ -120,10 +125,7 @@ private function parseDsn(string $dsn): array
$dsn = Dsn::parseFirst($dsn);

if ('sqs' !== $dsn->getSchemeProtocol()) {
throw new \LogicException(sprintf(
'The given scheme protocol "%s" is not supported. It must be "sqs"',
$dsn->getSchemeProtocol()
));
throw new \LogicException(sprintf('The given scheme protocol "%s" is not supported. It must be "sqs"', $dsn->getSchemeProtocol()));
}

return array_filter(array_replace($dsn->getQuery(), [
Expand All @@ -135,6 +137,7 @@ private function parseDsn(string $dsn): array
'version' => $dsn->getString('version'),
'lazy' => $dsn->getBool('lazy'),
'endpoint' => $dsn->getString('endpoint'),
'profile' => $dsn->getString('profile'),
'queue_owner_aws_account_id' => $dsn->getString('queue_owner_aws_account_id'),
]), function ($value) { return null !== $value; });
}
Expand All @@ -150,6 +153,7 @@ private function defaultConfig(): array
'version' => '2012-11-05',
'lazy' => true,
'endpoint' => null,
'profile' => null,
'queue_owner_aws_account_id' => null,
];
}
Expand Down
41 changes: 41 additions & 0 deletions pkg/sqs/Tests/SqsConnectionFactoryConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public static function provideConfigs()
'version' => '2012-11-05',
'lazy' => true,
'endpoint' => null,
'profile' => null,
'queue_owner_aws_account_id' => null,
],
];
Expand All @@ -78,6 +79,7 @@ public static function provideConfigs()
'version' => '2012-11-05',
'lazy' => true,
'endpoint' => null,
'profile' => null,
'queue_owner_aws_account_id' => null,
],
];
Expand All @@ -93,6 +95,7 @@ public static function provideConfigs()
'version' => '2012-11-05',
'lazy' => true,
'endpoint' => null,
'profile' => null,
'queue_owner_aws_account_id' => null,
],
];
Expand All @@ -108,6 +111,7 @@ public static function provideConfigs()
'version' => '2012-11-05',
'lazy' => false,
'endpoint' => null,
'profile' => null,
'queue_owner_aws_account_id' => null,
],
];
Expand All @@ -123,6 +127,23 @@ public static function provideConfigs()
'version' => '2012-11-05',
'lazy' => false,
'endpoint' => null,
'profile' => null,
'queue_owner_aws_account_id' => null,
],
];

yield [
['dsn' => 'sqs:?profile=staging&lazy=0'],
[
'key' => null,
'secret' => null,
'token' => null,
'region' => null,
'retries' => 3,
'version' => '2012-11-05',
'lazy' => false,
'endpoint' => null,
'profile' => 'staging',
'queue_owner_aws_account_id' => null,
],
];
Expand All @@ -138,6 +159,7 @@ public static function provideConfigs()
'version' => '2012-11-05',
'lazy' => false,
'endpoint' => null,
'profile' => null,
'queue_owner_aws_account_id' => null,
],
];
Expand All @@ -159,6 +181,25 @@ public static function provideConfigs()
'version' => '2012-11-05',
'lazy' => false,
'endpoint' => 'http://localstack:1111',
'profile' => null,
'queue_owner_aws_account_id' => null,
],
];

yield [
[
'profile' => 'staging',
],
[
'key' => null,
'secret' => null,
'token' => null,
'region' => null,
'retries' => 3,
'version' => '2012-11-05',
'lazy' => true,
'endpoint' => null,
'profile' => 'staging',
'queue_owner_aws_account_id' => null,
],
];
Expand Down
2 changes: 2 additions & 0 deletions pkg/sqs/Tests/SqsConnectionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function testCouldBeConstructedWithEmptyConfiguration()
'retries' => 3,
'version' => '2012-11-05',
'endpoint' => null,
'profile' => null,
'queue_owner_aws_account_id' => null,
], 'config', $factory);
}
Expand All @@ -49,6 +50,7 @@ public function testCouldBeConstructedWithCustomConfiguration()
'retries' => 3,
'version' => '2012-11-05',
'endpoint' => null,
'profile' => null,
'queue_owner_aws_account_id' => null,
], 'config', $factory);
}
Expand Down