68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241 | @dataclass(frozen=True, kw_only=True)
class ConnectProfile:
"""Connection to an existing Spark Connect service; no cluster is created."""
endpoint: str
tls: bool = True
token_env: str | None = None
user_id: str | None = None
allow_insecure: bool = False
def __post_init__(self):
if not isinstance(self.endpoint, str) or any(c.isspace() for c in self.endpoint):
raise ConnectError("endpoint must be a Spark Connect URL without whitespace.")
try:
url = urlsplit(self.endpoint)
port = url.port
except ValueError:
raise ConnectError("Invalid Spark Connect endpoint.") from None
if (
# PySpark requires this literal prefix; URL parsing alone also accepts "SC://".
not self.endpoint.startswith("sc://")
or not url.hostname
or url.username is not None
or url.password is not None
or url.path not in {"", "/"}
or url.query
or url.fragment
# Even an empty query or fragment would hide the appended TLS, token and user
# parameters from the client, which then connects in plaintext without credentials.
or any(separator in self.endpoint for separator in ";?#")
or port == 0
):
raise ConnectError("Use sc://host:port without credentials, parameters, query, or path.")
if type(self.tls) is not bool or type(self.allow_insecure) is not bool:
raise ConnectError("tls and allow_insecure must be booleans.")
try:
loopback = ipaddress.ip_address(url.hostname).is_loopback
except ValueError:
loopback = url.hostname.lower() == "localhost"
if not self.tls and not loopback and not self.allow_insecure:
raise ConnectError("Remote endpoints require TLS; use allow_insecure only on a trusted network.")
if self.token_env is not None and (
not isinstance(self.token_env, str) or not re.fullmatch(r"[A-Za-z_][A-Za-z0-9_]*", self.token_env)
):
raise ConnectError("token_env must name an environment variable.")
# PySpark Connect clients use TLS whenever a token is set, except local credentials for localhost.
if self.token_env is not None and not self.tls and url.hostname.lower() != "localhost":
raise ConnectError(
"token_env requires tls = true unless the endpoint host is localhost; "
"Spark Connect clients send tokens only over TLS or to localhost."
)
if self.user_id is not None and (not isinstance(self.user_id, str) or not self.user_id):
raise ConnectError("user_id must be a non-empty string.")
@classmethod
def from_file(cls, path: str | Path, *, profile: str = "default") -> ConnectProfile:
with Path(path).expanduser().open("rb") as stream:
content = stream.read(1024 * 1024 + 1)
if len(content) > 1024 * 1024:
raise ConnectError("Connect profile files must be no larger than 1 MiB.")
try:
document = tomllib.loads(content.decode("utf-8"))
except (UnicodeError, tomllib.TOMLDecodeError):
raise ConnectError("Invalid Connect profile TOML.") from None
if set(document) != {"profiles"} or not isinstance(document["profiles"], dict):
raise ConnectError("Expected a TOML [profiles.NAME] table.")
values = document["profiles"].get(profile)
if not isinstance(values, dict):
raise ConnectError("The selected Connect profile does not exist.")
if set(values) - set(cls.__dataclass_fields__) or "endpoint" not in values:
raise ConnectError("Connect profile has missing or unsupported fields.")
return cls(**values)
def connection_string(self) -> str:
"""Build a private URI for PySpark. This return value may contain credentials."""
url = self.endpoint.rstrip("/") + "/;use_ssl=" + str(self.tls).lower()
if self.token_env:
token = os.environ.get(self.token_env)
if not token:
raise ConnectError("The configured token environment variable is empty or missing.")
url += ";token=" + quote(token, safe="")
if self.user_id:
url += ";user_id=" + quote(self.user_id, safe="")
return url
@contextmanager
def session(self):
"""Create an independent session and attempt release without masking application failure."""
connection = self.connection_string()
try:
from pyspark.sql.connect.session import SparkSession
except ImportError:
raise ConnectClientUnavailable(
"Install spark-on-k8s[connect] on Python 3.11–3.14, or a matching pyspark[connect] client."
) from None
try:
session = SparkSession.builder.remote(connection).create()
except Exception:
raise ConnectError(
"Could not open a Spark Connect session; check endpoint and credentials."
) from None
try:
yield session
except BaseException as original:
try:
session.stop()
except Exception:
original.add_note("Spark Connect session release also failed; server expiry may be required.")
raise
else:
try:
session.stop()
except Exception:
raise ConnectError(
"Spark Connect session release failed; server expiry may be required."
) from None
def probe(self, *, timeout: float = 15, expected_app_id: str | None = None) -> dict:
"""Execute a tiny remote query in a child process with a hard total timeout.
The result reports the server's ``spark_version``, the child's PySpark
``client_version`` (``None`` when unknown) and advisory ``warnings``.
"""
if type(timeout) not in {int, float} or not math.isfinite(timeout) or timeout <= 0:
raise ConnectError("timeout must be a positive finite number of seconds.")
if expected_app_id is not None and (
not isinstance(expected_app_id, str)
or not re.fullmatch(r"[a-z0-9][a-z0-9-]{0,62}", expected_app_id)
):
raise ConnectError("expected_app_id must be a valid Spark application identifier")
request = asdict(self)
if expected_app_id is not None:
request["expected_app_id"] = expected_app_id
# Credentials remain in the environment/stdin, never process arguments or a temporary file.
try:
result = subprocess.run(
# -P keeps the working directory off sys.path. Unlike -I, it keeps PYTHONPATH and the
# user site, so the check imports the installation (and client) the caller imports.
[
sys.executable,
"-P",
*(["-E"] if sys.flags.ignore_environment else []),
*(["-s"] if sys.flags.no_user_site else []),
"-m",
"spark_on_k8s.connect",
],
input=json.dumps(request),
capture_output=True,
text=True,
timeout=timeout,
check=False,
)
except subprocess.TimeoutExpired:
raise TimeoutError("Spark Connect check timed out; the server was not stopped.") from None
if result.returncode:
try:
payload = json.loads(result.stdout)
error = payload["error"]
unavailable = payload.get("code") == _CLIENT_UNAVAILABLE
except (ValueError, KeyError, TypeError, AttributeError):
error, unavailable = _CHECK_ERROR, False
raise (ConnectClientUnavailable if unavailable else ConnectError)(error)
try:
payload = json.loads(result.stdout)
if payload.get("schema_version") != 1 or payload.get("status") != "ok":
raise ValueError
if expected_app_id is not None and payload.get("app_id") != expected_app_id:
raise ValueError
except (ValueError, AttributeError):
raise ConnectError("Spark Connect check returned an invalid response.") from None
payload.setdefault("client_version", None)
# The query succeeded; a version skew is reported without turning it into a failure.
payload["warnings"] = version_warnings(payload["client_version"], payload.get("spark_version"))
return payload
|