From c1e0869cbe999276630252b24a27632e71d54902 Mon Sep 17 00:00:00 2001 From: Patrick Florek Date: Fri, 18 Nov 2022 17:37:52 +0100 Subject: [PATCH 1/4] fix: check if aws key and secret are not empty if using `AWS_ACCESS_KEY_ID=''` or `AWS_SECRET_ACCESS_KEY=''` an exception will be thrown ``` Error executing "PutEvents" on "https://events.us-east-1.amazonaws.com"; AWS HTTP error: Client error: `POST https://events.us-east-1.amazonaws.com` resulted in a `400 Bad Request` response: {"__type":"UnrecognizedClientException","message":"The security token included in the request is invalid."} ``` --- src/EventServiceProvider.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/EventServiceProvider.php b/src/EventServiceProvider.php index d2afe14..5bd801c 100644 --- a/src/EventServiceProvider.php +++ b/src/EventServiceProvider.php @@ -124,8 +124,6 @@ public static function prepareConfigurationCredentials(array $config): array */ private static function configHasCredentials(array $config): bool { - return Arr::has($config, ['key', 'secret']) - && is_string(Arr::get($config, 'key')) - && is_string(Arr::get($config, 'secret')); + return ! empty($config['key']) && ! empty($config['secret']); } } From 38e418649507db7df6a104ed8f8c79347bbe8260 Mon Sep 17 00:00:00 2001 From: clemblanco Date: Tue, 7 Feb 2023 10:33:24 +0100 Subject: [PATCH 2/4] WIP --- composer.json | 2 +- src/EventServiceProvider.php | 4 +- tests/EventServiceProviderTest.php | 130 +++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 tests/EventServiceProviderTest.php diff --git a/composer.json b/composer.json index 8aed55f..a304679 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "pod-point/laravel-aws-pubsub", - "description": "A Laravel broadcasting driver and queue driver that broadcasts and listens to events published to AWS SNS and SQS.", + "description": "A Laravel broadcasting driver and queue driver that broadcasts and listens to published events utilising AWS SNS, EventBridge and SQS.", "keywords": ["laravel", "broadcasting", "broadcast", "queue", "listeners", "pubsub", "aws", "sns", "sqs"], "homepage": "https://github.com/pod-point/laravel-aws-pubsub", "license": "MIT", diff --git a/src/EventServiceProvider.php b/src/EventServiceProvider.php index 5bd801c..273ff63 100644 --- a/src/EventServiceProvider.php +++ b/src/EventServiceProvider.php @@ -124,6 +124,8 @@ public static function prepareConfigurationCredentials(array $config): array */ private static function configHasCredentials(array $config): bool { - return ! empty($config['key']) && ! empty($config['secret']); + return Arr::has($config, ['key', 'secret']) + && Arr::get($config, 'key') + && Arr::get($config, 'secret'); } } diff --git a/tests/EventServiceProviderTest.php b/tests/EventServiceProviderTest.php new file mode 100644 index 0000000..f6e3d6a --- /dev/null +++ b/tests/EventServiceProviderTest.php @@ -0,0 +1,130 @@ + 'bar', + 'key' => 'some_key', + 'secret' => 'some_secret', + ]); + + $this->assertEquals([ + 'foo' => 'bar', + 'key' => 'some_key', + 'secret' => 'some_secret', + 'credentials' => [ + 'key' => 'some_key', + 'secret' => 'some_secret', + ], + ], $config); + } + + /** @test */ + public function it_can_prepare_configuration_credentials_with_a_token_too() + { + $config = EventServiceProvider::prepareConfigurationCredentials([ + 'foo' => 'bar', + 'key' => 'some_key', + 'secret' => 'some_secret', + 'token' => 'some_token', + ]); + + $this->assertEquals([ + 'foo' => 'bar', + 'key' => 'some_key', + 'secret' => 'some_secret', + 'token' => 'some_token', + 'credentials' => [ + 'key' => 'some_key', + 'secret' => 'some_secret', + 'token' => 'some_token', + ], + ], $config); + } + + /** @test */ + public function it_can_make_sure_some_aws_credentials_are_provided_before_preparing_the_credentials() + { + $config = EventServiceProvider::prepareConfigurationCredentials([ + 'foo' => 'bar', + 'token' => 'some_token', + ]); + + $this->assertArrayNotHasKey('credentials', $config); + } + + public function invalidCredentialsDataProvider() + { + return [ + 'key_is_empty' => [ + 'creds' => [ + 'key' => '', + 'secret' => 'some_secret', + ], + ], + 'secret_is_empty' => [ + 'creds' => [ + 'key' => 'some_key', + 'secret' => '', + ], + ], + 'key_and_secret_are_empty' => [ + 'creds' => [ + 'key' => '', + 'secret' => '', + ], + ], + 'key_is_null' => [ + 'creds' => [ + 'key' => null, + 'secret' => 'some_secret', + ], + ], + 'secret_is_null' => [ + 'creds' => [ + 'key' => 'some_key', + 'secret' => null, + ], + ], + 'key_and_secret_are_null' => [ + 'creds' => [ + 'key' => null, + 'secret' => null, + ], + ], + 'key_is_empty_and_secret_is_null' => [ + 'creds' => [ + 'key' => '', + 'secret' => null, + ], + ], + 'key_is_null_and_secret_is_empty' => [ + 'creds' => [ + 'key' => null, + 'secret' => '', + ], + ], + ]; + } + + /** + * @test + * @dataProvider invalidCredentialsDataProvider + */ + public function it_can_make_sure_some_aws_credentials_are_provided_and_valid(array $invalidCredentials) + { + $config = EventServiceProvider::prepareConfigurationCredentials(array_merge([ + 'foo' => 'bar', + ], $invalidCredentials)); + + $this->assertArrayHasKey('foo', $config); + $this->assertArrayNotHasKey('credentials', $config); + } +} From de6938cfbbdaa8af72e89ae28e86cc509ec4e1a2 Mon Sep 17 00:00:00 2001 From: clemblanco Date: Tue, 7 Feb 2023 11:10:56 +0100 Subject: [PATCH 3/4] WIP --- tests/Sub/Concerns/MocksNotificationMessages.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Sub/Concerns/MocksNotificationMessages.php b/tests/Sub/Concerns/MocksNotificationMessages.php index f1ee22d..30bd33d 100644 --- a/tests/Sub/Concerns/MocksNotificationMessages.php +++ b/tests/Sub/Concerns/MocksNotificationMessages.php @@ -8,9 +8,9 @@ private function mockedRichNotificationMessage(array $attributes = []): \Aws\Res { $attributes = array_merge([ 'Type' => 'Notification', - 'TopicArn' => $this->faker->word, + 'TopicArn' => $this->faker->word(), 'Message' => json_encode(['foo' => 'bar']), - 'MessageId' => $this->faker->uuid, + 'MessageId' => $this->faker->uuid(), ], $attributes); return new \Aws\Result([ From 37bd83bbdf72ec09f2ae57bb436281ad974f7aa3 Mon Sep 17 00:00:00 2001 From: clemblanco Date: Tue, 7 Feb 2023 14:46:02 +0100 Subject: [PATCH 4/4] WIP --- tests/EventServiceProviderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/EventServiceProviderTest.php b/tests/EventServiceProviderTest.php index f6e3d6a..3b12c72 100644 --- a/tests/EventServiceProviderTest.php +++ b/tests/EventServiceProviderTest.php @@ -27,7 +27,7 @@ public function it_can_prepare_configuration_credentials() } /** @test */ - public function it_can_prepare_configuration_credentials_with_a_token_too() + public function it_can_prepare_configuration_credentials_with_a_token() { $config = EventServiceProvider::prepareConfigurationCredentials([ 'foo' => 'bar',