-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathConfig.php
615 lines (546 loc) · 15.6 KB
/
Config.php
1
2
3
4
5
6
7
8
9
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
<?php
declare(strict_types=1);
namespace DCarbone\PHPConsulAPI;
/*
Copyright 2016-2021 Daniel Carbone ([email protected])
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
use DCarbone\Go\Time;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\RequestOptions;
/**
* Class Config
*/
class Config
{
use Unmarshaller;
public const DEFAULT_REQUEST_OPTIONS = [
RequestOptions::HTTP_ERRORS => false,
RequestOptions::DECODE_CONTENT => false,
];
protected const FIELDS = [
self::FIELD_HTTP_AUTH => [
Transcoding::FIELD_UNMARSHAL_CALLBACK => 'setHttpAuth',
],
self::FIELD_WAIT_TIME => [
Transcoding::FIELD_UNMARSHAL_CALLBACK => Transcoding::UNMARSHAL_NULLABLE_DURATION,
],
];
private const FIELD_HTTP_AUTH = 'HttpAuth';
private const FIELD_WAIT_TIME = 'WaitTime';
private const FIELD_ADDRESS = 'Address';
private const FIELD_SCHEME = 'Scheme';
private const FIELD_JSON_ENCODE_OPTS = 'JSONEncodeOpts';
private const FIELD_TOKEN = 'Token';
private const FIELD_TOKEN_FILE = 'TokenFile';
private const FIELD_CA_FILE = 'CAFile';
private const FIELD_CERT_FILE = 'CertFile';
private const FIELD_KEY_FILE = 'KeyFile';
private const FIELD_INSECURE_SKIP_VERIFY = 'InsecureSkipVerify';
private const DEFAULT_CONFIG = [
self::FIELD_ADDRESS => '127.0.0.1:8500',
self::FIELD_SCHEME => 'http',
self::FIELD_JSON_ENCODE_OPTS => \JSON_UNESCAPED_SLASHES,
];
/**
* The address, including port, of your Consul Agent
*
* @var string
*/
public string $Address = '';
/**
* The scheme to use. Currently only HTTP and HTTPS are supported.
*
* @var string
*/
public string $Scheme = '';
/**
* The name of the datacenter you wish all queries to be made against by default
*
* @var string
*/
public string $Datacenter = '';
/**
* @var string
*/
public string $Namespace = '';
/**
* HTTP authentication, if used
*
* @var \DCarbone\PHPConsulAPI\HttpAuth|null
*/
public ?HttpAuth $HttpAuth = null;
/**
* Time to wait on certain blockable endpoints
*
* @var \DCarbone\Go\Time\Duration|null
*/
public ?Time\Duration $WaitTime = null;
/**
* ACL token to use by default
*
* @var string
*/
public string $Token = '';
/**
* File containing the current token to use for this client.
*
* If provided, it is read once at startup and never again
*
* @var string
*/
public string $TokenFile = '';
/**
* Optional path to CA certificate
*
* @var string
*/
public string $CAFile = '';
/**
* Optional path to certificate. If set, KeyFile must also be set
*
* @var string
*/
public string $CertFile = '';
/**
* Optional path to private key. If set, CertFile must also be set
*
* @var string
*/
public string $KeyFile = '';
/**
* Whether to skip SSL validation. This does nothing unless you use it within your HttpClient of choice.
*
* @var bool
*/
public bool $InsecureSkipVerify = false;
/**
* Your HttpClient of choice.
*
* @var \GuzzleHttp\ClientInterface
*/
public ClientInterface $HttpClient;
/**
* Bitwise options to provide to JSON encoder when encoding request bodies
*
* @var int
*/
public int $JSONEncodeOpts = 0;
/**
* Config constructor.
* @param array $config
*/
public function __construct(array $config = [])
{
foreach ($config + self::_getDefaultConfig() as $k => $v) {
$this->unmarshalField($k, $v);
}
// quick validation on key/cert combo
$c = $this->CertFile;
$k = $this->KeyFile;
if (('' !== $k && '' === $c) || ('' !== $c && '' === $k)) {
throw new \InvalidArgumentException(
sprintf(
'%s - CertFile and KeyFile must be both either empty or populated. Key: %s; Cert: %s',
static::class,
$k,
$c
)
);
}
// if client hasn't been constructed, construct.
if (!isset($this->HttpClient)) {
$this->HttpClient = new Client();
}
}
/**
* @param \DCarbone\PHPConsulAPI\Config|null $inc
* @return \DCarbone\PHPConsulAPI\Config
*/
public static function merge(?self $inc): self
{
$actual = static::newDefaultConfig();
if (null === $inc) {
return $actual;
}
if ('' !== $inc->Address) {
$actual->Address = $inc->Address;
}
if ('' !== $inc->Scheme) {
$actual->Scheme = $inc->Scheme;
}
if ('' !== $inc->Datacenter) {
$actual->Datacenter = $inc->Datacenter;
}
if ('' !== $inc->Namespace) {
$actual->Namespace = $inc->Namespace;
}
if (isset($inc->HttpAuth)) {
$actual->HttpAuth = clone $inc->HttpAuth;
}
if (isset($inc->WaitTime)) {
$actual->WaitTime = Time::Duration($inc->WaitTime);
}
if ('' !== $inc->Token) {
$actual->Token = $inc->Token;
}
if ('' !== $inc->TokenFile) {
$actual->TokenFile = $inc->TokenFile;
}
if ('' !== $inc->CAFile) {
$actual->CAFile = $inc->CAFile;
}
if ('' !== $inc->CertFile) {
$actual->CertFile = $inc->CertFile;
}
if ('' !== $inc->KeyFile) {
$actual->KeyFile = $inc->KeyFile;
}
if ($inc->InsecureSkipVerify) {
$actual->InsecureSkipVerify = true;
}
if (null !== $inc->HttpClient) {
$actual->HttpClient = $inc->HttpClient;
}
if (0 !== $inc->JSONEncodeOpts) {
$actual->JSONEncodeOpts = $inc->JSONEncodeOpts;
}
return $actual;
}
/**
* Construct a configuration object from Environment Variables and use bare guzzle client instance
*
* @return \DCarbone\PHPConsulAPI\Config
*/
public static function newDefaultConfig(): self
{
return new static(self::_getDefaultConfig());
}
/**
* @return string
*/
public function getAddress(): string
{
return $this->Address;
}
/**
* @param string $address
* @return \DCarbone\PHPConsulAPI\Config
*/
public function setAddress(string $address): self
{
$this->Address = $address;
return $this;
}
/**
* @return string
*/
public function getScheme(): string
{
return $this->Scheme;
}
/**
* @param string $scheme
* @return \DCarbone\PHPConsulAPI\Config
*/
public function setScheme(string $scheme): self
{
$this->Scheme = $scheme;
return $this;
}
/**
* @return string
*/
public function getDatacenter(): string
{
return $this->Datacenter;
}
/**
* @param string $datacenter
* @return \DCarbone\PHPConsulAPI\Config
*/
public function setDatacenter(string $datacenter): self
{
$this->Datacenter = $datacenter;
return $this;
}
/**
* @return string
*/
public function getNamespace(): string
{
return $this->Namespace;
}
/**
* @param string $namespace
*/
public function setNamespace(string $namespace): void
{
$this->Namespace = $namespace;
}
/**
* @return \DCarbone\Go\Time\Duration
*/
public function getWaitTime(): Time\Duration
{
return $this->WaitTime;
}
/**
* @param mixed $waitTime
* @return \DCarbone\PHPConsulAPI\Config
*/
public function setWaitTime(mixed $waitTime): self
{
$this->WaitTime = Time::Duration($waitTime);
return $this;
}
/**
* @return string
*/
public function getToken(): string
{
return $this->Token;
}
/**
* @param string $token
* @return \DCarbone\PHPConsulAPI\Config
*/
public function setToken(string $token): self
{
$this->Token = $token;
return $this;
}
/**
* @return string
*/
public function getTokenFile(): string
{
return $this->TokenFile;
}
/**
* @param string $tokenFile
* @return \DCarbone\PHPConsulAPI\Config
*/
public function setTokenFile(string $tokenFile): self
{
$this->TokenFile = $tokenFile;
return $this;
}
/**
* @return bool
*/
public function isInsecureSkipVerify(): bool
{
return $this->InsecureSkipVerify;
}
/**
* @param bool $insecureSkipVerify
* @return \DCarbone\PHPConsulAPI\Config
*/
public function setInsecureSkipVerify(bool $insecureSkipVerify): self
{
$this->InsecureSkipVerify = $insecureSkipVerify;
return $this;
}
/**
* @return \DCarbone\PHPConsulAPI\HttpAuth
*/
public function getHttpAuth(): HttpAuth
{
return $this->HttpAuth;
}
/**
* @param \DCarbone\PHPConsulAPI\HttpAuth|string $httpAuth
* @return \DCarbone\PHPConsulAPI\Config
*/
public function setHttpAuth(HttpAuth|string $httpAuth): self
{
if (\is_string($httpAuth)) {
$colon = strpos($httpAuth, ':');
if (false === $colon) {
$username = $httpAuth;
$password = null;
} else {
$username = substr($httpAuth, 0, $colon);
$password = substr($httpAuth, $colon + 1);
}
$httpAuth = new HttpAuth($username, $password);
}
if ($httpAuth instanceof HttpAuth) {
$this->HttpAuth = $httpAuth;
return $this;
}
throw new \InvalidArgumentException(
sprintf(
'%s::setHttpAuth - Value is expected to be string of "username:password" or instance of "\\DCarbone\\PHPConsulApi\\HttpAuth", %s seen.',
static::class,
\is_string($httpAuth) ? $httpAuth : \gettype($httpAuth)
)
);
}
/**
* @return string
*/
public function getCAFile(): string
{
return $this->CAFile;
}
/**
* @param string $caFile
* @return \DCarbone\PHPConsulAPI\Config
*/
public function setCAFile(string $caFile): self
{
$this->CAFile = $caFile;
return $this;
}
/**
* @return string
*/
public function getCertFile(): string
{
return $this->CertFile;
}
/**
* @param string $certFile
* @return \DCarbone\PHPConsulAPI\Config
*/
public function setCertFile(string $certFile): self
{
$this->CertFile = $certFile;
return $this;
}
/**
* @return string
*/
public function getKeyFile(): string
{
return $this->KeyFile;
}
/**
* @param string $keyFile
* @return \DCarbone\PHPConsulAPI\Config
*/
public function setKeyFile(string $keyFile): self
{
$this->KeyFile = $keyFile;
return $this;
}
/**
* @return \GuzzleHttp\ClientInterface
*/
public function getHttpClient(): ClientInterface
{
return $this->HttpClient;
}
/**
* @param \GuzzleHttp\ClientInterface $httpClient
* @return \DCarbone\PHPConsulAPI\Config
*/
public function setHttpClient(ClientInterface $httpClient): self
{
$this->HttpClient = $httpClient;
return $this;
}
/**
* @return int
*/
public function getJSONEncodeOpts(): int
{
return $this->JSONEncodeOpts;
}
/**
* @param int $jsonEncodeOpts
* @return \DCarbone\PHPConsulAPI\Config
*/
public function setJSONEncodeOpts(int $jsonEncodeOpts): self
{
$this->JSONEncodeOpts = $jsonEncodeOpts;
return $this;
}
/**
* @return array
*/
public static function getEnvironmentConfig(): array
{
$ret = [];
foreach (
[
Consul::HTTPAddrEnvName => static::_tryGetEnvParam(Consul::HTTPAddrEnvName),
Consul::HTTPTokenEnvName => static::_tryGetEnvParam(Consul::HTTPTokenEnvName),
Consul::HTTPTokenFileEnvName => static::_tryGetEnvParam(Consul::HTTPTokenFileEnvName),
Consul::HTTPAuthEnvName => static::_tryGetEnvParam(Consul::HTTPAuthEnvName),
Consul::HTTPCAFileEnvName => static::_tryGetEnvParam(Consul::HTTPCAFileEnvName),
Consul::HTTPClientCertEnvName => static::_tryGetEnvParam(Consul::HTTPClientCertEnvName),
Consul::HTTPClientKeyEnvName => static::_tryGetEnvParam(Consul::HTTPClientKeyEnvName),
Consul::HTTPSSLEnvName => static::_tryGetEnvParam(Consul::HTTPSSLEnvName),
Consul::HTTPSSLVerifyEnvName => static::_tryGetEnvParam(Consul::HTTPSSLVerifyEnvName),
] as $k => $v
) {
if (null !== $v) {
$ret[$k] = $v;
}
}
return $ret;
}
/**
* @param string $param
* @return string|null
*/
protected static function _tryGetEnvParam(string $param): ?string
{
if (isset($_ENV[$param])) {
return $_ENV[$param];
}
if (false !== ($value = getenv($param))) {
return $value;
}
if (isset($_SERVER[$param])) {
return $_SERVER[$param];
}
return null;
}
/**
* @return array
*/
private static function _getDefaultConfig(): array
{
$conf = self::DEFAULT_CONFIG;
// parse env vars
foreach (static::getEnvironmentConfig() as $k => $v) {
if (Consul::HTTPAddrEnvName === $k) {
$conf[self::FIELD_ADDRESS] = $v;
} elseif (Consul::HTTPTokenEnvName === $k) {
$conf[self::FIELD_TOKEN] = $v;
} elseif (Consul::HTTPTokenFileEnvName === $k) {
$conf[self::FIELD_TOKEN_FILE] = $v;
} elseif (Consul::HTTPAuthEnvName === $k) {
$conf[self::FIELD_HTTP_AUTH] = $v;
} elseif (Consul::HTTPCAFileEnvName === $k) {
$conf[self::FIELD_CA_FILE] = $v;
} elseif (Consul::HTTPClientCertEnvName === $k) {
$conf[self::FIELD_CERT_FILE] = $v;
} elseif (Consul::HTTPClientKeyEnvName === $k) {
$conf[self::FIELD_KEY_FILE] = $v;
} elseif (Consul::HTTPSSLEnvName === $k) {
if ((bool)$v) {
$conf[self::FIELD_SCHEME] = 'https';
}
} elseif (Consul::HTTPSSLVerifyEnvName === $k) {
if ((bool)$v) {
$conf[self::FIELD_INSECURE_SKIP_VERIFY] = true;
}
}
}
return $conf;
}
}