hello-stephen commented on code in PR #71031:
URL: https://github.com/apache/doris/pull/71031#discussion_r4734396112


##########
fe/fe-iceberg-common/src/main/java/org/apache/iceberg/io/NativeGcsResolvingFileIO.java:
##########
@@ -39,9 +50,139 @@ public class NativeGcsResolvingFileIO extends 
ResolvingFileIO {
     private static final String GCS_FILE_IO_IMPL = 
"org.apache.iceberg.gcp.gcs.GCSFileIO";
     private static final String HADOOP_FILE_IO_IMPL = 
"org.apache.iceberg.hadoop.HadoopFileIO";
 
+    // Clients are bounded by credential prefixes, not file locations, and are 
rebuilt after serialization.
+    private transient Map<String, DelegateFileIO> scopedIos;
+    private volatile Boolean hadoopGcsConfigured;
+    private boolean closed;
+
+    public static boolean hasSdkGcsProperties(Map<String, String> properties) {
+        return properties.keySet().stream().anyMatch(key -> key != null && 
key.startsWith("gcs."));
+    }
+
     @Override
-    String implFromLocation(String location) {
+    synchronized String implFromLocation(String location) {
         String impl = super.implFromLocation(location);
-        return GCS_FILE_IO_IMPL.equals(impl) ? HADOOP_FILE_IO_IMPL : impl;
+        if (!GCS_FILE_IO_IMPL.equals(impl)) {
+            return impl;
+        }
+        StorageCredential matching = matchingCredential(location);
+        Map<String, String> scope = matching == null ? Collections.emptyMap() 
: matching.config();
+        // Validate the effective scope before choosing an identity: a present 
null token is not an absent token.
+        GcsOAuthTokenValidator.validate(properties(), scope);
+        Map<String, String> effective = effectiveProperties(scope);
+        String impersonation = 
effective.get("gcs.impersonate.service-account");
+        boolean sdkIdentity = effective.containsKey("gcs.oauth2.token")
+                || Boolean.parseBoolean(effective.get("gcs.no-auth"))
+                || (impersonation != null && !impersonation.isEmpty());
+        if (sdkIdentity) {
+            return GCS_FILE_IO_IMPL;
+        }
+        // Project selection is not authentication. SDK-only options must 
never silently replace HMAC with ADC.
+        if (properties().containsKey("gs.access_key") || 
hasHadoopGcsConfiguration()) {
+            for (String key : effective.keySet()) {
+                if (key.startsWith("gcs.") && !"gcs.project-id".equals(key)) {
+                    throw new IllegalArgumentException("GCS option requires an 
explicit SDK storage identity: " + key);
+                }
+            }
+            return HADOOP_FILE_IO_IMPL;
+        }
+        // With no configured Hadoop identity, retain the upstream SDK/ADC 
default.
+        return GCS_FILE_IO_IMPL;
+    }
+
+    private boolean hasHadoopGcsConfiguration() {
+        if (hadoopGcsConfigured == null) {
+            boolean configured = false;
+            Configuration conf = getConf();
+            if (conf != null) {
+                for (Map.Entry<String, String> entry : conf) {
+                    if (entry.getKey().startsWith("fs.gs.")) {
+                        configured = true;
+                        break;
+                    }
+                }
+            }
+            hadoopGcsConfigured = configured;
+        }
+        return hadoopGcsConfigured;
+    }
+
+    @Override
+    public synchronized void setConf(Configuration conf) {
+        super.setConf(conf);
+        // Hadoop configurations can contain thousands of defaults; inspect 
them once per configuration.
+        hadoopGcsConfigured = null;
+    }
+
+    @Override
+    public synchronized void initialize(Map<String, String> properties) {
+        super.initialize(properties);
+        closed = false;
+    }
+
+    @Override
+    public synchronized void setCredentials(List<StorageCredential> 
credentials) {
+        boolean changed = !credentials().equals(credentials);
+        super.setCredentials(credentials);
+        if (changed && scopedIos != null) {
+            // Replacement credentials must not reuse a client initialized 
with the previous effective root map.
+            scopedIos.values().forEach(DelegateFileIO::close);
+            scopedIos.clear();
+        }
+    }
+
+    private StorageCredential matchingCredential(String location) {
+        StorageCredential matching = null;
+        for (StorageCredential credential : credentials()) {
+            if (location.startsWith(credential.prefix())
+                    && (matching == null || credential.prefix().length() >= 
matching.prefix().length())) {
+                matching = credential;
+            }
+        }
+        return matching;
+    }
+
+    private Map<String, String> effectiveProperties(Map<String, String> scope) 
{
+        Map<String, String> effective = new HashMap<>(properties());
+        effective.putAll(scope);
+        return effective;
+    }
+
+    @Override
+    synchronized DelegateFileIO io(String location) {
+        if (closed) {
+            throw new IllegalStateException("FileIO is closed");
+        }
+        if (!GCS_FILE_IO_IMPL.equals(implFromLocation(location))) {
+            return super.io(location);
+        }
+        StorageCredential matching = matchingCredential(location);
+        String prefix = matching == null ? "" : matching.prefix();
+        if (scopedIos == null) {
+            scopedIos = new HashMap<>();
+        }
+        return scopedIos.computeIfAbsent(prefix, ignored -> {
+            // GCSFileIO eagerly parses globals and every credential, 
including unrelated/overridden expiries.
+            // Give its root client the validated effective map, and retain 
only this scope for SDK refresh.
+            Map<String, String> effective = effectiveProperties(
+                    matching == null ? Collections.emptyMap() : 
matching.config());
+            return (DelegateFileIO) CatalogUtil.loadFileIO(GCS_FILE_IO_IMPL, 
effective, getConf(),
+                    matching == null ? Collections.emptyList() : 
Collections.singletonList(matching));

Review Comment:
   [P2] Preserve the selected prefix during OAuth refresh
   
   For a REST table with two GCS credential prefixes and 
`gcs.oauth2.refresh-credentials-endpoint`, passing one credential here scopes 
initial client creation, but the effective map still installs Iceberg 1.12's 
[OAuth2RefreshCredentialsHandler](https://github.com/apache/iceberg/blob/apache-iceberg-1.12.0/gcp/src/main/java/org/apache/iceberg/gcp/gcs/OAuth2RefreshCredentialsHandler.java#L62-L88).
 That handler fetches the full refresh response and rejects it unless exactly 
one GCS credential is returned. Once a vended token expires, manifest reads 
therefore fail even when the response contains valid replacements for both 
prefixes; the SDK's [scheduled 
refresh](https://github.com/apache/iceberg/blob/apache-iceberg-1.12.0/gcp/src/main/java/org/apache/iceberg/gcp/gcs/GCSFileIO.java#L224-L242)
 does not rebuild the existing client. This newly affects HMAC-backed catalogs 
whose compatibility resolver previously kept using Hadoop. Preserve 
longest-prefix selection during refresh and cover a l
 ocal refresh endpoint returning two scopes with short-lived tokens.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to