diff --git a/core/src/main/java/org/apache/brooklyn/core/BrooklynFeatureEnablement.java b/core/src/main/java/org/apache/brooklyn/core/BrooklynFeatureEnablement.java index 8bc2ffd298d..eb5693a478c 100644 --- a/core/src/main/java/org/apache/brooklyn/core/BrooklynFeatureEnablement.java +++ b/core/src/main/java/org/apache/brooklyn/core/BrooklynFeatureEnablement.java @@ -61,6 +61,37 @@ public class BrooklynFeatureEnablement { /** whether feeds are automatically registered when set on entities, so that they are persisted */ public static final String FEATURE_FEED_REGISTRATION_PROPERTY = FEATURE_PROPERTY_PREFIX+".feedRegistration"; + /** + * We strongly discourage enabling CORS! + * Using CORS expose you at great security risk! + * If you are thinking about using CORS on Apache Brooklyn side then probably that's the wrong solution to your problem. + * We recommend using middleware for delegating API requests from third party web applications. + * Apache Brooklyn API requests should be exposed to third party web apps with great attention and complete testing. + * The right fix is to change your calling structure; architecturally, the browser shouldn't be calling the Apache Brooklyn APIs directly. + * A web app should be interacting solely with the Apache Brooklyn server. + * If there is a need to get information from Apache Brooklyn APIs then, + * it could either simply proxy the request or could do the request on the client's behalf and potentially further processing on the results before finally getting back to the client. + * + * If brooklyn.experimental.feature.corsCxfFeature.allowedOrigins is not supplied then allowedOrigins will be on all domains. + * + * Currently there is no fine per API request control it is rather applied to the entire server. + * Even if you have per API request control and apply CORS to groups of pages/resources, + * then you have to think about how to configure the values that get added to the CORS header, + * as you really don't want to use a "*" wildcard. + * Also you have to think about what the user interface is to capture this config, and maybe issues around how to persist it, upgrade etc. + * + * It is best when web app communicates just with its own server, not with multiple servers. + * It's the Apache Brooklyn server that should be the single point of contact to moderate and control access to the information from the AMP API, which should never be independently exposed to a web UI. + * This sort of architecture can give you additional headaches behind proxies, with firewalls, etc. + * For another thing, CORS can be used and can be secure enough up to a point if implemented right (and that's not a trivial 'if'), + * but it is still an additional attack vector that can be exploited by mitm attacks etc. + * In short, the proposed architecture and use of CORS is more complex, less secure, + * and more difficult to manage than the alternative of web client ---> Apache Brooklyn Server ----> fan out to backend servers + Apache Brooklyn API + etc. + * + * Notes by Geoff Macartney. + */ + public static final String FEATURE_CORS_CXF_PROPERTY = FEATURE_PROPERTY_PREFIX + ".corsCxfFeature"; + public static final String FEATURE_CATALOG_PERSISTENCE_PROPERTY = FEATURE_PROPERTY_PREFIX+".catalogPersistence"; /** whether the default standby mode is {@link HighAvailabilityMode#HOT_STANDBY} or falling back to the traditional diff --git a/karaf/features/src/main/feature/feature.xml b/karaf/features/src/main/feature/feature.xml index 9cfc49aac07..effcf5226ea 100644 --- a/karaf/features/src/main/feature/feature.xml +++ b/karaf/features/src/main/feature/feature.xml @@ -194,6 +194,7 @@ brooklyn-camp-base cxf-jaxrs + mvn:org.apache.cxf/cxf-rt-rs-security-cors/${cxf.version} mvn:com.fasterxml.jackson.jaxrs/jackson-jaxrs-json-provider/${fasterxml.jackson.version} diff --git a/launcher/src/main/java/org/apache/brooklyn/launcher/BrooklynWebServer.java b/launcher/src/main/java/org/apache/brooklyn/launcher/BrooklynWebServer.java index 933ce68dc50..1004ebc31a7 100644 --- a/launcher/src/main/java/org/apache/brooklyn/launcher/BrooklynWebServer.java +++ b/launcher/src/main/java/org/apache/brooklyn/launcher/BrooklynWebServer.java @@ -36,6 +36,8 @@ import javax.annotation.Nullable; import javax.security.auth.spi.LoginModule; +import com.google.common.collect.ImmutableList; +import org.apache.brooklyn.core.BrooklynFeatureEnablement; import org.apache.brooklyn.rest.NopSecurityHandler; import org.apache.brooklyn.api.location.PortRange; import org.apache.brooklyn.api.mgmt.ManagementContext; @@ -52,6 +54,7 @@ import org.apache.brooklyn.rest.RestApiSetup; import org.apache.brooklyn.rest.filter.CsrfTokenFilter; import org.apache.brooklyn.rest.filter.EntitlementContextFilter; +import org.apache.brooklyn.rest.filter.CorsImplSupplierFilter; import org.apache.brooklyn.rest.filter.HaHotCheckResourceFilter; import org.apache.brooklyn.rest.filter.LoggingFilter; import org.apache.brooklyn.rest.filter.NoCacheFilter; @@ -80,6 +83,7 @@ import org.apache.brooklyn.util.text.Identifiers; import org.apache.brooklyn.util.text.Strings; import org.apache.brooklyn.util.web.ContextHandlerCollectionHotSwappable; +import org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter; import org.eclipse.jetty.http.HttpVersion; import org.eclipse.jetty.jaas.JAASLoginService; import org.eclipse.jetty.server.Connector; @@ -99,6 +103,8 @@ import com.google.common.base.Throwables; import com.google.common.collect.Maps; +import static org.apache.brooklyn.rest.filter.CorsImplSupplierFilter.ALLOWED_ORIGINS; + /** * Starts the web-app running, connected to the given management context */ @@ -455,7 +461,8 @@ public synchronized void start() throws Exception { } private WebAppContext deployRestApi(WebAppContext context) { - RestApiSetup.installRest(context, + ImmutableList.Builder providersListBuilder = ImmutableList.builder(); + providersListBuilder.add( new ManagementContextProvider(), new ShutdownHandlerProvider(shutdownHandler), new RequestTaggingRsFilter(), @@ -463,6 +470,12 @@ private WebAppContext deployRestApi(WebAppContext context) { new HaHotCheckResourceFilter(), new EntitlementContextFilter(), new CsrfTokenFilter()); + if (BrooklynFeatureEnablement.isEnabled(BrooklynFeatureEnablement.FEATURE_CORS_CXF_PROPERTY)) { + providersListBuilder.add(new CorsImplSupplierFilter(managementContext)); + } + + RestApiSetup.installRest(context, + providersListBuilder.build().toArray()); RestApiSetup.installServletFilters(context, RequestTaggingFilter.class, LoggingFilter.class); diff --git a/rest/rest-resources/pom.xml b/rest/rest-resources/pom.xml index 6587993d473..80c60c0bfbf 100644 --- a/rest/rest-resources/pom.xml +++ b/rest/rest-resources/pom.xml @@ -116,6 +116,11 @@ org.eclipse.jetty jetty-server + + org.apache.cxf + cxf-rt-rs-security-cors + ${cxf.version} + org.apache.brooklyn diff --git a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/filter/CorsImplSupplierFilter.java b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/filter/CorsImplSupplierFilter.java new file mode 100644 index 00000000000..1c2c4aae094 --- /dev/null +++ b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/filter/CorsImplSupplierFilter.java @@ -0,0 +1,99 @@ +/* + * 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.brooklyn.rest.filter; + +import com.google.common.reflect.TypeToken; +import org.apache.brooklyn.api.mgmt.ManagementContext; +import org.apache.brooklyn.config.ConfigKey; +import org.apache.brooklyn.core.BrooklynFeatureEnablement; +import org.apache.brooklyn.core.config.ConfigKeys; +import org.apache.brooklyn.util.JavaGroovyEquivalents; +import org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerResponseContext; +import javax.ws.rs.ext.Provider; +import java.util.Collections; +import java.util.List; + +/** + * We strongly discourage enabling CORS! + * Using CORS expose you at great security risk! + * If you are thinking about using CORS on Apache Brooklyn side then probably that's the wrong solution to your problem. + * We recommend using middleware for delegating API requests from third party web applications. + * Apache Brooklyn API requests should be exposed to third party web apps with great attention and complete testing. + * The right fix is to change your calling structure; architecturally, the browser shouldn't be calling the Apache Brooklyn APIs directly. + * A web app should be interacting solely with the Apache Brooklyn server. + * If there is a need to get information from Apache Brooklyn APIs then, + * it could either simply proxy the request or could do the request on the client's behalf and potentially further processing on the results before finally getting back to the client. + * + * If brooklyn.experimental.feature.corsCxfFeature.allowedOrigins is not supplied then allowedOrigins will be on all domains. + * + * Currently there is no fine per API request control it is rather applied to the entire server. + * Even if you have per API request control and apply CORS to groups of pages/resources, + * then you have to think about how to configure the values that get added to the CORS header, + * as you really don't want to use a "*" wildcard. + * Also you have to think about what the user interface is to capture this config, and maybe issues around how to persist it, upgrade etc. + * + * It is best when web app communicates just with its own server, not with multiple servers. + * It's the Apache Brooklyn server that should be the single point of contact to moderate and control access to the information from the AMP API, which should never be independently exposed to a web UI. + * This sort of architecture can give you additional headaches behind proxies, with firewalls, etc. + * For another thing, CORS can be used and can be secure enough up to a point if implemented right (and that's not a trivial 'if'), + * but it is still an additional attack vector that can be exploited by mitm attacks etc. + * In short, the proposed architecture and use of CORS is more complex, less secure, + * and more difficult to manage than the alternative of web client ---> Apache Brooklyn Server ----> fan out to backend servers + Apache Brooklyn API + etc. + * + * Notes by Geoff Macartney. + */ +@Provider +public class CorsImplSupplierFilter extends CrossOriginResourceSharingFilter { + public static final ConfigKey> ALLOWED_ORIGINS = ConfigKeys.newConfigKey(new TypeToken>() {}, BrooklynFeatureEnablement.FEATURE_CORS_CXF_PROPERTY + ".allowedOrigins"); + private static final Logger LOGGER = LoggerFactory.getLogger(CorsImplSupplierFilter.class); + + private static final boolean brooklynFeatureEnabled = BrooklynFeatureEnablement.isEnabled(BrooklynFeatureEnablement.FEATURE_CORS_CXF_PROPERTY); + static { + if (brooklynFeatureEnabled) { + LOGGER.warn("CORS brooklyn feature enabled."); + } + } + + public CorsImplSupplierFilter(@Nullable ManagementContext managementContext) { + setFindResourceMethod(false); + if (managementContext != null) { + setAllowOrigins(JavaGroovyEquivalents.>elvis(managementContext.getConfig().getConfig(ALLOWED_ORIGINS), Collections.emptyList())); + } + } + + @Override + public void filter(ContainerRequestContext requestContext) { + if (brooklynFeatureEnabled) { + super.filter(requestContext); + } + } + + @Override + public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) { + if (brooklynFeatureEnabled) { + super.filter(requestContext, responseContext); + } + } +} diff --git a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/util/DefaultExceptionMapper.java b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/util/DefaultExceptionMapper.java index 010bcca12aa..cf2c9cca66d 100644 --- a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/util/DefaultExceptionMapper.java +++ b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/util/DefaultExceptionMapper.java @@ -72,7 +72,7 @@ public Response toResponse(Throwable throwable1) { Exceptions.collapse(throwable1)); } else { LOG.debug("REST request running as {} threw: {}", Entitlements.getEntitlementContext(), - Exceptions.collapse(throwable1)); + Exceptions.collapse(throwable1)); } if (LOG.isTraceEnabled()) { LOG.trace("Full details of "+Entitlements.getEntitlementContext()+" "+throwable1, throwable1); diff --git a/rest/rest-resources/src/main/resources/OSGI-INF/blueprint/service.xml b/rest/rest-resources/src/main/resources/OSGI-INF/blueprint/service.xml index c53c623f68b..fef7a7d419a 100644 --- a/rest/rest-resources/src/main/resources/OSGI-INF/blueprint/service.xml +++ b/rest/rest-resources/src/main/resources/OSGI-INF/blueprint/service.xml @@ -110,6 +110,9 @@ limitations under the License. + + + diff --git a/rest/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncher.java b/rest/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncher.java index 722e3d9fa7a..4bd0ad51bc5 100644 --- a/rest/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncher.java +++ b/rest/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncher.java @@ -19,6 +19,7 @@ package org.apache.brooklyn.rest; import static com.google.common.base.Preconditions.checkNotNull; +import static org.apache.brooklyn.rest.filter.CorsImplSupplierFilter.ALLOWED_ORIGINS; import java.io.File; import java.io.FilenameFilter; @@ -31,11 +32,13 @@ import org.apache.brooklyn.api.mgmt.ManagementContext; import org.apache.brooklyn.camp.brooklyn.BrooklynCampPlatformLauncherAbstract; import org.apache.brooklyn.camp.brooklyn.BrooklynCampPlatformLauncherNoServer; +import org.apache.brooklyn.core.BrooklynFeatureEnablement; import org.apache.brooklyn.core.internal.BrooklynProperties; import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext; import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal; import org.apache.brooklyn.core.server.BrooklynServerConfig; import org.apache.brooklyn.core.server.BrooklynServiceAttributes; +import org.apache.brooklyn.rest.filter.CorsImplSupplierFilter; import org.apache.brooklyn.rest.filter.CsrfTokenFilter; import org.apache.brooklyn.rest.filter.EntitlementContextFilter; import org.apache.brooklyn.rest.filter.HaHotCheckResourceFilter; @@ -56,6 +59,7 @@ import org.apache.brooklyn.util.net.Networking; import org.apache.brooklyn.util.os.Os; import org.apache.brooklyn.util.text.WildcardGlobs; +import org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter; import org.eclipse.jetty.jaas.JAASLoginService; import org.eclipse.jetty.server.NetworkConnector; import org.eclipse.jetty.server.Server; @@ -232,7 +236,8 @@ private WebAppContext servletContextHandler(ManagementContext managementContext) context.setAttribute(BrooklynServiceAttributes.BROOKLYN_MANAGEMENT_CONTEXT, managementContext); installWar(context); - RestApiSetup.installRest(context, + ImmutableList.Builder providersListBuilder = ImmutableList.builder(); + providersListBuilder.add( new ManagementContextProvider(), new ShutdownHandlerProvider(shutdownListener), new RequestTaggingRsFilter(), @@ -240,6 +245,12 @@ private WebAppContext servletContextHandler(ManagementContext managementContext) new HaHotCheckResourceFilter(), new EntitlementContextFilter(), new CsrfTokenFilter()); + if (BrooklynFeatureEnablement.isEnabled(BrooklynFeatureEnablement.FEATURE_CORS_CXF_PROPERTY)) { + providersListBuilder.add(new CorsImplSupplierFilter(managementContext)); + } + RestApiSetup.installRest(context, + providersListBuilder.build().toArray()); + RestApiSetup.installServletFilters(context, this.filters); context.setContextPath("/"); diff --git a/rest/rest-server/src/test/java/org/apache/brooklyn/rest/CorsFilterLauncherTest.java b/rest/rest-server/src/test/java/org/apache/brooklyn/rest/CorsFilterLauncherTest.java new file mode 100644 index 00000000000..2d73e29b3fc --- /dev/null +++ b/rest/rest-server/src/test/java/org/apache/brooklyn/rest/CorsFilterLauncherTest.java @@ -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.brooklyn.rest; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import org.apache.brooklyn.api.mgmt.ManagementContext; +import org.apache.brooklyn.core.BrooklynFeatureEnablement; +import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests; +import org.apache.brooklyn.rest.filter.CorsImplSupplierFilter; +import org.apache.brooklyn.util.http.HttpTool; +import org.apache.brooklyn.util.http.HttpToolResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.client.methods.RequestBuilder; +import org.apache.http.impl.client.HttpClients; +import org.testng.annotations.Test; + +import javax.ws.rs.core.HttpHeaders; +import java.io.IOException; +import java.net.URI; +import java.util.List; + +import static org.apache.brooklyn.rest.CsrfTokenFilterLauncherTest.assertOkayResponse; +import static org.apache.cxf.rs.security.cors.CorsHeaderConstants.*; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; + +/** + * If brooklyn.experimental.feature.corsCxfFeature.allowedOrigins is not supplied then allowedOrigins will be all domains. + */ +public class CorsFilterLauncherTest extends BrooklynRestApiLauncherTestFixture { + @Test + public void testEnabledCorsSendsBasicAllowResponse() throws IOException { + BrooklynFeatureEnablement.enable(BrooklynFeatureEnablement.FEATURE_CORS_CXF_PROPERTY); + final String shouldAllowOrigin = "http://foo.bar.com"; + BrooklynRestApiLauncher apiLauncher = baseLauncher() + .withoutJsgui(); + ManagementContext mgmt = LocalManagementContextForTests.builder(true) + .useAdditionalProperties(ImmutableMap.of( + BrooklynFeatureEnablement.FEATURE_CORS_CXF_PROPERTY, true, + CorsImplSupplierFilter.ALLOWED_ORIGINS.getName(), ImmutableList.of(shouldAllowOrigin)) + ).build(); + apiLauncher.managementContext(mgmt); + useServerForTest(apiLauncher.start()); + + // preflight request + HttpClient client = client(); + HttpToolResponse response = HttpTool.execAndConsume(client, httpOptionsRequest("server/status", "GET", shouldAllowOrigin)); + assertAcAllowOrigin(response, shouldAllowOrigin, "GET"); + assertOkayResponse(response, ""); + + HttpUriRequest httpRequest = RequestBuilder.get(getBaseUriRest() + "server/status") + .addHeader("Origin", shouldAllowOrigin) + .addHeader(HEADER_AC_REQUEST_METHOD, "GET") + .build(); + response = HttpTool.execAndConsume(client, httpRequest); + assertAcAllowOrigin(response, shouldAllowOrigin, "GET", false); + assertOkayResponse(response, "MASTER"); + + // preflight request + response = HttpTool.execAndConsume(client, httpOptionsRequest("script/groovy", "POST", shouldAllowOrigin)); + assertAcAllowOrigin(response, shouldAllowOrigin, "POST"); + assertOkayResponse(response, ""); + + response = HttpTool.httpPost( + client, URI.create(getBaseUriRest() + "script/groovy"), + ImmutableMap.of( + "Origin", shouldAllowOrigin, + HttpHeaders.CONTENT_TYPE, "application/text"), + "return 0;".getBytes()); + assertAcAllowOrigin(response, shouldAllowOrigin, "POST", false); + assertOkayResponse(response, "{\"result\":\"0\"}"); + + final String thirdPartyOrigin = "http://foo.bar1.com"; + + // preflight request + response = HttpTool.execAndConsume(client, httpOptionsRequest("server/status", "GET", thirdPartyOrigin)); + assertAcNotAllowOrigin(response); + assertOkayResponse(response, ""); + + // preflight request + response = HttpTool.execAndConsume(client, httpOptionsRequest("script/groovy", "POST", thirdPartyOrigin)); + assertAcNotAllowOrigin(response); + assertOkayResponse(response, ""); + + response = HttpTool.httpPost( + client, URI.create(getBaseUriRest() + "script/groovy"), + ImmutableMap.of( + "Origin", thirdPartyOrigin, + HttpHeaders.CONTENT_TYPE, "application/text"), + "return 0;".getBytes()); + assertAcNotAllowOrigin(response); + assertOkayResponse(response, "{\"result\":\"0\"}"); + } + + @Test + public void testCorsIsDisabled() throws IOException { + BrooklynFeatureEnablement.disable(BrooklynFeatureEnablement.FEATURE_CORS_CXF_PROPERTY); + final String shouldAllowOrigin = "http://foo.bar.com"; + BrooklynRestApiLauncher apiLauncher = baseLauncher() + .withoutJsgui(); + ManagementContext mgmt = LocalManagementContextForTests.builder(true) + .useAdditionalProperties(ImmutableMap.of( + BrooklynFeatureEnablement.FEATURE_CORS_CXF_PROPERTY, false) + ).build(); + apiLauncher.managementContext(mgmt); + useServerForTest(apiLauncher.start()); + + HttpClient client = client(); + HttpToolResponse response = HttpTool.execAndConsume(client, httpOptionsRequest("server/status", "GET", shouldAllowOrigin)); + assertNull(response.getHeaderLists().get(HEADER_AC_ALLOW_ORIGIN), "Access Control Header should not be available."); + assertOkayResponse(response, ""); + + response = HttpTool.execAndConsume(client, httpOptionsRequest("script/groovy", shouldAllowOrigin, "POST")); + assertNull(response.getHeaderLists().get(HEADER_AC_ALLOW_ORIGIN), "Access Control Header should not be available."); + assertOkayResponse(response, ""); + } + + protected HttpClient client() { + return HttpClients.createMinimal(); + } + + public void assertAcAllowOrigin(HttpToolResponse response, String shouldAllowOrigin, String method) { + assertAcAllowOrigin(response, shouldAllowOrigin, method, true); + } + + public void assertAcAllowOrigin(HttpToolResponse response, String shouldAllowOrigin, String method, boolean preflightRequest) { + List accessControlAllowOrigin = response.getHeaderLists().get(HEADER_AC_ALLOW_ORIGIN); + assertEquals(accessControlAllowOrigin.size(), 1); + assertEquals(accessControlAllowOrigin.get(0), shouldAllowOrigin, "Should allow " + method + " requests made from " + shouldAllowOrigin); + + if (preflightRequest) { + List accessControlAllowHeaders = response.getHeaderLists().get(HEADER_AC_ALLOW_HEADERS); + assertEquals(accessControlAllowHeaders.size(), 1); + assertEquals(accessControlAllowHeaders.get(0), "x-csrf-token", "Should have asked and allowed x-csrf-token header from " + shouldAllowOrigin); + } + } + + public void assertAcNotAllowOrigin(HttpToolResponse response) { + List accessControlAllowOrigin = response.getHeaderLists().get(HEADER_AC_ALLOW_ORIGIN); + assertNull(accessControlAllowOrigin, "Access Control Header should not be available."); + } + + private HttpUriRequest httpOptionsRequest(String apiCall, String acRequestMethod, String origin) { + return RequestBuilder.options(getBaseUriRest() + apiCall) + .addHeader("Origin", origin) + .addHeader(HEADER_AC_REQUEST_HEADERS, "x-csrf-token") + .addHeader(HEADER_AC_REQUEST_METHOD, acRequestMethod) + .build(); + } +} diff --git a/rest/rest-server/src/test/java/org/apache/brooklyn/rest/CsrfTokenFilterLauncherTest.java b/rest/rest-server/src/test/java/org/apache/brooklyn/rest/CsrfTokenFilterLauncherTest.java index 31f9497173f..d4428758a08 100644 --- a/rest/rest-server/src/test/java/org/apache/brooklyn/rest/CsrfTokenFilterLauncherTest.java +++ b/rest/rest-server/src/test/java/org/apache/brooklyn/rest/CsrfTokenFilterLauncherTest.java @@ -117,9 +117,8 @@ protected HttpClient client() { .build(); } - protected void assertOkayResponse(HttpToolResponse response, String expecting) { + public static void assertOkayResponse(HttpToolResponse response, String expecting) { assertEquals(response.getResponseCode(), HttpStatus.SC_OK); assertEquals(response.getContentAsString(), expecting); } - }