HyukjinKwon opened a new pull request, #925:
URL: https://github.com/apache/spark-connect-rust/pull/925
### What changes were proposed in this pull request?
A follow-up of SPARK-62188 (#918): the forms of a gRPC target, and the reads
of a builder, that the KD row "Custom gRPC channels" lists as remaining there.
The PR has two commits: the first connects to the target forms, and the second,
after an independent review of the first, decides the TLS, the token, the
`:authority` and the proxy of a target of its own names, and fixes the merging
of the metadata of a call (see "The security round" below). SPARK-62252, right
after this PR, fixes the other findings of that review.
PySpark's client connects through its builder's `toChannel`, which gives
grpcio the builder's `endpoint` as a gRPC target. It reads the builder as it is
made:
- its `session_id` and its `userId`, once each, and again when they are set;
- what `toChannel` reads, including the keepalive properties of its channel
options;
- `metadata()`, for its ArtifactManager. It calls `metadata()` again for
each attempt of each call.
gRPC (grpcio 1.85) reads a target like this:
- It parses the target as a URI of the scheme of one of its resolvers, of
any case; otherwise it prefixes `dns:///`.
- It percent-decodes the authority and the path, and drops the query and the
fragment, after checking their characters.
- It splits a `dns:` name as its resolver does, of the ports `http`/`https`,
`absl::SimpleAtoi` and 16 bits.
- It tries the addresses of `ipv4:` and `ipv6:` in turn, and connects to the
Unix sockets of `unix:` and `unix-abstract:`.
- It fails the first call of a target that it refuses, with `UNKNOWN` (`the
target uri is not valid`), `INTERNAL` (`Cannot parse authority`) or
`UNAVAILABLE` (`empty address list`, `Failed to parse port in name`,
`Misformatted domain name`).
The drop-in (after #918):
- refused, when the session was made, a target with `/`, `?`, `#`, `;`, `@`
or whitespace, and any `unix:` target;
- took only the first address of `ipv4:` and `ipv6:`;
- decoded nothing, and parsed the port as its URL parser does;
- read `userId` three or four times, and never read the keepalive properties
or `metadata()`.
So:
- **`spark_connect_core::grpc_target` (new, doc-hidden) reads a target as
grpcio does.** It follows:
- gRPC's URI parser, with its percent-decoding (two hexadecimal digits)
and its checks of the query and fragment characters;
- the `SplitHostPort` of a `dns:` name, with the ports `http` and `https`,
`absl::SimpleAtoi`, and 16 bits (`localhost:99999` is port 34463);
- the authority check of `dns:`: an IP address and a port, else `INTERNAL`;
- the addresses of `ipv4:` and `ipv6:`, of `inet_pton`, an IPv6 zone (a
number or `if_nametoindex`) and `sscanf("%d")`, where an empty entry is skipped
and any invalid one makes the whole target invalid;
- the `unix:` and `unix-abstract:` paths, up to 107 bytes.
It gives a `channel::ChannelTarget`, and gRPC's names of the target,
`channel::TargetNames` (doc-hidden, see the security round).
`ChannelBuilder::with_target` and `with_target_names`, and
`SparkSessionBuilder::channel_target` and `channel_target_names`, give the
channel the target and its names in place of the connection string's host and
port. The connection string keeps a host and a port that stand for the target
in its URL only: they decide neither its TLS, nor its token, nor its
`:authority`, nor its proxy.
- **The connector connects to the target's forms:**
- It tries the name, the addresses or the Unix sockets in turn, with the
same happy-eyeballs pacing as a host's addresses. A target with no address
fails with `empty address list`.
- A refused target connects to an in-memory HTTP/2 server that fails every
call with the target's status. So the calls keep gRPC's code, and the retry
policies, which retry `UNAVAILABLE`, do not retry them.
- It makes its connections itself (socket2), so a `connect` that fails as
it starts gets gRPC's `FAILED_PRECONDITION` text (`connect failed: addr:
unix:/tmp/a.sock error: No such file or directory`).
- gRPC's local credentials of TCP refuse a Unix socket. TLS over a Unix
socket verifies gRPC's name of it, as any target's: the host of its default
authority, the percent-encoded path (`tmp` of `tmp%2Fa.sock`), which a
certificate of `localhost` does not match, so it fails as grpcio's does
(`Hostname Verification Check failed`).
- In the grpcio dialect, a name is resolved as c-ares takes it:
- an address of four decimal numbers, with leading zeros too, or an IPv6
address with a zone, is an address;
- `localhost` and the names under it are the loopback;
- a name with a character outside `[A-Za-z0-9-._/*]`, an empty label or
a label over 63 characters is not looked up (`Misformatted domain name`);
- a name that `inet_aton` would read as an address (`127.1`) is looked
up as a name.
- `client.rs` builds the `Target` of the target and of its names, and
takes the `:authority`, the TLS name and the proxy of the names.
- **The binding reads the builder as PySpark's client does**, with the same
order and counts:
- `_checked_channel_builder` returns the session ID and the user that it
read. The client state takes that user, so `userId` is not read again.
- The keepalive properties are read as `_effective_channel_options` reads
them.
- `metadata()` is read last; `ChannelBuilder::with_metadata` gives its
result to the requests.
- **A builder with its own `metadata()` is read again for each call**,
through `SparkConnectClient::set_metadata_source` (doc-hidden):
- The core marks each request, and `metadata::Checked` reads the source
just before sending it, so no transport lock is held.
- The read happens on the thread of the call, with the GIL, through
`deliver::call` (the same mechanism as the retry policy decisions), and keeps
any pending exception (`deliver::preserving_pending_exception`, SPARK-62227).
The requests that the transport sends in the background are read otherwise
until SPARK-62252 (see the KD row below).
- The result is encoded as grpcio encodes it (`_metadata_of_call`: None,
bytes, `-bin` keys and grpcio's `TypeError`s) and checked as gRPC's core checks
it (`INTERNAL`, `Invalid metadata`).
- An exception of `metadata()` fails the call as it is, without retries.
- The artifact requests carry what was read when the session was made, as
PySpark's ArtifactManager does. A reattachable execution carries what was read
when it started, as PySpark's iterator does.
- The default `ChannelBuilder.metadata()` path is unchanged, with no
per-call Python.
- **The URL of the channel now takes nothing of the endpoint but a validated
host and a numeric port.** From the builder it takes only the reserved
parameters: session ID, user, token, user agent and keepalive. Dropping the
endpoint's query and fragment, as gRPC does, is therefore safe. For example,
`127.0.0.1:15002/;use_ssl=true;token=t` is a name whose port does not parse,
not URL parameters (tested).
- **`dev/parity/known-differences.md`, the row "Custom gRPC channels", keeps
only what remains:**
- the `userAgent`, which PySpark reads for each request;
- a retry policy's own `can_retry` of an exception from `metadata()`;
- the order of the metadata of different keys, which the `http::HeaderMap`
of hyper groups by key (see the security round);
- the `:authority` of a target whose authority HTTP cannot hold, and the
SNI of a name that is not a DNS name (see the security round);
- the DNS server of a `dns://` authority, which the system's resolver does
not ask, until SPARK-62232;
- gRPC core's stderr lines for a target that it refuses;
- until SPARK-62252, the other findings of the review (see "What still
differs" below), among them the metadata of the requests that the transport
sends in the background: the `ExecutePlan` of a finalizer carries the metadata
of the builder's parameters as the session was made, and the token, but none of
a `metadata()` of its own, where PySpark's finalizer calls `metadata()` for it;
and the deletion of a native ML object reads `metadata()` on a thread of the
transport (`Dummy-1`).
- `docs/compatibility.md` describes the target forms, and lists the
doc-hidden APIs and the connector changes in the Rust notes of the 4.4 line.
Checked on both clients against a Spark 4.3.0 server, on the first commit
(eb4f4ca0, "now") and on e1 (e63ca674), the drop-in of #918; the second commit
changes none of these plaintext outcomes, and its own probes follow in the
security round. The endpoint forms (`range(2).count()`, or the status and the
details of its error):
| endpoint | pyspark-client 4.3.0 | e1 (e63ca674) | now |
|---|---|---|---|
| `ipv4:127.0.0.1:1,127.0.0.1:17839` | count=2 | UNAVAILABLE failed to
connect to all addresses; last error: UNKNOWN: ipv4:127.0.0.1:1: Failed to
connect to remote host: | count=2 |
| `ipv4:127.0.0.1:17839,127.0.0.1:1` | count=2 | count=2 | count=2 |
| `ipv4:127.0.0.1:1,127.0.0.1:2` | UNAVAILABLE failed to connect to all
addresses; last error: UNKNOWN: ipv4:127.0.0.1:2: Failed to connect to remote
host: | UNAVAILABLE failed to connect to all addresses; last error: UNKNOWN:
ipv4:127.0.0.1:1: Failed to connect to remote host: | UNAVAILABLE failed to
connect to all addresses; last error: UNKNOWN: ipv4:127.0.0.1:2: Failed to
connect to remote host: |
| `ipv6:[::1]:1,[::1]:17839` | count=2 | UNAVAILABLE failed to connect to
all addresses; last error: UNKNOWN: ipv6:%5B::1%5D:1: Failed to connect to
remote host: | count=2 |
| `ipv4:127.0.0.1:1,[::1]:17839` | UNKNOWN Failed to create channel to
'ipv4:127.0.0.1:1,[::1]:17839':the target uri is not valid:
ipv4:127.0.0.1:1,[::1]:1 | UNAVAILABLE failed to connect to all addresses; last
error: UNKNOWN: ipv4:127.0.0.1:1: Failed to connect to remote host: | UNKNOWN
Failed to create channel to 'ipv4:127.0.0.1:1,[::1]:17839':the target uri is
not valid: ipv4:127.0.0.1:1,[::1]:1 |
| `ipv4:127.0.0.1` | UNKNOWN Failed to create channel to
'ipv4:127.0.0.1':the target uri is not valid: ipv4:127.0.0.1 | UNAVAILABLE
failed to connect to all addresses; last error: UNKNOWN: ipv4:127.0.0.1:443:
Failed to connect to remote hos | UNKNOWN Failed to create channel to
'ipv4:127.0.0.1':the target uri is not valid: ipv4:127.0.0.1 |
| `ipv6:[::1]` | UNKNOWN Failed to create channel to 'ipv6:[::1]':the target
uri is not valid: ipv6:[::1] | UNAVAILABLE failed to connect to all addresses;
last error: UNKNOWN: ipv6:%5B::1%5D:443: Failed to connect to remote hos |
UNKNOWN Failed to create channel to 'ipv6:[::1]':the target uri is not valid:
ipv6:[::1] |
| `ipv4:127.0.0.1:17839,127.0.0.1` | UNKNOWN Failed to create channel to
'ipv4:127.0.0.1:17839,127.0.0.1':the target uri is not valid:
ipv4:127.0.0.1:17839,1 | count=2 | UNKNOWN Failed to create channel to
'ipv4:127.0.0.1:17839,127.0.0.1':the target uri is not valid:
ipv4:127.0.0.1:17839,1 |
| `ipv4:` | UNAVAILABLE empty address list | creation raises
PySparkValueError: [INVALID_CONNECT_URL] Invalid URL for Spark Connect:
Hostname is missing in the URL: | UNAVAILABLE empty address list |
| `ipv6:` | UNAVAILABLE empty address list | creation raises
PySparkValueError: [INVALID_CONNECT_URL] Invalid URL for Spark Connect:
Hostname is missing in the URL: | UNAVAILABLE empty address list |
| `ipv4:256.0.0.1:17839` | UNKNOWN Failed to create channel to
'ipv4:256.0.0.1:17839':the target uri is not valid: ipv4:256.0.0.1:17839 |
UNAVAILABLE errors resolving 256.0.0.1:17839: [... Domain name not found] |
UNKNOWN Failed to create channel to 'ipv4:256.0.0.1:17839':the target uri is
not valid: ipv4:256.0.0.1:17839 |
| `ipv4:localhost:17839` | UNKNOWN Failed to create channel to
'ipv4:localhost:17839':the target uri is not valid: ipv4:localhost:17839 |
count=2 | UNKNOWN Failed to create channel to 'ipv4:localhost:17839':the target
uri is not valid: ipv4:localhost:17839 |
| `ipv6:127.0.0.1:17839` | UNKNOWN Failed to create channel to
'ipv6:127.0.0.1:17839':the target uri is not valid: ipv6:127.0.0.1:17839 |
count=2 | UNKNOWN Failed to create channel to 'ipv6:127.0.0.1:17839':the target
uri is not valid: ipv6:127.0.0.1:17839 |
| `dns://8.8.8.8/localhost:17839` | INTERNAL Illegal status code from
resolver; original status: INVALID_ARGUMENT: Cannot parse authority: 8.8.8.8 |
count=2 | INTERNAL Illegal status code from resolver; original status:
INVALID_ARGUMENT: Cannot parse authority: 8.8.8.8 |
| `dns://[::1]/localhost:17839` | INTERNAL Illegal status code from
resolver; original status: INVALID_ARGUMENT: Cannot parse authority: [::1] |
count=2 | INTERNAL Illegal status code from resolver; original status:
INVALID_ARGUMENT: Cannot parse authority: [::1] |
| `dns://127.0.0.1:53/localhost:17839` | count=2 | count=2 | count=2 |
| `dns://x/localhost:17839` | INTERNAL Illegal status code from resolver;
original status: INVALID_ARGUMENT: Cannot parse authority: x | count=2 |
INTERNAL Illegal status code from resolver; original status: INVALID_ARGUMENT:
Cannot parse authority: x |
| `dns:///localhost` | UNAVAILABLE failed to connect to all addresses; last
error: UNKNOWN: ipv4:127.0.0.1:443: Failed to connect to remote hos |
UNAVAILABLE failed to connect to all addresses; last error: UNKNOWN:
ipv4:127.0.0.1:443: Failed to connect to remote hos | UNAVAILABLE failed to
connect to all addresses; last error: UNKNOWN: ipv4:127.0.0.1:443: Failed to
connect to remote hos |
| `dns:///` | UNKNOWN Failed to create channel to 'dns:///':the target uri
is not valid: dns:/// | creation raises PySparkValueError:
[INVALID_CONNECT_URL] Invalid URL for Spark Connect: Hostname is missing in the
URL: | UNKNOWN Failed to create channel to 'dns:///':the target uri is not
valid: dns:/// |
| `dns:localhost:17839` | count=2 | count=2 | count=2 |
| `vsock:1:17839` | UNAVAILABLE errors resolving vsock:1:17839:
[field:hostname lookup error:address lookup failed for vsock:1:17839: Misfor |
creation raises ValueError: Port could not be cast to integer value as
'1:17839' | UNAVAILABLE errors resolving vsock:1:17839: [field:hostname lookup
error:address lookup failed for vsock:1:17839: Misfor |
| `http://localhost:17839` | UNAVAILABLE errors resolving
http://localhost:17839: [field:hostname lookup error:address lookup failed for
http://local | creation raises PySparkValueError: [INVALID_CONNECT_URL] Invalid
URL for Spark Connect: The endpoint 'http://localhost:1 | UNAVAILABLE errors
resolving http://localhost:17839: [field:hostname lookup error:address lookup
failed for http://local |
| `localhost:17839/x` | UNAVAILABLE errors resolving localhost:17839/x:
[field:hostname lookup error:Failed to parse port in name: localhost:178 |
creation raises PySparkValueError: [INVALID_CONNECT_URL] Invalid URL for Spark
Connect: The endpoint 'localhost:17839/x' | UNAVAILABLE errors resolving
localhost:17839/x: [field:hostname lookup error:Failed to parse port in name:
localhost:178 |
| `127.0.0.1:17839?` | UNKNOWN Failed to create channel to
'127.0.0.1:17839?':the target uri is not valid: dns:///127.0.0.1:17839? |
creation raises PySparkValueError: [INVALID_CONNECT_URL] Invalid URL for Spark
Connect: The endpoint '127.0.0.1:17839?' | UNKNOWN Failed to create channel to
'127.0.0.1:17839?':the target uri is not valid: dns:///127.0.0.1:17839? |
| `127.0.0.1:17839#` | count=2 | creation raises PySparkValueError:
[INVALID_CONNECT_URL] Invalid URL for Spark Connect: The endpoint
'127.0.0.1:17839#' | count=2 |
| `localhost:17839;a=b` | UNAVAILABLE errors resolving localhost:17839;a=b:
[field:hostname lookup error:Failed to parse port in name: localhost:1 |
creation raises PySparkValueError: [INVALID_CONNECT_URL] Invalid URL for Spark
Connect: The endpoint 'localhost:17839;a= | UNAVAILABLE errors resolving
localhost:17839;a=b: [field:hostname lookup error:Failed to parse port in name:
localhost:1 |
| `user@localhost:17839` | UNAVAILABLE errors resolving
user@localhost:17839: [... Misformatted domain name] | creation raises
PySparkValueError: [INVALID_CONNECT_URL] Invalid URL for Spark Connect: The
endpoint 'user@localhost:178 | UNAVAILABLE errors resolving
user@localhost:17839: [... Misformatted domain name] |
| `local host:17839` | UNAVAILABLE errors resolving local host:17839: [...
Misformatted domain name] | creation raises PySparkValueError:
[INVALID_CONNECT_URL] Invalid URL for Spark Connect: The endpoint 'local
host:17839' | UNAVAILABLE errors resolving local host:17839: [... Misformatted
domain name] |
| `localhost:abc` | UNAVAILABLE errors resolving localhost:abc:
[field:hostname lookup error:Failed to parse port in name: localhost:abc] |
creation raises ValueError: Port could not be cast to integer value as 'abc' |
UNAVAILABLE errors resolving localhost:abc: [field:hostname lookup error:Failed
to parse port in name: localhost:abc] |
| `localhost:99999` | UNAVAILABLE failed to connect to all addresses; last
error: UNKNOWN: ipv4:127.0.0.1:34463: Failed to connect to remote h | creation
raises ValueError: Port out of range 0-65535 | UNAVAILABLE failed to connect to
all addresses; last error: UNKNOWN: ipv4:127.0.0.1:34463: Failed to connect to
remote h |
| `unix:/tmp/none.sock` | UNAVAILABLE failed to connect to all addresses;
last error: FAILED_PRECONDITION: unix:/tmp/none.sock: connect failed: ad |
creation raises PySparkNotImplementedError: [NOT_IMPLEMENTED] An endpoint of
the gRPC scheme 'unix' is not implemented. | UNAVAILABLE failed to connect to
all addresses; last error: FAILED_PRECONDITION: unix:/tmp/none.sock: connect
failed: ad |
| `unix:///tmp/none.sock` | UNAVAILABLE failed to connect to all addresses;
last error: FAILED_PRECONDITION: unix:/tmp/none.sock: connect failed: ad |
creation raises PySparkNotImplementedError: [NOT_IMPLEMENTED] An endpoint of
the gRPC scheme 'unix' is not implemented. | UNAVAILABLE failed to connect to
all addresses; last error: FAILED_PRECONDITION: unix:/tmp/none.sock: connect
failed: ad |
| `unix-abstract:spark62220` | UNAVAILABLE failed to connect to all
addresses; last error: FAILED_PRECONDITION: unix-abstract:spark62220: connect
faile | creation raises PySparkNotImplementedError: [NOT_IMPLEMENTED] An
endpoint of the gRPC scheme 'unix-abstract' is not impl | UNAVAILABLE failed to
connect to all addresses; last error: FAILED_PRECONDITION:
unix-abstract:spark62220: connect faile |
| `unix:/tmp/nonexistent.sock` | UNAVAILABLE failed to connect to all
addresses; last error: FAILED_PRECONDITION: unix:/tmp/nonexistent.sock: connect
fai | creation raises PySparkNotImplementedError: [NOT_IMPLEMENTED] An endpoint
of the gRPC scheme 'unix' is not implemented. | UNAVAILABLE failed to connect
to all addresses; last error: FAILED_PRECONDITION: unix:/tmp/nonexistent.sock:
connect fai |
| `unix:relative.sock` | UNAVAILABLE failed to connect to all addresses;
last error: FAILED_PRECONDITION: unix:relative.sock: connect failed: add |
creation raises PySparkNotImplementedError: [NOT_IMPLEMENTED] An endpoint of
the gRPC scheme 'unix' is not implemented. | UNAVAILABLE failed to connect to
all addresses; last error: FAILED_PRECONDITION: unix:relative.sock: connect
failed: add |
35 of 35 match PySpark now; 4 with e1.
More probes of names, ports, percent-decoding, queries and fragments,
schemes and Unix paths. The rest of each table is in the scratch outputs.
| probe | cases | e1 (e63ca674) | now | what still differs |
|---|---|---|---|---|
| names (characters, ports, queries, authorities, lists) | 63 | 11 | 57 | 6:
the DNS server's answer texts (`DNS server returned general failure` vs the
system resolver's `Could not contact DNS servers` / `Domain name not found`),
as the existing row "The details of a failure that the client detects" says |
| more (percent-decoding, c-ares names, ports, lists, dns forms, schemes,
unix paths) | 102 | 11 | 95 | 6 answer texts as above; 1 is identical, but the
PySpark line has a stderr line interleaved (`dns://:53/...`) |
| numbers and long names | 18 | 9 | 17 | 1 answer text (`2130706433`) |
Unix sockets, through a relay to the server:
| case | pyspark-client 4.3.0 | e1 (e63ca674) | now |
|---|---|---|---|
| unix, insecure | count=2 | creation raises PySparkNotImplementedError:
[NOT_IMPLEMENTED] An endpoint of the gRPC scheme 'unix' is not implemented. |
count=2 |
| unix://, insecure | count=2 | creation raises PySparkNotImplementedError:
[NOT_IMPLEMENTED] An endpoint of the gRPC scheme 'unix' is not implemented. |
count=2 |
| unix-abstract, insecure | count=2 | creation raises
PySparkNotImplementedError: [NOT_IMPLEMENTED] An endpoint of the gRPC scheme
'unix-abstract' is not impl | count=2 |
| unix, token, _host localhost (local) | UNAVAILABLE failed to connect to
all addresses; last error: UNKNOWN: unix:…/relay62220.sock: Endpoint is neither
UDS or | creation raises PySparkNotImplementedError: [NOT_IMPLEMENTED] An
endpoint of the gRPC scheme 'unix' is not implemented. | UNAVAILABLE failed to
connect to all addresses; last error: UNKNOWN: unix:…/relay62220.sock: Endpoint
is neither UDS or |
| unix, use_ssl (TLS) | UNAVAILABLE failed to connect to all addresses; last
error: UNKNOWN: unix:…/relay62220.sock: Tls handshake failed (TSI_P | creation
raises PySparkNotImplementedError: [NOT_IMPLEMENTED] An endpoint of the gRPC
scheme 'unix' is not implemented. | UNAVAILABLE failed to connect to all
addresses; last error: UNKNOWN: unix:…/relay62220.sock: Tls handshake failed
(TSI_P |
| unix, token, _host 127.0.0.1 (TLS) | UNAVAILABLE failed to connect to all
addresses; last error: UNKNOWN: unix:…/relay62220.sock: Tls handshake failed
(TSI_P | creation raises PySparkNotImplementedError: [NOT_IMPLEMENTED] An
endpoint of the gRPC scheme 'unix' is not implemented. | UNAVAILABLE failed to
connect to all addresses; last error: UNKNOWN: unix:…/relay62220.sock: Tls
handshake failed (TSI_P |
| unix-abstract, token (local) | UNAVAILABLE failed to connect to all
addresses; last error: UNKNOWN: unix-abstract:relay62220abs: Endpoint is
neither UD | creation raises PySparkNotImplementedError: [NOT_IMPLEMENTED] An
endpoint of the gRPC scheme 'unix-abstract' is not impl | UNAVAILABLE failed to
connect to all addresses; last error: UNKNOWN: unix-abstract:relay62220abs:
Endpoint is neither UD |
| unix of a dead path | UNAVAILABLE failed to connect to all addresses; last
error: FAILED_PRECONDITION: unix:/tmp/none62220.sock: connect faile | creation
raises PySparkNotImplementedError: [NOT_IMPLEMENTED] An endpoint of the gRPC
scheme 'unix' is not implemented. | UNAVAILABLE failed to connect to all
addresses; last error: FAILED_PRECONDITION: unix:/tmp/none62220.sock: connect
faile |
| unix-abstract of no listener | UNAVAILABLE failed to connect to all
addresses; last error: FAILED_PRECONDITION: unix-abstract:none62220: connect
failed | creation raises PySparkNotImplementedError: [NOT_IMPLEMENTED] An
endpoint of the gRPC scheme 'unix-abstract' is not impl | UNAVAILABLE failed to
connect to all addresses; last error: FAILED_PRECONDITION:
unix-abstract:none62220: connect failed |
9 of 9 match PySpark now; 0 with e1.
What the client reads of a logging `DefaultChannelBuilder` subclass, as the
session is made and at its calls:
| connection | what is read | pyspark-client 4.3.0 | e1 (e63ca674) | now |
|---|---|---|---|---|
| `(none)` | made | session_id userId secure use_ssl token endpoint _host
_port keepalive_enabled keepalive_time_ms keepalive_timeout_ms
keepalive_without_calls metadata() | session_id userId userId secure use_ssl
token endpoint _host _port userId | session_id userId secure use_ssl token
endpoint _host _port keepalive_enabled keepalive_time_ms keepalive_timeout_ms
keepalive_without_calls metadata() |
| `(none)` | collect | userAgent metadata() userAgent metadata() userAgent
metadata() | (nothing) | metadata() metadata() metadata() |
| `(none)` | conf.get | userAgent metadata() | (nothing) | metadata() |
| `(none)` | stop | userAgent metadata() | (nothing) | metadata() |
| `/;user_id=u` | made | session_id userId userId secure use_ssl token
endpoint _host _port keepalive_enabled keepalive_time_ms keepalive_timeout_ms
keepalive_without_calls metadata() | session_id userId userId secure use_ssl
token endpoint _host _port userId userId | session_id userId userId secure
use_ssl token endpoint _host _port keepalive_enabled keepalive_time_ms
keepalive_timeout_ms keepalive_without_calls metadata() |
| `/;user_id=u` | collect | userAgent metadata() userAgent metadata()
userAgent metadata() | (nothing) | metadata() metadata() metadata() |
| `/;user_id=u` | conf.get | userAgent metadata() | (nothing) | metadata() |
| `/;user_id=u` | stop | userAgent metadata() | (nothing) | metadata() |
| `/;session_id=<uuid>` | made | session_id session_id userId secure use_ssl
token endpoint _host _port keepalive_enabled keepalive_time_ms
keepalive_timeout_ms keepalive_without_calls metadata() | session_id userId
userId secure use_ssl token endpoint _host _port userId | session_id session_id
userId secure use_ssl token endpoint _host _port keepalive_enabled
keepalive_time_ms keepalive_timeout_ms keepalive_without_calls metadata() |
| `/;session_id=<uuid>` | collect | userAgent metadata() userAgent
metadata() userAgent metadata() | (nothing) | metadata() metadata() metadata() |
| `/;session_id=<uuid>` | conf.get | userAgent metadata() | (nothing) |
metadata() |
| `/;session_id=<uuid>` | stop | userAgent metadata() | (nothing) |
metadata() |
| `/;grpc_keepalive_enabled=false` | made | session_id userId secure use_ssl
token endpoint _host _port keepalive_enabled metadata() | session_id userId
userId secure use_ssl token endpoint _host _port userId | session_id userId
secure use_ssl token endpoint _host _port keepalive_enabled metadata() |
| `/;grpc_keepalive_enabled=false` | collect | userAgent metadata()
userAgent metadata() userAgent metadata() | (nothing) | metadata() metadata()
metadata() |
| `/;grpc_keepalive_enabled=false` | conf.get | userAgent metadata() |
(nothing) | metadata() |
| `/;grpc_keepalive_enabled=false` | stop | userAgent metadata() | (nothing)
| metadata() |
| `/;x-hdr=v;user_id=u` | made | session_id userId userId secure use_ssl
token endpoint _host _port keepalive_enabled keepalive_time_ms
keepalive_timeout_ms keepalive_without_calls metadata() | session_id userId
userId secure use_ssl token endpoint _host _port userId userId | session_id
userId userId secure use_ssl token endpoint _host _port keepalive_enabled
keepalive_time_ms keepalive_timeout_ms keepalive_without_calls metadata() |
| `/;x-hdr=v;user_id=u` | collect | userAgent metadata() userAgent
metadata() userAgent metadata() | (nothing) | metadata() metadata() metadata() |
| `/;x-hdr=v;user_id=u` | conf.get | userAgent metadata() | (nothing) |
metadata() |
| `/;x-hdr=v;user_id=u` | stop | userAgent metadata() | (nothing) |
metadata() |
The creation reads match in all 5 cases, and the per-call `metadata()` reads
match too. Only the `userAgent` of each request is not read, as the KD row says.
A builder whose `metadata()` returns odd values, at `conf.set` and `collect`:
| metadata() returns | pyspark-client 4.3.0 | e1 (e63ca674) | now |
|---|---|---|---|
| ok | conf.set: ok ; collect: ok | conf.set: ok ; collect: ok | conf.set:
ok ; collect: ok |
| bad key | conf.set: INTERNAL Invalid metadata ; collect: INTERNAL Invalid
metadata | conf.set: ok ; collect: ok | conf.set: INTERNAL Invalid metadata ;
collect: INTERNAL Invalid metadata |
| bad value | conf.set: INTERNAL Invalid metadata ; collect: INTERNAL
Invalid metadata | conf.set: ok ; collect: ok | conf.set: INTERNAL Invalid
metadata ; collect: INTERNAL Invalid metadata |
| int value | conf.set: TypeError: Expected str, not <class 'int'> ;
collect: TypeError: Expected str, not <class 'int'> | conf.set: ok ; collect:
ok | conf.set: TypeError: Expected str, not <class 'int'> ; collect: TypeError:
Expected str, not <class 'int'> |
| bytes value | conf.set: ok ; collect: ok | conf.set: ok ; collect: ok |
conf.set: ok ; collect: ok |
| bin key bytes | conf.set: ok ; collect: ok | conf.set: ok ; collect: ok |
conf.set: ok ; collect: ok |
| bin key str | conf.set: TypeError: Binary metadata key="x-bin" expected
bytes, got <class 'str'> ; collect: TypeError: Binary metadata | conf.set: ok ;
collect: ok | conf.set: TypeError: Binary metadata key="x-bin" expected bytes,
got <class 'str'> ; collect: TypeError: Binary metadata |
| None | conf.set: ok ; collect: ok | conf.set: ok ; collect: ok | conf.set:
ok ; collect: ok |
| tuple of 3 | conf.set: ValueError: too many values to unpack (expected 2)
; collect: ValueError: too many values to unpack (expected | conf.set: ok ;
collect: ok | conf.set: ValueError: too many values to unpack (expected 2) ;
collect: ValueError: too many values to unpack (expected |
| generator | conf.set: ok ; collect: ok | conf.set: ok ; collect: ok |
conf.set: ok ; collect: ok |
| raises | create: KeyError: 'boom' | conf.set: ok ; collect: ok | create:
KeyError: 'boom' |
| user-agent | conf.set: ok ; collect: ok | conf.set: ok ; collect: ok |
conf.set: ok ; collect: ok |
12 of 12 match PySpark now; 6 with e1.
#### The security round (the second commit)
An independent review of the first commit found that the host and the port
that stand for a target in the URL of the channel (`localhost` of a name that a
URL cannot hold, else the first address of a list) decided its security, and
that the merging of the metadata of a `metadata()` of its own was wrong. This
commit fixes those findings:
| finding | what was wrong | fix | test |
|---|---|---|---|
| 1, CRITICAL | A builder of its own sent its token in plaintext to a host
that is not `localhost` (`[::1%25lo]:18613`, `x*y.localhost:18611`), whose
stand-in was `localhost`. | `channel::TargetNames` (doc-hidden) carry gRPC's
default authority of the target, and `is_localhost`, and so whether a token
goes in plaintext, is of its host, never of the stand-in. |
`channel::the_host_of_a_target_of_grpc_decides_its_tls_never_its_stand_in` |
| 2, CRITICAL | TLS verified `localhost`, or the first address of a list, so
a certificate that grpcio rejects was accepted, and the token was sent to its
server. | `connector::Tls::of_target_name` verifies the name that gRPC's TLS
verifies: the host of the authority up to its first `%`
(`grpc_ssl_host_matches_name`). An IP address is checked against the addresses
of the certificate, a DNS name against its names. Any other name
(`x*y.localhost`, a list, none) fails each attempt as gRPC's check of the peer
does (`Hostname Verification Check failed.`), once the chain is verified, so
nothing is sent. |
`connector::the_tls_of_a_target_of_grpc_verifies_grpcs_name_of_it` |
| 3, HIGH | The metadata of a `metadata()` of its own lost the values after
the first of a key, the token went before it, and an execution replaced the
builder's `authorization` with the token's. | `metadata::append_metadata` keeps
every value of a key, in its order, and appends the token after the source's
entries, as gRPC sends a call's metadata and then its call credentials.
Executions, reattaches and releases append too. |
`metadata::the_metadata_of_a_call_is_sent_of_its_order_and_the_token_last`;
`client::the_token_follows_an_authorization_parameter` (renamed from
`token_parameter_wins_over_an_authorization_parameter`) |
| 8, MEDIUM | The `:authority` of the calls was the stand-in's. | gRPC's
default authority, the path of no first `/`, percent-encoded as an authority,
wherever HTTP's `Authority` can hold it. |
`grpc_target::the_names_of_a_target_are_grpcs` |
| 9, MEDIUM | The proxy and its exclusions used the stand-in. | The proxy is
chosen of the host of the path, of any target but one of `unix:`, which gRPC's
proxy mapper does not proxy, and its `CONNECT` names gRPC's server: the
`host:port` of the path, of the port 443 without one. |
`connector::the_tunnel_of_a_target_of_grpc_names_the_server_of_grpcs_proxy_mapper`
|
The review's probes, on both clients, against a Spark 4.3.0 server behind
the review's recording proxies and TLS relays (its own CA through
`GRPC_DEFAULT_SSL_ROOTS_FILE_PATH`, `use_ssl=true`):
TLS (`p_tls.py`): the relays present a certificate of `DNS:localhost`, or of
`IP:127.0.0.1` for the `ipv4:` cases. "check failed" is `UNAVAILABLE failed to
connect to all addresses; last error: UNKNOWN: <address>: Custom verification
check failed with error: UNAUTHENTICATED: Hostname Verification Check failed.`
| target | pyspark-client 4.3.0 | first commit (eb4f4ca0) | now |
|---|---|---|---|
| `localhost:18620` | count=2 | count=2 | count=2 |
| `127.0.0.1:18620` | check failed | check failed | check failed |
| `127.0.0.1%00:18620` | check failed | count=2 | check failed |
| `localhost%00:18620` | count=2 | count=2 | count=2 |
| `x*y.localhost:18620` | check failed | count=2 | check failed |
| `a/b.localhost:18620` | check failed | count=2 | check failed |
| `[::1%25lo]:18621` | check failed | count=2 | check failed |
| `ipv4:127.0.0.1:18622` | count=2 | count=2 | count=2 |
| `ipv4:127.0.0.1:18622,127.0.0.1:1` | `...127.0.0.1:1: Failed to connect to
remote host: Connection refused` | count=2 | `...127.0.0.1:1: Failed to connect
to remote host: Connection refused` |
| `ipv4:127.0.0.1:1,127.0.0.1:18622` | check failed | count=2 | check failed
|
| `ipv6:[::1]:18621` | check failed | check failed | check failed |
| `unix:/tmp/rv62220/tls.sock` | check failed | count=2 | check failed |
12 of 12 match PySpark now; 5 with the first commit. With a token
(`p_tls_token.py`), `ipv4:127.0.0.2:18626` and
`ipv4:127.0.0.1:1,127.0.0.2:18626`, whose server's certificate is of
`IP:127.0.0.1` only, and `x*y.localhost:18627` are refused by both clients now
(3 of 3), where the first commit sent `Bearer SECRET-ours-1` and `-2` to the
last two.
A builder of its own (`p_own_plain.py`): an `Own(ChannelBuilder)` of `host`,
`port` and a token, whose `toChannel` is an insecure grpcio channel, through
plaintext recording proxies:
| endpoint | pyspark-client 4.3.0 | first commit (eb4f4ca0) | now |
|---|---|---|---|
| `[::1%25lo]:18613` | count=2, no `authorization` sent (its own channel has
no call credentials) | count=2, `Bearer SECRET-ours-0` sent in plaintext | `Tls
handshake failed`, nothing sent |
| `x*y.localhost:18611` | count=2, no `authorization` | count=2, `Bearer
SECRET-ours-1` in plaintext | `Tls handshake failed`, nothing sent |
| `[::1]:18613` | count=2, no `authorization` | `Tls handshake failed` |
`Tls handshake failed`, nothing sent |
| `localhost:18611` | count=2, no `authorization` | count=2, the token over
the loopback | count=2, the token over the loopback |
The transport does not call a builder's own `toChannel`, which the KD row
says, so it cannot connect through that insecure channel; the token now goes in
plaintext to `localhost` alone, never to the host of another authority.
The metadata of each request (`p_headers.py`, `p_reattach.py`): a recording
proxy, in wire order. The `metadata()` of the builder returns `[x-seq,
authorization: Bearer FROM-METADATA, x-dup: 1, x-dup: 2]` and the URL has
`token=TOKEN-own`:
| calls | pyspark-client 4.3.0 | first commit (eb4f4ca0) | now |
|---|---|---|---|
| Config, Interrupt, AddArtifacts, ReleaseSession | `x-seq,
authorization=Bearer FROM-METADATA, x-dup=1, x-dup=2, authorization=Bearer
TOKEN-own` | `authorization=Bearer TOKEN-own, authorization=Bearer
FROM-METADATA, x-seq, x-dup=1`; `x-dup=2` lost | `x-seq, authorization=Bearer
FROM-METADATA, authorization=Bearer TOKEN-own, x-dup=1, x-dup=2` |
| ExecutePlan, ReleaseExecute, ReattachExecute | as above | `x-seq,
authorization=Bearer TOKEN-own, x-dup=1, x-dup=2`; FROM-METADATA gone | as for
Config |
Of the 54 requests that both clients sent, 53 carry the same values of each
key, in the same order, the token last; the other one is the `ExecutePlan` of a
checkpoint's finalizer, which SPARK-62252 fixes (finding 6). The 19 requests of
a reattached execution are the same on the wire.
The `:authority` of the calls (`p_authority.py`):
| endpoint | pyspark-client 4.3.0 | first commit (eb4f4ca0) | now |
|---|---|---|---|
| `localhost:18611`, `dns:///localhost:18611`, `loc%61lhost:18611` |
`localhost:18611` | `localhost:18611` | `localhost:18611` |
| `127.0.0.1:18611?x=1` | `127.0.0.1:18611` | `127.0.0.1:18611` |
`127.0.0.1:18611` |
| `LOCALHOST:18611` | `LOCALHOST:18611` | `localhost:18611` |
`LOCALHOST:18611` |
| `localhost:+18611` | `localhost:+18611` | `localhost:18611` |
`localhost:+18611` |
| `ipv4:127.0.0.1:1,127.0.0.1:18611` | `127.0.0.1:1,127.0.0.1:18611` |
`127.0.0.1:1` | `127.0.0.1:1` (HTTP cannot hold it) |
| `unix:/tmp/rv62220/rec.sock` | `tmp%2Frv62220%2Frec.sock` |
`localhost:443` | `localhost:443` (HTTP cannot hold it) |
The proxy (`p_proxy.py`, `http_proxy` of a CONNECT proxy and
`no_proxy=localhost`): both clients now send `CONNECT [::1%lo]:17841`, `CONNECT
rv62220ab:443` (of `unix-abstract:rv62220ab`) and `CONNECT
[127.0.0.1:1,127.0.0.1:17841]:443`, and neither proxies `localhost:17841`,
`unix:/tmp/rv62220/ok.sock` or `x*y.localhost:17841`; the first commit bypassed
the proxy for all six. The outcomes match, 6 of 6.
What stays different of these, in the KD row, as it cannot be the same here:
- **The `:authority` of a target whose authority HTTP cannot hold.**
`http::uri::Authority::parse` rejects a `%` outside brackets and more than one
`:`, and h2 takes `:authority` from the request's URI alone, so a Unix socket
(`tmp%2Frv62220%2Frec.sock`) and a list of addresses
(`127.0.0.1:1,127.0.0.1:18611`) keep the stand-in (`localhost:443`, the first
address). So grpc-java refuses the headers of grpcio's 3,000-address list
(GOAWAY `Header size exceeded max allowed size (10240)`), where the transport's
calls succeed.
- **The SNI of a name that is not a DNS name.** rustls sends SNI only of a
DNS name (`ServerName::DnsName`), so it sends none of `x*y.localhost` or
`a%2Fb.localhost`, where BoringSSL sends gRPC's host as it is. What is verified
is the same.
- **The order of the metadata of different keys.** The `http::HeaderMap` of
hyper holds the values of a key together, in their order: `x-seq,
authorization, authorization, x-dup, x-dup`, where grpcio sends `x-seq,
authorization, x-dup, x-dup, authorization`. The values of each key, and the
token last among the `authorization`s, are the same; HTTP gives no meaning to
the order of different fields (RFC 9110, section 5.3).
#### What still differs, which SPARK-62252 fixes right after this PR
The other findings of the review, which the KD row lists until then:
- (4) a byte of the endpoint that is not UTF-8, as of a Unix path
(`unix:/tmp/%FF.sock`), is read as U+FFFD, so another socket is used;
- (5) the endpoint is read past a NUL, where grpcio's target, a C string,
ends at it;
- (6) the `ExecutePlan` of a finalizer carries the metadata of the builder's
parameters as the session was made and the token, but none of a `metadata()` of
its own, where PySpark's finalizer calls `metadata()` for it; and the deletion
of a native ML object reads `metadata()` on a thread of the transport
(`Dummy-1`);
- (7) the keys of `metadata()` that gRPC's core sets itself (`te`,
`content-type`, `content-length`, `grpc-timeout`, `grpc-message`,
`grpc-status`) are sent, of which the server fails some calls, and the fields
of a connection that HTTP/2 forbids (`connection`, `upgrade`, ...) are dropped,
where grpcio's call of them fails;
- (10) a refused target longer than about 8 KB fails of
`RESOURCE_EXHAUSTED`, where grpcio's fails of `UNKNOWN`; a key or a value of
None of `metadata()` raises `TypeError`, where grpcio takes it as empty; a
builder that the program gave, of `ChannelBuilder`'s `metadata()`, is read
once, not again after `set()`; an `xds:` target fails of `Misformatted domain
name`, where grpcio's fails to create its xDS client; a full Unix backlog fails
of `EINVAL`, where grpcio's fails of `ENOTCONN`; and a request sent as the
interpreter exits carries none of the metadata of a `metadata()` of its own.
### Why are the changes needed?
So that a channel builder connects to every form of gRPC target as PySpark's
channel does, and fails a target that gRPC refuses with gRPC's status at the
first call. Also, so that a subclass that overrides its properties or
`metadata()` (for example, to refresh an auth header for each call) behaves as
it does in PySpark.
### Does this PR introduce _any_ user-facing change?
Yes. A channel builder's endpoint now:
- connects to `ipv4:`/`ipv6:` address lists in turn, and to
`unix:`/`unix-abstract:` sockets, which raised `NOT_IMPLEMENTED`;
- is percent-decoded, and has its query and fragment dropped
(`127.0.0.1:15002?x=1` connects), where it raised `INVALID_CONNECT_URL`;
- fails its first call with gRPC's status, where it raised when the session
was made.
A builder's `keepalive_*` properties and its `metadata()` are read as
PySpark reads them. A `metadata()` of its own is called for each call, and its
requests carry every value of a key, in its order, and then the token.
The TLS of a target verifies gRPC's name of it, the host of its authority,
and a token goes in plaintext only to a `localhost` of that host; the
`:authority` of the calls and the proxy are gRPC's too. Of a target whose
stand-in host was `localhost` or the first address of a list, a certificate
that grpcio rejects is now rejected, and the token is not sent.
### How was this patch tested?
- **New offline tests** in `python/tests/test_dropin_offline.py`:
- `test_the_endpoint_of_a_channel_builder_is_read_as_grpc_reads_a_target`
covers 24 endpoints that connect and 22 that fail, each with the expected code
and details. It also covers Unix and abstract sockets through a relay, a
missing socket, local credentials over a Unix socket, and that the URL takes
nothing of the endpoint.
- `test_a_channel_builder_is_read_as_pysparks_client_reads_it` covers the
creation reads in 4 cases, the per-call `metadata()` and the metadata the
server receives, the error of `metadata()` as the call's error, and that
`ChannelBuilder`'s own `metadata()` is not called per call.
- Both fail on e1 (e63ca674).
- The e1 tests are updated to the gRPC semantics: `dns://8.8.8.8/...` is
now INTERNAL, so the test uses `dns://127.0.0.1:53/...`, and the
`INVALID_CONNECT_URL` list is removed. The mock server records the metadata of
execute and config calls.
- **New Rust tests** in `spark-connect-core`:
- `grpc_target`: 5 tests, covering about 90 targets with grpcio's
outcomes, the dns authority, the hosts of the connection string, the number
parsers, and gRPC's names of each form (authority, TLS name, proxy host and
`CONNECT` server).
- `connector`: 9 tests, covering address lists, c-ares names, the texts of
an immediate connect failure, Unix and abstract sockets, the status of a
refused target over a real tonic channel, the TLS of gRPC's name of a target
against a certificate of `localhost` and `127.0.0.1` (names that match, a name
of another certificate, `x*y.localhost`, a list, none, and an untrusted chain),
and the tunnel of a proxy.
- `metadata`: 3 tests, covering gRPC core's checks of metadata from a
source, its error as the call's error, and every value of a key in its order
with the token last.
- `channel`: 1 test, that the host of gRPC's authority, never the
stand-in, decides TLS and the token (`[::1%25lo]`, `x*y.localhost`,
`a/b.localhost`, `localhost%00`, a list, a Unix socket, a zone of a LAN
address; `LOCALHOST` and `dns:///localhost` are `localhost`).
- `client`: `the_token_follows_an_authorization_parameter`, which replaces
`token_parameter_wins_over_an_authorization_parameter`: `;authorization=Basic
abc;token=tok` sends both values, the token last, as grpcio and the Scala
client do, so grpc-java's `Metadata.get`, which reads the last value, takes the
token.
- **Probes of both clients** against a Spark 4.3.0 server: the tables above.
Those of the first commit were run on it (eb4f4ca0); the second commit changes
the TLS, the `:authority`, the proxy and the order of the metadata only, which
none of their plaintext cases see, and its own probes are those of the security
round.
- **`cargo test -p apache-spark-connect -p apache-spark-connect-core
--release`**: 1,904 passed, 0 failed, 18 ignored, over 121 test binaries.
- **The Rust and Python gates** (`gates.sh build rust py`), (running; filled
in when it ends):
- `cargo fmt --all --check`;
- `cargo +1.100.0 clippy -p apache-spark-connect-core -p
apache-spark-connect -p pyspark-rs --all-targets -- -D warnings`;
- `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps`;
- `cargo test -p pyspark-rs --lib`: 32 passed;
- ruff 0.14.0, `test_ruff_excludes.py`, `scripts/audit_no_stubs.sh`, the
stub and dispatch generators, the test-list guards and the license checks;
- `test_type_stubs_offline.py`: 15 passed, 4 skipped;
- the offline suite of `coverage.yml`: (running).
- **`scripts/check_vendored_upstream.sh`**: every vendored file matches
Apache Spark v4.3.0 (185 files).
- **`test_dropin_e2e.py`** against a Spark 4.3.0 server: (running).
- `scripts/gen_native_binary_licenses.py` re-records the hash of
`Cargo.lock`, which now lists socket2 for the core. socket2 was already in the
tree, so `LICENSE-binary` and the JNI inventory list no new crate. The second
commit changes no dependency.
### 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]