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

ISPN-11765 Add internal headers to CORS rules #8286

Merged
merged 1 commit into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 25 additions & 4 deletions server/rest/src/main/java/org/infinispan/rest/ALPNHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package org.infinispan.rest;

import static io.netty.handler.codec.http.HttpHeaderNames.CACHE_CONTROL;
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
import static io.netty.handler.codec.http.HttpHeaderNames.IF_MODIFIED_SINCE;
import static io.netty.handler.codec.http.HttpHeaderNames.IF_NONE_MATCH;
import static io.netty.handler.codec.http.HttpHeaderNames.IF_UNMODIFIED_SINCE;
import static io.netty.handler.codec.http.HttpMethod.DELETE;
import static io.netty.handler.codec.http.HttpMethod.GET;
import static io.netty.handler.codec.http.HttpMethod.HEAD;
import static io.netty.handler.codec.http.HttpMethod.OPTIONS;
import static io.netty.handler.codec.http.HttpMethod.POST;
import static io.netty.handler.codec.http.HttpMethod.PUT;
import static org.infinispan.rest.NettyRestRequest.CREATED_HEADER;
import static org.infinispan.rest.NettyRestRequest.EXTENDED_HEADER;
import static org.infinispan.rest.NettyRestRequest.FLAGS_HEADER;
import static org.infinispan.rest.NettyRestRequest.KEY_CONTENT_TYPE_HEADER;
import static org.infinispan.rest.NettyRestRequest.LAST_USED_HEADER;
import static org.infinispan.rest.NettyRestRequest.MAX_TIME_IDLE_HEADER;
import static org.infinispan.rest.NettyRestRequest.TTL_SECONDS_HEADER;
import static org.infinispan.rest.RestChannelInitializer.MAX_HEADER_SIZE;
import static org.infinispan.rest.RestChannelInitializer.MAX_INITIAL_LINE_SIZE;

Expand All @@ -13,8 +31,6 @@
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.HttpContentCompressor;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.HttpServerUpgradeHandler;
Expand Down Expand Up @@ -131,8 +147,13 @@ private List<CorsConfig> addLocalhostPermissions(List<CorsConfig> corsRules, int
String localIpv6 = scheme + "://" + "[::1]" + ":" + port;
CorsConfig config = CorsConfigBuilder.forOrigins(localIpv4, localDomain, localIpv6)
.allowCredentials()
.allowedRequestMethods(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE, HttpMethod.HEAD, HttpMethod.OPTIONS)
.allowedRequestHeaders(HttpHeaderNames.CONTENT_TYPE.toString()).build();
.allowedRequestMethods(GET, POST, PUT, DELETE, HEAD, OPTIONS)
// Not all browsers support "*" (https://github.com/whatwg/fetch/issues/251) so we need to add each
// header individually
.allowedRequestHeaders(CACHE_CONTROL, CONTENT_TYPE, CREATED_HEADER, EXTENDED_HEADER, FLAGS_HEADER,
IF_MODIFIED_SINCE, IF_UNMODIFIED_SINCE, IF_NONE_MATCH, KEY_CONTENT_TYPE_HEADER,
MAX_TIME_IDLE_HEADER, LAST_USED_HEADER, TTL_SECONDS_HEADER)
.build();
configs.add(config);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public class NettyRestRequest implements RestRequest {

private static final MediaType DEFAULT_KEY_CONTENT_TYPE = MediaType.fromString("application/x-java-object;type=java.lang.String");

public static final String CREATED_HEADER = "created";
public static final String EXTENDED_HEADER = "extended";
private static final String MAX_TIME_IDLE_HEADER = "maxIdleTimeSeconds";
private static final String CREATED_HEADER = "created";
private static final String LAST_USED_HEADER = "lastUsed";
private static final String TTL_SECONDS_HEADER = "timeToLiveSeconds";
private static final String KEY_CONTENT_TYPE_HEADER = "key-content-type";
private static final String FLAGS_HEADER = "flags";
public static final String FLAGS_HEADER = "flags";
public static final String KEY_CONTENT_TYPE_HEADER = "key-content-type";
public static final String MAX_TIME_IDLE_HEADER = "maxIdleTimeSeconds";
public static final String LAST_USED_HEADER = "lastUsed";
public static final String TTL_SECONDS_HEADER = "timeToLiveSeconds";

private final FullHttpRequest request;
private final Map<String, List<String>> parameters;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.infinispan.rest.assertion;

import static org.testng.AssertJUnit.assertTrue;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -10,6 +12,8 @@
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.assertj.core.api.Assertions;
import org.eclipse.jetty.client.api.ContentResponse;
Expand Down Expand Up @@ -120,8 +124,17 @@ public ResponseAssertion hasHeaderMatching(String header, String regexp) {
return this;
}

public ResponseAssertion hasHeaderWithValues(String header, CharSequence... headers) {
Set<String> expected = Arrays.stream(headers).map(c -> c.toString().toLowerCase()).collect(Collectors.toSet());
for (String headerValue : response.getHeaders().get(header).split(",")) {
assertTrue(expected.contains(headerValue.toLowerCase()));
}
return this;
}

public ResponseAssertion containsAllHeaders(String... headers) {
Assertions.assertThat(response.getHeaders().stream().map(HttpField::getName)).contains(headers);
String[] values = Arrays.stream(headers).map(String::toLowerCase).toArray(String[]::new);
Assertions.assertThat(response.getHeaders().stream().map(HttpField::getName)).contains(values);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package org.infinispan.rest.resources;

import static io.netty.handler.codec.http.HttpHeaderNames.CACHE_CONTROL;
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
import static io.netty.handler.codec.http.HttpHeaderNames.IF_MODIFIED_SINCE;
import static io.netty.handler.codec.http.HttpHeaderNames.IF_NONE_MATCH;
import static io.netty.handler.codec.http.HttpHeaderNames.IF_UNMODIFIED_SINCE;
import static io.netty.handler.codec.http.HttpHeaders.Names.ACCESS_CONTROL_ALLOW_HEADERS;
import static io.netty.handler.codec.http.HttpHeaders.Names.ACCESS_CONTROL_ALLOW_METHODS;
import static io.netty.handler.codec.http.HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN;
import static org.eclipse.jetty.http.HttpHeader.ACCEPT_ENCODING;
import static org.eclipse.jetty.http.HttpHeader.CONTENT_TYPE;
import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_JSON;
import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_JSON_TYPE;
import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_OBJECT;
Expand All @@ -14,6 +21,13 @@
import static org.infinispan.commons.util.Util.getResourceAsString;
import static org.infinispan.dataconversion.Gzip.decompress;
import static org.infinispan.rest.JSONConstants.TYPE;
import static org.infinispan.rest.NettyRestRequest.CREATED_HEADER;
import static org.infinispan.rest.NettyRestRequest.EXTENDED_HEADER;
import static org.infinispan.rest.NettyRestRequest.FLAGS_HEADER;
import static org.infinispan.rest.NettyRestRequest.KEY_CONTENT_TYPE_HEADER;
import static org.infinispan.rest.NettyRestRequest.LAST_USED_HEADER;
import static org.infinispan.rest.NettyRestRequest.MAX_TIME_IDLE_HEADER;
import static org.infinispan.rest.NettyRestRequest.TTL_SECONDS_HEADER;
import static org.infinispan.rest.assertion.ResponseAssertion.assertThat;
import static org.testng.AssertJUnit.assertEquals;

Expand Down Expand Up @@ -312,7 +326,10 @@ public void testCORSPreflight() throws Exception {

assertThat(preFlight).isOk();
assertThat(preFlight).hasNoContent();
assertThat(preFlight).containsAllHeaders("access-control-allow-origin", "access-control-allow-methods");
assertThat(preFlight).containsAllHeaders(ACCESS_CONTROL_ALLOW_ORIGIN, ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_HEADERS);
assertThat(preFlight).hasHeaderWithValues(ACCESS_CONTROL_ALLOW_HEADERS, CACHE_CONTROL, CONTENT_TYPE, CREATED_HEADER, EXTENDED_HEADER, FLAGS_HEADER,
IF_MODIFIED_SINCE, IF_UNMODIFIED_SINCE, IF_NONE_MATCH, KEY_CONTENT_TYPE_HEADER,
MAX_TIME_IDLE_HEADER, LAST_USED_HEADER, TTL_SECONDS_HEADER);
}

@Test
Expand Down Expand Up @@ -434,7 +451,7 @@ public void testReplaceExistingObject() throws Exception {
private ContentResponse writeJsonToCache(String key, String json, String cacheName) throws Exception {
return client.newRequest(String.format("http://localhost:%d/rest/v2/caches/%s/%s", restServer().getPort(), cacheName, key))
.content(new StringContentProvider(json))
.header(CONTENT_TYPE, APPLICATION_JSON_TYPE)
.header(HttpHeader.CONTENT_TYPE, APPLICATION_JSON_TYPE)
.method(HttpMethod.PUT).send();
}

Expand All @@ -451,7 +468,7 @@ public void testServerDeserialization() throws Exception {
ContentResponse jsonResponse = client
.newRequest(String.format("http://localhost:%d/rest/v2/caches/%s/%s", restServer().getPort(), "objectCache", "addr2"))
.content(new BytesContentProvider(jsonMarshalled))
.header(CONTENT_TYPE, APPLICATION_JSON_TYPE)
.header(HttpHeader.CONTENT_TYPE, APPLICATION_JSON_TYPE)
.method(HttpMethod.PUT)
.send();

Expand All @@ -461,7 +478,7 @@ public void testServerDeserialization() throws Exception {
ContentResponse xmlResponse = client
.newRequest(String.format("http://localhost:%d/rest/v2/caches/%s/%s", restServer().getPort(), "objectCache", "addr3"))
.content(new BytesContentProvider(xmlMarshalled))
.header(CONTENT_TYPE, APPLICATION_XML_TYPE)
.header(HttpHeader.CONTENT_TYPE, APPLICATION_XML_TYPE)
.method(HttpMethod.PUT)
.send();

Expand All @@ -471,7 +488,7 @@ public void testServerDeserialization() throws Exception {
ContentResponse serializationResponse = client
.newRequest(String.format("http://localhost:%d/rest/v2/caches/%s/%s", restServer().getPort(), "objectCache", "addr4"))
.content(new BytesContentProvider(javaMarshalled))
.header(CONTENT_TYPE, APPLICATION_SERIALIZED_OBJECT_TYPE)
.header(HttpHeader.CONTENT_TYPE, APPLICATION_SERIALIZED_OBJECT_TYPE)
.method(HttpMethod.PUT)
.send();

Expand Down