Gabriel39 commented on code in PR #71031: URL: https://github.com/apache/doris/pull/71031#discussion_r4752756932
########## fe/fe-connector/fe-connector-iceberg/src/main/java/org/apache/doris/connector/iceberg/IcebergGoogleCredentialValidator.java: ########## @@ -0,0 +1,59 @@ +// 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.doris.connector.iceberg; + +import com.google.api.client.json.GenericJson; +import com.google.api.client.json.JsonObjectParser; +import com.google.api.client.json.gson.GsonFactory; + +import java.io.IOException; +import java.io.StringReader; +import java.util.Map; + +/** Checks the JSON fields the Google SDK consumes without resolving credential sources. */ +final class IcebergGoogleCredentialValidator { + private IcebergGoogleCredentialValidator() { + } + + static void validate(Map<String, String> properties) { + String json = properties.get("gcp.auth.credentials-json"); + if (json == null) { Review Comment: Previously fixed in 1bf25f579927c92a4d6e4f0c0d0d0d0d0d0d0d0d. Freshly verified on 6dbc3128951a143f5131e0fed6466017394a14fb: the CREATE, ALTER and runtime property-validation cases preserve empty inline JSON with credential-file or ADC configuration. ########## fe/fe-iceberg-common/src/main/java/org/apache/iceberg/io/NativeGcsResolvingFileIO.java: ########## @@ -39,9 +52,146 @@ 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) { Review Comment: Removed in 6dbc3128951a143f5131e0fed6466017394a14fb. The helper had no production or test callers; routing continues to use the effective credential map. ########## fe/fe-iceberg-common/src/main/java/org/apache/iceberg/io/NativeGcsResolvingFileIO.java: ########## @@ -39,9 +52,146 @@ 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); Review Comment: Fixed in 6dbc3128951a143f5131e0fed6466017394a14fb. The cache is cleared before closing a snapshot, every delegate is attempted, and secondary runtime failures are suppressed on the primary failure. Parent cleanup remains in finally, including when scoped cleanup throws an Error. Failure-injection tests cover credential replacement, close, reinitialization, cache recovery and exception preservation. -- 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]
