17
17
*/
18
18
use GuzzleHttp \Client ;
19
19
use GuzzleHttp \ClientInterface ;
20
+ use GuzzleHttp \RequestOptions ;
20
21
21
22
/**
22
23
* Class Config
@@ -65,6 +66,27 @@ class Config {
65
66
*/
66
67
public $ Token = '' ;
67
68
69
+ /**
70
+ * Optional path to CA certificate
71
+ *
72
+ * @var string
73
+ */
74
+ public $ CAFile = '' ;
75
+
76
+ /**
77
+ * Optional path to certificate. If set, KeyFile must also be set
78
+ *
79
+ * @var string
80
+ */
81
+ public $ CertFile = '' ;
82
+
83
+ /**
84
+ * Optional path to private key. If set, CertFile must also be set
85
+ *
86
+ * @var string
87
+ */
88
+ public $ KeyFile = '' ;
89
+
68
90
/**
69
91
* Whether to skip SSL validation. This does nothing unless you use it within your HttpClient of choice.
70
92
*
@@ -91,13 +113,30 @@ class Config {
91
113
* @param array $config
92
114
*/
93
115
public function __construct (array $ config = []) {
94
- foreach ($ config as $ k => $ v ) {
116
+ foreach ($ config + self :: getDefaultConfig () as $ k => $ v ) {
95
117
$ this ->{"set {$ k }" }($ v );
96
118
}
97
119
98
120
if (null !== $ this ->HttpAuth && !isset ($ this ->HttpAuth )) {
99
121
$ this ->HttpAuth = new HttpAuth ();
100
122
}
123
+
124
+ // quick validation on key/cert combo
125
+ $ c = $ this ->getCertFile ();
126
+ $ k = $ this ->getKeyFile ();
127
+ if (('' !== $ k && '' === $ c ) || ('' !== $ c && '' === $ k )) {
128
+ throw new \InvalidArgumentException (sprintf (
129
+ '%s - CertFile and KeyFile must be both either empty or populated. Key: %s; Cert: %s ' ,
130
+ get_class ($ this ),
131
+ $ k ,
132
+ $ c
133
+ ));
134
+ }
135
+
136
+ // if client hasn't been constructed, construct.
137
+ if (null === $ this ->HttpClient ) {
138
+ $ this ->HttpClient = new Client ();
139
+ }
101
140
}
102
141
103
142
/**
@@ -106,34 +145,52 @@ public function __construct(array $config = []) {
106
145
* @return \DCarbone\PHPConsulAPI\Config
107
146
*/
108
147
public static function newDefaultConfig () {
109
- $ conf = new static ([
148
+ return new static (self ::getDefaultConfig ());
149
+ }
150
+
151
+ /**
152
+ * @return array
153
+ */
154
+ private static function getDefaultConfig () {
155
+ $ conf = [
110
156
'Address ' => '127.0.0.1:8500 ' ,
111
157
'Scheme ' => 'http ' ,
112
- ]);
113
-
114
- $ envParams = static ::getEnvironmentConfig ();
115
- if (isset ($ envParams [Consul::HTTPAddrEnvName])) {
116
- $ conf ->setAddress ($ envParams [Consul::HTTPAddrEnvName]);
117
- }
118
-
119
- if (isset ($ envParams [Consul::HTTPTokenEnvName])) {
120
- $ conf ->setToken ($ envParams [Consul::HTTPTokenEnvName]);
121
- }
122
-
123
- if (isset ($ envParams [Consul::HTTPAuthEnvName])) {
124
- $ conf ->setHttpAuth ($ envParams [Consul::HTTPAuthEnvName]);
125
- }
126
-
127
- if (isset ($ envParams [Consul::HTTPSSLEnvName]) && $ envParams [Consul::HTTPSSLEnvName]) {
128
- $ conf ->setScheme ('https ' );
129
- }
130
-
131
- if (isset ($ envParams [Consul::HTTPSSLVerifyEnvName]) && !$ envParams [Consul::HTTPSSLVerifyEnvName]) {
132
- $ conf ->setInsecureSkipVerify (false );
158
+ ];
159
+
160
+ // parse env vars
161
+ foreach (static ::getEnvironmentConfig () as $ k => $ v ) {
162
+ switch ($ k ) {
163
+ case Consul::HTTPAddrEnvName:
164
+ $ conf ['Address ' ] = $ v ;
165
+ break ;
166
+ case Consul::HTTPTokenEnvName:
167
+ $ conf ['Token ' ] = $ v ;
168
+ break ;
169
+ case Consul::HTTPAuthEnvName:
170
+ $ conf ['HttpAuth ' ] = $ v ;
171
+ break ;
172
+ case Consul::HTTPCAFileEnvName:
173
+ $ conf ['CAFile ' ] = $ v ;
174
+ break ;
175
+ case Consul::HTTPClientCertEnvName:
176
+ $ conf ['CertFile ' ] = $ v ;
177
+ break ;
178
+ case Consul::HTTPClientKeyEnvName:
179
+ $ conf ['KeyFile ' ] = $ v ;
180
+ break ;
181
+ case Consul::HTTPSSLEnvName:
182
+ if ((bool )$ v ) {
183
+ $ conf ['Scheme ' ] = 'https ' ;
184
+ }
185
+ break ;
186
+ case Consul::HTTPSSLVerifyEnvName:
187
+ if ((bool )$ v ) {
188
+ $ conf ['InsecureSkipVerify ' ] = true ;
189
+ }
190
+ break ;
191
+ }
133
192
}
134
193
135
- $ conf ->setHttpClient (new Client ());
136
-
137
194
return $ conf ;
138
195
}
139
196
@@ -269,6 +326,54 @@ public function setHttpAuth($HttpAuth) {
269
326
));
270
327
}
271
328
329
+ /**
330
+ * @return string
331
+ */
332
+ public function getCAFile () {
333
+ return $ this ->CAFile ;
334
+ }
335
+
336
+ /**
337
+ * @param string $CAFile
338
+ * @return \DCarbone\PHPConsulAPI\Config
339
+ */
340
+ public function setCAFile ($ CAFile ) {
341
+ $ this ->CAFile = $ CAFile ;
342
+ return $ this ;
343
+ }
344
+
345
+ /**
346
+ * @return string
347
+ */
348
+ public function getCertFile () {
349
+ return $ this ->CertFile ;
350
+ }
351
+
352
+ /**
353
+ * @param string $CertFile
354
+ * @return \DCarbone\PHPConsulAPI\Config
355
+ */
356
+ public function setCertFile ($ CertFile ) {
357
+ $ this ->CertFile = $ CertFile ;
358
+ return $ this ;
359
+ }
360
+
361
+ /**
362
+ * @return string
363
+ */
364
+ public function getKeyFile () {
365
+ return $ this ->KeyFile ;
366
+ }
367
+
368
+ /**
369
+ * @param string $KeyFile
370
+ * @return \DCarbone\PHPConsulAPI\Config
371
+ */
372
+ public function setKeyFile ($ KeyFile ) {
373
+ $ this ->KeyFile = $ KeyFile ;
374
+ return $ this ;
375
+ }
376
+
272
377
/**
273
378
* @return \GuzzleHttp\ClientInterface
274
379
*/
@@ -319,15 +424,42 @@ public function intToMillisecond($in) {
319
424
return sprintf ('%dms ' , $ ms );
320
425
}
321
426
427
+ /**
428
+ * @return array
429
+ */
430
+ public function getGuzzleRequestOptions () {
431
+ // TODO: Define once?
432
+ $ opts = [
433
+ RequestOptions::HTTP_ERRORS => false ,
434
+ RequestOptions::DECODE_CONTENT => false ,
435
+ ];
436
+
437
+ if (!$ this ->isInsecureSkipVerify ()) {
438
+ $ opts [RequestOptions::VERIFY ] = false ;
439
+ } else if ('' !== ($ b = $ this ->getCAFile ())) {
440
+ $ opts [RequestOptions::VERIFY ] = $ b ;
441
+ }
442
+
443
+ if ('' !== ($ c = $ this ->getCertFile ())) {
444
+ $ opts [RequestOptions::CERT ] = $ c ;
445
+ $ opts [RequestOptions::SSL_KEY ] = $ this ->getKeyFile ();
446
+ }
447
+
448
+ return $ opts ;
449
+ }
450
+
322
451
/**
323
452
* @return array
324
453
*/
325
454
public static function getEnvironmentConfig () {
326
455
return array_filter ([
327
- 'CONSUL_HTTP_ADDR ' => static ::_tryGetEnvParam ('CONSUL_HTTP_ADDR ' ),
328
- 'CONSUL_HTTP_AUTH ' => static ::_tryGetEnvParam ('CONSUL_HTTP_AUTH ' ),
329
- 'CONSUL_HTTP_SSL ' => static ::_tryGetEnvParam ('CONSUL_HTTP_SSL ' ),
330
- 'CONSUL_HTTP_SSL_VERIFY ' => static ::_tryGetEnvParam ('CONSUL_HTTP_SSL_VERIFY ' )
456
+ Consul::HTTPAddrEnvName => static ::_tryGetEnvParam (Consul::HTTPAddrEnvName),
457
+ Consul::HTTPAuthEnvName => static ::_tryGetEnvParam (Consul::HTTPAuthEnvName),
458
+ Consul::HTTPCAFileEnvName => static ::_tryGetEnvParam (Consul::HTTPCAFileEnvName),
459
+ Consul::HTTPClientCertEnvName => static ::_tryGetEnvParam (Consul::HTTPClientCertEnvName),
460
+ Consul::HTTPClientKeyEnvName => static ::_tryGetEnvParam (Consul::HTTPClientKeyEnvName),
461
+ Consul::HTTPSSLEnvName => static ::_tryGetEnvParam (Consul::HTTPSSLEnvName),
462
+ Consul::HTTPSSLVerifyEnvName => static ::_tryGetEnvParam (Consul::HTTPSSLVerifyEnvName),
331
463
],
332
464
function ($ val ) {
333
465
return null !== $ val ;
@@ -339,6 +471,10 @@ function ($val) {
339
471
* @return string|null
340
472
*/
341
473
protected static function _tryGetEnvParam ($ param ) {
474
+ if (isset ($ _ENV [$ param ])) {
475
+ return $ _ENV [$ param ];
476
+ }
477
+
342
478
if (false !== ($ value = getenv ($ param ))) {
343
479
return $ value ;
344
480
}
0 commit comments