Skip to content

Commit b4d0531

Browse files
Jean85Nyholm
andauthored
Clear realpath cache when reading the token from file (#901)
* Apply same fix as aws/aws-sdk-php#2043 * Fix CS * Add intermediate variables to save on duplicate function calls * Update src/Core/src/Credentials/WebIdentityProvider.php Co-authored-by: Tobias Nyholm <tobias.nyholm@gmail.com>
1 parent 90ae174 commit b4d0531

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/Credentials/WebIdentityProvider.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@ private function getCredentialsFromRole(string $roleArn, string $tokenFile, ?str
8989
}
9090

9191
try {
92-
if (false === $token = file_get_contents($tokenFile)) {
93-
throw new RuntimeException('failed to read data');
94-
}
92+
$token = $this->getTokenFileContent($tokenFile);
9593
} catch (\Exception $e) {
9694
$this->logger->warning('"Error reading WebIdentityTokenFile "{tokenFile}.', ['tokenFile' => $tokenFile, 'exception' => $e]);
9795

@@ -122,4 +120,26 @@ private function getCredentialsFromRole(string $roleArn, string $tokenFile, ?str
122120
Credentials::adjustExpireDate($credentials->getExpiration(), $this->getDateFromResult($result))
123121
);
124122
}
123+
124+
/**
125+
* @see https://github.com/async-aws/aws/issues/900
126+
* @see https://github.com/aws/aws-sdk-php/issues/2014
127+
* @see https://github.com/aws/aws-sdk-php/pull/2043
128+
*/
129+
private function getTokenFileContent(string $tokenFile): string
130+
{
131+
if (!file_exists($tokenFile)) {
132+
$tokenDir = \dirname($tokenFile);
133+
$tokenLink = readlink($tokenFile);
134+
clearstatcache(true, $tokenDir . \DIRECTORY_SEPARATOR . $tokenLink);
135+
clearstatcache(true, $tokenDir . \DIRECTORY_SEPARATOR . \dirname($tokenLink));
136+
clearstatcache(true, $tokenFile);
137+
}
138+
139+
if (false === $token = file_get_contents($tokenFile)) {
140+
throw new RuntimeException('Failed to read data');
141+
}
142+
143+
return $token;
144+
}
125145
}

0 commit comments

Comments
 (0)