davsclaus opened a new pull request, #3200: URL: https://github.com/apache/camel-kamelets/pull/3200
Part of #3170: `mongodb-source`, `mongodb-sink` and `mongodb-changes-stream-source` take a MongoDB connection string, so they can connect to MongoDB Atlas (`mongodb+srv://`) and log in as a user defined in a database other than `admin` (`authSource`). ### What was missing The Kamelets built their client from `hosts`, `username` and `password` as `mongodb://user:password@hosts`, through a `SslAwareMongoClient` template bean. That allowed: - no `mongodb+srv://` string, which is what MongoDB Atlas gives you; - no `authSource`, so the user always had to be defined in `admin`; - no other connection-string settings, such as `replicaSet`, `tls`, `appName` or `w`. ### The option `connectionString` is an optional string, such as `mongodb+srv://myuser:[email protected]/?retryWrites=true&w=majority`. - It goes to the endpoint as `connectionUriString=RAW({{?connectionString}})`. Since CAMEL-27056 (Camel 4.24), the endpoint creates its own client from that string even without `hosts`, and closes the client when it stops. - When it is set, `hosts`, `username`, `password`, `ssl` and `sslValidationEnabled` are not used. TLS is used as the string says: always with `mongodb+srv://`, and with `mongodb://` only when it has `tls=true`. - It holds the password, so it is `format: password` with `x-descriptors: [urn:camel:group:credentials]`. - It has no `default`. When it is not set, the optional placeholder removes the parameter. **No separate `authSource` option.** `authSource` goes in the connection string (`?authSource=shop`). A separate option could not work with either way of connecting: - `SslAwareMongoClient` has no `authSource`, and adding something after `hosts` would change today's connection string. It would also break anyone who already puts `/?authSource=...` in `hosts` as a workaround. - The `authSource` endpoint option is used only when the endpoint builds its client from its own `hosts` option, and that path ignores TLS. The `username` description now says to use `connectionString` with `authSource` for a user defined in another database. **Template change.** A `mongoConnection` set on the endpoint wins over `connectionUriString`. So the `SslAwareMongoClient` bean moved from the endpoint (`mongoConnection=#{{mongo-client}}`) to the `mongoConnection` property of the template's `MongoDbComponent` bean. The endpoint uses the client of the component only when it has no `connectionUriString`. With the option unset, the Kamelet therefore uses exactly the same client built from the same properties. The endpoint URI only loses `mongoConnection=%23mongo-client-N`. I also tried the other route, looking the bean up through the endpoint path (`{{local-mongodb}}:{{mongo-client}}`). It fails: the template-local bean is not in the registry any more when the endpoint starts. **`hosts` is no longer required.** The bean takes `{{hosts:}}`. With neither `hosts` nor `connectionString`, the Kamelet fails at startup with `The connection string contains an empty host '[]'`. With `{{?hosts}}`, the client would instead wait 30 seconds for a server named `null`. The `hosts` description names this error. **Docs.** The descriptions say that the user name and the password must have `@`, `/`, `:` and `%` percent-encoded. This was already true for the `password` option, because the bean puts it into the connection string as it is. Each partial page has a "Connecting with a connection string" section with an Atlas example and an `authSource` example. ### Verification Tested with Camel JBang 4.24.0-SNAPSHOT against `mongo:8`, a one-member replica set (`--replSet rs0`, keyfile) with auth on: - `root` defined in `admin`; - `shopuser`, with password `shop/pw@1`, and `shopuser2`, both defined in `shop`; - `specialuser`, with password `a/b@c`, defined in `admin`. | Case | Kamelet | Result | |---|---|---| | Old way: `hosts` + `root`/`rootpw` + `ssl: false`, on `main` and on this branch | sink, source, changes stream | Inserted, read, and insert/update/delete received on both, with the same output | | Endpoint URI with options unset (`camel get endpoint`), `main` vs branch | sink, source | Same, except that `mongoConnection=%23mongo-client-N` is gone (the client is now on the component) | | `connectionString` with `root`, `?authSource=admin` | sink, source, changes stream | Works | | `connectionString` with `root`, no `authSource` | sink | Works (the default is `admin`) | | `connectionString` with `shopuser`, `?authSource=shop`, password percent-encoded | sink, source, changes stream | Works | | `connectionString` with `shopuser`, no `authSource` | sink | Fails: `AuthenticationFailed`, `source='admin'` | | `connectionString` with `shopuser`, database in the path (`/shop`) | sink | Works | | `connectionString` with `authSource=shop&replicaSet=rs0&appName=kw2` | sink | Works | | Old way with `shopuser2` (defined in `shop`) | sink | Fails: `AuthenticationFailed`, `source='admin'`, as on `main`. This is the gap. | | `connectionString` with `shopuser2`, `?authSource=shop` | sink | Works | | `connectionString` set together with `hosts: nosuchhost:1`, `ssl: true` | sink | Works: `hosts` and `ssl` are not used | | `connectionString` without credentials, plus `username`/`password` | sink | Fails: `requires authentication`, so `username` and `password` are not used | | `connectionString` `mongodb://...?tls=true` against the server without TLS | sink | Fails in the TLS handshake: the setting reaches the driver | | `connectionString` `mongodb+srv://[email protected]/...` | sink | Fails with `Failed looking up SRV record for '_mongodb._tcp.cluster0.kw2test.invalid'`: the `+srv` string reaches the driver unchanged | | Old way, password `a/b@c` as written, and as `a%2Fb%40c` | sink | Fails (`invalid host and port`); works when percent-encoded | | Neither `hosts` nor `connectionString` | sink, source, changes stream | Fails at startup: `The connection string contains an empty host '[]'` | `mongodb+srv://` against a real SRV record was **not** tested. The driver resolves SRV records through the system DNS of the JVM, which cannot be pointed at a local DNS server without changing the system configuration. The driver's own parsing shows that a `mongodb+srv://` string turns TLS on. `./mvnw clean verify -DskipTests` and `./mvnw clean verify` passed. The MongoDB Citrus itests (`MongoIT`) were not run locally; CI runs them. _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]
