Skip to content

Commit 21e831f

Browse files
committed
Fix inconsistency with .env loaders out of the box
PHP .env loaders such as `symfony/dotenv` or `vlucas/phpdotenv` populate `$_ENV` out of the box, but don't call `putenv()` in order to preserve thread safeness. This means .env is loaded by them, but AWS SDK does not read it, creating inconsistent experience when handling real env variables and env variables loaded by these loaders
1 parent 4f59bf5 commit 21e831f

24 files changed

+61
-0
lines changed

features/bootstrap/Aws/Test/Integ/ClientSideMonitoringContext.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use PHPUnit\Framework\Assert;
2222
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
2323

24+
use function Aws\getenv;
25+
2426
class ClientSideMonitoringContext extends TestCase
2527
implements Context, SnippetAcceptingContext
2628
{

features/bootstrap/Aws/Test/Integ/IntegUtils.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Aws\Test\Integ;
33

4+
use function Aws\getenv;
5+
46
trait IntegUtils
57
{
68
private static $originalCsmEnabled;

src/AbstractConfigurationProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
use GuzzleHttp\Promise;
55

6+
use function Aws\getenv;
7+
68
/**
79
* A configuration provider is a function that returns a promise that is
810
* fulfilled with a configuration object. This class provides base functionality

src/ClientResolver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
use InvalidArgumentException as IAE;
3737
use Psr\Http\Message\RequestInterface;
3838

39+
use function Aws\getenv;
40+
3941
/**
4042
* @internal Resolves a hash of client arguments to construct a client.
4143
*/

src/ClientSideMonitoring/ConfigurationProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use GuzzleHttp\Promise;
99
use GuzzleHttp\Promise\PromiseInterface;
1010

11+
use function Aws\getenv;
12+
1113
/**
1214
* A configuration provider is a function that accepts no arguments and returns
1315
* a promise that is fulfilled with a {@see \Aws\ClientSideMonitoring\ConfigurationInterface}

src/Configuration/ConfigurationResolver.php

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

33
namespace Aws\Configuration;
44

5+
use function Aws\getenv;
6+
57
class ConfigurationResolver
68
{
79
const ENV_PROFILE = 'AWS_PROFILE';

src/Credentials/AssumeRoleWithWebIdentityCredentialProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Aws\Sts\StsClient;
88
use GuzzleHttp\Promise;
99

10+
use function Aws\getenv;
11+
1012
/**
1113
* Credential provider that provides credentials via assuming a role with a web identity
1214
* More Information, see: https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sts-2011-06-15.html#assumerolewithwebidentity

src/Credentials/CredentialProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Aws\Exception\CredentialsException;
88
use Aws\Sts\StsClient;
99
use GuzzleHttp\Promise;
10+
11+
use function Aws\getenv;
1012
/**
1113
* Credential providers are functions that accept no arguments and return a
1214
* promise that is fulfilled with an {@see \Aws\Credentials\CredentialsInterface}

src/Credentials/EcsCredentialProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use GuzzleHttp\Promise\PromiseInterface;
88
use Psr\Http\Message\ResponseInterface;
99

10+
use function Aws\getenv;
11+
1012
/**
1113
* Credential provider that fetches container credentials with GET request.
1214
* container environment variables are used in constructing request URI.

src/Credentials/InstanceProfileProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use GuzzleHttp\Promise\PromiseInterface;
1212
use Psr\Http\Message\ResponseInterface;
1313

14+
use function Aws\getenv;
15+
1416
/**
1517
* Credential provider that provides credentials from the EC2 metadata service.
1618
*/

src/DefaultsMode/ConfigurationProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use GuzzleHttp\Promise;
99
use GuzzleHttp\Promise\PromiseInterface;
1010

11+
use function Aws\getenv;
12+
1113
/**
1214
* A configuration provider is a function that returns a promise that is
1315
* fulfilled with a {@see \Aws\DefaultsMode\ConfigurationInterface}

src/Endpoint/UseDualstackEndpoint/ConfigurationProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Aws\Endpoint\UseDualstackEndpoint\Exception\ConfigurationException;
88
use GuzzleHttp\Promise;
99

10+
use function Aws\getenv;
11+
1012
/**
1113
* A configuration provider is a function that returns a promise that is
1214
* fulfilled with a {@see \Aws\Endpoint\UseDualstackEndpoint\onfigurationInterface}

src/Endpoint/UseFipsEndpoint/ConfigurationProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Aws\Endpoint\UseFipsEndpoint\Exception\ConfigurationException;
88
use GuzzleHttp\Promise;
99

10+
use function Aws\getenv;
11+
1012
/**
1113
* A configuration provider is a function that returns a promise that is
1214
* fulfilled with a {@see \Aws\Endpoint\UseFipsEndpoint\onfigurationInterface}

src/EndpointDiscovery/ConfigurationProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use GuzzleHttp\Promise;
99
use GuzzleHttp\Promise\PromiseInterface;
1010

11+
use function Aws\getenv;
12+
1113
/**
1214
* A configuration provider is a function that returns a promise that is
1315
* fulfilled with a {@see \Aws\EndpointDiscovery\ConfigurationInterface}

src/Middleware.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use GuzzleHttp\Psr7\LazyOpenStream;
1515
use Psr\Http\Message\RequestInterface;
1616

17+
use function Aws\getenv;
18+
1719
final class Middleware
1820
{
1921
/**

src/Retry/ConfigurationProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use GuzzleHttp\Promise;
99
use GuzzleHttp\Promise\PromiseInterface;
1010

11+
use function Aws\getenv;
12+
1113
/**
1214
* A configuration provider is a function that returns a promise that is
1315
* fulfilled with a {@see \Aws\Retry\ConfigurationInterface}

src/S3/RegionalEndpoint/ConfigurationProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Aws\S3\RegionalEndpoint\Exception\ConfigurationException;
88
use GuzzleHttp\Promise;
99

10+
use function Aws\getenv;
11+
1012
/**
1113
* A configuration provider is a function that returns a promise that is
1214
* fulfilled with a {@see \Aws\S3\RegionalEndpoint\ConfigurationInterface}

src/S3/UseArnRegion/ConfigurationProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Aws\S3\UseArnRegion\Exception\ConfigurationException;
88
use GuzzleHttp\Promise;
99

10+
use function Aws\getenv;
11+
1012
/**
1113
* A configuration provider is a function that returns a promise that is
1214
* fulfilled with a {@see \Aws\S3\UseArnRegion\ConfigurationInterface}

src/Sts/RegionalEndpoints/ConfigurationProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use GuzzleHttp\Promise;
99
use GuzzleHttp\Promise\PromiseInterface;
1010

11+
use function Aws\getenv;
12+
1113
/**
1214
* A configuration provider is a function that returns a promise that is
1315
* fulfilled with a {@see \Aws\Sts\RegionalEndpoints\ConfigurationInterface}

src/Token/ParsesIniTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Aws\Token;
33

4+
use function Aws\getenv;
5+
46
trait ParsesIniTrait
57
{
68
/**

src/Token/SsoTokenProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
use Aws\Exception\TokenException;
55
use GuzzleHttp\Promise;
66

7+
use function Aws\getenv;
8+
79
/**
810
* Token that comes from the SSO provider
911
*/

src/Token/TokenProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Aws\Exception\TokenException;
88
use GuzzleHttp\Promise;
99

10+
use function Aws\getenv;
11+
1012
/**
1113
* Token providers are functions that accept no arguments and return a
1214
* promise that is fulfilled with an {@see \Aws\Token\TokenInterface}

src/functions.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,3 +603,11 @@ function strip_fips_pseudo_regions($region)
603603
return str_replace(['fips-', '-fips'], ['', ''], $region);
604604
}
605605

606+
/** @return string|false */
607+
function getenv(string $name)
608+
{
609+
$out = \getenv($name);
610+
611+
return $out === false ? ($_ENV[$name] ?? false) : $out;
612+
}
613+

tests/FunctionsTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,4 +516,13 @@ public function getIniFileServiceTestCases()
516516
]
517517
];
518518
}
519+
520+
/**
521+
* @covers Aws\getenv()
522+
*/
523+
public function testgetenv()
524+
{
525+
$this->assertEquals(false, Aws\getenv('FOO'));
526+
$this->assertEquals($_ENV['FOO'] = 'bar', Aws\getenv('FOO'));
527+
}
519528
}

0 commit comments

Comments
 (0)