allthingssecurity opened a new pull request, #30828: URL: https://github.com/apache/camel/pull/30828
# Description [CAMEL-27494](https://issues.apache.org/jira/browse/CAMEL-27494) `PubsubReloadTriggerTask` receives the secret events of the GCP secret refresh from a Google Pub/Sub `Subscriber` that it builds once, in `doStart`. `run()` runs every refresh period (30 s by default) on the timer thread that all the periodic tasks of the CamelContext share. It set `lastCheckTime` and started the `Subscriber` only when its state was `NEW`. A `Subscriber` whose streaming pull gets a status it does not retry (`PERMISSION_DENIED` while an IAM binding is changed, `NOT_FOUND`) goes to `FAILED`, and a failed `ApiService` cannot be started again. `run()` left it there for good. No secret event was received any more, so no later secret rotation reloaded the CamelContext until a restart. The failure was logged only at DEBUG (the `IllegalStateException` of `awaitRunning()`, which the timer ignores), and `run()` still set `lastCheckTime` every period, so the dev console showed a recent check while nothing was listening. Change: - `doStart` keeps a factory that builds the `Subscriber` (subscription name, the credentials it resolved, the task's receiver) and builds the first one with it, as before. - `run()` replaces a `Subscriber` that is `FAILED` (or `TERMINATED` while the task is started) with a new one from the factory, and starts it. That is at most one new `Subscriber` per refresh, so the retry rate is the refresh period. Only the client is recreated; the Pub/Sub subscription is not touched. The old `Subscriber` needs no stop: when its streaming pull fails, the `Subscriber` shuts its connections, executors and stub down itself before it reports `FAILED` (google-cloud-pubsub 1.160.0); 50 replacements in a row leave the number of threads unchanged. - logging: the first replacement in a row at WARN with the root cause of the failure (the gRPC status of the streaming pull, for example `PERMISSION_DENIED`; the `Subscriber`'s own `failureCause()` only says that an inner service failed, it goes to DEBUG with its stack trace), the repeats at DEBUG, and a `Subscriber` that is running again at INFO. A new `Subscriber` reports running before the service has answered its streaming pull, so one that replaced a failed `Subscriber` counts as running (the INFO, `lastCheckTime`) only when it is still running at the next refresh. While the streaming pull keeps being refused, there is one WARN and no INFO; without that, every refresh logged a WARN and a wrong "running again" INFO, and moved the last check. - `awaitRunning()` now has a deadline of 5 seconds. Before it had none, so a start that never completed held the shared timer thread and with it every other periodic task (AWS, HashiCorp, Azure refresh). A `Subscriber` still starting after 5 s is looked at again at the next refresh; one that failed while starting is replaced then. So a start that hangs holds the timer thread for at most 5 s per new `Subscriber`. A `Subscriber` that stays `STARTING` is not replaced; the real `Subscriber` opens its streams asynchronously and reports running quickly. - `lastCheckTime` is set only when the `Subscriber` is running, as above (and, as before, when the task has no `Subscriber`), so the "last check" of the dev console stops moving while it is not. - if Camel stops while `run()` starts a new `Subscriber`, `run()` stops it, so `doShutdown` (on another thread) cannot miss it. The `subscriber` field is `volatile`. The new `Subscriber` uses the credentials that `doStart` resolved. If the failure came from credentials that are no longer valid (a revoked service account key), the new subscribers fail the same way until a restart, but now with a WARN. Not changed here (separate follow-ups, I will file tickets): - stopping and starting the CamelContext again drops every scheduled vault refresh task (the scheduler clears its listeners on shutdown, and `configureVaultRefresh` runs only once). Low: restarting a stopped context is not recommended. - the Azure Event Hubs refresh task ignores processor errors (`processError` does nothing), and its `run()` only sets `lastCheckTime`; I did not check whether the Event Hubs processor can stop by itself. - the same kind of problem in the IBM Event Streams task (one failed poll ends it) has its own ticket and PR (CAMEL-27493). Test: new `PubsubReloadTriggerTaskSubscriberFailedTest` (package-private, no sleep). It runs the real `Subscriber` with the task's receiver against an in-process fake of the Pub/Sub Subscriber service (gRPC), and schedules the task with the CamelContext's period task scheduler (50 ms ticks). In the first test (100 ms refresh period) the fake first refuses the streaming pull with `PERMISSION_DENIED` (the real `Subscriber` goes to `FAILED`), then allows it and delivers one `SECRET_VERSION_ADD` event of the watched secret. Checked: a new `Subscriber` is running, the reload happens, the failure is logged at WARN with `PERMISSION_DENIED`, the new `Subscriber` still running at the next refresh is logged at INFO, and stopping Camel stops the new `Subscriber`. The second test refuses every streaming pull 50 ms after it starts (as over a network, so each new `Subscriber` reports running first; 500 ms refresh period): after five `Subscriber`s there is one WARN, no INFO, and `lastCheckTime` has not moved since the first failure. That test fails 3 of 3 runs when a new `Subscriber` counts as running at once (`lastCheckTime` moves, a WARN and an INFO at every refresh). The same scenario fails with the current task (the `Subscriber` stays `FAILED`, 0 reloads after 10 s, 3 of 3 runs); the committed test needs the new factory field, so that check ran with a copy that sets the `Subscriber` the old way (the copy passes with the change). With the change the unit tests of camel-google-secret-manager pass (20 run, 0 failures) and the new test class passes 20 of 20 runs (40 tests). The ITs of the module were not run locally (CI runs them). A TLA+ model of the push-based refresh listeners and the period task scheduler found this: "every rotation is eventually consumed while Camel runs" fails after one subscriber failure, and with `awaitRunning()` that never returns the shared timer thread is blocked for good. The fixed model (replace at the next refresh, bounded wait) is written but not checked yet. # 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-27494](https://issues.apache.org/jira/browse/CAMEL-27494). # 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-google-secret-manager and the core modules with the formatter and import-sort plugins and ran the module's unit tests, offline, so with google-cloud-pubsub 1.160.0 and google-cloud-secretmanager 2.102.0, the versions before the latest dependency bump. I did not run the full root build. No option or generated file changed.) # 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]
