-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathserver.py
957 lines (825 loc) · 33.7 KB
/
server.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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
import functools
import importlib
import json
try:
from re import Pattern
except ImportError:
from typing.re import Pattern
from typing import Callable, ClassVar, NamedTuple, Optional, Tuple, Type
import structlog
from flask import Flask, Response, request
from werkzeug.exceptions import (Forbidden, MethodNotAllowed, NotAcceptable,
NotFound, RequestEntityTooLarge, Unauthorized,
UnsupportedMediaType)
from opentaxii.persistence.exceptions import (DoesNotExistError,
NoReadNoWritePermission,
NoReadPermission,
NoWritePermission)
from opentaxii.taxii2.utils import taxii2_datetimeformat
from opentaxii.taxii2.validation import (validate_delete_filter_params,
validate_envelope,
validate_list_filter_params,
validate_object_filter_params,
validate_versions_filter_params)
from opentaxii.utils import register_handler
from .auth import AuthManager
from .config import ServerConfig
from .entities import Account
from .exceptions import UnauthorizedException
from .local import context
from .persistence import (BasePersistenceManager, Taxii1PersistenceManager,
Taxii2PersistenceManager)
from .taxii2.http import make_taxii2_response
from .taxii.bindings import (ALL_PROTOCOL_BINDINGS, MESSAGE_BINDINGS,
SERVICE_BINDINGS)
from .taxii.exceptions import (FailureStatus, StatusMessageException,
raise_failure)
from .taxii.http import (HTTP_ALLOW, HTTP_X_TAXII_CONTENT_TYPES,
get_content_type, get_http_headers,
make_taxii_response, validate_request_headers,
validate_request_headers_post_parse,
validate_response_headers)
from .taxii.services import (CollectionManagementService, DiscoveryService,
InboxService, PollService)
from .taxii.services.abstract import TAXIIService
from .taxii.status import process_status_exception
from .taxii.utils import configure_libtaxii_xml_parser, parse_message
from .utils import get_path_and_address, initialize_api
log = structlog.get_logger(__name__)
anonymous_full_access = Account(id=None, username=None, permissions={}, is_admin=True)
class BaseTAXIIServer:
"""
Base class for common functionality in taxii* servers.
"""
PERSISTENCE_MANAGER_CLASS: ClassVar[Type[BasePersistenceManager]]
ENDPOINT_MAPPING: Tuple[(Pattern, Callable[[], Response])]
app: Flask
config: dict
def __init__(self, config: dict):
self.config = config
self.persistence = self.PERSISTENCE_MANAGER_CLASS(
server=self, api=initialize_api(config["persistence_api"])
)
self.setup_endpoint_mapping()
def setup_endpoint_mapping(self):
mapping = []
for attr_name in self.__dir__():
attr = getattr(self, attr_name)
if hasattr(attr, "registered_url_re"):
mapping.append((attr.registered_url_re, attr))
if mapping:
self.ENDPOINT_MAPPING = tuple(mapping)
def init_app(self, app: Flask):
"""Connect server and persistence to flask."""
self.app = app
self.persistence.api.init_app(app)
def get_domain(self, service_id):
"""Get domain either from request handler or config."""
dynamic_domain = self.persistence.get_domain(service_id)
domain = dynamic_domain or self.config.get("domain")
return domain
def get_endpoint(self, relative_path: str) -> Optional[Callable[[], Response]]:
"""Get first endpoint matching relative_path."""
return
def handle_internal_error(self, error):
"""
Handle internal error and return appropriate response.
Placeholder for subclasses to implement.
"""
return
def handle_status_exception(self, error):
"""
Handle status exception and return appropriate response.
Placeholder for subclasses to implement.
"""
return
def handle_http_exception(self, error):
return error.get_response()
def handle_validation_exception(self, error):
"""
Handle validation exception and return appropriate response.
Placeholder for subclasses to implement.
"""
return
def raise_unauthorized(self):
"""
Handle unauthorized access.
Placeholder for subclasses to implement.
"""
raise UnauthorizedException()
class TAXII1Server(BaseTAXIIServer):
"""TAXII1 Server class.
This class keeps Presistence API manager instance for TAXII1
and creates TAXII1 Service instances on request.
:param dict config:
OpenTAXII1 server configuration
"""
TYPE_TO_SERVICE = {
"inbox": InboxService,
"discovery": DiscoveryService,
"collection_management": CollectionManagementService,
"poll": PollService,
}
PERSISTENCE_MANAGER_CLASS = Taxii1PersistenceManager
def __init__(self, config: dict):
super().__init__(config)
signal_hooks = config["hooks"]
if signal_hooks:
importlib.import_module(signal_hooks)
log.info("signal_hooks.imported", hooks=signal_hooks)
configure_libtaxii_xml_parser(config["xml_parser_supports_huge_tree"])
log.info("opentaxii.server_configured")
def _create_services(self, service_entities):
"""Create :class:`TAXIIService` instances from `service_entities`"""
discovery_services = []
services = []
for entity in service_entities:
_props = dict(entity.properties)
_props["server"] = self
_props["path"], _props["address"] = get_path_and_address(
self.get_domain(entity.id), _props["address"]
)
advertised = _props.pop("advertised_services", None)
if entity.type not in self.TYPE_TO_SERVICE:
raise ValueError('Unknown service type "%s"' % entity.type)
service = self.TYPE_TO_SERVICE[entity.type](id=entity.id, **_props)
services.append(service)
if advertised:
discovery_services.append((service, advertised))
for service, advertised in discovery_services:
service.set_advertised_services([s for s in services if s.id in advertised])
return services
def check_allowed_methods(self):
valid_methods = ["POST", "OPTIONS"]
if request.method not in valid_methods:
raise MethodNotAllowed(valid_methods=valid_methods)
def get_endpoint(self, relative_path: str) -> Optional[Callable[[], Response]]:
"""Get first endpoint matching relative_path."""
for endpoint in self.get_services():
if endpoint.path == relative_path:
return functools.partial(self.handle_request, endpoint=endpoint)
def get_services(self, service_ids=None):
"""Get services registered with this TAXII server instance.
:param list service_ids: list of service IDs (as strings)
:return: list of services
:rtype: list of
:py:class:`opentaxii.taxii.services.abstract.TAXIIService`
"""
# early return for filtering by empty list of ids
if service_ids is not None and not service_ids:
return []
service_entities = self.persistence.get_services()
# Services needs to be created all at once to ensure that
# discovery services list all active advertised services
services = self._create_services(service_entities)
if service_ids:
services = [service for service in services if service.id in service_ids]
return services
def get_service(self, id):
"""Get service by ID.
:param str id: service ID
:return: service with specified ID or None
:rtype: :py:class:`opentaxii.taxii.services.abstract.TAXIIService`
"""
services = self.get_services([id])
if services:
return services[0]
def get_services_for_collection(self, collection, service_type):
"""Get list of services with type ``service_type``, attached
to collection ``collection``.
:param `opentaxii.taxii.entities.CollectionEntity` collection:
collection in question
:param str service_type: service type, supported values are
listed as keys in :py:attr:`TYPE_TO_SERVICE`
:return: list of services
:rtype: list of :py:class:`opentaxii.taxii.services.abstract.TAXIIService` # noqa
"""
if service_type not in self.TYPE_TO_SERVICE:
raise ValueError("Wrong service type: %s" % service_type)
for_collection = self.persistence.get_services_for_collection(collection)
ids_for_type = [e.id for e in for_collection if e.type == service_type]
# Sync services for collection with registered services for this server
return self.get_services(ids_for_type)
def handle_request(self, endpoint: TAXIIService):
"""
Handle request and return appropriate response.
Process :class:`TAXIIService` with either :meth:`_process_with_service` or :meth:`_process_options_request`.
"""
self.check_allowed_methods()
if endpoint.authentication_required and context.account is None:
raise UnauthorizedException(
status_type=self.config["unauthorized_status"],
)
if not endpoint.authentication_required:
# if endpoint is not protected, full access
context.account = anonymous_full_access
if not endpoint.available:
raise_failure("The service is not available")
if request.method == "POST":
return self._process_with_service(endpoint)
if request.method == "OPTIONS":
return self._process_options_request(endpoint)
@staticmethod
def _process_options_request(service):
"""
OPTIONS request, return appropriate allow and content types headers.
"""
message_bindings = ",".join(service.supported_message_bindings or [])
return (
"",
200,
{HTTP_ALLOW: "POST, OPTIONS", HTTP_X_TAXII_CONTENT_TYPES: message_bindings},
)
@classmethod
def _process_with_service(cls, service) -> Response:
"""
POST request, return requested data.
"""
if "application/xml" not in request.accept_mimetypes:
raise_failure(
"The specified values of Accept is not supported: {}".format(
", ".join((request.accept_mimetypes or []))
)
)
validate_request_headers(request.headers, MESSAGE_BINDINGS)
taxii_message = parse_message(get_content_type(request.headers), request.data)
try:
validate_request_headers_post_parse(
request.headers,
supported_message_bindings=MESSAGE_BINDINGS,
service_bindings=SERVICE_BINDINGS,
protocol_bindings=ALL_PROTOCOL_BINDINGS,
)
except StatusMessageException as e:
e.in_response_to = taxii_message.message_id
raise e
response_message = service.process(request.headers, taxii_message)
response_headers = get_http_headers(response_message.version, request.is_secure)
validate_response_headers(response_headers)
taxii_xml = response_message.to_xml(pretty_print=True)
return make_taxii_response(taxii_xml, response_headers)
def handle_internal_error(self, error):
"""
Handle internal error and return appropriate response.
"""
log.error("Internal error", exc_info=True)
if "application/xml" not in request.accept_mimetypes:
return "Unacceptable", 406
if self.config["return_server_error_details"]:
message = "Server error occurred: {}".format(error)
else:
message = "Server error occurred"
new_error = FailureStatus(message, e=error)
xml, headers = process_status_exception(
new_error, request.headers, request.is_secure
)
return make_taxii_response(xml, headers)
def handle_status_exception(self, error):
"""
Handle status exception and return appropriate response.
"""
log.warning("Status exception", exc_info=True)
if "application/xml" not in request.accept_mimetypes:
return "Unacceptable", 406
xml, headers = process_status_exception(
error, request.headers, request.is_secure
)
return make_taxii_response(xml, headers)
def raise_unauthorized(self):
"""
Handle unauthorized access.
"""
raise UnauthorizedException(
status_type=self.config["unauthorized_status"],
)
class TAXII2Server(BaseTAXIIServer):
"""
TAXII2 server class.
Stub, implementation pending.
"""
PERSISTENCE_MANAGER_CLASS = Taxii2PersistenceManager
def handle_http_exception(self, error):
"""Return JSON instead of HTML for HTTP errors."""
# start with the correct headers and status code from the error
response = error.get_response()
# replace the body with JSON
response.data = json.dumps(
{
"code": error.code,
"name": error.name,
"description": error.description,
}
)
response.content_type = "application/taxii+json;version=2.1"
return response
def handle_validation_exception(self, error):
"""
Handle validation exception and return appropriate response.
"""
response = {
"code": 400,
"name": "validation error",
"description": error.messages,
}
return make_taxii2_response(response, status=400)
def raise_unauthorized(self):
"""
Handle unauthorized access.
"""
raise Unauthorized()
def get_endpoint(self, relative_path: str) -> Optional[Callable[[], Response]]:
endpoint = None
for regex, handler in self.ENDPOINT_MAPPING:
match = regex.match(relative_path)
if match:
endpoint = functools.partial(handler, **match.groupdict())
break
if endpoint:
return functools.partial(self.handle_request, endpoint)
def check_authentication(self, endpoint: Callable[[], Response]):
"""Check if account is authenticated, unless endpoint handles that itself."""
if endpoint.func.handles_own_auth:
# Endpoint will handle auth checks itself
return
if context.account is None:
raise Unauthorized()
def check_content_length(self):
if (request.content_length or 0) > self.config["max_content_length"] or len(
request.data
) > self.config[
"max_content_length"
]: # untestable with flask
raise RequestEntityTooLarge()
def check_headers(self, endpoint: Callable[[], Response]):
if not any(
[
valid_accept_mimetype in request.accept_mimetypes
for valid_accept_mimetype in endpoint.func.registered_valid_accept_mimetypes
]
):
raise NotAcceptable()
if (
request.method == "POST"
and request.content_type not in endpoint.func.registered_valid_content_types
):
raise UnsupportedMediaType()
def check_allowed_methods(self, endpoint: Callable[[], Response]):
if request.method not in endpoint.func.registered_valid_methods:
raise MethodNotAllowed(valid_methods=endpoint.func.registered_valid_methods)
def handle_request(self, endpoint: Callable[[], Response]):
self.check_authentication(endpoint)
self.check_content_length()
self.check_allowed_methods(endpoint)
self.check_headers(endpoint)
return endpoint()
@register_handler(r"^/taxii2/$", handles_own_auth=True)
def discovery_handler(self):
if context.account is None and not self.config["public_discovery"]:
raise Unauthorized()
response = {
"title": self.config["title"],
}
for key in ["description", "contact"]:
if self.config.get(key):
response[key] = self.config.get(key)
default_api_root, api_roots = self.persistence.get_api_roots()
if default_api_root:
response["default"] = f"/taxii2/{default_api_root.id}/"
response["api_roots"] = [f"/taxii2/{api_root.id}/" for api_root in api_roots]
return make_taxii2_response(response)
@register_handler(r"^/taxii2/(?P<api_root_id>[^/]+)/$", handles_own_auth=True)
def api_root_handler(self, api_root_id):
try:
api_root = self.persistence.get_api_root(api_root_id=api_root_id)
except DoesNotExistError:
if context.account is None:
raise Unauthorized()
raise NotFound()
if context.account is None and not api_root.is_public:
raise Unauthorized()
response = {
"title": api_root.title,
"versions": ["application/taxii+json;version=2.1"],
"max_content_length": self.config["max_content_length"],
}
if api_root.description:
response["description"] = api_root.description
return make_taxii2_response(response)
@register_handler(
r"^/taxii2/(?P<api_root_id>[^/]+)/status/(?P<job_id>[^/]+)/$",
handles_own_auth=True,
)
def job_handler(self, api_root_id, job_id):
try:
api_root = self.persistence.get_api_root(api_root_id=api_root_id)
except DoesNotExistError:
if context.account is None:
raise Unauthorized()
raise NotFound()
if context.account is None and not api_root.is_public:
raise Unauthorized()
try:
job = self.persistence.get_job_and_details(
api_root_id=api_root_id, job_id=job_id
)
except DoesNotExistError:
raise NotFound()
response = job.as_taxii2_dict()
return make_taxii2_response(response)
@register_handler(
r"^/taxii2/(?P<api_root_id>[^/]+)/collections/$", handles_own_auth=True
)
def collections_handler(self, api_root_id):
try:
api_root = self.persistence.get_api_root(api_root_id=api_root_id)
except DoesNotExistError:
if context.account is None:
raise Unauthorized()
raise NotFound()
if context.account is None and not api_root.is_public:
raise Unauthorized()
collections = self.persistence.get_collections(api_root_id=api_root_id)
response = {}
if collections:
response["collections"] = []
for collection in collections:
data = {
"id": collection.id,
"title": collection.title,
"can_read": collection.can_read(context.account),
"can_write": collection.can_write(context.account),
"media_types": ["application/stix+json;version=2.1"],
}
for key in ["description", "alias"]:
value = getattr(collection, key, None)
if value:
data[key] = value
response["collections"].append(data)
return make_taxii2_response(response)
@register_handler(
r"^/taxii2/(?P<api_root_id>[^/]+)/collections/(?P<collection_id_or_alias>[^/]+)/$",
handles_own_auth=True,
)
def collection_handler(self, api_root_id, collection_id_or_alias):
try:
collection = self.persistence.get_collection(
api_root_id=api_root_id, collection_id_or_alias=collection_id_or_alias
)
except DoesNotExistError:
if context.account is None:
raise Unauthorized()
raise NotFound()
if context.account is None and not (
collection.can_read(context.account)
or collection.can_write(context.account)
):
raise Unauthorized()
response = {
"id": collection.id,
"title": collection.title,
"can_read": collection.can_read(context.account),
"can_write": collection.can_write(context.account),
"media_types": ["application/stix+json;version=2.1"],
}
for key in ["description", "alias"]:
value = getattr(collection, key, None)
if value:
response[key] = value
return make_taxii2_response(response)
@register_handler(
r"^/taxii2/(?P<api_root_id>[^/]+)/collections/(?P<collection_id_or_alias>[^/]+)/manifest/$",
handles_own_auth=True,
)
def manifest_handler(self, api_root_id, collection_id_or_alias):
filter_params = validate_list_filter_params(request.args, self.persistence.api)
try:
manifest, more = self.persistence.get_manifest(
api_root_id=api_root_id,
collection_id_or_alias=collection_id_or_alias,
**filter_params,
)
except (DoesNotExistError, NoReadPermission):
if context.account is None:
raise Unauthorized()
raise NotFound()
if manifest:
response = {
"more": more,
"objects": [
{
"id": obj.id,
"date_added": taxii2_datetimeformat(obj.date_added),
"version": taxii2_datetimeformat(obj.version),
"media_type": f"application/stix+json;version={obj.spec_version}",
}
for obj in manifest
],
}
headers = {
"X-TAXII-Date-Added-First": min(
obj["date_added"] for obj in response["objects"]
),
"X-TAXII-Date-Added-Last": max(
obj["date_added"] for obj in response["objects"]
),
}
else:
response = {}
headers = {}
return make_taxii2_response(
response,
extra_headers=headers,
)
@register_handler(
r"^/taxii2/(?P<api_root_id>[^/]+)/collections/(?P<collection_id_or_alias>[^/]+)/objects/$",
("GET", "POST"),
valid_content_types=("application/taxii+json;version=2.1",),
handles_own_auth=True,
)
def objects_handler(self, api_root_id, collection_id_or_alias):
if request.method == "GET":
return self.objects_get_handler(api_root_id, collection_id_or_alias)
if request.method == "POST":
return self.objects_post_handler(api_root_id, collection_id_or_alias)
def objects_get_handler(self, api_root_id, collection_id_or_alias):
filter_params = validate_list_filter_params(request.args, self.persistence.api)
try:
objects, more, next_param = self.persistence.get_objects(
api_root_id=api_root_id,
collection_id_or_alias=collection_id_or_alias,
**filter_params,
)
except (DoesNotExistError, NoReadPermission):
if context.account is None:
raise Unauthorized()
raise NotFound()
if objects:
response = {
"more": more,
"objects": [
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in objects
],
}
headers = {
"X-TAXII-Date-Added-First": taxii2_datetimeformat(
min(obj.date_added for obj in objects)
),
"X-TAXII-Date-Added-Last": taxii2_datetimeformat(
max(obj.date_added for obj in objects)
),
}
if more:
response["next"] = next_param
else:
response = {}
headers = {}
return make_taxii2_response(
response,
extra_headers=headers,
)
def objects_post_handler(self, api_root_id, collection_id_or_alias):
validate_envelope(
request.data, allow_custom=self.config.get("allow_custom_properties", True)
)
try:
job = self.persistence.add_objects(
api_root_id=api_root_id,
collection_id_or_alias=collection_id_or_alias,
data=request.get_json(),
)
except (DoesNotExistError, NoWritePermission):
if context.account is None:
raise Unauthorized()
raise NotFound()
response = job.as_taxii2_dict()
headers = {}
return make_taxii2_response(
response,
202,
extra_headers=headers,
)
@register_handler(
r"^/taxii2/(?P<api_root_id>[^/]+)/collections/(?P<collection_id_or_alias>[^/]+)/objects/(?P<object_id>[^/]+)/$",
("GET", "DELETE"),
handles_own_auth=True,
)
def object_handler(self, api_root_id, collection_id_or_alias, object_id):
if request.method == "GET":
return self.object_get_handler(
api_root_id, collection_id_or_alias, object_id
)
if request.method == "DELETE":
return self.object_delete_handler(
api_root_id, collection_id_or_alias, object_id
)
def object_get_handler(self, api_root_id, collection_id_or_alias, object_id):
filter_params = validate_object_filter_params(
request.args, self.persistence.api
)
try:
versions, more, next_param = self.persistence.get_object(
api_root_id=api_root_id,
collection_id_or_alias=collection_id_or_alias,
object_id=object_id,
**filter_params,
)
except (DoesNotExistError, NoReadPermission):
if context.account is None:
raise Unauthorized()
raise NotFound()
if versions:
response = {
"more": more,
"objects": [
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in versions
],
}
headers = {
"X-TAXII-Date-Added-First": taxii2_datetimeformat(
min(obj.date_added for obj in versions)
),
"X-TAXII-Date-Added-Last": taxii2_datetimeformat(
max(obj.date_added for obj in versions)
),
}
if more:
response["next"] = next_param
else:
response = {}
headers = {}
return make_taxii2_response(
response,
extra_headers=headers,
)
def object_delete_handler(self, api_root_id, collection_id_or_alias, object_id):
filter_params = validate_delete_filter_params(request.args)
try:
self.persistence.delete_object(
api_root_id=api_root_id,
collection_id_or_alias=collection_id_or_alias,
object_id=object_id,
**filter_params,
)
except (DoesNotExistError, NoReadNoWritePermission):
if context.account is None:
raise Unauthorized()
raise NotFound()
except (NoReadPermission, NoWritePermission):
if context.account is None:
raise Unauthorized()
raise Forbidden()
return make_taxii2_response("")
@register_handler(
(
r"^/taxii2/(?P<api_root_id>[^/]+)/collections/(?P<collection_id_or_alias>[^/]+)"
r"/objects/(?P<object_id>[^/]+)/versions/$"
),
handles_own_auth=True,
)
def versions_handler(self, api_root_id, collection_id_or_alias, object_id):
filter_params = validate_versions_filter_params(
request.args, self.persistence.api
)
try:
versions, more = self.persistence.get_versions(
api_root_id=api_root_id,
collection_id_or_alias=collection_id_or_alias,
object_id=object_id,
**filter_params,
)
except (DoesNotExistError, NoReadPermission):
if context.account is None:
raise Unauthorized()
raise NotFound()
if versions:
response = {
"more": more,
"versions": [taxii2_datetimeformat(obj.version) for obj in versions],
}
headers = {
"X-TAXII-Date-Added-First": taxii2_datetimeformat(
min(obj.date_added for obj in versions)
),
"X-TAXII-Date-Added-Last": taxii2_datetimeformat(
max(obj.date_added for obj in versions)
),
}
else:
response = {}
headers = {}
return make_taxii2_response(
response,
extra_headers=headers,
)
class ServerMapping(NamedTuple):
taxii1: Optional[TAXII1Server]
taxii2: Optional[TAXII2Server]
class TAXIIServer:
"""
TAXII Server class.
This is the main entrypoint for http requests.
It keeps the Auth API manager instance and dispatches requests and error handling to
:class:`TAXII1Server`
and
:class:`TAXII2Server`
"""
AUTH_MANAGER_CLASS: ClassVar[Type[AuthManager]] = AuthManager
servers: ServerMapping
app: Flask
config: ServerConfig
def __init__(self, config: ServerConfig):
self.config = config
servers_kwargs = {
"taxii1": None,
"taxii2": None,
}
if "taxii1" in config and config["taxii1"]:
servers_kwargs["taxii1"] = TAXII1Server(
{**config["taxii1"], "domain": config.get("domain")}
)
if "taxii2" in config and config["taxii2"]:
servers_kwargs["taxii2"] = TAXII2Server(
{**config["taxii2"], "domain": config.get("domain")}
)
self.servers = ServerMapping(**servers_kwargs)
self.auth = self.AUTH_MANAGER_CLASS(
server=self, api=initialize_api(config["auth_api"])
)
@property
def real_servers(self):
return [server for server in self.servers if server is not None]
@property
def current_server(self):
try:
server = context.taxiiserver
except AttributeError:
if len(self.real_servers) == 1:
server = self.real_servers[0]
else:
server = None
return server
def init_app(self, app: Flask):
"""Connect taxii1, taxii2 and auth to flask."""
self.app = app
for server in self.real_servers:
server.init_app(app)
self.auth.api.init_app(app)
def is_basic_auth_supported(self):
"""Check if basic auth is a supported feature."""
return self.config.get("support_basic_auth", False)
def get_endpoint(self, relative_path: str) -> Optional[Callable[[], Response]]:
"""Get first endpoint matching relative_path."""
for server in self.real_servers:
endpoint = server.get_endpoint(relative_path)
if endpoint:
endpoint.server = server
return endpoint
def handle_request(self, relative_path: str) -> Response:
"""Dispatch request to appropriate taxii* server."""
relative_path = "/" + relative_path
endpoint = self.get_endpoint(relative_path)
if not endpoint:
raise NotFound()
context.taxiiserver = endpoint.server
return endpoint()
def handle_internal_error(self, error):
"""Dispatch internal error handling to appropriate taxii* server."""
return context.taxiiserver.handle_internal_error(error)
def handle_status_exception(self, error):
"""Dispatch status exception handling to appropriate taxii* server."""
return context.taxiiserver.handle_status_exception(error)
def handle_http_exception(self, error):
"""Dispatch http exception handling to appropriate taxii* server."""
server = self.current_server
if server:
return server.handle_http_exception(error)
else:
return error.get_response()
def handle_validation_exception(self, error):
"""Dispatch validation exception handling to appropriate taxii* server."""
server = self.current_server
if server:
return server.handle_validation_exception(error)
else:
return error.get_response()
def raise_unauthorized(self):
"""Dispatch unauthorized handling to appropriate taxii* server."""
endpoint = self.get_endpoint(request.path)
if endpoint:
server = endpoint.server
else:
server = self.servers.taxii1
context.taxiiserver = server
return server.raise_unauthorized()