Skip to content

Commit

Permalink
Bugfix: EndpointV2 sigv4a tests (#2619)
Browse files Browse the repository at this point in the history
* bugfix: crt ep2 test updates
  • Loading branch information
stobrien89 authored Jan 20, 2023
1 parent c600a07 commit 7b18adf
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 9 deletions.
7 changes: 7 additions & 0 deletions .changes/nextrelease/nextrelease.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"type": "bugfix",
"category": "EndpointV2",
"description": "Test fixes related to signing, particularly sigv4a."
}
]
20 changes: 15 additions & 5 deletions src/EndpointV2/EndpointV2SerializerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,22 @@ private function applyAuthSchemeToCommand($endpoint, $command)

private function selectAuthScheme($authSchemes)
{
$validAuthSchemes = ['sigv4', 'sigv4a' ];
$validAuthSchemes = ['sigv4', 'sigv4a', 'none', 'bearer'];
$invalidAuthSchemes = [];

foreach($authSchemes as $authScheme) {
if (in_array($authScheme['name'], $validAuthSchemes)) {
return $this->normalizeAuthScheme($authScheme);
} else {
$unsupportedScheme = $authScheme['name'];
$invalidAuthSchemes[] = "`{$authScheme['name']}`";
}
}

$invalidAuthSchemesString = implode(', ', $invalidAuthSchemes);
$validAuthSchemesString = '`' . implode('`, `', $validAuthSchemes) . '`';
throw new \InvalidArgumentException(
"This operation requests {$unsupportedScheme}
. but the client only supports sigv4 and sigv4a"
"This operation requests {$invalidAuthSchemesString}"
. " auth schemes, but the client only supports {$validAuthSchemesString}."
);
}

Expand All @@ -200,17 +203,24 @@ private function normalizeAuthScheme($authScheme)

if (isset($authScheme['disableDoubleEncoding'])
&& $authScheme['disableDoubleEncoding'] === true
&& $authScheme['name'] !== 'sigv4a'
) {
$normalizedAuthScheme['version'] = 's3v4';
} else {
} elseif ($authScheme['name'] === 'none') {
$normalizedAuthScheme['version'] = 'anonymous';
}
else {
$normalizedAuthScheme['version'] = str_replace(
'sig', '', $authScheme['name']
);
}

$normalizedAuthScheme['name'] = isset($authScheme['signingName']) ?
$authScheme['signingName'] : null;
$normalizedAuthScheme['region'] = isset($authScheme['signingRegion']) ?
$authScheme['signingRegion'] : null;
$normalizedAuthScheme['signingRegionSet'] = isset($authScheme['signingRegionSet']) ?
$authScheme['signingRegionSet'] : null;

return $normalizedAuthScheme;
}
Expand Down
16 changes: 12 additions & 4 deletions tests/EndpointV2/EndpointProviderV2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ public function testRulesetProtocolEndpointAndErrorCases($service, $clientArgs,
$expectedAuthSchemes = $expectedEndpoint['properties']['authSchemes'][0];
if ((isset($expectedAuthSchemes['disableDoubleEncoding'])
&& $expectedAuthSchemes['disableDoubleEncoding'] === true)
&& $expectedAuthSchemes['name'] !== 'sigv4a'
) {
$expectedVersion = 's3v4';
} else {
Expand All @@ -268,10 +269,17 @@ public function testRulesetProtocolEndpointAndErrorCases($service, $clientArgs,
$cmd->getAuthSchemes()['name'],
$expectedAuthSchemes['signingName']
);
$this->assertEquals(
$cmd->getAuthSchemes()['region'],
$expectedAuthSchemes['signingRegion']
);
if (isset($cmd->getAuthSchemes()['region'])) {
$this->assertEquals(
$cmd->getAuthSchemes()['region'],
$expectedAuthSchemes['signingRegion']
);
} elseif (isset($cmd->getAuthSchemes['signingRegionSet'])) {
$this->assertEquals(
$cmd->getAuthSchemes()['region'],
$expectedAuthSchemes['signingRegionSet']
);
}
}
if (isset($expectedEndpoint['headers'])) {
$expectedHeaders = $expectedEndpoint['headers'];
Expand Down
36 changes: 36 additions & 0 deletions tests/EndpointV2/EndpointV2SerializerTraitTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Aws\Test\EndpointV2;

use Aws\EndpointV2\EndpointDefinitionProvider;
use Aws\EndpointV2\EndpointProviderV2;
use Aws\Middleware;
use Aws\Test\UsesServiceTrait;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
Expand Down Expand Up @@ -43,4 +45,38 @@ public function testCommandEndpointDoesNotOverrideSdkEndpoint()
$handler = $list->resolve();
$handler($command)->wait();
}

/**
* Ensures SDK-level config options used for ruleset evaluation
* are not overridden by a collision with a command argument
*/
public function testThrowsExceptionForInvalidAuthScheme()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(
'This operation requests `sigvfoo`, `sigvbar`, `sigvbaz` auth schemes,'
. ' but the client only supports `sigv4`, `sigv4a`, `none`, `bearer`.'
);

$rulesetPath = __DIR__ . '/invalid-rules/invalid-scheme.json';
$rulesetDefinition = json_decode(file_get_contents($rulesetPath), true);
$partitions = EndpointDefinitionProvider::getPartitions();

$clientArgs = [
'region' => 'us-east-1',
'endpoint_provider' => new EndpointProviderV2($rulesetDefinition, $partitions)
];

$client = $this->getTestClient('s3', $clientArgs);
$this->addMockResults($client, [[]]);
$command = $client->getCommand(
'headBucket',
[
'Bucket' => 'foo',
]
);
$list = $client->getHandlerList();
$handler = $list->resolve();
$handler($command)->wait();
}
}
54 changes: 54 additions & 0 deletions tests/EndpointV2/invalid-rules/invalid-scheme.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"parameters": {
"Region": {
"type": "string",
"builtIn": "AWS::Region",
"documentation": "The region to dispatch this request, eg. `us-east-1`."
}
},
"rules": [
{
"documentation": "Template the region into the URI when region is set",
"conditions": [
{
"fn": "isSet",
"argv": [
{
"ref": "Region"
}
]
}
],
"endpoint": {
"url": "https://{Region}.amazonaws.com",
"properties": {
"authSchemes": [
{
"name": "sigvfoo",
"signingName": "serviceName",
"signingRegion": "{Region}"
},
{
"name": "sigvbar",
"signingName": "serviceName",
"signingRegion": "{Region}"
},
{
"name": "sigvbaz",
"signingName": "serviceName",
"signingRegion": "{Region}"
}
]
}
},
"type": "endpoint"
},
{
"documentation": "fallback when region is unset",
"conditions": [],
"error": "Region must be set to resolve a valid endpoint",
"type": "error"
}
],
"version": "1.3"
}

0 comments on commit 7b18adf

Please sign in to comment.