-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathfinderevents.py
339 lines (297 loc) · 9.75 KB
/
pathfinderevents.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
"""Create CloudEvents from REST-API Requests."""
from __future__ import annotations
import json
import logging
import signal
import sys
from typing import TYPE_CHECKING, Any, NoReturn, Self
from urllib.parse import parse_qs
if TYPE_CHECKING:
from collections.abc import Iterable
from wsgiref.types import StartResponse, WSGIEnvironment
import cherrypy # type: ignore[import-untyped]
from cloudevents.http import CloudEvent
from cloudevents.kafka import to_structured
from configargparse import ( # type: ignore[import-untyped]
ArgumentParser,
YAMLConfigFileParser,
)
from kafka import KafkaProducer # type: ignore[import-untyped]
from werkzeug.exceptions import HTTPException
from werkzeug.routing import Map, Rule
from werkzeug.wrappers import Request, Response
_RUNTIME_ERROR_MISSING_PRODUCER = "run_server called before set_producer"
logger = logging.getLogger(__name__)
def from_pathfinder_request(request: Request) -> CloudEvent:
"""Convert a basic pathfinder POST request's data into a proper CloudEvent."""
form = parse_qs(request.get_data(as_text=True))
return CloudEvent(
{
"type": f"ch.rabe.api.events.pathfinder.v0alpha1.{form['event'][0]}",
"source": "https://github.com/radiorabe/pathfinder-cloudevents-service",
"subject": form["channel"][0],
"datacontenttype": "text/plain",
},
form["channel"][0],
)
class ApiServer:
"""The API server."""
def __init__( # noqa: PLR0913
self: Self,
*,
bind_addr: str,
bind_port: int,
realm: str,
topic: str,
username: str,
password: str,
debug: bool = False,
) -> None:
"""Create ApiServer."""
self.producer: KafkaProducer
self.bind_addr: str = bind_addr
self.bind_port: int = bind_port
self.realm = realm
self.topic = topic
self.username = username
self.password = password
self.debug = debug
self.url_map = Map([Rule("/webhook", endpoint="webhook")])
def set_producer(self: Self, producer: KafkaProducer) -> None:
"""Set a producer."""
self.producer = producer
def run_server(self: Self) -> None:
"""Run the API server."""
if not self.producer:
raise RuntimeError(_RUNTIME_ERROR_MISSING_PRODUCER)
if self.debug:
from werkzeug.serving import run_simple
run_simple(
self.bind_addr,
self.bind_port,
self,
use_debugger=True,
use_reloader=True,
)
else: # pragma: no cover
cherrypy.tree.graft(self, "/")
cherrypy.server.unsubscribe()
self._server = cherrypy._cpserver.Server() # noqa: SLF001
self._server.socket_host = self.bind_addr
self._server.socket_port = self.bind_port
self._server.subscribe()
cherrypy.engine.start()
cherrypy.engine.block()
def stop_server(self: Self) -> None:
"""Stop the server."""
self._server.stop()
cherrypy.engine.exit()
def __call__(
self: Self,
environ: WSGIEnvironment,
start_response: StartResponse,
) -> Iterable[bytes]:
"""Forward calls to wsgi_app."""
return self.wsgi_app(environ, start_response)
def wsgi_app(
self: Self,
environ: WSGIEnvironment,
start_response: StartResponse,
) -> Iterable[bytes]:
"""Return a wsgi app."""
request = Request(environ)
auth = request.authorization
if auth and self.check_auth(str(auth.username), str(auth.password)):
response = self.dispatch_request(request)
else:
response = self.auth_required(request)
return response(environ, start_response)
def check_auth(self: Self, username: str, password: str) -> bool:
"""Check plaintext auth.
Pathfinder doesn't support sending any advanced API credentials like JWT or
similar so we resort to the most insecure way possible to authenticate its
requests.
"""
return self.username == username and self.password == password
def auth_required(self: Self, _: Request) -> Response:
"""Return a 401 unauthorized reponse."""
return Response(
"Could not verify your access level for that URL.\n"
"You have to login with proper credentials",
status=401,
headers={"WWW-Authenticate": f'Basic realm="{self.realm}"'},
)
def dispatch_request(self: Self, request: Request) -> Response:
"""Dispatch request and return any errors in response."""
adapter = self.url_map.bind_to_environ(request.environ)
try:
endpoint, values = adapter.match()
return getattr(self, f"on_{endpoint}")(request, **values)
except HTTPException as e:
return Response(
json.dumps(e.description),
e.code,
{"Content-Type": "application/json"},
)
def on_webhook(self: Self, request: Request) -> Response:
"""Receive a Pathfinder RestApi call and produce a CloudEvent."""
def on_send_error(ex: Exception) -> None: # pragma: no cover
logger.error("Failed to send CloudEvent", exc_info=ex)
def _key_mapper(ce: CloudEvent) -> Any | None: # noqa: ANN401
return ".".join(
[
ce.get("type"), # type: ignore[list-item]
ce.get("subject"), # type: ignore[list-item]
],
)
ce = from_pathfinder_request(request)
kafka_msg = to_structured(
ce,
key_mapper=_key_mapper,
)
headers: list[tuple[str, bytes]] | None
if kafka_msg.headers:
headers = list(kafka_msg.headers.items())
self.producer.send(
self.topic,
key=kafka_msg.key,
value=kafka_msg.value,
headers=headers,
).add_errback(on_send_error)
self.producer.flush()
logger.info(
"Forwarded event %s with channel %s",
ce.get("type"),
ce.get("subject"),
)
return Response(
status="200 Event Received",
)
def app( # noqa: PLR0913
api: ApiServer,
bootstrap_servers: list[str],
security_protocol: str,
tls_cafile: str,
tls_certfile: str,
tls_keyfile: str,
) -> None:
"""Set up pathfinder sub, kafka producer & block while processing messages."""
producer = KafkaProducer(
bootstrap_servers=bootstrap_servers,
security_protocol=security_protocol,
retries=5,
max_in_flight_requests_per_connection=1,
key_serializer=lambda k: bytes(k, "utf-8"),
ssl_cafile=tls_cafile,
ssl_certfile=tls_certfile,
ssl_keyfile=tls_keyfile,
)
api.set_producer(producer)
def on_sigint(*_: Any) -> NoReturn: # noqa: ANN401 # pragma: no cover
api.stop_server()
producer.flush()
producer.close()
sys.exit(0)
signal.signal(signal.SIGINT, on_sigint)
api.run_server() # blocking
producer.flush()
producer.close()
def main() -> None: # pragma: no cover
"""CLI entrypoint parses args, sets up logging, and calls `app()`."""
parser = ArgumentParser(
__name__,
config_file_parser_class=YAMLConfigFileParser,
default_config_files=[f"{__name__}.yaml"],
)
parser.add(
"--bind-addr",
default="127.0.0.1",
env_var="APP_BIND_ADDR",
)
parser.add(
"--bind-port",
default=8080,
env_var="APP_BIND_PORT",
)
parser.add(
"--realm",
default="pathfinder",
env_var="APP_REALM",
)
parser.add(
"--username",
default="pathfinder",
env_var="APP_USERNAME",
)
parser.add(
"--password",
required=True,
env_var="APP_PASSWORD",
)
parser.add(
"--kafka-bootstrap-servers",
required=True,
env_var="KAFKA_BOOTSTRAP_SERVERS",
)
parser.add(
"--kafka-security-protocol",
default="PLAINTEXT",
env_var="KAFKA_SECURITY_PROTOCOL",
)
parser.add(
"--kafka-tls-cafile",
default=None,
env_var="KAFKA_TLS_CAFILE",
)
parser.add(
"--kafka-tls-certfile",
default=None,
env_var="KAFKA_TLS_CERTFILE",
)
parser.add(
"--kafka-tls-keyfile",
default=None,
env_var="KAFKA_TLS_KEYFILE",
)
parser.add(
"--kafka-topic",
default="dev.cloudevents",
env_var="KAFKA_TOPIC",
)
parser.add(
"--quiet",
"-q",
default=False,
action="store_true",
env_var="QUIET",
)
parser.add(
"--debug",
default=False,
action="store_true",
env_var="DEBUG",
)
options = parser.parse_args()
if not options.quiet:
logging.basicConfig(level=logging.INFO)
if options.debug:
logging.basicConfig(level=logging.DEBUG)
logger.info("Starting %s", __name__)
app(
api=ApiServer(
bind_addr=options.bind_addr,
bind_port=options.bind_port,
realm=options.realm,
username=options.username,
password=options.password,
topic=options.kafka_topic,
debug=options.debug,
),
bootstrap_servers=options.kafka_bootstrap_servers,
security_protocol=options.kafka_security_protocol,
tls_cafile=options.kafka_tls_cafile,
tls_certfile=options.kafka_tls_certfile,
tls_keyfile=options.kafka_tls_keyfile,
)
if __name__ == "__main__": # pragma: no cover
main()