| 1 |
<?php |
| 2 |
namespace Aws\ClientSideMonitoring; |
| 3 |
|
| 4 |
use Aws\AbstractConfigurationProvider; |
| 5 |
use Aws\CacheInterface; |
| 6 |
use Aws\ClientSideMonitoring\Exception\ConfigurationException; |
| 7 |
use Aws\ConfigurationProviderInterface; |
| 8 |
use GuzzleHttp\Promise; |
| 9 |
use GuzzleHttp\Promise\PromiseInterface; |
| 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 |
|
| 45 |
class ConfigurationProvider extends AbstractConfigurationProvider |
| 46 |
implements ConfigurationProviderInterface |
| 47 |
{ |
| 48 |
const DEFAULT_CLIENT_ID = ''; |
| 49 |
const DEFAULT_ENABLED = false; |
| 50 |
const DEFAULT_HOST = '127.0.0.1'; |
| 51 |
const DEFAULT_PORT = 31000; |
| 52 |
const ENV_CLIENT_ID = 'AWS_CSM_CLIENT_ID'; |
| 53 |
const ENV_ENABLED = 'AWS_CSM_ENABLED'; |
| 54 |
const ENV_HOST = 'AWS_CSM_HOST'; |
| 55 |
const ENV_PORT = 'AWS_CSM_PORT'; |
| 56 |
const ENV_PROFILE = 'AWS_PROFILE'; |
| 57 |
|
| 58 |
public static $cacheKey = 'aws_cached_csm_config'; |
| 59 |
|
| 60 |
protected static $interfaceClass = ConfigurationInterface::class; |
| 61 |
protected static $exceptionClass = ConfigurationException::class; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public static function defaultProvider(array $config = []) |
| 79 |
{ |
| 80 |
$configProviders = [self::env()]; |
| 81 |
if ( |
| 82 |
!isset($config['use_aws_shared_config_files']) |
| 83 |
|| $config['use_aws_shared_config_files'] != false |
| 84 |
) { |
| 85 |
$configProviders[] = self::ini(); |
| 86 |
} |
| 87 |
$configProviders[] = self::fallback(); |
| 88 |
|
| 89 |
$memo = self::memoize( |
| 90 |
call_user_func_array([ConfigurationProvider::class, 'chain'], $configProviders) |
| 91 |
); |
| 92 |
|
| 93 |
if (isset($config['csm']) && $config['csm'] instanceof CacheInterface) { |
| 94 |
return self::cache($memo, $config['csm'], self::$cacheKey); |
| 95 |
} |
| 96 |
|
| 97 |
return $memo; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public static function env() |
| 106 |
{ |
| 107 |
return function () { |
| 108 |
|
| 109 |
$enabled = getenv(self::ENV_ENABLED); |
| 110 |
if ($enabled !== false) { |
| 111 |
return Promise\Create::promiseFor( |
| 112 |
new Configuration( |
| 113 |
$enabled, |
| 114 |
getenv(self::ENV_HOST) ?: self::DEFAULT_HOST, |
| 115 |
getenv(self::ENV_PORT) ?: self::DEFAULT_PORT, |
| 116 |
getenv(self:: ENV_CLIENT_ID) ?: self::DEFAULT_CLIENT_ID |
| 117 |
) |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
return self::reject('Could not find environment variable CSM config' |
| 122 |
. ' in ' . self::ENV_ENABLED. '/' . self::ENV_HOST . '/' |
| 123 |
. self::ENV_PORT . '/' . self::ENV_CLIENT_ID); |
| 124 |
}; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public static function fallback() |
| 133 |
{ |
| 134 |
return function() { |
| 135 |
return Promise\Create::promiseFor( |
| 136 |
new Configuration( |
| 137 |
self::DEFAULT_ENABLED, |
| 138 |
self::DEFAULT_HOST, |
| 139 |
self::DEFAULT_PORT, |
| 140 |
self::DEFAULT_CLIENT_ID |
| 141 |
) |
| 142 |
); |
| 143 |
}; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public static function ini($profile = null, $filename = null) |
| 159 |
{ |
| 160 |
$filename = $filename ?: (self::getDefaultConfigFilename()); |
| 161 |
$profile = $profile ?: (getenv(self::ENV_PROFILE) ?: 'aws_csm'); |
| 162 |
|
| 163 |
return function () use ($profile, $filename) { |
| 164 |
if (!@is_readable($filename)) { |
| 165 |
return self::reject("Cannot read CSM config from $filename"); |
| 166 |
} |
| 167 |
$data = \Aws\parse_ini_file($filename, true); |
| 168 |
if ($data === false) { |
| 169 |
return self::reject("Invalid config file: $filename"); |
| 170 |
} |
| 171 |
if (!isset($data[$profile])) { |
| 172 |
return self::reject("'$profile' not found in config file"); |
| 173 |
} |
| 174 |
if (!isset($data[$profile]['csm_enabled'])) { |
| 175 |
return self::reject("Required CSM config values not present in |
| 176 |
INI profile '{$profile}' ({$filename})"); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
if (empty($data[$profile]['csm_host'])) { |
| 181 |
$data[$profile]['csm_host'] = self::DEFAULT_HOST; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
if (!filter_var($data[$profile]['csm_port'] ?? null, FILTER_VALIDATE_INT)) { |
| 186 |
$data[$profile]['csm_port'] = self::DEFAULT_PORT; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
if (empty($data[$profile]['csm_client_id'])) { |
| 191 |
$data[$profile]['csm_client_id'] = self::DEFAULT_CLIENT_ID; |
| 192 |
} |
| 193 |
|
| 194 |
return Promise\Create::promiseFor( |
| 195 |
new Configuration( |
| 196 |
$data[$profile]['csm_enabled'], |
| 197 |
$data[$profile]['csm_host'], |
| 198 |
$data[$profile]['csm_port'], |
| 199 |
$data[$profile]['csm_client_id'] |
| 200 |
) |
| 201 |
); |
| 202 |
}; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public static function unwrap($config) |
| 214 |
{ |
| 215 |
if (is_callable($config)) { |
| 216 |
$config = $config(); |
| 217 |
} |
| 218 |
if ($config instanceof PromiseInterface) { |
| 219 |
$config = $config->wait(); |
| 220 |
} |
| 221 |
if ($config instanceof ConfigurationInterface) { |
| 222 |
return $config; |
| 223 |
} elseif (is_array($config) && isset($config['enabled'])) { |
| 224 |
$client_id = isset($config['client_id']) ? $config['client_id'] |
| 225 |
: self::DEFAULT_CLIENT_ID; |
| 226 |
$host = isset($config['host']) ? $config['host'] |
| 227 |
: self::DEFAULT_HOST; |
| 228 |
$port = isset($config['port']) ? $config['port'] |
| 229 |
: self::DEFAULT_PORT; |
| 230 |
return new Configuration($config['enabled'], $host, $port, $client_id); |
| 231 |
} |
| 232 |
|
| 233 |
throw new \InvalidArgumentException('Not a valid CSM configuration ' |
| 234 |
. 'argument.'); |
| 235 |
} |
| 236 |
} |