Claus Ibsen created CAMEL-27346:
-----------------------------------

             Summary: camel-mongodb - SslAwareMongoClient: a readPreference 
property
                 Key: CAMEL-27346
                 URL: https://issues.apache.org/jira/browse/CAMEL-27346
             Project: Camel
          Issue Type: Improvement
          Components: camel-mongodb
            Reporter: Claus Ibsen
             Fix For: 4.24.0


The MongoDB Kamelets (mongodb-source, mongodb-changes-stream-source and 
mongodb-sink of apache/camel-kamelets) do not give {{hosts}} to the endpoint. 
They create the client as a bean of 
{{org.apache.camel.component.mongodb.SslAwareMongoClient}} (properties 
{{hosts}}, {{username}}, {{password}}, {{ssl}}, {{sslValidationEnabled}}) and 
set it on the component as {{mongoConnection}}.

CAMEL-27193 applies {{readPreference}} and the other client options of the 
endpoint only to a client the endpoint creates itself. For a client set with 
{{mongoConnection}}, the endpoint logs:

{noformat}
WARN MongoDbEndpoint : The options [readPreference] of local-mongodb-2://test 
are not used: they apply only to a client the endpoint creates from hosts or 
connectionUriString, not to a MongoClient set with mongoConnection, on the 
component or from the registry. Configure that MongoClient instead.
{noformat}

So a Kamelet cannot offer a {{readPreference}} option 
(apache/camel-kamelets#3280, #3323): the client {{SslAwareMongoClient}} builds 
always reads from the primary. Checked with {{$currentOp}} and {{idleCursors}}: 
the tailable cursor of mongodb-source is opened with {{$readPreference}} 
{{primaryPreferred}} (the direct-connection form of primary), also on a Camel 
build with CAMEL-27193.

h3. Proposal

A {{readPreference}} property on {{SslAwareMongoClient}}, set on the 
{{MongoClientSettings}} it builds 
({{MongoClientSettings.Builder.readPreference}}). It takes the same values as 
the endpoint option: {{PRIMARY}}, {{PRIMARY_PREFERRED}}, {{SECONDARY}}, 
{{SECONDARY_PREFERRED}}, {{NEAREST}}, and the driver names such as 
{{secondaryPreferred}} (reuse the mapping CAMEL-27193 added to 
{{MongoDbEndpoint}}). Not set: as today (the driver default, primary). An 
unknown value fails when the client is created.

The Kamelets then pass their {{readPreference}} option to the bean:
{code:yaml}
- name: mongo-client
  type: "#class:org.apache.camel.component.mongodb.SslAwareMongoClient"
  properties:
    hosts: "{{hosts:}}"
    readPreference: "{{?readPreference}}"
{code}

h3. Also seen in SslAwareMongoClient

It builds {{mongodb://<username>:<password>@<hosts>}} without percent-encoding 
the username and password. Checked with the MongoDB Java driver 5.14.0 ({{new 
ConnectionString(...)}}): a password with {{@}} or {{:}} fails with "The 
connection string contains invalid user information. If the username or 
password contains a colon (:) or an at-sign (@) then it must be urlencoded", 
one with {{/}} or {{?}} fails with "invalid host and port", one with {{%}} 
followed by non-hex fails in URLDecoder, and {{p%41ss}} silently becomes the 
password {{pAss}}. So the client fails to connect, or logs in with another 
password. Building a {{MongoCredential}} 
({{MongoCredential.createCredential(username, "admin", password)}}) with 
{{applyToClusterSettings(b -> b.hosts(...))}}, or encoding both with 
{{URLEncoder}}, avoids it. Could be done in the same change, with a test for a 
password with those characters.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to