yidaimi opened a new pull request, #13215:
URL: https://github.com/apache/seatunnel/pull/13215

   ### Purpose of this pull request
   
   Closes #13199. Related to the Amazon DocumentDB sink discussion in #10443.
   
   The MongoDB Java driver ignores `tlsCAFile` in the connection string, so the 
MongoDB sink cannot reach servers whose certificates come from a CA that is not 
in the JDK trust store (for example Amazon DocumentDB with the Amazon RDS CA 
bundle, or a self-managed MongoDB with a corporate CA). The only workaround 
today is changing the JVM-wide trust store of every SeaTunnel node, which 
affects all jobs and connectors.
   
   This PR adds an optional sink option `tls_ca_file` (path to a PEM CA 
bundle), following the requirements discussed in #13199:
   
   - **One shared settings builder.** The new `MongodbClientSettingsFactory` 
builds the client settings for every sink component that opens a connection: 
the writer, the transaction committer, the save-mode catalog (`data_save_mode`) 
and the dry-run validator.
   - **Per-job trust only.** Only the certificates of the PEM file are trusted, 
through a per-client `SSLContext`. No trust-all, and `javax.net.ssl.*` / 
JVM-global settings are neither used nor changed.
   - **Hostname verification is kept.** It can only be disabled explicitly with 
`tlsAllowInvalidHostnames=true` in `uri`, exactly as without the option.
   - Setting `tls_ca_file` enables TLS; combining it with `tls=false` / 
`ssl=false` is rejected with a configuration error.
   - Missing, blank, empty or invalid files fail with an error naming the 
option and the file path. Errors never contain the connection string (which can 
carry credentials).
   - **No change without the option.** Clients are still created with 
`MongoClients.create(uri)` as before; the MongoDB source and 
`MongodbCatalogFactory` are unchanged.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, a new optional sink option `tls_ca_file`:
   
   ```hocon
   sink {
     MongoDB {
       uri = 
"mongodb://user:passw...@docdb-cluster.cluster-xxxx.us-west-2.docdb.amazonaws.com:27017/?replicaSet=rs0&retryWrites=false"
       database = "test_db"
       collection = "test_collection"
       tls_ca_file = "/opt/seatunnel/certs/global-bundle.pem"
     }
   }
   ```
   
   The file must exist at the same path on every node that runs the sink. 
Documented in `docs/en/connectors/sink/MongoDB.md` and 
`docs/zh/connectors/sink/MongoDB.md` (options table and a new "TLS With a 
Private CA" section). Jobs that do not set the option behave exactly as before.
   
   ### How was this patch tested?
   
   20 new unit tests; no external server needed.
   
   `MongodbTlsCaFileHandshakeTest` starts two in-process TLS servers with 
certificates signed by a test CA, one for `DNS:localhost` and one for 
`DNS:unrelated-host.invalid` only (so hostname mismatches do not depend on DNS 
or reverse lookups). A handshake is counted only when the client accepted the 
certificate and sent application data, so TLS 1.3 server-side completion cannot 
give a false positive.
   
   | Test | Result |
   |---|---|
   | `configuredCaIsTrusted` | handshake succeeds |
   | `untrustedCaIsRejected` | rejected, `PKIX path building failed` |
   | `withoutCaFileTheJvmDefaultTrustIsUsedAsBefore` | rejected by the JDK 
trust store, as before |
   | `hostnameMismatchIsRejectedEvenWithTrustedCa` | rejected, `No subject 
alternative DNS name matching` |
   | `hostnameVerificationIsStillControlledByTheConnectionString` | succeeds 
only with `tlsAllowInvalidHostnames=true` |
   
   `MongodbClientSettingsFactoryTest` (8): unchanged settings and 
`MongoClients.create(String)` without the option; TLS enabled with an isolated 
context; hostname setting kept from the URI; every certificate of a bundle 
trusted; `tls=false` rejected; missing / directory / blank / empty / garbage 
files rejected without leaking the URI.
   
   `MongodbSinkTlsCaFileTest` (7), through `MongodbSinkFactory` as the engine 
uses it: option registered as optional; writer and transaction committer 
receive the CA file; save-mode catalog and dry run use the CA-based settings; 
without the option the catalog still calls `MongoClients.create(uri)`; an 
invalid file fails the dry run with a message naming the file and not the URI.
   
   Test certificates are stored as PKCS#12 under `src/test/resources/tls` and 
can be regenerated with `generate-test-certs.sh` (CA private keys are not kept).
   
   ```shell
   ./mvnw spotless:apply -pl seatunnel-connectors-v2/connector-mongodb
   ./mvnw -pl seatunnel-connectors-v2/connector-mongodb test      # Tests run: 
63, Failures: 0, Errors: 0
   ./mvnw -q -DskipTests verify -pl seatunnel-connectors-v2/connector-mongodb
   ```
   
   This change was prepared with the help of Claude Code (AI assistant) and 
reviewed by me; the commit carries a `Generated-by` trailer as recommended by 
the ASF generative tooling guidance.
   
   ### Check list
   
   * [x] No new Jar binary package is added (only small PKCS#12 test 
certificates under `src/test/resources`).
   * [x] Documentation updated in English and Chinese.
   * [x] Not a new connector, so `plugin-mapping.properties`, 
`seatunnel-dist/pom.xml`, `label-scope-conf.yml`, e2e module and 
`plugin_config` are unchanged.
   * [x] No incompatible change: the option is optional and the previous 
behavior is kept when it is not set.
   
   🤖 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]

Reply via email to