26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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 | class RemoteClient:
"""Bounded, authenticated requests without local Kubernetes credentials or automatic retries."""
def __init__(self, api_url: str, token: str, *, timeout: float = 30):
url = urlsplit(api_url)
try:
loopback = ipaddress.ip_address(url.hostname or "").is_loopback
except ValueError:
loopback = url.hostname == "localhost"
if (
url.scheme not in {"https", "http"}
or not url.hostname
or url.username is not None
or url.password is not None
or url.query
or url.fragment
or url.scheme == "http"
and not loopback
):
raise ValueError(
"Use an HTTPS API URL without credentials, query or fragment; "
"HTTP is allowed only on loopback."
)
if not isinstance(token, str) or not token or any(ord(c) < 33 or ord(c) > 126 for c in token):
raise ValueError("A nonempty printable ASCII API token without spaces is required.")
if not math.isfinite(timeout) or not 0 < timeout <= 300:
raise ValueError("Remote timeout must be greater than zero and at most 300 seconds.")
self._url, self._token, self.timeout = url, token, timeout
def request(self, operation: str, document: dict) -> dict:
if operation not in {"plan", "submit"}:
raise ValueError("Unsupported remote operation.")
payload = json.dumps(document, allow_nan=False).encode("utf-8")
if len(payload) > 1024 * 1024:
raise ValueError("Remote job documents cannot exceed 1 MiB.")
connection_type = (
http.client.HTTPSConnection if self._url.scheme == "https" else http.client.HTTPConnection
)
kwargs = {"context": ssl.create_default_context()} if self._url.scheme == "https" else {}
# An explicit port stops http.client from parsing the tail of a bare IPv6 address as one.
port = connection_type.default_port if self._url.port is None else self._url.port
connection = connection_type(self._url.hostname, port, timeout=self.timeout, **kwargs)
deadline = time.monotonic() + self.timeout
timer = None
expired = threading.Event()
def invalid(message, status):
# Only a client-error status shows the request was rejected; anything else may follow creation.
if operation == "submit" and not 400 <= status < 500:
message += " " + UNCERTAIN_SUBMISSION
return RemoteError("invalid_response", message, status=status)
try:
connection.connect()
transport = connection.sock
def interrupt():
expired.set()
try:
transport.shutdown(socket.SHUT_RDWR)
except OSError:
pass
remaining = deadline - time.monotonic()
if remaining <= 0:
raise TimeoutError
# Shut down the actual socket, including HTTP/1.0 bodies after HTTPConnection detaches it.
timer = threading.Timer(remaining, interrupt)
timer.daemon = True
timer.start()
connection.request(
"POST",
self._url.path.rstrip("/") + "/jobs/v1/" + operation,
body=payload,
headers={
"Authorization": "Bearer " + self._token,
"Content-Type": "application/json",
"Accept": "application/json",
},
)
response = connection.getresponse()
if 300 <= response.status < 400:
raise RemoteError(
"redirect_refused",
"The API redirected the request; redirects are not followed.",
status=response.status,
)
data = bytearray()
while chunk := response.read1(65536):
data.extend(chunk)
if len(data) > MAX_RESPONSE_BYTES:
raise invalid("The API response exceeded the size limit.", response.status)
if expired.is_set() or time.monotonic() >= deadline:
raise TimeoutError
try:
result = json.loads(data)
if (
not isinstance(result, dict)
or type(result.get("schema_version")) is not int
or result.get("schema_version") != 1
or "error" not in result
):
raise ValueError
except (ValueError, UnicodeError) as error:
raise invalid("The API did not return a supported JSON result.", response.status) from error
if response.status >= 400:
error = result.get("error")
if not isinstance(error, dict) or not all(
isinstance(error.get(k), str) for k in ("code", "message")
):
raise invalid("The API returned an invalid error response.", response.status)
raise RemoteError(error["code"], error["message"], status=response.status)
if (
response.status != (200 if operation == "plan" else 201)
or result.get("operation") != operation
or result["error"] is not None
):
raise invalid("The API returned an unexpected operation result.", response.status)
if operation == "submit":
run = result.get("run")
if not isinstance(run, dict) or not all(
isinstance(run.get(k), str) and run[k] for k in ("namespace", "app_id", "driver_pod")
):
raise invalid("The API returned an invalid run receipt.", response.status)
elif not isinstance(result.get("plan"), dict):
raise invalid("The API returned an invalid plan.", response.status)
return result
except (TimeoutError, OSError, http.client.HTTPException) as error:
if expired.is_set() or isinstance(error, TimeoutError):
raise RemoteError("timeout", "Remote request timed out. " + UNCERTAIN_SUBMISSION) from error
raise RemoteError(
"connection_error", "The remote API connection failed. " + UNCERTAIN_SUBMISSION
) from error
finally:
if timer is not None:
timer.cancel()
timer.join()
connection.close()
|