nastra commented on code in PR #11995:
URL: https://github.com/apache/iceberg/pull/11995#discussion_r1946042519


##########
aws/src/test/java/org/apache/iceberg/aws/TestRESTSigV4AuthSession.java:
##########
@@ -0,0 +1,294 @@
+/*
+ * 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.aws;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+import java.net.URI;
+import java.util.Map;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.rest.HTTPHeaders;
+import org.apache.iceberg.rest.HTTPHeaders.HTTPHeader;
+import org.apache.iceberg.rest.HTTPRequest;
+import org.apache.iceberg.rest.HTTPRequest.HTTPMethod;
+import org.apache.iceberg.rest.ImmutableHTTPRequest;
+import org.apache.iceberg.rest.auth.AuthSession;
+import org.apache.iceberg.rest.requests.CreateNamespaceRequest;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+import software.amazon.awssdk.auth.signer.Aws4Signer;
+
+class TestRESTSigV4AuthSession {
+
+  private final Aws4Signer signer = Aws4Signer.create();
+
+  private final AwsProperties awsProperties =
+      new AwsProperties(
+          Map.of(
+              // CI environment doesn't have credentials, but a value must be 
set for signing
+              AwsProperties.REST_SIGNER_REGION,
+              "us-west-2",
+              AwsProperties.REST_ACCESS_KEY_ID,
+              "id",
+              AwsProperties.REST_SECRET_ACCESS_KEY,
+              "secret"));
+
+  @Test
+  void authenticateWithoutBody() {
+    HTTPRequest request = Mockito.mock(HTTPRequest.class);
+    AuthSession delegate = Mockito.mock(AuthSession.class);
+    when(delegate.authenticate(any()))
+        .thenReturn(
+            ImmutableHTTPRequest.builder()
+                .method(HTTPMethod.GET)
+                .baseUri(URI.create("http://localhost:8080";))
+                .path("path")
+                .headers(
+                    HTTPHeaders.of(
+                        HTTPHeader.of("Content-Type", "application/json"),
+                        HTTPHeader.of("Content-Encoding", "gzip")))
+                .build());
+    try (RESTSigV4AuthSession session = new RESTSigV4AuthSession(signer, 
delegate, awsProperties)) {

Review Comment:
   we should also have some separate test method(s) that pass nulls as 
parameters but also where the required aws props (region/name) aren't set



##########
aws/src/main/java/org/apache/iceberg/aws/RESTSigV4AuthManager.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.aws;
+
+import java.util.Map;
+import org.apache.iceberg.catalog.SessionCatalog;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.rest.RESTClient;
+import org.apache.iceberg.rest.RESTUtil;
+import org.apache.iceberg.rest.auth.AuthManager;
+import org.apache.iceberg.rest.auth.AuthSession;
+import software.amazon.awssdk.auth.signer.Aws4Signer;
+
+/**
+ * An AuthManager that authenticates requests with SigV4.
+ *
+ * <p>It takes a delegate AuthManager to handle double authentication cases, 
e.g. on top of OAuth2.
+ */
+@SuppressWarnings("unused") // loaded by reflection
+public class RESTSigV4AuthManager implements AuthManager {
+
+  private final Aws4Signer signer = Aws4Signer.create();
+  private final AuthManager delegate;
+
+  private Map<String, String> catalogProperties;

Review Comment:
   should this maybe be an empty map by default? What happens if the methods 
are called in the wrong order and the  map hasn't been initialized yet?



##########
aws/src/main/java/org/apache/iceberg/aws/RESTSigV4AuthSession.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.aws;
+
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import org.apache.commons.io.IOUtils;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.rest.HTTPHeaders;
+import org.apache.iceberg.rest.HTTPHeaders.HTTPHeader;
+import org.apache.iceberg.rest.HTTPRequest;
+import org.apache.iceberg.rest.ImmutableHTTPHeaders;
+import org.apache.iceberg.rest.ImmutableHTTPRequest;
+import org.apache.iceberg.rest.auth.AuthSession;
+import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
+import software.amazon.awssdk.auth.signer.Aws4Signer;
+import software.amazon.awssdk.auth.signer.internal.SignerConstant;
+import software.amazon.awssdk.auth.signer.params.Aws4SignerParams;
+import software.amazon.awssdk.auth.signer.params.SignerChecksumParams;
+import software.amazon.awssdk.core.checksums.Algorithm;
+import software.amazon.awssdk.http.SdkHttpFullRequest;
+import software.amazon.awssdk.http.SdkHttpMethod;
+import software.amazon.awssdk.regions.Region;
+
+/**
+ * An AuthSession that signs requests with SigV4.
+ *
+ * <p>The request is first authenticated by the delegate AuthSession, then 
signed with SigV4. In
+ * case of conflicting headers, the Authorization header set by delegate 
AuthSession will be
+ * relocated, then included in the canonical headers to sign.
+ *
+ * <p>See <a 
href="https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv.html";>Signing
 AWS
+ * API requests</a> for details about the SigV4 protocol.
+ */
+public class RESTSigV4AuthSession implements AuthSession {
+
+  static final String EMPTY_BODY_SHA256 =
+      "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
+  static final String RELOCATED_HEADER_PREFIX = "Original-";
+
+  private final Aws4Signer signer;
+  private final AuthSession delegate;
+  private final Region signingRegion;
+  private final String signingName;
+  private final AwsCredentialsProvider credentialsProvider;
+
+  public RESTSigV4AuthSession(
+      Aws4Signer aws4Signer, AuthSession delegateAuthSession, AwsProperties 
awsProperties) {
+    this.signer = Preconditions.checkNotNull(aws4Signer, "Invalid signer: 
null");
+    this.delegate = Preconditions.checkNotNull(delegateAuthSession, "Invalid 
delegate: null");
+    this.signingRegion = awsProperties.restSigningRegion();
+    this.signingName = awsProperties.restSigningName();
+    this.credentialsProvider = awsProperties.restCredentialsProvider();

Review Comment:
   is the credentials provider returned here always non-null?



##########
aws/src/main/java/org/apache/iceberg/aws/RESTSigV4AuthManager.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.aws;
+
+import java.util.Map;
+import org.apache.iceberg.catalog.SessionCatalog;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.rest.RESTClient;
+import org.apache.iceberg.rest.RESTUtil;
+import org.apache.iceberg.rest.auth.AuthManager;
+import org.apache.iceberg.rest.auth.AuthSession;
+import software.amazon.awssdk.auth.signer.Aws4Signer;
+
+/**
+ * An AuthManager that authenticates requests with SigV4.
+ *
+ * <p>It takes a delegate AuthManager to handle double authentication cases, 
e.g. on top of OAuth2.
+ */
+@SuppressWarnings("unused") // loaded by reflection
+public class RESTSigV4AuthManager implements AuthManager {
+
+  private final Aws4Signer signer = Aws4Signer.create();
+  private final AuthManager delegate;
+
+  private Map<String, String> catalogProperties;
+
+  public RESTSigV4AuthManager(String name, AuthManager delegate) {
+    this.delegate = delegate;

Review Comment:
   we might also want to make sure that the delegate is non-null here



##########
aws/src/main/java/org/apache/iceberg/aws/RESTSigV4AuthManager.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.aws;
+
+import java.util.Map;
+import org.apache.iceberg.catalog.SessionCatalog;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.rest.RESTClient;
+import org.apache.iceberg.rest.RESTUtil;
+import org.apache.iceberg.rest.auth.AuthManager;
+import org.apache.iceberg.rest.auth.AuthSession;
+import software.amazon.awssdk.auth.signer.Aws4Signer;
+
+/**
+ * An AuthManager that authenticates requests with SigV4.
+ *
+ * <p>It takes a delegate AuthManager to handle double authentication cases, 
e.g. on top of OAuth2.
+ */
+@SuppressWarnings("unused") // loaded by reflection
+public class RESTSigV4AuthManager implements AuthManager {
+
+  private final Aws4Signer signer = Aws4Signer.create();
+  private final AuthManager delegate;
+
+  private Map<String, String> catalogProperties;
+
+  public RESTSigV4AuthManager(String name, AuthManager delegate) {
+    this.delegate = delegate;
+  }
+
+  @Override
+  public RESTSigV4AuthSession initSession(RESTClient initClient, Map<String, 
String> properties) {
+    return new RESTSigV4AuthSession(
+        signer, delegate.initSession(initClient, properties), new 
AwsProperties(properties));
+  }
+
+  @Override
+  public RESTSigV4AuthSession catalogSession(
+      RESTClient sharedClient, Map<String, String> properties) {
+    catalogProperties = properties;

Review Comment:
   ```suggestion
       this.catalogProperties = properties;
   ```



-- 
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-unsubscr...@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to