Skip to content
Open
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
7 changes: 2 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ jobs:
test:
strategy:
matrix:
os: [ "ubuntu-latest", "windows-latest" ]
php: [ "8.1", "8.2", "8.3", "8.4" ]
os: [ ubuntu-latest ]
include:
- os: windows-latest
php: "8.1"
runs-on: ${{ matrix.os }}
name: PHP ${{ matrix.php }} Unit Test${{ matrix.os == 'windows-latest' && ' on Windows' || '' }}
steps:
Expand All @@ -31,7 +28,7 @@ jobs:
max_attempts: 3
command: composer install
- name: Run Script
run: vendor/bin/phpunit ${{ matrix.os == 'windows-latest' && '--filter GCECredentialsTest' || '' }}
run: vendor/bin/phpunit
test_lowest:
runs-on: ubuntu-latest
name: Test Prefer Lowest
Expand Down
50 changes: 25 additions & 25 deletions tests/ApplicationDefaultCredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testLoadsOKIfEnvSpecifiedIsValid()

public function testLoadsDefaultFileIfPresentAndEnvVarIsNotSet()
{
putenv('HOME=' . __DIR__ . '/fixtures');
setHomeEnv(__DIR__ . '/fixtures');
$this->assertNotNull(
ApplicationDefaultCredentials::getCredentials('a scope')
);
Expand All @@ -80,7 +80,7 @@ public function testFailsIfNotOnGceAndNoDefaultFileFound()
{
$this->expectException(DomainException::class);

putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
setHomeEnv(__DIR__ . '/not_exist_fixtures');
// simulate not being GCE and retry attempts by returning multiple 500s
$httpHandler = getHandler([
new Response(500),
Expand All @@ -93,7 +93,7 @@ public function testFailsIfNotOnGceAndNoDefaultFileFound()

public function testSuccedsIfNoDefaultFilesButIsOnGCE()
{
putenv('HOME');
setHomeEnv(null);

$wantedTokens = [
'access_token' => '1/abdef1234567890',
Expand All @@ -116,7 +116,7 @@ public function testSuccedsIfNoDefaultFilesButIsOnGCE()

public function testGceCredentials()
{
putenv('HOME');
setHomeEnv(null);

$jsonTokens = json_encode(['access_token' => 'abc']);

Expand Down Expand Up @@ -160,7 +160,7 @@ public function testGceCredentials()

public function testImpersonatedServiceAccountCredentials()
{
putenv('HOME=' . __DIR__ . '/fixtures5');
setHomeEnv(__DIR__ . '/fixtures5');
$creds = ApplicationDefaultCredentials::getCredentials(
null,
null,
Expand All @@ -183,7 +183,7 @@ public function testImpersonatedServiceAccountCredentials()

public function testUserRefreshCredentials()
{
putenv('HOME=' . __DIR__ . '/fixtures2');
setHomeEnv(__DIR__ . '/fixtures2');

$creds = ApplicationDefaultCredentials::getCredentials(
null, // $scope
Expand Down Expand Up @@ -219,7 +219,7 @@ public function testUserRefreshCredentials()

public function testServiceAccountCredentials()
{
putenv('HOME=' . __DIR__ . '/fixtures');
setHomeEnv(__DIR__ . '/fixtures');

$creds = ApplicationDefaultCredentials::getCredentials(
null, // $scope
Expand Down Expand Up @@ -255,7 +255,7 @@ public function testServiceAccountCredentials()

public function testDefaultScopeArray()
{
putenv('HOME=' . __DIR__ . '/fixtures2');
setHomeEnv(__DIR__ . '/fixtures2');

$creds = ApplicationDefaultCredentials::getCredentials(
null, // $scope
Expand Down Expand Up @@ -292,15 +292,15 @@ public function testGetMiddlewareLoadsOKIfEnvSpecifiedIsValid()

public function testLGetMiddlewareoadsDefaultFileIfPresentAndEnvVarIsNotSet()
{
putenv('HOME=' . __DIR__ . '/fixtures');
setHomeEnv(__DIR__ . '/fixtures');
$this->assertNotNull(ApplicationDefaultCredentials::getMiddleware('a scope'));
}

public function testGetMiddlewareFailsIfNotOnGceAndNoDefaultFileFound()
{
$this->expectException(DomainException::class);

putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
setHomeEnv(__DIR__ . '/not_exist_fixtures');

// simulate not being GCE and retry attempts by returning multiple 500s
$httpHandler = getHandler([
Expand Down Expand Up @@ -356,7 +356,7 @@ public function testOnGceCacheWithHit()
{
$this->expectException(DomainException::class);

putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
setHomeEnv(__DIR__ . '/not_exist_fixtures');

$mockCacheItem = $this->prophesize('Psr\Cache\CacheItemInterface');
$mockCacheItem->isHit()
Expand All @@ -380,7 +380,7 @@ public function testOnGceCacheWithHit()

public function testOnGceCacheWithoutHit()
{
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
setHomeEnv(__DIR__ . '/not_exist_fixtures');

$gceIsCalled = false;
$dummyHandler = function ($request) use (&$gceIsCalled) {
Expand Down Expand Up @@ -416,7 +416,7 @@ public function testOnGceCacheWithoutHit()

public function testOnGceCacheWithOptions()
{
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
setHomeEnv(__DIR__ . '/not_exist_fixtures');

$prefix = 'test_prefix_';
$lifetime = '70707';
Expand Down Expand Up @@ -473,7 +473,7 @@ public function testGetIdTokenCredentialsLoadsOKIfEnvSpecifiedIsValid()

public function testGetIdTokenCredentialsLoadsDefaultFileIfPresentAndEnvVarIsNotSet()
{
putenv('HOME=' . __DIR__ . '/fixtures');
setHomeEnv(__DIR__ . '/fixtures');
$creds = ApplicationDefaultCredentials::getIdTokenCredentials($this->targetAudience);
$this->assertInstanceOf(ServiceAccountCredentials::class, $creds);
}
Expand All @@ -483,7 +483,7 @@ public function testGetIdTokenCredentialsFailsIfNotOnGceAndNoDefaultFileFound()
$this->expectException(DomainException::class);
$this->expectExceptionMessage('Your default credentials were not found');

putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
setHomeEnv(__DIR__ . '/not_exist_fixtures');

// simulate not being GCE and retry attempts by returning multiple 500s
$httpHandler = getHandler([
Expand All @@ -500,7 +500,7 @@ public function testGetIdTokenCredentialsFailsIfNotOnGceAndNoDefaultFileFound()

public function testGetIdTokenCredentialsWithImpersonatedServiceAccountCredentials()
{
putenv('HOME=' . __DIR__ . '/fixtures5');
setHomeEnv(__DIR__ . '/fixtures5');
$creds = ApplicationDefaultCredentials::getIdTokenCredentials('[email protected]');
$this->assertInstanceOf(ImpersonatedServiceAccountCredentials::class, $creds);
}
Expand Down Expand Up @@ -529,7 +529,7 @@ public function testGetIdTokenCredentialsWithCacheOptions()

public function testGetIdTokenCredentialsSuccedsIfNoDefaultFilesButIsOnGCE()
{
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
setHomeEnv(__DIR__ . '/not_exist_fixtures');
$wantedTokens = [
'access_token' => '1/abdef1234567890',
'expires_in' => '57',
Expand All @@ -553,7 +553,7 @@ public function testGetIdTokenCredentialsSuccedsIfNoDefaultFilesButIsOnGCE()

public function testGetIdTokenCredentialsWithUserRefreshCredentials()
{
putenv('HOME=' . __DIR__ . '/fixtures2');
setHomeEnv(__DIR__ . '/fixtures2');

$creds = ApplicationDefaultCredentials::getIdTokenCredentials(
$this->targetAudience,
Expand Down Expand Up @@ -610,7 +610,7 @@ public function testGetCredentialsUtilizesQuotaProjectEnvVar()
{
$quotaProject = 'quota-project-from-env-var';
putenv(CredentialsLoader::QUOTA_PROJECT_ENV_VAR . '=' . $quotaProject);
putenv('HOME=' . __DIR__ . '/fixtures');
setHomeEnv(__DIR__ . '/fixtures');

$credentials = ApplicationDefaultCredentials::getCredentials();

Expand All @@ -625,7 +625,7 @@ public function testGetCredentialsUtilizesQuotaProjectParameterOverEnvVar()
{
$quotaProject = 'quota-project-from-parameter';
putenv(CredentialsLoader::QUOTA_PROJECT_ENV_VAR . '=quota-project-from-env-var');
putenv('HOME=' . __DIR__ . '/fixtures');
setHomeEnv(__DIR__ . '/fixtures');

$credentials = ApplicationDefaultCredentials::getCredentials(
null, // $scope
Expand Down Expand Up @@ -688,7 +688,7 @@ public function testWithFetchAuthTokenCacheAndExplicitQuotaProject()

public function testWithGCECredentials()
{
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
setHomeEnv(__DIR__ . '/not_exist_fixtures');
$wantedTokens = [
'access_token' => '1/abdef1234567890',
'expires_in' => '57',
Expand Down Expand Up @@ -721,7 +721,7 @@ public function testWithGCECredentials()
public function testAppEngineStandard()
{
$_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
setHomeEnv(__DIR__ . '/not_exist_fixtures');
$this->assertInstanceOf(
'Google\Auth\Credentials\AppIdentityCredentials',
ApplicationDefaultCredentials::getCredentials()
Expand All @@ -732,7 +732,7 @@ public function testAppEngineFlexible()
{
$_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
putenv('GAE_INSTANCE=aef-default-20180313t154438');
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
setHomeEnv(__DIR__ . '/not_exist_fixtures');
$httpHandler = getHandler([
new Response(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
]);
Expand All @@ -746,7 +746,7 @@ public function testAppEngineFlexibleIdToken()
{
$_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
putenv('GAE_INSTANCE=aef-default-20180313t154438');
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
setHomeEnv(__DIR__ . '/not_exist_fixtures');
$httpHandler = getHandler([
new Response(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
]);
Expand Down Expand Up @@ -868,7 +868,7 @@ public function testUniverseDomainInKeyFile()
/** @runInSeparateProcess */
public function testUniverseDomainInGceCredentials()
{
putenv('HOME');
setHomeEnv(null);

$expectedUniverseDomain = 'example-universe.com';
$creds = ApplicationDefaultCredentials::getCredentials(
Expand Down
26 changes: 17 additions & 9 deletions tests/Credentials/ExternalAccountCredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,12 @@ public function testExecutableSourceCacheKey()
*/
public function testExecutableCredentialSourceEnvironmentVars()
{
if (PHP_OS_FAMILY === 'Windows') {
$this->markTestSkipped('This test does not work on Windows');
}

putenv('GOOGLE_EXTERNAL_ACCOUNT_ALLOW_EXECUTABLES=1');

$tmpFile = tempnam(sys_get_temp_dir(), 'test');
$outputFile = tempnam(sys_get_temp_dir(), 'output');
$fileContents = 'foo-' . rand();
Expand All @@ -597,20 +602,23 @@ public function testExecutableCredentialSourceEnvironmentVars()
'id_token' => 'abc',
'expiration_time' => time() + 100,
]);

$command = sprintf(
'echo $GOOGLE_EXTERNAL_ACCOUNT_AUDIENCE,$GOOGLE_EXTERNAL_ACCOUNT_TOKEN_TYPE,%s > %s' .
' && echo \'%s\' > $GOOGLE_EXTERNAL_ACCOUNT_OUTPUT_FILE' .
' && echo \'%s\'',
$fileContents,
$tmpFile,
$successJson,
$successJson
);

$json = [
'audience' => 'test-audience',
'subject_token_type' => 'test-token-type',
'credential_source' => [
'executable' => [
'command' => sprintf(
'echo $GOOGLE_EXTERNAL_ACCOUNT_AUDIENCE,$GOOGLE_EXTERNAL_ACCOUNT_TOKEN_TYPE,%s > %s' .
' && echo \'%s\' > $GOOGLE_EXTERNAL_ACCOUNT_OUTPUT_FILE ' .
' && echo \'%s\'',
$fileContents,
$tmpFile,
$successJson,
$successJson,
),
'command' => $command,
'timeout_millis' => 5000,
'output_file' => $outputFile,
],
Expand Down
4 changes: 2 additions & 2 deletions tests/Credentials/ServiceAccountCredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function testSucceedIfFileExists()
/** @runInSeparateProcess */
public function testIsNullIfFileDoesNotExist()
{
putenv('HOME=' . __DIR__ . '/../not_exists_fixtures');
setHomeEnv(__DIR__ . '/../not_exists_fixtures');
$this->assertNull(
ServiceAccountCredentials::fromWellKnownFile()
);
Expand All @@ -201,7 +201,7 @@ public function testIsNullIfFileDoesNotExist()
/** @runInSeparateProcess */
public function testSucceedIfFileIsPresent()
{
putenv('HOME=' . __DIR__ . '/../fixtures');
setHomeEnv(__DIR__ . '/../fixtures');
$this->assertNotNull(
ApplicationDefaultCredentials::getCredentials('a scope')
);
Expand Down
6 changes: 3 additions & 3 deletions tests/Credentials/UserRefreshCredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function tearDown(): void
{
putenv(UserRefreshCredentials::ENV_VAR); // removes it from
if ($this->originalHome != getenv('HOME')) {
putenv('HOME=' . $this->originalHome);
setHomeEnv($this->originalHome);
}
}

Expand Down Expand Up @@ -179,15 +179,15 @@ public function testSucceedIfFileExists()

public function testIsNullIfFileDoesNotExist()
{
putenv('HOME=' . __DIR__ . '/../not_exist_fixtures');
setHomeEnv(__DIR__ . '/../not_exist_fixtures');
$this->assertNull(
UserRefreshCredentials::fromWellKnownFile('a scope')
);
}

public function testSucceedIfFileIsPresent()
{
putenv('HOME=' . __DIR__ . '/../fixtures2');
setHomeEnv(__DIR__ . '/../fixtures2');
$this->assertNotNull(
ApplicationDefaultCredentials::getCredentials('a scope')
);
Expand Down
12 changes: 6 additions & 6 deletions tests/CredentialsLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testUpdateMetadataSkipsWhenAuthenticationisSet()
/** @runInSeparateProcess */
public function testGetDefaultClientCertSource()
{
putenv('HOME=' . __DIR__ . '/fixtures4/valid');
setHomeEnv(__DIR__ . '/fixtures4/valid');

$callback = CredentialsLoader::getDefaultClientCertSource();
$this->assertNotNull($callback);
Expand All @@ -47,7 +47,7 @@ public function testGetDefaultClientCertSource()
/** @runInSeparateProcess */
public function testNonExistantDefaultClientCertSource()
{
putenv('HOME=');
setHomeEnv(null);

$callback = CredentialsLoader::getDefaultClientCertSource();
$this->assertNull($callback);
Expand All @@ -61,7 +61,7 @@ public function testDefaultClientCertSourceInvalidJsonThrowsException()
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Invalid client cert source JSON');

putenv('HOME=' . __DIR__ . '/fixtures4/invalidjson');
setHomeEnv(__DIR__ . '/fixtures4/invalidjson');

CredentialsLoader::getDefaultClientCertSource();
}
Expand All @@ -74,7 +74,7 @@ public function testDefaultClientCertSourceInvalidKeyThrowsException()
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('cert source requires "cert_provider_command"');

putenv('HOME=' . __DIR__ . '/fixtures4/invalidkey');
setHomeEnv(__DIR__ . '/fixtures4/invalidkey');

CredentialsLoader::getDefaultClientCertSource();
}
Expand All @@ -87,7 +87,7 @@ public function testDefaultClientCertSourceInvalidValueThrowsException()
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('cert source expects "cert_provider_command" to be an array');

putenv('HOME=' . __DIR__ . '/fixtures4/invalidvalue');
setHomeEnv(__DIR__ . '/fixtures4/invalidvalue');

CredentialsLoader::getDefaultClientCertSource();
}
Expand Down Expand Up @@ -115,7 +115,7 @@ public function testDefaultClientCertSourceInvalidCmdThrowsException()
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('"cert_provider_command" failed with a nonzero exit code');

putenv('HOME=' . __DIR__ . '/fixtures4/invalidcmd');
setHomeEnv(__DIR__ . '/fixtures4/invalidcmd');

$callback = CredentialsLoader::getDefaultClientCertSource();

Expand Down
Loading