Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: spec_version & version from modified/created #240

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var/
*.egg-info/
.installed.cfg
*.egg
venv/

# PyInstaller
# Usually these files are written by a python script from a template
Expand Down
21 changes: 18 additions & 3 deletions opentaxii/persistence/sqldb/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,9 +977,24 @@ def add_objects(
self.db.session.commit()
job_details = []
for obj in objects:
version = datetime.datetime.strptime(
obj["modified"], DATETIMEFORMAT
).replace(tzinfo=datetime.timezone.utc)
version = None
if "modified" in obj:
version = datetime.datetime.strptime(
obj["modified"], DATETIMEFORMAT
).replace(tzinfo=datetime.timezone.utc)
elif "created" in obj:
version = datetime.datetime.strptime(
obj["created"], DATETIMEFORMAT
).replace(tzinfo=datetime.timezone.utc)
else:
# If a STIX object is not versioned (and therefore does not have a modified
# timestamp) then this version parameter MUST use the created timestamp. If
# an object does not have a created or modified timestamp or any other
# version information that can be used, then the server should use a value for
# the version that is consistent to the server.
# -- TAXII 2.1 specification --
raise ValueError("STIX object MUST have `modified` or `created` timestamp "
"in order to create version")
if (
not self.db.session.query(literal(True))
.filter(
Expand Down
8 changes: 4 additions & 4 deletions opentaxii/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def collections_handler(self, api_root_id):
response["collections"] = []
for collection in collections:
data = {
"id": collection.id,
"id": str(collection.id),
"title": collection.title,
"can_read": collection.can_read(context.account),
"can_write": collection.can_write(context.account),
Expand Down Expand Up @@ -567,7 +567,7 @@ def collection_handler(self, api_root_id, collection_id_or_alias):
if context.account is None and not collection.can_read(context.account):
raise Unauthorized()
response = {
"id": collection.id,
"id": str(collection.id),
"title": collection.title,
"can_read": collection.can_read(context.account),
"can_write": collection.can_write(context.account),
Expand Down Expand Up @@ -655,7 +655,7 @@ def objects_get_handler(self, api_root_id, collection_id_or_alias):
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in objects
Expand Down Expand Up @@ -738,7 +738,7 @@ def object_get_handler(self, api_root_id, collection_id_or_alias, object_id):
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in versions
Expand Down
8 changes: 4 additions & 4 deletions opentaxii/taxii2/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ def can_read(self, account: Optional[Account]):
return self.is_public or (
account
and (
account.is_admin or "read" in set(account.permissions.get(self.id, []))
account.is_admin or "read" in account.permissions.get(str(self.id), [])
)
)

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


Expand Down Expand Up @@ -185,7 +185,7 @@ def __init__(

def as_taxii2_dict(self):
"""Turn this object into a taxii2 dict."""
response = {"id": self.stix_id, "version": taxii2_datetimeformat(self.version)}
response = {"id": str(self.stix_id), "version": taxii2_datetimeformat(self.version)}
if self.message:
response["message"] = self.message
return response
Expand Down Expand Up @@ -243,7 +243,7 @@ def __init__(
def as_taxii2_dict(self):
"""Turn this object into a taxii2 dict."""
response = {
"id": self.id,
"id": str(self.id),
"status": self.status,
"request_timestamp": taxii2_datetimeformat(self.request_timestamp),
"total_count": self.total_count,
Expand Down
26 changes: 13 additions & 13 deletions tests/taxii2/test_taxii2_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in [STIX_OBJECTS[0]]
Expand All @@ -70,7 +70,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in [STIX_OBJECTS[0]]
Expand Down Expand Up @@ -104,7 +104,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in [STIX_OBJECTS[2]]
Expand Down Expand Up @@ -177,7 +177,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in STIX_OBJECTS[:1]
Expand Down Expand Up @@ -210,7 +210,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in STIX_OBJECTS[:1]
Expand Down Expand Up @@ -242,7 +242,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in [STIX_OBJECTS[0], STIX_OBJECTS[2]]
Expand All @@ -269,7 +269,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in [STIX_OBJECTS[0]]
Expand Down Expand Up @@ -341,7 +341,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in [STIX_OBJECTS[2]]
Expand Down Expand Up @@ -386,7 +386,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in STIX_OBJECTS[:1]
Expand All @@ -413,7 +413,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in [STIX_OBJECTS[0]]
Expand Down Expand Up @@ -444,7 +444,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in [STIX_OBJECTS[2]]
Expand Down Expand Up @@ -473,7 +473,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in [STIX_OBJECTS[0], STIX_OBJECTS[2]]
Expand Down Expand Up @@ -514,7 +514,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in STIX_OBJECTS[:1]
Expand Down
36 changes: 18 additions & 18 deletions tests/taxii2/test_taxii2_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in STIX_OBJECTS[:2]
Expand Down Expand Up @@ -79,7 +79,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in STIX_OBJECTS[:2]
Expand Down Expand Up @@ -111,7 +111,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in STIX_OBJECTS[1:2]
Expand Down Expand Up @@ -176,7 +176,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in STIX_OBJECTS[:1]
Expand Down Expand Up @@ -206,7 +206,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in STIX_OBJECTS[:2]
Expand Down Expand Up @@ -236,7 +236,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in STIX_OBJECTS[:2]
Expand Down Expand Up @@ -291,7 +291,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in STIX_OBJECTS[1:2]
Expand Down Expand Up @@ -338,7 +338,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in [STIX_OBJECTS[0]]
Expand Down Expand Up @@ -368,7 +368,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in STIX_OBJECTS[:2]
Expand Down Expand Up @@ -396,7 +396,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in [STIX_OBJECTS[0]]
Expand Down Expand Up @@ -426,7 +426,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in STIX_OBJECTS[:2]
Expand Down Expand Up @@ -454,7 +454,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in STIX_OBJECTS[:1]
Expand Down Expand Up @@ -484,7 +484,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in STIX_OBJECTS[:2]
Expand Down Expand Up @@ -516,7 +516,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in STIX_OBJECTS[1:3]
Expand Down Expand Up @@ -546,7 +546,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in STIX_OBJECTS[:3]
Expand Down Expand Up @@ -580,7 +580,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in STIX_OBJECTS[:3]
Expand Down Expand Up @@ -608,7 +608,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in STIX_OBJECTS[:1]
Expand Down Expand Up @@ -642,7 +642,7 @@
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in STIX_OBJECTS[:2]
Expand Down