Skip to content

Commit 25c26e1

Browse files
committed
fix all putenv home, fix executable
1 parent 58c6896 commit 25c26e1

5 files changed

Lines changed: 94 additions & 45 deletions

tests/ApplicationDefaultCredentialsTest.php

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function testLoadsOKIfEnvSpecifiedIsValid()
7070

7171
public function testLoadsDefaultFileIfPresentAndEnvVarIsNotSet()
7272
{
73-
putenv('HOME=' . __DIR__ . '/fixtures');
73+
$this->setHomeEnv(__DIR__ . '/fixtures');
7474
$this->assertNotNull(
7575
ApplicationDefaultCredentials::getCredentials('a scope')
7676
);
@@ -80,7 +80,7 @@ public function testFailsIfNotOnGceAndNoDefaultFileFound()
8080
{
8181
$this->expectException(DomainException::class);
8282

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

9494
public function testSuccedsIfNoDefaultFilesButIsOnGCE()
9595
{
96-
putenv('HOME');
96+
$this->setHomeEnv(null);
9797

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

117117
public function testGceCredentials()
118118
{
119-
putenv('HOME');
119+
$this->setHomeEnv(null);
120120

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

@@ -160,7 +160,7 @@ public function testGceCredentials()
160160

161161
public function testImpersonatedServiceAccountCredentials()
162162
{
163-
putenv('HOME=' . __DIR__ . '/fixtures5');
163+
$this->setHomeEnv(__DIR__ . '/fixtures5');
164164
$creds = ApplicationDefaultCredentials::getCredentials(
165165
null,
166166
null,
@@ -183,7 +183,7 @@ public function testImpersonatedServiceAccountCredentials()
183183

184184
public function testUserRefreshCredentials()
185185
{
186-
putenv('HOME=' . __DIR__ . '/fixtures2');
186+
$this->setHomeEnv(__DIR__ . '/fixtures2');
187187

188188
$creds = ApplicationDefaultCredentials::getCredentials(
189189
null, // $scope
@@ -219,7 +219,7 @@ public function testUserRefreshCredentials()
219219

220220
public function testServiceAccountCredentials()
221221
{
222-
putenv('HOME=' . __DIR__ . '/fixtures');
222+
$this->setHomeEnv(__DIR__ . '/fixtures');
223223

224224
$creds = ApplicationDefaultCredentials::getCredentials(
225225
null, // $scope
@@ -255,7 +255,7 @@ public function testServiceAccountCredentials()
255255

256256
public function testDefaultScopeArray()
257257
{
258-
putenv('HOME=' . __DIR__ . '/fixtures2');
258+
$this->setHomeEnv(__DIR__ . '/fixtures2');
259259

260260
$creds = ApplicationDefaultCredentials::getCredentials(
261261
null, // $scope
@@ -292,15 +292,15 @@ public function testGetMiddlewareLoadsOKIfEnvSpecifiedIsValid()
292292

293293
public function testLGetMiddlewareoadsDefaultFileIfPresentAndEnvVarIsNotSet()
294294
{
295-
putenv('HOME=' . __DIR__ . '/fixtures');
295+
$this->setHomeEnv(__DIR__ . '/fixtures');
296296
$this->assertNotNull(ApplicationDefaultCredentials::getMiddleware('a scope'));
297297
}
298298

299299
public function testGetMiddlewareFailsIfNotOnGceAndNoDefaultFileFound()
300300
{
301301
$this->expectException(DomainException::class);
302302

303-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
303+
$this->setHomeEnv(__DIR__ . '/not_exist_fixtures');
304304

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

359-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
359+
$this->setHomeEnv(__DIR__ . '/not_exist_fixtures');
360360

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

381381
public function testOnGceCacheWithoutHit()
382382
{
383-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
383+
$this->setHomeEnv(__DIR__ . '/not_exist_fixtures');
384384

385385
$gceIsCalled = false;
386386
$dummyHandler = function ($request) use (&$gceIsCalled) {
@@ -416,7 +416,7 @@ public function testOnGceCacheWithoutHit()
416416

417417
public function testOnGceCacheWithOptions()
418418
{
419-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
419+
$this->setHomeEnv(__DIR__ . '/not_exist_fixtures');
420420

421421
$prefix = 'test_prefix_';
422422
$lifetime = '70707';
@@ -473,7 +473,7 @@ public function testGetIdTokenCredentialsLoadsOKIfEnvSpecifiedIsValid()
473473

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

486-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
486+
$this->setHomeEnv(__DIR__ . '/not_exist_fixtures');
487487

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

501501
public function testGetIdTokenCredentialsWithImpersonatedServiceAccountCredentials()
502502
{
503-
putenv('HOME=' . __DIR__ . '/fixtures5');
503+
$this->setHomeEnv(__DIR__ . '/fixtures5');
504504
$creds = ApplicationDefaultCredentials::getIdTokenCredentials('123@456.com');
505505
$this->assertInstanceOf(ImpersonatedServiceAccountCredentials::class, $creds);
506506
}
@@ -529,7 +529,7 @@ public function testGetIdTokenCredentialsWithCacheOptions()
529529

530530
public function testGetIdTokenCredentialsSuccedsIfNoDefaultFilesButIsOnGCE()
531531
{
532-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
532+
$this->setHomeEnv(__DIR__ . '/not_exist_fixtures');
533533
$wantedTokens = [
534534
'access_token' => '1/abdef1234567890',
535535
'expires_in' => '57',
@@ -553,7 +553,7 @@ public function testGetIdTokenCredentialsSuccedsIfNoDefaultFilesButIsOnGCE()
553553

554554
public function testGetIdTokenCredentialsWithUserRefreshCredentials()
555555
{
556-
putenv('HOME=' . __DIR__ . '/fixtures2');
556+
$this->setHomeEnv(__DIR__ . '/fixtures2');
557557

558558
$creds = ApplicationDefaultCredentials::getIdTokenCredentials(
559559
$this->targetAudience,
@@ -610,7 +610,7 @@ public function testGetCredentialsUtilizesQuotaProjectEnvVar()
610610
{
611611
$quotaProject = 'quota-project-from-env-var';
612612
putenv(CredentialsLoader::QUOTA_PROJECT_ENV_VAR . '=' . $quotaProject);
613-
putenv('HOME=' . __DIR__ . '/fixtures');
613+
$this->setHomeEnv(__DIR__ . '/fixtures');
614614

615615
$credentials = ApplicationDefaultCredentials::getCredentials();
616616

@@ -625,7 +625,7 @@ public function testGetCredentialsUtilizesQuotaProjectParameterOverEnvVar()
625625
{
626626
$quotaProject = 'quota-project-from-parameter';
627627
putenv(CredentialsLoader::QUOTA_PROJECT_ENV_VAR . '=quota-project-from-env-var');
628-
putenv('HOME=' . __DIR__ . '/fixtures');
628+
$this->setHomeEnv(__DIR__ . '/fixtures');
629629

630630
$credentials = ApplicationDefaultCredentials::getCredentials(
631631
null, // $scope
@@ -688,7 +688,7 @@ public function testWithFetchAuthTokenCacheAndExplicitQuotaProject()
688688

689689
public function testWithGCECredentials()
690690
{
691-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
691+
$this->setHomeEnv(__DIR__ . '/not_exist_fixtures');
692692
$wantedTokens = [
693693
'access_token' => '1/abdef1234567890',
694694
'expires_in' => '57',
@@ -721,7 +721,7 @@ public function testWithGCECredentials()
721721
public function testAppEngineStandard()
722722
{
723723
$_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
724-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
724+
$this->setHomeEnv(__DIR__ . '/not_exist_fixtures');
725725
$this->assertInstanceOf(
726726
'Google\Auth\Credentials\AppIdentityCredentials',
727727
ApplicationDefaultCredentials::getCredentials()
@@ -732,7 +732,7 @@ public function testAppEngineFlexible()
732732
{
733733
$_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
734734
putenv('GAE_INSTANCE=aef-default-20180313t154438');
735-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
735+
$this->setHomeEnv(__DIR__ . '/not_exist_fixtures');
736736
$httpHandler = getHandler([
737737
new Response(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
738738
]);
@@ -746,7 +746,7 @@ public function testAppEngineFlexibleIdToken()
746746
{
747747
$_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
748748
putenv('GAE_INSTANCE=aef-default-20180313t154438');
749-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
749+
$this->setHomeEnv(__DIR__ . '/not_exist_fixtures');
750750
$httpHandler = getHandler([
751751
new Response(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
752752
]);
@@ -868,7 +868,7 @@ public function testUniverseDomainInKeyFile()
868868
/** @runInSeparateProcess */
869869
public function testUniverseDomainInGceCredentials()
870870
{
871-
putenv('HOME');
871+
$this->setHomeEnv(null);
872872

873873
$expectedUniverseDomain = 'example-universe.com';
874874
$creds = ApplicationDefaultCredentials::getCredentials(
@@ -904,4 +904,16 @@ public function testUniverseDomainInGceCredentials()
904904
);
905905
$this->assertEquals(CredentialsLoader::DEFAULT_UNIVERSE_DOMAIN, $creds2->getUniverseDomain($httpHandler));
906906
}
907+
908+
private function setHomeEnv(string|null $value): void
909+
{
910+
$assigment = sprintf(
911+
'%s%s%s',
912+
PHP_OS_FAMILY === 'Windows' ? 'APPDATA' : 'HOME',
913+
$value === null ? '' : '=',
914+
(string) $value
915+
);
916+
917+
putenv($assigment);
918+
}
907919
}

tests/Credentials/ExternalAccountCredentialsTest.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -597,20 +597,21 @@ public function testExecutableCredentialSourceEnvironmentVars()
597597
'id_token' => 'abc',
598598
'expiration_time' => time() + 100,
599599
]);
600+
$rawBashCommand = sprintf(
601+
'echo $GOOGLE_EXTERNAL_ACCOUNT_AUDIENCE,$GOOGLE_EXTERNAL_ACCOUNT_TOKEN_TYPE,%s > %s' .
602+
' && echo \'%s\' > $GOOGLE_EXTERNAL_ACCOUNT_OUTPUT_FILE' .
603+
' && echo \'%s\'',
604+
escapeshellarg($fileContents),
605+
escapeshellarg($tmpFile),
606+
$successJson,
607+
$successJson
608+
);
600609
$json = [
601610
'audience' => 'test-audience',
602611
'subject_token_type' => 'test-token-type',
603612
'credential_source' => [
604613
'executable' => [
605-
'command' => sprintf(
606-
'echo $GOOGLE_EXTERNAL_ACCOUNT_AUDIENCE,$GOOGLE_EXTERNAL_ACCOUNT_TOKEN_TYPE,%s > %s' .
607-
' && echo \'%s\' > $GOOGLE_EXTERNAL_ACCOUNT_OUTPUT_FILE ' .
608-
' && echo \'%s\'',
609-
$fileContents,
610-
$tmpFile,
611-
$successJson,
612-
$successJson,
613-
),
614+
'command' => sprintf('bash -c %s', escapeshellarg($rawBashCommand)),
614615
'timeout_millis' => 5000,
615616
'output_file' => $outputFile,
616617
],

tests/Credentials/ServiceAccountCredentialsTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public function testSucceedIfFileExists()
192192
/** @runInSeparateProcess */
193193
public function testIsNullIfFileDoesNotExist()
194194
{
195-
putenv('HOME=' . __DIR__ . '/../not_exists_fixtures');
195+
$this->setHomeEnv(__DIR__ . '/../not_exists_fixtures');
196196
$this->assertNull(
197197
ServiceAccountCredentials::fromWellKnownFile()
198198
);
@@ -201,7 +201,7 @@ public function testIsNullIfFileDoesNotExist()
201201
/** @runInSeparateProcess */
202202
public function testSucceedIfFileIsPresent()
203203
{
204-
putenv('HOME=' . __DIR__ . '/../fixtures');
204+
$this->setHomeEnv(__DIR__ . '/../fixtures');
205205
$this->assertNotNull(
206206
ApplicationDefaultCredentials::getCredentials('a scope')
207207
);
@@ -424,4 +424,16 @@ public function testGetQuotaProject()
424424
$sa = new ServiceAccountCredentials('scope/1', $keyFile);
425425
$this->assertEquals('test_quota_project', $sa->getQuotaProject());
426426
}
427+
428+
private function setHomeEnv(string|null $value): void
429+
{
430+
$assigment = sprintf(
431+
'%s%s%s',
432+
PHP_OS_FAMILY === 'Windows' ? 'APPDATA' : 'HOME',
433+
$value === null ? '' : '=',
434+
(string) $value
435+
);
436+
437+
putenv($assigment);
438+
}
427439
}

tests/Credentials/UserRefreshCredentialsTest.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected function tearDown(): void
4040
{
4141
putenv(UserRefreshCredentials::ENV_VAR); // removes it from
4242
if ($this->originalHome != getenv('HOME')) {
43-
putenv('HOME=' . $this->originalHome);
43+
$this->setHomeEnv($this->originalHome);
4444
}
4545
}
4646

@@ -179,15 +179,15 @@ public function testSucceedIfFileExists()
179179

180180
public function testIsNullIfFileDoesNotExist()
181181
{
182-
putenv('HOME=' . __DIR__ . '/../not_exist_fixtures');
182+
$this->setHomeEnv(__DIR__ . '/../not_exist_fixtures');
183183
$this->assertNull(
184184
UserRefreshCredentials::fromWellKnownFile('a scope')
185185
);
186186
}
187187

188188
public function testSucceedIfFileIsPresent()
189189
{
190-
putenv('HOME=' . __DIR__ . '/../fixtures2');
190+
$this->setHomeEnv(__DIR__ . '/../fixtures2');
191191
$this->assertNotNull(
192192
ApplicationDefaultCredentials::getCredentials('a scope')
193193
);
@@ -286,4 +286,16 @@ public function testGetQuotaProject()
286286
$sa = new UserRefreshCredentials('a-scope', $keyFile);
287287
$this->assertEquals('test_quota_project', $sa->getQuotaProject());
288288
}
289+
290+
private function setHomeEnv(string|null $value): void
291+
{
292+
$assigment = sprintf(
293+
'%s%s%s',
294+
PHP_OS_FAMILY === 'Windows' ? 'APPDATA' : 'HOME',
295+
$value === null ? '' : '=',
296+
(string) $value
297+
);
298+
299+
putenv($assigment);
300+
}
289301
}

0 commit comments

Comments
 (0)