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

Headers and Documentation #108

Merged
merged 14 commits into from
Mar 26, 2020
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 .isort.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ known_third_party=flask,flask_httpauth,jwt,pymongo,pytest,pytz,six,werkzeug
known_first_party=medallion
not_skip=__init__.py
force_sort_within_sections=1
multi_line_output=5
41 changes: 20 additions & 21 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
|Build_Status| |Coverage| |Version|
|Build_Status| |Coverage| |Version| |Documentation_Status|

=========
medallion
=========

Expand Down Expand Up @@ -32,17 +31,15 @@ somewhat more robust and makes use of a MongoDB server, installed independently.
The MongoDB back-end can only be used if the pymongo python package is
installed. An error message will result if it is used without that package.


Installation
============
------------

The easiest way to install *medallion* is with pip::

$ pip install medallion


Usage
=====
-----

As a script
-----------
Expand Down Expand Up @@ -86,7 +83,7 @@ The <config_file> contains:

To use the Memory backend plugin, include the following in the <config-file>:

.. code:: json
.. code-block:: json

{
"backend": {
Expand All @@ -98,7 +95,7 @@ To use the Memory backend plugin, include the following in the <config-file>:

To use the directory features, include the following in the <config-file>:

.. code:: json
.. code-block:: json

{
"backend": {
Expand All @@ -117,7 +114,7 @@ into a single collection.

To use the MongoDB backend plugin, include the following in the <config-file>:

.. code:: json
.. code-block:: json

{
"backend": {
Expand All @@ -137,7 +134,7 @@ API Token authorization and JWT authorization. When stored in the <config-file>,

Here is an example:

.. code:: json
.. code-block:: json

{
"users": {
Expand All @@ -153,7 +150,7 @@ Here is an example:

*Note: the plaintext passwords for the above example are:*

.. code:: json
.. code-block:: json

{
"users": {
Expand All @@ -165,7 +162,7 @@ Here is an example:

If JWT authorization is used, a secret key is required in the config:

.. code:: json
.. code-block:: json

{
"flask": {
Expand All @@ -183,7 +180,7 @@ Authorization could be enhanced by changing the method "decorated" using

Configs may also contain a "taxii" section as well, as shown below:

.. code:: python
.. code-block:: json

{
"taxii": {
Expand All @@ -198,7 +195,7 @@ The backend for authorization can also be configured in the <config-file>:

To use the Memory Authorization backend plugin, include the following in the <config-file>:

.. code:: json
.. code-block:: json

{
"auth": {
Expand All @@ -211,7 +208,7 @@ To use the Memory Authorization backend plugin, include the following in the <co

To use the Mongo DB Authorization backend plugin, include the following in the <config-file>:

.. code:: json
.. code-block:: json

{
"auth": {
Expand All @@ -224,7 +221,7 @@ To use the Mongo DB Authorization backend plugin, include the following in the <

The structure expected by the mongo db authorization backend code is:

.. code:: json
.. code-block:: json

{
"user": {
Expand All @@ -248,7 +245,7 @@ A script for adding users and api-keys is included `auth_db_utils.py <https:meda

Multiple authorization are supported by *medallion* at the same time and can be added to the <config-file>:

.. code:: json
.. code-block:: json

{
"multi-auth": [
Expand All @@ -262,7 +259,7 @@ Additional configurations can be seen in `example_configs <https:/example_config
We welcome contributions for other back-end plugins.

Docker
======
------

We also provide a Docker image to make it easier to run *medallion*::

Expand All @@ -284,7 +281,7 @@ Replace ``<directory>`` with the full path to the directory containing your
medallion configuration.

Governance
==========
----------

This GitHub public repository (
**https://github.com/oasis-open/cti-taxii-client** ) was created at the request
Expand Down Expand Up @@ -341,7 +338,7 @@ additional or substitute Maintainers, per `consensus agreements
<https://www.oasis-open.org/resources/open-repositories/maintainers-guide#additionalMaintainers>`__.

Current Maintainers of this TC Open Repository
----------------------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- `Chris Lenk <mailto:clenk@mitre.org>`__; GitHub ID:
https://github.com/clenk/; WWW: `MITRE
Expand All @@ -355,7 +352,6 @@ Current Maintainers of this TC Open Repository
- `Jason Keirstead <mailto:Jason.Keirstead@ca.ibm.com>`__; GitHub ID:
https://github.com/JasonKeirstead; WWW: `IBM <http://www.ibm.com/>`__


About OASIS TC Open Repositories
--------------------------------

Expand Down Expand Up @@ -386,3 +382,6 @@ any specific CLA-related questions to repository-cla@oasis-open.org.
:target: https://codecov.io/gh/oasis-open/cti-taxii-server
.. |Version| image:: https://img.shields.io/pypi/v/medallion.svg?maxAge=3600
:target: https://pypi.python.org/pypi/medallion/
.. |Documentation_Status| image:: https://readthedocs.org/projects/medallion/badge/?version=latest
:target: https://medallion.readthedocs.io/en/latest/
:alt: Documentation Status
8 changes: 4 additions & 4 deletions medallion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ def register_blueprints(app):
from medallion.views.others.healthcheck import healthecheck_bp

log.debug("Registering medallion blueprints into {}".format(app))
app.register_blueprint(collections.mod)
app.register_blueprint(discovery.mod)
app.register_blueprint(manifest.mod)
app.register_blueprint(objects.mod)
app.register_blueprint(collections.collections_bp)
app.register_blueprint(discovery.discovery_bp)
app.register_blueprint(manifest.manifest_bp)
app.register_blueprint(objects.objects_bp)
app.register_blueprint(auth_bp)
app.register_blueprint(healthecheck_bp)

Expand Down
7 changes: 4 additions & 3 deletions medallion/backends/taxii/memory_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

from six import string_types

from ...common import (
create_bundle, datetime_to_string, determine_spec_version,
determine_version, generate_status, iterpath
)
from ...exceptions import ProcessingError
from ...filters.basic_filter import BasicFilter
from ...utils.common import (create_bundle, datetime_to_string,
determine_spec_version, determine_version,
generate_status, iterpath)
from .base import Backend

# Module-level logger
Expand Down
10 changes: 5 additions & 5 deletions medallion/backends/taxii/mongodb_backend.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import logging

from ...common import (
create_bundle, datetime_to_float, datetime_to_string,
datetime_to_string_stix, determine_spec_version, determine_version,
float_to_datetime, generate_status, string_to_datetime
)
from ...exceptions import MongoBackendError, ProcessingError
from ...filters.mongodb_filter import MongoDBFilter
from ...utils.common import (create_bundle, datetime_to_float,
datetime_to_string, datetime_to_string_stix,
determine_spec_version, determine_version,
float_to_datetime, generate_status,
string_to_datetime)
from .base import Backend

try:
Expand Down
2 changes: 1 addition & 1 deletion medallion/utils/common.py → medallion/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def generate_status(
):
"""
Generate Status Resource as defined in
`TAXII 2.0 section (4.3.1) <https://docs.google.com/document/d/1Jv9ICjUNZrOnwUXtenB1QcnBLO35RnjQcJLsa1mGSkI/pub#h.21tzry6u9dbz>`__.
`TAXII 2.0 Section (4.3.1) <http://docs.oasis-open.org/cti/taxii/v2.0/cs01/taxii-v2.0-cs01.html#_Toc496542732>`__.
"""
status = {
"id": str(uuid.uuid4()),
Expand Down
21 changes: 13 additions & 8 deletions medallion/filters/basic_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,19 @@ def process_filter(self, data, allowed, manifest_info):
added_after_date = self.filter_args.get("added_after")
if added_after_date:
new_results = []
for obj in results:
info = None
for item in manifest_info:
if item["id"] == obj["id"]:
info = item
break
if info:
if info["date_added"] > added_after_date:
if manifest_info is not None:
for obj in results:
info = None
for item in manifest_info:
if item["id"] == obj["id"]:
info = item
break
if info:
if info["date_added"] > added_after_date:
new_results.append(obj)
else:
for obj in results:
if obj["date_added"] > added_after_date:
new_results.append(obj)
return new_results
else:
Expand Down
3 changes: 2 additions & 1 deletion medallion/filters/mongodb_filter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ..utils.common import datetime_to_float, string_to_datetime
from medallion.common import datetime_to_float, string_to_datetime

from .basic_filter import BasicFilter


Expand Down
12 changes: 6 additions & 6 deletions medallion/scripts/auth_db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import six
from werkzeug.security import generate_password_hash

from medallion.utils.common import format_datetime, get_timestamp
from medallion.common import datetime_to_string, get_timestamp

try:
from pymongo import MongoClient
Expand Down Expand Up @@ -36,8 +36,8 @@ def add_auth_data_from_file(client, data):

timestamp = get_timestamp()

data['user']['created'] = format_datetime(timestamp)
data['api_key']['created'] = format_datetime(timestamp)
data['user']['created'] = datetime_to_string(timestamp)
data['api_key']['created'] = datetime_to_string(timestamp)

users.insert_one(data['user'])
api_keys.insert_one(data['api_key'])
Expand All @@ -49,7 +49,7 @@ def add_api_key_for_user(client, email):
api_key_obj = {
"_id": api_key,
"user_id": email,
"created": format_datetime(timestamp),
"created": datetime_to_string(timestamp),
"last_used_at": "",
"last_used_from": ""
}
Expand Down Expand Up @@ -119,8 +119,8 @@ def main():
"password": password_hash,
"company_name": company_name,
"contact_name": contact_name,
"created": format_datetime(timestamp),
"updated": format_datetime(timestamp),
"created": datetime_to_string(timestamp),
"updated": datetime_to_string(timestamp),
}

add_user(client, user)
Expand Down
6 changes: 5 additions & 1 deletion medallion/test/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from medallion import create_app
from medallion.test import config as test_configs
from medallion.test.data.initialize_mongodb import create_users, reset_db
from medallion.views import MEDIA_TYPE_TAXII_V20


class TaxiiTest(unittest.TestCase):
Expand Down Expand Up @@ -67,7 +68,10 @@ def setUp(self):

# TODO: It might be better to not reuse the test client.
self.client = self.app.test_client()
self.auth = {'Authorization': 'Token abc123'}
self.common_headers = {
'Accept': MEDIA_TYPE_TAXII_V20,
'Authorization': 'Token abc123'
}

def tearDown(self):
self.app_context.pop()
5 changes: 3 additions & 2 deletions medallion/test/data/initialize_mongodb.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from pymongo import ASCENDING, IndexModel

from medallion.common import datetime_to_float, string_to_datetime
from medallion.test.generic_initialize_mongodb import (
add_api_root, build_new_mongo_databases_and_collection, connect_to_client)
from medallion.utils.common import datetime_to_float, string_to_datetime
add_api_root, build_new_mongo_databases_and_collection, connect_to_client
)


def reset_db(url="mongodb://root:example@localhost:27017/"):
Expand Down
Loading