-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kim Pepper <[email protected]>
- Loading branch information
Showing
4 changed files
with
185 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenSearch\Aws; | ||
|
||
use Aws\Credentials\CredentialProvider; | ||
use Aws\Credentials\Credentials; | ||
use Aws\Exception\CredentialsException; | ||
use Aws\Signature\SignatureInterface; | ||
use Aws\Signature\SignatureV4; | ||
use OpenSearch\HttpClient\HttpClientFactoryInterface; | ||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* A decorator client that signs requests using the provided AWS credentials. | ||
*/ | ||
class AwsSigningHttpClientFactory implements HttpClientFactoryInterface | ||
{ | ||
public function __construct( | ||
protected ClientInterface $innerClient, | ||
protected ?SignatureInterface $signer = null, | ||
protected ?CredentialProvider $provider = null, | ||
protected ?LoggerInterface $logger = null, | ||
) { | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function create(array $options): ClientInterface | ||
{ | ||
if (!isset($options['base_uri'])) { | ||
throw new \InvalidArgumentException('The base_uri option is required.'); | ||
} | ||
|
||
// Get the credentials. | ||
$provider = $this->getCredentialProvider($options); | ||
$promise = $provider(); | ||
try { | ||
$credentials = $promise->wait(); | ||
} catch (CredentialsException $e) { | ||
$this->logger?->error('Failed to get AWS credentials: @message', ['@message' => $e->getMessage()]); | ||
$credentials = new Credentials('', ''); | ||
} | ||
|
||
// Get the signer. | ||
$signer = $this->getSigner($options); | ||
|
||
// Host header is required. | ||
$host = parse_url($options['base_uri'], PHP_URL_HOST); | ||
|
||
return new SigningClientDecorator($this->innerClient, $credentials, $signer, ['host' => $host]); | ||
} | ||
|
||
/** | ||
* Gets the credential provider. | ||
* | ||
* @param array<string,mixed> $options | ||
* The options array. | ||
*/ | ||
protected function getCredentialProvider(array $options): CredentialProvider|\Closure|null|callable | ||
{ | ||
// Check for a provided credential provider. | ||
if ($this->provider) { | ||
return $this->provider; | ||
} | ||
|
||
// Check for provided access key and secret. | ||
if (isset($options['access_key']) && isset($options['secret_key'])) { | ||
return CredentialProvider::fromCredentials( | ||
new Credentials( | ||
$options['access_key'], | ||
$options['secret_key'], | ||
$options['session_token'] ?? null, | ||
) | ||
); | ||
} | ||
|
||
// Fallback to the default provider. | ||
return CredentialProvider::defaultProvider(); | ||
} | ||
|
||
/** | ||
* Gets the request signer. | ||
* | ||
* @param array<string,string> $options | ||
* The options. | ||
*/ | ||
protected function getSigner(array $options): SignatureInterface | ||
{ | ||
if ($this->signer) { | ||
return $this->signer; | ||
} | ||
|
||
if (!isset($options['region'])) { | ||
throw new \InvalidArgumentException('The region option is required.'); | ||
} | ||
|
||
$service = $options['service'] ?? 'es'; | ||
|
||
return new SignatureV4($service, $options['region'], $options); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenSearch\Tests\Aws; | ||
|
||
use OpenSearch\Aws\AwsSigningHttpClientFactory; | ||
use OpenSearch\HttpClient\SymfonyHttpClientFactory; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Client\ClientInterface; | ||
|
||
/** | ||
* @coversDefaultClass \OpenSearch\Aws\AwsSigningHttpClientFactory | ||
*/ | ||
class AwsSigningHttpClientFactoryTest extends TestCase | ||
{ | ||
/** | ||
* @covers ::create | ||
*/ | ||
public function testCreate(): void | ||
{ | ||
$symfonyClient = (new SymfonyHttpClientFactory())->create([ | ||
'base_uri' => 'http://localhost:9200', | ||
]); | ||
$factory = new AwsSigningHttpClientFactory($symfonyClient); | ||
$client = $factory->create([ | ||
'base_uri' => 'http://localhost:9200', | ||
'access_key' => 'foo', | ||
'secret_key' => 'bar', | ||
'region' => 'us-east-1', | ||
]); | ||
|
||
// Check we get a client back. | ||
$this->assertInstanceOf(ClientInterface::class, $client); | ||
} | ||
} |