Skip to content

Commit 3e63576

Browse files
authored
feat: add support for $_ENV and $_SERVER in CredentialsLoader (#612)
1 parent aba8d8c commit 3e63576

File tree

5 files changed

+66
-5
lines changed

5 files changed

+66
-5
lines changed

src/CredentialsLoader.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private static function isOnWindows()
7676
*/
7777
public static function fromEnv()
7878
{
79-
$path = getenv(self::ENV_VAR);
79+
$path = self::getEnv(self::ENV_VAR);
8080
if (empty($path)) {
8181
return null;
8282
}
@@ -104,7 +104,7 @@ public static function fromEnv()
104104
public static function fromWellKnownFile()
105105
{
106106
$rootEnv = self::isOnWindows() ? 'APPDATA' : 'HOME';
107-
$path = [getenv($rootEnv)];
107+
$path = [self::getEnv($rootEnv)];
108108
if (!self::isOnWindows()) {
109109
$path[] = self::NON_WINDOWS_WELL_KNOWN_PATH_BASE;
110110
}
@@ -215,7 +215,7 @@ public static function makeInsecureCredentials()
215215
*/
216216
public static function quotaProjectFromEnv()
217217
{
218-
return getenv(self::QUOTA_PROJECT_ENV_VAR) ?: null;
218+
return self::getEnv(self::QUOTA_PROJECT_ENV_VAR) ?: null;
219219
}
220220

221221
/**
@@ -251,7 +251,7 @@ public static function getDefaultClientCertSource()
251251
*/
252252
public static function shouldLoadClientCertSource()
253253
{
254-
return filter_var(getenv(self::MTLS_CERT_ENV_VAR), FILTER_VALIDATE_BOOLEAN);
254+
return filter_var(self::getEnv(self::MTLS_CERT_ENV_VAR), FILTER_VALIDATE_BOOLEAN);
255255
}
256256

257257
/**
@@ -260,7 +260,7 @@ public static function shouldLoadClientCertSource()
260260
private static function loadDefaultClientCertSourceFile()
261261
{
262262
$rootEnv = self::isOnWindows() ? 'APPDATA' : 'HOME';
263-
$path = sprintf('%s/%s', getenv($rootEnv), self::MTLS_WELL_KNOWN_PATH);
263+
$path = sprintf('%s/%s', self::getEnv($rootEnv), self::MTLS_WELL_KNOWN_PATH);
264264
if (!file_exists($path)) {
265265
return null;
266266
}
@@ -292,4 +292,9 @@ public function getUniverseDomain(): string
292292
{
293293
return self::DEFAULT_UNIVERSE_DOMAIN;
294294
}
295+
296+
private static function getEnv(string $env): mixed
297+
{
298+
return getenv($env) ?: $_SERVER[$env] ?? $_ENV[$env] ?? null;
299+
}
295300
}

tests/CredentialsLoaderTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,59 @@ public function testShouldLoadClientCertSourceIsTrue()
154154

155155
$this->assertTrue(CredentialsLoader::shouldLoadClientCertSource());
156156
}
157+
158+
/**
159+
* @runInSeparateProcess
160+
*/
161+
public function testLoadJsonFromGetEnv(): void
162+
{
163+
putenv(CredentialsLoader::ENV_VAR . '=' . __DIR__ . '/fixtures7/getenv.json');
164+
165+
$json = CredentialsLoader::fromEnv();
166+
167+
$this->assertArrayHasKey('type', $json);
168+
$this->assertEquals('getenv', $json['type']);
169+
}
170+
171+
/**
172+
* @runInSeparateProcess
173+
*/
174+
public function testLoadJsonFromServer(): void
175+
{
176+
$_SERVER[CredentialsLoader::ENV_VAR] = __DIR__ . '/fixtures7/server.json';
177+
178+
$json = CredentialsLoader::fromEnv();
179+
180+
$this->assertArrayHasKey('type', $json);
181+
$this->assertEquals('server', $json['type']);
182+
}
183+
184+
/**
185+
* @runInSeparateProcess
186+
*/
187+
public function testLoadJsonFromEnv(): void
188+
{
189+
$_ENV[CredentialsLoader::ENV_VAR] = __DIR__ . '/fixtures7/env.json';
190+
191+
$json = CredentialsLoader::fromEnv();
192+
193+
$this->assertArrayHasKey('type', $json);
194+
$this->assertEquals('env', $json['type']);
195+
}
196+
197+
/**
198+
* @runInSeparateProcess
199+
*/
200+
public function testLoadJsonFromGetEnvBackwardsCompatibility(): void
201+
{
202+
$_SERVER[CredentialsLoader::ENV_VAR] = __DIR__ . '/fixtures7/server.json';
203+
putenv(CredentialsLoader::ENV_VAR . '=' . __DIR__ . '/fixtures7/getenv.json');
204+
205+
$json = CredentialsLoader::fromEnv();
206+
207+
$this->assertArrayHasKey('type', $json);
208+
$this->assertEquals('getenv', $json['type']);
209+
}
157210
}
158211

159212
class TestCredentialsLoader extends CredentialsLoader

tests/fixtures7/env.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"type": "env"}

tests/fixtures7/getenv.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"type": "getenv"}

tests/fixtures7/server.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"type": "server"}

0 commit comments

Comments
 (0)