diff --git a/composer.json b/composer.json index fdbf3ce..6b3bf9a 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "php": "^7.1", "ext-json": "*", "dcarbone/gotime": "0.3.*", - "guzzlehttp/guzzle": "~6", + "guzzlehttp/guzzle": "~7", "guzzlehttp/psr7": "~1" }, "autoload": { diff --git a/src/ACL/ACLClient.php b/src/ACL/ACLClient.php index 8296d0e..cce2403 100644 --- a/src/ACL/ACLClient.php +++ b/src/ACL/ACLClient.php @@ -1,7 +1,7 @@ EnableTagOverride; diff --git a/src/Agent/AgentServiceCheck.php b/src/Agent/AgentServiceCheck.php index 74be892..50b7e58 100644 --- a/src/Agent/AgentServiceCheck.php +++ b/src/Agent/AgentServiceCheck.php @@ -1,7 +1,7 @@ EnableTagOverride; } /** - * @param boolean $enableTagOverride + * @param bool $enableTagOverride * @return \DCarbone\PHPConsulAPI\Agent\AgentServiceRegistration */ public function setEnableTagOverride(bool $enableTagOverride): AgentServiceRegistration { diff --git a/src/Agent/GaugeValue.php b/src/Agent/GaugeValue.php index 5dd53da..8ed0f3a 100644 --- a/src/Agent/GaugeValue.php +++ b/src/Agent/GaugeValue.php @@ -1,7 +1,7 @@ '127.0.0.1:8500', + 'Scheme' => 'http', + ]; + /** * The address, including port, of your Consul Agent * @@ -67,6 +73,15 @@ class Config { */ public $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 $TokenFile = ''; + /** * Optional path to CA certificate * @@ -113,8 +128,9 @@ class Config { * Config constructor. * @param array $config */ - public function __construct(array $config = []) { - foreach ($config + self::getDefaultConfig() as $k => $v) { + public function __construct(array $config = []) + { + foreach ($config + self::_getDefaultConfig() as $k => $v) { $this->{"set{$k}"}($v); } @@ -140,19 +156,73 @@ public function __construct(array $config = []) { } } + /** + * @param \DCarbone\PHPConsulAPI\Config|null $inc + * @return \DCarbone\PHPConsulAPI\Config + */ + public static function merge(?Config $inc): Config + { + $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 (null !== $inc->HttpAuth) { + $actual->HttpAuth = clone($inc->HttpAuth); + } + if (0 !== $inc->WaitTime) { + $actual->WaitTime = $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 ($inc->TokenInHeader) { + $actual->TokenInHeader = true; + } + if (null !== $inc->HttpClient) { + $actual->HttpClient = $inc->HttpClient; + } + return $actual; + } + /** * Construct a configuration object from Environment Variables and use bare guzzle client instance * * @return \DCarbone\PHPConsulAPI\Config */ - public static function newDefaultConfig(): Config { - return new static(self::getDefaultConfig()); + public static function newDefaultConfig(): Config + { + return new static(self::_getDefaultConfig()); } /** * @return string */ - public function getAddress(): string { + public function getAddress(): string + { return $this->Address; } @@ -160,7 +230,8 @@ public function getAddress(): string { * @param string $address * @return \DCarbone\PHPConsulAPI\Config */ - public function setAddress(string $address): Config { + public function setAddress(string $address): Config + { $this->Address = $address; return $this; } @@ -168,7 +239,8 @@ public function setAddress(string $address): Config { /** * @return string */ - public function getScheme(): string { + public function getScheme(): string + { return $this->Scheme; } @@ -176,7 +248,8 @@ public function getScheme(): string { * @param string $scheme * @return \DCarbone\PHPConsulAPI\Config */ - public function setScheme(string $scheme): Config { + public function setScheme(string $scheme): Config + { $this->Scheme = $scheme; return $this; } @@ -184,7 +257,8 @@ public function setScheme(string $scheme): Config { /** * @return string */ - public function getDatacenter(): string { + public function getDatacenter(): string + { return $this->Datacenter; } @@ -192,7 +266,8 @@ public function getDatacenter(): string { * @param string $datacenter * @return \DCarbone\PHPConsulAPI\Config */ - public function setDatacenter(string $datacenter): Config { + public function setDatacenter(string $datacenter): Config + { $this->Datacenter = $datacenter; return $this; } @@ -200,7 +275,8 @@ public function setDatacenter(string $datacenter): Config { /** * @return int */ - public function getWaitTime(): int { + public function getWaitTime(): int + { return $this->WaitTime; } @@ -208,7 +284,8 @@ public function getWaitTime(): int { * @param int $waitTime * @return \DCarbone\PHPConsulAPI\Config */ - public function setWaitTime(int $waitTime): Config { + public function setWaitTime(int $waitTime): Config + { $this->WaitTime = $waitTime; return $this; } @@ -216,7 +293,8 @@ public function setWaitTime(int $waitTime): Config { /** * @return string */ - public function getToken(): string { + public function getToken(): string + { return $this->Token; } @@ -224,23 +302,44 @@ public function getToken(): string { * @param string $token * @return \DCarbone\PHPConsulAPI\Config */ - public function setToken(string $token): Config { + public function setToken(string $token): Config + { $this->Token = $token; return $this; } /** - * @return boolean + * @return string */ - public function isInsecureSkipVerify(): bool { + public function getTokenFile(): string + { + return $this->TokenFile; + } + + /** + * @param string $TokenFile + * @return \DCarbone\PHPConsulAPI\Config + */ + public function setTokenFile(string $TokenFile): Config + { + $this->TokenFile = $TokenFile; + return $this; + } + + /** + * @return bool + */ + public function isInsecureSkipVerify(): bool + { return $this->InsecureSkipVerify; } /** - * @param boolean $insecureSkipVerify + * @param bool $insecureSkipVerify * @return \DCarbone\PHPConsulAPI\Config */ - public function setInsecureSkipVerify(bool $insecureSkipVerify): Config { + public function setInsecureSkipVerify(bool $insecureSkipVerify): Config + { $this->InsecureSkipVerify = $insecureSkipVerify; return $this; } @@ -248,7 +347,8 @@ public function setInsecureSkipVerify(bool $insecureSkipVerify): Config { /** * @return \DCarbone\PHPConsulAPI\HttpAuth */ - public function getHttpAuth(): HttpAuth { + public function getHttpAuth(): HttpAuth + { return $this->HttpAuth; } @@ -256,7 +356,8 @@ public function getHttpAuth(): HttpAuth { * @param string|HttpAuth $httpAuth * @return \DCarbone\PHPConsulAPI\Config */ - public function setHttpAuth($httpAuth): Config { + public function setHttpAuth($httpAuth): Config + { if (is_string($httpAuth)) { $colon = strpos($httpAuth, ':'); if (false === $colon) { @@ -284,7 +385,8 @@ public function setHttpAuth($httpAuth): Config { /** * @return string */ - public function getCAFile(): string { + public function getCAFile(): string + { return $this->CAFile; } @@ -292,7 +394,8 @@ public function getCAFile(): string { * @param string $caFile * @return \DCarbone\PHPConsulAPI\Config */ - public function setCAFile(string $caFile): Config { + public function setCAFile(string $caFile): Config + { $this->CAFile = $caFile; return $this; } @@ -300,7 +403,8 @@ public function setCAFile(string $caFile): Config { /** * @return string */ - public function getCertFile(): string { + public function getCertFile(): string + { return $this->CertFile; } @@ -308,7 +412,8 @@ public function getCertFile(): string { * @param string $certFile * @return \DCarbone\PHPConsulAPI\Config */ - public function setCertFile(string $certFile): Config { + public function setCertFile(string $certFile): Config + { $this->CertFile = $certFile; return $this; } @@ -316,7 +421,8 @@ public function setCertFile(string $certFile): Config { /** * @return string */ - public function getKeyFile(): string { + public function getKeyFile(): string + { return $this->KeyFile; } @@ -324,7 +430,8 @@ public function getKeyFile(): string { * @param string $keyFile * @return \DCarbone\PHPConsulAPI\Config */ - public function setKeyFile(string $keyFile): Config { + public function setKeyFile(string $keyFile): Config + { $this->KeyFile = $keyFile; return $this; } @@ -332,7 +439,8 @@ public function setKeyFile(string $keyFile): Config { /** * @return \GuzzleHttp\ClientInterface */ - public function getHttpClient(): ClientInterface { + public function getHttpClient(): ClientInterface + { return $this->HttpClient; } @@ -340,23 +448,26 @@ public function getHttpClient(): ClientInterface { * @param \GuzzleHttp\ClientInterface $httpClient * @return \DCarbone\PHPConsulAPI\Config */ - public function setHttpClient(ClientInterface $httpClient): Config { + public function setHttpClient(ClientInterface $httpClient): Config + { $this->HttpClient = $httpClient; return $this; } /** - * @return boolean + * @return bool */ - public function isTokenInHeader(): bool { + public function isTokenInHeader(): bool + { return $this->TokenInHeader; } /** - * @param boolean $tokenInHeader + * @param bool $tokenInHeader * @return \DCarbone\PHPConsulAPI\Config */ - public function setTokenInHeader(bool $tokenInHeader): Config { + public function setTokenInHeader(bool $tokenInHeader): Config + { $this->TokenInHeader = $tokenInHeader; return $this; } @@ -365,7 +476,8 @@ public function setTokenInHeader(bool $tokenInHeader): Config { * @param int $in * @return string */ - public function intToMillisecond(int $in): string { + public function intToMillisecond(int $in): string + { if (!is_int($in)) { throw new \InvalidArgumentException(sprintf('$in must be integer, saw "%s".', gettype($in))); } @@ -382,10 +494,11 @@ public function intToMillisecond(int $in): string { /** * @return array */ - public function getGuzzleRequestOptions(): array { + public function getGuzzleRequestOptions(): array + { // TODO: Define once? $opts = [ - RequestOptions::HTTP_ERRORS => false, + RequestOptions::HTTP_ERRORS => false, RequestOptions::DECODE_CONTENT => false, ]; @@ -407,7 +520,8 @@ public function getGuzzleRequestOptions(): array { * @param string $param * @return string|null */ - protected static function _tryGetEnvParam(string $param) { + protected static function _tryGetEnvParam(string $param) + { if (isset($_ENV[$param])) { return $_ENV[$param]; } @@ -426,16 +540,19 @@ protected static function _tryGetEnvParam(string $param) { /** * @return array */ - public static function getEnvironmentConfig(): array { + public static function getEnvironmentConfig(): array + { $ret = []; foreach ([ - Consul::HTTPAddrEnvName => static::_tryGetEnvParam(Consul::HTTPAddrEnvName), - Consul::HTTPAuthEnvName => static::_tryGetEnvParam(Consul::HTTPAuthEnvName), - Consul::HTTPCAFileEnvName => static::_tryGetEnvParam(Consul::HTTPCAFileEnvName), + 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), + 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; @@ -447,11 +564,9 @@ public static function getEnvironmentConfig(): array { /** * @return array */ - private static function getDefaultConfig(): array { - $conf = [ - 'Address' => '127.0.0.1:8500', - 'Scheme' => 'http', - ]; + private static function _getDefaultConfig(): array + { + $conf = self::DefaultConfig; // parse env vars foreach (static::getEnvironmentConfig() as $k => $v) { @@ -459,6 +574,8 @@ private static function getDefaultConfig(): array { $conf['Address'] = $v; } else if (Consul::HTTPTokenEnvName === $k) { $conf['Token'] = $v; + } else if (Consul::HTTPTokenFileEnvName) { + $conf['TokenFile'] = $v; } else if (Consul::HTTPAuthEnvName === $k) { $conf['HttpAuth'] = $v; } else if (Consul::HTTPCAFileEnvName === $k) { diff --git a/src/Consul.php b/src/Consul.php index 6f1c607..39e9ec9 100644 --- a/src/Consul.php +++ b/src/Consul.php @@ -1,7 +1,7 @@ getAddress()) { - $config->setAddress($def->getAddress()); + public function __construct(Config $config = null) + { + $config = Config::merge($config); + + if ('' !== $config->TokenFile) { + if (!file_exists($config->TokenFile) || !is_readable($config->TokenFile)) { + throw new \RuntimeException(sprintf( + 'Provided $TokenFile "%s" either does not exist or is not readable', + $config->TokenFile + )); } - - if ('' === $config->getScheme()) { - $config->setScheme($def->getScheme()); + $data = trim(file_get_contents($config->TokenFile)); + if ('' === $config->Token && '' !== $data) { + $config->Token = $data; } } @@ -127,77 +131,88 @@ public function __construct(Config $config = null) { /** * @return \DCarbone\PHPConsulAPI\ACL\ACLClient */ - public function ACL(): ACLClient { + public function ACL(): ACLClient + { return $this->ACL; } /** * @return \DCarbone\PHPConsulAPI\Agent\AgentClient */ - public function Agent(): AgentClient { + public function Agent(): AgentClient + { return $this->Agent; } /** * @return \DCarbone\PHPConsulAPI\Catalog\CatalogClient */ - public function Catalog(): CatalogClient { + public function Catalog(): CatalogClient + { return $this->Catalog; } /** * @return \DCarbone\PHPConsulAPI\Coordinate\CoordinateClient */ - public function Coordinate(): CoordinateClient { + public function Coordinate(): CoordinateClient + { return $this->Coordinate; } /** * @return \DCarbone\PHPConsulAPI\Event\EventClient */ - public function Event(): EventClient { + public function Event(): EventClient + { return $this->Event; } /** * @return \DCarbone\PHPConsulAPI\Health\HealthClient */ - public function Health(): HealthClient { + public function Health(): HealthClient + { return $this->Health; } /** * @return \DCarbone\PHPConsulAPI\KV\KVClient */ - public function KV(): KVClient { + public function KV(): KVClient + { return $this->KV; } /** * @return \DCarbone\PHPConsulAPI\Operator\OperatorClient */ - public function Operator(): OperatorClient { + public function Operator(): OperatorClient + { return $this->Operator; } /** * @return \DCarbone\PHPConsulAPI\PreparedQuery\PreparedQueryClient */ - public function PreparedQuery(): PreparedQueryClient { + public function PreparedQuery(): PreparedQueryClient + { return $this->PreparedQuery; } /** * @return \DCarbone\PHPConsulAPI\Session\SessionClient */ - public function Session(): SessionClient { + public function Session(): SessionClient + { return $this->Session; } /** * @return \DCarbone\PHPConsulAPI\Status\StatusClient */ - public function Status(): StatusClient { + public function Status(): StatusClient + { return $this->Status; } } diff --git a/src/Coordinate/Coordinate.php b/src/Coordinate/Coordinate.php index beb25cb..19c5de4 100644 --- a/src/Coordinate/Coordinate.php +++ b/src/Coordinate/Coordinate.php @@ -1,7 +1,7 @@ Leader; @@ -99,7 +99,7 @@ public function setLeader(bool $leader): RaftServer { } /** - * @return boolean + * @return bool */ public function isVoter(): bool { return $this->Voter; diff --git a/src/Operator/ReadableDuration.php b/src/Operator/ReadableDuration.php index b3a4026..8ce7578 100644 --- a/src/Operator/ReadableDuration.php +++ b/src/Operator/ReadableDuration.php @@ -1,7 +1,7 @@ OnlyPassing; } /** - * @param boolean $onlyPassing + * @param bool $onlyPassing * @return \DCarbone\PHPConsulAPI\PreparedQuery\ServiceQuery */ public function setOnlyPassing(bool $onlyPassing): ServiceQuery { diff --git a/src/QueryMeta.php b/src/QueryMeta.php index 61c1aa2..6509bd9 100644 --- a/src/QueryMeta.php +++ b/src/QueryMeta.php @@ -1,7 +1,7 @@ KnownLeader; diff --git a/src/QueryOptions.php b/src/QueryOptions.php index a061d22..8c7bb1c 100644 --- a/src/QueryOptions.php +++ b/src/QueryOptions.php @@ -1,7 +1,7 @@