vendor/aws/aws-sdk-php/src/Endpoint/UseFipsEndpoint/ConfigurationProvider.php line 133

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