Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth Manager API part 1: HTTPRequest, HTTPHeader #11769

Merged
merged 8 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions core/src/main/java/org/apache/iceberg/rest/HTTPHeaders.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* 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.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableListMultimap;
import org.apache.iceberg.relocated.com.google.common.collect.ListMultimap;
import org.apache.iceberg.relocated.com.google.common.collect.Multimap;
import org.immutables.value.Value;

/**
* Represents a group of HTTP headers.
*
* <p>Header name comparison in this class is always case-insensitive, in accordance with RFC 2616.
*
* <p>This class exposes methods to convert to and from different representations such as maps and
* multimap, for easier access and manipulation – especially when dealing with multiple headers with
* the same name.
*/
@Value.Style(depluralize = true)
@Value.Immutable
@SuppressWarnings({"ImmutablesStyle", "SafeLoggingPropagation"})
public interface HTTPHeaders {
danielcweeks marked this conversation as resolved.
Show resolved Hide resolved

HTTPHeaders EMPTY = of();

/** Returns all the header entries in this group. */
List<HTTPHeader> entries();
Copy link
Contributor

@danielcweeks danielcweeks Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be Set<HTTPHeader>? This would also take care of cases where there are actualy k/v duplicates (which we don't appear to handle)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we should also annotate this as @NotNull

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a @NotNull annotation available in iceberg-core? Besides, the immutable generated class is annotated with @AllParametersAreNonNullByDefault.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danielcweeks @NotNull shouldn't be needed here, because everything not marked as @Nullable or Optional is implicitly not null with Immutables


/**
* Returns a map representation of the headers where each header name is mapped to a list of its
* values. Header names are case-insensitive.
*/
@Value.Lazy
default Map<String, List<String>> asMap() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these methods will be useful later.

return entries().stream()
.collect(Collectors.groupingBy(h -> h.name().toLowerCase(Locale.ROOT)))
.values()
.stream()
.collect(
Collectors.toMap(
headers -> headers.get(0).name(),
headers -> headers.stream().map(HTTPHeader::value).collect(Collectors.toList())));
}

/**
* Returns a simple map representation of the headers where each header name is mapped to its
* first value. If a header has multiple values, only the first value is used. Header names are
* case-insensitive.
*/
@Value.Lazy
default Map<String, String> asSimpleMap() {
return entries().stream()
.collect(Collectors.groupingBy(h -> h.name().toLowerCase(Locale.ROOT)))
.values()
.stream()
.collect(
Collectors.toMap(headers -> headers.get(0).name(), headers -> headers.get(0).value()));
}

/**
* Returns a {@link ListMultimap} representation of the headers. Header names are
* case-insensitive.
*/
@Value.Lazy
default ListMultimap<String, String> asMultiMap() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we actually have a use case for this? I see the value in having asMap() / asSimpleMap() but I feel like we don't need this method atm?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one will be very useful later to do things like this in HTTPClient:

HTTPRequest req = ...
HttpUriRequestBase request = new HttpUriRequestBase(req.method().name(), req.requestUri());
req.headers().asMultiMap().forEach(request::addHeader);

return entries().stream()
.collect(Collectors.groupingBy(h -> h.name().toLowerCase(Locale.ROOT)))
.values()
.stream()
.collect(
ImmutableListMultimap.flatteningToImmutableListMultimap(
headers -> headers.get(0).name(),
headers -> headers.stream().map(HTTPHeader::value)));
}

/** Returns all the entries in this group for the given name (case-insensitive). */
default List<HTTPHeader> entries(String name) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, set

return entries().stream()
.filter(header -> header.name().equalsIgnoreCase(name))
.collect(Collectors.toList());
}

/** Returns whether this group contains an entry with the given name (case-insensitive). */
default boolean contains(String name) {
danielcweeks marked this conversation as resolved.
Show resolved Hide resolved
return entries().stream().anyMatch(header -> header.name().equalsIgnoreCase(name));
}

/**
* Adds the given header to the current group if no entry with the same name is already present.
* Returns a new instance with the added header, or the current instance if the header is already
* present.
*/
default HTTPHeaders addIfAbsent(HTTPHeader header) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we might want to consider naming this (and the other method) withHeaderIfAbsent() to clearly indicate that (potentially) a new instance is returned because add in the name suggests that the existing object is modified

return contains(header.name())
? this
: ImmutableHTTPHeaders.builder().from(this).addEntry(header).build();
}

/**
* Adds the given headers to the current group if no entries with same names are already present.
* Returns a new instance with the added headers, or the current instance if all headers are
* already present.
*/
default HTTPHeaders addIfAbsent(HTTPHeaders headers) {
List<HTTPHeader> newHeaders =
headers.entries().stream().filter(e -> !contains(e.name())).collect(Collectors.toList());
return newHeaders.isEmpty()
? this
: ImmutableHTTPHeaders.builder().from(this).addAllEntries(newHeaders).build();
}

static HTTPHeaders of(HTTPHeader... headers) {
return ImmutableHTTPHeaders.builder().addEntries(headers).build();
}

static HTTPHeaders fromMap(Map<String, ? extends Iterable<String>> headers) {
ImmutableHTTPHeaders.Builder builder = ImmutableHTTPHeaders.builder();
headers.forEach(
(name, values) -> values.forEach(value -> builder.addEntry(HTTPHeader.of(name, value))));
return builder.build();
}

static HTTPHeaders fromSimpleMap(Map<String, String> headers) {
ImmutableHTTPHeaders.Builder builder = ImmutableHTTPHeaders.builder();
headers.forEach((name, value) -> builder.addEntry(HTTPHeader.of(name, value)));
return builder.build();
}

static HTTPHeaders fromMultiMap(Multimap<String, String> headers) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as I mentioned above. Do we actually have a use case for a bunch of these static methods? I would try to keep those to the amount we actually need

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the methods are tested, is that a big deal? This one indeed has no usage today, but more auth managers in the future could use it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Iceberg project typically tries to only add things to APIs that are needed for current use cases instead of planning for potential use cases that may or may not actually exist/happen (independent of whether there are tests for unused methods or not).
You also have to keep in mind that everything that's being added to iceberg-core can't be easily changed without going through an API deprecation cycle, so we're trying to be mindful about what's really required in terms of APIs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, let's remove all the static methods for now.

return fromMap(headers.asMap());
}

/** Represents an HTTP header as a name-value pair. */
@Value.Style(redactedMask = "****", depluralize = true)
@Value.Immutable
@SuppressWarnings({"ImmutablesStyle", "SafeLoggingPropagation"})
interface HTTPHeader {

String name();

@Value.Redacted
danielcweeks marked this conversation as resolved.
Show resolved Hide resolved
String value();

static HTTPHeader of(String name, String value) {
return ImmutableHTTPHeader.builder().name(name).value(value).build();
}
}
}
91 changes: 91 additions & 0 deletions core/src/main/java/org/apache/iceberg/rest/HTTPRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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 com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URI;
import java.util.Map;
import javax.annotation.Nullable;
import org.immutables.value.Value;

/** Represents an HTTP request. */
@Value.Style(redactedMask = "****", depluralize = true)
@Value.Immutable
@SuppressWarnings({"ImmutablesStyle", "SafeLoggingPropagation"})
public interface HTTPRequest {

enum HTTPMethod {
GET,
HEAD,
POST,
DELETE
}

/**
* Returns the base URI configured at the REST client level. The base URI is used to construct the
* full {@link #requestUri()}.
*/
URI baseUri();

/**
* Returns the full URI of this request. The URI is constructed from the base URI, path, and query
* parameters. It cannot be modified directly.
*/
@Value.Lazy
default URI requestUri() {
return RESTUtil.buildRequestUri(this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to me it seems like we should rather embed the functionality of building an URI from a HTTPRequest here (and also do the same for encoding the body). Additionally, building an URI from the path does some validation to make sure it doesn't start with a /. I'd say this is something that we can move into a check method annotated with @Value.Check. That way we don't need the util methods in RESTUtil and can move testing into TestHTTPRequest. I've added an example diff below:

--- a/core/src/main/java/org/apache/iceberg/rest/HTTPRequest.java
+++ b/core/src/main/java/org/apache/iceberg/rest/HTTPRequest.java
@@ -18,10 +18,14 @@
  */
 package org.apache.iceberg.rest;

+import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.Map;
 import javax.annotation.Nullable;
+import org.apache.hc.core5.net.URIBuilder;
+import org.apache.iceberg.exceptions.RESTException;
 import org.immutables.value.Value;

 /** Represents an HTTP request. */
@@ -49,7 +53,19 @@ public interface HTTPRequest {
    */
   @Value.Lazy
   default URI requestUri() {
-    return RESTUtil.buildRequestUri(this);
+    // if full path is provided, use the input path as path
+    String fullPath =
+        (path().startsWith("https://") || path().startsWith("http://"))
+            ? path()
+            : String.format("%s/%s", baseUri(), path());
+    try {
+      URIBuilder builder = new URIBuilder(RESTUtil.stripTrailingSlash(fullPath));
+      queryParameters().forEach(builder::addParameter);
+      return builder.build();
+    } catch (URISyntaxException e) {
+      throw new RESTException(
+          "Failed to create request URI from base %s, params %s", fullPath, queryParameters());
+    }
   }

   /** Returns the HTTP method of this request. */
@@ -77,7 +93,17 @@ public interface HTTPRequest {
   @Nullable
   @Value.Redacted
   default String encodedBody() {
-    return RESTUtil.encodeRequestBody(this);
+    Object body = body();
+    if (body instanceof Map) {
+      return RESTUtil.encodeFormData((Map<?, ?>) body);
+    } else if (body != null) {
+      try {
+        return mapper().writeValueAsString(body);
+      } catch (JsonProcessingException e) {
+        throw new RESTException(e, "Failed to encode request body: %s", body);
+      }
+    }
+    return null;
   }

   /**
@@ -88,4 +114,13 @@ public interface HTTPRequest {
   default ObjectMapper mapper() {
     return RESTObjectMapper.mapper();
   }
+
+  @Value.Check
+  default void check() {
+    if (path().startsWith("/")) {
+      throw new RESTException(
+          "Received a malformed path for a REST request: %s. Paths should not start with /",
+          path());
+    }
+  }
 }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that's a good idea.

}

/** Returns the HTTP method of this request. */
HTTPMethod method();

/** Returns the path of this request. */
String path();

/** Returns the query parameters of this request. */
Map<String, String> queryParameters();

/** Returns the headers of this request. */
@Value.Default
default HTTPHeaders headers() {
danielcweeks marked this conversation as resolved.
Show resolved Hide resolved
return HTTPHeaders.EMPTY;
}

/** Returns the raw, unencoded request body. */
@Nullable
@Value.Redacted
Object body();

/** Returns the encoded request body as a string. */
@Value.Lazy
@Nullable
@Value.Redacted
default String encodedBody() {
return RESTUtil.encodeRequestBody(this);
}

/**
* Returns the {@link ObjectMapper} to use for encoding the request body. The default is {@link
* RESTObjectMapper#mapper()}.
*/
@Value.Default
default ObjectMapper mapper() {
return RESTObjectMapper.mapper();
}
}
46 changes: 46 additions & 0 deletions core/src/main/java/org/apache/iceberg/rest/RESTUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@
*/
package org.apache.iceberg.rest;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.UncheckedIOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.apache.hc.core5.net.URIBuilder;
import org.apache.iceberg.catalog.Namespace;
import org.apache.iceberg.exceptions.RESTException;
import org.apache.iceberg.relocated.com.google.common.base.Joiner;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.base.Splitter;
Expand Down Expand Up @@ -215,4 +220,45 @@ public static Namespace decodeNamespace(String encodedNs) {

return Namespace.of(levels);
}

/** Builds a request URI from a base URI and an {@link HTTPRequest}. */
public static URI buildRequestUri(HTTPRequest request) {
// if full path is provided, use the input path as path
String path = request.path();
if (path.startsWith("/")) {
throw new RESTException(
"Received a malformed path for a REST request: %s. Paths should not start with /", path);
}
String fullPath =
(path.startsWith("https://") || path.startsWith("http://"))
? path
: String.format("%s/%s", request.baseUri(), path);
try {
URIBuilder builder = new URIBuilder(stripTrailingSlash(fullPath));
request.queryParameters().forEach(builder::addParameter);
return builder.build();
} catch (URISyntaxException e) {
throw new RESTException(
"Failed to create request URI from base %s, params %s",
fullPath, request.queryParameters());
}
}

/**
* Encodes the body of an HTTP request as a String. By convention, maps are encoded as form data
* and other objects are encoded as JSON.
*/
public static String encodeRequestBody(HTTPRequest request) {
Object body = request.body();
if (body instanceof Map) {
return encodeFormData((Map<?, ?>) body);
} else if (body != null) {
try {
return request.mapper().writeValueAsString(body);
} catch (JsonProcessingException e) {
throw new RESTException(e, "Failed to encode request body: %s", body);
}
}
return null;
}
}
Loading