From 37bcf18a557ad95aaaea4c55700c846bd72a48ad Mon Sep 17 00:00:00 2001 From: Yenfry Herrera Feliz Date: Fri, 24 May 2024 08:27:00 -0700 Subject: [PATCH] fix: clear cached path on ECS token file --- .../fix-clear-cached-path-ecs.json | 7 +++++++ src/Credentials/EcsCredentialProvider.php | 21 ++++++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 .changes/nextrelease/fix-clear-cached-path-ecs.json diff --git a/.changes/nextrelease/fix-clear-cached-path-ecs.json b/.changes/nextrelease/fix-clear-cached-path-ecs.json new file mode 100644 index 0000000000..6ec9978a25 --- /dev/null +++ b/.changes/nextrelease/fix-clear-cached-path-ecs.json @@ -0,0 +1,7 @@ +[ + { + "type": "bugfix", + "category": "Credentials", + "description": "Clears cached path for authorization token when necessary on ECS credentials provider." + } +] diff --git a/src/Credentials/EcsCredentialProvider.php b/src/Credentials/EcsCredentialProvider.php index 16d30708a6..ab00733f14 100644 --- a/src/Credentials/EcsCredentialProvider.php +++ b/src/Credentials/EcsCredentialProvider.php @@ -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) . "/" . @readlink($path)); + clearstatcache(true, dirname($path) . "/" . 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);