cube icon indicating copy to clipboard operation
cube copied to clipboard

Cubestore: S3 refresh loop not detecting new AIM Role token from EKS IRSA file

Open barbarosh opened this issue 4 months ago • 4 comments

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

  1. Run CubeStore in Kubernetes/EKS using a ServiceAccount associated with an AWS IAM role via IRSA/Web Identity.

  2. 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>
  1. Do not configure static AWS access and secret keys.

  2. Configure CubeStore to use an S3 bucket.

  3. Start CubeStore and verify that initial S3 access works successfully.

  4. Keep CubeStore running long enough for the Kubernetes projected ServiceAccount token and AWS STS credentials to rotate/expire.

  5. Observe that CubeStore may continue using expired AWS credentials or fail to refresh credentials successfully.

  6. 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:

  1. Kubernetes rotates the projected ServiceAccount token.
  2. CubeStore detects that the JWT content has changed.
  3. CubeStore calls AssumeRoleWithWebIdentity using the new JWT.
  4. New temporary AWS credentials are obtained.
  5. The S3 client/bucket credentials are replaced.
  6. 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.

barbarosh avatar Jul 14 '26 15:07 barbarosh

**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.~~

Necrokefalos avatar Jul 20 '26 14:07 Necrokefalos

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 AssumeRoleWithWebIdentity and 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-token volume used the default expirationSeconds: 86400.
  • The STS credentials obtained by CubeStore expired after approximately one hour.
  • CubeStore only requested new STS credentials when the projected token file's mtime changed.

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.

Necrokefalos avatar Jul 22 '26 14:07 Necrokefalos

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_MINS is 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 AssumeRoleWithWebIdentity succeeds, 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

paveltiunov avatar Oct 15 '26 16:10 paveltiunov

Thanks

barbarosh avatar Oct 18 '26 11:10 barbarosh