diff --git a/src/Aws/Common/Client/AbstractClient.php b/src/Aws/Common/Client/AbstractClient.php index 63a46832f7..d01b0f2e57 100644 --- a/src/Aws/Common/Client/AbstractClient.php +++ b/src/Aws/Common/Client/AbstractClient.php @@ -83,9 +83,7 @@ public function __construct(CredentialsInterface $credentials, SignatureInterfac // Add the event listener so that requests are signed before they are sent $dispatcher = $this->getEventDispatcher(); - if (!$credentials instanceof NullCredentials) { - $dispatcher->addSubscriber(new SignatureListener($credentials, $signature)); - } + $dispatcher->addSubscriber(new SignatureListener($credentials, $signature)); if ($backoff = $config->get(Options::BACKOFF)) { $dispatcher->addSubscriber($backoff, -255); diff --git a/src/Aws/Common/Signature/SignatureListener.php b/src/Aws/Common/Signature/SignatureListener.php index d24d252000..e53f36718f 100644 --- a/src/Aws/Common/Signature/SignatureListener.php +++ b/src/Aws/Common/Signature/SignatureListener.php @@ -17,6 +17,7 @@ namespace Aws\Common\Signature; use Aws\Common\Credentials\CredentialsInterface; +use Aws\Common\Credentials\NullCredentials; use Guzzle\Common\Event; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -75,6 +76,8 @@ public function onCredentialsChanged(Event $event) */ public function onRequestBeforeSend(Event $event) { - $this->signature->signRequest($event['request'], $this->credentials); + if(!$this->credentials instanceof NullCredentials) { + $this->signature->signRequest($event['request'], $this->credentials); + } } } diff --git a/tests/Aws/Tests/Common/Client/AbstractClientTest.php b/tests/Aws/Tests/Common/Client/AbstractClientTest.php index 584d168569..31885c2a0f 100644 --- a/tests/Aws/Tests/Common/Client/AbstractClientTest.php +++ b/tests/Aws/Tests/Common/Client/AbstractClientTest.php @@ -19,6 +19,7 @@ use Aws\Common\Aws; use Aws\Common\Client\AbstractClient; use Aws\Common\Client\AwsClientInterface; +use Aws\Common\Credentials\NullCredentials; use Aws\Common\Enum\Region; use Aws\Common\Signature\SignatureV4; use Aws\Common\Signature\SignatureListener; @@ -47,12 +48,12 @@ public function testConstructorConfiguresClient() $this->assertSame($config, $client->getConfig()); // Ensure a signature event dispatcher was added - $this->assertGreaterThan(0, array_filter( + $this->assertGreaterThan(0, count(array_filter( $client->getEventDispatcher()->getListeners('request.before_send'), function($e) { return $e[0] instanceof SignatureListener; } - )); + ))); // Ensure that the user agent string is correct $expectedUserAgent = 'aws-sdk-php2/' . Aws::VERSION; @@ -60,6 +61,26 @@ function($e) { $this->assertRegExp("@^{$expectedUserAgent}@", $actualUserAgent); } + public function testConstructorAlwaysConfiguresSignatureListener() + { + $signature = new SignatureV4(); + $credentials = new NullCredentials(); + $config = new Collection(); + + $client = $this->getMockBuilder('Aws\Common\Client\AbstractClient') + ->setConstructorArgs(array($credentials, $signature, $config)) + ->getMockForAbstractClass(); + + // Ensure a signature event dispatcher was added + $this->assertGreaterThan(0, count(array_filter( + $client->getEventDispatcher()->getListeners('request.before_send'), + function($e) { + return $e[0] instanceof SignatureListener; + } + ))); + + } + public function testUsesDefaultWaiterFactory() { $client = $this->getMockBuilder('Aws\Common\Client\AbstractClient') diff --git a/tests/Aws/Tests/Common/Signature/SignatureListenerTest.php b/tests/Aws/Tests/Common/Signature/SignatureListenerTest.php index f00835c566..bb0bfa5f67 100644 --- a/tests/Aws/Tests/Common/Signature/SignatureListenerTest.php +++ b/tests/Aws/Tests/Common/Signature/SignatureListenerTest.php @@ -17,6 +17,7 @@ namespace Aws\Tests\Common\Signature; use Aws\Common\Credentials\Credentials; +use Aws\Common\Credentials\NullCredentials; use Aws\Common\Signature\SignatureListener; use Guzzle\Common\Collection; use Guzzle\Http\Message\Request; @@ -49,6 +50,26 @@ public function testSignsRequestsProperly() $listener->onRequestBeforeSend($event); } + public function testDoesNotSignNullCredentialRequests() { + $request = new Request('GET', 'http://www.example.com'); + $request->getEventDispatcher(); + $credentials = new NullCredentials(); + $signature = $this->getMock('Aws\Common\Signature\SignatureV4'); + + // Ensure that signing the request occurred once with the correct args + $signature->expects($this->never()) + ->method('signRequest'); + + $listener = new SignatureListener($credentials, $signature); + + // Create a mock event + $event = new Event(array( + 'request' => $request + )); + + $listener->onRequestBeforeSend($event); + } + public function testCredentialsGetUpdated() { $credentials = new Credentials('access-key-one', 'secret');