| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Settings\Source; |
| 4 |
|
| 5 |
use DnsSrvDiscoverer; |
| 6 |
use GuzzleHttp\Client; |
| 7 |
use GuzzleHttp\Exception\ClientException; |
| 8 |
use GuzzleHttp\Exception\ConnectException; |
| 9 |
use GuzzleHttp\Exception\ServerException; |
| 10 |
use GuzzleHttp\Psr7\Uri; |
| 11 |
use MediaWiki\Settings\Cache\CacheableSource; |
| 12 |
use MediaWiki\Settings\SettingsBuilderException; |
| 13 |
use MediaWiki\Settings\Source\Format\JsonFormat; |
| 14 |
use Stringable; |
| 15 |
use UnexpectedValueException; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class EtcdSource implements Stringable, CacheableSource { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
private const TIMEOUT = 2; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
private const EXPIRY_TTL = 10; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
private const EXPIRY_WEIGHT = 1.0; |
| 45 |
|
| 46 |
|
| 47 |
private $client; |
| 48 |
|
| 49 |
|
| 50 |
private $uri; |
| 51 |
|
| 52 |
|
| 53 |
private $mapper; |
| 54 |
|
| 55 |
|
| 56 |
private $resolver; |
| 57 |
|
| 58 |
|
| 59 |
private $format; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function __construct( |
| 91 |
array $params = [], |
| 92 |
?callable $mapper = null, |
| 93 |
?Client $client = null, |
| 94 |
?callable $resolver = null |
| 95 |
) { |
| 96 |
$params += [ |
| 97 |
'host' => '', |
| 98 |
'port' => 2379, |
| 99 |
'protocol' => 'https', |
| 100 |
'directory' => 'mediawiki', |
| 101 |
'discover' => true, |
| 102 |
'service' => null, |
| 103 |
]; |
| 104 |
|
| 105 |
$service = |
| 106 |
$params['service'] ?? |
| 107 |
$params['protocol'] == 'https' |
| 108 |
? 'etcd-client-ssl' |
| 109 |
: 'etcd-client'; |
| 110 |
|
| 111 |
$this->mapper = $mapper ?? static function ( $settings ) { |
| 112 |
return $settings; |
| 113 |
}; |
| 114 |
|
| 115 |
$this->client = $client ?? new Client( [ |
| 116 |
'timeout' => self::TIMEOUT, |
| 117 |
'connect_timeout' => self::TIMEOUT, |
| 118 |
] ); |
| 119 |
|
| 120 |
$this->uri = ( new Uri() ) |
| 121 |
->withHost( $params['host'] ) |
| 122 |
->withPort( $params['port'] ) |
| 123 |
->withPath( '/v2/keys/' . trim( $params['directory'], '/' ) . '/' ) |
| 124 |
->withScheme( $params['protocol'] ) |
| 125 |
->withQuery( 'recursive=true' ); |
| 126 |
|
| 127 |
if ( $resolver !== null ) { |
| 128 |
$this->resolver = $resolver; |
| 129 |
} elseif ( $params['discover'] ) { |
| 130 |
$discoverer = new DnsSrvDiscoverer( $service, 'tcp', $params['host'] ); |
| 131 |
$this->uri = $this->uri->withHost( $discoverer->getSrvName() )->withPort( null ); |
| 132 |
$this->resolver = static function () use ( $discoverer ) { |
| 133 |
return $discoverer->getServers(); |
| 134 |
}; |
| 135 |
} else { |
| 136 |
$this->resolver = static function () use ( $params ) { |
| 137 |
return [ [ $params['host'], $params['port'] ] ]; |
| 138 |
}; |
| 139 |
} |
| 140 |
|
| 141 |
$this->format = new JsonFormat(); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public function allowsStaleLoad(): bool { |
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public function load(): array { |
| 161 |
$lastException = false; |
| 162 |
|
| 163 |
foreach ( ( $this->resolver )() as [ $host, $port ] ) { |
| 164 |
try { |
| 165 |
return $this->loadFromEtcdServer( $host, $port ); |
| 166 |
} catch ( ConnectException | ServerException $e ) { |
| 167 |
$lastException = $e; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
throw new SettingsBuilderException( |
| 172 |
'failed to load settings from etcd source: {source}: {message}', |
| 173 |
[ |
| 174 |
'source' => $this, |
| 175 |
'message' => $lastException ? $lastException->getMessage() : '', |
| 176 |
] |
| 177 |
); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public function getExpiryTtl(): int { |
| 186 |
return self::EXPIRY_TTL; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public function getExpiryWeight(): float { |
| 196 |
return self::EXPIRY_WEIGHT; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public function getHashKey(): string { |
| 208 |
return (string)$this->uri; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public function __toString(): string { |
| 217 |
return (string)$this->uri; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
private function loadFromEtcdServer( string $host, int $port ): array { |
| 228 |
$uri = $this->uri->withHost( $host )->withPort( $port ); |
| 229 |
|
| 230 |
try { |
| 231 |
$response = $this->client->get( $uri, [ 'http_errors' => true ] ); |
| 232 |
} catch ( ClientException $e ) { |
| 233 |
throw new SettingsBuilderException( |
| 234 |
'bad request made to etcd server: {message}: uri {uri}', |
| 235 |
[ 'message' => $e->getMessage(), 'uri' => $uri ] |
| 236 |
); |
| 237 |
} |
| 238 |
|
| 239 |
$settings = []; |
| 240 |
|
| 241 |
try { |
| 242 |
$resp = $this->format->decode( $response->getBody()->getContents() ); |
| 243 |
|
| 244 |
if ( |
| 245 |
!isset( $resp['node'] ) || !is_array( $resp['node'] ) |
| 246 |
|| !isset( $resp['node']['dir'] ) || !$resp['node']['dir'] |
| 247 |
) { |
| 248 |
throw new SettingsBuilderException( |
| 249 |
'etcd request to {uri} did not return a valid directory node', |
| 250 |
[ 'uri' => $uri ] |
| 251 |
); |
| 252 |
} |
| 253 |
|
| 254 |
$this->parseDirectory( |
| 255 |
$resp['node'], |
| 256 |
strlen( $resp['node']['key'] ) + 1, |
| 257 |
$settings |
| 258 |
); |
| 259 |
} catch ( UnexpectedValueException $e ) { |
| 260 |
throw new SettingsBuilderException( |
| 261 |
'failed to parse etcd response body: {message}', |
| 262 |
[ 'message' => $e->getMessage() ] |
| 263 |
); |
| 264 |
} |
| 265 |
|
| 266 |
return ( $this->mapper )( $settings ); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
private function parseDirectory( array $dir, int $prefix, array &$settings ) { |
| 275 |
foreach ( $dir['nodes'] as $node ) { |
| 276 |
if ( isset( $node['dir'] ) && $node['dir'] ) { |
| 277 |
$this->parseDirectory( $node, $prefix, $settings ); |
| 278 |
} else { |
| 279 |
$key = substr( $node['key'], $prefix ); |
| 280 |
$value = $this->format->decode( $node['value'] ); |
| 281 |
$settings[$key] = $value['val']; |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
} |