-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CXF CrossOriginResourceSharing filter
- Loading branch information
Showing
10 changed files
with
264 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
.../rest-resources/src/main/java/org/apache/brooklyn/rest/filter/CorsImplSupplierFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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; | ||
|
||
@Provider | ||
public class CorsImplSupplierFilter extends CrossOriginResourceSharingFilter { | ||
public static final ConfigKey<List<String>> ALLOWED_ORIGINS = ConfigKeys.newConfigKey(new TypeToken<List<String>>() {}, 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.<List<String>>elvis(managementContext.getConfig().getConfig(ALLOWED_ORIGINS), Collections.<String>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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
rest/rest-server/src/test/java/org/apache/brooklyn/rest/CorsFilterLauncherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* | ||
* 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.HEADER_AC_ALLOW_ORIGIN; | ||
import static org.apache.cxf.rs.security.cors.CorsHeaderConstants.HEADER_AC_REQUEST_METHOD; | ||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertNull; | ||
|
||
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.<String, Object>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"); | ||
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.<String,String>of( | ||
"Origin", shouldAllowOrigin, | ||
HttpHeaders.CONTENT_TYPE, "application/text"), | ||
"return 0;".getBytes()); | ||
assertAcAllowOrigin(response, shouldAllowOrigin, "POST"); | ||
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.<String,String>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.<String, Object>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) { | ||
List<String> accessControlAllowOrigin = response.getHeaderLists().get(HEADER_AC_ALLOW_ORIGIN); | ||
assertEquals(accessControlAllowOrigin.size(), 1); | ||
assertEquals(accessControlAllowOrigin.get(0), shouldAllowOrigin, "Should allow " + method + " requests made from " + shouldAllowOrigin); | ||
} | ||
|
||
public void assertAcNotAllowOrigin(HttpToolResponse response) { | ||
List<String> 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_METHOD, acRequestMethod) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters