-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
7db53d8
e486e0d
0449744
d9a05fb
4c0e650
56ff7b2
ca6eb1e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* 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.stream.Collectors; | ||
import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
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 { | ||
|
||
HTTPHeaders EMPTY = of(); | ||
|
||
/** Returns all the header entries in this group. */ | ||
List<HTTPHeader> entries(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like we should also annotate this as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @danielcweeks |
||
|
||
/** Returns all the entries in this group for the given name (case-insensitive). */ | ||
default List<HTTPHeader> entries(String name) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there are reason we're making this overridable? It seems like we should insulate the case behavior by making this a private static method. |
||
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 withHeaderIfAbsent(HTTPHeader header) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same idiomatic comment here. Generally we use |
||
Preconditions.checkNotNull(header, "header"); | ||
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 withHeaderIfAbsent(HTTPHeaders headers) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like
|
||
Preconditions.checkNotNull(headers, "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(); | ||
} | ||
|
||
/** 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean that we redact all header values? Maybe I don't fully understand the Immutable behavior here, but that's a bit problematic because we probably only want to redact sensitive headers (like |
||
String value(); | ||
|
||
@Value.Check | ||
default void check() { | ||
if (name().isEmpty()) { | ||
throw new IllegalArgumentException("Header name cannot be empty"); | ||
} | ||
} | ||
|
||
static HTTPHeader of(String name, String value) { | ||
return ImmutableHTTPHeader.builder().name(name).value(value).build(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* 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.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. */ | ||
@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() { | ||
// 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. */ | ||
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @danielcweeks @nastra FYI, I wasn't satisfied with returning |
||
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() { | ||
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; | ||
} | ||
|
||
/** | ||
* 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(); | ||
} | ||
|
||
@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()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* 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 static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import org.apache.iceberg.rest.HTTPHeaders.HTTPHeader; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class TestHTTPHeaders { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to add a test for duplicate behavior as well. |
||
|
||
private final HTTPHeaders headers = | ||
HTTPHeaders.of( | ||
HTTPHeader.of("header1", "value1a"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please also add a test where the key/value is null |
||
HTTPHeader.of("HEADER1", "value1b"), | ||
HTTPHeader.of("header2", "value2")); | ||
|
||
@Test | ||
void entries() { | ||
assertThat(headers.entries("header1")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please also add tests where |
||
.containsExactly(HTTPHeader.of("header1", "value1a"), HTTPHeader.of("HEADER1", "value1b")); | ||
assertThat(headers.entries("HEADER1")) | ||
.containsExactly(HTTPHeader.of("header1", "value1a"), HTTPHeader.of("HEADER1", "value1b")); | ||
assertThat(headers.entries("header2")).containsExactly(HTTPHeader.of("header2", "value2")); | ||
assertThat(headers.entries("HEADER2")).containsExactly(HTTPHeader.of("header2", "value2")); | ||
assertThat(headers.entries("header3")).isEmpty(); | ||
assertThat(headers.entries("HEADER3")).isEmpty(); | ||
assertThat(headers.entries(null)).isEmpty(); | ||
} | ||
|
||
@Test | ||
void contains() { | ||
assertThat(headers.contains("header1")).isTrue(); | ||
assertThat(headers.contains("HEADER1")).isTrue(); | ||
assertThat(headers.contains("header2")).isTrue(); | ||
assertThat(headers.contains("HEADER2")).isTrue(); | ||
assertThat(headers.contains("header3")).isFalse(); | ||
assertThat(headers.contains("HEADER3")).isFalse(); | ||
assertThat(headers.contains(null)).isFalse(); | ||
} | ||
|
||
@Test | ||
void withHeaderIfAbsentHTTPHeader() { | ||
HTTPHeaders actual = headers.withHeaderIfAbsent(HTTPHeader.of("Header1", "value1c")); | ||
assertThat(actual).isSameAs(headers); | ||
|
||
actual = headers.withHeaderIfAbsent(HTTPHeader.of("header3", "value3")); | ||
assertThat(actual.entries()) | ||
.containsExactly( | ||
HTTPHeader.of("header1", "value1a"), | ||
HTTPHeader.of("HEADER1", "value1b"), | ||
HTTPHeader.of("header2", "value2"), | ||
HTTPHeader.of("header3", "value3")); | ||
|
||
assertThatThrownBy(() -> headers.withHeaderIfAbsent((HTTPHeader) null)) | ||
.isInstanceOf(NullPointerException.class) | ||
.hasMessage("header"); | ||
} | ||
|
||
@Test | ||
void withHeaderIfAbsentHTTPHeaders() { | ||
HTTPHeaders actual = | ||
headers.withHeaderIfAbsent(HTTPHeaders.of(HTTPHeader.of("Header1", "value1c"))); | ||
assertThat(actual).isSameAs(headers); | ||
|
||
actual = | ||
headers.withHeaderIfAbsent( | ||
ImmutableHTTPHeaders.builder() | ||
.addEntry(HTTPHeader.of("Header1", "value1c")) | ||
.addEntry(HTTPHeader.of("header3", "value3")) | ||
.build()); | ||
assertThat(actual) | ||
.isEqualTo( | ||
ImmutableHTTPHeaders.builder() | ||
.addEntries( | ||
HTTPHeader.of("header1", "value1a"), | ||
HTTPHeader.of("HEADER1", "value1b"), | ||
HTTPHeader.of("header2", "value2"), | ||
HTTPHeader.of("header3", "value3")) | ||
.build()); | ||
|
||
assertThatThrownBy(() -> headers.withHeaderIfAbsent((HTTPHeaders) null)) | ||
.isInstanceOf(NullPointerException.class) | ||
.hasMessage("headers"); | ||
} | ||
|
||
@Test | ||
void invalidHeader() { | ||
// invalid input (null name or value) | ||
assertThatThrownBy(() -> HTTPHeader.of(null, "value1")) | ||
.isInstanceOf(NullPointerException.class) | ||
.hasMessage("name"); | ||
assertThatThrownBy(() -> HTTPHeader.of("header1", null)) | ||
.isInstanceOf(NullPointerException.class) | ||
.hasMessage("value"); | ||
|
||
// invalid input (empty name) | ||
assertThatThrownBy(() -> HTTPHeader.of("", "value1")) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("Header name cannot be empty"); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class is loosely inspired by
org.apache.hc.core5.http.message.HeaderGroup
.