Skip to content

Refresh statically configured GCP access token instead of caching it once - #5806

Merged
dimas-b merged 5 commits into
apache:mainfrom
iprithv:fix/gcp-static-token-refresh
Nov 6, 2026
Merged

dimas-b merged 5 commits into
apache:mainfrom
iprithv:fix/gcp-static-token-refresh

Conversation

@iprithv

@iprithv iprithv commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Polaris can take a GCP access token straight from config (polaris.storage.gcp.token) with an optional lifespan (gcp.lifespan, default 1h). On first use it wrapped that token in GoogleCredentials.create(token) and memoized it for the whole server lifetime, with the expiry stamped once as "now + lifespan". That credential cannot refresh, so once the lifespan passed, the sourceCredentials.refresh() call in GcpCredentialsStorageIntegration failed and server-side GCS FileIO stayed broken until a restart. The application-default credentials path is not affected because those refresh themselves.

This wraps the configured token in a small GoogleCredentials subclass that re-reads the configured value and re-stamps the expiry on every refresh, so the static-token path behaves like the ADC path.

Assumption: the lifespan acts as the refresh/re-stamp interval for the configured token (the same value is re-read from config on each refresh). Deployments whose config source can change at runtime pick up a rotated token without a restart; for a truly static value the token keeps working past the initial stamp instead of failing. If reviewers would prefer the lifespan stay a hard expiry, I can adjust.

@vigneshio vigneshio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @iprithv ! Left a couple of comments PTAL Below...

Fixes #2253

credentials.refresh();

AccessToken refreshed = credentials.getAccessToken();
assertThat(refreshed.getTokenValue()).isEqualTo(TEST_GCP_TOKEN);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This checks for the same TEST_GCP_TOKEN after refresh, but the whole point of the class is to pick up a rotated polaris.storage.gcp.token. can we swap the supplier mid test and assert refresh gives back the new value? WDYT??..

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch @vigneshio, and sorry for the confusion. @ConfigMapping keeps the original value, so token rotation isn’t picked up at runtime. refresh() only updates the token’s expiry.

Comment thread CHANGELOG.md Outdated
- A statically configured GCP access token (`polaris.storage.gcp.token`) is no longer cached for
the lifetime of the server. The token was wrapped in a credential that could not refresh, so its
one-time expiry eventually elapsed and server-side GCS access failed until the next restart. The
configured token is now re-read and its expiry re-stamped on every refresh, matching the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ADC actually fetches a new token from Google, but here it's just restamping the configured string, so it's not really the same thing. Can we just say refresh() re-reads polaris.storage.gcp.token and restamps the expiry so it doesn't throw anymore, and leave out the ADC comparison?

// GoogleCredentials.create(..) cannot refresh, which makes server FileIO against GCS
// fail once the lifespan elapses, until the server is restarted (#2253).
return new ConfiguredGcpAccessTokenCredentials(
() -> gcpAccessToken().orElseThrow(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This supplier never sees a rotated value: @ConfigMapping values are bound at creation.
I checked with a throwaway SmallRyeConfig test: after changing polaris.storage.gcp.token from token-A to token-B, the raw value is token-B but the mapping and refresh() still return token-A.
So the refresh re-stamps the expiry of the same token, which is what fixes #2253; the "re-reads the configured value" comment above could say that instead.

* polaris.storage.gcp.token} option).
*
* <p>Unlike {@link GoogleCredentials#create(AccessToken)}, which produces a credential that cannot
* refresh, this type re-reads the configured token and re-stamps its expiry on every {@link

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same point as on the supplier in StorageConfiguration: maybe describe this as re-stamping the expiry of the configured token on each refresh, since the value itself does not change at runtime.

@youngrae81

Copy link
Copy Markdown
Contributor

Checked out the branch and ran StorageConfigurationTest; all 8 tests pass, including the new one.
The fix for #2253 looks right to me; my inline comments are about describing what the refresh actually does.
LGTM otherwise (non-binding).

@iprithv
iprithv requested a review from vigneshio October 22, 2026 20:20
@iprithv

iprithv commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Checked out the branch and ran StorageConfigurationTest; all 8 tests pass, including the new one. The fix for #2253 looks right to me; my inline comments are about describing what the refresh actually does. LGTM otherwise (non-binding).

Thanks for confirming! The mapping retains the original token, so refresh only updates its expiry. Simplified the comment and Javadoc and removed the unnecessary Supplier indirection.

@dimas-b dimas-b left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, in general 👍 I wonder if we could simplify the code by using very long lifespans instead. though.

private static final long serialVersionUID = 1L;

private final String token;
private final transient Duration lifespan;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why transient?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this on the latest commit with a throwaway test: serialize the credential with ObjectOutputStream, read it back, then refresh. The cached token survives, but refreshAccessToken() and refresh() on the copy both fail with NullPointerException: Cannot invoke "java.time.Clock.instant()" because "clock" is null, since lifespan and clock come back as null.

With transient removed from both fields the same round trip works (Duration and Clock.systemUTC() are serializable).

As far as I can see Polaris itself never Java-serializes these credentials, so this is not hit today, but dropping transient seems simpler than keeping a half-restorable object.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, and thanks @youngrae81 for the serialization round-trip test - you're right. Removed transient from both fields. Duration and Clock.systemUTC() are both serializable, so the deserialized copy no longer comes back with a null clock and NPEs on refresh. Kept serialVersionUID to silence the missing-UID lint warning. Verified the module still compiles clean with no -Xlint:serial warning. Polaris doesn't Java-serialize these today, but no reason to ship a half-restorable object.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good

// until the server is restarted (#2253).
return new ConfiguredGcpAccessTokenCredentials(
gcpAccessToken().orElseThrow(),
gcpAccessTokenLifespan().orElse(DEFAULT_TOKEN_LIFESPAN),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to make the lifespan indefinite (or very long) instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that first but it doesn't get us out of it. compute() in GcpCredentialsStorageIntegration calls sourceCredentials.refresh() unconditionally on every vend (no expiry check), and a GoogleCredentials.create(token) credential can't refresh - refresh() throws IllegalStateException ("does not support refreshing the access token") regardless of the expiry. I checked against google-auth 1.52.0 with a 1h expiry, a 100-year expiry, an already-expired one, and null expiry, and it throws in all four cases. So the lifespan is never even looked at; the throw comes first. A very long/indefinite lifespan just changes the stamped number, not the throw, which is why it needs a refresh-capable credential. Happy to go a different shape if you'd prefer one, but it does have to be refresh-capable.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this property be treated as test-only? When it was added in #370, the Javadoc said it "should strictly be used for testing", and the PR said "users are never expected to pass in an access token via configuration string" (#370 (comment)). That Javadoc was dropped in the Quarkus move (#469), and nothing on main says so now: the property docs, application.properties and the Helm chart all present it as a normal option. Could the docs for polaris.storage.gcp.token say this is for testing? #2253 does treat this as a bug, though, and neither it nor this PR describes a real deployment using it, so I'm not sure which is intended.

I also don't think emulator tokens benefit from this during vending. compute passes the token to DownscopedCredentials, which posts to https://sts.googleapis.com/v1/token, and I couldn't find a way to change that from Polaris. So an emulator token would pass the local refresh and then go to Google's STS. I haven't run that against Google. The cases I found where a fake token gets through are tests with a fake GcpCredentialOps, and the it profile, which skips compute.

If it's test-only, that may also answer the long-lifespan question. The credential only has to be refresh-capable because compute calls refresh() unconditionally, while DownscopedCredentials already calls refreshIfExpired() on its source. I tried changing just that line and keeping GoogleCredentials.create(...), with a fake STS. An unexpired token vends, so the first-vend failure goes away with no new class. Within the library's 3-minute margin of the stamped expiry it fails with the same IllegalStateException, so lifespans of 3 minutes or less can't vend, and a long lifespan avoids that. For a service account key under ADC nothing changes (4 token fetches for 4 vends either way). gcloud user credentials go from 4 fetches to 1, so a revoked one is noticed later.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about the confusion... I'll start a new comment thread for my concern, hoping to explain my point more clearly 😅

@orca-review-bot orca-review-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

AI-generated review. Written by an agent. It can be wrong, so please push back where it is.

Nice small fix. The subclass survives createScoped (the base class returns this), so the vending path keeps the refresh override. A few things:

  • gcp.lifespan javadoc (StorageConfiguration.java:104-105, not in the diff): it still says "lifespan of the GCP access token". It's now just the expiry we report, re-stamped on every refresh. Can we update it and regenerate smallrye-polaris_storage.md?
  • PR description: it still says the configured value is re-read and a rotated token gets picked up. Per 36abc87 and the SmallRye check, the mapping is bound once, so neither happens. Can we drop those lines and add Fixes #2253?

*
* <p>Unlike {@link GoogleCredentials#create(AccessToken)}, which produces a credential that cannot
* refresh, this type re-stamps the expiry of the configured token on every {@link
* #refreshAccessToken() refresh}. The token value itself does not change at runtime. Without this,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes refresh stop throwing, but a real GCP OAuth token still expires on Google's side after about an hour. We'd keep reporting it as fresh, and the failure just moves to the downscope call or a GCS 401, which is harder to diagnose. Fine for emulators/Floci or tokens that never expire. Can we say here that this is only for tokens that don't really expire, and that a real token still needs a restart to rotate?

🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.

assertThat(firstExpiry).isEqualTo(start.plus(TEST_TOKEN_LIFESPAN));

// A plain GoogleCredentials.create(token) would throw here because it cannot refresh; the
// configured-token credential must re-read the token and re-stamp the expiry instead (#2253).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing is re-read anymore. Can we drop "re-read the token and" so this matches the class javadoc?

🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.

@iprithv
iprithv requested a review from dimas-b October 27, 2026 18:59
@iprithv
iprithv force-pushed the fix/gcp-static-token-refresh branch from 5d8f446 to 4acf09a Compare October 28, 2026 13:56

@vigneshio vigneshio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @iprithv. Refresh no longer throwing is the #2253 fix...

@sririshindra sririshindra left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this. The first-vend failure is real, and this does stop it: I ran GcpCredentialsStorageIntegration on main and on this PR with a fake STS. My comments cover the changelog wording, a vending regression test, and whether this property should be treated as test-only, which could simplify the fix. None of this was run against real Google.

Could you also update the title? It becomes the squash commit subject, and it reads as if the token gets renewed. Something like "Fix credential vending with a statically configured GCP access token" would be accurate.

Comment thread CHANGELOG.md Outdated
the packaged chart, so `helm test` ran no tests.
- A statically configured GCP access token (`polaris.storage.gcp.token`) no longer gets stuck with
a single expiry stamped at first use. The token was wrapped in a credential that could not
refresh, so once its one-time expiry elapsed, server-side GCS access failed until the next

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"once its one-time expiry elapsed" doesn't match what I see, or your comment on the lifespan thread that it throws "regardless of the expiry". On main, with the credential from GoogleCredentials.create(...), the first vend fails even with an unexpired token: compute calls sourceCredentials.refresh(), which throws IllegalStateException, and the caller gets a 500. I tried it through gcpCredentialsSupplier and GcpCredentialsStorageIntegration with several lifespans, and with gcsServiceAccount set. So this isn't specific to long-running servers, and a restart doesn't help. Could the entry say that? For example: "Credential vending with a statically configured GCP access token (polaris.storage.gcp.token) failed with IllegalStateException, because the token was wrapped in a credential that cannot refresh." The same wording is in the class Javadoc in ConfiguredGcpAccessTokenCredentials (lines 34-36) and the comment in StorageConfiguration (lines 164-165).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I've updated the changelog, class Javadoc, and StorageConfiguration comment to clarify that it fails immediately with an IllegalStateException because the token cannot be refreshed.


// A plain GoogleCredentials.create(token) would throw here because it cannot refresh; the
// configured-token credential re-stamps the expiry instead (#2253).
credentials.refresh();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shows the credential can refresh, but not that vending works, and that's the actual failure. I couldn't find a unit or integration test in runtime-service that vends GCS with this credential: the it profile sets SKIP_CREDENTIAL_SUBSCOPING_INDIRECTION=true, so PolarisRestCatalogFlociGcpIT never reaches compute. Could we add a regression test through the integration with a fake GcpCredentialOps? It needs no network.
This passes with this PR and fails on main (supplier comes from gcpCredentialsSupplier, and realmConfig is new RealmConfigImpl((rc, name) -> null, () -> "test-realm")):

GcpCredentialOps ops = new GcpCredentialOps() {
  @Override
  public AccessToken refreshAccessToken(DownscopedCredentials credentials) {
    return new AccessToken("vended", new Date(System.currentTimeMillis() + 3_600_000));
  }
};
StorageAccessConfig vended =
    new GcpCredentialsStorageIntegration(
            supplier.get(), NetHttpTransport::new,
            GcpStorageConfigurationInfo.builder().addAllowedLocation("gs://b/p/").build(),
            realmConfig, ops)
        .getStorageAccessConfig(
            List.of(new LocationGrant(Set.of("gs://b/p/"), Set.of(PolarisStorageActions.READ))),
            Optional.empty(), CredentialVendingContext.empty());
assertThat(vended.get(StorageAccessProperty.GCS_ACCESS_TOKEN)).isEqualTo("vended");

// until the server is restarted (#2253).
return new ConfiguredGcpAccessTokenCredentials(
gcpAccessToken().orElseThrow(),
gcpAccessTokenLifespan().orElse(DEFAULT_TOKEN_LIFESPAN),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this property be treated as test-only? When it was added in #370, the Javadoc said it "should strictly be used for testing", and the PR said "users are never expected to pass in an access token via configuration string" (#370 (comment)). That Javadoc was dropped in the Quarkus move (#469), and nothing on main says so now: the property docs, application.properties and the Helm chart all present it as a normal option. Could the docs for polaris.storage.gcp.token say this is for testing? #2253 does treat this as a bug, though, and neither it nor this PR describes a real deployment using it, so I'm not sure which is intended.

I also don't think emulator tokens benefit from this during vending. compute passes the token to DownscopedCredentials, which posts to https://sts.googleapis.com/v1/token, and I couldn't find a way to change that from Polaris. So an emulator token would pass the local refresh and then go to Google's STS. I haven't run that against Google. The cases I found where a fake token gets through are tests with a fake GcpCredentialOps, and the it profile, which skips compute.

If it's test-only, that may also answer the long-lifespan question. The credential only has to be refresh-capable because compute calls refresh() unconditionally, while DownscopedCredentials already calls refreshIfExpired() on its source. I tried changing just that line and keeping GoogleCredentials.create(...), with a fake STS. An unexpired token vends, so the first-vend failure goes away with no new class. Within the library's 3-minute margin of the stamped expiry it fails with the same IllegalStateException, so lifespans of 3 minutes or less can't vend, and a long lifespan avoids that. For a service account key under ADC nothing changes (4 token fetches for 4 vends either way). gcloud user credentials go from 4 fetches to 1, so a revoked one is noticed later.


@Override
public AccessToken refreshAccessToken() {
return newAccessToken(token, lifespan, clock);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The token never changes during refresh. I think that lifespan should logically apply to the time of credential construction.

Both the token and lifespan come from StorageConfiguration. Presumably, the admin user configuring the static token there, also sets it actual life span.

We can certainly support the no-op refresh method here, but I think it is not correct to prolong the expiry time on every refresh.

I believe a more correct configuration would be to take a fixed expiry Instant via StorageConfiguration and use it on every refresh call.

We could add Optional<Instant> gcpAccessTokenExpiry() to StorageConfiguration. If unset, use a hard-coded value far in the future.

WDYT?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, extending the expiry of a token that never changes was incorrect. I’ve changed it to use a fixed expiry, calculated once at construction and returned unchanged on every refresh. I’ve also renamed the class to FixedGcpAccessTokenCredentials.

I’ve kept using the existing gcp.lifespan to calculate the expiry instead of adding a new property. Do you still want an explicit Optional<Instant> gcpAccessTokenExpiry() with a far-future default, or is setting the expiry from construction time enough?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding a new config property should not cause any issues 😉 more derailed comment below.

* GCS call would then fail at the provider instead. Rotating a real configured token still requires
* a restart.
*/
final class ConfiguredGcpAccessTokenCredentials extends GoogleCredentials {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: FixedGcpAccessTokenCredentials?

@iprithv

iprithv commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review @sririshindra, Good question, and I'm not sure either. #2253 treats this as a real bug, and neither it nor this PR mentions any production deployment using it. So I'd prefer not to label the property as test-only in this PR.

Happy to raise this on the dev list or in a follow-up if we want to clarify that polaris.storage.gcp.token is intended mainly for testing.

CC: @dimas-b

// token in a credential that supports refresh by returning it with a fixed expiry,
// computed once here from the configured lifespan.
Instant expiry =
clock.instant().plus(gcpAccessTokenLifespan().orElse(DEFAULT_TOKEN_LIFESPAN));

@dimas-b dimas-b Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic matches old code, but I think the old code was actually incorrect here 😅

The user configures a life span, but the user cannot now the base time for it. I cannot imagine how I could advise users about setting this property 😅

I believe this awkwardness existed from day 0 of Polaris, but it does not mean we cannot fix it 😉

Let's add a new config with an expiry Instant. If unset, it is fine to fall back to the old logic. At least users will be able to set a meaningful expiry time, if they care to read the javadoc.

@dimas-b dimas-b left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update, @iprithv !

The changes are non-intrusive, as far as I can tell 👍

I'm enabling merge once CI is green.

@github-project-automation github-project-automation Bot moved this from PRs In Progress to Ready to merge in Basic Kanban Board Nov 6, 2026
@dimas-b

dimas-b commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

polaris.storage.gcp.token is a very old config method. I believe we can expect users to know that it is settable via Quakus configuration mechanisms and therefore generally cannot be refreshed in runtime.

We can certainly discuss and implement further improvements under #2253.

This PR looks valuable by itself anyway, IMHO.

@dimas-b
dimas-b enabled auto-merge (squash) November 6, 2026 19:40
@dimas-b
dimas-b merged commit 4afcc5f into apache:main Nov 6, 2026
26 checks passed
@github-project-automation github-project-automation Bot moved this from Ready to merge to Done in Basic Kanban Board Nov 6, 2026
sririshindra added a commit to sririshindra/polaris that referenced this pull request Nov 7, 2026
A storage name of token, lifespan or expiry was silently skipped, so a
catalog using it later failed with a misleading "not configured" error.
These names are properties of the default GCP credentials, and expiry was
added by apache#5806. Configuring one of them now stops the server from starting.
The storage integration provider is created at startup for this, as
S3CredentialVendingMechanisms already is.

A storage name used while GCS_PRINCIPAL_ATTRIBUTION_ENABLED is true now
fails with an IllegalStateException (500), like the other attribution
misconfigurations, since server settings cause it.

The tests now cover Polaris's own behavior only, in line with the AWS and
Azure named credentials: the token endpoint test, the retry test and the
cache key toString and impersonation tests are removed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants