You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/09/07 10:54:22 UTC

[GitHub] [iceberg] nastra commented on a diff in pull request #5698: REST: implement handling of OAuth error responses

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


##########
core/src/main/java/org/apache/iceberg/rest/responses/OAuthErrorResponseParser.java:
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.responses;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonNode;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.UncheckedIOException;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.util.JsonUtil;
+
+public class OAuthErrorResponseParser {
+
+  private OAuthErrorResponseParser() {}
+
+  private static final String ERROR = "error";
+  private static final String ERROR_DESCRIPTION = "error_description";
+  private static final String ERROR_URI = "error_uri";
+
+  public static String toJson(OAuthErrorResponse errorResponse) {
+    return toJson(errorResponse, false);
+  }
+
+  public static String toJson(OAuthErrorResponse errorResponse, boolean pretty) {
+    try {
+      StringWriter writer = new StringWriter();
+      JsonGenerator generator = JsonUtil.factory().createGenerator(writer);
+      if (pretty) {
+        generator.useDefaultPrettyPrinter();
+      }
+      toJson(errorResponse, generator);
+      generator.flush();
+      return writer.toString();
+    } catch (IOException e) {
+      throw new UncheckedIOException(
+          String.format("Failed to write error response json for: %s", errorResponse), e);
+    }
+  }
+
+  public static void toJson(OAuthErrorResponse errorResponse, JsonGenerator generator)
+      throws IOException {
+    generator.writeStartObject();
+
+    generator.writeStringField(ERROR, errorResponse.error());
+    generator.writeStringField(ERROR_DESCRIPTION, errorResponse.errorDescription());
+    generator.writeStringField(ERROR_URI, errorResponse.errorUri());
+
+    generator.writeEndObject();
+  }
+
+  /**
+   * Read OAuthErrorResponse from a JSON string.
+   *
+   * @param json a JSON string of an OAuthErrorResponse
+   * @return an OAuthErrorResponse object
+   */
+  public static OAuthErrorResponse fromJson(String json) {
+    try {
+      return fromJson(JsonUtil.mapper().readValue(json, JsonNode.class));
+    } catch (IOException e) {
+      throw new UncheckedIOException("Failed to read JSON string: " + json, e);
+    }
+  }
+
+  public static OAuthErrorResponse fromJson(JsonNode jsonNode) {
+    Preconditions.checkArgument(
+        jsonNode != null && jsonNode.isObject(),
+        "Cannot parse error response from non-object value: %s",
+        jsonNode);
+    Preconditions.checkArgument(jsonNode.has(ERROR), "Cannot parse missing field: error");

Review Comment:
   this is already handled in `JsonUtil.getString(ERROR, jsonNode);`



##########
core/src/test/java/org/apache/iceberg/rest/responses/TestOAuthErrorResponseParser.java:
##########
@@ -0,0 +1,116 @@
+/*
+ * 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.responses;
+
+import org.apache.iceberg.rest.auth.OAuth2Properties;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestOAuthErrorResponseParser {
+
+  @Test
+  public void testOAuthErrorResponseToJson() {
+    String error = OAuth2Properties.INVALID_CLIENT_ERROR;
+    String description = "Credentials given were invalid";
+    String uri = "http://iceberg.apache.org";
+    String json =
+        String.format(
+            "{\"error\":\"%s\",\"error_description\":\"%s\",\"error_uri\":\"%s\"}",
+            error, description, uri);
+    OAuthErrorResponse response =
+        OAuthErrorResponse.builder()
+            .withError(error)
+            .withErrorDescription(description)
+            .withErrorUri(uri)
+            .build();
+    Assert.assertEquals(
+        "Should be able to serialize an error response as json",
+        OAuthErrorResponseParser.toJson(response),
+        json);
+  }
+
+  @Test
+  public void testOAuthErrorResponseToJsonWithNulls() {
+    String error = OAuth2Properties.INVALID_CLIENT_ERROR;
+    String expected =
+        String.format("{\"error\":\"%s\",\"error_description\":null,\"error_uri\":null}", error);
+    OAuthErrorResponse response = OAuthErrorResponse.builder().withError(error).build();
+    Assert.assertEquals(
+        "Should be able to serialize an error response as json",
+        OAuthErrorResponseParser.toJson(response),
+        expected);
+  }
+
+  @Test
+  public void testOAuthErrorResponseBuilderMIssingError() {
+    Assert.assertThrows(
+        "Missing error should throw exception",
+        IllegalArgumentException.class,
+        () -> OAuthErrorResponse.builder().build());
+  }
+
+  @Test
+  public void testOAuthErrorResponseFromJson() {
+    String error = OAuth2Properties.INVALID_CLIENT_ERROR;
+    String description = "Credentials given were invalid";
+    String uri = "http://iceberg.apache.org";
+    String json =
+        String.format(
+            "{\"error\":\"%s\",\"error_description\":\"%s\",\"error_uri\":\"%s\"}",
+            error, description, uri);
+    OAuthErrorResponse expected =
+        OAuthErrorResponse.builder()
+            .withError(error)
+            .withErrorDescription(description)
+            .withErrorUri(uri)
+            .build();
+    assertEquals(expected, OAuthErrorResponseParser.fromJson(json));
+  }
+
+  @Test
+  public void testOAuthErrorResponseFromJsonWithNulls() {
+    String error = OAuth2Properties.INVALID_CLIENT_ERROR;
+    String json = String.format("{\"error\":\"%s\"}", error);
+    OAuthErrorResponse expected = OAuthErrorResponse.builder().withError(error).build();
+    assertEquals(expected, OAuthErrorResponseParser.fromJson(json));
+
+    // test with explicitly set nulls
+    json = String.format("{\"error\":\"%s\",\"error_description\":null,\"error_uri\":null}", error);
+    assertEquals(expected, OAuthErrorResponseParser.fromJson(json));
+  }
+
+  @Test
+  public void testOAuthErrorResponseFromJsonMissingError() {
+    String description = "Credentials given were invalid";
+    String uri = "http://iceberg.apache.org";
+    String json =
+        String.format("{\"error_description\":\"%s\",\"error_uri\":\"%s\"}", description, uri);
+    Assert.assertThrows(
+        "Missing error should throw exception",
+        IllegalArgumentException.class,
+        () -> OAuthErrorResponseParser.fromJson(json));
+  }
+
+  public void assertEquals(OAuthErrorResponse expected, OAuthErrorResponse actual) {
+    Assert.assertEquals("Error should be equal", expected.error(), actual.error());

Review Comment:
   nit: I think it would be nice to use AssertJ assertions as then you wouldn't need to add a description message and when the assertion fails it generally has enough context to understand why it failed



##########
core/src/main/java/org/apache/iceberg/rest/responses/OAuthErrorResponseParser.java:
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.responses;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonNode;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.UncheckedIOException;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.util.JsonUtil;
+
+public class OAuthErrorResponseParser {
+
+  private OAuthErrorResponseParser() {}
+
+  private static final String ERROR = "error";
+  private static final String ERROR_DESCRIPTION = "error_description";
+  private static final String ERROR_URI = "error_uri";
+
+  public static String toJson(OAuthErrorResponse errorResponse) {
+    return toJson(errorResponse, false);
+  }
+
+  public static String toJson(OAuthErrorResponse errorResponse, boolean pretty) {

Review Comment:
   I wonder whether this can be simplified to the below code snippet
   ```
   static String toJson(OAuthErrorResponse errorResponse, boolean pretty) {
       return JsonUtil.generate(gen -> toJson(errorResponse, gen), pretty);
     }
   ```



##########
core/src/test/java/org/apache/iceberg/rest/responses/TestOAuthErrorResponseParser.java:
##########
@@ -0,0 +1,116 @@
+/*
+ * 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.responses;
+
+import org.apache.iceberg.rest.auth.OAuth2Properties;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestOAuthErrorResponseParser {
+
+  @Test
+  public void testOAuthErrorResponseToJson() {
+    String error = OAuth2Properties.INVALID_CLIENT_ERROR;
+    String description = "Credentials given were invalid";
+    String uri = "http://iceberg.apache.org";
+    String json =
+        String.format(
+            "{\"error\":\"%s\",\"error_description\":\"%s\",\"error_uri\":\"%s\"}",
+            error, description, uri);
+    OAuthErrorResponse response =
+        OAuthErrorResponse.builder()
+            .withError(error)
+            .withErrorDescription(description)
+            .withErrorUri(uri)
+            .build();
+    Assert.assertEquals(
+        "Should be able to serialize an error response as json",
+        OAuthErrorResponseParser.toJson(response),
+        json);
+  }
+
+  @Test
+  public void testOAuthErrorResponseToJsonWithNulls() {
+    String error = OAuth2Properties.INVALID_CLIENT_ERROR;
+    String expected =
+        String.format("{\"error\":\"%s\",\"error_description\":null,\"error_uri\":null}", error);
+    OAuthErrorResponse response = OAuthErrorResponse.builder().withError(error).build();
+    Assert.assertEquals(
+        "Should be able to serialize an error response as json",
+        OAuthErrorResponseParser.toJson(response),
+        expected);
+  }
+
+  @Test
+  public void testOAuthErrorResponseBuilderMIssingError() {
+    Assert.assertThrows(
+        "Missing error should throw exception",
+        IllegalArgumentException.class,
+        () -> OAuthErrorResponse.builder().build());

Review Comment:
   I think generally it's good to also verify that the correct error message is being returned



##########
core/src/test/java/org/apache/iceberg/rest/responses/TestOAuthErrorResponseParser.java:
##########
@@ -0,0 +1,116 @@
+/*
+ * 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.responses;
+
+import org.apache.iceberg.rest.auth.OAuth2Properties;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestOAuthErrorResponseParser {
+
+  @Test
+  public void testOAuthErrorResponseToJson() {
+    String error = OAuth2Properties.INVALID_CLIENT_ERROR;
+    String description = "Credentials given were invalid";
+    String uri = "http://iceberg.apache.org";
+    String json =
+        String.format(
+            "{\"error\":\"%s\",\"error_description\":\"%s\",\"error_uri\":\"%s\"}",
+            error, description, uri);
+    OAuthErrorResponse response =
+        OAuthErrorResponse.builder()
+            .withError(error)
+            .withErrorDescription(description)
+            .withErrorUri(uri)
+            .build();
+    Assert.assertEquals(
+        "Should be able to serialize an error response as json",
+        OAuthErrorResponseParser.toJson(response),
+        json);
+  }
+
+  @Test
+  public void testOAuthErrorResponseToJsonWithNulls() {
+    String error = OAuth2Properties.INVALID_CLIENT_ERROR;
+    String expected =
+        String.format("{\"error\":\"%s\",\"error_description\":null,\"error_uri\":null}", error);
+    OAuthErrorResponse response = OAuthErrorResponse.builder().withError(error).build();
+    Assert.assertEquals(
+        "Should be able to serialize an error response as json",
+        OAuthErrorResponseParser.toJson(response),
+        expected);
+  }
+
+  @Test
+  public void testOAuthErrorResponseBuilderMIssingError() {
+    Assert.assertThrows(

Review Comment:
   ```suggestion
       Assertions.assertThatThrownBy(() -> OAuthErrorResponse.builder().build())
           .isInstanceOf(IllegalArgumentException.class)
           .hasMessage("Invalid response, missing field: error");
   ```



##########
core/src/test/java/org/apache/iceberg/rest/responses/TestOAuthErrorResponseParser.java:
##########
@@ -0,0 +1,116 @@
+/*
+ * 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.responses;
+
+import org.apache.iceberg.rest.auth.OAuth2Properties;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestOAuthErrorResponseParser {
+
+  @Test
+  public void testOAuthErrorResponseToJson() {
+    String error = OAuth2Properties.INVALID_CLIENT_ERROR;
+    String description = "Credentials given were invalid";
+    String uri = "http://iceberg.apache.org";
+    String json =
+        String.format(
+            "{\"error\":\"%s\",\"error_description\":\"%s\",\"error_uri\":\"%s\"}",
+            error, description, uri);
+    OAuthErrorResponse response =
+        OAuthErrorResponse.builder()
+            .withError(error)
+            .withErrorDescription(description)
+            .withErrorUri(uri)
+            .build();
+    Assert.assertEquals(
+        "Should be able to serialize an error response as json",
+        OAuthErrorResponseParser.toJson(response),
+        json);
+  }
+
+  @Test
+  public void testOAuthErrorResponseToJsonWithNulls() {
+    String error = OAuth2Properties.INVALID_CLIENT_ERROR;
+    String expected =
+        String.format("{\"error\":\"%s\",\"error_description\":null,\"error_uri\":null}", error);
+    OAuthErrorResponse response = OAuthErrorResponse.builder().withError(error).build();
+    Assert.assertEquals(
+        "Should be able to serialize an error response as json",
+        OAuthErrorResponseParser.toJson(response),
+        expected);
+  }
+
+  @Test
+  public void testOAuthErrorResponseBuilderMIssingError() {

Review Comment:
   nit: MIssing -> Missing



##########
core/src/test/java/org/apache/iceberg/rest/responses/TestOAuthErrorResponseParser.java:
##########
@@ -0,0 +1,116 @@
+/*
+ * 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.responses;
+
+import org.apache.iceberg.rest.auth.OAuth2Properties;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestOAuthErrorResponseParser {
+
+  @Test
+  public void testOAuthErrorResponseToJson() {
+    String error = OAuth2Properties.INVALID_CLIENT_ERROR;
+    String description = "Credentials given were invalid";
+    String uri = "http://iceberg.apache.org";
+    String json =
+        String.format(
+            "{\"error\":\"%s\",\"error_description\":\"%s\",\"error_uri\":\"%s\"}",
+            error, description, uri);
+    OAuthErrorResponse response =
+        OAuthErrorResponse.builder()
+            .withError(error)
+            .withErrorDescription(description)
+            .withErrorUri(uri)
+            .build();
+    Assert.assertEquals(
+        "Should be able to serialize an error response as json",
+        OAuthErrorResponseParser.toJson(response),
+        json);
+  }
+
+  @Test
+  public void testOAuthErrorResponseToJsonWithNulls() {
+    String error = OAuth2Properties.INVALID_CLIENT_ERROR;
+    String expected =
+        String.format("{\"error\":\"%s\",\"error_description\":null,\"error_uri\":null}", error);
+    OAuthErrorResponse response = OAuthErrorResponse.builder().withError(error).build();
+    Assert.assertEquals(
+        "Should be able to serialize an error response as json",
+        OAuthErrorResponseParser.toJson(response),
+        expected);
+  }
+
+  @Test
+  public void testOAuthErrorResponseBuilderMIssingError() {
+    Assert.assertThrows(
+        "Missing error should throw exception",
+        IllegalArgumentException.class,
+        () -> OAuthErrorResponse.builder().build());
+  }
+
+  @Test
+  public void testOAuthErrorResponseFromJson() {
+    String error = OAuth2Properties.INVALID_CLIENT_ERROR;
+    String description = "Credentials given were invalid";
+    String uri = "http://iceberg.apache.org";
+    String json =
+        String.format(
+            "{\"error\":\"%s\",\"error_description\":\"%s\",\"error_uri\":\"%s\"}",
+            error, description, uri);
+    OAuthErrorResponse expected =
+        OAuthErrorResponse.builder()
+            .withError(error)
+            .withErrorDescription(description)
+            .withErrorUri(uri)
+            .build();
+    assertEquals(expected, OAuthErrorResponseParser.fromJson(json));
+  }
+
+  @Test
+  public void testOAuthErrorResponseFromJsonWithNulls() {
+    String error = OAuth2Properties.INVALID_CLIENT_ERROR;
+    String json = String.format("{\"error\":\"%s\"}", error);
+    OAuthErrorResponse expected = OAuthErrorResponse.builder().withError(error).build();
+    assertEquals(expected, OAuthErrorResponseParser.fromJson(json));
+
+    // test with explicitly set nulls
+    json = String.format("{\"error\":\"%s\",\"error_description\":null,\"error_uri\":null}", error);
+    assertEquals(expected, OAuthErrorResponseParser.fromJson(json));
+  }
+
+  @Test
+  public void testOAuthErrorResponseFromJsonMissingError() {
+    String description = "Credentials given were invalid";
+    String uri = "http://iceberg.apache.org";
+    String json =
+        String.format("{\"error_description\":\"%s\",\"error_uri\":\"%s\"}", description, uri);
+    Assert.assertThrows(

Review Comment:
   ```suggestion
       Assertions.assertThatThrownBy(() -> OAuthErrorResponseParser.fromJson(json))
           .isInstanceOf(IllegalArgumentException.class)
           .hasMessage("...");
   ```



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

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


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