HyukjinKwon opened a new pull request, #391: URL: https://github.com/apache/spark-connect-rust/pull/391
### What changes were proposed in this pull request? [SPARK-61205](https://issues.apache.org/jira/browse/SPARK-61205): Use TLS for a token on any host other than `localhost`, also `127.0.0.1` and `[::1]`, as PySpark does. - `ChannelBuilder::secure()` is TLS when `use_ssl=true`, or when a token is present (in the connection string or in `SPARK_CONNECT_AUTHENTICATE_TOKEN`), unless the host is `localhost`. The loopback literals `127.0.0.1` and `[::1]` were exempt before, so a token to them was sent in cleartext over `http://`. - The exemption is now exactly the host `localhost`, compared case-insensitively, because `urllib` lowercases the host that PySpark compares (`core.py` `toChannel`: `not use_ssl and host == "localhost"`). `LOCALHOST` is therefore plaintext, as in PySpark, and `127.0.0.1` and `[::1]` are TLS. - The token guard that SPARK-59139 added uses the same decision, so a token is never sent in cleartext to a host other than `localhost`. - The TLS server name of an IPv6 host is its address without the brackets. A TLS channel to `[::1]` failed to configure before, because the bracketed name is not a valid server name. - `docs/configuration.md` says that a token is sent without TLS only to `localhost`. - The Rust `ChannelBuilder` gets `is_localhost()`, which `secure()` now uses. `is_loopback()` stays, with its meaning (localhost or a loopback literal), so the public API of `spark-connect-core` only grows. Official PySpark 4.3.0 against a plaintext server, for `sc://<host>:<port>/;token=T`: `localhost` and `LOCALHOST` connect, `127.0.0.1` and `[::1]` time out, because they use TLS. This PR gives the same results. The Python drop-in has no TLS decision of its own: it passes the URL to this code. ### Why are the changes needed? A connection string such as `sc://127.0.0.1:443/;token=T`, for example through an SSH tunnel to a TLS endpoint, made the client dial plaintext, and the token went in cleartext. PySpark uses TLS for every host except `localhost`, so the same URL works there and sends the token over TLS. ### Does this PR introduce _any_ user-facing change? Yes. A token to `127.0.0.1` or `[::1]` without `use_ssl=true` is sent over TLS, as in PySpark. The connections to such a host without TLS now fail (a plaintext server times out). `localhost` is unchanged. The Rust API adds `ChannelBuilder::is_localhost()`. ### How was this patch tested? - Unit tests in `channel.rs`: `scheme_is_tls_for_a_token_on_any_host_but_localhost` checks the scheme for `localhost`, `LOCALHOST`, `127.0.0.1` and `[::1]` with and without a token, and with `use_ssl=true`. Without this change the `127.0.0.1`, `[::1]` and `LOCALHOST` rows fail. - `test_token_without_ssl_to_localhost_is_allowed` (client.rs) keeps `localhost` and `LOCALHOST` plaintext, with the bearer header attached. `test_token_to_loopback_ip_literals_is_sent_over_tls` connects to `127.0.0.1` and `[::1]` with a token, over TLS, and checks the bearer header. Without this change the scheme assertion fails; the IPv6 server name fix is what lets the TLS connect succeed. - The integration tests `deadlines.rs` and `reattach_cleanup.rs` connect to their in-process gRPC servers with a token. They now use `sc://localhost:<port>`, the plaintext case, because the server is plaintext. - Measured against a plaintext Spark Connect 4.3.0 server with the drop-in: `sc://localhost/;token=T` and `sc://LOCALHOST/;token=T` connect, `sc://127.0.0.1/;token=T` and `sc://[::1]/;token=T` time out (TLS), as official PySpark 4.3.0 does for the same URLs. - `cargo test -p apache-spark-connect-core --release` passes (all targets), and `cargo test -p apache-spark-connect --release --no-run` compiles all tests. - Against a local Spark 4.3.0 server, the live `test_dropin_e2e.py -k "remote or connect or channel or token or session"` tests (11 passed). - The lib tests of both crates (619 passed), the build of all tests, also with `wasm-udf`, and the offline Python suites (2539 passed, 8 skipped); rustfmt passes, and clippy reports no diagnostics in the changed lines. CI runs the e2e and live suites. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Opus 5.5 This pull request and its description were written by Isaac. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
