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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
|
# $Id$
#
# Copyright (C) 2013--2014 Dragon Research Labs ("DRL")
# Portions copyright (C) 2009--2012 Internet Systems Consortium ("ISC")
# Portions copyright (C) 2007--2008 American Registry for Internet Numbers ("ARIN")
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notices and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND DRL, ISC, AND ARIN DISCLAIM ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DRL,
# ISC, OR ARIN BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""
RPKI CA engine.
"""
import os
import time
import random
import logging
import weakref
import argparse
import urlparse
import tornado.gen
import tornado.web
import tornado.locks
import tornado.ioloop
import tornado.httputil
import tornado.httpclient
import tornado.httpserver
from lxml.etree import Element, SubElement, tostring as ElementToString
import rpki.resource_set
import rpki.up_down
import rpki.left_right
import rpki.x509
import rpki.config
import rpki.exceptions
import rpki.relaxng
import rpki.log
import rpki.daemonize
import rpki.rpkid_tasks
logger = logging.getLogger(__name__)
class main(object):
"""
Main program for rpkid.
"""
def __init__(self):
os.environ.update(TZ = "UTC",
DJANGO_SETTINGS_MODULE = "rpki.django_settings.rpkid")
time.tzset()
self.irdbd_cms_timestamp = None
self.irbe_cms_timestamp = None
self.task_queue = []
self.task_event = tornado.locks.Event()
self.http_client_serialize = weakref.WeakValueDictionary()
parser = argparse.ArgumentParser(description = __doc__)
parser.add_argument("-c", "--config",
help = "override default location of configuration file")
parser.add_argument("-f", "--foreground", action = "store_true",
help = "do not daemonize")
parser.add_argument("--pidfile",
help = "override default location of pid file")
parser.add_argument("--profile",
help = "enable profiling, saving data to PROFILE")
rpki.log.argparse_setup(parser)
args = parser.parse_args()
self.profile = args.profile
rpki.log.init("rpkid", args)
self.cfg = rpki.config.parser(set_filename = args.config, section = "rpkid")
self.cfg.set_global_flags()
if not args.foreground:
rpki.daemonize.daemon(pidfile = args.pidfile)
if self.profile:
import cProfile
prof = cProfile.Profile()
try:
prof.runcall(self.main)
finally:
prof.dump_stats(self.profile)
logger.info("Dumped profile data to %s", self.profile)
else:
self.main()
def main(self):
startup_msg = self.cfg.get("startup-message", "")
if startup_msg:
logger.info(startup_msg)
if self.profile:
logger.info("Running in profile mode with output to %s", self.profile)
logger.debug("Initializing Django")
import django
django.setup()
logger.debug("Initializing rpkidb...")
global rpki # pylint: disable=W0602
import rpki.rpkidb # pylint: disable=W0621
logger.debug("Initializing rpkidb...done")
self.bpki_ta = rpki.x509.X509(Auto_update = self.cfg.get("bpki-ta"))
self.irdb_cert = rpki.x509.X509(Auto_update = self.cfg.get("irdb-cert"))
self.irbe_cert = rpki.x509.X509(Auto_update = self.cfg.get("irbe-cert"))
self.rpkid_cert = rpki.x509.X509(Auto_update = self.cfg.get("rpkid-cert"))
self.rpkid_key = rpki.x509.RSA( Auto_update = self.cfg.get("rpkid-key"))
self.irdb_url = self.cfg.get("irdb-url")
self.http_server_host = self.cfg.get("server-host", "")
self.http_server_port = self.cfg.getint("server-port")
self.http_client_timeout = self.cfg.getint("http-client-timeout", 300)
self.use_internal_cron = self.cfg.getboolean("use-internal-cron", True)
self.initial_delay = random.randint(self.cfg.getint("initial-delay-min", 10),
self.cfg.getint("initial-delay-max", 120))
# Should be much longer in production
self.cron_period = self.cfg.getint("cron-period", 120)
if self.use_internal_cron:
logger.debug("Scheduling initial cron pass in %s seconds", self.initial_delay)
tornado.ioloop.IOLoop.current().spawn_callback(self.cron_loop)
logger.debug("Scheduling task loop")
tornado.ioloop.IOLoop.current().spawn_callback(self.task_loop)
rpkid = self
class LeftRightHandler(tornado.web.RequestHandler): # pylint: disable=W0223
@tornado.gen.coroutine
def post(self):
yield rpkid.left_right_handler(self)
class UpDownHandler(tornado.web.RequestHandler): # pylint: disable=W0223
@tornado.gen.coroutine
def post(self, tenant_handle, child_handle): # pylint: disable=W0221
yield rpkid.up_down_handler(self, tenant_handle, child_handle)
class CronjobHandler(tornado.web.RequestHandler): # pylint: disable=W0223
@tornado.gen.coroutine
def post(self):
yield rpkid.cronjob_handler(self)
application = tornado.web.Application((
(r"/left-right", LeftRightHandler),
(r"/up-down/([-a-zA-Z0-9_]+)/([-a-zA-Z0-9_]+)", UpDownHandler),
(r"/cronjob", CronjobHandler)))
application.listen(
address = self.http_server_host,
port = self.http_server_port)
tornado.ioloop.IOLoop.current().start()
def task_add(self, tasks):
"""
Add zero or more tasks to the task queue.
"""
for task in tasks:
if task in self.task_queue:
logger.debug("Task %r already queued", task)
else:
logger.debug("Adding %r to task queue", task)
self.task_queue.append(task)
def task_run(self):
"""
Kick the task loop to notice recently added tasks.
"""
self.task_event.set()
@tornado.gen.coroutine
def task_loop(self):
"""
Asynchronous infinite loop to run background tasks.
This code is a bit finicky, because it's managing a collection of
Future objects which are running independently of the control flow
here, and the wave function doesn't collapse until we do a yield.
So we keep this brutally simple and don't try to hide too much of
it in the AbstractTask class. For similar reasons, AbstractTask
sets aside a .future instance variable for this method's use.
"""
logger.debug("Starting task loop")
task_event_future = None
while True:
while None in self.task_queue:
self.task_queue.remove(None)
futures = []
for task in self.task_queue:
if task.future is None:
task.future = task.start()
futures.append(task.future)
if task_event_future is None:
task_event_future = self.task_event.wait()
futures.append(task_event_future)
iterator = tornado.gen.WaitIterator(*futures)
while not iterator.done():
yield iterator.next()
if iterator.current_future is task_event_future:
self.task_event.clear()
task_event_future = None
break
else:
task = self.task_queue[iterator.current_index]
task.future = None
waiting = task.waiting()
if not waiting:
self.task_queue[iterator.current_index] = None
for task in self.task_queue:
if task is not None and not task.runnable.is_set():
logger.debug("Reenabling task %r", task)
task.runnable.set()
if waiting:
break
@tornado.gen.coroutine
def cron_loop(self):
"""
Asynchronous infinite loop to drive internal cron cycle.
"""
logger.debug("cron_loop(): Starting")
assert self.use_internal_cron
logger.debug("cron_loop(): Startup delay %d seconds", self.initial_delay)
yield tornado.gen.sleep(self.initial_delay)
while True:
logger.debug("cron_loop(): Running")
yield self.cron_run()
logger.debug("cron_loop(): Sleeping %d seconds", self.cron_period)
yield tornado.gen.sleep(self.cron_period)
@tornado.gen.coroutine
def cron_run(self):
"""
Schedule periodic tasks and wait for them to finish.
"""
now = rpki.sundial.now()
logger.debug("Starting cron run")
try:
tenants = rpki.rpkidb.models.Tenant.objects.all()
except:
logger.exception("Error pulling tenants from SQL, maybe SQL server is down?")
else:
tasks = tuple(task for tenant in tenants for task in tenant.cron_tasks(self))
self.task_add(tasks)
futures = [task.wait() for task in tasks]
self.task_run()
yield futures
logger.info("Finished cron run started at %s", now)
@tornado.gen.coroutine
def cronjob_handler(self, handler):
"""
External trigger to schedule periodic tasks. Obsolete for
produciton use, but portions of the test framework still use this.
"""
if self.use_internal_cron:
handler.set_status(500, "Running cron internally")
else:
logger.debug("Starting externally triggered cron")
yield self.cron_run()
handler.set_status(200)
handler.finish()
@tornado.gen.coroutine
def http_fetch(self, request, serialize_on_full_url = False):
"""
Wrapper around tornado.httpclient.AsyncHTTPClient() which
serializes requests to any particular HTTP server, to avoid
spurious CMS replay errors.
"""
# The current definition of "particular HTTP server" is based only
# on the "netloc" portion of the URL, which could in theory could
# cause deadlocks in a loopback scenario; no such deadlocks have
# shown up in testing, but if such a thing were to occur, it would
# look like an otherwise inexplicable HTTP timeout. The solution,
# should this occur, would be to use the entire URL as the lookup
# key, perhaps only for certain protocols.
#
# The reason for the current scheme is that at least one protocol
# (publication) uses RESTful URLs but has a single service-wide
# CMS replay detection database, which translates to meaning that
# we need to serialize all requests for that service, not just
# requests to a particular URL.
if serialize_on_full_url:
netlock = request.url
else:
netlock = urlparse.urlparse(request.url).netloc
try:
lock = self.http_client_serialize[netlock]
except KeyError:
lock = self.http_client_serialize[netlock] = tornado.locks.Lock()
http_client = tornado.httpclient.AsyncHTTPClient()
with (yield lock.acquire()):
response = yield http_client.fetch(request,
connect_timeout = self.http_client_timeout,
request_timeout = self.http_client_timeout)
raise tornado.gen.Return(response)
@staticmethod
def compose_left_right_query():
"""
Compose top level element of a left-right query to irdbd.
"""
return Element(rpki.left_right.tag_msg, nsmap = rpki.left_right.nsmap,
type = "query", version = rpki.left_right.version)
@tornado.gen.coroutine
def irdb_query(self, q_msg):
"""
Perform an IRDB callback query.
"""
q_tags = set(q_pdu.tag for q_pdu in q_msg)
q_der = rpki.left_right.cms_msg().wrap(q_msg, self.rpkid_key, self.rpkid_cert)
http_request = tornado.httpclient.HTTPRequest(
url = self.irdb_url,
method = "POST",
body = q_der,
headers = { "Content-Type" : rpki.left_right.content_type })
http_response = yield self.http_fetch(http_request)
# Tornado already checked http_response.code for us
content_type = http_response.headers.get("Content-Type")
if content_type not in rpki.left_right.allowed_content_types:
raise rpki.exceptions.BadContentType("HTTP Content-Type %r, expected %r" % (rpki.left_right.content_type, content_type))
r_der = http_response.body
r_cms = rpki.left_right.cms_msg(DER = r_der)
r_msg = r_cms.unwrap((self.bpki_ta, self.irdb_cert))
self.irdbd_cms_timestamp = r_cms.check_replay(self.irdbd_cms_timestamp, self.irdb_url)
#rpki.left_right.check_response(r_msg)
if r_msg.get("type") != "reply" or not all(r_pdu.tag in q_tags for r_pdu in r_msg):
raise rpki.exceptions.BadIRDBReply("Unexpected response to IRDB query: %s" % r_cms.pretty_print_content())
raise tornado.gen.Return(r_msg)
@tornado.gen.coroutine
def irdb_query_child_resources(self, tenant_handle, child_handle):
"""
Ask IRDB about a child's resources.
"""
q_msg = self.compose_left_right_query()
SubElement(q_msg, rpki.left_right.tag_list_resources, tenant_handle = tenant_handle, child_handle = child_handle)
r_msg = yield self.irdb_query(q_msg)
if len(r_msg) != 1:
raise rpki.exceptions.BadIRDBReply("Expected exactly one PDU from IRDB: %s" % r_msg.pretty_print_content())
bag = rpki.resource_set.resource_bag(
asn = rpki.resource_set.resource_set_as(r_msg[0].get("asn")),
v4 = rpki.resource_set.resource_set_ipv4(r_msg[0].get("ipv4")),
v6 = rpki.resource_set.resource_set_ipv6(r_msg[0].get("ipv6")),
valid_until = rpki.sundial.datetime.fromXMLtime(r_msg[0].get("valid_until")))
raise tornado.gen.Return(bag)
@tornado.gen.coroutine
def irdb_query_roa_requests(self, tenant_handle):
"""
Ask IRDB about self's ROA requests.
"""
q_msg = self.compose_left_right_query()
SubElement(q_msg, rpki.left_right.tag_list_roa_requests, tenant_handle = tenant_handle)
r_msg = yield self.irdb_query(q_msg)
raise tornado.gen.Return(r_msg)
@tornado.gen.coroutine
def irdb_query_ghostbuster_requests(self, tenant_handle, parent_handles):
"""
Ask IRDB about self's ghostbuster record requests.
"""
q_msg = self.compose_left_right_query()
for parent_handle in parent_handles:
SubElement(q_msg, rpki.left_right.tag_list_ghostbuster_requests,
tenant_handle = tenant_handle, parent_handle = parent_handle)
r_msg = yield self.irdb_query(q_msg)
raise tornado.gen.Return(r_msg)
@tornado.gen.coroutine
def irdb_query_ee_certificate_requests(self, tenant_handle):
"""
Ask IRDB about self's EE certificate requests.
"""
q_msg = self.compose_left_right_query()
SubElement(q_msg, rpki.left_right.tag_list_ee_certificate_requests, tenant_handle = tenant_handle)
r_msg = yield self.irdb_query(q_msg)
raise tornado.gen.Return(r_msg)
@property
def left_right_models(self):
"""
Map element tag to rpkidb model.
"""
# pylint: disable=W0621,W0201
try:
return self._left_right_models
except AttributeError:
import rpki.rpkidb.models
self._left_right_models = {
rpki.left_right.tag_tenant : rpki.rpkidb.models.Tenant,
rpki.left_right.tag_bsc : rpki.rpkidb.models.BSC,
rpki.left_right.tag_parent : rpki.rpkidb.models.Parent,
rpki.left_right.tag_child : rpki.rpkidb.models.Child,
rpki.left_right.tag_repository : rpki.rpkidb.models.Repository }
return self._left_right_models
@property
def left_right_trivial_handlers(self):
"""
Map element tag to bound handler methods for trivial PDU types.
"""
# pylint: disable=W0201
try:
return self._left_right_trivial_handlers
except AttributeError:
self._left_right_trivial_handlers = {
rpki.left_right.tag_list_published_objects : self.handle_list_published_objects,
rpki.left_right.tag_list_received_resources : self.handle_list_received_resources }
return self._left_right_trivial_handlers
def handle_list_published_objects(self, q_pdu, r_msg):
"""
<list_published_objects/> server.
"""
tenant_handle = q_pdu.get("tenant_handle")
msg_tag = q_pdu.get("tag")
kw = dict(tenant_handle = tenant_handle)
if msg_tag is not None:
kw.update(tag = msg_tag)
for ca_detail in rpki.rpkidb.models.CADetail.objects.filter(ca__parent__tenant__tenant_handle = tenant_handle, state = "active"):
SubElement(r_msg, rpki.left_right.tag_list_published_objects,
uri = ca_detail.crl_uri, **kw).text = ca_detail.latest_crl.get_Base64()
SubElement(r_msg, rpki.left_right.tag_list_published_objects,
uri = ca_detail.manifest_uri, **kw).text = ca_detail.latest_manifest.get_Base64()
for c in ca_detail.child_certs.all():
SubElement(r_msg, rpki.left_right.tag_list_published_objects,
uri = c.uri, child_handle = c.child.child_handle, **kw).text = c.cert.get_Base64()
for r in ca_detail.roas.filter(roa__isnull = False):
SubElement(r_msg, rpki.left_right.tag_list_published_objects,
uri = r.uri, **kw).text = r.roa.get_Base64()
for g in ca_detail.ghostbusters.all():
SubElement(r_msg, rpki.left_right.tag_list_published_objects,
uri = g.uri, **kw).text = g.ghostbuster.get_Base64()
for c in ca_detail.ee_certificates.all():
SubElement(r_msg, rpki.left_right.tag_list_published_objects,
uri = c.uri, **kw).text = c.cert.get_Base64()
def handle_list_received_resources(self, q_pdu, r_msg):
"""
<list_received_resources/> server.
"""
logger.debug(".handle_list_received_resources() %s", ElementToString(q_pdu))
tenant_handle = q_pdu.get("tenant_handle")
msg_tag = q_pdu.get("tag")
for ca_detail in rpki.rpkidb.models.CADetail.objects.filter(ca__parent__tenant__tenant_handle = tenant_handle,
state = "active", latest_ca_cert__isnull = False):
cert = ca_detail.latest_ca_cert
resources = cert.get_3779resources()
r_pdu = SubElement(r_msg, rpki.left_right.tag_list_received_resources,
tenant_handle = tenant_handle,
parent_handle = ca_detail.ca.parent.parent_handle,
uri = ca_detail.ca_cert_uri,
notBefore = str(cert.getNotBefore()),
notAfter = str(cert.getNotAfter()),
sia_uri = cert.get_sia_directory_uri(),
aia_uri = cert.get_aia_uri(),
asn = str(resources.asn),
ipv4 = str(resources.v4),
ipv6 = str(resources.v6))
if msg_tag is not None:
r_pdu.set("tag", msg_tag)
@tornado.gen.coroutine
def left_right_handler(self, handler):
"""
Process one left-right message.
"""
content_type = handler.request.headers["Content-Type"]
if content_type not in rpki.left_right.allowed_content_types:
handler.set_status(415, "No handler for Content-Type %s" % content_type)
handler.finish()
return
handler.set_header("Content-Type", rpki.left_right.content_type)
try:
q_cms = rpki.left_right.cms_msg(DER = handler.request.body)
q_msg = q_cms.unwrap((self.bpki_ta, self.irbe_cert))
r_msg = Element(rpki.left_right.tag_msg, nsmap = rpki.left_right.nsmap,
type = "reply", version = rpki.left_right.version)
self.irbe_cms_timestamp = q_cms.check_replay(self.irbe_cms_timestamp, handler.request.path)
assert q_msg.tag.startswith(rpki.left_right.xmlns)
assert all(q_pdu.tag.startswith(rpki.left_right.xmlns) for q_pdu in q_msg)
if q_msg.get("version") != rpki.left_right.version:
raise rpki.exceptions.BadQuery("Unrecognized protocol version")
if q_msg.get("type") != "query":
raise rpki.exceptions.BadQuery("Message type is not query")
for q_pdu in q_msg:
try:
action = q_pdu.get("action")
model = self.left_right_models.get(q_pdu.tag)
if q_pdu.tag in self.left_right_trivial_handlers:
self.left_right_trivial_handlers[q_pdu.tag](q_pdu, r_msg)
elif action in ("get", "list"):
for obj in model.objects.xml_list(q_pdu):
obj.xml_template.encode(obj, q_pdu, r_msg)
elif action == "destroy":
obj = model.objects.xml_get_for_delete(q_pdu)
yield obj.xml_pre_delete_hook(self)
obj.delete()
obj.xml_template.acknowledge(obj, q_pdu, r_msg)
elif action in ("create", "set"):
obj = model.objects.xml_get_or_create(q_pdu)
obj.xml_template.decode(obj, q_pdu)
obj.xml_pre_save_hook(q_pdu)
obj.save()
yield obj.xml_post_save_hook(self, q_pdu)
obj.xml_template.acknowledge(obj, q_pdu, r_msg)
else:
raise rpki.exceptions.BadQuery("Unrecognized action %r" % action)
except Exception, e:
if not isinstance(e, rpki.exceptions.NotFound):
logger.exception("Unhandled exception serving left-right PDU %r", q_pdu)
error_tenant_handle = q_pdu.get("tenant_handle")
error_tag = q_pdu.get("tag")
r_pdu = SubElement(r_msg, rpki.left_right.tag_report_error, error_code = e.__class__.__name__)
r_pdu.text = str(e)
if error_tag is not None:
r_pdu.set("tag", error_tag)
if error_tenant_handle is not None:
r_pdu.set("tenant_handle", error_tenant_handle)
break
handler.set_status(200)
handler.finish(rpki.left_right.cms_msg().wrap(r_msg, self.rpkid_key, self.rpkid_cert))
except Exception, e:
logger.exception("Unhandled exception serving left-right request")
handler.set_status(500, "Unhandled exception %s: %s" % (e.__class__.__name__, e))
handler.finish()
@tornado.gen.coroutine
def up_down_handler(self, handler, tenant_handle, child_handle):
"""
Process one up-down PDU.
"""
content_type = handler.request.headers["Content-Type"]
if content_type not in rpki.up_down.allowed_content_types:
handler.set_status(415, "No handler for Content-Type %s" % content_type)
handler.finish()
return
try:
child = rpki.rpkidb.models.Child.objects.get(tenant__tenant_handle = tenant_handle, child_handle = child_handle)
q_der = handler.request.body
r_der = yield child.serve_up_down(self, q_der)
handler.set_header("Content-Type", rpki.up_down.content_type)
handler.set_status(200)
handler.finish(r_der)
except rpki.rpkidb.models.Child.DoesNotExist:
logger.info("Child %r of tenant %r not found", child_handle, tenant_handle)
handler.set_status(400, "Child %r not found" % child_handle)
handler.finish()
except Exception, e:
logger.exception("Unhandled exception processing up-down request")
handler.set_status(400, "Could not process PDU: %s" % e)
handler.finish()
class publication_queue(object):
"""
Utility to simplify publication from within rpkid.
General idea here is to accumulate a collection of objects to be
published, in one or more repositories, each potentially with its
own completion callback. Eventually we want to publish everything
we've accumulated, at which point we need to iterate over the
collection and do repository.call_pubd() for each repository.
"""
# At present, ._inplay and .inplay() are debugging tools only. If
# there turns out to be a real race condition here, this might
# evolve into the hook for some kind of Condition()-based
# mechanism.
_inplay = weakref.WeakValueDictionary()
def __init__(self, rpkid):
self.rpkid = rpkid
self.clear()
def clear(self):
self.repositories = {}
self.msgs = {}
self.handlers = {}
self.uris = {}
def inplay(self, uri):
who = self._inplay.get(uri, self)
return who is not self and uri in who.uris
def queue(self, uri, repository, handler = None,
old_obj = None, new_obj = None, old_hash = None):
assert old_obj is not None or new_obj is not None or old_hash is not None
assert old_obj is None or old_hash is None
assert old_obj is None or isinstance(old_obj, rpki.x509.uri_dispatch(uri))
assert new_obj is None or isinstance(new_obj, rpki.x509.uri_dispatch(uri))
logger.debug("Queuing publication action: uri %s, old %r, new %r, hash %s",
uri, old_obj, new_obj, old_hash)
if self.inplay(uri):
logger.warning("%s is already in play", uri)
rid = repository.peer_contact_uri
if rid not in self.repositories:
self.repositories[rid] = repository
self.msgs[rid] = Element(rpki.publication.tag_msg, nsmap = rpki.publication.nsmap,
type = "query", version = rpki.publication.version)
if uri in self.uris:
logger.debug("Removing publication duplicate %r %s hash %s", self.uris[uri], uri, self.uris[uri].get("hash"))
old_pdu = self.uris.pop(uri)
self.msgs[rid].remove(old_pdu)
pdu_hash = old_pdu.get("hash")
if pdu_hash is None and new_obj is None:
logger.debug("Withdrawing object %r which was never published simplifies to no-op", old_pdu)
return
elif old_hash is not None:
logger.debug("Old hash supplied") # XXX Debug log
pdu_hash = old_hash
elif old_obj is None:
logger.debug("No old object present") # XXX Debug log
pdu_hash = None
else:
logger.debug("Calculating hash of old object") # XXX Debug log
pdu_hash = rpki.x509.sha256(old_obj.get_DER()).encode("hex")
logger.debug("uri %s old hash %s new hash %s", uri, pdu_hash, # XXX Debug log
None if new_obj is None else rpki.x509.sha256(new_obj.get_DER()).encode("hex"))
if new_obj is None:
pdu = SubElement(self.msgs[rid], rpki.publication.tag_withdraw, uri = uri, hash = pdu_hash)
else:
pdu = SubElement(self.msgs[rid], rpki.publication.tag_publish, uri = uri)
pdu.text = new_obj.get_Base64()
if pdu_hash is not None:
pdu.set("hash", pdu_hash)
if handler is not None:
self.handlers[uri] = handler
self.uris[uri] = pdu
self._inplay[uri] = self
@tornado.gen.coroutine
def call_pubd(self):
for rid in self.repositories:
logger.debug("Calling pubd[%r]", self.repositories[rid])
yield self.repositories[rid].call_pubd(self.rpkid, self.msgs[rid], self.handlers)
for k in self.uris.iterkeys():
if self._inplay.get(k) is self:
del self._inplay[k]
self.clear()
@property
def size(self):
return sum(len(self.msgs[rid]) for rid in self.repositories)
def empty(self):
return not self.msgs
|