Skip to content

Commit 85700db

Browse files
authored
Merge branch 'main' into renovate/kelvinmo-simplejwt-0.x
2 parents 29f84e3 + be5b162 commit 85700db

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
* [feat]: add support for Firebase v6.0 (#391)
44

5+
## [1.29.0](https://github.com/googleapis/google-auth-library-php/compare/v1.28.0...v1.29.0) (2023-08-22)
6+
7+
8+
### Features
9+
10+
* Check unix residency for gce when ping fails ([#469](https://github.com/googleapis/google-auth-library-php/issues/469)) ([3c672f9](https://github.com/googleapis/google-auth-library-php/commit/3c672f9aff61529f4af836558caa50fa29fb9447))
11+
512
## [1.28.0](https://github.com/googleapis/google-auth-library-php/compare/v1.27.0...v1.28.0) (2023-05-11)
613

714

src/Credentials/GCECredentials.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ class GCECredentials extends CredentialsLoader implements
100100
*/
101101
const FLAVOR_HEADER = 'Metadata-Flavor';
102102

103+
/**
104+
* The Linux file which contains the product name.
105+
*/
106+
private const GKE_PRODUCT_NAME_FILE = '/sys/class/dmi/id/product_name';
107+
103108
/**
104109
* Note: the explicit `timeout` and `tries` below is a workaround. The underlying
105110
* issue is that resolving an unknown host on some networks will take
@@ -340,6 +345,22 @@ public static function onGce(callable $httpHandler = null)
340345
} catch (ConnectException $e) {
341346
}
342347
}
348+
349+
if (PHP_OS === 'Windows') {
350+
// @TODO: implement GCE residency detection on Windows
351+
return false;
352+
}
353+
354+
// Detect GCE residency on Linux
355+
return self::detectResidencyLinux(self::GKE_PRODUCT_NAME_FILE);
356+
}
357+
358+
private static function detectResidencyLinux(string $productNameFile): bool
359+
{
360+
if (file_exists($productNameFile)) {
361+
$productName = trim((string) file_get_contents($productNameFile));
362+
return 0 === strpos($productName, 'Google');
363+
}
343364
return false;
344365
}
345366

tests/Credentials/GCECredentialsTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,38 @@ public function testOnGCEIsFalseOnServerErrorStatus()
7373
$this->assertFalse(GCECredentials::onGCE($httpHandler));
7474
}
7575

76+
public function testCheckProductNameFile()
77+
{
78+
$tmpFile = tempnam(sys_get_temp_dir(), 'gce-test-product-name');
79+
80+
$method = (new \ReflectionClass(GCECredentials::class))
81+
->getMethod('detectResidencyLinux');
82+
$method->setAccessible(true);
83+
84+
$this->assertFalse($method->invoke(null, '/nonexistant/file'));
85+
86+
file_put_contents($tmpFile, 'Google');
87+
$this->assertTrue($method->invoke(null, $tmpFile));
88+
89+
file_put_contents($tmpFile, 'Not Google');
90+
$this->assertFalse($method->invoke(null, $tmpFile));
91+
}
92+
93+
public function testOnGceWithResidency()
94+
{
95+
if (!GCECredentials::onGCE()) {
96+
$this->markTestSkipped('This test only works while running on GCE');
97+
}
98+
99+
// If calling metadata server fails, this will check the residency file.
100+
$httpHandler = function () {
101+
// Mock an exception, such as a ping timeout
102+
throw $this->prophesize(ClientException::class)->reveal();
103+
};
104+
105+
$this->assertTrue(GCECredentials::onGCE($httpHandler));
106+
}
107+
76108
public function testOnGCEIsFalseOnOkStatusWithoutExpectedHeader()
77109
{
78110
$httpHandler = getHandler([

0 commit comments

Comments
 (0)