Skip to content

Commit

Permalink
fix: clear cached path on ECS token file
Browse files Browse the repository at this point in the history
  • Loading branch information
yenfryherrerafeliz committed May 28, 2024
1 parent 079dd48 commit 5e2607b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .changes/nextrelease/fix-clear-cached-path-ecs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"type": "bugfix",
"category": "Credentials",
"description": "Clears cached path for authorization token when necessary on ECS credentials provider."
}
]
21 changes: 16 additions & 5 deletions src/Credentials/EcsCredentialProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,24 @@ public function __invoke()
private function getEcsAuthToken()
{
if (!empty($path = getenv(self::ENV_AUTH_TOKEN_FILE))) {
if (is_readable($path)) {
return file_get_contents($path);
$token = @file_get_contents($path);
if (false === $token) {
clearstatcache(true, dirname($path) . DIRECTORY_SEPARATOR . @readlink($path));
clearstatcache(true, dirname($path) . DIRECTORY_SEPARATOR . dirname(@readlink($path)));
clearstatcache(true, $path);
}

throw new CredentialsException(
"Failed to read authorization token from '{$path}': no such file or directory."
);
if (!is_readable($path)) {
throw new CredentialsException("Failed to read authorization token from '{$path}': no such file or directory.");
}

$token = @file_get_contents($path);

if (empty($token)) {
throw new CredentialsException("Invalid authorization token read from `$path`. Token file is empty!");
}

return $token;
}

return getenv(self::ENV_AUTH_TOKEN);
Expand Down

0 comments on commit 5e2607b

Please sign in to comment.