allthingssecurity opened a new pull request, #30248: URL: https://github.com/apache/camel/pull/30248
# Description [CAMEL-27042](https://issues.apache.org/jira/browse/CAMEL-27042) Found while checking the calls of the consumer against Paho 1.2.5 for the Paho DST world of #30138 (CAMEL-26958). Reproduced here with a plain unit test. The consumer subscribes on start (`doStart`), after an automatic reconnect of the client (`resubscribe`, CAMEL-24511), and after its own reconnect when the broker did not keep the session (`reconnect`). It never looked at the result of `MqttClient.subscribe`. In MQTT 5 a broker that refuses a subscription still answers with a SUBACK, with a reason code of 0x80 or higher: 0x80 Unspecified error, 0x83 Implementation specific error, 0x87 Not authorized, 0x8F Topic Filter invalid, 0x97 Quota exceeded, 0x9E, 0xA1, 0xA2 (MQTT 5.0, 3.9.3; 2.4 says a reason code of 0x80 or greater indicates failure). Paho 1.2.5 completes the token of the SUBSCRIBE with those reason codes and no exception (javap: `ClientState.notifyReceivedAck` raises an exception only for a failed PUBREC, `Token.markComplete` copies the reason codes of the SUBACK to the token, and `MqttClient.subscribe` returns the token once it is complete). The Paho MQTT 3 client does throw (`MqttClient.subscribe` throws `MqttException( 128)` for a granted QoS of 0x80), so camel-paho gets this check from Paho, and camel-paho-mqtt5 did not have it. So a route whose subscription is refused, for example by an ACL of the broker, started and stayed Started without ever receiving a message, and after a reconnect the resubscribe of CAMEL-24511 counted a refused subscription as done. Change: the consumer subscribes through one method that throws `MqttException` with the reason code when the SUBACK has a reason code of 0x80 or higher (the message comes from Paho's catalog, for example `Not authorized.`), used in the three places. So a refused subscription is handled as a subscribe that throws: - on start the route does not start, and the own client is disconnected and closed (as before for a failed subscribe); - after an automatic reconnect, the route is restarted for an own client, and an error is logged for a client given with the `client` option (CAMEL-24511); - after the consumer's own reconnect (a failed message with `manualAcksEnabled` on a kept session, CAMEL-25793), when the broker did not keep the session, the route is restarted too. That connect created a session the broker keeps, so a later reconnect would find it and not subscribe, and the consumer would stay connected without its subscription. This also applies when that subscribe fails because the connection is lost; before, the failure went to the exception handler and the next reconnect found the session and did not subscribe. A granted QoS lower than the requested one (0x00 or 0x01 for a QoS 2 request) is not a failure and is left as is. A token without reason codes is taken as granted. Tests, new `PahoMqtt5SubscribeRefusedTest` (a mocked `MqttClient` whose `subscribe` returns a token with the reason codes, as Paho does): - `routeDoesNotStartWhenTheBrokerRefusesTheSubscription` (0x87): on main the route started. - `refusedSubscriptionOnReconnectStopsTheRoute`: the subscription is granted on start and refused after `connectComplete(reconnect=true)`; the test waits until the restart closed the client twice and subscribed three times, then checks Stopped. On main the route stayed Started. - `refusedSubscriptionAfterTheReconnectOfAFailedMessageStopsTheRoute`: `manualAcksEnabled`, `cleanStart=false`, `sessionExpiryInterval=60`, a message that fails, and a reconnect that finds no session and gets a refused subscription; on main the route stayed Started. - `routeStartsWhenTheBrokerGrantsTheSubscription` (reason code 2): the control. Without the change the three refused cases fail; with it all four pass, and the camel-paho-mqtt5 unit tests pass (79 run, 0 failures, 0 errors, 0 skipped). The ITs need Docker and were not run locally (CI runs them). I built offline with the versions cached on my machine (Caffeine 3.3.0, commons-lang3 3.21.0, Guava 33.7.2); Paho 1.2.5 is the version of main. #30138 is merged, and the fake of its world answers only the token methods it models and fails a seed on any other one, so this PR carries a test-only change to it (`FakeMqtt`): it answers `getReasonCodes` (the granted QoS; the fake broker refuses no subscription), and it also models `disconnectForcibly`, which the CAMEL-27041 fix calls (with the two corrections from the review of #30138: a forced disconnect during an automatic reconnect reschedules the cycle, and an older connect does not clear the connecting flag of a newer one). Without it every seed of the world's default set fails once the consumer reads the reason codes (seed 1: "not modelled: IMqttToken.getReasonCodes"). With it, on main the world passes 1000 of 1000 seeds and the camel-paho-mqtt5 unit tests pass (1079 run including the seeds, 0 failures). The three camel-paho-mqtt5 fixes of this batch (CAMEL-27040, CAMEL-27041, CAMEL-27042) all change `PahoMqtt5Consumer`, in different methods. They merge cleanly in either order, and merged together on main (with the fake change of the CAMEL-27042 PR) the camel-paho-mqtt5 unit tests and the world pass (1089 run including 1000 seeds, 0 failures). Upgrade guide: a 4.24 entry, "camel-paho-mqtt5 - a subscription the broker refuses fails the consumer", as a route that started before now fails to start when the broker refuses its subscription. # Target - [x] I checked that the commit is targeting the correct branch (Camel 4 uses the `main` branch) # Tracking - [x] If this is a large change, bug fix, or code improvement, I checked there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for the change (usually before you start working on it): [CAMEL-27042](https://issues.apache.org/jira/browse/CAMEL-27042). # Apache Camel coding standards and style - [x] I checked that each commit in the pull request has a meaningful subject line and body. - [ ] I have run `mvn clean install -DskipTests` locally from root folder and I have committed all auto-generated changes. (I built camel-paho-mqtt5 and the core modules with the formatter and import-sort plugins and ran the camel-paho-mqtt5 unit tests. I did not run the full root build.) # AI-assisted contributions - [x] If this PR includes AI-generated code, commits have proper co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR description identifies the AI tool used. This PR was prepared with Claude Code (Claude Opus 5.5). The commit carries a `Co-Authored-By` trailer. _Claude Code on behalf of allthingssecurity_ 🤖 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]
