You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@polaris.apache.org by "obelix74 (via GitHub)" <gi...@apache.org> on 2026/06/11 15:05:19 UTC

[PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

obelix74 opened a new pull request, #4707:
URL: https://github.com/apache/polaris/pull/4707

   For issue https://github.com/apache/polaris/issues/4706.
   
   GCP counterpart of AWS STS session tags. GCP downscoped credentials have no session-tag mechanism, and x-goog-custom-audit-* request headers only reach GCS audit logs if the client forwards them (arbitrary Iceberg clients do not), so GCS Data Access logs cannot today be tied to the requesting Polaris principal.
   
   This vends the attribution in the one channel that survives any client: the identity of the credential itself. When configured, credential vending chains catalog-signed JWT (sub=<realm>/<principal>) -> GCP STS token exchange (via IdentityPoolCredentials programmatic supplier) -> tenant service-account impersonation -> existing CAB downscoping, so every GCS Data Access audit entry carries the principal in serviceAccountDelegationInfo.principalSubject.
   
   New FeatureConfiguration flags:
   - GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE
   - GCS_PRINCIPAL_ATTRIBUTION_TOKEN_ISSUER
   - GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_FILE
   - GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_ID (kid for JWKS rotation)
   
   There is no separate on/off flag: attribution activates once the audience, issuer, and signing key file are all set, and additionally requires a gcpServiceAccount on the storage config. When unconfigured, GCP vending is unchanged.
   
   GcpStorageCredentialCacheKey gains a principalName data field, populated only when attribution is configured, so per-principal attributed tokens are never shared across principals (and cross-principal cache reuse is preserved when attribution is off). CredentialVendingContext already carries principalName, so no new plumbing into the catalog core is required.
   
   New classes: GcpAttributionSubjectBuilder (builds <realm>/<principal> within GCP's 127-char google.subject limit) and GcpFederatedCredentialsExchanger (mints the RS256 JWT via com.auth0:java-jwt, already in the version catalog; performs the STS exchange via google-auth IdentityPoolCredentials, so no new HTTP machinery; caches the parsed signing key JVM-wide).
   
   Tests cover the subject budget/sanitization, JWT claims + kid, signing-key caching, the IdentityPoolCredentials configuration, and per-principal cache-key identity. polaris-core compiles, tests pass, spotless clean.
   
   ## Checklist
   - [x] ๐Ÿ›ก๏ธ Don't disclose security issues! (contact security@apache.org)
   - [x] ๐Ÿ”— Clearly explained why the changes are needed, or linked related issues: Fixes #
   - [x] ๐Ÿงช Added/updated tests with good coverage, or manually tested (and explained how)
   - [x] ๐Ÿ’ก Added comments for complex logic
   - [ ] ๐Ÿงพ Updated `CHANGELOG.md` (if needed)
   - [ ] ๐Ÿ“š Updated documentation in `site/content/in-dev/unreleased` (if needed)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "obelix74 (via GitHub)" <gi...@apache.org>.
obelix74 commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3453687340


##########
polaris-core/src/main/java/org/apache/polaris/core/config/FeatureConfiguration.java:
##########
@@ -209,6 +209,64 @@ public static void enforceFeatureEnabledOrThrow(
               .defaultValue(List.<String>of())
               .buildFeatureConfiguration();
 
+  // ---------------------------------------------------------------------------
+  // GCS principal attribution via Workload Identity Federation
+  //
+  // GCP downscoped credentials have no session-tag mechanism (unlike AWS STS), and custom audit
+  // headers only reach GCS audit logs if the client forwards them. To attribute GCS data access
+  // to the Polaris principal for ANY client, credential vending can chain
+  // catalog-signed JWT -> STS token exchange -> per-catalog service-account impersonation, so the
+  // principal appears in serviceAccountDelegationInfo of every GCS Data Access audit log entry.
+  //
+  // Attribution activates automatically once the audience, issuer, and signing key file are all
+  // set (no on/off flag); it additionally requires a gcpServiceAccount on the storage config.
+  // ---------------------------------------------------------------------------
+
+  public static final FeatureConfiguration<String> GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE =

Review Comment:
   Addressed it in commit #4707 7738c9c32



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "adnanhemani (via GitHub)" <gi...@apache.org>.
adnanhemani commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3454715787


##########
polaris-core/src/test/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegrationTest.java:
##########
@@ -502,4 +515,147 @@ private static String expressionAt(JsonNode parsedRules, int ruleIndex) {
         .path("expression")
         .asText();
   }
+
+  private static RealmConfig configWith(Map<String, String> values) {
+    RealmConfigurationSource source = (rc, name) -> values.get(name);
+    return new RealmConfigImpl(source, () -> "test-realm");
+  }
+
+  @Test
+  void resolveAttributionParamsDisabledByDefault() {
+    assertThat(GcpCredentialsStorageIntegration.resolveAttributionParams(EMPTY_REALM_CONFIG))
+        .isEmpty();
+  }
+
+  @Test
+  void resolveAttributionParamsReturnsParamsWhenFullyConfigured() {
+    RealmConfig config =
+        configWith(
+            Map.of(
+                "GCS_PRINCIPAL_ATTRIBUTION_ENABLED", "true",
+                "GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE",
+                    "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/p/providers/pr",
+                "GCS_PRINCIPAL_ATTRIBUTION_TOKEN_ISSUER", "https://issuer.example.com",
+                "GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_FILE", "/tmp/key.pem"));
+    Optional<GcpAttributionParams> result =
+        GcpCredentialsStorageIntegration.resolveAttributionParams(config);
+    assertThat(result).isPresent();
+    assertThat(result.get().wifAudience())
+        .isEqualTo(
+            "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/p/providers/pr");
+    assertThat(result.get().tokenIssuer()).isEqualTo("https://issuer.example.com");
+    assertThat(result.get().signingKeyFile()).isEqualTo("/tmp/key.pem");
+  }
+
+  @Test
+  void resolveAttributionParamsThrowsWhenEnabledButAudienceMissing() {
+    RealmConfig config =
+        configWith(
+            Map.of(
+                "GCS_PRINCIPAL_ATTRIBUTION_ENABLED", "true",
+                "GCS_PRINCIPAL_ATTRIBUTION_TOKEN_ISSUER", "https://issuer.example.com",
+                "GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_FILE", "/tmp/key.pem"));
+    assertThatThrownBy(() -> GcpCredentialsStorageIntegration.resolveAttributionParams(config))
+        .isInstanceOf(IllegalStateException.class)
+        .hasMessageContaining("GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE");
+  }
+
+  @Test
+  void resolveAttributionParamsThrowsWhenMultipleRequiredParamsMissing() {
+    RealmConfig config = configWith(Map.of("GCS_PRINCIPAL_ATTRIBUTION_ENABLED", "true"));
+    assertThatThrownBy(() -> GcpCredentialsStorageIntegration.resolveAttributionParams(config))
+        .isInstanceOf(IllegalStateException.class)
+        .hasMessageContaining("GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE")
+        .hasMessageContaining("GCS_PRINCIPAL_ATTRIBUTION_TOKEN_ISSUER")
+        .hasMessageContaining("GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_FILE");
+  }
+
+  @Test
+  void testWifAttributionFlowUsesFederatedCredentials(@TempDir Path tempDir) throws Exception {
+    String serviceAccount = "test-sa@project.iam.gserviceaccount.com";
+    GcpStorageConfigurationInfo config =
+        GcpStorageConfigurationInfo.builder()
+            .addAllAllowedLocations(List.of("gs://bucket/path"))
+            .gcpServiceAccount(serviceAccount)
+            .build();
+
+    AtomicReference<GoogleCredentials> capturedSource = new AtomicReference<>();
+
+    IamCredentialsClient mockIamClient = Mockito.mock(IamCredentialsClient.class);
+    GenerateAccessTokenResponse mockResponse =
+        GenerateAccessTokenResponse.newBuilder()
+            .setAccessToken("wif-impersonated-token")
+            .setExpireTime(
+                Timestamp.newBuilder().setSeconds(System.currentTimeMillis() / 1000 + 3600).build())
+            .build();
+    Mockito.when(mockIamClient.generateAccessToken(Mockito.any(GenerateAccessTokenRequest.class)))
+        .thenReturn(mockResponse);
+
+    GoogleCredentials mockCreds = Mockito.mock(GoogleCredentials.class);
+    Mockito.when(mockCreds.createScoped(Mockito.any(String.class))).thenReturn(mockCreds);
+
+    GcpCredentialOps testOps =
+        new GcpCredentialOps() {
+          @Override
+          public IamCredentialsClient createIamCredentialsClient(GoogleCredentials credentials) {
+            capturedSource.set(credentials);
+            return mockIamClient;
+          }
+
+          @Override
+          public AccessToken refreshAccessToken(DownscopedCredentials credentials) {
+            return new AccessToken("downscoped-token", new Date());
+          }
+        };
+
+    KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
+    gen.initialize(2048);
+    KeyPair keyPair = gen.generateKeyPair();
+    String pem =
+        "-----BEGIN PRIVATE KEY-----\n"
+            + Base64.getMimeEncoder(64, "\n".getBytes(UTF_8))
+                .encodeToString(keyPair.getPrivate().getEncoded())
+            + "\n-----END PRIVATE KEY-----\n";
+    Path keyFile = tempDir.resolve("attribution-key.pem");
+    Files.writeString(keyFile, pem);
+
+    GcpAttributionParams params =
+        GcpAttributionParams.of(
+            "https://issuer.example.com",
+            "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/p/providers/pr",
+            keyFile.toString(),
+            "key-1");
+
+    GcpCredentialsStorageIntegration integration =
+        new GcpCredentialsStorageIntegration(
+            mockCreds,
+            ServiceOptions.getFromServiceLoader(HttpTransportFactory.class, NetHttpTransport::new),
+            null,
+            config,
+            EMPTY_REALM_CONFIG,
+            testOps,
+            Optional.of(params));
+
+    integration.getStorageAccessConfig(
+        toGrants(
+            Set.of("gs://bucket/path"), Set.of("gs://bucket/path"), Set.of("gs://bucket/path")),
+        Optional.empty(),
+        CredentialVendingContext.builder()
+            .realm(Optional.of("realm1"))
+            .principalName(Optional.of("principal1"))
+            .build());
+
+    Mockito.verify(mockIamClient)
+        .generateAccessToken(
+            Mockito.argThat(
+                request ->
+                    request
+                        .getName()
+                        .equals(
+                            GcpCredentialsStorageIntegration.SERVICE_ACCOUNT_PREFIX

Review Comment:
   This looks better now, thanks!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "obelix74 (via GitHub)" <gi...@apache.org>.
obelix74 commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3409579820


##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpFederatedCredentialsExchanger.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.core.storage.gcp;
+
+import com.auth0.jwt.JWT;
+import com.auth0.jwt.JWTCreator;
+import com.auth0.jwt.algorithms.Algorithm;
+import com.google.auth.http.HttpTransportFactory;
+import com.google.auth.oauth2.GoogleCredentials;
+import com.google.auth.oauth2.IdentityPoolCredentials;
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.security.KeyFactory;
+import java.security.interfaces.RSAPrivateKey;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.Base64;
+import java.util.Date;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Produces a GCP federated {@link GoogleCredentials} whose identity carries {@code
+ * <realm>/<principal>}, so that GCS Data Access audit logs attribute access to the requesting
+ * Polaris principal. This is the GCP counterpart of AWS STS session tags.
+ *
+ * <p>The federated credential is an {@link IdentityPoolCredentials} backed by a programmatic
+ * subject-token supplier: on each token refresh google-auth invokes the supplier, which mints a
+ * short-lived RS256 JWT ({@code sub = <realm>/<principal>}, {@code realm} claim), and exchanges it
+ * at the Workload Identity Pool provider's STS endpoint. The provider maps {@code google.subject =
+ * assertion.sub} and {@code attribute.realm = assertion.realm}; per-realm {@code attribute.realm}
+ * IAM bindings then enforce that a realm-A identity can only impersonate realm-A's service account.
+ * The returned credential is intended to be used as the source for tenant service-account
+ * impersonation (see {@link GcpCredentialsStorageIntegration}).
+ *
+ * <p>Network note: this performs an STS token exchange against {@code sts.googleapis.com} in
+ * addition to the existing {@code iamcredentials.googleapis.com} and {@code storage.googleapis.com}
+ * traffic.
+ */
+public class GcpFederatedCredentialsExchanger {
+
+  static final String STS_TOKEN_URL = "https://sts.googleapis.com/v1/token";
+  static final String SUBJECT_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:jwt";
+  static final String CLOUD_PLATFORM_SCOPE = "https://www.googleapis.com/auth/cloud-platform";
+
+  /** Attribution JWTs are single-purpose and short-lived. */
+  static final Duration JWT_LIFETIME = Duration.ofMinutes(5);
+
+  /**
+   * JVM-wide cache of parsed signing keys, keyed by file path. The key file is a stable pod-mounted
+   * secret; parsing it (disk read + {@link KeyFactory}) once per path amortizes across vends rather
+   * than re-reading on every credential-cache miss. Key rotation is delivered by a process restart
+   * (the secret is mounted at startup), which clears this cache.
+   */
+  private static final ConcurrentHashMap<Path, RSAPrivateKey> SIGNING_KEY_CACHE =
+      new ConcurrentHashMap<>();
+
+  private final String issuer;
+  private final String wifAudience;
+  private final Path signingKeyPath;
+  private final String signingKeyId;
+  private final HttpTransportFactory transportFactory;
+
+  public GcpFederatedCredentialsExchanger(
+      String issuer,
+      String wifAudience,
+      Path signingKeyPath,
+      String signingKeyId,
+      HttpTransportFactory transportFactory) {
+    this.issuer = issuer;
+    this.wifAudience = wifAudience;
+    this.signingKeyPath = signingKeyPath;
+    this.signingKeyId = signingKeyId;
+    this.transportFactory = transportFactory;
+  }
+
+  /**
+   * Builds a federated credential whose subject is {@code <realm>/<principal>}.
+   *
+   * @param subject the attribution subject, {@code <realm>/<principal>} (see {@link
+   *     GcpAttributionSubjectBuilder})
+   * @param realm the realm identifier, emitted as the {@code realm} claim for {@code
+   *     attribute.realm} mapping
+   * @return federated credentials suitable as the source for tenant-SA impersonation
+   */
+  public GoogleCredentials federatedCredentials(String subject, String realm) {
+    return IdentityPoolCredentials.newBuilder()
+        .setHttpTransportFactory(transportFactory)
+        .setAudience(wifAudience)
+        .setSubjectTokenType(SUBJECT_TOKEN_TYPE)
+        .setTokenUrl(STS_TOKEN_URL)
+        .setScopes(List.of(CLOUD_PLATFORM_SCOPE))
+        .setSubjectTokenSupplier(context -> mintAttributionJwt(subject, realm))
+        .build();
+  }
+
+  @VisibleForTesting
+  String mintAttributionJwt(String subject, String realm) throws IOException {
+    Instant now = Instant.now();
+    JWTCreator.Builder builder =
+        JWT.create()
+            .withIssuer(issuer)
+            .withSubject(subject)
+            .withAudience(wifAudience)
+            .withClaim("realm", realm)
+            .withIssuedAt(Date.from(now))
+            .withExpiresAt(Date.from(now.plus(JWT_LIFETIME)))
+            .withJWTId(UUID.randomUUID().toString());
+    // Set the kid header so the WIF provider can pick the right public key from its JWKS during
+    // rotation (when the JWKS holds both the old and new keys). Omitted only for a single-key JWKS.
+    if (signingKeyId != null && !signingKeyId.isEmpty()) {
+      builder.withKeyId(signingKeyId);
+    }
+    return builder.sign(Algorithm.RSA256(null, loadSigningKey()));
+  }
+
+  private RSAPrivateKey loadSigningKey() throws IOException {
+    RSAPrivateKey cached = SIGNING_KEY_CACHE.get(signingKeyPath);
+    if (cached != null) {
+      return cached;
+    }
+    RSAPrivateKey key = readPkcs8PrivateKey(signingKeyPath);
+    SIGNING_KEY_CACHE.putIfAbsent(signingKeyPath, key);
+    return key;
+  }
+
+  /** Reads an RSA private key from a PKCS#8 PEM file. */
+  @VisibleForTesting
+  static RSAPrivateKey readPkcs8PrivateKey(Path pemPath) throws IOException {
+    String pem = Files.readString(pemPath);
+    String base64 =
+        pem.replaceAll("-----BEGIN [A-Z ]+-----", "")

Review Comment:
   `PemUtils` lives in `runtime/service` (`org.apache.polaris.service.auth.internal.broker`), which is a downstream module that depends on `polaris-core`. Using it from `polaris-core` would invert that dependency and create a cycle. Moving it to `polaris-core` could work in principle, but it is also package-private and has broader scope (key-pair generation, public-key reading, file writing) that does not belong in core storage. The `readPkcs8PrivateKey` here is a small, targeted helper with no deps beyond the JDK โ€” I think keeping it local is the cleaner choice. Happy to file a follow-up to consolidate PEM utilities into a shared module if that is desirable.



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpFederatedCredentialsExchanger.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.core.storage.gcp;
+
+import com.auth0.jwt.JWT;
+import com.auth0.jwt.JWTCreator;
+import com.auth0.jwt.algorithms.Algorithm;
+import com.google.auth.http.HttpTransportFactory;
+import com.google.auth.oauth2.GoogleCredentials;
+import com.google.auth.oauth2.IdentityPoolCredentials;
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.security.KeyFactory;
+import java.security.interfaces.RSAPrivateKey;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.Base64;
+import java.util.Date;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Produces a GCP federated {@link GoogleCredentials} whose identity carries {@code
+ * <realm>/<principal>}, so that GCS Data Access audit logs attribute access to the requesting
+ * Polaris principal. This is the GCP counterpart of AWS STS session tags.
+ *
+ * <p>The federated credential is an {@link IdentityPoolCredentials} backed by a programmatic
+ * subject-token supplier: on each token refresh google-auth invokes the supplier, which mints a
+ * short-lived RS256 JWT ({@code sub = <realm>/<principal>}, {@code realm} claim), and exchanges it
+ * at the Workload Identity Pool provider's STS endpoint. The provider maps {@code google.subject =
+ * assertion.sub} and {@code attribute.realm = assertion.realm}; per-realm {@code attribute.realm}
+ * IAM bindings then enforce that a realm-A identity can only impersonate realm-A's service account.
+ * The returned credential is intended to be used as the source for tenant service-account
+ * impersonation (see {@link GcpCredentialsStorageIntegration}).
+ *
+ * <p>Network note: this performs an STS token exchange against {@code sts.googleapis.com} in
+ * addition to the existing {@code iamcredentials.googleapis.com} and {@code storage.googleapis.com}
+ * traffic.
+ */
+public class GcpFederatedCredentialsExchanger {
+
+  static final String STS_TOKEN_URL = "https://sts.googleapis.com/v1/token";
+  static final String SUBJECT_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:jwt";
+  static final String CLOUD_PLATFORM_SCOPE = "https://www.googleapis.com/auth/cloud-platform";
+
+  /** Attribution JWTs are single-purpose and short-lived. */
+  static final Duration JWT_LIFETIME = Duration.ofMinutes(5);
+
+  /**
+   * JVM-wide cache of parsed signing keys, keyed by file path. The key file is a stable pod-mounted
+   * secret; parsing it (disk read + {@link KeyFactory}) once per path amortizes across vends rather
+   * than re-reading on every credential-cache miss. Key rotation is delivered by a process restart
+   * (the secret is mounted at startup), which clears this cache.
+   */
+  private static final ConcurrentHashMap<Path, RSAPrivateKey> SIGNING_KEY_CACHE =
+      new ConcurrentHashMap<>();
+
+  private final String issuer;
+  private final String wifAudience;
+  private final Path signingKeyPath;
+  private final String signingKeyId;
+  private final HttpTransportFactory transportFactory;
+
+  public GcpFederatedCredentialsExchanger(
+      String issuer,
+      String wifAudience,
+      Path signingKeyPath,
+      String signingKeyId,
+      HttpTransportFactory transportFactory) {
+    this.issuer = issuer;
+    this.wifAudience = wifAudience;
+    this.signingKeyPath = signingKeyPath;
+    this.signingKeyId = signingKeyId;
+    this.transportFactory = transportFactory;
+  }
+
+  /**
+   * Builds a federated credential whose subject is {@code <realm>/<principal>}.
+   *
+   * @param subject the attribution subject, {@code <realm>/<principal>} (see {@link
+   *     GcpAttributionSubjectBuilder})
+   * @param realm the realm identifier, emitted as the {@code realm} claim for {@code
+   *     attribute.realm} mapping
+   * @return federated credentials suitable as the source for tenant-SA impersonation
+   */
+  public GoogleCredentials federatedCredentials(String subject, String realm) {
+    return IdentityPoolCredentials.newBuilder()
+        .setHttpTransportFactory(transportFactory)
+        .setAudience(wifAudience)
+        .setSubjectTokenType(SUBJECT_TOKEN_TYPE)
+        .setTokenUrl(STS_TOKEN_URL)
+        .setScopes(List.of(CLOUD_PLATFORM_SCOPE))
+        .setSubjectTokenSupplier(context -> mintAttributionJwt(subject, realm))
+        .build();
+  }
+
+  @VisibleForTesting
+  String mintAttributionJwt(String subject, String realm) throws IOException {
+    Instant now = Instant.now();
+    JWTCreator.Builder builder =
+        JWT.create()
+            .withIssuer(issuer)
+            .withSubject(subject)
+            .withAudience(wifAudience)
+            .withClaim("realm", realm)
+            .withIssuedAt(Date.from(now))
+            .withExpiresAt(Date.from(now.plus(JWT_LIFETIME)))
+            .withJWTId(UUID.randomUUID().toString());
+    // Set the kid header so the WIF provider can pick the right public key from its JWKS during
+    // rotation (when the JWKS holds both the old and new keys). Omitted only for a single-key JWKS.
+    if (signingKeyId != null && !signingKeyId.isEmpty()) {
+      builder.withKeyId(signingKeyId);
+    }
+    return builder.sign(Algorithm.RSA256(null, loadSigningKey()));
+  }
+
+  private RSAPrivateKey loadSigningKey() throws IOException {
+    RSAPrivateKey cached = SIGNING_KEY_CACHE.get(signingKeyPath);
+    if (cached != null) {
+      return cached;
+    }
+    RSAPrivateKey key = readPkcs8PrivateKey(signingKeyPath);
+    SIGNING_KEY_CACHE.putIfAbsent(signingKeyPath, key);

Review Comment:
   Good call โ€” switched to `computeIfAbsent`, which atomically ensures only one thread does the file I/O per path. Used `UncheckedIOException` to wrap the checked `IOException` inside the lambda and re-throws it at the call site.



##########
runtime/admin/distribution/LICENSE:
##########
@@ -399,6 +399,36 @@ License: Apache License 2.0 - https://github.com/hyperxpro/Brotli4j/blob/v1.16.0
 
 --------------------------------------------------------------------------------
 
+This product bundles Auth0 Java JWT.
+
+* Maven group:artifact IDs: com.auth0:java-jwt

Review Comment:
   `java-jwt` was already a transitive dependency in the admin distribution before this PR โ€” `runtime/service` has used it for internal JWT token signing since before this branch. Its absence from the LICENSE was a pre-existing omission rather than something introduced here. Since this PR adds `polaris-core` as a direct consumer it was a natural point to catch and correct that gap. The entry is needed: `java-jwt` is bundled in the admin distribution jar regardless of whether the dependency is direct or transitive.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "sungwy (via GitHub)" <gi...@apache.org>.
sungwy commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3453291702


##########
polaris-core/src/main/java/org/apache/polaris/core/config/FeatureConfiguration.java:
##########
@@ -209,6 +209,64 @@ public static void enforceFeatureEnabledOrThrow(
               .defaultValue(List.<String>of())
               .buildFeatureConfiguration();
 
+  // ---------------------------------------------------------------------------
+  // GCS principal attribution via Workload Identity Federation
+  //
+  // GCP downscoped credentials have no session-tag mechanism (unlike AWS STS), and custom audit
+  // headers only reach GCS audit logs if the client forwards them. To attribute GCS data access
+  // to the Polaris principal for ANY client, credential vending can chain
+  // catalog-signed JWT -> STS token exchange -> per-catalog service-account impersonation, so the
+  // principal appears in serviceAccountDelegationInfo of every GCS Data Access audit log entry.
+  //
+  // Attribution activates automatically once the audience, issuer, and signing key file are all
+  // set (no on/off flag); it additionally requires a gcpServiceAccount on the storage config.
+  // ---------------------------------------------------------------------------
+
+  public static final FeatureConfiguration<String> GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE =

Review Comment:
   Thanks all for chiming in. I agree it's fine to keep the config at the realm level.
   
   On validation timing: would moving the presence/value checks into a `ProductionReadinessCheck` be a good way to verify them at startup? There's precedent for per-realm checks there: [checkTokenBrokers](https://github.com/apache/polaris/blob/e3f26cf69f6ee189328f1b7adb90ec83cbbb26d9/runtime/service/src/main/java/org/apache/polaris/service/config/ProductionReadinessChecks.java#L143) iterates the realms and validates each realm's config at startup. We could do the value-presence checks there, and keep the more involved logical validation at request time as we do today. I think this is also fine to add as a follow up PR.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "obelix74 (via GitHub)" <gi...@apache.org>.
obelix74 commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3453687340


##########
polaris-core/src/main/java/org/apache/polaris/core/config/FeatureConfiguration.java:
##########
@@ -209,6 +209,64 @@ public static void enforceFeatureEnabledOrThrow(
               .defaultValue(List.<String>of())
               .buildFeatureConfiguration();
 
+  // ---------------------------------------------------------------------------
+  // GCS principal attribution via Workload Identity Federation
+  //
+  // GCP downscoped credentials have no session-tag mechanism (unlike AWS STS), and custom audit
+  // headers only reach GCS audit logs if the client forwards them. To attribute GCS data access
+  // to the Polaris principal for ANY client, credential vending can chain
+  // catalog-signed JWT -> STS token exchange -> per-catalog service-account impersonation, so the
+  // principal appears in serviceAccountDelegationInfo of every GCS Data Access audit log entry.
+  //
+  // Attribution activates automatically once the audience, issuer, and signing key file are all
+  // set (no on/off flag); it additionally requires a gcpServiceAccount on the storage config.
+  // ---------------------------------------------------------------------------
+
+  public static final FeatureConfiguration<String> GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE =

Review Comment:
   Addressed it in commit #7738c9c32



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3399843829


##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegration.java:
##########
@@ -246,6 +273,42 @@ static StorageAccessConfig compute(GcpStorageCredentialCacheKey key) {
     return accessConfig.build();
   }
 
+  /**
+   * Returns the credential to be used as the source for downscoping.
+   *
+   * <p>When GCS principal attribution is configured and a principal is present (so the cache key
+   * carries it), the impersonation source is a federated identity whose subject is {@code
+   * <realm>/<principal>}, which surfaces the principal in {@code serviceAccountDelegationInfo} of
+   * GCS Data Access audit logs. Otherwise this is the standard path: impersonate the configured
+   * service account from the ambient source credentials, or use those credentials directly.
+   */
+  private static GoogleCredentials baseCredentialsForVending(
+      GcpStorageCredentialCacheKey key,
+      GcpStorageConfigurationInfo storageConfig,
+      GoogleCredentials sourceCredentials,
+      GcpCredentialOps credentialOps) {
+    RealmConfig realmConfig = key.realmConfig();
+    String serviceAccount = storageConfig.getGcpServiceAccount();
+    if (serviceAccount != null
+        && !key.principalName().isEmpty()
+        && principalAttributionConfigured(realmConfig)) {
+      String subject =
+          GcpAttributionSubjectBuilder.buildSubject(key.realmId(), key.principalName());
+      GcpFederatedCredentialsExchanger exchanger =
+          new GcpFederatedCredentialsExchanger(
+              realmConfig.getConfig(FeatureConfiguration.GCS_PRINCIPAL_ATTRIBUTION_TOKEN_ISSUER),

Review Comment:
   This will probably work correctly given that `realm ID` is part of the cache key, but conceptually, I think it would be nicer to evaluate these parameters at cache key build time (i.e. around line 185) and just use the values here. 
   
   The cache key is meant to be a "rich" object, so it should be fine to add new data as another (immutable) sub-object, that if we want to avoid having too many properties at the same level (just an aesthetic concern).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3417624710


##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpFederatedCredentialsExchanger.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.core.storage.gcp;
+
+import com.auth0.jwt.JWT;
+import com.auth0.jwt.JWTCreator;
+import com.auth0.jwt.algorithms.Algorithm;
+import com.google.auth.http.HttpTransportFactory;
+import com.google.auth.oauth2.GoogleCredentials;
+import com.google.auth.oauth2.IdentityPoolCredentials;
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.security.KeyFactory;
+import java.security.interfaces.RSAPrivateKey;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.Base64;
+import java.util.Date;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Produces a GCP federated {@link GoogleCredentials} whose identity carries {@code
+ * <realm>/<principal>}, so that GCS Data Access audit logs attribute access to the requesting
+ * Polaris principal. This is the GCP counterpart of AWS STS session tags.
+ *
+ * <p>The federated credential is an {@link IdentityPoolCredentials} backed by a programmatic
+ * subject-token supplier: on each token refresh google-auth invokes the supplier, which mints a
+ * short-lived RS256 JWT ({@code sub = <realm>/<principal>}, {@code realm} claim), and exchanges it
+ * at the Workload Identity Pool provider's STS endpoint. The provider maps {@code google.subject =
+ * assertion.sub} and {@code attribute.realm = assertion.realm}; per-realm {@code attribute.realm}
+ * IAM bindings then enforce that a realm-A identity can only impersonate realm-A's service account.
+ * The returned credential is intended to be used as the source for tenant service-account
+ * impersonation (see {@link GcpCredentialsStorageIntegration}).
+ *
+ * <p>Network note: this performs an STS token exchange against {@code sts.googleapis.com} in
+ * addition to the existing {@code iamcredentials.googleapis.com} and {@code storage.googleapis.com}
+ * traffic.
+ */
+public class GcpFederatedCredentialsExchanger {
+
+  static final String STS_TOKEN_URL = "https://sts.googleapis.com/v1/token";
+  static final String SUBJECT_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:jwt";
+  static final String CLOUD_PLATFORM_SCOPE = "https://www.googleapis.com/auth/cloud-platform";
+
+  /** Attribution JWTs are single-purpose and short-lived. */
+  static final Duration JWT_LIFETIME = Duration.ofMinutes(5);
+
+  /**
+   * JVM-wide cache of parsed signing keys, keyed by file path. The key file is a stable pod-mounted
+   * secret; parsing it (disk read + {@link KeyFactory}) once per path amortizes across vends rather
+   * than re-reading on every credential-cache miss. Key rotation is delivered by a process restart
+   * (the secret is mounted at startup), which clears this cache.
+   */
+  private static final ConcurrentHashMap<Path, RSAPrivateKey> SIGNING_KEY_CACHE =
+      new ConcurrentHashMap<>();
+
+  private final String issuer;
+  private final String wifAudience;
+  private final Path signingKeyPath;
+  private final String signingKeyId;
+  private final HttpTransportFactory transportFactory;
+
+  public GcpFederatedCredentialsExchanger(
+      String issuer,
+      String wifAudience,
+      Path signingKeyPath,
+      String signingKeyId,
+      HttpTransportFactory transportFactory) {
+    this.issuer = issuer;
+    this.wifAudience = wifAudience;
+    this.signingKeyPath = signingKeyPath;
+    this.signingKeyId = signingKeyId;
+    this.transportFactory = transportFactory;
+  }
+
+  /**
+   * Builds a federated credential whose subject is {@code <realm>/<principal>}.
+   *
+   * @param subject the attribution subject, {@code <realm>/<principal>} (see {@link
+   *     GcpAttributionSubjectBuilder})
+   * @param realm the realm identifier, emitted as the {@code realm} claim for {@code
+   *     attribute.realm} mapping
+   * @return federated credentials suitable as the source for tenant-SA impersonation
+   */
+  public GoogleCredentials federatedCredentials(String subject, String realm) {
+    return IdentityPoolCredentials.newBuilder()
+        .setHttpTransportFactory(transportFactory)
+        .setAudience(wifAudience)
+        .setSubjectTokenType(SUBJECT_TOKEN_TYPE)
+        .setTokenUrl(STS_TOKEN_URL)
+        .setScopes(List.of(CLOUD_PLATFORM_SCOPE))
+        .setSubjectTokenSupplier(context -> mintAttributionJwt(subject, realm))
+        .build();
+  }
+
+  @VisibleForTesting
+  String mintAttributionJwt(String subject, String realm) throws IOException {
+    Instant now = Instant.now();
+    JWTCreator.Builder builder =
+        JWT.create()
+            .withIssuer(issuer)
+            .withSubject(subject)
+            .withAudience(wifAudience)
+            .withClaim("realm", realm)
+            .withIssuedAt(Date.from(now))
+            .withExpiresAt(Date.from(now.plus(JWT_LIFETIME)))
+            .withJWTId(UUID.randomUUID().toString());
+    // Set the kid header so the WIF provider can pick the right public key from its JWKS during
+    // rotation (when the JWKS holds both the old and new keys). Omitted only for a single-key JWKS.
+    if (signingKeyId != null && !signingKeyId.isEmpty()) {
+      builder.withKeyId(signingKeyId);
+    }
+    return builder.sign(Algorithm.RSA256(null, loadSigningKey()));
+  }
+
+  private RSAPrivateKey loadSigningKey() throws IOException {
+    RSAPrivateKey cached = SIGNING_KEY_CACHE.get(signingKeyPath);
+    if (cached != null) {
+      return cached;
+    }
+    RSAPrivateKey key = readPkcs8PrivateKey(signingKeyPath);
+    SIGNING_KEY_CACHE.putIfAbsent(signingKeyPath, key);
+    return key;
+  }
+
+  /** Reads an RSA private key from a PKCS#8 PEM file. */
+  @VisibleForTesting
+  static RSAPrivateKey readPkcs8PrivateKey(Path pemPath) throws IOException {
+    String pem = Files.readString(pemPath);
+    String base64 =
+        pem.replaceAll("-----BEGIN [A-Z ]+-----", "")

Review Comment:
   Thanks for the recap, @obelix74 ! Follow-up SGTM :+1: 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3431665794


##########
polaris-core/src/main/java/org/apache/polaris/core/config/FeatureConfiguration.java:
##########
@@ -209,6 +209,64 @@ public static void enforceFeatureEnabledOrThrow(
               .defaultValue(List.<String>of())
               .buildFeatureConfiguration();
 
+  // ---------------------------------------------------------------------------
+  // GCS principal attribution via Workload Identity Federation
+  //
+  // GCP downscoped credentials have no session-tag mechanism (unlike AWS STS), and custom audit
+  // headers only reach GCS audit logs if the client forwards them. To attribute GCS data access
+  // to the Polaris principal for ANY client, credential vending can chain
+  // catalog-signed JWT -> STS token exchange -> per-catalog service-account impersonation, so the
+  // principal appears in serviceAccountDelegationInfo of every GCS Data Access audit log entry.
+  //
+  // Attribution activates automatically once the audience, issuer, and signing key file are all
+  // set (no on/off flag); it additionally requires a gcpServiceAccount on the storage config.
+  // ---------------------------------------------------------------------------
+
+  public static final FeatureConfiguration<String> GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE =

Review Comment:
   Since audience is apparently tightly coupled to the signing key, and the key is a deployment artifact, keeping the audience config at the global level (`FeatureConfiguration`) sound reasonable to me.
   
   Multi-org setup is probably something that can happen in a SaaS setting, which by itself is likely to need a custom downstream server build.
   
   Speaking of that, to facilitate reuse downstream, it might be worth making `GcpAttributionParams` a constructor parameter of `GcpCredentialsStorageIntegration`. WDYT? (can be done separately).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "obelix74 (via GitHub)" <gi...@apache.org>.
obelix74 commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3433036133


##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegration.java:
##########
@@ -246,6 +333,40 @@ static StorageAccessConfig compute(GcpStorageCredentialCacheKey key) {
     return accessConfig.build();
   }
 
+  /**
+   * Returns the credential to be used as the source for downscoping.
+   *
+   * <p>When GCS principal attribution is configured and a principal is present (so the cache key
+   * carries pre-computed {@link GcpAttributionParams}), the impersonation source is a federated
+   * identity whose subject is {@code <realm>/<principal>}, which surfaces the principal in {@code
+   * serviceAccountDelegationInfo} of GCS Data Access audit logs. Otherwise this is the standard
+   * path: impersonate the configured service account from the ambient source credentials, or use
+   * those credentials directly.
+   */
+  private static GoogleCredentials baseCredentialsForVending(

Review Comment:
   Renamed to `resolveSourceCredentials` โ€” it now reads clearly as "pick the right source credential for downscoping" regardless of whether attribution is active.



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegration.java:
##########
@@ -246,6 +333,40 @@ static StorageAccessConfig compute(GcpStorageCredentialCacheKey key) {
     return accessConfig.build();
   }
 
+  /**
+   * Returns the credential to be used as the source for downscoping.
+   *
+   * <p>When GCS principal attribution is configured and a principal is present (so the cache key
+   * carries pre-computed {@link GcpAttributionParams}), the impersonation source is a federated
+   * identity whose subject is {@code <realm>/<principal>}, which surfaces the principal in {@code
+   * serviceAccountDelegationInfo} of GCS Data Access audit logs. Otherwise this is the standard
+   * path: impersonate the configured service account from the ambient source credentials, or use
+   * those credentials directly.
+   */
+  private static GoogleCredentials baseCredentialsForVending(
+      GcpStorageCredentialCacheKey key,
+      GcpStorageConfigurationInfo storageConfig,
+      GoogleCredentials sourceCredentials,
+      GcpCredentialOps credentialOps) {
+    Optional<GcpAttributionParams> attributionParams = key.attributionParams();
+    if (attributionParams.isPresent()) {
+      GcpAttributionParams params = attributionParams.get();
+      String subject =
+          GcpAttributionSubjectBuilder.buildSubject(key.realmId(), key.principalName());
+      GcpFederatedCredentialsExchanger exchanger =
+          new GcpFederatedCredentialsExchanger(
+              params.tokenIssuer(),
+              params.wifAudience(),
+              Path.of(params.signingKeyFile()),

Review Comment:
   Moved the validation to `GcpAttributionParams` itself via a `@Value.Derived signingKeyPath()` field that calls `Path.of(signingKeyFile())`. Immutables evaluates derived fields at construction time, so an invalid path string now throws `InvalidPathException` in `resolveAttributionParams` (at integration creation) rather than on the first cache miss.



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpStorageCredentialCacheKey.java:
##########
@@ -55,25 +61,43 @@ public interface GcpStorageCredentialCacheKey extends StorageCredentialCacheKey
   @Value.Parameter(order = 6)
   Optional<String> refreshCredentialsEndpoint();
 
+  /**
+   * The requesting principal name, included in the cache key only when GCS principal attribution is
+   * enabled (otherwise empty). When attribution is active the vended credential carries this
+   * principal's identity, so it must participate in cache identity to avoid serving one principal's
+   * attributed credentials to another.
+   */
+  @Value.Parameter(order = 7)
+  String principalName();

Review Comment:
   Changed to `Optional<String>`. Also updated `buildCacheKey` to use `Optional.empty()` as the absent-principal sentinel and `context.principalName().filter(n -> !n.isEmpty())` to populate it, keeping the logic consistent with the rest of the file.



##########
polaris-core/src/main/java/org/apache/polaris/core/config/FeatureConfiguration.java:
##########
@@ -209,6 +209,64 @@ public static void enforceFeatureEnabledOrThrow(
               .defaultValue(List.<String>of())
               .buildFeatureConfiguration();
 
+  // ---------------------------------------------------------------------------
+  // GCS principal attribution via Workload Identity Federation
+  //
+  // GCP downscoped credentials have no session-tag mechanism (unlike AWS STS), and custom audit
+  // headers only reach GCS audit logs if the client forwards them. To attribute GCS data access
+  // to the Polaris principal for ANY client, credential vending can chain
+  // catalog-signed JWT -> STS token exchange -> per-catalog service-account impersonation, so the
+  // principal appears in serviceAccountDelegationInfo of every GCS Data Access audit log entry.
+  //
+  // Attribution activates automatically once the audience, issuer, and signing key file are all
+  // set (no on/off flag); it additionally requires a gcpServiceAccount on the storage config.
+  // ---------------------------------------------------------------------------
+
+  public static final FeatureConfiguration<String> GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE =

Review Comment:
   RealmConfig settings are read-only once the server is up โ€” there is no admin API to mutate them in-flight. The validation in `resolveAttributionParams` fires once per `GcpCredentialsStorageIntegration` construction (which happens once per realm at startup via `PolarisStorageIntegrationProviderImpl`), so in practice it behaves like a startup check. Moving it to an explicit lifecycle hook would be cleaner but requires a larger refactor across storage providers; happy to file a follow-up if the project wants a uniform "validate on start" contract.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "obelix74 (via GitHub)" <gi...@apache.org>.
obelix74 commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3425667845


##########
polaris-core/src/main/java/org/apache/polaris/core/config/FeatureConfiguration.java:
##########
@@ -209,6 +209,64 @@ public static void enforceFeatureEnabledOrThrow(
               .defaultValue(List.<String>of())
               .buildFeatureConfiguration();
 
+  // ---------------------------------------------------------------------------
+  // GCS principal attribution via Workload Identity Federation
+  //
+  // GCP downscoped credentials have no session-tag mechanism (unlike AWS STS), and custom audit
+  // headers only reach GCS audit logs if the client forwards them. To attribute GCS data access
+  // to the Polaris principal for ANY client, credential vending can chain
+  // catalog-signed JWT -> STS token exchange -> per-catalog service-account impersonation, so the
+  // principal appears in serviceAccountDelegationInfo of every GCS Data Access audit log entry.
+  //
+  // Attribution activates automatically once the audience, issuer, and signing key file are all
+  // set (no on/off flag); it additionally requires a gcpServiceAccount on the storage config.
+  // ---------------------------------------------------------------------------
+
+  public static final FeatureConfiguration<String> GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE =

Review Comment:
   The WIF signing infrastructure (signing key, issuer, WIF pool audience) is naturally a deployment-level concern โ€” Polaris uses a single RSA key to sign all attribution JWTs regardless of which catalog the token is for. The per-catalog variation is handled entirely by `gcpServiceAccount` on the `StorageConfiguration`: each catalog's service account can live in a different GCP project, and GCP STS will impersonate it regardless of which project hosts the WIF pool. So a single WIF pool (single audience) can serve catalogs across multiple GCP projects as long as each SA has the WIF service account binding.
   
   The limit would be a case where different catalogs need *different WIF pools* (e.g. isolated orgs with separate OIDC trust configs) โ€” that is not supported today. If that is a real concern, happy to explore moving `WIF_AUDIENCE` to `StorageConfiguration` in a follow-up, but I would lean toward keeping it deployment-level for now since the common multi-project scenario is covered by the per-SA impersonation.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b merged PR #4707:
URL: https://github.com/apache/polaris/pull/4707


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "obelix74 (via GitHub)" <gi...@apache.org>.
obelix74 commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3431937184


##########
polaris-core/src/main/java/org/apache/polaris/core/config/FeatureConfiguration.java:
##########
@@ -209,6 +209,64 @@ public static void enforceFeatureEnabledOrThrow(
               .defaultValue(List.<String>of())
               .buildFeatureConfiguration();
 
+  // ---------------------------------------------------------------------------
+  // GCS principal attribution via Workload Identity Federation
+  //
+  // GCP downscoped credentials have no session-tag mechanism (unlike AWS STS), and custom audit
+  // headers only reach GCS audit logs if the client forwards them. To attribute GCS data access
+  // to the Polaris principal for ANY client, credential vending can chain
+  // catalog-signed JWT -> STS token exchange -> per-catalog service-account impersonation, so the
+  // principal appears in serviceAccountDelegationInfo of every GCS Data Access audit log entry.
+  //
+  // Attribution activates automatically once the audience, issuer, and signing key file are all
+  // set (no on/off flag); it additionally requires a gcpServiceAccount on the storage config.
+  // ---------------------------------------------------------------------------
+
+  public static final FeatureConfiguration<String> GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE =

Review Comment:
   Done (commit 30e69a383). Added a new full constructor `GcpCredentialsStorageIntegration(..., Optional<GcpAttributionParams> attributionParams)` so downstream builds can pass their own attribution config directly. The existing constructors continue to work via a new public static `resolveAttributionParams(RealmConfig)` helper that preserves the current fail-fast validation. The old private `principalAttributionConfigured()` method is removed โ€” its logic now lives in `resolveAttributionParams()`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "adnanhemani (via GitHub)" <gi...@apache.org>.
adnanhemani commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3439385116


##########
polaris-core/src/test/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegrationTest.java:
##########
@@ -502,4 +515,147 @@ private static String expressionAt(JsonNode parsedRules, int ruleIndex) {
         .path("expression")
         .asText();
   }
+
+  private static RealmConfig configWith(Map<String, String> values) {
+    RealmConfigurationSource source = (rc, name) -> values.get(name);
+    return new RealmConfigImpl(source, () -> "test-realm");
+  }
+
+  @Test
+  void resolveAttributionParamsDisabledByDefault() {
+    assertThat(GcpCredentialsStorageIntegration.resolveAttributionParams(EMPTY_REALM_CONFIG))
+        .isEmpty();
+  }
+
+  @Test
+  void resolveAttributionParamsReturnsParamsWhenFullyConfigured() {
+    RealmConfig config =
+        configWith(
+            Map.of(
+                "GCS_PRINCIPAL_ATTRIBUTION_ENABLED", "true",
+                "GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE",
+                    "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/p/providers/pr",
+                "GCS_PRINCIPAL_ATTRIBUTION_TOKEN_ISSUER", "https://issuer.example.com",
+                "GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_FILE", "/tmp/key.pem"));
+    Optional<GcpAttributionParams> result =
+        GcpCredentialsStorageIntegration.resolveAttributionParams(config);
+    assertThat(result).isPresent();
+    assertThat(result.get().wifAudience())
+        .isEqualTo(
+            "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/p/providers/pr");
+    assertThat(result.get().tokenIssuer()).isEqualTo("https://issuer.example.com");
+    assertThat(result.get().signingKeyFile()).isEqualTo("/tmp/key.pem");
+  }
+
+  @Test
+  void resolveAttributionParamsThrowsWhenEnabledButAudienceMissing() {
+    RealmConfig config =
+        configWith(
+            Map.of(
+                "GCS_PRINCIPAL_ATTRIBUTION_ENABLED", "true",
+                "GCS_PRINCIPAL_ATTRIBUTION_TOKEN_ISSUER", "https://issuer.example.com",
+                "GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_FILE", "/tmp/key.pem"));
+    assertThatThrownBy(() -> GcpCredentialsStorageIntegration.resolveAttributionParams(config))
+        .isInstanceOf(IllegalStateException.class)
+        .hasMessageContaining("GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE");
+  }
+
+  @Test
+  void resolveAttributionParamsThrowsWhenMultipleRequiredParamsMissing() {
+    RealmConfig config = configWith(Map.of("GCS_PRINCIPAL_ATTRIBUTION_ENABLED", "true"));
+    assertThatThrownBy(() -> GcpCredentialsStorageIntegration.resolveAttributionParams(config))
+        .isInstanceOf(IllegalStateException.class)
+        .hasMessageContaining("GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE")
+        .hasMessageContaining("GCS_PRINCIPAL_ATTRIBUTION_TOKEN_ISSUER")
+        .hasMessageContaining("GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_FILE");
+  }
+
+  @Test
+  void testWifAttributionFlowUsesFederatedCredentials(@TempDir Path tempDir) throws Exception {
+    String serviceAccount = "test-sa@project.iam.gserviceaccount.com";
+    GcpStorageConfigurationInfo config =
+        GcpStorageConfigurationInfo.builder()
+            .addAllAllowedLocations(List.of("gs://bucket/path"))
+            .gcpServiceAccount(serviceAccount)
+            .build();
+
+    AtomicReference<GoogleCredentials> capturedSource = new AtomicReference<>();
+
+    IamCredentialsClient mockIamClient = Mockito.mock(IamCredentialsClient.class);
+    GenerateAccessTokenResponse mockResponse =
+        GenerateAccessTokenResponse.newBuilder()
+            .setAccessToken("wif-impersonated-token")
+            .setExpireTime(
+                Timestamp.newBuilder().setSeconds(System.currentTimeMillis() / 1000 + 3600).build())
+            .build();
+    Mockito.when(mockIamClient.generateAccessToken(Mockito.any(GenerateAccessTokenRequest.class)))
+        .thenReturn(mockResponse);
+
+    GoogleCredentials mockCreds = Mockito.mock(GoogleCredentials.class);
+    Mockito.when(mockCreds.createScoped(Mockito.any(String.class))).thenReturn(mockCreds);
+
+    GcpCredentialOps testOps =
+        new GcpCredentialOps() {
+          @Override
+          public IamCredentialsClient createIamCredentialsClient(GoogleCredentials credentials) {
+            capturedSource.set(credentials);
+            return mockIamClient;
+          }
+
+          @Override
+          public AccessToken refreshAccessToken(DownscopedCredentials credentials) {
+            return new AccessToken("downscoped-token", new Date());
+          }
+        };
+
+    KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
+    gen.initialize(2048);
+    KeyPair keyPair = gen.generateKeyPair();
+    String pem =
+        "-----BEGIN PRIVATE KEY-----\n"
+            + Base64.getMimeEncoder(64, "\n".getBytes(UTF_8))
+                .encodeToString(keyPair.getPrivate().getEncoded())
+            + "\n-----END PRIVATE KEY-----\n";
+    Path keyFile = tempDir.resolve("attribution-key.pem");
+    Files.writeString(keyFile, pem);
+
+    GcpAttributionParams params =
+        GcpAttributionParams.of(
+            "https://issuer.example.com",
+            "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/p/providers/pr",
+            keyFile.toString(),
+            "key-1");
+
+    GcpCredentialsStorageIntegration integration =
+        new GcpCredentialsStorageIntegration(
+            mockCreds,
+            ServiceOptions.getFromServiceLoader(HttpTransportFactory.class, NetHttpTransport::new),
+            null,
+            config,
+            EMPTY_REALM_CONFIG,
+            testOps,
+            Optional.of(params));
+
+    integration.getStorageAccessConfig(
+        toGrants(
+            Set.of("gs://bucket/path"), Set.of("gs://bucket/path"), Set.of("gs://bucket/path")),
+        Optional.empty(),
+        CredentialVendingContext.builder()
+            .realm(Optional.of("realm1"))
+            .principalName(Optional.of("principal1"))
+            .build());
+
+    Mockito.verify(mockIamClient)
+        .generateAccessToken(
+            Mockito.argThat(
+                request ->
+                    request
+                        .getName()
+                        .equals(
+                            GcpCredentialsStorageIntegration.SERVICE_ACCOUNT_PREFIX

Review Comment:
   I'm not sure exactly what was tested in here. The whole point of the feature was to ensure that attribution was done according to the realm and principal name - but we never even test that here? Understood if this may not be the exact right place or if additional infra is required - but I'm not sure that this code is sustainable if there is no way to test that a regression has happened somewhere.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3417629844


##########
runtime/admin/distribution/LICENSE:
##########
@@ -399,6 +399,36 @@ License: Apache License 2.0 - https://github.com/hyperxpro/Brotli4j/blob/v1.16.0
 
 --------------------------------------------------------------------------------
 
+This product bundles Auth0 Java JWT.
+
+* Maven group:artifact IDs: com.auth0:java-jwt

Review Comment:
   TIL - LGTM :+1: 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "snazy (via GitHub)" <gi...@apache.org>.
snazy commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3413074243


##########
runtime/admin/distribution/LICENSE:
##########
@@ -399,6 +399,36 @@ License: Apache License 2.0 - https://github.com/hyperxpro/Brotli4j/blob/v1.16.0
 
 --------------------------------------------------------------------------------
 
+This product bundles Auth0 Java JWT.
+
+* Maven group:artifact IDs: com.auth0:java-jwt

Review Comment:
   `com.auth0:java-jwt` wasn't in `:polaris-admin`'s `runtimeClasspath` before.
   This PR adds it via `:polaris-core`.
   `:polaris-runtime-service` is not in `:polaris-admin`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "adnanhemani (via GitHub)" <gi...@apache.org>.
adnanhemani commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3439239640


##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegration.java:
##########
@@ -123,13 +135,75 @@ public GcpCredentialsStorageIntegration(
       GcpStorageConfigurationInfo storageConfig,
       RealmConfig realmConfig,
       GcpCredentialOps credentialOps) {
+    this(
+        sourceCredentials,
+        transportFactory,
+        cache,
+        storageConfig,
+        realmConfig,
+        credentialOps,
+        resolveAttributionParams(realmConfig));
+  }
+
+  /**
+   * Full constructor accepting pre-resolved attribution params. Downstream integrations can pass
+   * their own {@link GcpAttributionParams} without depending on {@link
+   * org.apache.polaris.core.config.FeatureConfiguration}.
+   */
+  public GcpCredentialsStorageIntegration(
+      GoogleCredentials sourceCredentials,
+      HttpTransportFactory transportFactory,
+      org.apache.polaris.core.storage.cache.StorageCredentialCache cache,

Review Comment:
   Still missing addressing this comment.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3453462312


##########
polaris-core/src/main/java/org/apache/polaris/core/config/FeatureConfiguration.java:
##########
@@ -209,6 +209,64 @@ public static void enforceFeatureEnabledOrThrow(
               .defaultValue(List.<String>of())
               .buildFeatureConfiguration();
 
+  // ---------------------------------------------------------------------------
+  // GCS principal attribution via Workload Identity Federation
+  //
+  // GCP downscoped credentials have no session-tag mechanism (unlike AWS STS), and custom audit
+  // headers only reach GCS audit logs if the client forwards them. To attribute GCS data access
+  // to the Polaris principal for ANY client, credential vending can chain
+  // catalog-signed JWT -> STS token exchange -> per-catalog service-account impersonation, so the
+  // principal appears in serviceAccountDelegationInfo of every GCS Data Access audit log entry.
+  //
+  // Attribution activates automatically once the audience, issuer, and signing key file are all
+  // set (no on/off flag); it additionally requires a gcpServiceAccount on the storage config.
+  // ---------------------------------------------------------------------------
+
+  public static final FeatureConfiguration<String> GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE =

Review Comment:
   Good point about `ProductionReadinessCheck` :+1: 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3406518203


##########
runtime/admin/distribution/LICENSE:
##########
@@ -399,6 +399,36 @@ License: Apache License 2.0 - https://github.com/hyperxpro/Brotli4j/blob/v1.16.0
 
 --------------------------------------------------------------------------------
 
+This product bundles Auth0 Java JWT.
+
+* Maven group:artifact IDs: com.auth0:java-jwt

Review Comment:
   This PR does not introduce the dependency on `java-jwt` it just makes it explicit... I believe `java-jwt` was a transitive dependency even before this PR .... so why did not not have to mention it in the license before ๐Ÿค” 
   
   @snazy @jbonofre : WDYT?



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpFederatedCredentialsExchanger.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.core.storage.gcp;
+
+import com.auth0.jwt.JWT;
+import com.auth0.jwt.JWTCreator;
+import com.auth0.jwt.algorithms.Algorithm;
+import com.google.auth.http.HttpTransportFactory;
+import com.google.auth.oauth2.GoogleCredentials;
+import com.google.auth.oauth2.IdentityPoolCredentials;
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.security.KeyFactory;
+import java.security.interfaces.RSAPrivateKey;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.Base64;
+import java.util.Date;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Produces a GCP federated {@link GoogleCredentials} whose identity carries {@code
+ * <realm>/<principal>}, so that GCS Data Access audit logs attribute access to the requesting
+ * Polaris principal. This is the GCP counterpart of AWS STS session tags.
+ *
+ * <p>The federated credential is an {@link IdentityPoolCredentials} backed by a programmatic
+ * subject-token supplier: on each token refresh google-auth invokes the supplier, which mints a
+ * short-lived RS256 JWT ({@code sub = <realm>/<principal>}, {@code realm} claim), and exchanges it
+ * at the Workload Identity Pool provider's STS endpoint. The provider maps {@code google.subject =
+ * assertion.sub} and {@code attribute.realm = assertion.realm}; per-realm {@code attribute.realm}
+ * IAM bindings then enforce that a realm-A identity can only impersonate realm-A's service account.
+ * The returned credential is intended to be used as the source for tenant service-account
+ * impersonation (see {@link GcpCredentialsStorageIntegration}).
+ *
+ * <p>Network note: this performs an STS token exchange against {@code sts.googleapis.com} in
+ * addition to the existing {@code iamcredentials.googleapis.com} and {@code storage.googleapis.com}
+ * traffic.
+ */
+public class GcpFederatedCredentialsExchanger {
+
+  static final String STS_TOKEN_URL = "https://sts.googleapis.com/v1/token";
+  static final String SUBJECT_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:jwt";
+  static final String CLOUD_PLATFORM_SCOPE = "https://www.googleapis.com/auth/cloud-platform";
+
+  /** Attribution JWTs are single-purpose and short-lived. */
+  static final Duration JWT_LIFETIME = Duration.ofMinutes(5);
+
+  /**
+   * JVM-wide cache of parsed signing keys, keyed by file path. The key file is a stable pod-mounted
+   * secret; parsing it (disk read + {@link KeyFactory}) once per path amortizes across vends rather
+   * than re-reading on every credential-cache miss. Key rotation is delivered by a process restart
+   * (the secret is mounted at startup), which clears this cache.
+   */
+  private static final ConcurrentHashMap<Path, RSAPrivateKey> SIGNING_KEY_CACHE =
+      new ConcurrentHashMap<>();
+
+  private final String issuer;
+  private final String wifAudience;
+  private final Path signingKeyPath;
+  private final String signingKeyId;
+  private final HttpTransportFactory transportFactory;
+
+  public GcpFederatedCredentialsExchanger(
+      String issuer,
+      String wifAudience,
+      Path signingKeyPath,
+      String signingKeyId,
+      HttpTransportFactory transportFactory) {
+    this.issuer = issuer;
+    this.wifAudience = wifAudience;
+    this.signingKeyPath = signingKeyPath;
+    this.signingKeyId = signingKeyId;
+    this.transportFactory = transportFactory;
+  }
+
+  /**
+   * Builds a federated credential whose subject is {@code <realm>/<principal>}.
+   *
+   * @param subject the attribution subject, {@code <realm>/<principal>} (see {@link
+   *     GcpAttributionSubjectBuilder})
+   * @param realm the realm identifier, emitted as the {@code realm} claim for {@code
+   *     attribute.realm} mapping
+   * @return federated credentials suitable as the source for tenant-SA impersonation
+   */
+  public GoogleCredentials federatedCredentials(String subject, String realm) {
+    return IdentityPoolCredentials.newBuilder()
+        .setHttpTransportFactory(transportFactory)
+        .setAudience(wifAudience)
+        .setSubjectTokenType(SUBJECT_TOKEN_TYPE)
+        .setTokenUrl(STS_TOKEN_URL)
+        .setScopes(List.of(CLOUD_PLATFORM_SCOPE))
+        .setSubjectTokenSupplier(context -> mintAttributionJwt(subject, realm))
+        .build();
+  }
+
+  @VisibleForTesting
+  String mintAttributionJwt(String subject, String realm) throws IOException {
+    Instant now = Instant.now();
+    JWTCreator.Builder builder =
+        JWT.create()
+            .withIssuer(issuer)
+            .withSubject(subject)
+            .withAudience(wifAudience)
+            .withClaim("realm", realm)
+            .withIssuedAt(Date.from(now))
+            .withExpiresAt(Date.from(now.plus(JWT_LIFETIME)))
+            .withJWTId(UUID.randomUUID().toString());
+    // Set the kid header so the WIF provider can pick the right public key from its JWKS during
+    // rotation (when the JWKS holds both the old and new keys). Omitted only for a single-key JWKS.
+    if (signingKeyId != null && !signingKeyId.isEmpty()) {
+      builder.withKeyId(signingKeyId);
+    }
+    return builder.sign(Algorithm.RSA256(null, loadSigningKey()));
+  }
+
+  private RSAPrivateKey loadSigningKey() throws IOException {
+    RSAPrivateKey cached = SIGNING_KEY_CACHE.get(signingKeyPath);
+    if (cached != null) {
+      return cached;
+    }
+    RSAPrivateKey key = readPkcs8PrivateKey(signingKeyPath);
+    SIGNING_KEY_CACHE.putIfAbsent(signingKeyPath, key);
+    return key;
+  }
+
+  /** Reads an RSA private key from a PKCS#8 PEM file. */
+  @VisibleForTesting
+  static RSAPrivateKey readPkcs8PrivateKey(Path pemPath) throws IOException {
+    String pem = Files.readString(pemPath);
+    String base64 =
+        pem.replaceAll("-----BEGIN [A-Z ]+-----", "")

Review Comment:
   WDYT about reusing `PemUtils`? If it's not conveniently located it might be worth moving refactoring it to allow PEM-related code reuse ๐Ÿค” 



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpFederatedCredentialsExchanger.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.core.storage.gcp;
+
+import com.auth0.jwt.JWT;
+import com.auth0.jwt.JWTCreator;
+import com.auth0.jwt.algorithms.Algorithm;
+import com.google.auth.http.HttpTransportFactory;
+import com.google.auth.oauth2.GoogleCredentials;
+import com.google.auth.oauth2.IdentityPoolCredentials;
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.security.KeyFactory;
+import java.security.interfaces.RSAPrivateKey;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.Base64;
+import java.util.Date;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Produces a GCP federated {@link GoogleCredentials} whose identity carries {@code
+ * <realm>/<principal>}, so that GCS Data Access audit logs attribute access to the requesting
+ * Polaris principal. This is the GCP counterpart of AWS STS session tags.
+ *
+ * <p>The federated credential is an {@link IdentityPoolCredentials} backed by a programmatic
+ * subject-token supplier: on each token refresh google-auth invokes the supplier, which mints a
+ * short-lived RS256 JWT ({@code sub = <realm>/<principal>}, {@code realm} claim), and exchanges it
+ * at the Workload Identity Pool provider's STS endpoint. The provider maps {@code google.subject =
+ * assertion.sub} and {@code attribute.realm = assertion.realm}; per-realm {@code attribute.realm}
+ * IAM bindings then enforce that a realm-A identity can only impersonate realm-A's service account.
+ * The returned credential is intended to be used as the source for tenant service-account
+ * impersonation (see {@link GcpCredentialsStorageIntegration}).
+ *
+ * <p>Network note: this performs an STS token exchange against {@code sts.googleapis.com} in
+ * addition to the existing {@code iamcredentials.googleapis.com} and {@code storage.googleapis.com}
+ * traffic.
+ */
+public class GcpFederatedCredentialsExchanger {
+
+  static final String STS_TOKEN_URL = "https://sts.googleapis.com/v1/token";
+  static final String SUBJECT_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:jwt";
+  static final String CLOUD_PLATFORM_SCOPE = "https://www.googleapis.com/auth/cloud-platform";
+
+  /** Attribution JWTs are single-purpose and short-lived. */
+  static final Duration JWT_LIFETIME = Duration.ofMinutes(5);
+
+  /**
+   * JVM-wide cache of parsed signing keys, keyed by file path. The key file is a stable pod-mounted
+   * secret; parsing it (disk read + {@link KeyFactory}) once per path amortizes across vends rather
+   * than re-reading on every credential-cache miss. Key rotation is delivered by a process restart
+   * (the secret is mounted at startup), which clears this cache.
+   */
+  private static final ConcurrentHashMap<Path, RSAPrivateKey> SIGNING_KEY_CACHE =
+      new ConcurrentHashMap<>();
+
+  private final String issuer;
+  private final String wifAudience;
+  private final Path signingKeyPath;
+  private final String signingKeyId;
+  private final HttpTransportFactory transportFactory;
+
+  public GcpFederatedCredentialsExchanger(
+      String issuer,
+      String wifAudience,
+      Path signingKeyPath,
+      String signingKeyId,
+      HttpTransportFactory transportFactory) {
+    this.issuer = issuer;
+    this.wifAudience = wifAudience;
+    this.signingKeyPath = signingKeyPath;
+    this.signingKeyId = signingKeyId;
+    this.transportFactory = transportFactory;
+  }
+
+  /**
+   * Builds a federated credential whose subject is {@code <realm>/<principal>}.
+   *
+   * @param subject the attribution subject, {@code <realm>/<principal>} (see {@link
+   *     GcpAttributionSubjectBuilder})
+   * @param realm the realm identifier, emitted as the {@code realm} claim for {@code
+   *     attribute.realm} mapping
+   * @return federated credentials suitable as the source for tenant-SA impersonation
+   */
+  public GoogleCredentials federatedCredentials(String subject, String realm) {
+    return IdentityPoolCredentials.newBuilder()
+        .setHttpTransportFactory(transportFactory)
+        .setAudience(wifAudience)
+        .setSubjectTokenType(SUBJECT_TOKEN_TYPE)
+        .setTokenUrl(STS_TOKEN_URL)
+        .setScopes(List.of(CLOUD_PLATFORM_SCOPE))
+        .setSubjectTokenSupplier(context -> mintAttributionJwt(subject, realm))
+        .build();
+  }
+
+  @VisibleForTesting
+  String mintAttributionJwt(String subject, String realm) throws IOException {
+    Instant now = Instant.now();
+    JWTCreator.Builder builder =
+        JWT.create()
+            .withIssuer(issuer)
+            .withSubject(subject)
+            .withAudience(wifAudience)
+            .withClaim("realm", realm)
+            .withIssuedAt(Date.from(now))
+            .withExpiresAt(Date.from(now.plus(JWT_LIFETIME)))
+            .withJWTId(UUID.randomUUID().toString());
+    // Set the kid header so the WIF provider can pick the right public key from its JWKS during
+    // rotation (when the JWKS holds both the old and new keys). Omitted only for a single-key JWKS.
+    if (signingKeyId != null && !signingKeyId.isEmpty()) {
+      builder.withKeyId(signingKeyId);
+    }
+    return builder.sign(Algorithm.RSA256(null, loadSigningKey()));
+  }
+
+  private RSAPrivateKey loadSigningKey() throws IOException {
+    RSAPrivateKey cached = SIGNING_KEY_CACHE.get(signingKeyPath);
+    if (cached != null) {
+      return cached;
+    }
+    RSAPrivateKey key = readPkcs8PrivateKey(signingKeyPath);
+    SIGNING_KEY_CACHE.putIfAbsent(signingKeyPath, key);

Review Comment:
   nit: `return SIGNING_KEY_CACHE.compute(signingKeyPath, (k,v) -> key)` might be more correct (returning the same key to all threads, while avoiding blocking others on local file I/O).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "adnanhemani (via GitHub)" <gi...@apache.org>.
adnanhemani commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3432376930


##########
polaris-core/src/main/java/org/apache/polaris/core/config/FeatureConfiguration.java:
##########
@@ -209,6 +209,82 @@ public static void enforceFeatureEnabledOrThrow(
               .defaultValue(List.<String>of())
               .buildFeatureConfiguration();
 
+  // ---------------------------------------------------------------------------
+  // GCS principal attribution via Workload Identity Federation

Review Comment:
   Personally, don't think we need this comment given that the description fields are already quite detailed.



##########
polaris-core/src/main/java/org/apache/polaris/core/config/FeatureConfiguration.java:
##########
@@ -209,6 +209,82 @@ public static void enforceFeatureEnabledOrThrow(
               .defaultValue(List.<String>of())
               .buildFeatureConfiguration();
 
+  // ---------------------------------------------------------------------------
+  // GCS principal attribution via Workload Identity Federation
+  //
+  // GCP downscoped credentials have no session-tag mechanism (unlike AWS STS), and custom audit
+  // headers only reach GCS audit logs if the client forwards them. To attribute GCS data access
+  // to the Polaris principal for ANY client, credential vending can chain
+  // catalog-signed JWT -> STS token exchange -> per-catalog service-account impersonation, so the
+  // principal appears in serviceAccountDelegationInfo of every GCS Data Access audit log entry.
+  //
+  // Attribution must be explicitly enabled via GCS_PRINCIPAL_ATTRIBUTION_ENABLED. When enabled,
+  // WIF_AUDIENCE, TOKEN_ISSUER, and SIGNING_KEY_FILE are all required; Polaris will throw at the
+  // first credential-vending attempt if any are missing. Additionally requires a gcpServiceAccount
+  // on the per-catalog StorageConfiguration.
+  // ---------------------------------------------------------------------------
+
+  public static final FeatureConfiguration<Boolean> GCS_PRINCIPAL_ATTRIBUTION_ENABLED =
+      PolarisConfiguration.<Boolean>builder()
+          .key("GCS_PRINCIPAL_ATTRIBUTION_ENABLED")
+          .description(
+              "Enables GCS principal attribution via Workload Identity Federation.\n"
+                  + "When true, credential vending chains a catalog-signed JWT through an STS token\n"
+                  + "exchange and service-account impersonation so the Polaris principal appears in GCS\n"
+                  + "Data Access audit logs (serviceAccountDelegationInfo.principalSubject).\n"
+                  + "Requires GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE, GCS_PRINCIPAL_ATTRIBUTION_TOKEN_ISSUER,\n"
+                  + "and GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_FILE to also be set;\n"
+                  + "a missing required value is a fatal configuration error.\n"
+                  + "Also requires a gcpServiceAccount on the catalog StorageConfiguration.\n"
+                  + "Default: false (attribution disabled).")
+          .defaultValue(false)
+          .buildFeatureConfiguration();
+
+  public static final FeatureConfiguration<String> GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE =
+      PolarisConfiguration.<String>builder()
+          .key("GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE")
+          .description(
+              "Full resource name of the Workload Identity Pool provider used for GCS principal\n"
+                  + "attribution, e.g.\n"
+                  + "//iam.googleapis.com/projects/<num>/locations/global/workloadIdentityPools/<pool>/providers/<provider>.\n"
+                  + "Used as both the attribution JWT 'aud' claim and the STS token-exchange audience.\n"
+                  + "Empty (default) disables principal attribution.")

Review Comment:
   This description says "Empty (default) disables principal attribution." That was accurate before `GCS_PRINCIPAL_ATTRIBUTION_ENABLED` was added, but is now stale. With the explicit enable flag, an empty `WIF_AUDIENCE` while `ENABLED=true` does not gracefully disable attribution โ€” it triggers a fatal validation error. `TOKEN_ISSUER` (line ~262) and `SIGNING_KEY_FILE` (line ~272) carry the same stale description. All three should say something like: "Required when `GCS_PRINCIPAL_ATTRIBUTION_ENABLED=true`; ignored otherwise."



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpFederatedCredentialsExchanger.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.core.storage.gcp;
+
+import com.auth0.jwt.JWT;
+import com.auth0.jwt.JWTCreator;
+import com.auth0.jwt.algorithms.Algorithm;
+import com.google.auth.http.HttpTransportFactory;
+import com.google.auth.oauth2.GoogleCredentials;
+import com.google.auth.oauth2.IdentityPoolCredentials;
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.security.KeyFactory;
+import java.security.interfaces.RSAPrivateKey;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.Base64;
+import java.util.Date;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Produces a GCP federated {@link GoogleCredentials} whose identity carries {@code
+ * <realm>/<principal>}, so that GCS Data Access audit logs attribute access to the requesting
+ * Polaris principal. This is the GCP counterpart of AWS STS session tags.
+ *
+ * <p>The federated credential is an {@link IdentityPoolCredentials} backed by a programmatic
+ * subject-token supplier: on each token refresh google-auth invokes the supplier, which mints a
+ * short-lived RS256 JWT ({@code sub = <realm>/<principal>}, {@code realm} claim), and exchanges it
+ * at the Workload Identity Pool provider's STS endpoint. The provider maps {@code google.subject =
+ * assertion.sub} and {@code attribute.realm = assertion.realm}; per-realm {@code attribute.realm}
+ * IAM bindings then enforce that a realm-A identity can only impersonate realm-A's service account.
+ * The returned credential is intended to be used as the source for tenant service-account
+ * impersonation (see {@link GcpCredentialsStorageIntegration}).
+ *
+ * <p>Network note: this performs an STS token exchange against {@code sts.googleapis.com} in
+ * addition to the existing {@code iamcredentials.googleapis.com} and {@code storage.googleapis.com}
+ * traffic.
+ */
+public class GcpFederatedCredentialsExchanger {
+
+  static final String STS_TOKEN_URL = "https://sts.googleapis.com/v1/token";
+  static final String SUBJECT_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:jwt";
+  static final String CLOUD_PLATFORM_SCOPE = "https://www.googleapis.com/auth/cloud-platform";
+
+  /** Attribution JWTs are single-purpose and short-lived. */
+  static final Duration JWT_LIFETIME = Duration.ofMinutes(5);
+
+  /**
+   * JVM-wide cache of parsed signing keys, keyed by file path. The key file is a stable pod-mounted
+   * secret; parsing it (disk read + {@link KeyFactory}) once per path amortizes across vends rather
+   * than re-reading on every credential-cache miss. Key rotation is delivered by a process restart
+   * (the secret is mounted at startup), which clears this cache.
+   */
+  private static final ConcurrentHashMap<Path, RSAPrivateKey> SIGNING_KEY_CACHE =
+      new ConcurrentHashMap<>();
+
+  private final String issuer;
+  private final String wifAudience;
+  private final Path signingKeyPath;
+  private final String signingKeyId;
+  private final HttpTransportFactory transportFactory;
+
+  public GcpFederatedCredentialsExchanger(
+      String issuer,
+      String wifAudience,
+      Path signingKeyPath,
+      String signingKeyId,
+      HttpTransportFactory transportFactory) {
+    this.issuer = issuer;
+    this.wifAudience = wifAudience;
+    this.signingKeyPath = signingKeyPath;
+    this.signingKeyId = signingKeyId;
+    this.transportFactory = transportFactory;
+  }
+
+  /**
+   * Builds a federated credential whose subject is {@code <realm>/<principal>}.
+   *
+   * @param subject the attribution subject, {@code <realm>/<principal>} (see {@link
+   *     GcpAttributionSubjectBuilder})
+   * @param realm the realm identifier, emitted as the {@code realm} claim for {@code
+   *     attribute.realm} mapping
+   * @return federated credentials suitable as the source for tenant-SA impersonation
+   */
+  public GoogleCredentials federatedCredentials(String subject, String realm) {
+    return IdentityPoolCredentials.newBuilder()
+        .setHttpTransportFactory(transportFactory)
+        .setAudience(wifAudience)
+        .setSubjectTokenType(SUBJECT_TOKEN_TYPE)
+        .setTokenUrl(STS_TOKEN_URL)
+        .setScopes(List.of(CLOUD_PLATFORM_SCOPE))
+        .setSubjectTokenSupplier(context -> mintAttributionJwt(subject, realm))
+        .build();
+  }
+
+  @VisibleForTesting
+  String mintAttributionJwt(String subject, String realm) throws IOException {
+    Instant now = Instant.now();
+    JWTCreator.Builder builder =
+        JWT.create()
+            .withIssuer(issuer)
+            .withSubject(subject)
+            .withAudience(wifAudience)
+            .withClaim("realm", realm)
+            .withIssuedAt(Date.from(now))
+            .withExpiresAt(Date.from(now.plus(JWT_LIFETIME)))
+            .withJWTId(UUID.randomUUID().toString());
+    // Set the kid header so the WIF provider can pick the right public key from its JWKS during
+    // rotation (when the JWKS holds both the old and new keys). Omitted only for a single-key JWKS.
+    if (signingKeyId != null && !signingKeyId.isEmpty()) {
+      builder.withKeyId(signingKeyId);
+    }
+    return builder.sign(Algorithm.RSA256(null, loadSigningKey()));

Review Comment:
   May I suggest a quick comment here to reduce confusion around the `null` param here?
   ```java
   // null public key: sign-only; verification is the WIF provider's responsibility
   ```



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegration.java:
##########
@@ -123,13 +135,75 @@ public GcpCredentialsStorageIntegration(
       GcpStorageConfigurationInfo storageConfig,
       RealmConfig realmConfig,
       GcpCredentialOps credentialOps) {
+    this(
+        sourceCredentials,
+        transportFactory,
+        cache,
+        storageConfig,
+        realmConfig,
+        credentialOps,
+        resolveAttributionParams(realmConfig));
+  }
+
+  /**
+   * Full constructor accepting pre-resolved attribution params. Downstream integrations can pass
+   * their own {@link GcpAttributionParams} without depending on {@link
+   * org.apache.polaris.core.config.FeatureConfiguration}.
+   */
+  public GcpCredentialsStorageIntegration(
+      GoogleCredentials sourceCredentials,
+      HttpTransportFactory transportFactory,
+      org.apache.polaris.core.storage.cache.StorageCredentialCache cache,
+      GcpStorageConfigurationInfo storageConfig,
+      RealmConfig realmConfig,
+      GcpCredentialOps credentialOps,
+      Optional<GcpAttributionParams> attributionParams) {
     super(cache, realmConfig, storageConfig);
     // Needed for when environment variable GOOGLE_APPLICATION_CREDENTIALS points to google service
     // account key json
     this.sourceCredentials =
         sourceCredentials.createScoped("https://www.googleapis.com/auth/cloud-platform");
     this.transportFactory = transportFactory;
     this.credentialOps = credentialOps;
+    this.attributionParams = attributionParams;
+  }
+
+  /**
+   * Resolves {@link GcpAttributionParams} from realm config. Returns empty when attribution is
+   * disabled; throws {@link IllegalStateException} when enabled but misconfigured.
+   */
+  public static Optional<GcpAttributionParams> resolveAttributionParams(RealmConfig realmConfig) {

Review Comment:
   Not sure why this is a `static` function?



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpAttributionSubjectBuilder.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.core.storage.gcp;
+
+/**
+ * Builds the {@code sub} claim for GCS principal-attribution JWTs as {@code <realm>/<principal>},
+ * within GCP's 127-character {@code google.subject} limit.
+ *
+ * <p>The character budget mirrors the AWS session-name builder: one character is reserved for the
+ * separator, then each field receives an equal share of the remainder, and budget unused by a short
+ * field flows to the other. ISO control characters and the {@code /} separator are stripped from
+ * each field so the subject stays unambiguously parseable, and the {@code unknown} placeholder
+ * substitutes null/empty fields so the subject shape stays stable.
+ */
+public final class GcpAttributionSubjectBuilder {
+
+  /** GCP limit for the {@code google.subject} attribute of a federated identity. */
+  public static final int MAX_SUBJECT_LENGTH = 127;
+
+  static final String SEPARATOR = "/";
+
+  static final String VALUE_UNKNOWN = "unknown";
+
+  private GcpAttributionSubjectBuilder() {}
+
+  /**
+   * Builds the attribution subject {@code <realm>/<principal>}, guaranteed to be at most {@value
+   * #MAX_SUBJECT_LENGTH} characters.
+   *
+   * @param realm the realm identifier (gets first-half budget priority)
+   * @param principalName the Polaris principal name
+   * @return the subject string
+   */
+  public static String buildSubject(String realm, String principalName) {
+    String cleanRealm = sanitize(realm);
+    String cleanPrincipal = sanitize(principalName);
+
+    int budget = MAX_SUBJECT_LENGTH - SEPARATOR.length();
+    int remaining = budget;
+
+    int realmAlloc = remaining / 2;
+    int realmUsed = Math.min(cleanRealm.length(), realmAlloc);
+    remaining -= realmUsed;
+
+    int principalUsed = Math.min(cleanPrincipal.length(), remaining);
+    remaining -= principalUsed;
+
+    // Carry-forward: if the principal left budget unused, the realm may take more than its
+    // initial half-share.
+    int realmFinal = Math.min(cleanRealm.length(), realmUsed + remaining);

Review Comment:
   nit: I think the math goes both ways (principal OR realm can take over budget of the opposite). Might want to change the comment to reflect that 



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegration.java:
##########
@@ -176,17 +250,30 @@ private GcpStorageCredentialCacheKey buildCacheKey(
       @NonNull Set<String> writeLocations,
       @NonNull Optional<String> refreshEndpoint,
       @NonNull CredentialVendingContext context) {
+    // Principal attribution makes the vended token per-principal, so the principal must
+    // participate in cache identity; otherwise it is left empty to preserve cross-principal cache
+    // reuse. Attribution requires a service account to impersonate and a principal to attribute.
+    String principalName = "";
+    Optional<GcpAttributionParams> resolvedAttributionParams = Optional.empty();
+    if (attributionParams.isPresent() && storageConfig().getGcpServiceAccount() != null) {
+      principalName = context.principalName().orElse("");
+      if (!principalName.isEmpty()) {

Review Comment:
   I don't think it's possible to have an empty principalName here. I agree with the defensive coding style, but maybe we should just throw an exception here (or send a WARN log line at the minimum) rather than just acting like nothing is wrong.



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpFederatedCredentialsExchanger.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.core.storage.gcp;
+
+import com.auth0.jwt.JWT;
+import com.auth0.jwt.JWTCreator;
+import com.auth0.jwt.algorithms.Algorithm;
+import com.google.auth.http.HttpTransportFactory;
+import com.google.auth.oauth2.GoogleCredentials;
+import com.google.auth.oauth2.IdentityPoolCredentials;
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.security.KeyFactory;
+import java.security.interfaces.RSAPrivateKey;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.Base64;
+import java.util.Date;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Produces a GCP federated {@link GoogleCredentials} whose identity carries {@code
+ * <realm>/<principal>}, so that GCS Data Access audit logs attribute access to the requesting
+ * Polaris principal. This is the GCP counterpart of AWS STS session tags.
+ *
+ * <p>The federated credential is an {@link IdentityPoolCredentials} backed by a programmatic
+ * subject-token supplier: on each token refresh google-auth invokes the supplier, which mints a
+ * short-lived RS256 JWT ({@code sub = <realm>/<principal>}, {@code realm} claim), and exchanges it
+ * at the Workload Identity Pool provider's STS endpoint. The provider maps {@code google.subject =
+ * assertion.sub} and {@code attribute.realm = assertion.realm}; per-realm {@code attribute.realm}
+ * IAM bindings then enforce that a realm-A identity can only impersonate realm-A's service account.
+ * The returned credential is intended to be used as the source for tenant service-account
+ * impersonation (see {@link GcpCredentialsStorageIntegration}).
+ *
+ * <p>Network note: this performs an STS token exchange against {@code sts.googleapis.com} in
+ * addition to the existing {@code iamcredentials.googleapis.com} and {@code storage.googleapis.com}
+ * traffic.
+ */
+public class GcpFederatedCredentialsExchanger {
+
+  static final String STS_TOKEN_URL = "https://sts.googleapis.com/v1/token";
+  static final String SUBJECT_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:jwt";
+  static final String CLOUD_PLATFORM_SCOPE = "https://www.googleapis.com/auth/cloud-platform";
+
+  /** Attribution JWTs are single-purpose and short-lived. */
+  static final Duration JWT_LIFETIME = Duration.ofMinutes(5);
+
+  /**
+   * JVM-wide cache of parsed signing keys, keyed by file path. The key file is a stable pod-mounted
+   * secret; parsing it (disk read + {@link KeyFactory}) once per path amortizes across vends rather
+   * than re-reading on every credential-cache miss. Key rotation is delivered by a process restart
+   * (the secret is mounted at startup), which clears this cache.
+   */
+  private static final ConcurrentHashMap<Path, RSAPrivateKey> SIGNING_KEY_CACHE =
+      new ConcurrentHashMap<>();
+
+  private final String issuer;
+  private final String wifAudience;
+  private final Path signingKeyPath;
+  private final String signingKeyId;
+  private final HttpTransportFactory transportFactory;
+
+  public GcpFederatedCredentialsExchanger(
+      String issuer,
+      String wifAudience,
+      Path signingKeyPath,
+      String signingKeyId,
+      HttpTransportFactory transportFactory) {
+    this.issuer = issuer;
+    this.wifAudience = wifAudience;
+    this.signingKeyPath = signingKeyPath;
+    this.signingKeyId = signingKeyId;
+    this.transportFactory = transportFactory;
+  }
+
+  /**
+   * Builds a federated credential whose subject is {@code <realm>/<principal>}.
+   *
+   * @param subject the attribution subject, {@code <realm>/<principal>} (see {@link
+   *     GcpAttributionSubjectBuilder})
+   * @param realm the realm identifier, emitted as the {@code realm} claim for {@code
+   *     attribute.realm} mapping
+   * @return federated credentials suitable as the source for tenant-SA impersonation
+   */
+  public GoogleCredentials federatedCredentials(String subject, String realm) {
+    return IdentityPoolCredentials.newBuilder()
+        .setHttpTransportFactory(transportFactory)
+        .setAudience(wifAudience)
+        .setSubjectTokenType(SUBJECT_TOKEN_TYPE)
+        .setTokenUrl(STS_TOKEN_URL)
+        .setScopes(List.of(CLOUD_PLATFORM_SCOPE))
+        .setSubjectTokenSupplier(context -> mintAttributionJwt(subject, realm))
+        .build();
+  }
+
+  @VisibleForTesting
+  String mintAttributionJwt(String subject, String realm) throws IOException {
+    Instant now = Instant.now();
+    JWTCreator.Builder builder =
+        JWT.create()
+            .withIssuer(issuer)
+            .withSubject(subject)
+            .withAudience(wifAudience)
+            .withClaim("realm", realm)
+            .withIssuedAt(Date.from(now))
+            .withExpiresAt(Date.from(now.plus(JWT_LIFETIME)))
+            .withJWTId(UUID.randomUUID().toString());
+    // Set the kid header so the WIF provider can pick the right public key from its JWKS during
+    // rotation (when the JWKS holds both the old and new keys). Omitted only for a single-key JWKS.
+    if (signingKeyId != null && !signingKeyId.isEmpty()) {
+      builder.withKeyId(signingKeyId);
+    }
+    return builder.sign(Algorithm.RSA256(null, loadSigningKey()));
+  }
+
+  private RSAPrivateKey loadSigningKey() throws IOException {
+    try {
+      return SIGNING_KEY_CACHE.computeIfAbsent(
+          signingKeyPath,
+          path -> {
+            try {
+              return readPkcs8PrivateKey(path);
+            } catch (IOException e) {
+              throw new UncheckedIOException(e);
+            }
+          });
+    } catch (UncheckedIOException e) {
+      throw e.getCause();
+    }
+  }
+
+  /** Reads an RSA private key from a PKCS#8 PEM file. */
+  @VisibleForTesting
+  static RSAPrivateKey readPkcs8PrivateKey(Path pemPath) throws IOException {
+    String pem = Files.readString(pemPath);
+    String base64 =
+        pem.replaceAll("-----BEGIN [A-Z ]+-----", "")

Review Comment:
   The regex strips any PEM header (`BEGIN [A-Z ]+`), so it silently accepts both `BEGIN PRIVATE KEY` (PKCS#8, correct) and `BEGIN RSA PRIVATE KEY` (PKCS#1, wrong). If an operator provides the default `openssl genrsa` output, the header is stripped cleanly but `PKCS8EncodedKeySpec` then fails on the raw DER with a generic `InvalidKeySpecException` that gives no hint about the format mismatch. Worth adding an explicit check on the header string and throwing a descriptive error if it isn't `BEGIN PRIVATE KEY`.



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegration.java:
##########
@@ -246,6 +333,40 @@ static StorageAccessConfig compute(GcpStorageCredentialCacheKey key) {
     return accessConfig.build();
   }
 
+  /**
+   * Returns the credential to be used as the source for downscoping.
+   *
+   * <p>When GCS principal attribution is configured and a principal is present (so the cache key
+   * carries pre-computed {@link GcpAttributionParams}), the impersonation source is a federated
+   * identity whose subject is {@code <realm>/<principal>}, which surfaces the principal in {@code
+   * serviceAccountDelegationInfo} of GCS Data Access audit logs. Otherwise this is the standard
+   * path: impersonate the configured service account from the ambient source credentials, or use
+   * those credentials directly.
+   */
+  private static GoogleCredentials baseCredentialsForVending(

Review Comment:
   `baseCredentialsForVending` and `getBaseCredentials` (line ~374) are confusingly similar names that sit ~30 lines apart. A reader trying to understand the non-attribution path has to pause and confirm which one applies. `baseCredentialsForVending` could be named `resolveSourceCredentials` or `attributedSourceCredentials` to make the dispatch purpose immediately clear.



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpFederatedCredentialsExchanger.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.core.storage.gcp;
+
+import com.auth0.jwt.JWT;
+import com.auth0.jwt.JWTCreator;
+import com.auth0.jwt.algorithms.Algorithm;
+import com.google.auth.http.HttpTransportFactory;
+import com.google.auth.oauth2.GoogleCredentials;
+import com.google.auth.oauth2.IdentityPoolCredentials;
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.security.KeyFactory;
+import java.security.interfaces.RSAPrivateKey;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.Base64;
+import java.util.Date;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Produces a GCP federated {@link GoogleCredentials} whose identity carries {@code
+ * <realm>/<principal>}, so that GCS Data Access audit logs attribute access to the requesting
+ * Polaris principal. This is the GCP counterpart of AWS STS session tags.
+ *
+ * <p>The federated credential is an {@link IdentityPoolCredentials} backed by a programmatic
+ * subject-token supplier: on each token refresh google-auth invokes the supplier, which mints a
+ * short-lived RS256 JWT ({@code sub = <realm>/<principal>}, {@code realm} claim), and exchanges it
+ * at the Workload Identity Pool provider's STS endpoint. The provider maps {@code google.subject =
+ * assertion.sub} and {@code attribute.realm = assertion.realm}; per-realm {@code attribute.realm}
+ * IAM bindings then enforce that a realm-A identity can only impersonate realm-A's service account.
+ * The returned credential is intended to be used as the source for tenant service-account
+ * impersonation (see {@link GcpCredentialsStorageIntegration}).
+ *
+ * <p>Network note: this performs an STS token exchange against {@code sts.googleapis.com} in
+ * addition to the existing {@code iamcredentials.googleapis.com} and {@code storage.googleapis.com}
+ * traffic.
+ */
+public class GcpFederatedCredentialsExchanger {
+
+  static final String STS_TOKEN_URL = "https://sts.googleapis.com/v1/token";
+  static final String SUBJECT_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:jwt";
+  static final String CLOUD_PLATFORM_SCOPE = "https://www.googleapis.com/auth/cloud-platform";
+
+  /** Attribution JWTs are single-purpose and short-lived. */
+  static final Duration JWT_LIFETIME = Duration.ofMinutes(5);
+
+  /**
+   * JVM-wide cache of parsed signing keys, keyed by file path. The key file is a stable pod-mounted
+   * secret; parsing it (disk read + {@link KeyFactory}) once per path amortizes across vends rather
+   * than re-reading on every credential-cache miss. Key rotation is delivered by a process restart
+   * (the secret is mounted at startup), which clears this cache.
+   */
+  private static final ConcurrentHashMap<Path, RSAPrivateKey> SIGNING_KEY_CACHE =
+      new ConcurrentHashMap<>();

Review Comment:
   `SIGNING_KEY_CACHE` is an unbounded, never-evicted JVM-wide map. The PR description documents that key rotation requires a process restart (correct for a pod-mounted secret), but the map has no size bound. In a deployment where many realms each have distinct signing-key paths, all keys accumulate for the process lifetime. Low risk for typical deployments, but worth documenting the assumption ("one or very few distinct key paths per process") directly on the field - or using a Guava cache maybe.
   
   Although, I think in practice, it will be very hard to reach a heavy amount of these stored in memory (I believe RSAPrivateKeys are a few KBs each maximum) - so maybe we just leave a comment here saying that we're basically bounded by the amount of realms we support more than anything here.



##########
site/content/in-dev/unreleased/configuration/config-sections/flags-polaris_features.md:
##########
@@ -335,6 +335,51 @@ The maximum weight for the entity cache. This is a heuristic value without any p
 
 ---
 
+##### `polaris.features."GCS_PRINCIPAL_ATTRIBUTION_ENABLED"`
+
+Enables GCS principal attribution via Workload Identity Federation. When true, credential vending chains a catalog-signed JWT through an STS token exchange and service-account impersonation so the Polaris principal appears in GCS Data Access audit logs (serviceAccountDelegationInfo.principalSubject). Requires GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE, GCS_PRINCIPAL_ATTRIBUTION_TOKEN_ISSUER, and GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_FILE to also be set; a missing required value is a fatal configuration error. Also requires a gcpServiceAccount on the catalog StorageConfiguration. Default: false (attribution disabled).
+
+- **Type:** `Boolean`
+- **Default:** `false`
+
+---
+
+##### `polaris.features."GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_FILE"`
+
+Filesystem path to the PKCS#8 PEM RSA private key used to sign GCS attribution JWTs (RS256). The corresponding public key must be published in the Workload Identity Pool provider's uploaded JWKS. Empty (default) disables principal attribution.

Review Comment:
   Same stale description issue as in `FeatureConfiguration.java`: three of the flag docs (`SIGNING_KEY_FILE` here, `TOKEN_ISSUER` at line ~367, and `WIF_AUDIENCE` at line ~376) all say "Empty (default) disables principal attribution." Now that `GCS_PRINCIPAL_ATTRIBUTION_ENABLED` is the master switch, an empty value with `ENABLED=true` is a fatal misconfiguration, not a graceful disable. These descriptions should say they are required when `GCS_PRINCIPAL_ATTRIBUTION_ENABLED=true`.



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegration.java:
##########
@@ -123,13 +135,75 @@ public GcpCredentialsStorageIntegration(
       GcpStorageConfigurationInfo storageConfig,
       RealmConfig realmConfig,
       GcpCredentialOps credentialOps) {
+    this(
+        sourceCredentials,
+        transportFactory,
+        cache,
+        storageConfig,
+        realmConfig,
+        credentialOps,
+        resolveAttributionParams(realmConfig));
+  }
+
+  /**
+   * Full constructor accepting pre-resolved attribution params. Downstream integrations can pass
+   * their own {@link GcpAttributionParams} without depending on {@link
+   * org.apache.polaris.core.config.FeatureConfiguration}.
+   */
+  public GcpCredentialsStorageIntegration(
+      GoogleCredentials sourceCredentials,
+      HttpTransportFactory transportFactory,
+      org.apache.polaris.core.storage.cache.StorageCredentialCache cache,

Review Comment:
   No FQNs please.



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegration.java:
##########
@@ -176,17 +250,30 @@ private GcpStorageCredentialCacheKey buildCacheKey(
       @NonNull Set<String> writeLocations,
       @NonNull Optional<String> refreshEndpoint,
       @NonNull CredentialVendingContext context) {
+    // Principal attribution makes the vended token per-principal, so the principal must
+    // participate in cache identity; otherwise it is left empty to preserve cross-principal cache
+    // reuse. Attribution requires a service account to impersonate and a principal to attribute.
+    String principalName = "";
+    Optional<GcpAttributionParams> resolvedAttributionParams = Optional.empty();
+    if (attributionParams.isPresent() && storageConfig().getGcpServiceAccount() != null) {

Review Comment:
   When attribution is enabled (`attributionParams.isPresent()`) but `storageConfig().getGcpServiceAccount()` is null, the code silently falls back to unattributed credentials with no log line. Would recommend putting a WARN or DEBUG line here stating that we fell back regardless of the fact that attribution was enabled



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpStorageCredentialCacheKey.java:
##########
@@ -55,25 +61,43 @@ public interface GcpStorageCredentialCacheKey extends StorageCredentialCacheKey
   @Value.Parameter(order = 6)
   Optional<String> refreshCredentialsEndpoint();
 
+  /**
+   * The requesting principal name, included in the cache key only when GCS principal attribution is
+   * enabled (otherwise empty). When attribution is active the vended credential carries this
+   * principal's identity, so it must participate in cache identity to avoid serving one principal's
+   * attributed credentials to another.
+   */
+  @Value.Parameter(order = 7)
+  String principalName();

Review Comment:
   What about making this Optional<String> to match the convention in the rest of the file rather than using "" to signal no-principal-name?



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegration.java:
##########
@@ -246,6 +333,40 @@ static StorageAccessConfig compute(GcpStorageCredentialCacheKey key) {
     return accessConfig.build();
   }
 
+  /**
+   * Returns the credential to be used as the source for downscoping.
+   *
+   * <p>When GCS principal attribution is configured and a principal is present (so the cache key
+   * carries pre-computed {@link GcpAttributionParams}), the impersonation source is a federated
+   * identity whose subject is {@code <realm>/<principal>}, which surfaces the principal in {@code
+   * serviceAccountDelegationInfo} of GCS Data Access audit logs. Otherwise this is the standard
+   * path: impersonate the configured service account from the ambient source credentials, or use
+   * those credentials directly.
+   */
+  private static GoogleCredentials baseCredentialsForVending(
+      GcpStorageCredentialCacheKey key,
+      GcpStorageConfigurationInfo storageConfig,
+      GoogleCredentials sourceCredentials,
+      GcpCredentialOps credentialOps) {
+    Optional<GcpAttributionParams> attributionParams = key.attributionParams();
+    if (attributionParams.isPresent()) {
+      GcpAttributionParams params = attributionParams.get();
+      String subject =
+          GcpAttributionSubjectBuilder.buildSubject(key.realmId(), key.principalName());
+      GcpFederatedCredentialsExchanger exchanger =
+          new GcpFederatedCredentialsExchanger(
+              params.tokenIssuer(),
+              params.wifAudience(),
+              Path.of(params.signingKeyFile()),

Review Comment:
   `Path.of(params.signingKeyFile())` can throw `InvalidPathException` (unchecked) if the configured string contains characters that are invalid on the OS filesystem (e.g. null bytes). `resolveAttributionParams` validates that the value is non-empty, but not that it is a syntactically valid path. The exception would surface opaquely during a credential-cache miss rather than at startup. Maybe worth moving the `Path.of(...)` call into `resolveAttributionParams` (or whenever we do the initial param validations) so the error is caught at construction time alongside the other config validation.



##########
polaris-core/src/main/java/org/apache/polaris/core/config/FeatureConfiguration.java:
##########
@@ -209,6 +209,64 @@ public static void enforceFeatureEnabledOrThrow(
               .defaultValue(List.<String>of())
               .buildFeatureConfiguration();
 
+  // ---------------------------------------------------------------------------
+  // GCS principal attribution via Workload Identity Federation
+  //
+  // GCP downscoped credentials have no session-tag mechanism (unlike AWS STS), and custom audit
+  // headers only reach GCS audit logs if the client forwards them. To attribute GCS data access
+  // to the Polaris principal for ANY client, credential vending can chain
+  // catalog-signed JWT -> STS token exchange -> per-catalog service-account impersonation, so the
+  // principal appears in serviceAccountDelegationInfo of every GCS Data Access audit log entry.
+  //
+  // Attribution activates automatically once the audience, issuer, and signing key file are all
+  // set (no on/off flag); it additionally requires a gcpServiceAccount on the storage config.
+  // ---------------------------------------------------------------------------
+
+  public static final FeatureConfiguration<String> GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE =

Review Comment:
   I agree with @obelix74 on keeping this as a realm-level param. I'm not sure I see a reasonable use case currently to need to control WIF Attribution at a finer-grain than realm IMO. However, I do find it odd that you're checking if all the arguments are present at the time of creating a StorageIntegration rather than at server start time...
   
   Is there a way that a Polaris admin can change the settings for a realm during a running server instance?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "obelix74 (via GitHub)" <gi...@apache.org>.
obelix74 commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3433034039


##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpFederatedCredentialsExchanger.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.core.storage.gcp;
+
+import com.auth0.jwt.JWT;
+import com.auth0.jwt.JWTCreator;
+import com.auth0.jwt.algorithms.Algorithm;
+import com.google.auth.http.HttpTransportFactory;
+import com.google.auth.oauth2.GoogleCredentials;
+import com.google.auth.oauth2.IdentityPoolCredentials;
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.security.KeyFactory;
+import java.security.interfaces.RSAPrivateKey;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.Base64;
+import java.util.Date;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Produces a GCP federated {@link GoogleCredentials} whose identity carries {@code
+ * <realm>/<principal>}, so that GCS Data Access audit logs attribute access to the requesting
+ * Polaris principal. This is the GCP counterpart of AWS STS session tags.
+ *
+ * <p>The federated credential is an {@link IdentityPoolCredentials} backed by a programmatic
+ * subject-token supplier: on each token refresh google-auth invokes the supplier, which mints a
+ * short-lived RS256 JWT ({@code sub = <realm>/<principal>}, {@code realm} claim), and exchanges it
+ * at the Workload Identity Pool provider's STS endpoint. The provider maps {@code google.subject =
+ * assertion.sub} and {@code attribute.realm = assertion.realm}; per-realm {@code attribute.realm}
+ * IAM bindings then enforce that a realm-A identity can only impersonate realm-A's service account.
+ * The returned credential is intended to be used as the source for tenant service-account
+ * impersonation (see {@link GcpCredentialsStorageIntegration}).
+ *
+ * <p>Network note: this performs an STS token exchange against {@code sts.googleapis.com} in
+ * addition to the existing {@code iamcredentials.googleapis.com} and {@code storage.googleapis.com}
+ * traffic.
+ */
+public class GcpFederatedCredentialsExchanger {
+
+  static final String STS_TOKEN_URL = "https://sts.googleapis.com/v1/token";
+  static final String SUBJECT_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:jwt";
+  static final String CLOUD_PLATFORM_SCOPE = "https://www.googleapis.com/auth/cloud-platform";
+
+  /** Attribution JWTs are single-purpose and short-lived. */
+  static final Duration JWT_LIFETIME = Duration.ofMinutes(5);
+
+  /**
+   * JVM-wide cache of parsed signing keys, keyed by file path. The key file is a stable pod-mounted
+   * secret; parsing it (disk read + {@link KeyFactory}) once per path amortizes across vends rather
+   * than re-reading on every credential-cache miss. Key rotation is delivered by a process restart
+   * (the secret is mounted at startup), which clears this cache.
+   */
+  private static final ConcurrentHashMap<Path, RSAPrivateKey> SIGNING_KEY_CACHE =
+      new ConcurrentHashMap<>();
+
+  private final String issuer;
+  private final String wifAudience;
+  private final Path signingKeyPath;
+  private final String signingKeyId;
+  private final HttpTransportFactory transportFactory;
+
+  public GcpFederatedCredentialsExchanger(
+      String issuer,
+      String wifAudience,
+      Path signingKeyPath,
+      String signingKeyId,
+      HttpTransportFactory transportFactory) {
+    this.issuer = issuer;
+    this.wifAudience = wifAudience;
+    this.signingKeyPath = signingKeyPath;
+    this.signingKeyId = signingKeyId;
+    this.transportFactory = transportFactory;
+  }
+
+  /**
+   * Builds a federated credential whose subject is {@code <realm>/<principal>}.
+   *
+   * @param subject the attribution subject, {@code <realm>/<principal>} (see {@link
+   *     GcpAttributionSubjectBuilder})
+   * @param realm the realm identifier, emitted as the {@code realm} claim for {@code
+   *     attribute.realm} mapping
+   * @return federated credentials suitable as the source for tenant-SA impersonation
+   */
+  public GoogleCredentials federatedCredentials(String subject, String realm) {
+    return IdentityPoolCredentials.newBuilder()
+        .setHttpTransportFactory(transportFactory)
+        .setAudience(wifAudience)
+        .setSubjectTokenType(SUBJECT_TOKEN_TYPE)
+        .setTokenUrl(STS_TOKEN_URL)
+        .setScopes(List.of(CLOUD_PLATFORM_SCOPE))
+        .setSubjectTokenSupplier(context -> mintAttributionJwt(subject, realm))
+        .build();
+  }
+
+  @VisibleForTesting
+  String mintAttributionJwt(String subject, String realm) throws IOException {
+    Instant now = Instant.now();
+    JWTCreator.Builder builder =
+        JWT.create()
+            .withIssuer(issuer)
+            .withSubject(subject)
+            .withAudience(wifAudience)
+            .withClaim("realm", realm)
+            .withIssuedAt(Date.from(now))
+            .withExpiresAt(Date.from(now.plus(JWT_LIFETIME)))
+            .withJWTId(UUID.randomUUID().toString());
+    // Set the kid header so the WIF provider can pick the right public key from its JWKS during
+    // rotation (when the JWKS holds both the old and new keys). Omitted only for a single-key JWKS.
+    if (signingKeyId != null && !signingKeyId.isEmpty()) {
+      builder.withKeyId(signingKeyId);
+    }
+    return builder.sign(Algorithm.RSA256(null, loadSigningKey()));

Review Comment:
   Added exactly that comment: `// null public key: sign-only; verification is the WIF provider's responsibility`



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpFederatedCredentialsExchanger.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.core.storage.gcp;
+
+import com.auth0.jwt.JWT;
+import com.auth0.jwt.JWTCreator;
+import com.auth0.jwt.algorithms.Algorithm;
+import com.google.auth.http.HttpTransportFactory;
+import com.google.auth.oauth2.GoogleCredentials;
+import com.google.auth.oauth2.IdentityPoolCredentials;
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.security.KeyFactory;
+import java.security.interfaces.RSAPrivateKey;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.Base64;
+import java.util.Date;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Produces a GCP federated {@link GoogleCredentials} whose identity carries {@code
+ * <realm>/<principal>}, so that GCS Data Access audit logs attribute access to the requesting
+ * Polaris principal. This is the GCP counterpart of AWS STS session tags.
+ *
+ * <p>The federated credential is an {@link IdentityPoolCredentials} backed by a programmatic
+ * subject-token supplier: on each token refresh google-auth invokes the supplier, which mints a
+ * short-lived RS256 JWT ({@code sub = <realm>/<principal>}, {@code realm} claim), and exchanges it
+ * at the Workload Identity Pool provider's STS endpoint. The provider maps {@code google.subject =
+ * assertion.sub} and {@code attribute.realm = assertion.realm}; per-realm {@code attribute.realm}
+ * IAM bindings then enforce that a realm-A identity can only impersonate realm-A's service account.
+ * The returned credential is intended to be used as the source for tenant service-account
+ * impersonation (see {@link GcpCredentialsStorageIntegration}).
+ *
+ * <p>Network note: this performs an STS token exchange against {@code sts.googleapis.com} in
+ * addition to the existing {@code iamcredentials.googleapis.com} and {@code storage.googleapis.com}
+ * traffic.
+ */
+public class GcpFederatedCredentialsExchanger {
+
+  static final String STS_TOKEN_URL = "https://sts.googleapis.com/v1/token";
+  static final String SUBJECT_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:jwt";
+  static final String CLOUD_PLATFORM_SCOPE = "https://www.googleapis.com/auth/cloud-platform";
+
+  /** Attribution JWTs are single-purpose and short-lived. */
+  static final Duration JWT_LIFETIME = Duration.ofMinutes(5);
+
+  /**
+   * JVM-wide cache of parsed signing keys, keyed by file path. The key file is a stable pod-mounted
+   * secret; parsing it (disk read + {@link KeyFactory}) once per path amortizes across vends rather
+   * than re-reading on every credential-cache miss. Key rotation is delivered by a process restart
+   * (the secret is mounted at startup), which clears this cache.
+   */
+  private static final ConcurrentHashMap<Path, RSAPrivateKey> SIGNING_KEY_CACHE =
+      new ConcurrentHashMap<>();
+
+  private final String issuer;
+  private final String wifAudience;
+  private final Path signingKeyPath;
+  private final String signingKeyId;
+  private final HttpTransportFactory transportFactory;
+
+  public GcpFederatedCredentialsExchanger(
+      String issuer,
+      String wifAudience,
+      Path signingKeyPath,
+      String signingKeyId,
+      HttpTransportFactory transportFactory) {
+    this.issuer = issuer;
+    this.wifAudience = wifAudience;
+    this.signingKeyPath = signingKeyPath;
+    this.signingKeyId = signingKeyId;
+    this.transportFactory = transportFactory;
+  }
+
+  /**
+   * Builds a federated credential whose subject is {@code <realm>/<principal>}.
+   *
+   * @param subject the attribution subject, {@code <realm>/<principal>} (see {@link
+   *     GcpAttributionSubjectBuilder})
+   * @param realm the realm identifier, emitted as the {@code realm} claim for {@code
+   *     attribute.realm} mapping
+   * @return federated credentials suitable as the source for tenant-SA impersonation
+   */
+  public GoogleCredentials federatedCredentials(String subject, String realm) {
+    return IdentityPoolCredentials.newBuilder()
+        .setHttpTransportFactory(transportFactory)
+        .setAudience(wifAudience)
+        .setSubjectTokenType(SUBJECT_TOKEN_TYPE)
+        .setTokenUrl(STS_TOKEN_URL)
+        .setScopes(List.of(CLOUD_PLATFORM_SCOPE))
+        .setSubjectTokenSupplier(context -> mintAttributionJwt(subject, realm))
+        .build();
+  }
+
+  @VisibleForTesting
+  String mintAttributionJwt(String subject, String realm) throws IOException {
+    Instant now = Instant.now();
+    JWTCreator.Builder builder =
+        JWT.create()
+            .withIssuer(issuer)
+            .withSubject(subject)
+            .withAudience(wifAudience)
+            .withClaim("realm", realm)
+            .withIssuedAt(Date.from(now))
+            .withExpiresAt(Date.from(now.plus(JWT_LIFETIME)))
+            .withJWTId(UUID.randomUUID().toString());
+    // Set the kid header so the WIF provider can pick the right public key from its JWKS during
+    // rotation (when the JWKS holds both the old and new keys). Omitted only for a single-key JWKS.
+    if (signingKeyId != null && !signingKeyId.isEmpty()) {
+      builder.withKeyId(signingKeyId);
+    }
+    return builder.sign(Algorithm.RSA256(null, loadSigningKey()));
+  }
+
+  private RSAPrivateKey loadSigningKey() throws IOException {
+    try {
+      return SIGNING_KEY_CACHE.computeIfAbsent(
+          signingKeyPath,
+          path -> {
+            try {
+              return readPkcs8PrivateKey(path);
+            } catch (IOException e) {
+              throw new UncheckedIOException(e);
+            }
+          });
+    } catch (UncheckedIOException e) {
+      throw e.getCause();
+    }
+  }
+
+  /** Reads an RSA private key from a PKCS#8 PEM file. */
+  @VisibleForTesting
+  static RSAPrivateKey readPkcs8PrivateKey(Path pemPath) throws IOException {
+    String pem = Files.readString(pemPath);
+    String base64 =
+        pem.replaceAll("-----BEGIN [A-Z ]+-----", "")

Review Comment:
   Done. Added an explicit check before stripping the PEM headers: if the file contains `BEGIN RSA PRIVATE KEY` we now throw an `IOException` immediately with a message like "PKCS#1 key format (BEGIN RSA PRIVATE KEY) is not supported ... convert to PKCS#8 with: openssl pkcs8 -topk8 -nocrypt -in key.pem -out key-pkcs8.pem". Added a test (`readPkcs8PrivateKeyRejectsPkcs1Format`) to cover this path.



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegration.java:
##########
@@ -123,13 +135,75 @@ public GcpCredentialsStorageIntegration(
       GcpStorageConfigurationInfo storageConfig,
       RealmConfig realmConfig,
       GcpCredentialOps credentialOps) {
+    this(
+        sourceCredentials,
+        transportFactory,
+        cache,
+        storageConfig,
+        realmConfig,
+        credentialOps,
+        resolveAttributionParams(realmConfig));
+  }
+
+  /**
+   * Full constructor accepting pre-resolved attribution params. Downstream integrations can pass
+   * their own {@link GcpAttributionParams} without depending on {@link
+   * org.apache.polaris.core.config.FeatureConfiguration}.
+   */
+  public GcpCredentialsStorageIntegration(
+      GoogleCredentials sourceCredentials,
+      HttpTransportFactory transportFactory,
+      org.apache.polaris.core.storage.cache.StorageCredentialCache cache,
+      GcpStorageConfigurationInfo storageConfig,
+      RealmConfig realmConfig,
+      GcpCredentialOps credentialOps,
+      Optional<GcpAttributionParams> attributionParams) {
     super(cache, realmConfig, storageConfig);
     // Needed for when environment variable GOOGLE_APPLICATION_CREDENTIALS points to google service
     // account key json
     this.sourceCredentials =
         sourceCredentials.createScoped("https://www.googleapis.com/auth/cloud-platform");
     this.transportFactory = transportFactory;
     this.credentialOps = credentialOps;
+    this.attributionParams = attributionParams;
+  }
+
+  /**
+   * Resolves {@link GcpAttributionParams} from realm config. Returns empty when attribution is
+   * disabled; throws {@link IllegalStateException} when enabled but misconfigured.
+   */
+  public static Optional<GcpAttributionParams> resolveAttributionParams(RealmConfig realmConfig) {

Review Comment:
   It's static because it's called in constructor-chaining expressions (`this(..., resolveAttributionParams(realmConfig))`). In Java you cannot call instance methods in a `this(...)` argument list before the object is initialized, so it must be static. Added a Javadoc note to that effect.



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegration.java:
##########
@@ -176,17 +250,30 @@ private GcpStorageCredentialCacheKey buildCacheKey(
       @NonNull Set<String> writeLocations,
       @NonNull Optional<String> refreshEndpoint,
       @NonNull CredentialVendingContext context) {
+    // Principal attribution makes the vended token per-principal, so the principal must
+    // participate in cache identity; otherwise it is left empty to preserve cross-principal cache
+    // reuse. Attribution requires a service account to impersonate and a principal to attribute.
+    String principalName = "";
+    Optional<GcpAttributionParams> resolvedAttributionParams = Optional.empty();
+    if (attributionParams.isPresent() && storageConfig().getGcpServiceAccount() != null) {

Review Comment:
   Added a WARN log: "GCS principal attribution is enabled but no gcpServiceAccount is configured on the StorageConfiguration; falling back to non-attributed credentials".



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegration.java:
##########
@@ -176,17 +250,30 @@ private GcpStorageCredentialCacheKey buildCacheKey(
       @NonNull Set<String> writeLocations,
       @NonNull Optional<String> refreshEndpoint,
       @NonNull CredentialVendingContext context) {
+    // Principal attribution makes the vended token per-principal, so the principal must
+    // participate in cache identity; otherwise it is left empty to preserve cross-principal cache
+    // reuse. Attribution requires a service account to impersonate and a principal to attribute.
+    String principalName = "";
+    Optional<GcpAttributionParams> resolvedAttributionParams = Optional.empty();
+    if (attributionParams.isPresent() && storageConfig().getGcpServiceAccount() != null) {
+      principalName = context.principalName().orElse("");
+      if (!principalName.isEmpty()) {

Review Comment:
   Agreed it shouldn't happen in practice. Added a WARN log here: "GCS principal attribution is enabled but no principal name is present in the credential vending context; falling back to non-attributed credentials".



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on PR #4707:
URL: https://github.com/apache/polaris/pull/4707#issuecomment-4769412118

   Related `dev` thread: https://lists.apache.org/thread/yq1sz8y0nkfhloycw9lrqtc9k084ln2f


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "obelix74 (via GitHub)" <gi...@apache.org>.
obelix74 commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3455139236


##########
polaris-core/src/test/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegrationTest.java:
##########
@@ -502,4 +515,147 @@ private static String expressionAt(JsonNode parsedRules, int ruleIndex) {
         .path("expression")
         .asText();
   }
+
+  private static RealmConfig configWith(Map<String, String> values) {
+    RealmConfigurationSource source = (rc, name) -> values.get(name);
+    return new RealmConfigImpl(source, () -> "test-realm");
+  }
+
+  @Test
+  void resolveAttributionParamsDisabledByDefault() {
+    assertThat(GcpCredentialsStorageIntegration.resolveAttributionParams(EMPTY_REALM_CONFIG))
+        .isEmpty();
+  }
+
+  @Test
+  void resolveAttributionParamsReturnsParamsWhenFullyConfigured() {
+    RealmConfig config =
+        configWith(
+            Map.of(
+                "GCS_PRINCIPAL_ATTRIBUTION_ENABLED", "true",
+                "GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE",
+                    "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/p/providers/pr",
+                "GCS_PRINCIPAL_ATTRIBUTION_TOKEN_ISSUER", "https://issuer.example.com",
+                "GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_FILE", "/tmp/key.pem"));
+    Optional<GcpAttributionParams> result =
+        GcpCredentialsStorageIntegration.resolveAttributionParams(config);
+    assertThat(result).isPresent();
+    assertThat(result.get().wifAudience())
+        .isEqualTo(
+            "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/p/providers/pr");
+    assertThat(result.get().tokenIssuer()).isEqualTo("https://issuer.example.com");
+    assertThat(result.get().signingKeyFile()).isEqualTo("/tmp/key.pem");
+  }
+
+  @Test
+  void resolveAttributionParamsThrowsWhenEnabledButAudienceMissing() {
+    RealmConfig config =
+        configWith(
+            Map.of(
+                "GCS_PRINCIPAL_ATTRIBUTION_ENABLED", "true",
+                "GCS_PRINCIPAL_ATTRIBUTION_TOKEN_ISSUER", "https://issuer.example.com",
+                "GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_FILE", "/tmp/key.pem"));
+    assertThatThrownBy(() -> GcpCredentialsStorageIntegration.resolveAttributionParams(config))
+        .isInstanceOf(IllegalStateException.class)
+        .hasMessageContaining("GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE");
+  }
+
+  @Test
+  void resolveAttributionParamsThrowsWhenMultipleRequiredParamsMissing() {
+    RealmConfig config = configWith(Map.of("GCS_PRINCIPAL_ATTRIBUTION_ENABLED", "true"));
+    assertThatThrownBy(() -> GcpCredentialsStorageIntegration.resolveAttributionParams(config))
+        .isInstanceOf(IllegalStateException.class)
+        .hasMessageContaining("GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE")
+        .hasMessageContaining("GCS_PRINCIPAL_ATTRIBUTION_TOKEN_ISSUER")
+        .hasMessageContaining("GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_FILE");
+  }
+
+  @Test
+  void testWifAttributionFlowUsesFederatedCredentials(@TempDir Path tempDir) throws Exception {
+    String serviceAccount = "test-sa@project.iam.gserviceaccount.com";
+    GcpStorageConfigurationInfo config =
+        GcpStorageConfigurationInfo.builder()
+            .addAllAllowedLocations(List.of("gs://bucket/path"))
+            .gcpServiceAccount(serviceAccount)
+            .build();
+
+    AtomicReference<GoogleCredentials> capturedSource = new AtomicReference<>();
+
+    IamCredentialsClient mockIamClient = Mockito.mock(IamCredentialsClient.class);
+    GenerateAccessTokenResponse mockResponse =
+        GenerateAccessTokenResponse.newBuilder()
+            .setAccessToken("wif-impersonated-token")
+            .setExpireTime(
+                Timestamp.newBuilder().setSeconds(System.currentTimeMillis() / 1000 + 3600).build())
+            .build();
+    Mockito.when(mockIamClient.generateAccessToken(Mockito.any(GenerateAccessTokenRequest.class)))
+        .thenReturn(mockResponse);
+
+    GoogleCredentials mockCreds = Mockito.mock(GoogleCredentials.class);
+    Mockito.when(mockCreds.createScoped(Mockito.any(String.class))).thenReturn(mockCreds);
+
+    GcpCredentialOps testOps =
+        new GcpCredentialOps() {
+          @Override
+          public IamCredentialsClient createIamCredentialsClient(GoogleCredentials credentials) {
+            capturedSource.set(credentials);
+            return mockIamClient;
+          }
+
+          @Override
+          public AccessToken refreshAccessToken(DownscopedCredentials credentials) {
+            return new AccessToken("downscoped-token", new Date());
+          }
+        };
+
+    KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
+    gen.initialize(2048);
+    KeyPair keyPair = gen.generateKeyPair();
+    String pem =
+        "-----BEGIN PRIVATE KEY-----\n"
+            + Base64.getMimeEncoder(64, "\n".getBytes(UTF_8))
+                .encodeToString(keyPair.getPrivate().getEncoded())
+            + "\n-----END PRIVATE KEY-----\n";
+    Path keyFile = tempDir.resolve("attribution-key.pem");
+    Files.writeString(keyFile, pem);
+
+    GcpAttributionParams params =
+        GcpAttributionParams.of(
+            "https://issuer.example.com",
+            "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/p/providers/pr",
+            keyFile.toString(),
+            "key-1");
+
+    GcpCredentialsStorageIntegration integration =
+        new GcpCredentialsStorageIntegration(
+            mockCreds,
+            ServiceOptions.getFromServiceLoader(HttpTransportFactory.class, NetHttpTransport::new),
+            null,
+            config,
+            EMPTY_REALM_CONFIG,
+            testOps,
+            Optional.of(params));
+
+    integration.getStorageAccessConfig(
+        toGrants(
+            Set.of("gs://bucket/path"), Set.of("gs://bucket/path"), Set.of("gs://bucket/path")),
+        Optional.empty(),
+        CredentialVendingContext.builder()
+            .realm(Optional.of("realm1"))
+            .principalName(Optional.of("principal1"))
+            .build());
+
+    Mockito.verify(mockIamClient)
+        .generateAccessToken(
+            Mockito.argThat(
+                request ->
+                    request
+                        .getName()
+                        .equals(
+                            GcpCredentialsStorageIntegration.SERVICE_ACCOUNT_PREFIX

Review Comment:
   @adnanhemani Can you please approve the PR if you are ok with this?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "sungwy (via GitHub)" <gi...@apache.org>.
sungwy commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3424956527


##########
polaris-core/src/main/java/org/apache/polaris/core/config/FeatureConfiguration.java:
##########
@@ -209,6 +209,64 @@ public static void enforceFeatureEnabledOrThrow(
               .defaultValue(List.<String>of())
               .buildFeatureConfiguration();
 
+  // ---------------------------------------------------------------------------
+  // GCS principal attribution via Workload Identity Federation
+  //
+  // GCP downscoped credentials have no session-tag mechanism (unlike AWS STS), and custom audit
+  // headers only reach GCS audit logs if the client forwards them. To attribute GCS data access
+  // to the Polaris principal for ANY client, credential vending can chain
+  // catalog-signed JWT -> STS token exchange -> per-catalog service-account impersonation, so the
+  // principal appears in serviceAccountDelegationInfo of every GCS Data Access audit log entry.
+  //
+  // Attribution activates automatically once the audience, issuer, and signing key file are all
+  // set (no on/off flag); it additionally requires a gcpServiceAccount on the storage config.
+  // ---------------------------------------------------------------------------
+
+  public static final FeatureConfiguration<String> GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE =

Review Comment:
   A `StorageConfiguration` is defined per catalog (and already carries the `gcpServiceAccount`). Is there a reason the attribution config is configured per realm rather than per SA / `StorageConfiguration`? I'm wondering how it'd handle a realm with catalogs in different GCP projects. WDYT?



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegration.java:
##########
@@ -176,17 +178,57 @@ private GcpStorageCredentialCacheKey buildCacheKey(
       @NonNull Set<String> writeLocations,
       @NonNull Optional<String> refreshEndpoint,
       @NonNull CredentialVendingContext context) {
+    // Principal attribution makes the vended token per-principal, so the principal must
+    // participate in cache identity; otherwise it is left empty to preserve cross-principal cache
+    // reuse. Attribution requires a service account to impersonate and a principal to attribute.
+    String principalName = "";
+    Optional<GcpAttributionParams> attributionParams = Optional.empty();
+    if (principalAttributionConfigured(realmConfig())
+        && storageConfig().getGcpServiceAccount() != null) {
+      principalName = context.principalName().orElse("");
+      if (!principalName.isEmpty()) {
+        attributionParams =
+            Optional.of(
+                GcpAttributionParams.of(
+                    realmConfig()
+                        .getConfig(FeatureConfiguration.GCS_PRINCIPAL_ATTRIBUTION_TOKEN_ISSUER),
+                    realmConfig()
+                        .getConfig(FeatureConfiguration.GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE),
+                    realmConfig()
+                        .getConfig(FeatureConfiguration.GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_FILE),
+                    realmConfig()
+                        .getConfig(FeatureConfiguration.GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_ID)));
+      }
+    }
     return GcpStorageCredentialCacheKey.of(
         context.realm().orElse(""),
         storageConfig(),
         readLocations,
         listLocations,
         writeLocations,
         refreshEndpoint,
+        principalName,
         sourceCredentials,
         transportFactory,
         realmConfig(),
-        credentialOps);
+        credentialOps,
+        attributionParams);
+  }
+
+  /**
+   * Returns true when GCS principal attribution is fully configured (WIF audience, token issuer,
+   * and signing key file all set). There is intentionally no separate on/off flag.
+   */
+  private static boolean principalAttributionConfigured(RealmConfig realmConfig) {

Review Comment:
   Could we make this an explicit opt-in flag (like `GCS_PRINCIPAL_ATTRIBUTION_ENABLED`) with validation, rather than activating implicitly once all three are set? Today if an operator sets only some of them (typo, or misses one), this returns false and we silently fall back to the default vending path. This means a misconfigured setup looks fine on startup but attribution is quietly off. An explicit enable flag + a startup error when required values are missing would make a misconfiguration obvious.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "obelix74 (via GitHub)" <gi...@apache.org>.
obelix74 commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3425648913


##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegration.java:
##########
@@ -176,17 +178,57 @@ private GcpStorageCredentialCacheKey buildCacheKey(
       @NonNull Set<String> writeLocations,
       @NonNull Optional<String> refreshEndpoint,
       @NonNull CredentialVendingContext context) {
+    // Principal attribution makes the vended token per-principal, so the principal must
+    // participate in cache identity; otherwise it is left empty to preserve cross-principal cache
+    // reuse. Attribution requires a service account to impersonate and a principal to attribute.
+    String principalName = "";
+    Optional<GcpAttributionParams> attributionParams = Optional.empty();
+    if (principalAttributionConfigured(realmConfig())
+        && storageConfig().getGcpServiceAccount() != null) {
+      principalName = context.principalName().orElse("");
+      if (!principalName.isEmpty()) {
+        attributionParams =
+            Optional.of(
+                GcpAttributionParams.of(
+                    realmConfig()
+                        .getConfig(FeatureConfiguration.GCS_PRINCIPAL_ATTRIBUTION_TOKEN_ISSUER),
+                    realmConfig()
+                        .getConfig(FeatureConfiguration.GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE),
+                    realmConfig()
+                        .getConfig(FeatureConfiguration.GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_FILE),
+                    realmConfig()
+                        .getConfig(FeatureConfiguration.GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_ID)));
+      }
+    }
     return GcpStorageCredentialCacheKey.of(
         context.realm().orElse(""),
         storageConfig(),
         readLocations,
         listLocations,
         writeLocations,
         refreshEndpoint,
+        principalName,
         sourceCredentials,
         transportFactory,
         realmConfig(),
-        credentialOps);
+        credentialOps,
+        attributionParams);
+  }
+
+  /**
+   * Returns true when GCS principal attribution is fully configured (WIF audience, token issuer,
+   * and signing key file all set). There is intentionally no separate on/off flag.
+   */
+  private static boolean principalAttributionConfigured(RealmConfig realmConfig) {

Review Comment:
   Good point โ€” done. Added an explicit `GCS_PRINCIPAL_ATTRIBUTION_ENABLED` boolean flag (default `false`). When `true`, `principalAttributionConfigured()` validates that `WIF_AUDIENCE`, `TOKEN_ISSUER`, and `SIGNING_KEY_FILE` are all set and throws `IllegalStateException` naming the missing values if not โ€” so a misconfigured deployment fails fast on the first credential-vending attempt rather than silently producing unattributed tokens. Config docs regenerated. Pushed in 6906ebc8e.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "obelix74 (via GitHub)" <gi...@apache.org>.
obelix74 commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3433033153


##########
polaris-core/src/main/java/org/apache/polaris/core/config/FeatureConfiguration.java:
##########
@@ -209,6 +209,82 @@ public static void enforceFeatureEnabledOrThrow(
               .defaultValue(List.<String>of())
               .buildFeatureConfiguration();
 
+  // ---------------------------------------------------------------------------
+  // GCS principal attribution via Workload Identity Federation

Review Comment:
   Done โ€” removed the block comment. The individual `description` fields on each config constant are sufficient.



##########
polaris-core/src/main/java/org/apache/polaris/core/config/FeatureConfiguration.java:
##########
@@ -209,6 +209,82 @@ public static void enforceFeatureEnabledOrThrow(
               .defaultValue(List.<String>of())
               .buildFeatureConfiguration();
 
+  // ---------------------------------------------------------------------------
+  // GCS principal attribution via Workload Identity Federation
+  //
+  // GCP downscoped credentials have no session-tag mechanism (unlike AWS STS), and custom audit
+  // headers only reach GCS audit logs if the client forwards them. To attribute GCS data access
+  // to the Polaris principal for ANY client, credential vending can chain
+  // catalog-signed JWT -> STS token exchange -> per-catalog service-account impersonation, so the
+  // principal appears in serviceAccountDelegationInfo of every GCS Data Access audit log entry.
+  //
+  // Attribution must be explicitly enabled via GCS_PRINCIPAL_ATTRIBUTION_ENABLED. When enabled,
+  // WIF_AUDIENCE, TOKEN_ISSUER, and SIGNING_KEY_FILE are all required; Polaris will throw at the
+  // first credential-vending attempt if any are missing. Additionally requires a gcpServiceAccount
+  // on the per-catalog StorageConfiguration.
+  // ---------------------------------------------------------------------------
+
+  public static final FeatureConfiguration<Boolean> GCS_PRINCIPAL_ATTRIBUTION_ENABLED =
+      PolarisConfiguration.<Boolean>builder()
+          .key("GCS_PRINCIPAL_ATTRIBUTION_ENABLED")
+          .description(
+              "Enables GCS principal attribution via Workload Identity Federation.\n"
+                  + "When true, credential vending chains a catalog-signed JWT through an STS token\n"
+                  + "exchange and service-account impersonation so the Polaris principal appears in GCS\n"
+                  + "Data Access audit logs (serviceAccountDelegationInfo.principalSubject).\n"
+                  + "Requires GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE, GCS_PRINCIPAL_ATTRIBUTION_TOKEN_ISSUER,\n"
+                  + "and GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_FILE to also be set;\n"
+                  + "a missing required value is a fatal configuration error.\n"
+                  + "Also requires a gcpServiceAccount on the catalog StorageConfiguration.\n"
+                  + "Default: false (attribution disabled).")
+          .defaultValue(false)
+          .buildFeatureConfiguration();
+
+  public static final FeatureConfiguration<String> GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE =
+      PolarisConfiguration.<String>builder()
+          .key("GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE")
+          .description(
+              "Full resource name of the Workload Identity Pool provider used for GCS principal\n"
+                  + "attribution, e.g.\n"
+                  + "//iam.googleapis.com/projects/<num>/locations/global/workloadIdentityPools/<pool>/providers/<provider>.\n"
+                  + "Used as both the attribution JWT 'aud' claim and the STS token-exchange audience.\n"
+                  + "Empty (default) disables principal attribution.")

Review Comment:
   Fixed in 79fa1a380. Updated all three descriptions (`WIF_AUDIENCE`, `TOKEN_ISSUER`, `SIGNING_KEY_FILE`) to say "Required when `GCS_PRINCIPAL_ATTRIBUTION_ENABLED=true`; ignored otherwise."



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpAttributionSubjectBuilder.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.core.storage.gcp;
+
+/**
+ * Builds the {@code sub} claim for GCS principal-attribution JWTs as {@code <realm>/<principal>},
+ * within GCP's 127-character {@code google.subject} limit.
+ *
+ * <p>The character budget mirrors the AWS session-name builder: one character is reserved for the
+ * separator, then each field receives an equal share of the remainder, and budget unused by a short
+ * field flows to the other. ISO control characters and the {@code /} separator are stripped from
+ * each field so the subject stays unambiguously parseable, and the {@code unknown} placeholder
+ * substitutes null/empty fields so the subject shape stays stable.
+ */
+public final class GcpAttributionSubjectBuilder {
+
+  /** GCP limit for the {@code google.subject} attribute of a federated identity. */
+  public static final int MAX_SUBJECT_LENGTH = 127;
+
+  static final String SEPARATOR = "/";
+
+  static final String VALUE_UNKNOWN = "unknown";
+
+  private GcpAttributionSubjectBuilder() {}
+
+  /**
+   * Builds the attribution subject {@code <realm>/<principal>}, guaranteed to be at most {@value
+   * #MAX_SUBJECT_LENGTH} characters.
+   *
+   * @param realm the realm identifier (gets first-half budget priority)
+   * @param principalName the Polaris principal name
+   * @return the subject string
+   */
+  public static String buildSubject(String realm, String principalName) {
+    String cleanRealm = sanitize(realm);
+    String cleanPrincipal = sanitize(principalName);
+
+    int budget = MAX_SUBJECT_LENGTH - SEPARATOR.length();
+    int remaining = budget;
+
+    int realmAlloc = remaining / 2;
+    int realmUsed = Math.min(cleanRealm.length(), realmAlloc);
+    remaining -= realmUsed;
+
+    int principalUsed = Math.min(cleanPrincipal.length(), remaining);
+    remaining -= principalUsed;
+
+    // Carry-forward: if the principal left budget unused, the realm may take more than its
+    // initial half-share.
+    int realmFinal = Math.min(cleanRealm.length(), realmUsed + remaining);

Review Comment:
   Updated the comment to say: "Unused budget from either field flows to the other. A short realm gives principal more room via the larger `remaining`, and a short principal gives realm more room here."



##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpFederatedCredentialsExchanger.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.core.storage.gcp;
+
+import com.auth0.jwt.JWT;
+import com.auth0.jwt.JWTCreator;
+import com.auth0.jwt.algorithms.Algorithm;
+import com.google.auth.http.HttpTransportFactory;
+import com.google.auth.oauth2.GoogleCredentials;
+import com.google.auth.oauth2.IdentityPoolCredentials;
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.security.KeyFactory;
+import java.security.interfaces.RSAPrivateKey;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.Base64;
+import java.util.Date;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Produces a GCP federated {@link GoogleCredentials} whose identity carries {@code
+ * <realm>/<principal>}, so that GCS Data Access audit logs attribute access to the requesting
+ * Polaris principal. This is the GCP counterpart of AWS STS session tags.
+ *
+ * <p>The federated credential is an {@link IdentityPoolCredentials} backed by a programmatic
+ * subject-token supplier: on each token refresh google-auth invokes the supplier, which mints a
+ * short-lived RS256 JWT ({@code sub = <realm>/<principal>}, {@code realm} claim), and exchanges it
+ * at the Workload Identity Pool provider's STS endpoint. The provider maps {@code google.subject =
+ * assertion.sub} and {@code attribute.realm = assertion.realm}; per-realm {@code attribute.realm}
+ * IAM bindings then enforce that a realm-A identity can only impersonate realm-A's service account.
+ * The returned credential is intended to be used as the source for tenant service-account
+ * impersonation (see {@link GcpCredentialsStorageIntegration}).
+ *
+ * <p>Network note: this performs an STS token exchange against {@code sts.googleapis.com} in
+ * addition to the existing {@code iamcredentials.googleapis.com} and {@code storage.googleapis.com}
+ * traffic.
+ */
+public class GcpFederatedCredentialsExchanger {
+
+  static final String STS_TOKEN_URL = "https://sts.googleapis.com/v1/token";
+  static final String SUBJECT_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:jwt";
+  static final String CLOUD_PLATFORM_SCOPE = "https://www.googleapis.com/auth/cloud-platform";
+
+  /** Attribution JWTs are single-purpose and short-lived. */
+  static final Duration JWT_LIFETIME = Duration.ofMinutes(5);
+
+  /**
+   * JVM-wide cache of parsed signing keys, keyed by file path. The key file is a stable pod-mounted
+   * secret; parsing it (disk read + {@link KeyFactory}) once per path amortizes across vends rather
+   * than re-reading on every credential-cache miss. Key rotation is delivered by a process restart
+   * (the secret is mounted at startup), which clears this cache.
+   */
+  private static final ConcurrentHashMap<Path, RSAPrivateKey> SIGNING_KEY_CACHE =
+      new ConcurrentHashMap<>();

Review Comment:
   Added a note to the Javadoc: "In practice the map is bounded by the number of realms in the deployment โ€” each realm has at most one signing-key path โ€” so memory growth is proportional to realm count."



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "obelix74 (via GitHub)" <gi...@apache.org>.
obelix74 commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3440092468


##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegration.java:
##########
@@ -123,13 +135,75 @@ public GcpCredentialsStorageIntegration(
       GcpStorageConfigurationInfo storageConfig,
       RealmConfig realmConfig,
       GcpCredentialOps credentialOps) {
+    this(
+        sourceCredentials,
+        transportFactory,
+        cache,
+        storageConfig,
+        realmConfig,
+        credentialOps,
+        resolveAttributionParams(realmConfig));
+  }
+
+  /**
+   * Full constructor accepting pre-resolved attribution params. Downstream integrations can pass
+   * their own {@link GcpAttributionParams} without depending on {@link
+   * org.apache.polaris.core.config.FeatureConfiguration}.
+   */
+  public GcpCredentialsStorageIntegration(
+      GoogleCredentials sourceCredentials,
+      HttpTransportFactory transportFactory,
+      org.apache.polaris.core.storage.cache.StorageCredentialCache cache,

Review Comment:
   Fixed in fd5994fe9 โ€” added `import org.apache.polaris.core.storage.cache.StorageCredentialCache;` and replaced all three FQNs with the simple type name.



##########
polaris-core/src/test/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegrationTest.java:
##########
@@ -502,4 +515,147 @@ private static String expressionAt(JsonNode parsedRules, int ruleIndex) {
         .path("expression")
         .asText();
   }
+
+  private static RealmConfig configWith(Map<String, String> values) {
+    RealmConfigurationSource source = (rc, name) -> values.get(name);
+    return new RealmConfigImpl(source, () -> "test-realm");
+  }
+
+  @Test
+  void resolveAttributionParamsDisabledByDefault() {
+    assertThat(GcpCredentialsStorageIntegration.resolveAttributionParams(EMPTY_REALM_CONFIG))
+        .isEmpty();
+  }
+
+  @Test
+  void resolveAttributionParamsReturnsParamsWhenFullyConfigured() {
+    RealmConfig config =
+        configWith(
+            Map.of(
+                "GCS_PRINCIPAL_ATTRIBUTION_ENABLED", "true",
+                "GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE",
+                    "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/p/providers/pr",
+                "GCS_PRINCIPAL_ATTRIBUTION_TOKEN_ISSUER", "https://issuer.example.com",
+                "GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_FILE", "/tmp/key.pem"));
+    Optional<GcpAttributionParams> result =
+        GcpCredentialsStorageIntegration.resolveAttributionParams(config);
+    assertThat(result).isPresent();
+    assertThat(result.get().wifAudience())
+        .isEqualTo(
+            "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/p/providers/pr");
+    assertThat(result.get().tokenIssuer()).isEqualTo("https://issuer.example.com");
+    assertThat(result.get().signingKeyFile()).isEqualTo("/tmp/key.pem");
+  }
+
+  @Test
+  void resolveAttributionParamsThrowsWhenEnabledButAudienceMissing() {
+    RealmConfig config =
+        configWith(
+            Map.of(
+                "GCS_PRINCIPAL_ATTRIBUTION_ENABLED", "true",
+                "GCS_PRINCIPAL_ATTRIBUTION_TOKEN_ISSUER", "https://issuer.example.com",
+                "GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_FILE", "/tmp/key.pem"));
+    assertThatThrownBy(() -> GcpCredentialsStorageIntegration.resolveAttributionParams(config))
+        .isInstanceOf(IllegalStateException.class)
+        .hasMessageContaining("GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE");
+  }
+
+  @Test
+  void resolveAttributionParamsThrowsWhenMultipleRequiredParamsMissing() {
+    RealmConfig config = configWith(Map.of("GCS_PRINCIPAL_ATTRIBUTION_ENABLED", "true"));
+    assertThatThrownBy(() -> GcpCredentialsStorageIntegration.resolveAttributionParams(config))
+        .isInstanceOf(IllegalStateException.class)
+        .hasMessageContaining("GCS_PRINCIPAL_ATTRIBUTION_WIF_AUDIENCE")
+        .hasMessageContaining("GCS_PRINCIPAL_ATTRIBUTION_TOKEN_ISSUER")
+        .hasMessageContaining("GCS_PRINCIPAL_ATTRIBUTION_SIGNING_KEY_FILE");
+  }
+
+  @Test
+  void testWifAttributionFlowUsesFederatedCredentials(@TempDir Path tempDir) throws Exception {
+    String serviceAccount = "test-sa@project.iam.gserviceaccount.com";
+    GcpStorageConfigurationInfo config =
+        GcpStorageConfigurationInfo.builder()
+            .addAllAllowedLocations(List.of("gs://bucket/path"))
+            .gcpServiceAccount(serviceAccount)
+            .build();
+
+    AtomicReference<GoogleCredentials> capturedSource = new AtomicReference<>();
+
+    IamCredentialsClient mockIamClient = Mockito.mock(IamCredentialsClient.class);
+    GenerateAccessTokenResponse mockResponse =
+        GenerateAccessTokenResponse.newBuilder()
+            .setAccessToken("wif-impersonated-token")
+            .setExpireTime(
+                Timestamp.newBuilder().setSeconds(System.currentTimeMillis() / 1000 + 3600).build())
+            .build();
+    Mockito.when(mockIamClient.generateAccessToken(Mockito.any(GenerateAccessTokenRequest.class)))
+        .thenReturn(mockResponse);
+
+    GoogleCredentials mockCreds = Mockito.mock(GoogleCredentials.class);
+    Mockito.when(mockCreds.createScoped(Mockito.any(String.class))).thenReturn(mockCreds);
+
+    GcpCredentialOps testOps =
+        new GcpCredentialOps() {
+          @Override
+          public IamCredentialsClient createIamCredentialsClient(GoogleCredentials credentials) {
+            capturedSource.set(credentials);
+            return mockIamClient;
+          }
+
+          @Override
+          public AccessToken refreshAccessToken(DownscopedCredentials credentials) {
+            return new AccessToken("downscoped-token", new Date());
+          }
+        };
+
+    KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
+    gen.initialize(2048);
+    KeyPair keyPair = gen.generateKeyPair();
+    String pem =
+        "-----BEGIN PRIVATE KEY-----\n"
+            + Base64.getMimeEncoder(64, "\n".getBytes(UTF_8))
+                .encodeToString(keyPair.getPrivate().getEncoded())
+            + "\n-----END PRIVATE KEY-----\n";
+    Path keyFile = tempDir.resolve("attribution-key.pem");
+    Files.writeString(keyFile, pem);
+
+    GcpAttributionParams params =
+        GcpAttributionParams.of(
+            "https://issuer.example.com",
+            "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/p/providers/pr",
+            keyFile.toString(),
+            "key-1");
+
+    GcpCredentialsStorageIntegration integration =
+        new GcpCredentialsStorageIntegration(
+            mockCreds,
+            ServiceOptions.getFromServiceLoader(HttpTransportFactory.class, NetHttpTransport::new),
+            null,
+            config,
+            EMPTY_REALM_CONFIG,
+            testOps,
+            Optional.of(params));
+
+    integration.getStorageAccessConfig(
+        toGrants(
+            Set.of("gs://bucket/path"), Set.of("gs://bucket/path"), Set.of("gs://bucket/path")),
+        Optional.empty(),
+        CredentialVendingContext.builder()
+            .realm(Optional.of("realm1"))
+            .principalName(Optional.of("principal1"))
+            .build());
+
+    Mockito.verify(mockIamClient)
+        .generateAccessToken(
+            Mockito.argThat(
+                request ->
+                    request
+                        .getName()
+                        .equals(
+                            GcpCredentialsStorageIntegration.SERVICE_ACCOUNT_PREFIX

Review Comment:
   Strengthened in fd5994fe9. The test now calls `IdentityPoolCredentials.retrieveSubjectToken()` on the captured federated credential after `getStorageAccessConfig` returns. This triggers the `SubjectTokenSupplier` lambda locally (JWT signing only, no HTTP) and lets us decode the resulting JWT and assert:
   ```java
   assertThat(jwt.getSubject()).isEqualTo("realm1/principal1");
   assertThat(jwt.getClaim("realm").asString()).isEqualTo("realm1");
   ```
   A regression in `GcpAttributionSubjectBuilder.buildSubject` or in how realm/principal are threaded through `buildCacheKey` โ†’ `resolveSourceCredentials` โ†’ `federatedCredentials` will now fail this test.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3399843829


##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegration.java:
##########
@@ -246,6 +273,42 @@ static StorageAccessConfig compute(GcpStorageCredentialCacheKey key) {
     return accessConfig.build();
   }
 
+  /**
+   * Returns the credential to be used as the source for downscoping.
+   *
+   * <p>When GCS principal attribution is configured and a principal is present (so the cache key
+   * carries it), the impersonation source is a federated identity whose subject is {@code
+   * <realm>/<principal>}, which surfaces the principal in {@code serviceAccountDelegationInfo} of
+   * GCS Data Access audit logs. Otherwise this is the standard path: impersonate the configured
+   * service account from the ambient source credentials, or use those credentials directly.
+   */
+  private static GoogleCredentials baseCredentialsForVending(
+      GcpStorageCredentialCacheKey key,
+      GcpStorageConfigurationInfo storageConfig,
+      GoogleCredentials sourceCredentials,
+      GcpCredentialOps credentialOps) {
+    RealmConfig realmConfig = key.realmConfig();
+    String serviceAccount = storageConfig.getGcpServiceAccount();
+    if (serviceAccount != null
+        && !key.principalName().isEmpty()
+        && principalAttributionConfigured(realmConfig)) {
+      String subject =
+          GcpAttributionSubjectBuilder.buildSubject(key.realmId(), key.principalName());
+      GcpFederatedCredentialsExchanger exchanger =
+          new GcpFederatedCredentialsExchanger(
+              realmConfig.getConfig(FeatureConfiguration.GCS_PRINCIPAL_ATTRIBUTION_TOKEN_ISSUER),

Review Comment:
   This will probably work correctly given that `realm ID` is part of the cache key, but conceptually, I think it would be nicer to evaluate these parameters at cache key build time (i.e. around line 185) and just use the values here. 
   
   The cache key is meant to be a "rich" object, so it should be fine to add new data as another (immutable) sub-object, that is if we want to avoid having too many properties at the same level (just an aesthetic concern).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add GCS principal attribution to vended credentials via Workload Identity Federation [polaris]

Posted by "obelix74 (via GitHub)" <gi...@apache.org>.
obelix74 commented on code in PR #4707:
URL: https://github.com/apache/polaris/pull/4707#discussion_r3400623299


##########
polaris-core/src/main/java/org/apache/polaris/core/storage/gcp/GcpCredentialsStorageIntegration.java:
##########
@@ -246,6 +273,42 @@ static StorageAccessConfig compute(GcpStorageCredentialCacheKey key) {
     return accessConfig.build();
   }
 
+  /**
+   * Returns the credential to be used as the source for downscoping.
+   *
+   * <p>When GCS principal attribution is configured and a principal is present (so the cache key
+   * carries it), the impersonation source is a federated identity whose subject is {@code
+   * <realm>/<principal>}, which surfaces the principal in {@code serviceAccountDelegationInfo} of
+   * GCS Data Access audit logs. Otherwise this is the standard path: impersonate the configured
+   * service account from the ambient source credentials, or use those credentials directly.
+   */
+  private static GoogleCredentials baseCredentialsForVending(
+      GcpStorageCredentialCacheKey key,
+      GcpStorageConfigurationInfo storageConfig,
+      GoogleCredentials sourceCredentials,
+      GcpCredentialOps credentialOps) {
+    RealmConfig realmConfig = key.realmConfig();
+    String serviceAccount = storageConfig.getGcpServiceAccount();
+    if (serviceAccount != null
+        && !key.principalName().isEmpty()
+        && principalAttributionConfigured(realmConfig)) {
+      String subject =
+          GcpAttributionSubjectBuilder.buildSubject(key.realmId(), key.principalName());
+      GcpFederatedCredentialsExchanger exchanger =
+          new GcpFederatedCredentialsExchanger(
+              realmConfig.getConfig(FeatureConfiguration.GCS_PRINCIPAL_ATTRIBUTION_TOKEN_ISSUER),

Review Comment:
   Addressed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org