Skip to content
Merged
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 .flake8
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[flake8]
max-line-length=120
exclude = docs/*,.tox/*
ignore = E203, W503, W504, E704
5 changes: 5 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Initial commit to format the codebase with black
e454428fdf5b3d32f8ffbe9bded3878a80e7b169
# Initial commit to sort import on the codebase
2d69e4fb6daf037cb2f4358fde0c6c96e3f7ca16

19 changes: 19 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Check format

on:
- pull_request
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install deps
run: pip install -r requirements-dev.txt
- name: Run black
run: black --check opentaxii/ tests/
- name: Run isort
run: isort --check-only opentaxii/ tests/
3 changes: 2 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
# serve to show the default.

import datetime
import sys
import os
import sys

import sphinx_rtd_theme

# If extensions (or modules to document with autodoc) are in another directory,
Expand Down
3 changes: 2 additions & 1 deletion docs/update_db_schema_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
current_dir = os.path.dirname(__file__)
sys.path.append(os.path.abspath(os.path.join(current_dir, "..")))

from opentaxii.persistence.sqldb.models import Base
from sqla_graphs import TableGrapher

from opentaxii.persistence.sqldb.models import Base

grapher = TableGrapher(
style={"node_table_header": {"bgcolor": "#000080"}},
graph_options={"size": "30,30!"}, # inches, this maps to 2880px
Expand Down
4 changes: 3 additions & 1 deletion examples/hooks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

from opentaxii.signals import (
CONTENT_BLOCK_CREATED, INBOX_MESSAGE_CREATED, SUBSCRIPTION_CREATED
CONTENT_BLOCK_CREATED,
INBOX_MESSAGE_CREATED,
SUBSCRIPTION_CREATED,
)


Expand Down
6 changes: 3 additions & 3 deletions opentaxii/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'''
OpenTAXII, TAXII server implementation from EclecticIQ.
OpenTAXII, TAXII server implementation from EclecticIQ.
'''

# flake8: noqa

from ._version import __version__
from .server import TAXIIServer
from .config import ServerConfig
from .entities import Account

from .local import context
from .server import TAXIIServer
1 change: 0 additions & 1 deletion opentaxii/auth/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

class OpenTAXIIAuthAPI:
'''Abstract class that represents OpenTAXII Authentication API.

Expand Down
4 changes: 1 addition & 3 deletions opentaxii/auth/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ def update_account(self, account, password):
for colname, permission in list(account.permissions.items()):
collection = self.server.servers.taxii1.persistence.get_collection(colname)
if not collection:
log.warning(
"update_account.unknown_collection",
collection=colname)
log.warning("update_account.unknown_collection", collection=colname)
account.permissions.pop(colname)
account = self.api.update_account(account, password)
return account
Expand Down
38 changes: 25 additions & 13 deletions opentaxii/auth/sqldb/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import jwt
import structlog
from sqlalchemy.orm import exc

from opentaxii.auth import OpenTAXIIAuthAPI
from opentaxii.common.sqldb import BaseSQLDatabaseAPI
from opentaxii.entities import Account as AccountEntity
from sqlalchemy.orm import exc

from .models import Account, Base

Expand All @@ -32,16 +33,19 @@ class SQLDatabaseAPI(BaseSQLDatabaseAPI, OpenTAXIIAuthAPI):
BASEMODEL = Base

def __init__(
self,
db_connection,
create_tables=False,
secret=None,
token_ttl_secs=None,
**engine_parameters):
self,
db_connection,
create_tables=False,
secret=None,
token_ttl_secs=None,
**engine_parameters,
):
super().__init__(db_connection, create_tables, **engine_parameters)
if not secret:
raise ValueError('Secret is not defined for %s.%s' % (
self.__module__, self.__class__.__name__))
raise ValueError(
'Secret is not defined for %s.%s'
% (self.__module__, self.__class__.__name__)
)
self.secret = secret
self.token_ttl_secs = token_ttl_secs or 60 * 60 # 60min

Expand Down Expand Up @@ -71,18 +75,25 @@ def get_account(self, token):
return account_to_account_entity(account)

def delete_account(self, username):
account = self.db.session.query(Account).filter_by(username=username).one_or_none()
account = (
self.db.session.query(Account).filter_by(username=username).one_or_none()
)
if account:
self.db.session.delete(account)
self.db.session.commit()

def get_accounts(self):
return [
account_to_account_entity(account)
for account in self.db.session.query(Account).all()]
for account in self.db.session.query(Account).all()
]

def update_account(self, obj, password=None):
account = self.db.session.query(Account).filter_by(username=obj.username).one_or_none()
account = (
self.db.session.query(Account)
.filter_by(username=obj.username)
.one_or_none()
)
if not account:
account = Account(username=obj.username)
self.db.session.add(account)
Expand Down Expand Up @@ -120,4 +131,5 @@ def account_to_account_entity(account):
id=account.id,
username=account.username,
is_admin=account.is_admin,
permissions=account.permissions)
permissions=account.permissions,
)
11 changes: 5 additions & 6 deletions opentaxii/auth/sqldb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

from sqlalchemy import schema, types
from sqlalchemy.ext.declarative import declarative_base

from werkzeug.security import (
check_password_hash, generate_password_hash
)
from werkzeug.security import check_password_hash, generate_password_hash

__all__ = ['Base', 'Account']

Expand Down Expand Up @@ -42,6 +39,8 @@ def permissions(self, permissions):
for collection_name, permission in permissions.items():
if permission not in ALL_PERMISSIONS:
raise ValueError(
"Unknown permission '{}' specified for collection '{}'"
.format(permission, collection_name))
"Unknown permission '{}' specified for collection '{}'".format(
permission, collection_name
)
)
self._permissions = json.dumps(permissions)
3 changes: 1 addition & 2 deletions opentaxii/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from opentaxii.server import TAXIIServer
from opentaxii.config import ServerConfig
from opentaxii.middleware import create_app
from opentaxii.server import TAXIIServer
from opentaxii.utils import configure_logging


config = ServerConfig()
configure_logging(config['logging'], plain=True)

Expand Down
4 changes: 2 additions & 2 deletions opentaxii/cli/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
def create_account(argv=None):
parser = argparse.ArgumentParser(
description="Create Account via OpenTAXII Auth API",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("-u", "--username", required=True)
parser.add_argument("-p", "--password", required=True)
Expand Down Expand Up @@ -38,7 +38,7 @@ def is_truely(text):
def update_account(argv=None):
parser = argparse.ArgumentParser(
description="Update Account via OpenTAXII Auth API",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
fields = ("password", "admin")
parser.add_argument("-u", "--username", required=True)
Expand Down
1 change: 0 additions & 1 deletion opentaxii/cli/run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from opentaxii.cli import app


Expand Down
2 changes: 1 addition & 1 deletion opentaxii/common/sqldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, db_connection, create_tables=False, **engine_parameters):
"autocommit": False,
"autoflush": True,
},
**engine_parameters
**engine_parameters,
)
if create_tables:
self.db.create_all_tables()
Expand Down
2 changes: 1 addition & 1 deletion opentaxii/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def _get_env_config(env=os.environ, optional_env_var=None):
continue
if key == optional_env_var:
continue
key = key[len(ENV_VAR_PREFIX):].lstrip("_").lower()
key = key[len(ENV_VAR_PREFIX) :].lstrip("_").lower()
value = yaml.safe_load(value)

container = result
Expand Down
19 changes: 7 additions & 12 deletions opentaxii/entities.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

class Account:
'''Represents Account entity.

Expand All @@ -9,25 +8,21 @@ class Account:
:param dict details: additional details of an account
'''

def __init__(
self, id, username, permissions, is_admin=False, **details):
def __init__(self, id, username, permissions, is_admin=False, **details):
self.id = id
self.username = username
self.permissions = permissions
self.is_admin = is_admin
self.details = details

def can_read(self, collection_name):
return (
self.is_admin or
self.permissions.get(collection_name) in ('read', 'modify'))
return self.is_admin or self.permissions.get(collection_name) in (
'read',
'modify',
)

def can_modify(self, collection_name):
return (
self.is_admin or
self.permissions.get(collection_name) == 'modify')
return self.is_admin or self.permissions.get(collection_name) == 'modify'

def __repr__(self):
return (
'Account(username={}, is_admin={})'
.format(self.username, self.is_admin))
return 'Account(username={}, is_admin={})'.format(self.username, self.is_admin)
1 change: 0 additions & 1 deletion opentaxii/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from .taxii.exceptions import UnauthorizedStatus


Expand Down
11 changes: 2 additions & 9 deletions opentaxii/http.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@

from .middleware import create_app
from .config import ServerConfig
from .middleware import create_app
from .server import TAXIIServer
from .utils import configure_logging


# This module is also used as a Gunicorn configuration module, i.e. passed
# as ``--config python:opentaxii.http``. ``logconfig_dict`` module-level
# variable is recognised by Gunicorn >= 19.8. The desired effect is to
Expand All @@ -15,12 +13,7 @@
'version': 1,
'disable_existing_loggers': False,
'root': {},
'loggers': {
'gunicorn.error': {
'level': 'INFO',
'propagate': True
}
}
'loggers': {'gunicorn.error': {'level': 'INFO', 'propagate': True}},
}

config_obj = ServerConfig()
Expand Down
7 changes: 4 additions & 3 deletions opentaxii/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import structlog
from flask import Flask, request
from marshmallow.exceptions import \
ValidationError as MarshmallowValidationError
from marshmallow.exceptions import ValidationError as MarshmallowValidationError
from werkzeug.exceptions import HTTPException

from .exceptions import InvalidAuthHeader
Expand Down Expand Up @@ -42,7 +41,9 @@ def create_app(server):
app.register_error_handler(500, server.handle_internal_error)
app.register_error_handler(StatusMessageException, server.handle_status_exception)
app.register_error_handler(HTTPException, server.handle_http_exception)
app.register_error_handler(MarshmallowValidationError, server.handle_validation_exception)
app.register_error_handler(
MarshmallowValidationError, server.handle_validation_exception
)
app.before_request(functools.partial(create_context_before_request, server))
app.after_request(cleanup_context)
return app
Expand Down
7 changes: 5 additions & 2 deletions opentaxii/persistence/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# flake8: noqa
from .api import OpenTAXII2PersistenceAPI, OpenTAXIIPersistenceAPI
from .manager import (BasePersistenceManager, Taxii1PersistenceManager,
Taxii2PersistenceManager)
from .manager import (
BasePersistenceManager,
Taxii1PersistenceManager,
Taxii2PersistenceManager,
)
20 changes: 13 additions & 7 deletions opentaxii/persistence/api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import datetime
from typing import Dict, List, Optional, Tuple

from opentaxii.taxii2.entities import (ApiRoot, Collection, Job,
ManifestRecord, STIXObject,
VersionRecord)
from opentaxii.taxii2.entities import (
ApiRoot,
Collection,
Job,
ManifestRecord,
STIXObject,
VersionRecord,
)


class OpenTAXIIPersistenceAPI:
Expand Down Expand Up @@ -267,6 +272,7 @@ class OpenTAXII2PersistenceAPI:

Stub, pending implementation.
"""

@staticmethod
def get_next_param(self, kwargs: Dict) -> str:
"""
Expand Down Expand Up @@ -295,9 +301,7 @@ def get_api_roots(self) -> List[ApiRoot]:
def get_api_root(self, api_root_id: str) -> Optional[ApiRoot]:
raise NotImplementedError

def get_job_and_details(
self, api_root_id: str, job_id: str
) -> Optional[Job]:
def get_job_and_details(self, api_root_id: str, job_id: str) -> Optional[Job]:
raise NotImplementedError

def get_collections(self, api_root_id: str) -> List[Collection]:
Expand Down Expand Up @@ -334,7 +338,9 @@ def get_objects(
) -> Tuple[List[STIXObject], bool, Optional[str]]:
raise NotImplementedError

def add_objects(self, api_root_id: str, collection_id: str, objects: List[Dict]) -> Job:
def add_objects(
self, api_root_id: str, collection_id: str, objects: List[Dict]
) -> Job:
raise NotImplementedError

def get_object(
Expand Down
Loading