This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27395-cxf-ws-security-download in repository https://gitbox.apache.org/repos/asf/camel.git
commit 5b6896a2feac6f6d8fc0caa3f11815567a69b611 Author: Claus Ibsen <[email protected]> AuthorDate: Sun Nov 22 20:17:42 2026 +0100 CAMEL-27395: camel-jbang - download cxf-rt-ws-security when a cxf endpoint uses the wsSecurity options A cxf endpoint with a WS-Security UsernameToken needs org.apache.cxf:cxf-rt-ws-security of the CXF version of Camel, which camel-cxf-soap does not bring. camel run now downloads it (as a dependency of the integration, so camel export adds it) when a cxf endpoint of a route, a consumer or a producer, sets wsSecurityUsername, wsSecurityPassword, wsSecurityCallbackHandler or wsSecurityCredentials to a value that is not empty. The placeholders are resolved with the parameters of the route template first, so a Kamelet with optional wsSecurity parameters only gets it when they are set. On Quarkus, export uses quarkus-cxf-rt-ws-security. The cxf docs say that the Camel CLI adds the dependency, and that a refused UsernameToken gives a SOAP fault with the code SecurityError (SOAP 1.1), which the WS-Security test now checks. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../apache/camel/catalog/docs/cxf-component.adoc | 9 +- .../src/main/docs/cxf-component.adoc | 9 +- .../cxf/jaxws/CxfWsSecurityUsernameTokenTest.java | 2 + .../main/resources/quarkus-dependencies.properties | 1 + .../dsl/jbang/run/export/ProjectExporterTest.java | 36 ++++++ .../main/download/DependencyDownloaderCxf.java | 94 +++++++++++++++- .../main/download/DependencyDownloaderCxfTest.java | 123 +++++++++++++++++++++ 7 files changed, 271 insertions(+), 3 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/cxf-component.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/cxf-component.adoc index fe18d35a0c96..e1d07b61d30e 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/cxf-component.adoc +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/cxf-component.adoc @@ -157,7 +157,14 @@ The service is at `\http://localhost:8080/services/orders`. An address with a sc The `wsSecurity` options set up a WS-Security UsernameToken without Java code. The producer sends a UsernameToken in the SOAP header of its requests, and the consumer rejects a request without a valid UsernameToken with a SOAP fault. -The options need the `org.apache.cxf:cxf-rt-ws-security` dependency, which `camel-cxf-soap` does not bring by itself. +With SOAP 1.1, the code of that fault is `SecurityError`, whatever the reason: no token, an unknown user, a wrong +password, or another password type than `wsSecurityPasswordType`. The fault does not tell the client which check +failed. + +The options need the `org.apache.cxf:cxf-rt-ws-security` dependency of the CXF version that Camel uses, which +`camel-cxf-soap` does not bring by itself. The Camel CLI adds it: `camel run` downloads it when a cxf endpoint sets +`wsSecurityUsername`, `wsSecurityPassword`, `wsSecurityCallbackHandler` or `wsSecurityCredentials`, and `camel export` +adds it to the project (on Quarkus as `io.quarkiverse.cxf:quarkus-cxf-rt-ws-security`). NOTE: The `username` and `password` options are for HTTP Basic authentication, not for WS-Security. diff --git a/components/camel-cxf/camel-cxf-soap/src/main/docs/cxf-component.adoc b/components/camel-cxf/camel-cxf-soap/src/main/docs/cxf-component.adoc index fe18d35a0c96..e1d07b61d30e 100644 --- a/components/camel-cxf/camel-cxf-soap/src/main/docs/cxf-component.adoc +++ b/components/camel-cxf/camel-cxf-soap/src/main/docs/cxf-component.adoc @@ -157,7 +157,14 @@ The service is at `\http://localhost:8080/services/orders`. An address with a sc The `wsSecurity` options set up a WS-Security UsernameToken without Java code. The producer sends a UsernameToken in the SOAP header of its requests, and the consumer rejects a request without a valid UsernameToken with a SOAP fault. -The options need the `org.apache.cxf:cxf-rt-ws-security` dependency, which `camel-cxf-soap` does not bring by itself. +With SOAP 1.1, the code of that fault is `SecurityError`, whatever the reason: no token, an unknown user, a wrong +password, or another password type than `wsSecurityPasswordType`. The fault does not tell the client which check +failed. + +The options need the `org.apache.cxf:cxf-rt-ws-security` dependency of the CXF version that Camel uses, which +`camel-cxf-soap` does not bring by itself. The Camel CLI adds it: `camel run` downloads it when a cxf endpoint sets +`wsSecurityUsername`, `wsSecurityPassword`, `wsSecurityCallbackHandler` or `wsSecurityCredentials`, and `camel export` +adds it to the project (on Quarkus as `io.quarkiverse.cxf:quarkus-cxf-rt-ws-security`). NOTE: The `username` and `password` options are for HTTP Basic authentication, not for WS-Security. diff --git a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfWsSecurityUsernameTokenTest.java b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfWsSecurityUsernameTokenTest.java index d049395d005c..9aae0e8be4e9 100644 --- a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfWsSecurityUsernameTokenTest.java +++ b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfWsSecurityUsernameTokenTest.java @@ -140,6 +140,8 @@ class CxfWsSecurityUsernameTokenTest extends CamelTestSupport { Exchange exchange = template.request(uri, e -> e.getIn().setBody(REQUEST)); SoapFault fault = assertInstanceOf(SoapFault.class, exchange.getException()); assertTrue(fault.getMessage().contains("security"), fault.getMessage()); + // the code that the docs give for a refused UsernameToken (SOAP 1.1), which does not tell why + assertEquals("SecurityError", fault.getFaultCode().getLocalPart(), fault.getFaultCode().toString()); } @Test diff --git a/dsl/camel-jbang/camel-jbang-run/src/main/resources/quarkus-dependencies.properties b/dsl/camel-jbang/camel-jbang-run/src/main/resources/quarkus-dependencies.properties index 8f42ee0adb3c..8df20f1faca6 100644 --- a/dsl/camel-jbang/camel-jbang-run/src/main/resources/quarkus-dependencies.properties +++ b/dsl/camel-jbang/camel-jbang-run/src/main/resources/quarkus-dependencies.properties @@ -17,6 +17,7 @@ org.messaginghub\:pooled-jms = io.quarkiverse.messaginghub:quarkus-pooled-jms org.hibernate.orm\:hibernate-core = io.quarkus:quarkus-hibernate-orm +org.apache.cxf\:cxf-rt-ws-security = io.quarkiverse.cxf:quarkus-cxf-rt-ws-security camel\:resilience4j = org.apache.camel.quarkus:camel-quarkus-microprofile-fault-tolerance # the Jackson 3 line of the catalog: Camel Quarkus has the Jackson extensions only, which move to camel-jackson3 themselves camel\:jackson3 = org.apache.camel.quarkus:camel-quarkus-jackson diff --git a/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/export/ProjectExporterTest.java b/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/export/ProjectExporterTest.java index 51d1d3b8bfe6..71f3e4a4dfc6 100644 --- a/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/export/ProjectExporterTest.java +++ b/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/export/ProjectExporterTest.java @@ -554,6 +554,42 @@ class ProjectExporterTest { .doesNotContain("camel.server.enabled"); } + @ParameterizedTest + @EnumSource(value = RuntimeType.class, names = { "main", "springBoot", "quarkus" }) + void cxfWithAUsernameTokenHasWsSecurity(RuntimeType runtime) throws Exception { + // CAMEL-27395: camel run adds cxf-rt-ws-security of the CXF version for a cxf endpoint with a wsSecurity option + Path route = writeRoute("soap"); + DependencyResolver cxf = r -> { + Path settings = r.workDir.resolve(ExportHelper.RUN_SETTINGS_FILE); + Files.createDirectories(settings.getParent()); + Files.writeString(settings, """ + camel.main.routesIncludePattern=file:%s + dependency=camel:cxf-soap + dependency=mvn:org.apache.cxf:cxf-rt-ws-security:4.2.4 + """.formatted(route)); + return 0; + }; + Path exportDir = dir.resolve("export"); + ExportRequest request = request(route, exportDir); + request.runtime = runtime; + + StringPrinter printer = new StringPrinter(); + int exit = new ProjectExporter(new ExportContext().withPrinter(printer).withDependencyResolver(cxf)) + .export(request); + + assertThat(exit).as(printer.getOutput()).isZero(); + String pom = Files.readString(exportDir.resolve("pom.xml")); + if (runtime == RuntimeType.quarkus) { + // the CXF extension of Quarkus, whose version is in the Quarkus Platform BOM + assertThat(pom).contains("<groupId>io.quarkiverse.cxf</groupId>", + "<artifactId>quarkus-cxf-rt-ws-security</artifactId>") + .doesNotContain("<artifactId>cxf-rt-ws-security</artifactId>", "4.2.4"); + } else { + assertThat(pom).contains("<groupId>org.apache.cxf</groupId>", "<artifactId>cxf-rt-ws-security</artifactId>", + "<version>4.2.4</version>"); + } + } + @ParameterizedTest @EnumSource(value = RuntimeType.class, names = { "main", "springBoot", "quarkus" }) void localJarsAreInTheLibDirectoryAsThePomRefersToThem(RuntimeType runtime) throws Exception { diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderCxf.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderCxf.java index 7af9f1192ef0..5d10453f298a 100644 --- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderCxf.java +++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderCxf.java @@ -16,16 +16,25 @@ */ package org.apache.camel.main.download; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; import org.apache.camel.RuntimeCamelException; +import org.apache.camel.model.EndpointRequiredDefinition; import org.apache.camel.model.ModelCamelContext; import org.apache.camel.model.ModelLifecycleStrategy; +import org.apache.camel.model.ProcessorDefinitionHelper; import org.apache.camel.model.RouteDefinition; import org.apache.camel.model.RouteTemplateDefinition; +import org.apache.camel.spi.PropertiesComponent; import org.apache.camel.support.service.ServiceSupport; +import org.apache.camel.util.URISupport; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,6 +46,10 @@ import org.slf4j.LoggerFactory; * camel-cxf-platform-http is downloaded, as a dependency of the integration, so camel export includes it, and its * transport is bound with a starter of the platform-http server, which starts when the first cxf consumer starts. An * integration with only cxf producers (SOAP clients) needs neither. + * <p> + * When the first cxf endpoint (a consumer or a producer) with a WS-Security UsernameToken is added (one of the + * wsSecurity options that turn it on is set), cxf-rt-ws-security of the CXF version of Camel is downloaded, as a + * dependency of the integration as well. The jar brings WSS4J and more, which most SOAP routes do not need. */ public final class DependencyDownloaderCxf extends ServiceSupport implements CamelContextAware, ModelLifecycleStrategy { @@ -49,6 +62,14 @@ public final class DependencyDownloaderCxf extends ServiceSupport private CamelContext camelContext; private final boolean silent; private final AtomicBoolean done = new AtomicBoolean(); + private final AtomicBoolean wsSecurityDone = new AtomicBoolean(); + + /** + * The options of a cxf endpoint that turn on a WS-Security UsernameToken (see + * CxfEndpoint#isWsSecurityUsernameTokenEnabled), which needs cxf-rt-ws-security. + */ + private static final Set<String> WS_SECURITY_OPTIONS = Set.of( + "wsSecurityUsername", "wsSecurityPassword", "wsSecurityCallbackHandler", "wsSecurityCredentials"); public DependencyDownloaderCxf(CamelContext camelContext, boolean silent) { this.camelContext = camelContext; @@ -78,13 +99,25 @@ public final class DependencyDownloaderCxf extends ServiceSupport @Override public void onAddRouteDefinition(RouteDefinition routeDefinition) { - if (!done.get() && routeDefinition.getInput() != null + if (routeDefinition.getInput() == null) { + return; + } + if (!done.get() && isCxfConsumer(camelContext, routeDefinition.getInput().getEndpointUri()) && done.compareAndSet(false, true)) { LOG.debug("Route: {} has a cxf consumer, which is served on the platform-http server", routeDefinition.getRouteId()); servePlatformHttp(); } + if (!wsSecurityDone.get() + && usesWsSecurity(camelContext, routeDefinition) + && wsSecurityDone.compareAndSet(false, true)) { + LOG.debug("Route: {} has a cxf endpoint with a WS-Security UsernameToken", routeDefinition.getRouteId()); + DependencyDownloader downloader = camelContext.hasService(DependencyDownloader.class); + if (downloader != null) { + downloader.downloadDependency("org.apache.cxf", "cxf-rt-ws-security", "${cxf-version}"); + } + } } @Override @@ -117,6 +150,65 @@ public final class DependencyDownloaderCxf extends ServiceSupport return uri != null && uri.startsWith("cxf:"); } + /** + * Whether a cxf endpoint of the route (its input or an output) sets one of the wsSecurity options that turn on a + * WS-Security UsernameToken, to a value that is not empty. The property placeholders are resolved first, with the + * parameters of the route template (such as a Kamelet), so an optional parameter that is not set, as in + * wsSecurityUsername=RAW({{?wsSecurityUsername}}), does not count. + */ + static boolean usesWsSecurity(CamelContext camelContext, RouteDefinition route) { + List<String> uris = new ArrayList<>(); + uris.add(route.getInput().getEndpointUri()); + for (EndpointRequiredDefinition erd : ProcessorDefinitionHelper.filterTypeInOutputs(route.getOutputs(), + EndpointRequiredDefinition.class)) { + uris.add(erd.getEndpointUri()); + } + PropertiesComponent pc = camelContext.getPropertiesComponent(); + Properties previous = pc.getLocalProperties(); + Map<String, Object> parameters = route.getTemplateParameters(); + if (parameters != null) { + Properties local = new Properties(); + if (previous != null) { + local.putAll(previous); + } + parameters.forEach((k, v) -> { + if (k != null && v != null) { + local.put(k, v); + } + }); + pc.setLocalProperties(local); + } + try { + for (String uri : uris) { + if (uri != null && uri.contains("wsSecurity") && hasWsSecurityOption(camelContext, uri)) { + return true; + } + } + return false; + } finally { + if (parameters != null) { + pc.setLocalProperties(previous); + } + } + } + + private static boolean hasWsSecurityOption(CamelContext camelContext, String uri) { + try { + uri = camelContext.resolvePropertyPlaceholders(uri); + if (uri == null || !uri.startsWith("cxf:")) { + return false; + } + Map<String, Object> query = URISupport.parseQuery(URISupport.extractQuery(uri), true, true); + URISupport.resolveRawParameterValues(query); + return query.entrySet().stream() + .anyMatch(e -> WS_SECURITY_OPTIONS.contains(e.getKey()) + && e.getValue() != null && !e.getValue().toString().isBlank()); + } catch (Exception e) { + // a placeholder that cannot be resolved, or not a uri + return false; + } + } + private void servePlatformHttp() { DependencyDownloader downloader = camelContext.hasService(DependencyDownloader.class); if (downloader != null) { diff --git a/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderCxfTest.java b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderCxfTest.java index b1dcc1c891bc..02365a4d1c07 100644 --- a/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderCxfTest.java +++ b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderCxfTest.java @@ -35,6 +35,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; class DependencyDownloaderCxfTest { private static final String MODULE = "org.apache.camel:camel-cxf-platform-http"; + private static final String WS_SECURITY = "org.apache.cxf:cxf-rt-ws-security"; @Test void aRouteWithACxfConsumerDownloadsTheModuleAndSetsTheStarterOfTheHttpServer() throws Exception { @@ -117,6 +118,126 @@ class DependencyDownloaderCxfTest { } } + @Test + void aCxfProducerWithAUsernameTokenDownloadsWsSecurity() throws Exception { + RecordingDownloader downloader = new RecordingDownloader(); + FakeTransport transport = new FakeTransport(); + try (CamelContext context = context(downloader, transport)) { + context.addService(new DependencyDownloaderCxf(context, true)); + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("direct:start") + .to("cxf:http://localhost:9000/orders?serviceClass=org.example.Orders" + + "&wsSecurityUsername=alice&wsSecurityPassword=RAW(se&cret)"); + from("direct:other") + .to("cxf:http://localhost:9000/invoices?serviceClass=org.example.Invoices" + + "&wsSecurityUsername=bob&wsSecurityPassword=secret"); + } + }); + + // of the CXF version of Camel, once, and no platform-http for producers only + assertEquals(List.of(WS_SECURITY + ":${cxf-version}"), downloader.downloadedWithVersion); + assertNull(transport.starter); + } + } + + @Test + void aCxfConsumerWithAUsernameTokenDownloadsWsSecurityToo() throws Exception { + RecordingDownloader downloader = new RecordingDownloader(); + FakeTransport transport = new FakeTransport(); + try (CamelContext context = context(downloader, transport)) { + context.addService(new DependencyDownloaderCxf(context, true)); + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("cxf:/orders?serviceClass=org.example.Orders&wsSecurityCallbackHandler=#myHandler") + .to("log:orders"); + } + }); + + assertEquals(List.of(MODULE, WS_SECURITY), downloader.downloaded); + } + } + + @Test + void theWsSecurityOptionsThatDoNotTurnOnAUsernameTokenDownloadNothing() throws Exception { + RecordingDownloader downloader = new RecordingDownloader(); + FakeTransport transport = new FakeTransport(); + try (CamelContext context = context(downloader, transport)) { + context.addService(new DependencyDownloaderCxf(context, true)); + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("direct:start") + .to("cxf:http://localhost:9000/orders?serviceClass=org.example.Orders" + + "&wsSecurityUsername=&wsSecurityPasswordType=Digest") + // not a cxf endpoint + .to("log:foo?wsSecurityUsername=alice"); + } + }); + + assertTrue(downloader.downloaded.isEmpty(), downloader.downloaded.toString()); + } + } + + @Test + void aKameletWithAnOptionalUsernameTokenDownloadsWsSecurityOnlyWhenItIsSet() throws Exception { + // as the soap-action Kamelet, whose uri always has the wsSecurity options, with optional parameters + RecordingDownloader downloader = new RecordingDownloader(); + FakeTransport transport = new FakeTransport(); + try (CamelContext context = context(downloader, transport)) { + context.addService(new DependencyDownloaderCxf(context, true)); + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + routeTemplate("soap-action").templateParameter("address") + .templateOptionalParameter("wsSecurityUsername") + .templateOptionalParameter("wsSecurityPassword") + .from("direct:{{address}}") + .to("cxf://{{address}}?serviceClass=org.example.Orders" + + "&wsSecurityUsername=RAW({{?wsSecurityUsername}})" + + "&wsSecurityPassword=RAW({{?wsSecurityPassword}})"); + } + }); + + context.addRouteFromTemplate("plain", "soap-action", Map.of("address", "http://localhost:9000/orders")); + assertTrue(downloader.downloaded.isEmpty(), downloader.downloaded.toString()); + + context.addRouteFromTemplate("secured", "soap-action", + Map.of("address", "http://localhost:9000/invoices", "wsSecurityUsername", "alice", + "wsSecurityPassword", "secret")); + assertEquals(List.of(WS_SECURITY), downloader.downloaded); + // the local properties of the template are not left behind + assertNull(context.getPropertiesComponent().getLocalProperties()); + } + } + + @Test + void aUsernameTokenFromAPropertyPlaceholder() throws Exception { + RecordingDownloader downloader = new RecordingDownloader(); + FakeTransport transport = new FakeTransport(); + try (CamelContext context = context(downloader, transport)) { + Properties props = new Properties(); + props.setProperty("soap.user", "alice"); + context.getPropertiesComponent().setInitialProperties(props); + context.addService(new DependencyDownloaderCxf(context, true)); + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("direct:none") + .to("cxf:http://localhost:9000/orders?serviceClass=org.example.Orders" + + "&wsSecurityUsername={{?soap.none}}"); + from("direct:start") + .to("cxf:http://localhost:9000/orders?serviceClass=org.example.Orders" + + "&wsSecurityUsername={{soap.user}}"); + } + }); + + assertEquals(List.of(WS_SECURITY), downloader.downloaded); + } + } + @Test void isCxfConsumer() throws Exception { try (CamelContext context = new DefaultCamelContext()) { @@ -159,6 +280,7 @@ class DependencyDownloaderCxfTest { private static class RecordingDownloader extends MavenDependencyDownloader { private final List<String> downloaded = new CopyOnWriteArrayList<>(); + private final List<String> downloadedWithVersion = new CopyOnWriteArrayList<>(); @Override protected void doBuild() { @@ -183,6 +305,7 @@ class DependencyDownloaderCxfTest { @Override public void downloadDependency(String groupId, String artifactId, String version) { downloaded.add(groupId + ":" + artifactId); + downloadedWithVersion.add(groupId + ":" + artifactId + ":" + version); } } }
