| 1 |
<?php |
| 2 |
namespace Aws\Endpoint\UseFipsEndpoint; |
| 3 |
|
| 4 |
use Aws\AbstractConfigurationProvider; |
| 5 |
use Aws\CacheInterface; |
| 6 |
use Aws\ConfigurationProviderInterface; |
| 7 |
use Aws\Endpoint\UseFipsEndpoint\Exception\ConfigurationException; |
| 8 |
use GuzzleHttp\Promise; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class ConfigurationProvider extends AbstractConfigurationProvider |
| 45 |
implements ConfigurationProviderInterface |
| 46 |
{ |
| 47 |
const ENV_USE_FIPS_ENDPOINT = 'AWS_USE_FIPS_ENDPOINT'; |
| 48 |
const INI_USE_FIPS_ENDPOINT = 'use_fips_endpoint'; |
| 49 |
|
| 50 |
public static $cacheKey = 'aws_cached_use_fips_endpoint_config'; |
| 51 |
|
| 52 |
protected static $interfaceClass = ConfigurationInterface::class; |
| 53 |
protected static $exceptionClass = ConfigurationException::class; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public static function defaultProvider(array $config = []) |
| 71 |
{ |
| 72 |
$configProviders = [self::env()]; |
| 73 |
if ( |
| 74 |
!isset($config['use_aws_shared_config_files']) |
| 75 |
|| $config['use_aws_shared_config_files'] != false |
| 76 |
) { |
| 77 |
$configProviders[] = self::ini(); |
| 78 |
} |
| 79 |
$configProviders[] = self::fallback($config['region']); |
| 80 |
|
| 81 |
$memo = self::memoize( |
| 82 |
call_user_func_array([ConfigurationProvider::class, 'chain'], $configProviders) |
| 83 |
); |
| 84 |
|
| 85 |
if (isset($config['use_fips_endpoint']) |
| 86 |
&& $config['use_fips_endpoint'] instanceof CacheInterface |
| 87 |
) { |
| 88 |
return self::cache($memo, $config['use_fips_endpoint'], self::$cacheKey); |
| 89 |
} |
| 90 |
|
| 91 |
return $memo; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public static function env() |
| 100 |
{ |
| 101 |
return function () { |
| 102 |
|
| 103 |
$useFipsEndpoint = getenv(self::ENV_USE_FIPS_ENDPOINT); |
| 104 |
if (!empty($useFipsEndpoint)) { |
| 105 |
return Promise\Create::promiseFor( |
| 106 |
new Configuration($useFipsEndpoint) |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
return self::reject('Could not find environment variable config' |
| 111 |
. ' in ' . self::ENV_USE_FIPS_ENDPOINT); |
| 112 |
}; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public static function ini($profile = null, $filename = null) |
| 128 |
{ |
| 129 |
$filename = $filename ?: (self::getDefaultConfigFilename()); |
| 130 |
$profile = $profile ?: (getenv(self::ENV_PROFILE) ?: 'default'); |
| 131 |
|
| 132 |
return function () use ($profile, $filename) { |
| 133 |
if (!@is_readable($filename)) { |
| 134 |
return self::reject("Cannot read configuration from $filename"); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
$data = \Aws\parse_ini_file($filename, true, INI_SCANNER_NORMAL); |
| 139 |
if ($data === false) { |
| 140 |
return self::reject("Invalid config file: $filename"); |
| 141 |
} |
| 142 |
if (!isset($data[$profile])) { |
| 143 |
return self::reject("'$profile' not found in config file"); |
| 144 |
} |
| 145 |
if (!isset($data[$profile][self::INI_USE_FIPS_ENDPOINT])) { |
| 146 |
return self::reject("Required use fips endpoint config values |
| 147 |
not present in INI profile '{$profile}' ({$filename})"); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
if ($data[$profile][self::INI_USE_FIPS_ENDPOINT] === "") { |
| 152 |
$data[$profile][self::INI_USE_FIPS_ENDPOINT] = false; |
| 153 |
} |
| 154 |
|
| 155 |
return Promise\Create::promiseFor( |
| 156 |
new Configuration($data[$profile][self::INI_USE_FIPS_ENDPOINT]) |
| 157 |
); |
| 158 |
}; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public static function fallback($region) |
| 167 |
{ |
| 168 |
return function () use ($region) { |
| 169 |
$isFipsPseudoRegion = strpos($region, 'fips-') !== false |
| 170 |
|| strpos($region, '-fips') !== false; |
| 171 |
if ($isFipsPseudoRegion){ |
| 172 |
$configuration = new Configuration(true); |
| 173 |
} else { |
| 174 |
$configuration = new Configuration(false); |
| 175 |
} |
| 176 |
return Promise\Create::promiseFor($configuration); |
| 177 |
}; |
| 178 |
} |
| 179 |
} |