| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\ClientSideMonitoring; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\AbstractConfigurationProvider; |
| 6 |
use Dudlewebs\WPMCS\s3\Aws\CacheInterface; |
| 7 |
use Dudlewebs\WPMCS\s3\Aws\ClientSideMonitoring\Exception\ConfigurationException; |
| 8 |
use Dudlewebs\WPMCS\s3\Aws\ConfigurationProviderInterface; |
| 9 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise; |
| 10 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\PromiseInterface; |
| 11 |
/** |
| 12 |
* A configuration provider is a function that accepts no arguments and returns |
| 13 |
* a promise that is fulfilled with a {@see \Aws\ClientSideMonitoring\ConfigurationInterface} |
| 14 |
* or rejected with an {@see \Aws\ClientSideMonitoring\Exception\ConfigurationException}. |
| 15 |
* |
| 16 |
* <code> |
| 17 |
* use Aws\ClientSideMonitoring\ConfigurationProvider; |
| 18 |
* $provider = ConfigurationProvider::defaultProvider(); |
| 19 |
* // Returns a ConfigurationInterface or throws. |
| 20 |
* $config = $provider()->wait(); |
| 21 |
* </code> |
| 22 |
* |
| 23 |
* Configuration providers can be composed to create configuration using |
| 24 |
* conditional logic that can create different configurations in different |
| 25 |
* environments. You can compose multiple providers into a single provider using |
| 26 |
* {@see Aws\ClientSideMonitoring\ConfigurationProvider::chain}. This function |
| 27 |
* accepts providers as variadic arguments and returns a new function that will |
| 28 |
* invoke each provider until a successful configuration is returned. |
| 29 |
* |
| 30 |
* <code> |
| 31 |
* // First try an INI file at this location. |
| 32 |
* $a = ConfigurationProvider::ini(null, '/path/to/file.ini'); |
| 33 |
* // Then try an INI file at this location. |
| 34 |
* $b = ConfigurationProvider::ini(null, '/path/to/other-file.ini'); |
| 35 |
* // Then try loading from environment variables. |
| 36 |
* $c = ConfigurationProvider::env(); |
| 37 |
* // Combine the three providers together. |
| 38 |
* $composed = ConfigurationProvider::chain($a, $b, $c); |
| 39 |
* // Returns a promise that is fulfilled with a configuration or throws. |
| 40 |
* $promise = $composed(); |
| 41 |
* // Wait on the configuration to resolve. |
| 42 |
* $config = $promise->wait(); |
| 43 |
* </code> |
| 44 |
*/ |
| 45 |
class ConfigurationProvider extends AbstractConfigurationProvider implements ConfigurationProviderInterface |
| 46 |
{ |
| 47 |
const DEFAULT_CLIENT_ID = ''; |
| 48 |
const DEFAULT_ENABLED = \false; |
| 49 |
const DEFAULT_HOST = '127.0.0.1'; |
| 50 |
const DEFAULT_PORT = 31000; |
| 51 |
const ENV_CLIENT_ID = 'AWS_CSM_CLIENT_ID'; |
| 52 |
const ENV_ENABLED = 'AWS_CSM_ENABLED'; |
| 53 |
const ENV_HOST = 'AWS_CSM_HOST'; |
| 54 |
const ENV_PORT = 'AWS_CSM_PORT'; |
| 55 |
const ENV_PROFILE = 'AWS_PROFILE'; |
| 56 |
public static $cacheKey = 'aws_cached_csm_config'; |
| 57 |
protected static $interfaceClass = ConfigurationInterface::class; |
| 58 |
protected static $exceptionClass = ConfigurationException::class; |
| 59 |
/** |
| 60 |
* Create a default config provider that first checks for environment |
| 61 |
* variables, then checks for a specified profile in the environment-defined |
| 62 |
* config file location (env variable is 'AWS_CONFIG_FILE', file location |
| 63 |
* defaults to ~/.aws/config), then checks for the "default" profile in the |
| 64 |
* environment-defined config file location, and failing those uses a default |
| 65 |
* fallback set of configuration options. |
| 66 |
* |
| 67 |
* This provider is automatically wrapped in a memoize function that caches |
| 68 |
* previously provided config options. |
| 69 |
* |
| 70 |
* @param array $config |
| 71 |
* |
| 72 |
* @return callable |
| 73 |
*/ |
| 74 |
public static function defaultProvider(array $config = []) |
| 75 |
{ |
| 76 |
$configProviders = [self::env()]; |
| 77 |
if (!isset($config['use_aws_shared_config_files']) || $config['use_aws_shared_config_files'] != \false) { |
| 78 |
$configProviders[] = self::ini(); |
| 79 |
} |
| 80 |
$configProviders[] = self::fallback(); |
| 81 |
$memo = self::memoize(\call_user_func_array([ConfigurationProvider::class, 'chain'], $configProviders)); |
| 82 |
if (isset($config['csm']) && $config['csm'] instanceof CacheInterface) { |
| 83 |
return self::cache($memo, $config['csm'], self::$cacheKey); |
| 84 |
} |
| 85 |
return $memo; |
| 86 |
} |
| 87 |
/** |
| 88 |
* Provider that creates CSM config from environment variables. |
| 89 |
* |
| 90 |
* @return callable |
| 91 |
*/ |
| 92 |
public static function env() |
| 93 |
{ |
| 94 |
return function () { |
| 95 |
// Use credentials from environment variables, if available |
| 96 |
$enabled = \getenv(self::ENV_ENABLED); |
| 97 |
if ($enabled !== \false) { |
| 98 |
return Promise\Create::promiseFor(new Configuration($enabled, \getenv(self::ENV_HOST) ?: self::DEFAULT_HOST, \getenv(self::ENV_PORT) ?: self::DEFAULT_PORT, \getenv(self::ENV_CLIENT_ID) ?: self::DEFAULT_CLIENT_ID)); |
| 99 |
} |
| 100 |
return self::reject('Could not find environment variable CSM config' . ' in ' . self::ENV_ENABLED . '/' . self::ENV_HOST . '/' . self::ENV_PORT . '/' . self::ENV_CLIENT_ID); |
| 101 |
}; |
| 102 |
} |
| 103 |
/** |
| 104 |
* Fallback config options when other sources are not set. |
| 105 |
* |
| 106 |
* @return callable |
| 107 |
*/ |
| 108 |
public static function fallback() |
| 109 |
{ |
| 110 |
return function () { |
| 111 |
return Promise\Create::promiseFor(new Configuration(self::DEFAULT_ENABLED, self::DEFAULT_HOST, self::DEFAULT_PORT, self::DEFAULT_CLIENT_ID)); |
| 112 |
}; |
| 113 |
} |
| 114 |
/** |
| 115 |
* Config provider that creates config using a config file whose location |
| 116 |
* is specified by an environment variable 'AWS_CONFIG_FILE', defaulting to |
| 117 |
* ~/.aws/config if not specified |
| 118 |
* |
| 119 |
* @param string|null $profile Profile to use. If not specified will use |
| 120 |
* the "default" profile. |
| 121 |
* @param string|null $filename If provided, uses a custom filename rather |
| 122 |
* than looking in the default directory. |
| 123 |
* |
| 124 |
* @return callable |
| 125 |
*/ |
| 126 |
public static function ini($profile = null, $filename = null) |
| 127 |
{ |
| 128 |
$filename = $filename ?: self::getDefaultConfigFilename(); |
| 129 |
$profile = $profile ?: (\getenv(self::ENV_PROFILE) ?: 'aws_csm'); |
| 130 |
return function () use($profile, $filename) { |
| 131 |
if (!@\is_readable($filename)) { |
| 132 |
return self::reject("Cannot read CSM config from {$filename}"); |
| 133 |
} |
| 134 |
$data = \Dudlewebs\WPMCS\s3\Aws\parse_ini_file($filename, \true); |
| 135 |
if ($data === \false) { |
| 136 |
return self::reject("Invalid config file: {$filename}"); |
| 137 |
} |
| 138 |
if (!isset($data[$profile])) { |
| 139 |
return self::reject("'{$profile}' not found in config file"); |
| 140 |
} |
| 141 |
if (!isset($data[$profile]['csm_enabled'])) { |
| 142 |
return self::reject("Required CSM config values not present in\n INI profile '{$profile}' ({$filename})"); |
| 143 |
} |
| 144 |
// host is optional |
| 145 |
if (empty($data[$profile]['csm_host'])) { |
| 146 |
$data[$profile]['csm_host'] = self::DEFAULT_HOST; |
| 147 |
} |
| 148 |
// port is optional |
| 149 |
if (empty($data[$profile]['csm_port'])) { |
| 150 |
$data[$profile]['csm_port'] = self::DEFAULT_PORT; |
| 151 |
} |
| 152 |
// client_id is optional |
| 153 |
if (empty($data[$profile]['csm_client_id'])) { |
| 154 |
$data[$profile]['csm_client_id'] = self::DEFAULT_CLIENT_ID; |
| 155 |
} |
| 156 |
return Promise\Create::promiseFor(new Configuration($data[$profile]['csm_enabled'], $data[$profile]['csm_host'], $data[$profile]['csm_port'], $data[$profile]['csm_client_id'])); |
| 157 |
}; |
| 158 |
} |
| 159 |
/** |
| 160 |
* Unwraps a configuration object in whatever valid form it is in, |
| 161 |
* always returning a ConfigurationInterface object. |
| 162 |
* |
| 163 |
* @param mixed $config |
| 164 |
* @return ConfigurationInterface |
| 165 |
* @throws \InvalidArgumentException |
| 166 |
*/ |
| 167 |
public static function unwrap($config) |
| 168 |
{ |
| 169 |
if (\is_callable($config)) { |
| 170 |
$config = $config(); |
| 171 |
} |
| 172 |
if ($config instanceof PromiseInterface) { |
| 173 |
$config = $config->wait(); |
| 174 |
} |
| 175 |
if ($config instanceof ConfigurationInterface) { |
| 176 |
return $config; |
| 177 |
} elseif (\is_array($config) && isset($config['enabled'])) { |
| 178 |
$client_id = isset($config['client_id']) ? $config['client_id'] : self::DEFAULT_CLIENT_ID; |
| 179 |
$host = isset($config['host']) ? $config['host'] : self::DEFAULT_HOST; |
| 180 |
$port = isset($config['port']) ? $config['port'] : self::DEFAULT_PORT; |
| 181 |
return new Configuration($config['enabled'], $host, $port, $client_id); |
| 182 |
} |
| 183 |
throw new \InvalidArgumentException('Not a valid CSM configuration ' . 'argument.'); |
| 184 |
} |
| 185 |
} |
| 186 |
|