Skip to content

Commit

Permalink
Support an aws_credential_provider connection parameter for more flex…
Browse files Browse the repository at this point in the history
…ible credentials strategies (#1667)
  • Loading branch information
jeskew authored and ruflin committed Jan 6, 2020
1 parent 98e5e92 commit 642d932
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ All notable changes to this project will be documented in this file based on the
* Fixed handling of Search::OPTION_SEARCH_IGNORE_UNAVAILABLE inside Scroll object

### Added
Added `DiversifiedSampler` aggregation [#1735](https://github.com/ruflin/Elastica/pull/1735)

* Added `DiversifiedSampler` aggregation [#1735](https://github.com/ruflin/Elastica/pull/1735)
* Added `\Elastica\Query\DistanceFeature` [#1730](https://github.com/ruflin/Elastica/pull/1730)
* Added support for injecting a callable AWS credential provider to use static, cached, or custom-sourced credentials [#1667](https://github.com/ruflin/Elastica/pull/1667)

### Improvements

Expand Down
9 changes: 7 additions & 2 deletions lib/Elastica/Transport/AwsAuthV4.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function _getBaseUrl(Connection $connection): string
return parent::_getBaseUrl($connection);
}

private function getSigningMiddleware()
private function getSigningMiddleware(): callable
{
$region = $this->getConnection()->hasParam('aws_region')
? $this->getConnection()->getParam('aws_region')
Expand All @@ -51,9 +51,14 @@ private function getSigningMiddleware()
});
}

private function getCredentialProvider()
private function getCredentialProvider(): callable
{
$connection = $this->getConnection();

if ($connection->hasParam('aws_credential_provider')) {
return $connection->getParam('aws_credential_provider');
}

if ($connection->hasParam('aws_secret_access_key')) {
return CredentialProvider::fromCredentials(new Credentials(
$connection->getParam('aws_access_key_id'),
Expand Down
81 changes: 81 additions & 0 deletions test/Elastica/Transport/AwsAuthV4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Elastica\Test\Transport;

use Aws\Credentials\CredentialProvider;
use Aws\Credentials\Credentials;
use Elastica\Exception\Connection\GuzzleException;
use GuzzleHttp\Exception\RequestException;

Expand All @@ -14,6 +16,85 @@ public static function setUpBeforeClass()
}
}

/**
* @group unit
*/
public function testSignsWithProvidedCredentialProvider()
{
$config = [
'persistent' => false,
'transport' => 'AwsAuthV4',
'aws_credential_provider' => CredentialProvider::fromCredentials(
new Credentials('foo', 'bar', 'baz')
),
'aws_region' => 'us-east-1',
];

$client = $this->_getClient($config);

try {
$client->request('_status', 'GET');
} catch (GuzzleException $e) {
$guzzleException = $e->getGuzzleException();
if ($guzzleException instanceof RequestException) {
$request = $guzzleException->getRequest();
$expected = 'AWS4-HMAC-SHA256 Credential=foo/'
.\date('Ymd').'/us-east-1/es/aws4_request, ';
$this->assertStringStartsWith(
$expected,
$request->getHeaderLine('Authorization')
);
$this->assertSame(
'baz',
$request->getHeaderLine('X-Amz-Security-Token')
);
} else {
throw $e;
}
}
}

/**
* @group unit
*/
public function testPrefersCredentialProviderToHardCodedCredentials()
{
$config = [
'persistent' => false,
'transport' => 'AwsAuthV4',
'aws_credential_provider' => CredentialProvider::fromCredentials(
new Credentials('foo', 'bar', 'baz')
),
'aws_access_key_id' => 'snap',
'aws_secret_access_key' => 'crackle',
'aws_session_token' => 'pop',
'aws_region' => 'us-east-1',
];

$client = $this->_getClient($config);

try {
$client->request('_status', 'GET');
} catch (GuzzleException $e) {
$guzzleException = $e->getGuzzleException();
if ($guzzleException instanceof RequestException) {
$request = $guzzleException->getRequest();
$expected = 'AWS4-HMAC-SHA256 Credential=foo/'
.\date('Ymd').'/us-east-1/es/aws4_request, ';
$this->assertStringStartsWith(
$expected,
$request->getHeaderLine('Authorization')
);
$this->assertSame(
'baz',
$request->getHeaderLine('X-Amz-Security-Token')
);
} else {
throw $e;
}
}
}

/**
* @group unit
*/
Expand Down

0 comments on commit 642d932

Please sign in to comment.