diff --git a/api.py b/api.py index ee83b44a3d046eae5f2e7ea6df1e8a1d65ba19f7..1b0724e2b77e27259cc6cd8e6fbbf33d6afd9579 100644 --- a/api.py +++ b/api.py @@ -2,7 +2,7 @@ from flask import Flask from flask_cors import CORS from flask_restful import Api -from endpoints.clients import Client, ClientList +from endpoints.client import Client, ClientList from endpoints.notify import Notify from endpoints.receive import Receive from endpoints.send import Send @@ -30,7 +30,9 @@ def create_app(): return app -flask_host = os.getenv("FLASK_HOST", "localhost") # Sets to whatever MQ_HOST is, or defaults to localhost +flask_host = os.getenv( + "FLASK_HOST", "localhost" +) # Sets to whatever MQ_HOST is, or defaults to localhost if __name__ == "__main__": app = create_app() diff --git a/api_receive_test.py b/api_receive_test.py index cee13f44a1dc3f7b6ac9c5a8237d760519adc887..9414843d2b2450997830efd077633e0c3295161f 100644 --- a/api_receive_test.py +++ b/api_receive_test.py @@ -22,9 +22,7 @@ def test_get_receive( ) as mock_read_from_queue, app.test_client() as app_test_client: mock_read_from_queue.return_value = mock_read_from_queue_return auth_header = get_auth_header(app_test_client, mock_client_credentials) - response = app_test_client.get( - "/receive", headers=auth_header - ) + response = app_test_client.get("/receive", headers=auth_header) assert response.status_code == 200 mock_read_from_queue.assert_called_once_with( queue_name="client-1-inbox", max_msgs=10 @@ -48,7 +46,7 @@ def test_get_receive_max_messages( auth_header = get_auth_header(app_test_client, mock_client_credentials) max_messages = 2 response = app_test_client.get( - "/receive", query_string={ "max_messages": max_messages }, headers=auth_header + "/receive", query_string={"max_messages": max_messages}, headers=auth_header ) assert response.status_code == 200 mock_read_from_queue.assert_called_once_with( @@ -57,12 +55,8 @@ def test_get_receive_max_messages( assert response.json == mock_read_from_queue_return -@pytest.mark.usefixtures( - "mock_clients", "mock_read_from_queue_return" -) -def test_get_receive_no_token( - mock_clients, mock_read_from_queue_return -): +@pytest.mark.usefixtures("mock_clients", "mock_read_from_queue_return") +def test_get_receive_no_token(mock_clients, mock_read_from_queue_return): app = create_app() with patch( "builtins.open", mock_open(read_data=json.dumps(mock_clients)) @@ -70,19 +64,13 @@ def test_get_receive_no_token( receive, "read_from_queue" ) as mock_read_from_queue, app.test_client() as app_test_client: mock_read_from_queue.return_value = mock_read_from_queue_return - response = app_test_client.get( - "/receive" - ) + response = app_test_client.get("/receive") assert response.status_code == 403 mock_read_from_queue.assert_not_called() -@pytest.mark.usefixtures( - "mock_clients", "mock_read_from_queue_return" -) -def test_get_receive_invalid_token( - mock_clients, mock_read_from_queue_return -): +@pytest.mark.usefixtures("mock_clients", "mock_read_from_queue_return") +def test_get_receive_invalid_token(mock_clients, mock_read_from_queue_return): app = create_app() with patch( "builtins.open", mock_open(read_data=json.dumps(mock_clients)) @@ -91,8 +79,6 @@ def test_get_receive_invalid_token( ) as mock_read_from_queue, app.test_client() as app_test_client: mock_read_from_queue.return_value = mock_read_from_queue_return auth_header = {"Authorization": "made-up-token"} - response = app_test_client.get( - "/receive", headers=auth_header - ) + response = app_test_client.get("/receive", headers=auth_header) assert response.status_code == 403 mock_read_from_queue.assert_not_called() diff --git a/conftest.py b/conftest.py index bf5c70d672a87141879e4ff2e2c4b450ce6bb34b..9c47f5377324e892d869b2d2dfa0b2f0b805ee1d 100644 --- a/conftest.py +++ b/conftest.py @@ -97,6 +97,6 @@ def mock_read_from_queue_return(): return [ { "topic": "soar.client-1.something", - "message": "this is a pub/sub message from client-1" + "message": "this is a pub/sub message from client-1", } ] diff --git a/docker/Dockerfile b/docker/Dockerfile index f4b5099a261ba54308f848a9f0a60e289ce2525a..606b939c91e83cacbf12f7f874eda56ca05853c1 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -3,7 +3,6 @@ FROM python:alpine3.17 WORKDIR /app COPY requirements.txt requirements.txt -COPY requirements-dev.txt requirements-dev.txt -RUN pip install -r requirements-dev.txt +RUN pip install -r requirements.txt ENTRYPOINT [ "python" ] \ No newline at end of file diff --git a/endpoints/__pycache__/__init__.cpython-311.pyc b/endpoints/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..90dd255747839886af44be58d42c042fa8f675fb Binary files /dev/null and b/endpoints/__pycache__/__init__.cpython-311.pyc differ diff --git a/endpoints/__pycache__/__init__.cpython-38.pyc b/endpoints/__pycache__/__init__.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c8b93c8d85807055316845360929a85862f83d02 Binary files /dev/null and b/endpoints/__pycache__/__init__.cpython-38.pyc differ diff --git a/endpoints/__pycache__/auth_resource.cpython-311.pyc b/endpoints/__pycache__/auth_resource.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e50827f92efbacef42e988414c57f9d73cb00f66 Binary files /dev/null and b/endpoints/__pycache__/auth_resource.cpython-311.pyc differ diff --git a/endpoints/__pycache__/auth_resource.cpython-38.pyc b/endpoints/__pycache__/auth_resource.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..20e4e7d7a70221599174906dc5ad05d908690a83 Binary files /dev/null and b/endpoints/__pycache__/auth_resource.cpython-38.pyc differ diff --git a/endpoints/__pycache__/client.cpython-311.pyc b/endpoints/__pycache__/client.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..b6f4da28b00790f54bd8b6200260e22d5ee38452 Binary files /dev/null and b/endpoints/__pycache__/client.cpython-311.pyc differ diff --git a/endpoints/__pycache__/client.cpython-38.pyc b/endpoints/__pycache__/client.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..05c47bcbc025487d7530d08068ab23caa2d8c956 Binary files /dev/null and b/endpoints/__pycache__/client.cpython-38.pyc differ diff --git a/endpoints/__pycache__/clients.cpython-38.pyc b/endpoints/__pycache__/clients.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..0bb1d7b211061c49cee7960f75be1fe51d5a8816 Binary files /dev/null and b/endpoints/__pycache__/clients.cpython-38.pyc differ diff --git a/endpoints/__pycache__/notify.cpython-311.pyc b/endpoints/__pycache__/notify.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..9a3fd6de0a1d968e3f0cd5403364dcdf98698d8d Binary files /dev/null and b/endpoints/__pycache__/notify.cpython-311.pyc differ diff --git a/endpoints/__pycache__/notify.cpython-38.pyc b/endpoints/__pycache__/notify.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..4fdce693c7c1526f496cc73604a0050af63822ca Binary files /dev/null and b/endpoints/__pycache__/notify.cpython-38.pyc differ diff --git a/endpoints/__pycache__/receive.cpython-311.pyc b/endpoints/__pycache__/receive.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..b36021e5d3fd24567e69dff85bb568e124db584e Binary files /dev/null and b/endpoints/__pycache__/receive.cpython-311.pyc differ diff --git a/endpoints/__pycache__/receive.cpython-38.pyc b/endpoints/__pycache__/receive.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..dd67b0dde8db6e4278f0696febbee5f6991ed2a1 Binary files /dev/null and b/endpoints/__pycache__/receive.cpython-38.pyc differ diff --git a/endpoints/__pycache__/send.cpython-311.pyc b/endpoints/__pycache__/send.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..1649c8f9a0fbf2f5f5d2ae2ebe7592dcdc15ff91 Binary files /dev/null and b/endpoints/__pycache__/send.cpython-311.pyc differ diff --git a/endpoints/__pycache__/send.cpython-38.pyc b/endpoints/__pycache__/send.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..66c8d271b504ddbebdee621cb00e94ccb18a8256 Binary files /dev/null and b/endpoints/__pycache__/send.cpython-38.pyc differ diff --git a/endpoints/__pycache__/token.cpython-311.pyc b/endpoints/__pycache__/token.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..018291e92c31dee8f7dd10e6b632c5183d304bf5 Binary files /dev/null and b/endpoints/__pycache__/token.cpython-311.pyc differ diff --git a/endpoints/__pycache__/token.cpython-38.pyc b/endpoints/__pycache__/token.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e30786ca086d55c64bfe3fa9f53abe3a6588289c Binary files /dev/null and b/endpoints/__pycache__/token.cpython-38.pyc differ diff --git a/endpoints/clients.py b/endpoints/clients.py deleted file mode 100644 index 4acdc54e5712da79b694165ccb9375f152f7a297..0000000000000000000000000000000000000000 --- a/endpoints/clients.py +++ /dev/null @@ -1,140 +0,0 @@ -from flask_restful import Resource, request, abort -from marshmallow import Schema, fields -import json -import os -import random -import string - - -class ClientSchema(Schema): - client_id = fields.Str(required=True) - client_name = fields.Str(required=True) - subscription = fields.Str(required=True) - - -class ClientsFile: - file = "./data/clients.json" - mtime = 0 - clients = {} - parser = None - - def __init__(self): - self.get() - - def get(self): - try: - mtime = os.path.getmtime(self.file) - if mtime > self.mtime: - with open(self.file, "r") as client_file: - self.clients = json.load(client_file) - except FileNotFoundError as error: - self.clients = {} - self.save() - - return self.clients - - def find(self, client_id): - self.get() - if client_id in self.clients: - client = self.clients[client_id] - else: - client = None - return client - - def add(self, client): - client['secret'] = self.secret() - self.clients[client["client_id"]] = client - self.save() - return client - - def remove(self, client): - del self.clients[client["client_id"]] - self.save() - - def update(self, client_updates): - client = self.find(client_updates["client_id"]) - client.update(client_updates) - self.clients[client["client_id"]] = client - self.save() - return client - - def save(self): - try: - with open(self.file, "w") as client_file: - client_file.write(json.dumps(self.clients, indent=2)) - return True - except OSError as error: - print(str(error)) - return False - - def secret(self, chars=36): - res = "".join( - random.choices( - string.ascii_lowercase + string.ascii_uppercase + string.digits, k=chars - ) - ) - return str(res) - -clients_file = ClientsFile() - -# Client -class Client(Resource): - clients_file = None - def __init__(self): - self.schema = ClientSchema() - self.clients_file = ClientsFile() - - def get(self, client_id): - client = self.clients_file.find(client_id) - del client['secret'] - if not client: - abort(404, message="No client with id: {}".format(client_id)) - return client - - def delete(self, todo_id): - client = self.clients_file.find(client_id) - if not client: - abort(404, message="No client with id: {}".format(client_id)) - else: - self.clients_file.remove(client) - return client, 204 - - def put(self, client_id): - args = request.get_json() - errors = self.schema.validate(args) - if errors: - abort(400, message=str(errors)) - - client = self.clients_file.find(client_id) - if not client: - abort(404, message="No client with id: {}".format(client_id)) - else: - client = self.clients_file.update(args) - return client, 201 - - -# ClientList -class ClientList(Resource): - def __init__(self): - self.schema = ClientSchema() - self.clients_file = ClientsFile() - - def get(self): - return { - client_id: (client, client.pop("secret", None))[0] - for client_id, client in self.clients_file.get().items() - } - - def post(self): - args = request.get_json() - - errors = self.schema.validate(args) - if errors: - abort(400, message=str(errors)) - - client = self.clients_file.find(args["client_id"]) - if client: - abort(403, message="Duplicate client id: {}".format(client_id)) - else: - client = self.clients_file.add(args) - return client, 201 diff --git a/endpoints/token.py b/endpoints/token.py index 7d1f28e468f3a18e907516f52b2749482d51902b..5603157d1e1e9b285ff4ff76945de6364e9d0780 100644 --- a/endpoints/token.py +++ b/endpoints/token.py @@ -3,6 +3,7 @@ from flask_restful import Resource, request, abort from marshmallow import Schema, fields from models.token_model import TokenModel + class TokenQuerySchema(Schema): client_id = fields.Str(required=True) secret = fields.Str(required=True) diff --git a/models/__pycache__/client_model.cpython-311.pyc b/models/__pycache__/client_model.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..622561eadf895fc0b06f7a2fee3c4ac811661d05 Binary files /dev/null and b/models/__pycache__/client_model.cpython-311.pyc differ diff --git a/models/__pycache__/client_model.cpython-38.pyc b/models/__pycache__/client_model.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..1c573ddf424ba88e642baef944331b5341c91a6e Binary files /dev/null and b/models/__pycache__/client_model.cpython-38.pyc differ diff --git a/models/__pycache__/client_model_test.cpython-38-pytest-7.2.1.pyc b/models/__pycache__/client_model_test.cpython-38-pytest-7.2.1.pyc new file mode 100644 index 0000000000000000000000000000000000000000..329ff8c5c7f6b2bab5240ec7e120c5f2a74120fd Binary files /dev/null and b/models/__pycache__/client_model_test.cpython-38-pytest-7.2.1.pyc differ diff --git a/models/__pycache__/token_model.cpython-311.pyc b/models/__pycache__/token_model.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..ec10ad519902561aef1216452ece68756638e068 Binary files /dev/null and b/models/__pycache__/token_model.cpython-311.pyc differ diff --git a/models/__pycache__/token_model.cpython-38.pyc b/models/__pycache__/token_model.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e545ab00353e0df56f573df8d4a4df3564fd7a44 Binary files /dev/null and b/models/__pycache__/token_model.cpython-38.pyc differ diff --git a/models/__pycache__/token_model_test.cpython-38-pytest-7.2.1.pyc b/models/__pycache__/token_model_test.cpython-38-pytest-7.2.1.pyc new file mode 100644 index 0000000000000000000000000000000000000000..9b01b09d8e08a723ec952c11c5e7da6b43aa73f5 Binary files /dev/null and b/models/__pycache__/token_model_test.cpython-38-pytest-7.2.1.pyc differ diff --git a/models/client_model.py b/models/client_model.py index ec522194695c9fa3638a043ac119ce3c4696a372..ec170ec272bff1cec02560e9544d1dd93987465d 100644 --- a/models/client_model.py +++ b/models/client_model.py @@ -18,7 +18,7 @@ import string class ClientModel: - file = "clients.json" + file = "./data/clients.json" mtime = 0 clients = {} parser = None diff --git a/models/client_model_test.py b/models/client_model_test.py index 2791092d6213c3f65c48fc6a02fc78aa4492243a..c56de826ccb26074ef365e2c75dd5ae1fa48d8e8 100644 --- a/models/client_model_test.py +++ b/models/client_model_test.py @@ -42,10 +42,8 @@ def test_get_modified(mock_clients): with patch( "builtins.open", mock_open(read_data=json.dumps(mock_clients)) ) as mock_file_open, patch("os.path.getmtime", return_value=3) as mock_getmtime: - print(os.path.getmtime("clients.json")) clients_file = ClientModel() mock_getmtime.return_value = 4 - print(os.path.getmtime("clients.json")) clients = clients_file.get() assert mock_file_open.call_count == 2 @@ -115,9 +113,9 @@ def test_save(mock_clients): "builtins.open", mock_open(read_data=json.dumps(mock_clients)) ) as mock_file_open: clients_file = ClientModel() - mock_file_open.assert_any_call("clients.json", "r") + mock_file_open.assert_any_call("./data/clients.json", "r") clients_file.save() - mock_file_open.assert_any_call("clients.json", "w") + mock_file_open.assert_any_call("./data/clients.json", "w") mock_file_open().write.has_any_call(json.dumps(mock_clients)) diff --git a/rmq.py b/rmq.py index 719835eaf7e3cf938198c28bf94a09f71b6b6948..d66f036022aab7366d0b5e9fecebd94d5253f77c 100644 --- a/rmq.py +++ b/rmq.py @@ -3,7 +3,9 @@ import os import pika -host = os.getenv("MQ_HOST", "localhost") # Sets to whatever MQ_HOST is, or defaults to localhost +host = os.getenv( + "MQ_HOST", "localhost" +) # Sets to whatever MQ_HOST is, or defaults to localhost # ------------------------------------------------------------------------------------------------------------------------------------------------------------- @@ -17,8 +19,14 @@ def pika_connect(host): if connection is not None: channel = connection.channel() else: - print("ERROR: Pika has been unable to connect to host '%s'. Is RabbitMQ running?" % host) - raise Exception("ERROR: Pika has been unable to connect to host '%s'. Is RabbitMQ running?" % host) + print( + "ERROR: Pika has been unable to connect to host '%s'. Is RabbitMQ running?" + % host + ) + raise Exception( + "ERROR: Pika has been unable to connect to host '%s'. Is RabbitMQ running?" + % host + ) return connection, channel diff --git a/soar_bus.py b/soar_bus.py index 741f6af4dfc575b783b350d8daa3885a7e346030..ccb03fe161f3eab9d5c7e1960131097b1d8543f3 100644 --- a/soar_bus.py +++ b/soar_bus.py @@ -65,5 +65,6 @@ def main(): except Exception as e: print(e) + if __name__ == "__main__": main()