← All changes
|
includes/sdk/google/google/gax/src/ClientOptionsTrait.php
+91
-31
1.2.8
→
1.4.2
View file →
| @@ -29,15 +29,20 @@ | ||
| 29 | 29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | 30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | 31 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | 32 | */ |
| 33 | -namespace Dudlewebs\WPMCS\Google\ApiCore; | |
| 33 | +namespace Dudlewebs\WPMCS\GCP\Google\ApiCore; | |
| 34 | 34 | |
| 35 | -use Dudlewebs\WPMCS\Google\Auth\CredentialsLoader; | |
| 36 | -use Dudlewebs\WPMCS\Google\Auth\FetchAuthTokenInterface; | |
| 37 | -use Dudlewebs\WPMCS\Google\Auth\GetUniverseDomainInterface; | |
| 38 | -use Dudlewebs\WPMCS\Grpc\Gcp\ApiConfig; | |
| 39 | -use Dudlewebs\WPMCS\Grpc\Gcp\Config; | |
| 35 | +use Dudlewebs\WPMCS\GCP\Google\ApiCore\Options\ClientOptions; | |
| 36 | +use Dudlewebs\WPMCS\GCP\Google\Auth\ApplicationDefaultCredentials; | |
| 37 | +use Dudlewebs\WPMCS\GCP\Google\Auth\CredentialsLoader; | |
| 38 | +use Dudlewebs\WPMCS\GCP\Google\Auth\FetchAuthTokenInterface; | |
| 39 | +use Dudlewebs\WPMCS\GCP\Google\Auth\GetUniverseDomainInterface; | |
| 40 | +use Dudlewebs\WPMCS\GCP\Google\Auth\HttpHandler\HttpHandlerFactory; | |
| 41 | +use Dudlewebs\WPMCS\GCP\Grpc\Gcp\ApiConfig; | |
| 42 | +use Dudlewebs\WPMCS\GCP\Grpc\Gcp\Config; | |
| 43 | +use Dudlewebs\WPMCS\GCP\Psr\Log\LoggerInterface; | |
| 44 | +use Dudlewebs\WPMCS\GCP\Psr\Log\LogLevel; | |
| 40 | 45 | /** |
| 41 | 46 | * Common functions used to work with various clients. |
| 42 | 47 | * |
| 43 | 48 | * @internal |
| @@ -58,9 +63,9 @@ | ||
| 58 | 63 | } |
| 59 | 64 | private static function initGrpcGcpConfig(string $hostName, string $confPath) |
| 60 | 65 | { |
| 61 | 66 | $apiConfig = new ApiConfig(); |
| 62 | - $apiConfig->mergeFromJsonString(file_get_contents($confPath)); | |
| 67 | + $apiConfig->mergeFromJsonString(\file_get_contents($confPath)); | |
| 63 | 68 | $config = new Config($hostName, $apiConfig); |
| 64 | 69 | return $config; |
| 65 | 70 | } |
| 66 | 71 | /** |
| @@ -73,30 +78,61 @@ | ||
| 73 | 78 | private static function getClientDefaults() |
| 74 | 79 | { |
| 75 | 80 | return []; |
| 76 | 81 | } |
| 77 | - private function buildClientOptions(array $options) | |
| 82 | + /** | |
| 83 | + * Resolve client options based on the client's default | |
| 84 | + * ({@see ClientOptionsTrait::getClientDefault}) and the default for all | |
| 85 | + * Google APIs. | |
| 86 | + * | |
| 87 | + * 1. Set default client option values | |
| 88 | + * 2. Set default logger (and log user-supplied configuration options) | |
| 89 | + * 3. Set default transport configuration | |
| 90 | + * 4. Call "modifyClientOptions" (for backwards compatibility) | |
| 91 | + * 5. Use "defaultScopes" when custom endpoint is supplied | |
| 92 | + * 6. Load mTLS from the environment if configured | |
| 93 | + * 7. Resolve endpoint based on universe domain template when possible | |
| 94 | + * 8. Load sysvshm grpc config when possible | |
| 95 | + */ | |
| 96 | + private function buildClientOptions(array|ClientOptions $options) | |
| 78 | 97 | { |
| 98 | + if ($options instanceof ClientOptions) { | |
| 99 | + $options = $options->toArray(); | |
| 100 | + } | |
| 79 | 101 | // Build $defaultOptions starting from top level |
| 80 | 102 | // variables, then going into deeper nesting, so that |
| 81 | 103 | // we will not encounter missing keys |
| 82 | 104 | $defaultOptions = self::getClientDefaults(); |
| 83 | - $defaultOptions += ['disableRetries' => \false, 'credentials' => null, 'credentialsConfig' => [], 'transport' => null, 'transportConfig' => [], 'gapicVersion' => self::getGapicVersion($options), 'libName' => null, 'libVersion' => null, 'apiEndpoint' => null, 'clientCertSource' => null, 'universeDomain' => null]; | |
| 105 | + $defaultOptions += ['disableRetries' => \false, 'credentials' => null, 'credentialsConfig' => [], 'transport' => null, 'transportConfig' => [], 'gapicVersion' => self::getGapicVersion($options), 'libName' => null, 'libVersion' => null, 'apiEndpoint' => null, 'clientCertSource' => null, 'universeDomain' => null, 'logger' => null]; | |
| 84 | 106 | $supportedTransports = $this->supportedTransports(); |
| 85 | 107 | foreach ($supportedTransports as $transportName) { |
| 86 | - if (!array_key_exists($transportName, $defaultOptions['transportConfig'])) { | |
| 108 | + if (!\array_key_exists($transportName, $defaultOptions['transportConfig'])) { | |
| 87 | 109 | $defaultOptions['transportConfig'][$transportName] = []; |
| 88 | 110 | } |
| 89 | 111 | } |
| 90 | - if (in_array('grpc', $supportedTransports)) { | |
| 112 | + if (\in_array('grpc', $supportedTransports)) { | |
| 91 | 113 | $defaultOptions['transportConfig']['grpc'] = ['stubOpts' => ['grpc.service_config_disable_resolution' => 1]]; |
| 92 | 114 | } |
| 93 | 115 | // Keep track of the API Endpoint |
| 94 | 116 | $apiEndpoint = $options['apiEndpoint'] ?? null; |
| 117 | + // Keep track of the original user supplied options for logging the configuration | |
| 118 | + $clientSuppliedOptions = $options; | |
| 95 | 119 | // Merge defaults into $options starting from top level |
| 96 | 120 | // variables, then going into deeper nesting, so that |
| 97 | 121 | // we will not encounter missing keys |
| 98 | 122 | $options += $defaultOptions; |
| 123 | + // If logger is explicitly set to false, logging is disabled | |
| 124 | + if (\is_null($options['logger'])) { | |
| 125 | + $options['logger'] = ApplicationDefaultCredentials::getDefaultLogger(); | |
| 126 | + } | |
| 127 | + if ($options['logger'] !== null && $options['logger'] !== \false && !$options['logger'] instanceof LoggerInterface) { | |
| 128 | + throw new ValidationException('The "logger" option in the options array should be PSR-3 LoggerInterface compatible'); | |
| 129 | + } | |
| 130 | + // Log the user supplied configuration. | |
| 131 | + $this->logConfiguration($options['logger'], $clientSuppliedOptions); | |
| 132 | + if (isset($options['logger'])) { | |
| 133 | + $options['credentialsConfig']['authHttpHandler'] = HttpHandlerFactory::build(logger: $options['logger']); | |
| 134 | + } | |
| 99 | 135 | $options['credentialsConfig'] += $defaultOptions['credentialsConfig']; |
| 100 | 136 | $options['transportConfig'] += $defaultOptions['transportConfig']; |
| 101 | 137 | // @phpstan-ignore-line |
| 102 | 138 | if (isset($options['transportConfig']['grpc'])) { |
| @@ -101,12 +137,17 @@ | ||
| 101 | 137 | // @phpstan-ignore-line |
| 102 | 138 | if (isset($options['transportConfig']['grpc'])) { |
| 103 | 139 | $options['transportConfig']['grpc'] += $defaultOptions['transportConfig']['grpc']; |
| 104 | 140 | $options['transportConfig']['grpc']['stubOpts'] += $defaultOptions['transportConfig']['grpc']['stubOpts']; |
| 141 | + $options['transportConfig']['grpc']['logger'] = $options['logger'] ?? null; | |
| 105 | 142 | } |
| 106 | 143 | if (isset($options['transportConfig']['rest'])) { |
| 107 | 144 | $options['transportConfig']['rest'] += $defaultOptions['transportConfig']['rest']; |
| 145 | + $options['transportConfig']['rest']['logger'] = $options['logger'] ?? null; | |
| 108 | 146 | } |
| 147 | + if (isset($options['transportConfig']['grpc-fallback'])) { | |
| 148 | + $options['transportConfig']['grpc-fallback']['logger'] = $options['logger'] ?? null; | |
| 149 | + } | |
| 109 | 150 | // These calls do not apply to "New Surface" clients. |
| 110 | 151 | if ($this->isBackwardsCompatibilityMode()) { |
| 111 | 152 | $preModifiedOptions = $options; |
| 112 | 153 | $this->modifyClientOptions($options); |
| @@ -132,10 +173,10 @@ | ||
| 132 | 173 | // mTLS: detect and load the default clientCertSource if the environment variable |
| 133 | 174 | // "GOOGLE_API_USE_CLIENT_CERTIFICATE" is true, and the cert source is available |
| 134 | 175 | if (empty($options['clientCertSource']) && CredentialsLoader::shouldLoadClientCertSource()) { |
| 135 | 176 | if ($defaultCertSource = CredentialsLoader::getDefaultClientCertSource()) { |
| 136 | - $options['clientCertSource'] = function () use ($defaultCertSource) { | |
| 137 | - $cert = call_user_func($defaultCertSource); | |
| 177 | + $options['clientCertSource'] = function () use($defaultCertSource) { | |
| 178 | + $cert = \call_user_func($defaultCertSource); | |
| 138 | 179 | // the key and the cert are returned in one string |
| 139 | 180 | return [$cert, $cert]; |
| 140 | 181 | }; |
| 141 | 182 | } |
| @@ -141,22 +182,22 @@ | ||
| 141 | 182 | } |
| 142 | 183 | } |
| 143 | 184 | // mTLS: If no apiEndpoint has been supplied by the user, and either |
| 144 | 185 | // GOOGLE_API_USE_MTLS_ENDPOINT tells us to, or mTLS is available, use the mTLS endpoint. |
| 145 | - if (is_null($apiEndpoint) && $this->shouldUseMtlsEndpoint($options)) { | |
| 186 | + if (\is_null($apiEndpoint) && $this->shouldUseMtlsEndpoint($options)) { | |
| 146 | 187 | $apiEndpoint = self::determineMtlsEndpoint($options['apiEndpoint']); |
| 147 | 188 | } |
| 148 | 189 | // If the user has not supplied a universe domain, use the environment variable if set. |
| 149 | 190 | // Otherwise, use the default ("googleapis.com"). |
| 150 | - $options['universeDomain'] ??= getenv('GOOGLE_CLOUD_UNIVERSE_DOMAIN') ?: GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN; | |
| 191 | + $options['universeDomain'] ??= \getenv('GOOGLE_CLOUD_UNIVERSE_DOMAIN') ?: GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN; | |
| 151 | 192 | // mTLS: It is not valid to configure mTLS outside of "googleapis.com" (yet) |
| 152 | 193 | if (isset($options['clientCertSource']) && $options['universeDomain'] !== GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN) { |
| 153 | 194 | throw new ValidationException('mTLS is not supported outside the "googleapis.com" universe'); |
| 154 | 195 | } |
| 155 | - if (is_null($apiEndpoint)) { | |
| 156 | - if (defined('self::SERVICE_ADDRESS_TEMPLATE')) { | |
| 196 | + if (\is_null($apiEndpoint)) { | |
| 197 | + if (\defined('self::SERVICE_ADDRESS_TEMPLATE')) { | |
| 157 | 198 | // Derive the endpoint from the service address template and the universe domain |
| 158 | - $apiEndpoint = str_replace('UNIVERSE_DOMAIN', $options['universeDomain'], self::SERVICE_ADDRESS_TEMPLATE); | |
| 199 | + $apiEndpoint = \str_replace('UNIVERSE_DOMAIN', $options['universeDomain'], self::SERVICE_ADDRESS_TEMPLATE); | |
| 159 | 200 | } else { |
| 160 | 201 | // For older clients, the service address template does not exist. Use the default |
| 161 | 202 | // endpoint instead. |
| 162 | 203 | $apiEndpoint = $defaultOptions['apiEndpoint']; |
| @@ -161,11 +202,11 @@ | ||
| 161 | 202 | // endpoint instead. |
| 162 | 203 | $apiEndpoint = $defaultOptions['apiEndpoint']; |
| 163 | 204 | } |
| 164 | 205 | } |
| 165 | - if (extension_loaded('sysvshm') && isset($options['gcpApiConfigPath']) && file_exists($options['gcpApiConfigPath']) && !empty($apiEndpoint)) { | |
| 206 | + if (\extension_loaded('sysvshm') && isset($options['gcpApiConfigPath']) && \file_exists($options['gcpApiConfigPath']) && !empty($apiEndpoint)) { | |
| 166 | 207 | $grpcGcpConfig = self::initGrpcGcpConfig($apiEndpoint, $options['gcpApiConfigPath']); |
| 167 | - if (!array_key_exists('stubOpts', $options['transportConfig']['grpc'])) { | |
| 208 | + if (!\array_key_exists('stubOpts', $options['transportConfig']['grpc'])) { | |
| 168 | 209 | $options['transportConfig']['grpc']['stubOpts'] = []; |
| 169 | 210 | } |
| 170 | 211 | $options['transportConfig']['grpc']['stubOpts'] += ['grpc_call_invoker' => $grpcGcpConfig->callInvoker()]; |
| 171 | 212 | } |
| @@ -173,9 +214,9 @@ | ||
| 173 | 214 | return $options; |
| 174 | 215 | } |
| 175 | 216 | private function shouldUseMtlsEndpoint(array $options) |
| 176 | 217 | { |
| 177 | - $mtlsEndpointEnvVar = getenv('GOOGLE_API_USE_MTLS_ENDPOINT'); | |
| 218 | + $mtlsEndpointEnvVar = \getenv('GOOGLE_API_USE_MTLS_ENDPOINT'); | |
| 178 | 219 | if ('always' === $mtlsEndpointEnvVar) { |
| 179 | 220 | return \true; |
| 180 | 221 | } |
| 181 | 222 | if ('never' === $mtlsEndpointEnvVar) { |
| @@ -185,14 +226,14 @@ | ||
| 185 | 226 | return !empty($options['clientCertSource']); |
| 186 | 227 | } |
| 187 | 228 | private static function determineMtlsEndpoint(string $apiEndpoint) |
| 188 | 229 | { |
| 189 | - $parts = explode('.', $apiEndpoint); | |
| 190 | - if (count($parts) < 3) { | |
| 230 | + $parts = \explode('.', $apiEndpoint); | |
| 231 | + if (\count($parts) < 3) { | |
| 191 | 232 | return $apiEndpoint; |
| 192 | 233 | // invalid endpoint! |
| 193 | 234 | } |
| 194 | - return sprintf('%s.mtls.%s', array_shift($parts), implode('.', $parts)); | |
| 235 | + return \sprintf('%s.mtls.%s', \array_shift($parts), \implode('.', $parts)); | |
| 195 | 236 | } |
| 196 | 237 | /** |
| 197 | 238 | * @param mixed $credentials |
| 198 | 239 | * @param array $credentialsConfig |
| @@ -200,20 +241,23 @@ | ||
| 200 | 241 | * @throws ValidationException |
| 201 | 242 | */ |
| 202 | 243 | private function createCredentialsWrapper($credentials, array $credentialsConfig, string $universeDomain) |
| 203 | 244 | { |
| 204 | - if (is_null($credentials)) { | |
| 245 | + if (\is_null($credentials)) { | |
| 246 | + // If the user has explicitly set the apiKey option, use Api Key credentials | |
| 205 | 247 | return CredentialsWrapper::build($credentialsConfig, $universeDomain); |
| 206 | - } elseif (is_string($credentials) || is_array($credentials)) { | |
| 248 | + } | |
| 249 | + if (\is_string($credentials) || \is_array($credentials)) { | |
| 207 | 250 | return CredentialsWrapper::build(['keyFile' => $credentials] + $credentialsConfig, $universeDomain); |
| 208 | - } elseif ($credentials instanceof FetchAuthTokenInterface) { | |
| 251 | + } | |
| 252 | + if ($credentials instanceof FetchAuthTokenInterface) { | |
| 209 | 253 | $authHttpHandler = $credentialsConfig['authHttpHandler'] ?? null; |
| 210 | 254 | return new CredentialsWrapper($credentials, $authHttpHandler, $universeDomain); |
| 211 | - } elseif ($credentials instanceof CredentialsWrapper) { | |
| 255 | + } | |
| 256 | + if ($credentials instanceof CredentialsWrapper) { | |
| 212 | 257 | return $credentials; |
| 213 | - } else { | |
| 214 | - throw new ValidationException('Unexpected value in $auth option, got: ' . print_r($credentials, \true)); | |
| 215 | 258 | } |
| 259 | + throw new ValidationException(\sprintf('Unexpected value in $auth option, got: %s', \print_r($credentials, \true))); | |
| 216 | 260 | } |
| 217 | 261 | /** |
| 218 | 262 | * This defaults to all three transports, which One-Platform supports. |
| 219 | 263 | * Discovery clients should define this function and only return ['rest']. |
| @@ -239,9 +283,25 @@ | ||
| 239 | 283 | } |
| 240 | 284 | /** |
| 241 | 285 | * @internal |
| 242 | 286 | */ |
| 243 | - private function isBackwardsCompatibilityMode(): bool | |
| 287 | + private function isBackwardsCompatibilityMode() : bool | |
| 244 | 288 | { |
| 245 | 289 | return \false; |
| 290 | + } | |
| 291 | + /** | |
| 292 | + * @param null|false|LoggerInterface $logger | |
| 293 | + * @param string $options | |
| 294 | + */ | |
| 295 | + private function logConfiguration(null|false|LoggerInterface $logger, array $options) : void | |
| 296 | + { | |
| 297 | + if (!$logger) { | |
| 298 | + return; | |
| 299 | + } | |
| 300 | + $configurationLog = ['timestamp' => \date(\DATE_RFC3339), 'severity' => \strtoupper(LogLevel::DEBUG), 'processId' => \getmypid(), 'jsonPayload' => [ | |
| 301 | + 'serviceName' => self::SERVICE_NAME, | |
| 302 | + // @phpstan-ignore-line | |
| 303 | + 'clientConfiguration' => $options, | |
| 304 | + ]]; | |
| 305 | + $logger->debug(\json_encode($configurationLog)); | |
| 246 | 306 | } |
| 247 | 307 | } |