Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for universe domain (phase 1) #477

Merged
merged 18 commits into from
Nov 28, 2023
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
19 changes: 19 additions & 0 deletions src/Credentials/ServiceAccountCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ class ServiceAccountCredentials extends CredentialsLoader implements
*/
private $jwtAccessCredentials;

/**
* @var string|null
vishwarajanand marked this conversation as resolved.
Show resolved Hide resolved
*/
private ?string $universeDomain;

/**
* Create a new ServiceAccountCredentials.
*
Expand Down Expand Up @@ -159,6 +164,7 @@ public function __construct(
]);

$this->projectId = $jsonKey['project_id'] ?? null;
$this->universeDomain = $jsonKey['universe_domain'] ?? null;
}

/**
Expand Down Expand Up @@ -328,6 +334,19 @@ public function getQuotaProject()
return $this->quotaProject;
}

/**
* Get the universe domain configured in the JSON credential.
*
* @return string
*/
public function getUniverseDomain(): string
{
if (null === $this->universeDomain) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you want to handle the case when universeDomain is an empty string? Or is it guaranteed to be having a valid value (eg. when always downloaded from gcp console)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question. The spec does not provide any guidance around empty strings for universe domain (go/cloudsdk-tpc-authlibs), except when it is a 404 error from the MDS (which is handled in phase 2).

If we added special handling for empty strings, it would be to throw an exception. But as an empty string for a universe domain will throw an error in the client libraries anyway (because we throw an error for a user-supplied empty string, and also for a mismatch), I can't think of a real important reason why we need to throw an exception now, and would prefer to leave it out (unless we can think of a specific case where it would be important to do so).

return self::DEFAULT_UNIVERSE_DOMAIN;
}
return $this->universeDomain;
}
bshaffer marked this conversation as resolved.
Show resolved Hide resolved

/**
* @return bool
*/
Expand Down
12 changes: 12 additions & 0 deletions src/CredentialsLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* credentials files on the file system.
*/
abstract class CredentialsLoader implements
GetUniverseDomainInterface,
FetchAuthTokenInterface,
UpdateMetadataInterface
{
Expand Down Expand Up @@ -273,4 +274,15 @@ private static function loadDefaultClientCertSourceFile()
}
return $clientCertSourceJson;
}

/**
* Get the universe domain from the credential. Defaults to "googleapis.com"
* for all credential types which do not support universe domain.
*
* @return string
*/
public function getUniverseDomain(): string
{
return self::DEFAULT_UNIVERSE_DOMAIN;
}
}
15 changes: 15 additions & 0 deletions src/FetchAuthTokenCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
class FetchAuthTokenCache implements
FetchAuthTokenInterface,
GetQuotaProjectInterface,
GetUniverseDomainInterface,
SignBlobInterface,
ProjectIdProviderInterface,
UpdateMetadataInterface
Expand Down Expand Up @@ -191,6 +192,20 @@ public function getProjectId(callable $httpHandler = null)
return $this->fetcher->getProjectId($httpHandler);
}

/*
* Get the Universe Domain from the fetcher.
*
* @return string
*/
public function getUniverseDomain(): string
{
if ($this->fetcher instanceof GetUniverseDomainInterface) {
return $this->fetcher->getUniverseDomain();
}

return GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN;
}

/**
* Updates metadata with the authorization token.
*
Expand Down
35 changes: 35 additions & 0 deletions src/GetUniverseDomainInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/*
* Copyright 2023 Google LLC
*
* 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.
*/

namespace Google\Auth;

/**
* An interface implemented by objects that can get universe domain for Google Cloud APIs.
*/
interface GetUniverseDomainInterface
{
const DEFAULT_UNIVERSE_DOMAIN = 'googleapis.com';

/**
* Get the universe domain from the credential. This should always return
* a string, and default to "googleapis.com" if no universe domain is
* configured.
*
* @return string
*/
public function getUniverseDomain(): string;
}
22 changes: 22 additions & 0 deletions tests/ApplicationDefaultCredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -783,4 +783,26 @@ public function provideExternalAccountCredentials()
['aws_credentials.json', CredentialSource\AwsNativeSource::class],
];
}

/** @runInSeparateProcess */
public function testUniverseDomainInKeyFile()
{
// Test no universe domain in keyfile defaults to "googleapis.com"
$keyFile = __DIR__ . '/fixtures3/service_account_credentials.json';
putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
$creds = ApplicationDefaultCredentials::getCredentials();
$this->assertEquals(CredentialsLoader::DEFAULT_UNIVERSE_DOMAIN, $creds->getUniverseDomain());

// Test universe domain in "service_account" keyfile
$keyFile = __DIR__ . '/fixtures/private.json';
putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
$creds = ApplicationDefaultCredentials::getCredentials();
$this->assertEquals('example-universe.com', $creds->getUniverseDomain());

// Test universe domain in "authenticated_user" keyfile is not read.
$keyFile = __DIR__ . '/fixtures2/private.json';
putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
$creds2 = ApplicationDefaultCredentials::getCredentials();
$this->assertEquals(CredentialsLoader::DEFAULT_UNIVERSE_DOMAIN, $creds2->getUniverseDomain());
}
}
8 changes: 8 additions & 0 deletions tests/Credentials/GCECredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,12 @@ public function testGetClientNameWithServiceAccountIdentity()
$creds = new GCECredentials(null, null, null, null, 'foo');
$this->assertEquals($expected, $creds->getClientName($httpHandler));
}

public function testGetUniverseDomain()
{
$creds = new GCECredentials();

// Universe domain should always be the default
$this->assertEquals(GCECredentials::DEFAULT_UNIVERSE_DOMAIN, $creds->getUniverseDomain());
}
}
36 changes: 36 additions & 0 deletions tests/FetchAuthTokenCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Google\Auth\Credentials\ServiceAccountCredentials;
use Google\Auth\CredentialsLoader;
use Google\Auth\FetchAuthTokenCache;
use Google\Auth\GetUniverseDomainInterface;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use RuntimeException;
Expand Down Expand Up @@ -603,6 +604,41 @@ public function testGetProjectIdInvalidFetcher()
$fetcher->getProjectId();
}

public function testGetUniverseDomain()
{
$universeDomain = 'foobar';

$mockFetcher = $this->prophesize('Google\Auth\GetUniverseDomainInterface');
$mockFetcher->willImplement('Google\Auth\FetchAuthTokenInterface');
$mockFetcher->getUniverseDomain()
->shouldBeCalled()
->willReturn($universeDomain);

$fetcher = new FetchAuthTokenCache(
$mockFetcher->reveal(),
[],
$this->mockCache->reveal()
);

$this->assertEquals($universeDomain, $fetcher->getUniverseDomain());
}

public function testGetUniverseDomainInvalidFetcher()
{
$mockFetcher = $this->prophesize('Google\Auth\FetchAuthTokenInterface');

$fetcher = new FetchAuthTokenCache(
$mockFetcher->reveal(),
[],
$this->mockCache->reveal()
);

$this->assertEquals(
GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN,
$fetcher->getUniverseDomain()
);
}

public function testGetFetcher()
{
$mockFetcher = $this->prophesize('Google\Auth\FetchAuthTokenInterface')
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/private.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"client_email": "[email protected]",
"client_id": "client123",
"type": "service_account",
"quota_project_id": "test_quota_project"
"quota_project_id": "test_quota_project",
"universe_domain": "example-universe.com"
}
3 changes: 2 additions & 1 deletion tests/fixtures2/private.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"client_secret": "clientSecret123",
"refresh_token": "refreshToken123",
"type": "authorized_user",
"quota_project_id": "test_quota_project"
"quota_project_id": "test_quota_project",
"universe_domain": "example-universe.com"
}