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

Allow customization of netty channel handles before and during decompression #10261

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0925b7e
Make the decompressor extensible and allow for a plugin to define a s…
cwperks Sep 27, 2023
a8d9b73
Add to CHANGELOG
cwperks Sep 27, 2023
5188697
Use getMethods
cwperks Sep 28, 2023
cd9e72f
Create new instance of each inbound handler
cwperks Sep 28, 2023
bf2d707
Update name
cwperks Sep 28, 2023
bfc3ceb
Update name
cwperks Sep 28, 2023
2584e94
Update test
cwperks Sep 28, 2023
e02a8a3
Add netty request tests
cwperks Sep 28, 2023
0a0214d
Merge branch 'main' into improve-compressed-requests
cwperks Sep 28, 2023
104c512
Add test for createRestRequest
cwperks Sep 28, 2023
aec43e9
Very basic header validator
peternied Sep 28, 2023
46f3e4a
Revert "Very basic header validator"
cwperks Sep 29, 2023
16ecd7f
Remove createDecompressor extension point in favor of attributeKey th…
cwperks Sep 29, 2023
e6209c7
Minor update
cwperks Sep 29, 2023
54a0a96
Match previous name
cwperks Sep 29, 2023
f4eb416
Add license header
cwperks Sep 29, 2023
226299a
Back out DelegatingRestHandler changes to simplify this PR and follow…
cwperks Sep 29, 2023
4689e30
Small update to test
cwperks Sep 29, 2023
c227e6e
remove printStackTrace
cwperks Sep 29, 2023
a83c64f
Merge branch 'main' into improve-compressed-requests
cwperks Oct 3, 2023
aec3ad3
Remove channel attributes that are request specific
cwperks Oct 4, 2023
01dfa89
Move new AttributeKeys to security plugin
cwperks Oct 4, 2023
a1d6968
Merge branch 'main' into improve-compressed-requests
cwperks Oct 4, 2023
3085f64
Add charset
cwperks Oct 4, 2023
7ca4c7e
Add javadoc on new extension points
cwperks Oct 5, 2023
4c49159
Merge branch 'main' into improve-compressed-requests
cwperks Oct 5, 2023
23feffd
Merge branch 'improve-compressed-requests' of https://github.com/cwpe…
cwperks Oct 5, 2023
02b92ab
Single request class
cwperks Oct 5, 2023
5af481b
Revert access modifier changes
cwperks Oct 5, 2023
91fc5bc
Spotless
cwperks Oct 5, 2023
ddaca29
Remove createRestRequest changes in favor of new security rest channe…
cwperks Oct 5, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- [Telemetry-Otel] Added support for OtlpGrpcSpanExporter exporter ([#9666](https://github.com/opensearch-project/OpenSearch/pull/9666))
- Async blob read support for encrypted containers ([#10131](https://github.com/opensearch-project/OpenSearch/pull/10131))
- Add capability to restrict async durability mode for remote indexes ([#10189](https://github.com/opensearch-project/OpenSearch/pull/10189))
- Improve compressed request handling ([#10261](https://github.com/opensearch-project/OpenSearch/pull/10261))

### Dependencies
- Bump `peter-evans/create-or-update-comment` from 2 to 3 ([#9575](https://github.com/opensearch-project/OpenSearch/pull/9575))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.http.netty4;

import org.opensearch.rest.RestRequest;

import java.util.AbstractMap;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;

public class AbstractNetty4HttpRequest {

protected HttpHeadersMap headers;
protected Exception inboundException;

protected RestRequest.Method getHttpMethod(HttpRequest request) {
HttpMethod httpMethod = request.method();
if (httpMethod == HttpMethod.GET) return RestRequest.Method.GET;

if (httpMethod == HttpMethod.POST) return RestRequest.Method.POST;

if (httpMethod == HttpMethod.PUT) return RestRequest.Method.PUT;

if (httpMethod == HttpMethod.DELETE) return RestRequest.Method.DELETE;

if (httpMethod == HttpMethod.HEAD) {
return RestRequest.Method.HEAD;

Check warning on line 41 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java#L41

Added line #L41 was not covered by tests
}

if (httpMethod == HttpMethod.OPTIONS) {
return RestRequest.Method.OPTIONS;
}

if (httpMethod == HttpMethod.PATCH) {
return RestRequest.Method.PATCH;

Check warning on line 49 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java#L49

Added line #L49 was not covered by tests
}

if (httpMethod == HttpMethod.TRACE) {
return RestRequest.Method.TRACE;

Check warning on line 53 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java#L53

Added line #L53 was not covered by tests
}

if (httpMethod == HttpMethod.CONNECT) {
return RestRequest.Method.CONNECT;

Check warning on line 57 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java#L57

Added line #L57 was not covered by tests
}

throw new IllegalArgumentException("Unexpected http method: " + httpMethod);

Check warning on line 60 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java#L60

Added line #L60 was not covered by tests
}

/**
* A wrapper of {@link HttpHeaders} that implements a map to prevent copying unnecessarily. This class does not support modifications
* and due to the underlying implementation, it performs case insensitive lookups of key to values.
*
* It is important to note that this implementation does have some downsides in that each invocation of the
* {@link #values()} and {@link #entrySet()} methods will perform a copy of the values in the HttpHeaders rather than returning a
* view of the underlying values.
*/
protected static class HttpHeadersMap implements Map<String, List<String>> {

private final HttpHeaders httpHeaders;

HttpHeadersMap(HttpHeaders httpHeaders) {
this.httpHeaders = httpHeaders;
}

@Override
public int size() {
return httpHeaders.size();

Check warning on line 81 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java#L81

Added line #L81 was not covered by tests
}

@Override
public boolean isEmpty() {
return httpHeaders.isEmpty();

Check warning on line 86 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java#L86

Added line #L86 was not covered by tests
}

@Override
public boolean containsKey(Object key) {
return key instanceof String && httpHeaders.contains((String) key);
}

@Override
public boolean containsValue(Object value) {
return value instanceof List && httpHeaders.names().stream().map(httpHeaders::getAll).anyMatch(value::equals);
}

@Override
public List<String> get(Object key) {
return key instanceof String ? httpHeaders.getAll((String) key) : null;
}

@Override
public List<String> put(String key, List<String> value) {
throw new UnsupportedOperationException("modifications are not supported");

Check warning on line 106 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java#L106

Added line #L106 was not covered by tests
}

@Override
public List<String> remove(Object key) {
throw new UnsupportedOperationException("modifications are not supported");

Check warning on line 111 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java#L111

Added line #L111 was not covered by tests
}

@Override
public void putAll(Map<? extends String, ? extends List<String>> m) {
throw new UnsupportedOperationException("modifications are not supported");

Check warning on line 116 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java#L116

Added line #L116 was not covered by tests
}

@Override
public void clear() {
throw new UnsupportedOperationException("modifications are not supported");

Check warning on line 121 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java#L121

Added line #L121 was not covered by tests
}

@Override
public Set<String> keySet() {
return httpHeaders.names();

Check warning on line 126 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java#L126

Added line #L126 was not covered by tests
}

@Override
public Collection<List<String>> values() {
return httpHeaders.names().stream().map(k -> Collections.unmodifiableList(httpHeaders.getAll(k))).collect(Collectors.toList());

Check warning on line 131 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/AbstractNetty4HttpRequest.java#L131

Added line #L131 was not covered by tests
}

@Override
public Set<Entry<String, List<String>>> entrySet() {
return httpHeaders.names()
.stream()
.map(k -> new AbstractMap.SimpleImmutableEntry<>(k, httpHeaders.getAll(k)))
.collect(Collectors.toSet());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.http.netty4;

import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.http.HttpRequest;
import org.opensearch.rest.RestRequest;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.cookie.Cookie;
import io.netty.handler.codec.http.cookie.ServerCookieDecoder;
import io.netty.handler.codec.http.cookie.ServerCookieEncoder;

public class Netty4DefaultHttpRequest extends AbstractNetty4HttpRequest implements HttpRequest {
peternied marked this conversation as resolved.
Show resolved Hide resolved

private final DefaultHttpRequest request;

public Netty4DefaultHttpRequest(DefaultHttpRequest request) {
this(request, new HttpHeadersMap(request.headers()), null);
}

Check warning on line 34 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java#L33-L34

Added lines #L33 - L34 were not covered by tests

private Netty4DefaultHttpRequest(DefaultHttpRequest request, HttpHeadersMap headers, Exception inboundException) {
this.request = request;
this.headers = headers;
this.inboundException = inboundException;
}

Check warning on line 40 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java#L36-L40

Added lines #L36 - L40 were not covered by tests

@Override
public RestRequest.Method method() {
return getHttpMethod(request);

Check warning on line 44 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java#L44

Added line #L44 was not covered by tests
}

@Override
public String uri() {
return request.uri();

Check warning on line 49 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java#L49

Added line #L49 was not covered by tests
}

@Override
public BytesReference content() {
// throw new RuntimeException("Not implemented");
return BytesArray.EMPTY;

Check warning on line 55 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java#L55

Added line #L55 was not covered by tests
}

@Override
public final Map<String, List<String>> getHeaders() {
return headers;

Check warning on line 60 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java#L60

Added line #L60 was not covered by tests
}

@Override
public List<String> strictCookies() {
String cookieString = request.headers().get(HttpHeaderNames.COOKIE);

Check warning on line 65 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java#L65

Added line #L65 was not covered by tests
if (cookieString != null) {
Set<Cookie> cookies = ServerCookieDecoder.STRICT.decode(cookieString);

Check warning on line 67 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java#L67

Added line #L67 was not covered by tests
if (!cookies.isEmpty()) {
return ServerCookieEncoder.STRICT.encode(cookies);

Check warning on line 69 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java#L69

Added line #L69 was not covered by tests
}
}
return Collections.emptyList();

Check warning on line 72 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java#L72

Added line #L72 was not covered by tests
}

@Override
public HttpVersion protocolVersion() {
if (request.protocolVersion().equals(io.netty.handler.codec.http.HttpVersion.HTTP_1_0)) {
return HttpRequest.HttpVersion.HTTP_1_0;

Check warning on line 78 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java#L78

Added line #L78 was not covered by tests
} else if (request.protocolVersion().equals(io.netty.handler.codec.http.HttpVersion.HTTP_1_1)) {
return HttpRequest.HttpVersion.HTTP_1_1;

Check warning on line 80 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java#L80

Added line #L80 was not covered by tests
} else {
throw new IllegalArgumentException("Unexpected http protocol version: " + request.protocolVersion());

Check warning on line 82 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java#L82

Added line #L82 was not covered by tests
}
}

@Override
public HttpRequest removeHeader(String header) {
return null;

Check warning on line 88 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java#L88

Added line #L88 was not covered by tests
}

@Override
public Netty4HttpResponse createResponse(RestStatus status, BytesReference content) {
return new Netty4HttpResponse(request.headers(), request.protocolVersion(), status, content);

Check warning on line 93 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java#L93

Added line #L93 was not covered by tests
}

@Override
public Exception getInboundException() {
return inboundException;

Check warning on line 98 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java#L98

Added line #L98 was not covered by tests
}

@Override
public void release() {
// do nothing
}

Check warning on line 104 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java#L104

Added line #L104 was not covered by tests

@Override
public HttpRequest releaseAndCopy() {
return this;

Check warning on line 108 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java#L108

Added line #L108 was not covered by tests
}

public DefaultHttpRequest nettyRequest() {
return request;

Check warning on line 112 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4DefaultHttpRequest.java#L112

Added line #L112 was not covered by tests
}
}
Loading
Loading