Skip to content

Commit

Permalink
Fix inconsistency with .env loaders out of the box
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ostrolucky committed Dec 27, 2023
1 parent 4f59bf5 commit 21e831f
Show file tree
Hide file tree
Showing 24 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use PHPUnit\Framework\Assert;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;

use function Aws\getenv;

class ClientSideMonitoringContext extends TestCase
implements Context, SnippetAcceptingContext
{
Expand Down
2 changes: 2 additions & 0 deletions features/bootstrap/Aws/Test/Integ/IntegUtils.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Aws\Test\Integ;

use function Aws\getenv;

trait IntegUtils
{
private static $originalCsmEnabled;
Expand Down
2 changes: 2 additions & 0 deletions src/AbstractConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

use GuzzleHttp\Promise;

use function Aws\getenv;

/**
* A configuration provider is a function that returns a promise that is
* fulfilled with a configuration object. This class provides base functionality
Expand Down
2 changes: 2 additions & 0 deletions src/ClientResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
use InvalidArgumentException as IAE;
use Psr\Http\Message\RequestInterface;

use function Aws\getenv;

/**
* @internal Resolves a hash of client arguments to construct a client.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/ClientSideMonitoring/ConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use GuzzleHttp\Promise;
use GuzzleHttp\Promise\PromiseInterface;

use function Aws\getenv;

/**
* A configuration provider is a function that accepts no arguments and returns
* a promise that is fulfilled with a {@see \Aws\ClientSideMonitoring\ConfigurationInterface}
Expand Down
2 changes: 2 additions & 0 deletions src/Configuration/ConfigurationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Aws\Configuration;

use function Aws\getenv;

class ConfigurationResolver
{
const ENV_PROFILE = 'AWS_PROFILE';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Aws\Sts\StsClient;
use GuzzleHttp\Promise;

use function Aws\getenv;

/**
* Credential provider that provides credentials via assuming a role with a web identity
* More Information, see: https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sts-2011-06-15.html#assumerolewithwebidentity
Expand Down
2 changes: 2 additions & 0 deletions src/Credentials/CredentialProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Aws\Exception\CredentialsException;
use Aws\Sts\StsClient;
use GuzzleHttp\Promise;

use function Aws\getenv;
/**
* Credential providers are functions that accept no arguments and return a
* promise that is fulfilled with an {@see \Aws\Credentials\CredentialsInterface}
Expand Down
2 changes: 2 additions & 0 deletions src/Credentials/EcsCredentialProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Message\ResponseInterface;

use function Aws\getenv;

/**
* Credential provider that fetches container credentials with GET request.
* container environment variables are used in constructing request URI.
Expand Down
2 changes: 2 additions & 0 deletions src/Credentials/InstanceProfileProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Message\ResponseInterface;

use function Aws\getenv;

/**
* Credential provider that provides credentials from the EC2 metadata service.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/DefaultsMode/ConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use GuzzleHttp\Promise;
use GuzzleHttp\Promise\PromiseInterface;

use function Aws\getenv;

/**
* A configuration provider is a function that returns a promise that is
* fulfilled with a {@see \Aws\DefaultsMode\ConfigurationInterface}
Expand Down
2 changes: 2 additions & 0 deletions src/Endpoint/UseDualstackEndpoint/ConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Aws\Endpoint\UseDualstackEndpoint\Exception\ConfigurationException;
use GuzzleHttp\Promise;

use function Aws\getenv;

/**
* A configuration provider is a function that returns a promise that is
* fulfilled with a {@see \Aws\Endpoint\UseDualstackEndpoint\onfigurationInterface}
Expand Down
2 changes: 2 additions & 0 deletions src/Endpoint/UseFipsEndpoint/ConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Aws\Endpoint\UseFipsEndpoint\Exception\ConfigurationException;
use GuzzleHttp\Promise;

use function Aws\getenv;

/**
* A configuration provider is a function that returns a promise that is
* fulfilled with a {@see \Aws\Endpoint\UseFipsEndpoint\onfigurationInterface}
Expand Down
2 changes: 2 additions & 0 deletions src/EndpointDiscovery/ConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use GuzzleHttp\Promise;
use GuzzleHttp\Promise\PromiseInterface;

use function Aws\getenv;

/**
* A configuration provider is a function that returns a promise that is
* fulfilled with a {@see \Aws\EndpointDiscovery\ConfigurationInterface}
Expand Down
2 changes: 2 additions & 0 deletions src/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use GuzzleHttp\Psr7\LazyOpenStream;
use Psr\Http\Message\RequestInterface;

use function Aws\getenv;

final class Middleware
{
/**
Expand Down
2 changes: 2 additions & 0 deletions src/Retry/ConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use GuzzleHttp\Promise;
use GuzzleHttp\Promise\PromiseInterface;

use function Aws\getenv;

/**
* A configuration provider is a function that returns a promise that is
* fulfilled with a {@see \Aws\Retry\ConfigurationInterface}
Expand Down
2 changes: 2 additions & 0 deletions src/S3/RegionalEndpoint/ConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Aws\S3\RegionalEndpoint\Exception\ConfigurationException;
use GuzzleHttp\Promise;

use function Aws\getenv;

/**
* A configuration provider is a function that returns a promise that is
* fulfilled with a {@see \Aws\S3\RegionalEndpoint\ConfigurationInterface}
Expand Down
2 changes: 2 additions & 0 deletions src/S3/UseArnRegion/ConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Aws\S3\UseArnRegion\Exception\ConfigurationException;
use GuzzleHttp\Promise;

use function Aws\getenv;

/**
* A configuration provider is a function that returns a promise that is
* fulfilled with a {@see \Aws\S3\UseArnRegion\ConfigurationInterface}
Expand Down
2 changes: 2 additions & 0 deletions src/Sts/RegionalEndpoints/ConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use GuzzleHttp\Promise;
use GuzzleHttp\Promise\PromiseInterface;

use function Aws\getenv;

/**
* A configuration provider is a function that returns a promise that is
* fulfilled with a {@see \Aws\Sts\RegionalEndpoints\ConfigurationInterface}
Expand Down
2 changes: 2 additions & 0 deletions src/Token/ParsesIniTrait.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Aws\Token;

use function Aws\getenv;

trait ParsesIniTrait
{
/**
Expand Down
2 changes: 2 additions & 0 deletions src/Token/SsoTokenProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use Aws\Exception\TokenException;
use GuzzleHttp\Promise;

use function Aws\getenv;

/**
* Token that comes from the SSO provider
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Token/TokenProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Aws\Exception\TokenException;
use GuzzleHttp\Promise;

use function Aws\getenv;

/**
* Token providers are functions that accept no arguments and return a
* promise that is fulfilled with an {@see \Aws\Token\TokenInterface}
Expand Down
8 changes: 8 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,11 @@ function strip_fips_pseudo_regions($region)
return str_replace(['fips-', '-fips'], ['', ''], $region);
}

/** @return string|false */
function getenv(string $name)
{
$out = \getenv($name);

return $out === false ? ($_ENV[$name] ?? false) : $out;
}

9 changes: 9 additions & 0 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,13 @@ public function getIniFileServiceTestCases()
]
];
}

/**
* @covers Aws\getenv()
*/
public function testgetenv()
{
$this->assertEquals(false, Aws\getenv('FOO'));
$this->assertEquals($_ENV['FOO'] = 'bar', Aws\getenv('FOO'));
}
}

0 comments on commit 21e831f

Please sign in to comment.