media-cloud-sync
/
includes
/
sdk
/
s3
/
Aws
/
Endpoint
/
UseFipsEndpoint
/
ConfigurationProvider.php
ConfigurationProvider.php in Media Cloud Sync 1.4.1, at includes/sdk/s3/Aws/Endpoint/UseFipsEndpoint/ConfigurationProvider.php
| 1 | <?php |
| 2 | |
| 3 | namespace Dudlewebs\WPMCS\s3\Aws\Endpoint\UseFipsEndpoint; |
| 4 | |
| 5 | use Dudlewebs\WPMCS\s3\Aws\AbstractConfigurationProvider; |
| 6 | use Dudlewebs\WPMCS\s3\Aws\CacheInterface; |
| 7 | use Dudlewebs\WPMCS\s3\Aws\ConfigurationProviderInterface; |
| 8 | use Dudlewebs\WPMCS\s3\Aws\Endpoint\UseFipsEndpoint\Exception\ConfigurationException; |
| 9 | use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise; |
| 10 | /** |
| 11 | * A configuration provider is a function that returns a promise that is |
| 12 | * fulfilled with a {@see \Aws\Endpoint\UseFipsEndpoint\onfigurationInterface} |
| 13 | * or rejected with an {@see \Aws\Endpoint\UseFipsEndpoint\ConfigurationException}. |
| 14 | * |
| 15 | * <code> |
| 16 | * use Aws\Endpoint\UseFipsEndpoint\ConfigurationProvider; |
| 17 | * $provider = ConfigurationProvider::defaultProvider(); |
| 18 | * // Returns a ConfigurationInterface or throws. |
| 19 | * $config = $provider()->wait(); |
| 20 | * </code> |
| 21 | * |
| 22 | * Configuration providers can be composed to create configuration using |
| 23 | * conditional logic that can create different configurations in different |
| 24 | * environments. You can compose multiple providers into a single provider using |
| 25 | * {@see Aws\Endpoint\UseFipsEndpoint\ConfigurationProvider::chain}. This function |
| 26 | * accepts providers as variadic arguments and returns a new function that will |
| 27 | * invoke each provider until a successful configuration is returned. |
| 28 | * |
| 29 | * <code> |
| 30 | * // First try an INI file at this location. |
| 31 | * $a = ConfigurationProvider::ini(null, '/path/to/file.ini'); |
| 32 | * // Then try an INI file at this location. |
| 33 | * $b = ConfigurationProvider::ini(null, '/path/to/other-file.ini'); |
| 34 | * // Then try loading from environment variables. |
| 35 | * $c = ConfigurationProvider::env(); |
| 36 | * // Combine the three providers together. |
| 37 | * $composed = ConfigurationProvider::chain($a, $b, $c); |
| 38 | * // Returns a promise that is fulfilled with a configuration or throws. |
| 39 | * $promise = $composed(); |
| 40 | * // Wait on the configuration to resolve. |
| 41 | * $config = $promise->wait(); |
| 42 | * </code> |
| 43 | */ |
| 44 | class ConfigurationProvider extends AbstractConfigurationProvider implements ConfigurationProviderInterface |
| 45 | { |
| 46 | const ENV_USE_FIPS_ENDPOINT = 'AWS_USE_FIPS_ENDPOINT'; |
| 47 | const INI_USE_FIPS_ENDPOINT = 'use_fips_endpoint'; |
| 48 | public static $cacheKey = 'aws_cached_use_fips_endpoint_config'; |
| 49 | protected static $interfaceClass = ConfigurationInterface::class; |
| 50 | protected static $exceptionClass = ConfigurationException::class; |
| 51 | /** |
| 52 | * Create a default config provider that first checks for environment |
| 53 | * variables, then checks for a specified profile in the environment-defined |
| 54 | * config file location (env variable is 'AWS_CONFIG_FILE', file location |
| 55 | * defaults to ~/.aws/config), then checks for the "default" profile in the |
| 56 | * environment-defined config file location, and failing those uses a default |
| 57 | * fallback set of configuration options. |
| 58 | * |
| 59 | * This provider is automatically wrapped in a memoize function that caches |
| 60 | * previously provided config options. |
| 61 | * |
| 62 | * @param array $config |
| 63 | * |
| 64 | * @return callable |
| 65 | */ |
| 66 | public static function defaultProvider(array $config = []) |
| 67 | { |
| 68 | $configProviders = [self::env()]; |
| 69 | if (!isset($config['use_aws_shared_config_files']) || $config['use_aws_shared_config_files'] != \false) { |
| 70 | $configProviders[] = self::ini(); |
| 71 | } |
| 72 | $configProviders[] = self::fallback($config['region']); |
| 73 | $memo = self::memoize(\call_user_func_array([ConfigurationProvider::class, 'chain'], $configProviders)); |
| 74 | if (isset($config['use_fips_endpoint']) && $config['use_fips_endpoint'] instanceof CacheInterface) { |
| 75 | return self::cache($memo, $config['use_fips_endpoint'], self::$cacheKey); |
| 76 | } |
| 77 | return $memo; |
| 78 | } |
| 79 | /** |
| 80 | * Provider that creates config from environment variables. |
| 81 | * |
| 82 | * @return callable |
| 83 | */ |
| 84 | public static function env() |
| 85 | { |
| 86 | return function () { |
| 87 | // Use config from environment variables, if available |
| 88 | $useFipsEndpoint = \getenv(self::ENV_USE_FIPS_ENDPOINT); |
| 89 | if (!empty($useFipsEndpoint)) { |
| 90 | return Promise\Create::promiseFor(new Configuration($useFipsEndpoint)); |
| 91 | } |
| 92 | return self::reject('Could not find environment variable config' . ' in ' . self::ENV_USE_FIPS_ENDPOINT); |
| 93 | }; |
| 94 | } |
| 95 | /** |
| 96 | * Config provider that creates config using a config file whose location |
| 97 | * is specified by an environment variable 'AWS_CONFIG_FILE', defaulting to |
| 98 | * ~/.aws/config if not specified |
| 99 | * |
| 100 | * @param string|null $profile Profile to use. If not specified will use |
| 101 | * the "default" profile. |
| 102 | * @param string|null $filename If provided, uses a custom filename rather |
| 103 | * than looking in the default directory. |
| 104 | * |
| 105 | * @return callable |
| 106 | */ |
| 107 | public static function ini($profile = null, $filename = null) |
| 108 | { |
| 109 | $filename = $filename ?: self::getDefaultConfigFilename(); |
| 110 | $profile = $profile ?: (\getenv(self::ENV_PROFILE) ?: 'default'); |
| 111 | return function () use($profile, $filename) { |
| 112 | if (!@\is_readable($filename)) { |
| 113 | return self::reject("Cannot read configuration from {$filename}"); |
| 114 | } |
| 115 | // Use INI_SCANNER_NORMAL instead of INI_SCANNER_TYPED for PHP 5.5 compatibility |
| 116 | $data = \Dudlewebs\WPMCS\s3\Aws\parse_ini_file($filename, \true, \INI_SCANNER_NORMAL); |
| 117 | if ($data === \false) { |
| 118 | return self::reject("Invalid config file: {$filename}"); |
| 119 | } |
| 120 | if (!isset($data[$profile])) { |
| 121 | return self::reject("'{$profile}' not found in config file"); |
| 122 | } |
| 123 | if (!isset($data[$profile][self::INI_USE_FIPS_ENDPOINT])) { |
| 124 | return self::reject("Required use fips endpoint config values\n not present in INI profile '{$profile}' ({$filename})"); |
| 125 | } |
| 126 | // INI_SCANNER_NORMAL parses false-y values as an empty string |
| 127 | if ($data[$profile][self::INI_USE_FIPS_ENDPOINT] === "") { |
| 128 | $data[$profile][self::INI_USE_FIPS_ENDPOINT] = \false; |
| 129 | } |
| 130 | return Promise\Create::promiseFor(new Configuration($data[$profile][self::INI_USE_FIPS_ENDPOINT])); |
| 131 | }; |
| 132 | } |
| 133 | /** |
| 134 | * Fallback config options when other sources are not set. |
| 135 | * |
| 136 | * @return callable |
| 137 | */ |
| 138 | public static function fallback($region) |
| 139 | { |
| 140 | return function () use($region) { |
| 141 | $isFipsPseudoRegion = \strpos($region, 'fips-') !== \false || \strpos($region, '-fips') !== \false; |
| 142 | if ($isFipsPseudoRegion) { |
| 143 | $configuration = new Configuration(\true); |
| 144 | } else { |
| 145 | $configuration = new Configuration(\false); |
| 146 | } |
| 147 | return Promise\Create::promiseFor($configuration); |
| 148 | }; |
| 149 | } |
| 150 | } |
| 151 |