Skip to content
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

Support an aws_credential_provider connection parameter for more flexible credentials strategies #1667

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
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