Skip to content

Commit

Permalink
[resolves wildfly-extras#2605] Test CXF RS endpoints with Elytron
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Sep 26, 2018
1 parent 48fe450 commit 1083c85
Show file tree
Hide file tree
Showing 11 changed files with 398 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,23 @@ public class SecurityUtils {
+ "</web-app>"
;

public static void addSpringXml(WebArchive archive) {
public static void addSpringXmlWs(WebArchive archive) {
addSpringXml(archive, "cxfws-camel-context.xml");
}
public static void addSpringXmlRs(WebArchive archive) {
addSpringXml(archive, "cxfrs-camel-context.xml");
}
public static void addSpringXml(WebArchive archive, String file) {
final StringBuilder sb = new StringBuilder();
try {
FileUtils.copy(
SecurityUtils.class.getClassLoader().getResource("cxf/secure/spring/cxfws-camel-context.xml"), sb);
SecurityUtils.class.getClassLoader().getResource("cxf/secure/spring/"+ file), sb);
} catch (IOException e) {
throw new RuntimeException(e);
}
final String xml = sb.toString().replace("${SPRING_CONSUMER_ENDPOINT_ADDRESS}",
SPRING_CONSUMER_ENDPOINT_ADDRESS);
archive.addAsWebInfResource(new StringAsset(xml), "cxfws-camel-context.xml");
archive.addAsWebInfResource(new StringAsset(xml), file);
}

private static void copy(String fileName, Path targetDirectory) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* #%L
* Wildfly Camel :: Testsuite
* %%
* Copyright (C) 2013 - 2014 RedHat
* %%
* Licensed 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.
* #L%
*/
package org.wildfly.camel.test.cxf.rs.secure;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.Map;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.as.arquillian.api.ServerSetup;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.camel.test.common.security.BasicSecurityDomainASetup;
import org.wildfly.camel.test.common.security.SecurityUtils;
import org.wildfly.camel.test.cxf.rs.secure.subA.Application;
import org.wildfly.camel.test.cxf.rs.secure.subA.CxfRsRouteBuilder;
import org.wildfly.extension.camel.CamelAware;

/**
* @author <a href="https://github.com/ppalaga">Peter Palaga</a>
*/
@CamelAware
@RunAsClient
@RunWith(Arquillian.class)
@ServerSetup(BasicSecurityDomainASetup.class)
public class CXFRSBasicSecureProducerIntegrationTest {
public static final String APP_NAME = "CXFRSBasicSecureProducerIntegrationTest";
private static final Map<String, String> PATH_ROLE_MAP = new LinkedHashMap<String, String>() {
private static final long serialVersionUID = 1L;
{
try {
put("//" + new URI(Application.CXF_ENDPOINT_URI).getPath(),
BasicSecurityDomainASetup.APPLICATION_ROLE);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
};
static final Path WILDFLY_HOME = Paths.get(System.getProperty("jbossHome"));

@Deployment
public static Archive<?> deployment() {
final WebArchive archive = ShrinkWrap
.create(WebArchive.class, APP_NAME + ".war")
.addClasses(BasicSecurityDomainASetup.class, CXFRSSecureUtils.class)
.addPackage(CxfRsRouteBuilder.class.getPackage())
.addAsWebInfResource(new StringAsset(""), "beans.xml")
;
SecurityUtils.enhanceArchive(archive, BasicSecurityDomainASetup.SECURITY_DOMAIN,
BasicSecurityDomainASetup.AUTH_METHOD, PATH_ROLE_MAP);
return archive;
}

@Test
public void greetAnonymous() throws Exception {
CXFRSSecureUtils.assertGreet(WILDFLY_HOME, Application.CXF_ENDPOINT_URI, null, null, 401, null);
}

@Test
public void greetBasicBadUser() throws Exception {
CXFRSSecureUtils.assertGreet(WILDFLY_HOME, Application.CXF_ENDPOINT_URI,
BasicSecurityDomainASetup.APPLICATION_USER_SUB, BasicSecurityDomainASetup.APPLICATION_PASSWORD_SUB, 403,
null);
}

@Test
public void greetBasicGoodUser() throws Exception {
CXFRSSecureUtils.assertGreet(WILDFLY_HOME, Application.CXF_ENDPOINT_URI,
BasicSecurityDomainASetup.APPLICATION_USER, BasicSecurityDomainASetup.APPLICATION_PASSWORD, 200,
"Hi Joe");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.wildfly.camel.test.cxf.rs.secure;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.Base64;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Assert;
import org.wildfly.camel.test.common.security.SecurityUtils;

/**
* @author <a href="https://github.com/ppalaga">Peter Palaga</a>
*/
public class CXFRSSecureUtils {

static void assertGreet(Path wildFlyHome, String uri, String user, String password, int responseCode,
String responseBody) throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException,
KeyStoreException, CertificateException, IOException {
try (CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(SecurityUtils.createBasicSocketFactory(wildFlyHome)).build()) {
HttpPost request = new HttpPost(uri);
request.setHeader("Content-Type", "text/plain");

if (user != null) {
String auth = user + ":" + password;
String authHeader = "Basic "
+ Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.ISO_8859_1));
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
}

request.setEntity(new StringEntity("Joe", StandardCharsets.UTF_8));
try (CloseableHttpResponse response = httpclient.execute(request)) {
final int actualCode = response.getStatusLine().getStatusCode();
Assert.assertEquals(responseCode, actualCode);
if (actualCode == 200) {
HttpEntity entity = response.getEntity();
String body = EntityUtils.toString(entity, StandardCharsets.UTF_8);
Assert.assertEquals(responseBody, body);
}
}
}
}

private CXFRSSecureUtils() {}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* #%L
* Wildfly Camel :: Example :: Camel CXF JAX-WS CDI Secure
* %%
* Copyright (C) 2013 - 2017 RedHat
* %%
* Licensed 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.
* #L%
*/
package org.wildfly.camel.test.cxf.rs.secure.subA;

import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import org.apache.camel.CamelContext;
import org.apache.camel.Processor;
import org.apache.camel.cdi.ContextName;
import org.apache.camel.component.cxf.jaxrs.CxfRsComponent;
import org.apache.camel.component.cxf.jaxrs.CxfRsEndpoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named("cxf_cdi_security_app")
public class Application {

public static final String CXF_ENDPOINT_BASE_URI = "https://localhost:8443/rest";
public static final String CXF_ENDPOINT_URI = CXF_ENDPOINT_BASE_URI + "/greet/hi";
private static final Logger log = LoggerFactory.getLogger(Application.class);

@Inject
@ContextName("cxfrs-secure-cdi-camel-context")
CamelContext camelContext;

@Named("cxfConsumerEndpoint")
@Produces
public CxfRsEndpoint createCxfConsumerEndpoint() {
CxfRsComponent cxfConsumerComponent = new CxfRsComponent(this.camelContext);
CxfRsEndpoint cxfConsumerEndpoint = new CxfRsEndpoint(CXF_ENDPOINT_BASE_URI, cxfConsumerComponent);
cxfConsumerEndpoint.setBeanId("cxfConsumerEndpoint");
cxfConsumerEndpoint.addResourceClass(GreetingsService.class);
return cxfConsumerEndpoint;
}

@Named("cxfProducerEndpoint")
@Produces
public CxfRsEndpoint createCxfProducerEndpoint() {
CxfRsComponent cxfProducerComponent = new CxfRsComponent(this.camelContext);
CxfRsEndpoint cxfProducerEndpoint = new CxfRsEndpoint(CXF_ENDPOINT_BASE_URI, cxfProducerComponent);
cxfProducerEndpoint.setBeanId("cxfProducerEndpoint");
cxfProducerEndpoint.addResourceClass(GreetingsService.class);

// Not for use in production
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
cxfProducerEndpoint.setHostnameVerifier(hostnameVerifier);

return cxfProducerEndpoint;
}

@Named("greetingsProcessor")
@Produces
public Processor produceGreetingsProcessor() {
return new GreetingsProcessor();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* #%L
* Wildfly Camel :: Example :: Camel CXF JAX-WS CDI Secure
* %%
* Copyright (C) 2013 - 2017 RedHat
* %%
* Licensed 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.
* #L%
*/
package org.wildfly.camel.test.cxf.rs.secure.subA;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.cdi.ContextName;
import org.apache.camel.component.cxf.CxfEndpoint;
import org.apache.camel.component.cxf.jaxrs.CxfRsEndpoint;

@ApplicationScoped
@ContextName("cxfrs-secure-cdi-camel-context")
public class CxfRsRouteBuilder extends RouteBuilder {

@Inject
@Named("greetingsProcessor")
Processor greetingsProcessor;

@Inject
@Named("cxfConsumerEndpoint")
CxfRsEndpoint cxfConsumerEndpoint;

@Inject
@Named("cxfProducerEndpoint")
CxfRsEndpoint cxfProducerEndpoint;

@Override
public void configure() throws Exception {
from("direct:start")
.to(this.cxfProducerEndpoint);

from(this.cxfConsumerEndpoint)
.process(this.greetingsProcessor);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* #%L
* Wildfly Camel :: Example :: Camel CXF JAX-WS CDI Secure
* %%
* Copyright (C) 2013 - 2017 RedHat
* %%
* Licensed 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.
* #L%
*/
package org.wildfly.camel.test.cxf.rs.secure.subA;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;

public class GreetingsProcessor implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
String name = exchange.getIn().getBody(String.class);
exchange.getOut().setBody("Hi " + name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.wildfly.camel.test.cxf.rs.secure.subA;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/greet")
public interface GreetingsService {

@POST
@Path("/hi")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.TEXT_PLAIN)
public String hi(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static Archive<?> deployment() {
.addAsWebInfResource("cxf/secure/spring/cxfws-camel-context.xml")
.addClasses(BasicSecurityDomainBSetup.class, CXFWSSecureUtils.class, GreetingService.class,
GreetingsProcessor.class);
SecurityUtils.addSpringXml(warB);
SecurityUtils.addSpringXmlWs(warB);
SecurityUtils.enhanceArchive(warB, BasicSecurityDomainBSetup.SECURITY_DOMAIN,
BasicSecurityDomainBSetup.AUTH_METHOD, PATH_ROLE_MAP_B);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static Archive<?> deployment() {
.create(WebArchive.class, CXFWSSpringBasicSecureProducerIntegrationTest.class.getSimpleName() + ".war")
.addClasses(BasicSecurityDomainASetup.class, CXFWSSecureUtils.class, GreetingService.class,
GreetingsProcessor.class);
SecurityUtils.addSpringXml(archive);
SecurityUtils.addSpringXmlWs(archive);
SecurityUtils.enhanceArchive(archive, BasicSecurityDomainASetup.SECURITY_DOMAIN,
BasicSecurityDomainASetup.AUTH_METHOD, PATH_ROLE_MAP);
return archive;
Expand Down
Loading

0 comments on commit 1083c85

Please sign in to comment.