| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\S3; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\Api\Service; |
| 6 |
use Dudlewebs\WPMCS\s3\Aws\Arn\ArnInterface; |
| 7 |
use Dudlewebs\WPMCS\s3\Aws\Arn\S3\OutpostsArnInterface; |
| 8 |
use Dudlewebs\WPMCS\s3\Aws\Endpoint\PartitionEndpointProvider; |
| 9 |
use Dudlewebs\WPMCS\s3\Aws\Exception\InvalidRegionException; |
| 10 |
/** |
| 11 |
* @internal |
| 12 |
*/ |
| 13 |
trait EndpointRegionHelperTrait |
| 14 |
{ |
| 15 |
/** @var array */ |
| 16 |
private $config; |
| 17 |
/** @var PartitionEndpointProvider */ |
| 18 |
private $partitionProvider; |
| 19 |
/** @var string */ |
| 20 |
private $region; |
| 21 |
/** @var Service */ |
| 22 |
private $service; |
| 23 |
private function getPartitionSuffix(ArnInterface $arn, PartitionEndpointProvider $provider) |
| 24 |
{ |
| 25 |
$partition = $provider->getPartition($arn->getRegion(), $arn->getService()); |
| 26 |
return $partition->getDnsSuffix(); |
| 27 |
} |
| 28 |
private function getSigningRegion($region, $service, PartitionEndpointProvider $provider) |
| 29 |
{ |
| 30 |
$partition = $provider->getPartition($region, $service); |
| 31 |
$data = $partition->toArray(); |
| 32 |
if (isset($data['services'][$service]['endpoints'][$region]['credentialScope']['region'])) { |
| 33 |
return $data['services'][$service]['endpoints'][$region]['credentialScope']['region']; |
| 34 |
} |
| 35 |
return $region; |
| 36 |
} |
| 37 |
private function isMatchingSigningRegion($arnRegion, $clientRegion, $service, PartitionEndpointProvider $provider) |
| 38 |
{ |
| 39 |
$arnRegion = \Dudlewebs\WPMCS\s3\Aws\strip_fips_pseudo_regions(\strtolower($arnRegion)); |
| 40 |
$clientRegion = \strtolower($clientRegion); |
| 41 |
if ($arnRegion === $clientRegion) { |
| 42 |
return \true; |
| 43 |
} |
| 44 |
if ($this->getSigningRegion($clientRegion, $service, $provider) === $arnRegion) { |
| 45 |
return \true; |
| 46 |
} |
| 47 |
return \false; |
| 48 |
} |
| 49 |
private function validateFipsConfigurations(ArnInterface $arn) |
| 50 |
{ |
| 51 |
$useFipsEndpoint = !empty($this->config['use_fips_endpoint']); |
| 52 |
if ($arn instanceof OutpostsArnInterface) { |
| 53 |
if (empty($this->config['use_arn_region']) || !$this->config['use_arn_region']->isUseArnRegion()) { |
| 54 |
$region = $this->region; |
| 55 |
} else { |
| 56 |
$region = $arn->getRegion(); |
| 57 |
} |
| 58 |
if (\Dudlewebs\WPMCS\s3\Aws\is_fips_pseudo_region($region)) { |
| 59 |
throw new InvalidRegionException('Fips is currently not supported with S3 Outposts access' . ' points. Please provide a non-fips region or do not supply an' . ' access point ARN.'); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
private function validateMatchingRegion(ArnInterface $arn) |
| 64 |
{ |
| 65 |
if (!$this->isMatchingSigningRegion($arn->getRegion(), $this->region, $this->service->getEndpointPrefix(), $this->partitionProvider)) { |
| 66 |
if (empty($this->config['use_arn_region']) || !$this->config['use_arn_region']->isUseArnRegion()) { |
| 67 |
throw new InvalidRegionException('The region' . " specified in the ARN (" . $arn->getRegion() . ") does not match the client region (" . "{$this->region})."); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|