Cubestore: S3 refresh loop not detecting new AIM Role token from EKS IRSA file
Describe the bug
CubeStore fails to reliably refresh AWS S3 credentials when using AWS Web Identity credentials with a Kubernetes projected ServiceAccount token (for example, EKS IRSA).
CubeStore detects Web Identity token rotation by comparing the token file's modified timestamp (mtime):
let current_modified = std::fs::metadata(file)
.ok()
.and_then(|m| m.modified().ok());
if current_modified == last_modified {
continue;
}
However, Kubernetes projected ServiceAccount tokens are managed and rotated by kubelet. Relying only on the file modification timestamp is not a reliable way to detect that the JWT token content has changed.
As a result, CubeStore may fail to detect a rotated ServiceAccount token and does not call Credentials::from_sts(...) with the new token. The previously issued temporary AWS credentials eventually expire, causing S3 requests to fail with expired token/credentials errors.
Additionally, last_modified is updated before the STS credential refresh succeeds:
last_modified = current_modified;
info!("Web identity token file changed, refreshing S3 credentials");
If the STS request fails after last_modified has been updated, CubeStore considers the token file already processed and does not retry the refresh unless the file modification timestamp changes again.
To Reproduce
-
Run CubeStore in Kubernetes/EKS using a ServiceAccount associated with an AWS IAM role via IRSA/Web Identity.
-
Configure CubeStore to use the projected ServiceAccount token:
CUBESTORE_AWS_WEB_IDENTITY_TOKEN_FILE=/var/run/secrets/eks.amazonaws.com/serviceaccount/token
CUBESTORE_AWS_WEB_IDENTITY_ROLE_ARN=arn:aws:iam::<account-id>:role/<role-name>
-
Do not configure static AWS access and secret keys.
-
Configure CubeStore to use an S3 bucket.
-
Start CubeStore and verify that initial S3 access works successfully.
-
Keep CubeStore running long enough for the Kubernetes projected ServiceAccount token and AWS STS credentials to rotate/expire.
-
Observe that CubeStore may continue using expired AWS credentials or fail to refresh credentials successfully.
-
S3 operations start failing with an expired token/credentials error.
Restarting the CubeStore pod restores S3 access because CubeStore reads the current projected ServiceAccount token and obtains new STS credentials during startup.
Expected behavior
CubeStore should reliably refresh AWS credentials when using Kubernetes/EKS Web Identity authentication.
The refresh logic should not rely exclusively on the projected token file's mtime.
CubeStore should periodically read the token file and detect changes in the JWT content, for example by comparing the token content or a hash of the token.
The token should only be marked as successfully processed after Credentials::from_sts(...) succeeds.
If the STS credential refresh fails, CubeStore should retry the refresh on the next polling interval instead of waiting for another token file modification.
Expected behavior:
- Kubernetes rotates the projected ServiceAccount token.
- CubeStore detects that the JWT content has changed.
- CubeStore calls
AssumeRoleWithWebIdentityusing the new JWT. - New temporary AWS credentials are obtained.
- The S3 client/bucket credentials are replaced.
- S3 access continues without interruption.
Version:
1.6.69
Additional context
The issue occurs when running CubeStore on Kubernetes/EKS with AWS IRSA and a projected ServiceAccount token.
The relevant credential refresh logic uses file metadata to detect token rotation:
let mut last_modified = token_file
.as_ref()
.and_then(|f| std::fs::metadata(f).ok()?.modified().ok());
and later:
let current_modified = std::fs::metadata(file)
.ok()
.and_then(|m| m.modified().ok());
if current_modified == last_modified {
continue;
}
last_modified = current_modified;
A more reliable implementation would periodically read the projected token and compare its content or hash:
let jwt = std::fs::read_to_string(file)?;
if last_token.as_deref() == Some(jwt.as_str()) {
continue;
}
let credentials = Credentials::from_sts(
arn,
"cubestore",
&jwt,
)?;
// Mark the token as processed only after STS succeeds.
last_token = Some(jwt);
Alternatively, CubeStore could refresh temporary AWS credentials based on their expiration time with an appropriate safety margin.
Currently, restarting the CubeStore pod is a workaround because the current ServiceAccount token is read and fresh STS credentials are obtained during startup.
**Update:** This recommendation was incorrect for web-identity mode. Please see
my follow-up below; `CUBESTORE_AWS_CREDS_REFRESH_EVERY_MINS` should remain
unset.
~~I recently changed from iam user to irsa had the same issue refreshing token, but after some research i found this env var CUBESTORE_AWS_CREDS_REFRESH_EVERY_MINS.
For my case i deploy cubestore components with gadsme/cubestore helm chart, where by default this env var is not set, you can set it at cloudStorage.aws.refreshCredsEveryMinutes with value 60.
The above env var is not mentioned in the cube docs where instructs you how to configure IRSA instead of IAM user.~~
Correction to my previous comment: setting
CUBESTORE_AWS_CREDS_REFRESH_EVERY_MINS=60 does not resolve this issue and
should not be necessary for EKS IRSA.
I reproduced the problem with CubeStore v1.6.69:
- Initial
AssumeRoleWithWebIdentityand S3 operations succeeded. - The first S3 error occurred approximately 61 minutes after pod startup:
AWS S3 error: Got HTTP 400 with content ''. - The EKS-injected
aws-iam-tokenvolume used the defaultexpirationSeconds: 86400. - The STS credentials obtained by CubeStore expired after approximately one hour.
- CubeStore only requested new STS credentials when the projected token file's
mtimechanged.
When CUBESTORE_AWS_CREDS_REFRESH_EVERY_MINS is unset, CubeStore's web
identity refresh loop already checks the token file every 30 seconds. Setting
the variable to 60 changes that check interval to 60 minutes, but CubeStore
still skips the refresh when the token file has not changed. Therefore, lowering
or enabling this variable does not address the underlying problem.
As a temporary EKS workaround, I added the following annotation to the CubeStore pod template:
metadata:
annotations:
eks.amazonaws.com/token-expiration: "3600"
EKS then rotates the projected token at approximately 80% of its lifetime (around 48 minutes). CubeStore detects the token file change before its existing STS credentials expire and logs:
INFO [cubestore::remotefs::s3] <pid:1> Web identity token file changed, refreshing S3 credentials
The permanent fix should still be in CubeStore: refresh based on the STS
credential expiration with a safety margin, or reload/compare the token content
instead of depending only on its mtime.
This no longer reproduces on current master. It was fixed by #11808, which shipped in v1.8.0. With web identity credentials, Cube Store now:
- checks the token file every 30 seconds by default (
CUBESTORE_AWS_CREDS_REFRESH_EVERY_MINSis capped at 5 minutes in this mode); - refreshes the STS credentials before they expire, even when the token file's mtime hasn't changed;
- records the token mtime and expiration only after
AssumeRoleWithWebIdentitysucceeds, so a failed exchange is retried on the next poll.
I ran the refresh-loop unit tests in rust/cubestore/cubestore/src/remotefs/s3.rs on master and all of them pass, including refresh_needed_near_expiry_with_unchanged_token_file, refresh_needed_after_expiry_with_unchanged_token_file and failed_exchange_near_expiry_retries_on_next_poll, which cover the scenarios in this report.
Please upgrade to v1.8.0 or later. You can then remove the eks.amazonaws.com/token-expiration workaround. I'm closing this issue for now. If you still see expired-token errors on a current version, please open a new issue with your Cube Store version and logs.
Generated by Claude Code
Thanks