davsclaus opened a new pull request, #30554: URL: https://github.com/apache/camel/pull/30554
[CAMEL-27346](https://issues.apache.org/jira/browse/CAMEL-27346) The MongoDB Kamelets (`mongodb-source`, `mongodb-changes-stream-source`, `mongodb-sink`) do not give `hosts` to the endpoint. They create the client as a bean of `org.apache.camel.component.mongodb.SslAwareMongoClient` and set it on the component as `mongoConnection`. So the `readPreference` endpoint option of CAMEL-27193 (#30547, endpoint client options) does not reach them: the endpoint logs that the option is not used, and the client always reads from the primary. ### Changes in `SslAwareMongoClient` 1. **A `readPreference` property**, set on the `MongoClientSettings` the client builds. It takes the same values as the endpoint option (`PRIMARY`, `PRIMARY_PREFERRED`, `SECONDARY`, `SECONDARY_PREFERRED`, `NEAREST`, and driver names such as `secondaryPreferred`). The mapping is shared with `MongoDbEndpoint` (new package-private `MongoDbEndpoint.toReadPreference`). If the property is not set, or is blank (as a Kamelet's `{{?readPreference}}` can be), the driver default is kept. An unknown value fails when the client is created. The property has a setter only, because `MongoClient.getReadPreference()` already exists and returns the read preference of the client. 2. **The username and password are percent-encoded.** They went into `mongodb://user:password@hosts` as they were. With driver 5.14.0, a password containing `@`, `:`, `/`, `?` or `%` failed to connect, and `p%41ss` silently became `pAss`. They are now encoded with `URLEncoder`, with a space written as `%20`, because the driver decodes `+` as a literal `+`. I kept the connection string, not a `MongoCredential` with cluster hosts, so everything else stays as before: `hosts` is parsed the same way, the `admin` authSource is the default (the Kamelets document "a user defined in the admin database"), and a single host still gets a direct connection. ### Tests - `SslAwareMongoClientTest` (unit tests, no server): - the read preference values, not set, blank, and an unknown value - binding the properties as the Kamelet bean does (`PropertyBindingSupport`) - passwords `p@ss`, `p:ss`, `p/ss`, `p?ss`, `p%ss`, `p%41ss`, `p+s s#&=`, `@:/?%` and `pässwörd` - a username with special characters - the `admin` source - `SslAwareMongoClientIT` (container): - logs in as an `admin` user with the password `p@ss:w/o?r%d %41` - counts through `mongodb:kameletClient?...` and checks that the collection reads with `secondaryPreferred` - a wrong password is rejected, so the server really checks the credentials - All 1116 unit tests of camel-mongodb pass with `-Psourcecheck`. `MongoDbClientOptionsIT` and `MongoDbCredentialsFromUriConnectionIT` pass. ### Docs - The `mongodb` component page lists the properties of `SslAwareMongoClient` (catalog copy regenerated). - An upgrade guide 4.24 entry: a password that was percent-encoded by hand to work around this must now be given as it is. apache/camel-kamelets#3323 (draft) waits on this. _Claude Code on behalf of Claus Ibsen_ 🤖 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]
