Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

EOS-26021: S3 Audit Log: Expose an API to push audit logs #633

Merged
merged 6 commits into from
Dec 13, 2021
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
2 changes: 1 addition & 1 deletion py-utils/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def get_requirements_files() -> list:
'cortx.utils.shared_storage', 'cortx.utils.support_framework',
'cortx.utils.manifest', 'cortx.utils.setup.openldap',
'cortx.utils.setup.consul', 'cortx.utils.setup.elasticsearch',
'cortx.utils.cortx'
'cortx.utils.audit_log', 'cortx.utils.cortx',
],
package_data={
'cortx': ['py.typed'],
Expand Down
30 changes: 30 additions & 0 deletions py-utils/src/utils/audit_log/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env python3

# CORTX-Py-Utils: CORTX Python common library.
# Copyright (c) 2021 Seagate Technology LLC and/or its Affiliates
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# For any questions about this software or licensing,
# please email [email protected] or [email protected].

__title__ = 'audit_log'

from cortx.utils.audit_log.audit_log_server import AuditLogRequestHandler
from cortx.utils.audit_log.error import AuditLogError

__doc__ = """
Audit Log Server.

Audit log server can be used to send/recieve Audit logs to/from a webhook.
"""

# adds all above defined packages in import *
__all__ = ('AuditLogRequestHandler', 'AuditLogError')
63 changes: 63 additions & 0 deletions py-utils/src/utils/audit_log/audit_log_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python3

# CORTX-Py-Utils: CORTX Python common library.
# Copyright (c) 2021 Seagate Technology LLC and/or its Affiliates
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# For any questions about this software or licensing,
# please email [email protected] or [email protected].

import json
from aiohttp import web

from cortx.utils.utils_server import RestServer
from cortx.utils.audit_log.error import AuditLogError
from cortx.utils.utils_server.error import RestServerError
from cortx.utils.log import Log

routes = web.RouteTableDef()


class AuditLogRequestHandler(RestServer):
"""Rest interface of Audit log."""
@staticmethod
async def receive(request):
Log.debug("Received POST request for audit message")
try:
payload = await request.json()
messages = payload['messages']
# TODO Store audit logs messages to a persistent storage.
except AuditLogError as e:
rahul-tripathi-git marked this conversation as resolved.
Show resolved Hide resolved
status_code = e.rc
error_message = e.desc
Log.error(f"Unable to receive audit messages, status code: {status_code}," \
f" error: {error_message}")
response_obj = {'error_code': status_code, 'exception': \
['AuditLogError', {'message': error_message}]}
except Exception as e:
exception_key = type(e).__name__
exception = RestServerError(exception_key).http_error()
status_code = exception[0]
error_message = exception[1]
Log.error(f"Internal error while receiving audit messages." \
f"status code: {status_code}, error: {error_message}")
response_obj = {'error_code': status_code, 'exception': \
[exception_key, {'message': error_message}]}
raise AuditLogError(status_code, error_message) from e
else:
status_code = 200 # No exception, Success
response_obj = {}
Log.debug(f"Receiving audit messages using POST method finished with status " \
f"code: {status_code}")
response_obj = {'status_code': status_code, 'status': 'success'}
finally:
return web.Response(text=json.dumps(response_obj), \
status=status_code)
26 changes: 26 additions & 0 deletions py-utils/src/utils/audit_log/error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env python3

# CORTX-Py-Utils: CORTX Python common library.
# Copyright (c) 2021 Seagate Technology LLC and/or its Affiliates
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# For any questions about this software or licensing,
# please email [email protected] or [email protected].

from cortx.utils.errors import UtilsError


class AuditLogError(UtilsError):
""" Generic Exception with error code and output. """

def __init__(self, rc, message, *args):
"""Initialize AuditLogError."""
super().__init__(rc, message, *args)
6 changes: 5 additions & 1 deletion py-utils/src/utils/utils_server/utils_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ def __init__(self):
app = web.Application()
from cortx.utils.iem_framework import IemRequestHandler
from cortx.utils.message_bus import MessageBusRequestHandler
from cortx.utils.audit_log import AuditLogRequestHandler
app.add_routes([web.post('/EventMessage/event', IemRequestHandler.send), \
web.get('/EventMessage/event', IemRequestHandler.receive), \
web.post('/MessageBus/message/{message_type}', \
MessageBusRequestHandler.send), \
web.get('/MessageBus/message/{message_type}', \
MessageBusRequestHandler.receive)])
MessageBusRequestHandler.receive),
web.post('/AuditLog/message/', \
AuditLogRequestHandler.receive)
sachinpunadikar marked this conversation as resolved.
Show resolved Hide resolved
])

Log.info("Starting Message Server 0.0.0.0 on port 28300")
web.run_app(app, port=28300)
Expand Down
40 changes: 40 additions & 0 deletions py-utils/test/audit_log/test_audit_log_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python3

# CORTX Python common library.
# Copyright (c) 2021 Seagate Technology LLC and/or its Affiliates
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# For any questions about this software or licensing,
# please email [email protected] or [email protected].


import json
import unittest
import requests


class TestAuditLog(unittest.TestCase):
"""Test AuditLog rest server functionality."""
_base_url = 'http://127.0.0.1:28300/AuditLog/message/'

def test_send_audit_log(self):
"""Send Audit log message."""
url = self._base_url
data = json.dumps({'messages': ['Hello', 'How are you?']})
headers = {'content-type': 'application/json'}
response = requests.post(url=url, data=data, headers=headers)
self.assertEqual(response.json()['status'], 'success')
self.assertEqual(response.status_code, 200)


if __name__ == '__main__':
unittest.main()