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