Skip to content

Commit

Permalink
Always add SignatureListener, even on NullCredential requests
Browse files Browse the repository at this point in the history
  • Loading branch information
rbayliss committed Dec 21, 2014
1 parent 087912b commit cc83c2a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
4 changes: 1 addition & 3 deletions src/Aws/Common/Client/AbstractClient.php
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/Aws/Common/Signature/SignatureListener.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
}
25 changes: 23 additions & 2 deletions tests/Aws/Tests/Common/Client/AbstractClientTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -47,19 +48,39 @@ 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;
$actualUserAgent = $this->readAttribute($client, 'userAgent');
$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')
Expand Down
21 changes: 21 additions & 0 deletions tests/Aws/Tests/Common/Signature/SignatureListenerTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit cc83c2a

Please sign in to comment.