davsclaus opened a new pull request, #30595: URL: https://github.com/apache/camel/pull/30595
## What was wrong [CAMEL-27373](https://issues.apache.org/jira/browse/CAMEL-27373): WS-Security in camel-cxf could only be set up through the free-form CXF `properties.*` map, and it needed Java code in most cases: - Client: `properties.security.username/password` only work when the WSDL has a WS-SecurityPolicy. Without one you need a `WSS4JOutInterceptor` bean. - Server: you always need a Java `CallbackHandler`. - YAML camelCases the dashed CXF keys (`callback-handler`), see CAMEL-27362. - Secrets in `properties.*` carry no `security = "secret"`, so `camel.security.secretPolicy` never sees them. Found while adding SOAP Kamelets to the Kamelet catalog. ## The fix (design summary) The new endpoint options all have `label = "security"`. The producer and the consumer both use them: | Option | Producer (client) | Consumer (server) | |---|---|---| | `wsSecurityUsername` (`security = "secret"`) | user name sent in the UsernameToken | the one user that is accepted | | `wsSecurityPassword` (`security = "secret"`) | password sent | password of that user | | `wsSecurityPasswordType` `Text`/`Digest`, default `Text` | PasswordText or PasswordDigest | the type that is required | | `wsSecurityCallbackHandler` (`#bean:`) | supplies the password instead of `wsSecurityPassword` | looks up the password of any user (vault, JAAS, ...) | | `wsSecurityCredentials` (`Map` bean) | not used | several users without Java code | The options have a `wsSecurity` prefix because `username`/`password` already mean HTTP Basic on this endpoint. The two credential options use `security = "secret"`, like the existing `username`/`password`. How it works: - When any `wsSecurity*` option is set, `getChainedCxfConfigurer()` adds a new `WsSecurityUsernameTokenConfigurer` to the chain, next to `SslCxfConfigurer`. It builds one `CallbackHandler`: the bean if one is given, otherwise the map, otherwise the single user and password. An unknown user gets no password, so the token is rejected. - It always sets the CXF properties `security.username`, `security.password` (client only) and `security.callback-handler` on the CXF endpoint. A WSDL whose WS-SecurityPolicy asks for a UsernameToken uses these. - When the port has no WS-SecurityPolicy, it also adds a `WSS4JOutInterceptor` (client) or `WSS4JInInterceptor` (server) with `action=UsernameToken`. It never adds both, since the security header would then be processed twice. The policy is detected automatically, with no extra option. The check runs in `configureClient`/`configureServer`, after the WSDL has been read. It asks the policy providers of CXF's `PolicyEngine` for the effective policy of the service, the endpoint, and each binding operation and message, and looks for an assertion in a WS-SecurityPolicy 1.1/1.2/1.3 namespace. - **`CxfConsumer` now calls `getChainedCxfConfigurer().configureServer(...)`** instead of `getCxfConfigurer()`, so the new configurer runs on the server side. The other configurers in the chain (SSL, hostname verifier) do nothing in `configureServer`, so this changes nothing for existing routes. - The configurer is the only class that imports wss4j / ws-security / ws-policy, and it is loaded only when an option is set. `cxf-rt-ws-security` stays an optional dependency. When it is missing, endpoint init fails with a message that names `org.apache.cxf:cxf-rt-ws-security`. - `dataFormat=RAW` with a `wsSecurity*` option fails at endpoint init. CXF does not process the SOAP envelope in RAW mode, so the consumer would otherwise accept requests without checking them. - The authenticated user reaches the route: the consumer sets `CamelAuthentication` (a `Subject` with the UsernameToken principal) on both the policy path and the interceptor path. The tests check this. Scope, as agreed: UsernameToken only (no Timestamp, signature or encryption). `wsSecurityCredentials` and `wsSecurityCallbackHandler` are both kept. ### `#bean:` in `properties.*`: already works on main, no code change While implementing this, I found that `#bean:` values in `properties.*` are **already resolved on main**. `DefaultComponent.setProperties(endpoint, parameters)` binds the `properties.` multi-value prefix through the generated configurer, and that resolves `#bean:name`. A bare `#name` stays literal text. The kamelets prototype used `#utPasswordCallback` (bare), and that is why it saw the text passed through. The `#bean:` resolution I first added in `CxfComponent` was therefore a no-op and is not in this PR. Because no behaviour changed, there is **no upgrade guide entry**. The PR adds tests that pin the current behaviour: - `properties.security.callback-handler=#bean:x` is the bean instance and is used by the policy interceptors. - `properties.security.password=#secret` stays literal. - A missing `#bean:` fails endpoint creation. The docs now mention that `#bean:` works there and a bare `#name` does not. I did not touch the side note from the design (`CxfEndpoint.setProperties` also runs `PropertyBindingSupport.bindProperties` on the CXF map). ## Docs - A new "WS-Security UsernameToken" section in `cxf-component.adoc`: an options table, policy vs no policy, `CamelAuthentication`, RAW not supported, a warning to use HTTPS (Digest uses SHA-1 and is no replacement for TLS), and YAML examples for a consumer and a producer. The catalog copy is regenerated. - The component JSON, the catalog JSON and the endpoint DSL are regenerated. The component DSL has no change (no component options). ## Tests New tests in `camel-cxf-soap`, using JUnit assertions like the rest of the module. The test WSDLs are `wssec/orders.wsdl` (no policy) and `wssec/orders-ut.wsdl` (a UsernameToken WS-SecurityPolicy, from the kamelets prototype). The tests run over Undertow. `CxfWsSecurityUsernameTokenTest` (17 tests): - No policy, Text: ok (the route sees user `alice` in `CamelAuthentication`), a wrong password is rejected, a request without a token is rejected, a client `wsSecurityCallbackHandler` without a password works. - No policy, Digest with a `wsSecurityCredentials` map bean: two users ok, a wrong password, an unknown user, and a Text token to a server that requires Digest are all rejected. - With policy: ok, and a wrong password is rejected. No plain `WSS4JOutInterceptor`/`WSS4JInInterceptor` is added when the WSDL has a policy, and both are added when it has none. - `properties.security.callback-handler=#bean:serverCallback` on a policy server, with a client that uses the old `properties.security.username/password`: ok, and a wrong password is rejected. - `#name` is not resolved, a missing `#bean:` fails, a client without `wsSecurityUsername` fails, `dataFormat=RAW` fails, and a server with a user but no password fails to start. `CxfWsSecurityYamlTest` (2 tests) uses a YAML route with only options (a `HashMap` bean, Digest): the call succeeds, and a wrong password gives a `SoapFault`. This adds `camel-yaml-dsl` as a test dependency. Results: - `mvn test -Dtest='CxfWsSecurity*Test'` in camel-cxf-soap: 19 tests, 0 failures. - With the `CxfConsumer` change reverted, 7 of these fail, because the server-side configurer does not run. - Full camel-cxf-soap suite: 230 tests, 0 failures, 4 skipped (existing `@Disabled` tests). - camel-cxf-spring-soap suite, including `WSSecurityRouteTest`, against this camel-cxf-soap: 161 tests. `WSSecurityRouteTest` passes (4 tests). One failure, `CxfEndpointBeansRouterTest.testCxfBusConfiguration`, which expects a fault from a call to a random port and got none during the parallel run. It passes when run alone (4/4), and it does not use the new options. ## Possible follow-ups (not in this PR) - JBang: the `camel-cxf-soap` auto-configure in camel-kamelet-main could download `cxf-rt-ws-security` when a `wsSecurity*` option is used, in the same way it downloads the Undertow transport. - camel-kamelets: `soap-source`/`soap-action` can add `wsSecurityUsername`/`wsSecurityPassword` (`format: password`). _Claude Code on behalf of davsclaus_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
