Skip to content

Commit 974e9a6

Browse files
fix: spec_version & version from modified/created
1 parent 0268539 commit 974e9a6

File tree

6 files changed

+58
-42
lines changed

6 files changed

+58
-42
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var/
2121
*.egg-info/
2222
.installed.cfg
2323
*.egg
24+
venv/
2425

2526
# PyInstaller
2627
# Usually these files are written by a python script from a template

opentaxii/persistence/sqldb/api.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -977,9 +977,24 @@ def add_objects(
977977
self.db.session.commit()
978978
job_details = []
979979
for obj in objects:
980-
version = datetime.datetime.strptime(
981-
obj["modified"], DATETIMEFORMAT
982-
).replace(tzinfo=datetime.timezone.utc)
980+
version = None
981+
if "modified" in obj:
982+
version = datetime.datetime.strptime(
983+
obj["modified"], DATETIMEFORMAT
984+
).replace(tzinfo=datetime.timezone.utc)
985+
elif "created" in obj:
986+
version = datetime.datetime.strptime(
987+
obj["created"], DATETIMEFORMAT
988+
).replace(tzinfo=datetime.timezone.utc)
989+
else:
990+
# If a STIX object is not versioned (and therefore does not have a modified
991+
# timestamp) then this version parameter MUST use the created timestamp. If
992+
# an object does not have a created or modified timestamp or any other
993+
# version information that can be used, then the server should use a value for
994+
# the version that is consistent to the server.
995+
# -- TAXII 2.1 specification --
996+
raise ValueError("STIX object MUST have `modified` or `created` timestamp "
997+
"in order to create version")
983998
if (
984999
not self.db.session.query(literal(True))
9851000
.filter(

opentaxii/server.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ def collections_handler(self, api_root_id):
538538
response["collections"] = []
539539
for collection in collections:
540540
data = {
541-
"id": collection.id,
541+
"id": str(collection.id),
542542
"title": collection.title,
543543
"can_read": collection.can_read(context.account),
544544
"can_write": collection.can_write(context.account),
@@ -567,7 +567,7 @@ def collection_handler(self, api_root_id, collection_id_or_alias):
567567
if context.account is None and not collection.can_read(context.account):
568568
raise Unauthorized()
569569
response = {
570-
"id": collection.id,
570+
"id": str(collection.id),
571571
"title": collection.title,
572572
"can_read": collection.can_read(context.account),
573573
"can_write": collection.can_write(context.account),
@@ -655,7 +655,7 @@ def objects_get_handler(self, api_root_id, collection_id_or_alias):
655655
{
656656
"id": obj.id,
657657
"type": obj.type,
658-
"spec_version": obj.type,
658+
"spec_version": obj.spec_version,
659659
**obj.serialized_data,
660660
}
661661
for obj in objects
@@ -738,7 +738,7 @@ def object_get_handler(self, api_root_id, collection_id_or_alias, object_id):
738738
{
739739
"id": obj.id,
740740
"type": obj.type,
741-
"spec_version": obj.type,
741+
"spec_version": obj.spec_version,
742742
**obj.serialized_data,
743743
}
744744
for obj in versions

opentaxii/taxii2/entities.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ def can_read(self, account: Optional[Account]):
6363
return self.is_public or (
6464
account
6565
and (
66-
account.is_admin or "read" in set(account.permissions.get(self.id, []))
66+
account.is_admin or "read" in account.permissions.get(str(self.id), [])
6767
)
6868
)
6969

7070
def can_write(self, account: Optional[Account]):
7171
"""Determine if `account` is allowed to write to this collection."""
7272
return account and (
73-
account.is_admin or "write" in set(account.permissions.get(self.id, []))
73+
account.is_admin or "write" in account.permissions.get(str(self.id), [])
7474
)
7575

7676

@@ -185,7 +185,7 @@ def __init__(
185185

186186
def as_taxii2_dict(self):
187187
"""Turn this object into a taxii2 dict."""
188-
response = {"id": self.stix_id, "version": taxii2_datetimeformat(self.version)}
188+
response = {"id": str(self.stix_id), "version": taxii2_datetimeformat(self.version)}
189189
if self.message:
190190
response["message"] = self.message
191191
return response
@@ -243,7 +243,7 @@ def __init__(
243243
def as_taxii2_dict(self):
244244
"""Turn this object into a taxii2 dict."""
245245
response = {
246-
"id": self.id,
246+
"id": str(self.id),
247247
"status": self.status,
248248
"request_timestamp": taxii2_datetimeformat(self.request_timestamp),
249249
"total_count": self.total_count,

tests/taxii2/test_taxii2_object.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
{
4444
"id": obj.id,
4545
"type": obj.type,
46-
"spec_version": obj.type,
46+
"spec_version": obj.spec_version,
4747
**obj.serialized_data,
4848
}
4949
for obj in [STIX_OBJECTS[0]]
@@ -70,7 +70,7 @@
7070
{
7171
"id": obj.id,
7272
"type": obj.type,
73-
"spec_version": obj.type,
73+
"spec_version": obj.spec_version,
7474
**obj.serialized_data,
7575
}
7676
for obj in [STIX_OBJECTS[0]]
@@ -104,7 +104,7 @@
104104
{
105105
"id": obj.id,
106106
"type": obj.type,
107-
"spec_version": obj.type,
107+
"spec_version": obj.spec_version,
108108
**obj.serialized_data,
109109
}
110110
for obj in [STIX_OBJECTS[2]]
@@ -177,7 +177,7 @@
177177
{
178178
"id": obj.id,
179179
"type": obj.type,
180-
"spec_version": obj.type,
180+
"spec_version": obj.spec_version,
181181
**obj.serialized_data,
182182
}
183183
for obj in STIX_OBJECTS[:1]
@@ -210,7 +210,7 @@
210210
{
211211
"id": obj.id,
212212
"type": obj.type,
213-
"spec_version": obj.type,
213+
"spec_version": obj.spec_version,
214214
**obj.serialized_data,
215215
}
216216
for obj in STIX_OBJECTS[:1]
@@ -242,7 +242,7 @@
242242
{
243243
"id": obj.id,
244244
"type": obj.type,
245-
"spec_version": obj.type,
245+
"spec_version": obj.spec_version,
246246
**obj.serialized_data,
247247
}
248248
for obj in [STIX_OBJECTS[0], STIX_OBJECTS[2]]
@@ -269,7 +269,7 @@
269269
{
270270
"id": obj.id,
271271
"type": obj.type,
272-
"spec_version": obj.type,
272+
"spec_version": obj.spec_version,
273273
**obj.serialized_data,
274274
}
275275
for obj in [STIX_OBJECTS[0]]
@@ -341,7 +341,7 @@
341341
{
342342
"id": obj.id,
343343
"type": obj.type,
344-
"spec_version": obj.type,
344+
"spec_version": obj.spec_version,
345345
**obj.serialized_data,
346346
}
347347
for obj in [STIX_OBJECTS[2]]
@@ -386,7 +386,7 @@
386386
{
387387
"id": obj.id,
388388
"type": obj.type,
389-
"spec_version": obj.type,
389+
"spec_version": obj.spec_version,
390390
**obj.serialized_data,
391391
}
392392
for obj in STIX_OBJECTS[:1]
@@ -413,7 +413,7 @@
413413
{
414414
"id": obj.id,
415415
"type": obj.type,
416-
"spec_version": obj.type,
416+
"spec_version": obj.spec_version,
417417
**obj.serialized_data,
418418
}
419419
for obj in [STIX_OBJECTS[0]]
@@ -444,7 +444,7 @@
444444
{
445445
"id": obj.id,
446446
"type": obj.type,
447-
"spec_version": obj.type,
447+
"spec_version": obj.spec_version,
448448
**obj.serialized_data,
449449
}
450450
for obj in [STIX_OBJECTS[2]]
@@ -473,7 +473,7 @@
473473
{
474474
"id": obj.id,
475475
"type": obj.type,
476-
"spec_version": obj.type,
476+
"spec_version": obj.spec_version,
477477
**obj.serialized_data,
478478
}
479479
for obj in [STIX_OBJECTS[0], STIX_OBJECTS[2]]
@@ -514,7 +514,7 @@
514514
{
515515
"id": obj.id,
516516
"type": obj.type,
517-
"spec_version": obj.type,
517+
"spec_version": obj.spec_version,
518518
**obj.serialized_data,
519519
}
520520
for obj in STIX_OBJECTS[:1]

tests/taxii2/test_taxii2_objects.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
{
5050
"id": obj.id,
5151
"type": obj.type,
52-
"spec_version": obj.type,
52+
"spec_version": obj.spec_version,
5353
**obj.serialized_data,
5454
}
5555
for obj in STIX_OBJECTS[:2]
@@ -79,7 +79,7 @@
7979
{
8080
"id": obj.id,
8181
"type": obj.type,
82-
"spec_version": obj.type,
82+
"spec_version": obj.spec_version,
8383
**obj.serialized_data,
8484
}
8585
for obj in STIX_OBJECTS[:2]
@@ -111,7 +111,7 @@
111111
{
112112
"id": obj.id,
113113
"type": obj.type,
114-
"spec_version": obj.type,
114+
"spec_version": obj.spec_version,
115115
**obj.serialized_data,
116116
}
117117
for obj in STIX_OBJECTS[1:2]
@@ -176,7 +176,7 @@
176176
{
177177
"id": obj.id,
178178
"type": obj.type,
179-
"spec_version": obj.type,
179+
"spec_version": obj.spec_version,
180180
**obj.serialized_data,
181181
}
182182
for obj in STIX_OBJECTS[:1]
@@ -206,7 +206,7 @@
206206
{
207207
"id": obj.id,
208208
"type": obj.type,
209-
"spec_version": obj.type,
209+
"spec_version": obj.spec_version,
210210
**obj.serialized_data,
211211
}
212212
for obj in STIX_OBJECTS[:2]
@@ -236,7 +236,7 @@
236236
{
237237
"id": obj.id,
238238
"type": obj.type,
239-
"spec_version": obj.type,
239+
"spec_version": obj.spec_version,
240240
**obj.serialized_data,
241241
}
242242
for obj in STIX_OBJECTS[:2]
@@ -291,7 +291,7 @@
291291
{
292292
"id": obj.id,
293293
"type": obj.type,
294-
"spec_version": obj.type,
294+
"spec_version": obj.spec_version,
295295
**obj.serialized_data,
296296
}
297297
for obj in STIX_OBJECTS[1:2]
@@ -338,7 +338,7 @@
338338
{
339339
"id": obj.id,
340340
"type": obj.type,
341-
"spec_version": obj.type,
341+
"spec_version": obj.spec_version,
342342
**obj.serialized_data,
343343
}
344344
for obj in [STIX_OBJECTS[0]]
@@ -368,7 +368,7 @@
368368
{
369369
"id": obj.id,
370370
"type": obj.type,
371-
"spec_version": obj.type,
371+
"spec_version": obj.spec_version,
372372
**obj.serialized_data,
373373
}
374374
for obj in STIX_OBJECTS[:2]
@@ -396,7 +396,7 @@
396396
{
397397
"id": obj.id,
398398
"type": obj.type,
399-
"spec_version": obj.type,
399+
"spec_version": obj.spec_version,
400400
**obj.serialized_data,
401401
}
402402
for obj in [STIX_OBJECTS[0]]
@@ -426,7 +426,7 @@
426426
{
427427
"id": obj.id,
428428
"type": obj.type,
429-
"spec_version": obj.type,
429+
"spec_version": obj.spec_version,
430430
**obj.serialized_data,
431431
}
432432
for obj in STIX_OBJECTS[:2]
@@ -454,7 +454,7 @@
454454
{
455455
"id": obj.id,
456456
"type": obj.type,
457-
"spec_version": obj.type,
457+
"spec_version": obj.spec_version,
458458
**obj.serialized_data,
459459
}
460460
for obj in STIX_OBJECTS[:1]
@@ -484,7 +484,7 @@
484484
{
485485
"id": obj.id,
486486
"type": obj.type,
487-
"spec_version": obj.type,
487+
"spec_version": obj.spec_version,
488488
**obj.serialized_data,
489489
}
490490
for obj in STIX_OBJECTS[:2]
@@ -516,7 +516,7 @@
516516
{
517517
"id": obj.id,
518518
"type": obj.type,
519-
"spec_version": obj.type,
519+
"spec_version": obj.spec_version,
520520
**obj.serialized_data,
521521
}
522522
for obj in STIX_OBJECTS[1:3]
@@ -546,7 +546,7 @@
546546
{
547547
"id": obj.id,
548548
"type": obj.type,
549-
"spec_version": obj.type,
549+
"spec_version": obj.spec_version,
550550
**obj.serialized_data,
551551
}
552552
for obj in STIX_OBJECTS[:3]
@@ -580,7 +580,7 @@
580580
{
581581
"id": obj.id,
582582
"type": obj.type,
583-
"spec_version": obj.type,
583+
"spec_version": obj.spec_version,
584584
**obj.serialized_data,
585585
}
586586
for obj in STIX_OBJECTS[:3]
@@ -608,7 +608,7 @@
608608
{
609609
"id": obj.id,
610610
"type": obj.type,
611-
"spec_version": obj.type,
611+
"spec_version": obj.spec_version,
612612
**obj.serialized_data,
613613
}
614614
for obj in STIX_OBJECTS[:1]
@@ -642,7 +642,7 @@
642642
{
643643
"id": obj.id,
644644
"type": obj.type,
645-
"spec_version": obj.type,
645+
"spec_version": obj.spec_version,
646646
**obj.serialized_data,
647647
}
648648
for obj in STIX_OBJECTS[:2]

0 commit comments

Comments
 (0)