Skip to content

Commit 8c4204d

Browse files
authored
Merge pull request #385 from emmanvg/taxii-datastore-updates
Test Datastore TAXII Updates
2 parents c7fb79d + 2b0d63c commit 8c4204d

File tree

5 files changed

+111
-16
lines changed

5 files changed

+111
-16
lines changed

stix2/datastore/taxii.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from stix2.utils import deduplicate
1313

1414
try:
15-
from taxii2client import ValidationError
15+
from taxii2client.exceptions import ValidationError
1616
_taxii2_client = True
1717
except ImportError:
1818
_taxii2_client = False

stix2/test/v20/conftest.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,42 @@ def stix_objs1():
114114
return [ind1, ind2, ind3, ind4, ind5]
115115

116116

117+
@pytest.fixture
118+
def stix_objs1_manifests():
119+
# Tests against latest medallion (TAXII 2.1)
120+
ind1 = {
121+
"date_added": "2017-01-27T13:49:53.935Z",
122+
"id": "indicator--00000000-0000-4000-8000-000000000001",
123+
"media_type": "application/stix+json;version=2.1",
124+
"version": "2017-01-27T13:49:53.935Z",
125+
}
126+
ind2 = {
127+
"date_added": "2017-01-27T13:49:53.935Z",
128+
"id": "indicator--00000000-0000-4000-8000-000000000001",
129+
"media_type": "application/stix+json;version=2.1",
130+
"version": "2017-01-27T13:49:53.935Z",
131+
}
132+
ind3 = {
133+
"date_added": "2017-01-27T13:49:53.935Z",
134+
"id": "indicator--00000000-0000-4000-8000-000000000001",
135+
"media_type": "application/stix+json;version=2.1",
136+
"version": "2017-01-27T13:49:53.936Z",
137+
}
138+
ind4 = {
139+
"date_added": "2017-01-27T13:49:53.935Z",
140+
"id": "indicator--00000000-0000-4000-8000-000000000002",
141+
"media_type": "application/stix+json;version=2.1",
142+
"version": "2017-01-27T13:49:53.935Z",
143+
}
144+
ind5 = {
145+
"date_added": "2017-01-27T13:49:53.935Z",
146+
"id": "indicator--00000000-0000-4000-8000-000000000002",
147+
"media_type": "application/stix+json;version=2.1",
148+
"version": "2017-01-27T13:49:53.935Z",
149+
}
150+
return [ind1, ind2, ind3, ind4, ind5]
151+
152+
117153
@pytest.fixture
118154
def stix_objs2():
119155
ind6 = {

stix2/test/v20/test_datastore_taxii.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
import pytest
55
from requests.models import Response
66
import six
7-
from taxii2client import Collection
87
from taxii2client.common import _filter_kwargs_to_query_params
8+
from taxii2client.v20 import Collection
99

1010
import stix2
1111
from stix2.datastore import DataSourceError
1212
from stix2.datastore.filters import Filter
13+
from stix2.utils import get_timestamp
1314

1415
COLLECTION_URL = 'https://example.com/api1/collections/91a7b528-80eb-42ed-a74d-c6fbd5a26116/'
1516

@@ -22,13 +23,22 @@ def __init__(self, url, collection_info):
2223
url, collection_info=collection_info,
2324
)
2425
self.objects = []
26+
self.manifests = []
2527

2628
def add_objects(self, bundle):
2729
self._verify_can_write()
2830
if isinstance(bundle, six.string_types):
2931
bundle = json.loads(bundle, encoding='utf-8')
3032
for object in bundle.get("objects", []):
3133
self.objects.append(object)
34+
self.manifests.append(
35+
{
36+
"date_added": get_timestamp(),
37+
"id": object["id"],
38+
"media_type": "application/stix+json;version=2.1",
39+
"version": object.get("modified", object.get("created", get_timestamp())),
40+
},
41+
)
3242

3343
def get_objects(self, **filter_kwargs):
3444
self._verify_can_read()
@@ -38,8 +48,8 @@ def get_objects(self, **filter_kwargs):
3848
objs = full_filter.process_filter(
3949
self.objects,
4050
("id", "type", "version"),
41-
[],
42-
None,
51+
self.manifests,
52+
100,
4353
)[0]
4454
if objs:
4555
return stix2.v20.Bundle(objects=objs)
@@ -60,8 +70,8 @@ def get_object(self, id, **filter_kwargs):
6070
filtered_objects = full_filter.process_filter(
6171
objects,
6272
("version",),
63-
[],
64-
None,
73+
self.manifests,
74+
100,
6575
)[0]
6676
else:
6777
filtered_objects = []
@@ -74,7 +84,7 @@ def get_object(self, id, **filter_kwargs):
7484

7585

7686
@pytest.fixture
77-
def collection(stix_objs1):
87+
def collection(stix_objs1, stix_objs1_manifests):
7888
mock = MockTAXIICollectionEndpoint(
7989
COLLECTION_URL, {
8090
"id": "91a7b528-80eb-42ed-a74d-c6fbd5a26116",
@@ -89,11 +99,12 @@ def collection(stix_objs1):
8999
)
90100

91101
mock.objects.extend(stix_objs1)
102+
mock.manifests.extend(stix_objs1_manifests)
92103
return mock
93104

94105

95106
@pytest.fixture
96-
def collection_no_rw_access(stix_objs1):
107+
def collection_no_rw_access(stix_objs1, stix_objs1_manifests):
97108
mock = MockTAXIICollectionEndpoint(
98109
COLLECTION_URL, {
99110
"id": "91a7b528-80eb-42ed-a74d-c6fbd5a26116",
@@ -108,6 +119,7 @@ def collection_no_rw_access(stix_objs1):
108119
)
109120

110121
mock.objects.extend(stix_objs1)
122+
mock.manifests.extend(stix_objs1_manifests)
111123
return mock
112124

113125

stix2/test/v21/conftest.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,42 @@ def stix_objs1():
135135
return [ind1, ind2, ind3, ind4, ind5]
136136

137137

138+
@pytest.fixture
139+
def stix_objs1_manifests():
140+
# Tests against latest medallion (TAXII 2.1)
141+
ind1 = {
142+
"date_added": "2017-01-27T13:49:53.935Z",
143+
"id": "indicator--00000000-0000-4000-8000-000000000001",
144+
"media_type": "application/stix+json;version=2.1",
145+
"version": "2017-01-27T13:49:53.935Z",
146+
}
147+
ind2 = {
148+
"date_added": "2017-01-27T13:49:53.935Z",
149+
"id": "indicator--00000000-0000-4000-8000-000000000001",
150+
"media_type": "application/stix+json;version=2.1",
151+
"version": "2017-01-27T13:49:53.935Z",
152+
}
153+
ind3 = {
154+
"date_added": "2017-01-27T13:49:53.935Z",
155+
"id": "indicator--00000000-0000-4000-8000-000000000001",
156+
"media_type": "application/stix+json;version=2.1",
157+
"version": "2017-01-27T13:49:53.936Z",
158+
}
159+
ind4 = {
160+
"date_added": "2017-01-27T13:49:53.935Z",
161+
"id": "indicator--00000000-0000-4000-8000-000000000002",
162+
"media_type": "application/stix+json;version=2.1",
163+
"version": "2017-01-27T13:49:53.935Z",
164+
}
165+
ind5 = {
166+
"date_added": "2017-01-27T13:49:53.935Z",
167+
"id": "indicator--00000000-0000-4000-8000-000000000002",
168+
"media_type": "application/stix+json;version=2.1",
169+
"version": "2017-01-27T13:49:53.935Z",
170+
}
171+
return [ind1, ind2, ind3, ind4, ind5]
172+
173+
138174
@pytest.fixture
139175
def stix_objs2():
140176
ind6 = {

stix2/test/v21/test_datastore_taxii.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
import pytest
55
from requests.models import Response
66
import six
7-
from taxii2client import Collection
87
from taxii2client.common import _filter_kwargs_to_query_params
8+
from taxii2client.v21 import Collection
99

1010
import stix2
1111
from stix2.datastore import DataSourceError
1212
from stix2.datastore.filters import Filter
13+
from stix2.utils import get_timestamp
1314

1415
COLLECTION_URL = 'https://example.com/api1/collections/91a7b528-80eb-42ed-a74d-c6fbd5a26116/'
1516

@@ -22,13 +23,22 @@ def __init__(self, url, collection_info):
2223
url, collection_info=collection_info,
2324
)
2425
self.objects = []
26+
self.manifests = []
2527

2628
def add_objects(self, bundle):
2729
self._verify_can_write()
2830
if isinstance(bundle, six.string_types):
2931
bundle = json.loads(bundle, encoding='utf-8')
3032
for object in bundle.get("objects", []):
3133
self.objects.append(object)
34+
self.manifests.append(
35+
{
36+
"date_added": get_timestamp(),
37+
"id": object["id"],
38+
"media_type": "application/stix+json;version=2.1",
39+
"version": object.get("modified", object.get("created", get_timestamp())),
40+
},
41+
)
3242

3343
def get_objects(self, **filter_kwargs):
3444
self._verify_can_read()
@@ -38,11 +48,10 @@ def get_objects(self, **filter_kwargs):
3848
objs = full_filter.process_filter(
3949
self.objects,
4050
("id", "type", "version"),
41-
[],
42-
None,
51+
self.manifests,
52+
100,
4353
)[0]
4454
if objs:
45-
print(objs)
4655
return stix2.v21.Bundle(objects=objs)
4756
else:
4857
resp = Response()
@@ -61,8 +70,8 @@ def get_object(self, id, **filter_kwargs):
6170
filtered_objects = full_filter.process_filter(
6271
objects,
6372
("version",),
64-
[],
65-
None,
73+
self.manifests,
74+
100,
6675
)[0]
6776
else:
6877
filtered_objects = []
@@ -75,7 +84,7 @@ def get_object(self, id, **filter_kwargs):
7584

7685

7786
@pytest.fixture
78-
def collection(stix_objs1):
87+
def collection(stix_objs1, stix_objs1_manifests):
7988
mock = MockTAXIICollectionEndpoint(
8089
COLLECTION_URL, {
8190
"id": "91a7b528-80eb-42ed-a74d-c6fbd5a26116",
@@ -90,11 +99,12 @@ def collection(stix_objs1):
9099
)
91100

92101
mock.objects.extend(stix_objs1)
102+
mock.manifests.extend(stix_objs1_manifests)
93103
return mock
94104

95105

96106
@pytest.fixture
97-
def collection_no_rw_access(stix_objs1):
107+
def collection_no_rw_access(stix_objs1, stix_objs1_manifests):
98108
mock = MockTAXIICollectionEndpoint(
99109
COLLECTION_URL, {
100110
"id": "91a7b528-80eb-42ed-a74d-c6fbd5a26116",
@@ -109,6 +119,7 @@ def collection_no_rw_access(stix_objs1):
109119
)
110120

111121
mock.objects.extend(stix_objs1)
122+
mock.manifests.extend(stix_objs1_manifests)
112123
return mock
113124

114125

0 commit comments

Comments
 (0)