Gabriel39 commented on code in PR #71031:
URL: https://github.com/apache/doris/pull/71031#discussion_r4748337500


##########
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:
   Fixed in d20cd66a3b861f96e2a03bc8f0f94cb4bb60c549. Scoped SDK clients now 
keep their bound prefix during refresh, select the last exact-prefix 
credential, and validate its replacement token/expiry. A missing scope fails 
instead of switching identity. Existing Storage and Analytics Core readers 
share the refreshable credentials; owned clients and refresh handlers close 
with the delegate, without adding a scheduler.
   
   The four baseline cases reproduced `only one GCS credential should exist`. 
The fix passes 113 relevant tests across eight classes, including 10 new 
refresh cases: multiple scopes, duplicate scopes, serialization, 
invalid/missing replacements, retained InputFile reads, encryption/billing 
options and retirement. Fresh FE build and Checkstyle passed. Actual FE and BE 
plugin loaders both read the two scopes after refresh with zero wrong-identity 
requests and matching candidate class bytes. Live-cloud and optional Analytics 
Core library integration were not run.



-- 
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