Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative credential retrieval - threadsafe #2722

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"type": "bugfix",
"category": "",
"description": "Fix using credentials provider on multi-threaded servers"
}
]
4 changes: 2 additions & 2 deletions src/Credentials/CredentialProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ public static function env()
{
return function () {
// Use credentials from environment variables, if available
$key = getenv(self::ENV_KEY);
$secret = getenv(self::ENV_SECRET);
$key = getenv(self::ENV_KEY) ?: $_SERVER[self::ENV_KEY];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The $_SERVER references should check to see if it is set. Also, we should fall back to false if it isn't. I think you could add a null coalescing operator to what's already here and add false on the right side of the operand.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you Sean. And I think that's what I SHOULD have done with the ENV_SESSION variable too. 12 month ago, I was less familiar with the difference between null coalesce and Elvis!

Do we need more tests?

$secret = getenv(self::ENV_SECRET) ?: $_SERVER[self::ENV_SECRET];
if ($key && $secret) {
return Promise\Create::promiseFor(
new Credentials($key, $secret, getenv(self::ENV_SESSION) ?: NULL)
Expand Down
46 changes: 46 additions & 0 deletions tests/Credentials/CredentialProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,38 @@ private function clearEnv()
return $dir;
}

private function clearEnvExceptServer()
{
putenv(CredentialProvider::ENV_KEY . '=');
putenv(CredentialProvider::ENV_SECRET . '=');
putenv(CredentialProvider::ENV_PROFILE . '=');
putenv('AWS_CONTAINER_CREDENTIALS_RELATIVE_URI');
putenv('AWS_CONTAINER_CREDENTIALS_FULL_URI');
putenv('AWS_CONTAINER_AUTHORIZATION_TOKEN');
putenv('AWS_SDK_LOAD_NONDEFAULT_CONFIG');
putenv('AWS_WEB_IDENTITY_TOKEN_FILE');
putenv('AWS_ROLE_ARN');
putenv('AWS_ROLE_SESSION_NAME');
putenv('AWS_SHARED_CREDENTIALS_FILE');

unset($_SERVER['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI']);
unset($_SERVER['AWS_CONTAINER_CREDENTIALS_FULL_URI']);
unset($_SERVER['AWS_CONTAINER_AUTHORIZATION_TOKEN']);
unset($_SERVER['AWS_SDK_LOAD_NONDEFAULT_CONFIG']);
unset($_SERVER['AWS_WEB_IDENTITY_TOKEN_FILE']);
unset($_SERVER['AWS_ROLE_ARN']);
unset($_SERVER['AWS_ROLE_SESSION_NAME']);
unset($_SERVER['AWS_SHARED_CREDENTIALS_FILE']);

$dir = sys_get_temp_dir() . '/.aws';

if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}

return $dir;
}

public function set_up()
{
$this->home = getenv('HOME');
Expand Down Expand Up @@ -173,6 +205,20 @@ public function testCreatesFromEnvironmentVariables()
$this->assertSame('456', $creds->getSecurityToken());
}

/**
* @server ENV_KEY=abc
* @server ENV_SECRET=123
* @server ENV_SESSION=456
*/
public function testCreatesFromServerVariables()
{
$this->clearEnvExceptServer();
$creds = call_user_func(CredentialProvider::env())->wait();
$this->assertSame('abc', $creds->getAccessKeyId());
$this->assertSame('123', $creds->getSecretKey());
$this->assertSame('456', $creds->getSecurityToken());
}

public function testCreatesFromEnvironmentVariablesNullToken()
{
$this->clearEnv();
Expand Down
Loading