Permalink
Browse files

delete content blocks cli command, related to #45

  • Loading branch information...
traut committed Jun 1, 2016
1 parent 89890cb commit dc6fddc27a98e8450c7e05e583b2bfb741f6e197
@@ -1,6 +1,13 @@
Changelog
=========

0.1.6 (2016-06-01)
------------------
* Python 3.4 compatibility of the codebase. Tox configuration extended with python 3.4 environment run.
* Flake8 full style compatibility. Flake8 check added to Tox configuration.
* SQLAlchemy session scope issue fixed (related to `#38 <https://github.com/EclecticIQ/OpenTAXII/issues/38>`_).
* `opentaxii-delete-blocks` CLI command added (related to `#45 <https://github.com/EclecticIQ/OpenTAXII/issues/45>`_).

0.1.5 (2016-03-15)
------------------
* Fix for the issue with persistence backend returning ``None`` instead of ``InboxMessage`` object
@@ -17,7 +24,6 @@ Changelog

0.1.2 (2015-07-24)
------------------

* Docker configuration added.
* Health check endpoint added.
* Basic authentication support added.
@@ -28,13 +34,11 @@ Changelog

0.1.1 (2015-04-08)
------------------

* Alias for Root Logger added to logging configuration.
* Context object in a request scope that holds account and token added.
* Support for OPTIONS HTTP request to enable auto version negotiation added.
* Documentation improved.

0.1.0 (2015-03-31)
------------------

* Initial release
@@ -3,4 +3,4 @@
This module defines the package version for use in __init__.py and setup.py.
"""

__version__ = '0.1.6a1'
__version__ = '0.1.6'
@@ -1,3 +1,7 @@
import structlog

log = structlog.getLogger(__name__)


class AuthManager(object):
'''Manager responsible for authentication.
@@ -39,4 +43,7 @@ def create_account(self, username, password):
NOTE: Additional method that is only used in the helper scripts
shipped with OpenTAXII.
'''
return self.api.create_account(username, password)
account = self.api.create_account(username, password)
log.info("account.created", username=account.username)

return account
@@ -72,7 +72,7 @@ def get_account(self, token):
if not account:
return

return AccountEntity(id=account.id)
return AccountEntity(id=account.id, username=account.username)

def create_account(self, username, password):

@@ -82,7 +82,7 @@ def create_account(self, username, password):
self.db.session.add(account)
self.db.session.commit()

return AccountEntity(id=account.id)
return AccountEntity(id=account.id, username=username)

def _generate_token(self, account_id, ttl=TOKEN_TTL):
exp = datetime.utcnow() + timedelta(minutes=ttl)
@@ -0,0 +1,13 @@
from opentaxii.server import TAXIIServer
from opentaxii.config import ServerConfig
from opentaxii.middleware import create_app
from opentaxii.utils import configure_logging


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

server = TAXIIServer(config)

app = create_app(server)
app.debug = True
@@ -2,27 +2,18 @@
import argparse
import structlog

from opentaxii.server import TAXIIServer
from opentaxii.config import ServerConfig
from opentaxii.utils import configure_logging

config = ServerConfig()
configure_logging(config.get('logging'), plain=True)
from opentaxii.cli import app

log = structlog.getLogger(__name__)


def get_parser():
def create_account():

parser = argparse.ArgumentParser(
description="Create Account via OpenTAXII Auth API",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
return parser


def create_account():

parser = get_parser()
parser.add_argument(
"-u", "--username", dest="username",
help="Username for the new account", required=True)
@@ -33,7 +24,12 @@ def create_account():

args = parser.parse_args()

server = TAXIIServer(config)
token = server.auth.create_account(args.username, args.password)
with app.app_context():

account = app.taxii_server.auth.create_account(
args.username, args.password)

token = app.taxii_server.auth.authenticate(
account.username, args.password)

log.info("Account created", username=args.username, token=token)
log.info("account.token", token=token, username=account.username)
@@ -2,82 +2,106 @@
import argparse
import structlog

from opentaxii.config import ServerConfig
from opentaxii.server import TAXIIServer
from opentaxii.utils import configure_logging
from opentaxii.taxii.entities import CollectionEntity

config = ServerConfig()
configure_logging(config.get('logging'), plain=True)
from opentaxii.cli import app

log = structlog.getLogger(__name__)


def get_parser():
def create_services():

parser = argparse.ArgumentParser(
description="Create services via OpenTAXII Auth API",
description="Create services using OpenTAXII Persistence API",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
return parser


def create_services():

parser = get_parser()

parser.add_argument(
"-c", "--services-config", dest="config",
help="YAML file with services configuration", required=True)

args = parser.parse_args()

server = TAXIIServer(config)

services_config = anyconfig.load(args.config, forced_type="yaml")
server.persistence.create_services_from_object(services_config)

log.info("Services created", count=len(services_config))
with app.app_context():

app.taxii_server.persistence.create_services_from_object(
services_config)


def create_collections():

parser = get_parser()
parser = argparse.ArgumentParser(
description="Create collections using OpenTAXII Persistence API",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)

parser.add_argument(
"-c", "--collections-config", dest="config",
help="YAML file with collections configuration", required=True)

args = parser.parse_args()
server = TAXIIServer(config)
collections_config = anyconfig.load(args.config, forced_type="yaml")

created = 0
for collection in collections_config:
with app.app_context():

created = 0
for collection in collections_config:

service_ids = collection.pop('service_ids')
service_ids = collection.pop('service_ids')
existing = None

for service_id in service_ids:
existing = app.taxii_server.persistence.get_collection(
collection['name'],
service_id)
if existing:
break

existing = None
# To keep things simple we assume here that collection name
# is unique globally so we first check if it already exists.
for service_id in service_ids:
existing = server.persistence.get_collection(
collection['name'],
service_id)
if existing:
break
log.warning(
"collection.skipped.already_exists",
collection_name=collection['name'],
existing_id=existing.id)
continue

c = app.taxii_server.persistence.create_collection(
CollectionEntity(**collection))

if existing:
log.warning(
"collection.skipped.already_exists",
collection_name=collection['name'],
existing_id=existing.id)
continue
app.taxii_server.persistence.attach_collection_to_services(
c.id, service_ids=service_ids)

created += 1


def delete_content_blocks():

parser = argparse.ArgumentParser(
description=(
"Delete content blocks from specified collections "
"with timestamp labels matching defined time window"),
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)

parser.add_argument(
"-c", "--collection", action="append", dest="collection",
help="Collection to remove content blocks from", required=True)

parser.add_argument(
"--begin", dest="begin",
help="exclusive beginning of time window as ISO8601 formatted date",
required=True)

parser.add_argument(
"--end", dest="end",
help="inclusive ending of time window as ISO8601 formatted date")

args = parser.parse_args()

entity = CollectionEntity(**collection)
with app.app_context():

c = server.persistence.create_collection(entity)
server.persistence.attach_collection_to_services(
c.id, service_ids=service_ids)
created += 1
start_time = args.begin
end_time = args.end

log.info("Collections created", count=created)
for collection in args.collection:
app.taxii_server.persistence.delete_content_blocks(
collection, start_time=start_time, end_time=end_time)
@@ -1,18 +1,6 @@

from opentaxii.server import TAXIIServer
from opentaxii.config import ServerConfig
from opentaxii.middleware import create_app
from opentaxii.utils import configure_logging
from opentaxii.cli import app


def run_in_dev_mode():

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

server = TAXIIServer(config)

app = create_app(server)
app.debug = True

app.run(port=9000)
@@ -9,7 +9,8 @@ class Account(object):
:param dict details: additional details of an account
'''

def __init__(self, id, **details):
def __init__(self, id, username, **details):

self.id = id
self.username = username
self.details = details
@@ -209,3 +209,14 @@ def get_domain(self, service_id):
:param str service_id: ID of a service
'''
return None

def delete_content_blocks(self, collection_name, start_time,
end_time=None):
'''Delete content blocks in a specified collection with
timestamp label in a specified time frame.
:param str collection_name: collection name
:param datetime start_time: exclusive beginning of a timeframe
:param datetime end_time: inclusive end of a timeframe
'''
pass
@@ -1,9 +1,12 @@
import structlog
from opentaxii.signals import (
CONTENT_BLOCK_CREATED, INBOX_MESSAGE_CREATED,
SUBSCRIPTION_CREATED
)
from opentaxii.taxii.converters import blob_to_service_entity

log = structlog.getLogger(__name__)


class PersistenceManager(object):
'''Manager responsible for persisting and retrieving data.
@@ -44,7 +47,9 @@ def create_collection(self, entity):
NOTE: Additional method that is only used in the helper scripts
shipped with OpenTAXII.
'''
return self.api.create_collection(entity)
collection = self.api.create_collection(entity)
log.info("collection.created", collection=collection.name)
return collection

def create_services_from_object(self, services_config):
'''Create services from configuration object and persis them.
@@ -54,7 +59,10 @@ def create_services_from_object(self, services_config):
'''

for blob in services_config:
self.create_service(blob_to_service_entity(blob))
service = blob_to_service_entity(blob)
self.create_service(service)

log.info("service.created", id=service.id, type=service.type)

# =================================================================

@@ -280,3 +288,25 @@ def get_domain(self, service_id):
:param str service_id: ID of a service
'''
return self.api.get_domain(service_id)

def delete_content_blocks(self, collection_name, start_time,
end_time=None):
'''Delete content blocks in a specified collection with
timestamp label in a specified time frame.
:param str collection_name: collection name
:param datetime start_time: exclusive beginning of a timeframe
:param datetime end_time: inclusive end of a timeframe
:return: the count of rows deleted
:rtype: int
'''
count = self.api.delete_content_blocks(
collection_name, start_time, end_time=end_time)

log.info(
"collection.content_blocks.deleted",
collection=collection_name,
count=count)

return count
Oops, something went wrong.

0 comments on commit dc6fddc

Please sign in to comment.