From a71df70a87bb95e45bf80ebfaf56bc352a70665d Mon Sep 17 00:00:00 2001 From: Andrey Miroshnichenko Date: Fri, 7 Oct 2022 04:30:46 +0300 Subject: [PATCH 1/4] Fixing an issue with UUID to JSON serialization --- opentaxii/taxii2/http.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/opentaxii/taxii2/http.py b/opentaxii/taxii2/http.py index a0408d33..0f9a2b72 100644 --- a/opentaxii/taxii2/http.py +++ b/opentaxii/taxii2/http.py @@ -3,12 +3,21 @@ from typing import Dict, Optional from flask import Response, make_response +from uuid import UUID + + +class Taxii2JSONEncoder(json.JSONEncoder): + def default(self, o): + if isinstance(o, UUID): + return str(o) + + return super().default(o) def make_taxii2_response(data, status: Optional[int] = 200, extra_headers: Optional[Dict] = None) -> Response: """Turn input data into valid taxii2 response.""" if not isinstance(data, str): - data = json.dumps(data) + data = json.dumps(data, cls=Taxii2JSONEncoder) response = make_response((data, status)) response.content_type = "application/taxii+json;version=2.1" response.headers.update(extra_headers or {}) From 45c9fca6f6694437bdc69fd38d7934e717662e71 Mon Sep 17 00:00:00 2001 From: Andrey Miroshnichenko Date: Fri, 7 Oct 2022 04:35:12 +0300 Subject: [PATCH 2/4] Fixing an issue with UUID to JSON serialization (Adding comments) --- opentaxii/taxii2/http.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/opentaxii/taxii2/http.py b/opentaxii/taxii2/http.py index 0f9a2b72..989330e9 100644 --- a/opentaxii/taxii2/http.py +++ b/opentaxii/taxii2/http.py @@ -7,7 +7,11 @@ class Taxii2JSONEncoder(json.JSONEncoder): + """ + Extended JSONEncoder class with additional data types support. + """ def default(self, o): + """Implements UUID serialization""" if isinstance(o, UUID): return str(o) From 790839772261239bbd34cd48bb78f27d3a9a7ed3 Mon Sep 17 00:00:00 2001 From: Andrey Miroshnichenko Date: Fri, 7 Oct 2022 05:37:46 +0300 Subject: [PATCH 3/4] Fixing an issue with UUID to JSON serialization when checking the Account permissions to the collections. --- opentaxii/entities.py | 4 ++-- opentaxii/taxii2/entities.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/opentaxii/entities.py b/opentaxii/entities.py index 9ff27a15..4c317e30 100644 --- a/opentaxii/entities.py +++ b/opentaxii/entities.py @@ -20,12 +20,12 @@ def __init__( def can_read(self, collection_name): return ( self.is_admin or - self.permissions.get(collection_name) in ('read', 'modify')) + self.permissions.get(str(collection_name)) in ('read', 'modify')) def can_modify(self, collection_name): return ( self.is_admin or - self.permissions.get(collection_name) == 'modify') + self.permissions.get(str(collection_name)) == 'modify') def __repr__(self): return ( diff --git a/opentaxii/taxii2/entities.py b/opentaxii/taxii2/entities.py index 867b2a6e..adf7c0b3 100644 --- a/opentaxii/taxii2/entities.py +++ b/opentaxii/taxii2/entities.py @@ -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 set(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 set(account.permissions.get(str(self.id), [])) ) From 279bf3b8f263c8ec8a3d31c2ad0e087a82de0cf6 Mon Sep 17 00:00:00 2001 From: Andrey Miroshnichenko Date: Fri, 7 Oct 2022 06:19:58 +0300 Subject: [PATCH 4/4] Fixes an error when opentaxii-create-account ignored `--admin` flag --- opentaxii/cli/auth.py | 1 + 1 file changed, 1 insertion(+) diff --git a/opentaxii/cli/auth.py b/opentaxii/cli/auth.py index 428b81fe..26af5afc 100644 --- a/opentaxii/cli/auth.py +++ b/opentaxii/cli/auth.py @@ -21,6 +21,7 @@ def create_account(argv=None): account = app.taxii_server.auth.api.create_account( username=args.username, password=args.password, + is_admin=args.admin, ) token = app.taxii_server.auth.authenticate( username=account.username,