Skip to content

Commit 4675170

Browse files
authored
fix: don't set keyFile when credentialsFetcher is given (#8657)
1 parent 3ad3766 commit 4675170

4 files changed

Lines changed: 53 additions & 1 deletion

File tree

Core/src/ClientTrait.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
use Google\Auth\Credentials\GCECredentials;
2121
use Google\Auth\CredentialsLoader;
22+
use Google\Auth\FetchAuthTokenInterface;
23+
use Google\Auth\ProjectIdProviderInterface;
2224
use Google\Cloud\Core\Compute\Metadata;
2325
use Google\Cloud\Core\Exception\GoogleException;
2426

@@ -93,7 +95,12 @@ private function getGrpcInstallationMessage()
9395
*/
9496
private function configureAuthentication(array $config)
9597
{
96-
$config['keyFile'] = $this->getKeyFile($config);
98+
$credentialsFetcher = $config['credentialsFetcher'] ?? null;
99+
100+
if (!($credentialsFetcher instanceof FetchAuthTokenInterface)) {
101+
$config['keyFile'] = $this->getKeyFile($config);
102+
}
103+
97104
$this->projectId = $this->detectProjectId($config);
98105

99106
return $config;
@@ -170,6 +177,7 @@ private function getKeyFile(array $config = [])
170177
private function detectProjectId(array $config)
171178
{
172179
$config += [
180+
'credentialsFetcher' => null,
173181
'httpHandler' => null,
174182
'projectId' => null,
175183
'projectIdRequired' => false,
@@ -186,6 +194,10 @@ private function detectProjectId(array $config)
186194
return 'emulator-project';
187195
}
188196

197+
if ($config['credentialsFetcher'] instanceof ProjectIdProviderInterface) {
198+
return $config['credentialsFetcher']->getProjectId();
199+
}
200+
189201
if (isset($config['keyFile'])) {
190202
if (isset($config['keyFile']['project_id'])) {
191203
return $config['keyFile']['project_id'];

Core/tests/Unit/ClientTraitTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace Google\Cloud\Core\Tests\Unit;
1919

2020
use Exception;
21+
use Google\Auth\Credentials\ServiceAccountCredentials;
2122
use Google\Cloud\Core\ClientTrait;
2223
use Google\Cloud\Core\Compute\Metadata;
2324
use Google\Cloud\Core\Exception\GoogleException;
@@ -165,6 +166,28 @@ public function testConfigureAuthenticationWithKeyFilePath()
165166
$this->assertEquals('example_project', $this->impl->___getProperty('projectId'));
166167
}
167168

169+
public function testIgnoreKeyFileWhenUsingCredentialsFetcher()
170+
{
171+
$credentials = new ServiceAccountCredentials([], Fixtures::SERVICE_ACCOUNT_FIXTURE());
172+
173+
$conf = $this->impl->call('configureAuthentication', [[
174+
'credentialsFetcher' => $credentials,
175+
]]);
176+
177+
$this->assertArrayNotHasKey('keyFile', $conf);
178+
}
179+
180+
public function testGetProjectIdFromCredentialsFetcher()
181+
{
182+
$credentials = new ServiceAccountCredentials([], Fixtures::SERVICE_ACCOUNT_FIXTURE());
183+
184+
$this->impl->call('configureAuthentication', [[
185+
'credentialsFetcher' => $credentials,
186+
]]);
187+
188+
$this->assertEquals('example-project-12345', $this->impl->___getProperty('projectId'));
189+
}
190+
168191
public function testConfigureAuthenticationWithInvalidKeyFilePath()
169192
{
170193
$this->expectException(GoogleException::class);

Core/tests/Unit/Fixtures.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public static function JSON_KEY_FIXTURE()
2525
return __DIR__ . '/fixtures/json-key-fixture.json';
2626
}
2727

28+
public static function SERVICE_ACCOUNT_FIXTURE()
29+
{
30+
return __DIR__ . '/fixtures/service-account-fixture.json';
31+
}
32+
2833
public static function SERVICE_FIXTURE()
2934
{
3035
return __DIR__ . '/fixtures/service-fixture.json';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"type": "service_account",
3+
"project_id": "example-project-12345",
4+
"private_key_id": "1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b",
5+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC4ziPSL1C/RTMN\nEUen4AyKlQ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8yFTQDLHjeJ9uF6lwjM3KUqm/vZ0N9t+LcXQqD5krj\njrny0zB/hsU+SSWI5AJrrKrwKV1dM+mHeVkQOkHssbrbtXXbTRH/mssOTGsjNKmT\nTGwNQHcW2NyN503Y3vPwT25QK9q9QIXqe1b2G774/bnrbHZAh/F63JerPwKBgD59\n58gRoFRa+5CaQWTZfVRq4+YPA/l7Qkqm/eljS/QfQJkhZ8PiqA9i6jD9l1wVnCxh\nD3vtMOZWexsx7Brdr+KHG+JobmAWQkgUJs2a33WfVJa78NV0rre9rVlUinMKaruy\nnAHu6T8rBX+AZ09/IQu/CkfMsxF1CahyO5DYUXeXAoGASocxulMRKaa3NbcsDl0t\nRK89ZgBxii6SgwvARpODTmsnGex5pNI6XVNCPcevyAhuWpoXPeEeg9Qj3FpyGTtz\nZUfKfFtFAAb3TU2C5PF0mTn+4vEL55YBXD4tfeimXzMhg/t2d6FP38g7wN2ejJPr\nBbFaAE9VZFi2OcxtIysoxek=\n-----END PRIVATE KEY-----\n",
6+
"client_email": "test-service-account@example-project-12345.iam.gserviceaccount.com",
7+
"client_id": "123456789012345678901",
8+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
9+
"token_uri": "https://oauth2.googleapis.com/token",
10+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
11+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/test-service-account%40example-project-12345.iam.gserviceaccount.com"
12+
}

0 commit comments

Comments
 (0)