sungwy commented on code in PR #18110:
URL: https://github.com/apache/iceberg/pull/18110#discussion_r4010347002


##########
core/src/main/java/org/apache/iceberg/rest/RemoteSigningClient.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.iceberg.rest;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.net.URI;
+import java.util.Collection;
+import java.util.Map;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import org.apache.iceberg.CatalogProperties;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.apache.iceberg.rest.auth.AuthManager;
+import org.apache.iceberg.rest.auth.AuthManagers;
+import org.apache.iceberg.rest.auth.AuthSession;
+import org.apache.iceberg.rest.auth.OAuth2Properties;
+import org.apache.iceberg.rest.requests.ImmutableRemoteSignRequest;
+import org.apache.iceberg.rest.requests.RemoteSignRequest;
+import org.apache.iceberg.rest.responses.ErrorResponse;
+import org.apache.iceberg.rest.responses.RemoteSignResponse;
+
+/**
+ * Client of the catalog's signing endpoint on behalf of a FileIO. {@link 
#preSign} obtains
+ * pre-signed URLs: each request carries {@code X-Iceberg-Access-Delegation: 
presigned-urls} and
+ * names the storage location to sign.
+ */
+public class RemoteSigningClient implements AutoCloseable {
+
+  public static final String ACCESS_DELEGATION_HEADER = 
"X-Iceberg-Access-Delegation";
+  public static final String PRESIGNED_URLS = "presigned-urls";
+
+  private static final String SCOPE = "sign";
+  private static final Consumer<ErrorResponse> ERROR_HANDLER =
+      error -> {
+        if (error.code() == 406) {
+          throw new UnsupportedOperationException(
+              "Signing endpoint does not support " + PRESIGNED_URLS + ": " + 
error.message());
+        }
+
+        ErrorHandlers.defaultErrorHandler().accept(error);
+      };
+
+  private final Map<String, String> properties;
+  private final RemoteSigningConfig config;
+  private final Map<String, String> configHeaders;
+  private final String endpoint;
+  private final RESTClient restClient;
+  private final AuthManager authManager;
+
+  public static RemoteSigningClient create(Map<String, String> properties) {
+    return new RemoteSigningClient(properties);
+  }
+
+  private RemoteSigningClient(Map<String, String> properties) {
+    String uri = properties.get(CatalogProperties.URI);
+    Preconditions.checkArgument(null != uri, "Pre-signing requires the REST 
catalog URI");
+    String endpointPath = 
properties.get(RESTCatalogProperties.REMOTE_SIGNING_ENDPOINT);
+    Preconditions.checkArgument(
+        null != endpointPath,
+        "Pre-signing requires the remote signing endpoint (%s)",
+        RESTCatalogProperties.REMOTE_SIGNING_ENDPOINT);
+
+    this.properties = properties;
+    String json = properties.get(RESTCatalogProperties.REMOTE_SIGNING_CONFIG);
+    this.config =
+        null == json ? RemoteSigningConfig.EMPTY : 
RemoteSigningConfigParser.fromJson(json);
+    this.configHeaders = configHeaders(config);
+    this.endpoint = RESTUtil.resolveEndpoint(uri, endpointPath);
+    this.restClient =
+        HTTPClient.builder(properties)
+            .uri(uri)
+            .withHeaders(RESTUtil.configHeaders(properties))
+            .build();
+    this.authManager = AuthManagers.loadAuthManager("pre-sign", properties);
+  }
+
+  public Map<String, URI> preSign(
+      Collection<String> locations, Function<String, RemoteSignRequest> 
requests) {
+    ImmutableMap.Builder<String, URI> urls = ImmutableMap.builder();
+    for (String location : Sets.newLinkedHashSet(locations)) {
+      urls.put(location, preSign(location, requests.apply(location)));

Review Comment:
   For FILE value, this is a catalog round trip at row cardinality. Worth 
looking into a batch API



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