Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gax/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"license": "BSD-3-Clause",
"require": {
"php": "^8.1",
"google/auth": "^1.49",
"google/auth": "^1.52",
"google/grpc-gcp": "^0.4",
"grpc/grpc": "^1.13",
"google/protobuf": "^4.31||^5.34",
Expand Down
22 changes: 15 additions & 7 deletions Gax/src/CredentialsWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class CredentialsWrapper implements HeaderCredentialsInterface, ProjectIdProvide
/** @var callable $authHttpHandle */
private $authHttpHandler;

private string $universeDomain;
private bool $hasCheckedUniverse = false;

/** @var int */
Expand All @@ -76,14 +75,13 @@ class CredentialsWrapper implements HeaderCredentialsInterface, ProjectIdProvide
public function __construct(
FetchAuthTokenInterface $credentialsFetcher,
?callable $authHttpHandler = null,
string $universeDomain = GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN
private string $universeDomain = GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN,
) {
$this->credentialsFetcher = $credentialsFetcher;
$this->authHttpHandler = $authHttpHandler;
if (empty($universeDomain)) {
throw new ValidationException('The universe domain cannot be empty');
}
$this->universeDomain = $universeDomain;
}

/**
Expand Down Expand Up @@ -116,6 +114,9 @@ public function __construct(
* @type bool $useJwtAccessWithScope
* Ensures service account credentials use JWT Access (also known as self-signed
* JWTs), even when user-defined scopes are supplied.
* @type bool $enableRegionalAccessBoundary
* Enable the Regional Access Boundary lookup in the credentials which sets the
* `x-allowed-locations` header in the request.
* }
* @param string $universeDomain The expected universe of the credentials. Defaults to
* "googleapis.com"
Expand All @@ -136,6 +137,7 @@ public static function build(
'quotaProject' => null,
'defaultScopes' => null,
'useJwtAccessWithScope' => true,
'enableRegionalAccessBoundary' => false,
];

$keyFile = $args['keyFile'];
Expand All @@ -147,7 +149,8 @@ public static function build(
$args['authCacheOptions'],
$args['authCache'],
$args['quotaProject'],
$args['defaultScopes']
$args['defaultScopes'],
$args['enableRegionalAccessBoundary'],
);
if ($loader instanceof FetchAuthTokenCache) {
$loader = $loader->getFetcher();
Expand All @@ -167,7 +170,8 @@ public static function build(
$loader = CredentialsLoader::makeCredentials(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PHPSTAN analysis is failing, but I believe this is due the other PR not being merged yet.

$args['scopes'],
$keyFile,
$args['defaultScopes']
$args['defaultScopes'],
$args['enableRegionalAccessBoundary'],
);
}

Expand Down Expand Up @@ -322,7 +326,8 @@ private static function buildApplicationDefaultCredentials(
?array $authCacheOptions = null,
?CacheItemPoolInterface $authCache = null,
$quotaProject = null,
?array $defaultScopes = null
?array $defaultScopes = null,
bool $enableRegionalAccessBoundary = true,
) {
try {
return ApplicationDefaultCredentials::getCredentials(
Expand All @@ -331,7 +336,10 @@ private static function buildApplicationDefaultCredentials(
$authCacheOptions,
$authCache,
$quotaProject,
$defaultScopes
$defaultScopes,
null, // $universeDomain
null, // $logger
$enableRegionalAccessBoundary,
);
} catch (DomainException $ex) {
throw new ValidationException('Could not construct ApplicationDefaultCredentials', $ex->getCode(), $ex);
Expand Down
12 changes: 10 additions & 2 deletions Gax/src/GapicClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,18 @@ private function setClientOptions(array $options)
$options['credentialsConfig']['quotaProject'] ?? null
);
} else {
$enableRegionalAccessBoundary = filter_var(
getenv('GOOGLE_AUTH_TRUST_BOUNDARY_ENABLE_EXPERIMENT'),
FILTER_VALIDATE_BOOLEAN
);
$isRegional = str_ends_with($options['apiEndpoint'], '.rep.googleapis.com')
|| str_ends_with($options['apiEndpoint'], '.rep.sandbox.googleapis.com');
$this->credentialsWrapper = $this->createCredentialsWrapper(
$options['credentials'],
$options['credentialsConfig'],
$options['universeDomain']
$options['credentialsConfig'] + [
'enableRegionalAccessBoundary' => $enableRegionalAccessBoundary && !$isRegional
],
$options['universeDomain'],
);
}

Expand Down
28 changes: 28 additions & 0 deletions Gax/tests/Unit/CredentialsWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,34 @@ public function buildDataWithoutExplicitKeyFile()
quotaProject: $quotaProject
)),
],
[
['enableRegionalAccessBoundary' => true],
new CredentialsWrapper(ApplicationDefaultCredentials::getCredentials(
null,
$authHttpHandler,
null,
$defaultAuthCache,
null,
null,
null,
null,
true, // $enableRegionalAccessBoundary
)),
],
[
['enableRegionalAccessBoundary' => false],
new CredentialsWrapper(ApplicationDefaultCredentials::getCredentials(
null,
$authHttpHandler,
null,
$defaultAuthCache,
null,
null,
null,
null,
false, // $enableRegionalAccessBoundary
)),
],
];

$this->setEnv('GOOGLE_APPLICATION_CREDENTIALS', $appDefaultCreds);
Expand Down
45 changes: 45 additions & 0 deletions Gax/tests/Unit/GapicClientTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,51 @@ public function setClientOptionsData()
];
}

/**
* @dataProvider buildClientOptionsRegionalEndpointData
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testBuildClientOptionsRegionalEndpoint(?string $apiEndpoint, ?string $envVarValue, bool $rabEnabled)
{
if ($envVarValue !== null) {
putenv('GOOGLE_AUTH_TRUST_BOUNDARY_ENABLE_EXPERIMENT=' . $envVarValue);
}

$client = new class() extends StubGapicClient {
public array $capturedCredentialsConfig = [];

public function createCredentialsWrapper($credentials, array $credentialsConfig, string $universeDomain)
{
$this->capturedCredentialsConfig = $credentialsConfig;
return null;
}
};

$clientOptions = $client->buildClientOptions(
$apiEndpoint !== null ? ['apiEndpoint' => $apiEndpoint] : []
);
$client->setClientOptions($clientOptions);

$this->assertEquals(
$rabEnabled,
$client->capturedCredentialsConfig['enableRegionalAccessBoundary'] ?? null
);
}

public function buildClientOptionsRegionalEndpointData()
{
return [
['test.googleapis.com', 'true', true],
['test.rep.googleapis.com', 'true', false],
['test.rep.sandbox.googleapis.com', 'true', false],
['test.googleapis.com', 'false', false],
['test.googleapis.com', null, false],
['', 'true', true], // Empty endpoint case
[null, 'true', true], // Unset endpoint case
];
}

/**
* @dataProvider buildRequestHeaderParams
*/
Expand Down
Loading