forked from IdentityPython/pyFF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdx.py
895 lines (759 loc) · 32.3 KB
/
mdx.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
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
"""
An implementation of draft-lajoie-md-query
.. code-block:: bash
Usage: pyffd <options> {pipeline-files}+
-C|--no-cache
Turn off caching
-p <pidfile>
Write a pidfile at the specified location
-f
Run in foreground
-a
Restart pyffd if any of the pipeline files change
--log=<log> | -l<log>
Set to either a file or syslog:<facility> (eg syslog:auth)
--error-log=<log> | --access-log=<log>
As --log but only affects the error or access log streams.
--loglevel=<level>
Set logging level
-P<port>|--port=<port>
Listen on the specified port
-H<host>|--host=<host>
Listen on the specified interface
--frequency=<seconds>
Wake up every <seconds> and run the update pipeline. By
default the frequency is set to 600.
-A<name:uri>|--alias=<name:uri>
Add the mapping 'name: uri' to the toplevel URL alias
table. This causes URLs on the form http://server/<name>/x
to be processed as http://server/metadata/{uri}x. The
default alias table is presented at http://server
--dir=<dir>
Chdir into <dir> after the server starts up.
--proxy
The service is running behind a proxy - respect the X-Forwarded-Host header.
-m <module>|--modules=<module>
Load a module
{pipeline-files}+
One or more pipeline files
"""
from __future__ import absolute_import, print_function, unicode_literals
import importlib
import pkg_resources
from six import StringIO
from six.moves.urllib_parse import urlparse, quote_plus
import getopt
from cherrypy.lib.cpstats import StatsPage
import os
import sys
from threading import Lock
import cherrypy
from cherrypy._cpdispatch import Dispatcher
from cherrypy._cperror import NotFound, HTTPError
from cherrypy.lib import cptools
from cherrypy.process.plugins import Monitor, SimplePlugin
from cherrypy.lib import caching
from simplejson import dumps
from .constants import config
from .locks import ReadWriteLock
from .mdrepo import MDRepository
from .pipes import plumbing
from .utils import resource_string, xslt_transform, dumptree, duration2timedelta, \
debug_observer, render_template, hash_id
from .logs import log, SysLogLibHandler
from .samlmd import entity_simple_summary, entity_display_name, entity_info
import logging
from .stats import stats
from datetime import datetime
from lxml import etree
from . import __version__ as pyff_version
from publicsuffix import PublicSuffixList
from .i18n import language
from . import samlmd
_ = language.ugettext
site_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "site")
class MDUpdate(Monitor):
def __init__(self, bus, frequency=int(config.update_frequency), server=None):
self.lock = Lock()
self.server = server
self.bus = bus
Monitor.__init__(self, bus, lambda: self.run(server), frequency=frequency)
self.subscribe()
self.nruns = 0
def run(self, server):
locked = False
try:
self.lock.acquire()
locked = True
for p in server.plumbings:
state = {'update': True}
p.process(self.server.md, state=state)
self.server.ready = True
except Exception as ex:
log.error(ex.message)
finally:
if locked:
self.lock.release()
def start(self):
self.run(self.server)
super(MDUpdate, self).start()
def stop(self):
super(MDUpdate, self).stop()
start.priority = 80
class DirPlugin(SimplePlugin):
def __init__(self, bus, d=None):
SimplePlugin.__init__(self, bus)
self.dir = d
def start(self):
os.chdir(self.dir)
start.priority = 79
class EncodingDispatcher(object):
"""Cherrypy ass-u-me-s a lot about how requests are processed. In particular it is diffucult to send
something that contains '/' and ':' (like a URL) using the standard dispatchers. This class provides
a workaround by base64-encoding the troubling stuff and sending the result through the normal displatch
pipeline. At the other end base64-encoded data is unpacked.
"""
def __init__(self, prefixes, enc, next_dispatcher=Dispatcher()):
self.prefixes = prefixes
self.enc = enc
self.next_dispatcher = next_dispatcher
def dispatch(self, path_info):
# log.debug("EncodingDispatcher (%s) called with %s" % (",".join(self.prefixes), path_info))
# vpath = path_info.replace("%2F", "/")
vpath = path_info
for prefix in self.prefixes:
if vpath.startswith(prefix):
# log.debug("EncodingDispatcher (%s) called with %s" % (",".join(self.prefixes), path_info))
vpath = path_info.replace("%2F", "/")
plen = len(prefix)
vpath = vpath[plen + 1:]
npath = "%s/%s" % (prefix, self.enc(vpath))
# log.debug("EncodingDispatcher %s" % npath)
return self.next_dispatcher(npath.encode('ascii', errors='ignore'))
return self.next_dispatcher(vpath)
class MDStats(StatsPage):
"""Renders the standard stats page with pyFF style decoration. We use the lxml html parser to locate the
body and replace it with a '<div>'. The result is passed as the content using the 'basic' template.
"""
@cherrypy.expose
def index(self):
h = "".join(super(MDStats, self).index())
parser = etree.HTMLParser()
tree = etree.parse(StringIO(h), parser)
body = tree.getroot().find("body")
body.tag = 'div'
body.set('class', 'cpstats')
for h in body.findall("h1"):
h.tag = 'h3'
for h in body.findall("h2"):
h.tag = 'h4'
for t in body.findall('table'):
t.set('class', 'table table-striped table-bordered table-condensed')
for b in body.findall('button'):
b.set('class', 'btn btn-small')
hstr = etree.tostring(body, pretty_print=True, method="html")
return render_template("ui.html", content=hstr, headline="Statistics")
class WellKnown(object):
"""Implementation of the .well-known URL namespace for pyFF. In particular this contains the webfinger
implementation which returns information about up- and downstream metadata.
"""
def __init__(self, server=None):
self.server = server
@cherrypy.expose
def webfinger(self, resource=None, rel=None):
"""An implementation the webfinger protocol (http://tools.ietf.org/html/draft-ietf-appsawg-webfinger-12)
in order to provide information about up and downstream metadata available at this pyFF instance.
Example:
.. code-block:: bash
# curl http://localhost:8080/.well-known/webfinger?resource=http://localhost:8080
This should result in a JSON structure that looks something like this:
.. code-block:: json
{"expires": "2013-04-13T17:40:42.188549",
"links": [
{"href": "http://reep.refeds.org:8080/role/sp.xml", "rel": "urn:oasis:names:tc:SAML:2.0:metadata"},
{"href": "http://reep.refeds.org:8080/role/sp.json", "rel": "disco-json"}],
"subject": "http://reep.refeds.org:8080"}
Depending on which version of pyFF your're running and the configuration you may also see downstream metadata
listed using the 'role' attribute to the link elements.
"""
if resource is None:
resource = cherrypy.request.base
jrd = dict()
dt = datetime.now() + duration2timedelta("PT1H")
jrd['expires'] = dt.isoformat()
jrd['subject'] = cherrypy.request.base
links = list()
jrd['links'] = links
_dflt_rels = {
'urn:oasis:names:tc:SAML:2.0:metadata': '.xml',
'disco-json': '.json'
}
if rel is None:
rel = _dflt_rels.keys()
else:
rel = [rel]
def _links(url):
if url.startswith('/'):
url = url.lstrip('/')
for r in rel:
suffix = ""
if not url.endswith('/'):
suffix = _dflt_rels[r]
links.append(dict(rel=r,
href='%s/%s%s' % (cherrypy.request.base, url, suffix)))
_links('/entities/')
for a in self.server.md.store.collections():
if '://' not in a:
_links(a)
for entity_id in self.server.md.store.entity_ids():
_links("/metadata/%s" % hash_id(entity_id))
for a in self.server.aliases.keys():
for v in self.server.md.store.attribute(self.server.aliases[a]):
_links('%s/%s' % (a, quote_plus(v)))
cherrypy.response.headers['Content-Type'] = 'application/json'
return dumps(jrd)
class NotImplementedFunction(object):
def __init__(self, message):
self.message = message
def index(self):
return self.message
class SHIBDiscovery(object):
"""
An endpoint designed to provide backwards compatibility with standard shibboleth-based discovery services.
"""
def __init__(self, server=None):
self.server = server
@cherrypy.expose
def DS(self, *args, **kwargs):
kwargs['path'] = "/role/idp.ds"
return self.server.request(**kwargs)
@cherrypy.expose
def WAYF(self, *args, **kwargs):
raise HTTPError(400, _("400 Bad Request - shibboleth WAYF protocol not supported"))
@cherrypy.expose
def default(self, *args, **kwargs):
log.debug("default args: %s, kwargs: %s" % (repr(args), repr(kwargs)))
if len(args) > 0 and args[0] in self.server.aliases:
kwargs['pfx'] = args[0]
if len(args) > 1:
kwargs['path'] = args[1]
return self.server.request(**kwargs)
else:
kwargs['pfx'] = None
kwargs['path'] = "/" + "/".join(args)
return self.server.request(**kwargs)
class MDRoot(object):
"""The root application of pyFF. The root application assembles the MDStats and WellKnown classes with an
MDServer instance.
"""
def __init__(self, server):
self.server = server
self._well_known.server = server
self.discovery.server = server
stats = MDStats()
discovery = SHIBDiscovery()
if config.devel_memory_profile:
try: # pragma: nocover
import dowser
memory = dowser.Root()
except ImportError:
memory = NotImplementedFunction('Memory profiling needs dowser')
_well_known = WellKnown()
static = cherrypy.tools.staticdir.handler("/static", os.path.join(site_dir, "static"))
@cherrypy.expose
def status(self):
status = "loading"
if self.server.ready:
status = "running"
version = pkg_resources.require("pyFF")[0].version
cherrypy.response.headers['Content-Type'] = 'application/json'
return dumps({'status': status, 'version': version})
@cherrypy.expose
def shutdown(self):
cfg = cherrypy.request.app.config['global']
if 'allow_shutdown' in cfg and bool(cfg.get('allow_shutdown')):
from threading import Timer
Timer(3, cherrypy.engine.exit, ()).start()
return "bye ..."
else: # pragma: nocover
raise cherrypy.HTTPError(403, _("Endpoint disabled in configuration"))
@cherrypy.expose
@cherrypy.tools.expires(secs=3600, debug=True)
def robots_txt(self):
"""Returns a robots.txt that disables all robots.
"""
return """
User-agent: *
Disallow: /
"""
@cherrypy.expose()
@cherrypy.tools.expires(secs=3600, debug=True)
def storage(self):
"""
The crossdomain storage hub iframe
"""
entity_id = cherrypy.request.params.get('entity_id')
return render_template("storage.html")
@cherrypy.expose
def favicon_ico(self):
"""Returns the pyff icon (the alchemic symbol for sublimation).
"""
cherrypy.response.headers['Content-Type'] = 'image/x-icon'
return resource_string('favicon.ico', "site/static/icons")
@cherrypy.expose
def entities(self, path=None):
"""Process an MDX request with Content-Type hard-coded to application/xml. Regardless of the suffix
you will get XML back from /entities/...
"""
return self.server.request(path=path, content_type="application/xml")
@cherrypy.expose
def metadata(self, path=None):
"""The main request entry point. Any requests are subject to content negotiation based on Accept headers
and based on file name extension. Requesting /metadata/foo.xml gets you (signed) XML (assuming your pipeline
contains that mode), requesting /metadata/foo.json gets you json, and /metadata/foo.ds gets you a discovery
interface based on the IdPs found in 'foo'. Here 'foo' is any supported lookup expression.
"""
return self.server.request(path=path)
@cherrypy.expose
def about(self):
"""The 'about' page. Contains links to statistics etc.
"""
import pkg_resources # part of setuptools
version = pkg_resources.require("pyFF")[0].version
return render_template("about.html",
version=version,
cversion=cherrypy.__version__,
sysinfo=" ".join(os.uname()),
cmdline=" ".join(sys.argv),
stats=stats,
repo=self.server.md,
plumbings=self.server.plumbings)
@cherrypy.expose
def reset(self):
"""The /reset page clears all local browser settings for the device. After visiting
this page users of the discovery service will see a "new device" page.
"""
return render_template("reset.html")
@cherrypy.expose
def settings(self):
"""The /settings page documents the (non) use of cookies.
"""
return render_template("settings.html")
@cherrypy.expose
def search(self, paged=False, query=None, page=0, page_limit=10, entity_filter=None, related=None):
"""
Search the active set for matching entities.
:param paged: page the result when True
:param query: the string query
:param page: the page to return of the paged result
:param page_limit: the number of result per page
:param entity_filter: an optional filter to apply to the active set before searching
:param related: an optional '+'-separated list of related domain names for prioritizing search results
:return: a JSON-formatted search result
"""
cherrypy.response.headers['Content-Type'] = 'application/json'
if paged:
res, more, total = self.server.md.search(query,
page=int(page),
page_limit=int(page_limit),
entity_filter=entity_filter,
related=related)
return dumps({'entities': res, 'more': more, 'total': total})
else:
return dumps(self.server.md.search(query, entity_filter=entity_filter, related=related))
@cherrypy.expose
def index(self):
"""Alias for /metadata
"""
return self.server.request()
@cherrypy.expose
def default(self, *args, **kwargs):
"""The default request processor unpacks base64-encoded reuqests and passes them onto the MDServer.request
handler.
"""
# log.debug("ROOT default args: %s, kwargs: %s" % (repr(args), repr(kwargs)))
if len(args) > 0 and args[0] in self.server.aliases:
kwargs['pfx'] = args[0]
if len(args) > 1:
kwargs['path'] = args[1]
return self.server.request(**kwargs)
else:
kwargs['pfx'] = None
kwargs['path'] = "/" + "/".join(args)
return self.server.request(**kwargs)
class MDServer(object):
"""The MDServer class is the business logic of pyFF. This class is isolated from the request-decoding logic
of MDRoot and from the ancilliary classes like MDStats and WellKnown.
"""
def __init__(self, pipes=None, observers=None):
if not observers:
observers = []
if not pipes:
pipes = []
self._pipes = pipes
self.lock = ReadWriteLock()
self.plumbings = [plumbing(v) for v in pipes]
self.refresh = MDUpdate(cherrypy.engine, server=self, frequency=config.update_frequency)
self.refresh.subscribe()
self.aliases = config.aliases
self.psl = PublicSuffixList()
self.md = MDRepository()
self.ready = False
if config.autoreload:
for f in pipes:
cherrypy.engine.autoreload.files.add(f)
def reload_pipeline(self):
new_plumbings = [plumbing(v) for v in self._pipes]
self.plumbings = new_plumbings
class MediaAccept(object):
def __init__(self):
pass
def has_key(self, key):
return True
def get(self, item):
return self.__getitem__(item)
def __getitem__(self, item):
try:
return cptools.accept(item, debug=True)
except HTTPError:
return False
def request(self, **kwargs):
"""The main request processor. This code implements all rendering of metadata.
"""
stats['MD Requests'] += 1
if not self.ready:
raise HTTPError(503, _("Service Unavailable (repository loading)"))
pfx = kwargs.get('pfx', None)
path = kwargs.get('path', None)
content_type = kwargs.get('content_type', None)
# log.debug("MDServer pfx=%s, path=%s, content_type=%s" % (pfx, path, content_type))
def _d(x, do_split=True):
if x is not None:
x = x.strip()
# log.debug("_d(%s,%s)" % (x, do_split))
if x is None or len(x) == 0:
return None, None
if x.startswith("{base64}"):
x = x[8:].decode('base64')
if do_split and '.' in x:
(pth, dot, extn) = x.rpartition('.')
assert (dot == '.')
if extn in _ctypes:
return pth, extn
return x, None
_ctypes = {'xml': 'application/xml',
'json': 'application/json',
'htm': 'text/html',
'html': 'text/html',
'ds': 'text/html',
's': 'application/json'}
alias = None
if pfx:
alias = pfx
pfx = self.aliases.get(alias, None)
if pfx is None:
raise NotFound()
path, ext = _d(path, content_type is None)
if pfx and path:
q = "{%s}%s" % (pfx, path)
path = "/%s/%s" % (alias, path)
else:
q = path
if ext is not None:
log.debug("request path: %s.%s, headers: %s" % (path, ext, cherrypy.request.headers))
else:
log.debug("request path: %s, headers: %s" % (path, cherrypy.request.headers))
accept = {}
if content_type is None:
if ext is not None and ext in _ctypes:
accept = {_ctypes[ext]: True}
else:
accept = MDServer.MediaAccept()
if ext is not None:
path = "%s.%s" % (path, ext)
else:
accept = {content_type: True}
with self.lock.readlock:
if ext == 'ds':
pdict = dict()
entity_id = kwargs.get('entityID', None)
if entity_id is None:
raise HTTPError(400, _("400 Bad Request - missing entityID"))
e = self.md.store.lookup(entity_id)
if e is None or len(e) == 0:
raise HTTPError(404)
if len(e) > 1:
raise HTTPError(400, _("Bad Request - multiple matches for") + " %s" % entity_id)
pdict['entity'] = entity_simple_summary(e[0])
if not path:
pdict['search'] = "/search/"
pdict['list'] = "/role/idp.json"
else:
pdict['search'] = u"{}.s".format(path)
pdict['list'] = u"{}.json".format(path)
pdict['storage'] = "/storage/"
cherrypy.response.headers['Content-Type'] = 'text/html'
return render_template(config.ds_template, **pdict)
elif ext == 's':
paged = bool(kwargs.get('paged', False))
query = kwargs.get('query', None)
page = kwargs.get('page', 0)
page_limit = kwargs.get('page_limit', 10)
entity_filter = kwargs.get('entity_filter', None)
related = kwargs.get('related', None)
cherrypy.response.headers['Content-Type'] = 'application/json'
if query is None:
log.debug("empty query - creating one")
query = [cherrypy.request.remote.ip]
# XXX fix this - urlparse is not 3.x and also this way to handle extra info sucks
referrer = cherrypy.request.headers.get('referrer', None)
if referrer is not None:
log.debug("including referrer: %s" % referrer)
url = urlparse.urlparse(referrer)
host = url.netloc
if ':' in url.netloc:
(host, port) = url.netloc.split(':')
for host_part in host.rstrip(self.psl.get_public_suffix(host)).split('.'):
if host_part is not None and len(host_part) > 0:
query.append(host_part)
log.debug("created query: %s" % ",".join(query))
if paged:
res, more, total = self.md.search(query,
path=q,
page=int(page),
page_limit=int(page_limit),
entity_filter=entity_filter,
related=related)
# log.debug(dumps({'entities': res, 'more': more, 'total': total}))
return dumps({'entities': res, 'more': more, 'total': total})
else:
return dumps(self.md.search(query,
path=q,
entity_filter=entity_filter,
related=related))
elif accept.get('text/html'):
if not q:
if pfx:
title = pfx
else:
title = _("Metadata By Attributes")
return render_template("index.html",
md=self.md,
samlmd=samlmd,
alias=alias,
aliases=self.aliases,
title=title)
else:
entities = self.md.lookup(q)
if not entities:
raise NotFound()
if len(entities) > 1:
return render_template("metadata.html",
md=self.md,
samlmd=samlmd,
subheading=q,
entities=entities)
else:
entity = entities[0]
return render_template("entity.html",
headline=entity_display_name(entity),
subheading=entity.get('entityID'),
entity_id=entity.get('entityID'),
samlmd=samlmd,
entity=entity_info(entity))
else:
for p in self.plumbings:
state = {'request': True,
'headers': {'Content-Type': 'text/xml'},
'accept': accept,
'url': cherrypy.url(relative=False),
'select': q,
'path': path,
'stats': {}}
r = p.process(self.md, state=state)
if r is not None:
cache_ttl = state.get('cache', 0)
log.debug("caching for %d seconds" % cache_ttl)
for k, v in state.get('headers', {}).iteritems():
cherrypy.response.headers[k] = v
caching.expires(secs=cache_ttl)
return r
raise NotFound()
def main():
"""
The main entrypoint for the pyffd command.
"""
try:
opts, args = getopt.getopt(sys.argv[1:],
'hP:p:H:CfaA:l:Rm:',
['help', 'loglevel=', 'log=', 'access-log=', 'error-log=',
'port=', 'host=', 'no-caching', 'autoreload', 'frequency=', 'modules=',
'alias=', 'dir=', 'version', 'proxy', 'allow_shutdown'])
except getopt.error as msg:
print(msg)
print(__doc__)
sys.exit(2)
if config.loglevel is None:
config.loglevel = logging.INFO
if config.aliases is None:
config.aliases = dict()
if config.modules is None:
config.modules = []
try: # pragma: nocover
for o, a in opts:
if o in ('-h', '--help'):
print(__doc__)
sys.exit(0)
elif o == '--loglevel':
config.loglevel = getattr(logging, a.upper(), None)
if not isinstance(config.loglevel, int):
raise ValueError('Invalid log level: %s' % config.loglevel)
elif o in ('--log', '-l'):
config.error_log = a
config.access_log = a
elif o in '--error-log':
config.error_log = a
elif o in '--access-log':
config.access_log = a
elif o in ('--host', '-H'):
config.bind_address = a
elif o in ('--port', '-P'):
config.port = int(a)
elif o in ('--pidfile', '-p'):
config.pid_file = a
elif o in ('--no-caching', '-C'):
config.caching_enabled = False
elif o in ('--caching-delay', 'D'):
config.caching_delay = int(o)
elif o in ('--foreground', '-f'):
config.daemonize = False
elif o in ('--autoreload', '-a'):
config.autoreload = True
elif o in '--frequency':
config.frequency = int(a)
elif o in ('-A', '--alias'):
(a, colon, uri) = a.partition(':')
assert (colon == ':')
if a and uri:
config.aliases[a] = uri
elif o in '--dir':
config.base_dir = a
elif o in '--proxy':
config.proxy = True
elif o in '--allow_shutdown':
config.allow_shutdown = True
elif o in ('-m', '--module'):
config.modules.append(a)
elif o in '--version':
print("pyffd version %s (cherrypy version %s)" % (pyff_version, cherrypy.__version__))
sys.exit(0)
else:
raise ValueError("Unknown option '%s'" % o)
except Exception as ex:
print(ex)
print(__doc__)
sys.exit(3)
engine = cherrypy.engine
plugins = cherrypy.process.plugins
if config.daemonize:
cherrypy.config.update({'environment': 'production'})
cherrypy.config.update({'log.screen': False})
if config.error_log is None:
config.error_log = 'syslog:daemon'
if config.access_log is None:
config.access_log = 'syslog:daemon'
plugins.Daemonizer(engine).subscribe()
if config.base_dir is not None:
DirPlugin(engine, config.base_dir).subscribe()
if config.pid_file:
plugins.PIDFile(engine, config.pid_file).subscribe()
def _b64(p):
if p:
return "{base64}%s" % p.encode('base64')
else:
return ""
def error_page(code, **kwargs):
return render_template("%d.html" % code, **kwargs)
observers = []
if config.loglevel == logging.DEBUG:
observers.append(debug_observer)
config.modules.append('pyff.builtins')
for mn in config.modules:
importlib.import_module(mn)
server = MDServer(pipes=args, observers=observers)
pfx = ["/entities", "/metadata"] + ["/" + x for x in server.aliases.keys()]
cfg = {
'global': {
'tools.encode.encoding': 'UTF-8',
'server.socket_port': config.port,
'server.socket_host': config.bind_address,
'tools.caching.on': config.caching_enabled,
'tools.caching.debug': config.caching_enabled,
'tools.trailing_slash.on': True,
'tools.caching.maxobj_size': 1000000000000, # effectively infinite
'tools.caching.maxsize': 1000000000000,
'tools.caching.antistampede_timeout': 30,
'tools.caching.delay': 3600, # this is how long we keep static stuff
'tools.cpstats.on': True,
'checker.on': False,
'log.screen': True,
'tools.proxy.on': config.proxy,
'allow_shutdown': config.allow_shutdown,
'error_page.404': lambda **kwargs: error_page(404, _=_, **kwargs),
'error_page.503': lambda **kwargs: error_page(503, _=_, **kwargs),
'error_page.500': lambda **kwargs: error_page(500, _=_, **kwargs),
'error_page.400': lambda **kwargs: error_page(400, _=_, **kwargs)
},
'/': {
'tools.caching.delay': config.caching_delay,
'tools.cpstats.on': True,
'tools.proxy.on': config.proxy,
'request.dispatch': EncodingDispatcher(pfx, _b64).dispatch,
'request.dispatpch.debug': True,
},
'/static': {
'tools.cpstats.on': True,
'tools.caching.on': config.caching_enabled,
'tools.caching.delay': config.caching_delay,
'tools.proxy.on': config.proxy
},
'/shutdown': {
'allow_shutdown': config.allow_shutdown
}
}
cherrypy.config.update(cfg)
if config.error_log is not None:
cherrypy.config.update({'log.screen': False})
root = MDRoot(server)
app = cherrypy.tree.mount(root, config=cfg)
app.log.error_log.setLevel(config.loglevel)
log_args = {'level': config.loglevel}
if config.error_log is not None:
log_args['filename'] = config.error_log
logging.basicConfig(**log_args)
if config.error_log is not None:
if config.error_log.startswith('syslog:'):
facility = config.error_log[7:]
h = SysLogLibHandler(facility=facility)
app.log.error_log.addHandler(h)
cherrypy.config.update({'log.error_file': ''})
else:
cherrypy.config.update({'log.error_file': config.error_log})
if config.access_log is not None:
if config.access_log.startswith('syslog:'):
facility = config.error_log[7:]
h = SysLogLibHandler(facility=facility)
app.log.access_log.addHandler(h)
cherrypy.config.update({'log.access_file': ''})
else:
cherrypy.config.update({'log.access_file': config.access_log})
engine.signals.subscribe()
try:
engine.start()
except Exception as ex:
logging.error(ex)
sys.exit(1)
else:
engine.block()
if __name__ == "__main__":
main()