Skip to content

Commit

Permalink
Unset port for SigV4Handler, fixes #84 (#85)
Browse files Browse the repository at this point in the history
Signed-off-by: Soner Sayakci <[email protected]>

Signed-off-by: Soner Sayakci <[email protected]>
  • Loading branch information
shyim authored Oct 1, 2022
1 parent 85c5731 commit 27e04b1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/OpenSearch/Handlers/SigV4Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,17 @@ private function createRingRequest(RequestInterface $request, array $originalReq
$body = null;
}

// Reset the explicit port in the URL
$client = $originalRequest['client'];
unset($client['curl'][CURLOPT_PORT]);

$ringRequest = [
'http_method' => $request->getMethod(),
'scheme' => $uri->getScheme(),
'uri' => $uri->getPath(),
'body' => $body,
'headers' => $request->getHeaders(),
'client' => $originalRequest['client']
'client' => $client
];
if ($uri->getQuery()) {
$ringRequest['query_string'] = $uri->getQuery();
Expand Down
25 changes: 20 additions & 5 deletions tests/Handlers/SigV4HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ public function testSignsRequestsTheSdkDefaultCredentialProviderChain()

public function testSignsWithProvidedCredentials()
{
$provider = CredentialProvider::fromCredentials(
new Credentials('foo', 'bar', 'baz')
);
$toWrap = function (array $ringRequest) {
$this->assertArrayHasKey('X-Amz-Security-Token', $ringRequest['headers']);
$this->assertSame('baz', $ringRequest['headers']['X-Amz-Security-Token'][0]);
Expand Down Expand Up @@ -141,7 +138,7 @@ public function testClientParametersShouldBePassedToHandler()
$this->assertArrayHasKey('client', $ringRequest);
$this->assertArrayHasKey('timeout', $ringRequest['client']);
$this->assertArrayHasKey('connect_timeout', $ringRequest['client']);

return $this->getGenericResponse();
};

Expand All @@ -155,7 +152,25 @@ public function testClientParametersShouldBePassedToHandler()
$client->indices()->exists(['index' => 'index']);
}

private function getGenericResponse()
public function testClientPortDeterminedByURL()
{
$toWrap = function (array $ringRequest) {
$this->assertArrayNotHasKey(CURLOPT_PORT, $ringRequest['client']['curl']);

return $this->getGenericResponse();
};

$client = ClientBuilder::create()
->setHandler($toWrap)
->setHosts(['https://search--hgkaewb2ytci3t3y6yghh5m5vje.eu-central-1.es.amazonaws.com'])
->setSigV4Region('us-west-2')
->setSigV4CredentialProvider(new Credentials('foo', 'bar', 'baz'))
->build();

$client->indices()->exists(['index' => 'index']);
}

private function getGenericResponse(): CompletedFutureArray
{
return new CompletedFutureArray([
'status' => 200,
Expand Down

0 comments on commit 27e04b1

Please sign in to comment.