-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29a6958
commit 1da1b53
Showing
14 changed files
with
347 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from guillotina import configure | ||
|
||
|
||
app_settings = { | ||
"load_utilities": { | ||
"audit": { | ||
"provides": "guillotina.contrib.audit.interfaces.IAuditUtility", | ||
"factory": "guillotina.contrib.audit.utility.AuditUtility", | ||
"settings": {"index_name": "audit"}, | ||
} | ||
} | ||
} | ||
|
||
|
||
def includeme(root, settings): | ||
configure.scan("guillotina.contrib.audit.install") | ||
configure.scan("guillotina.contrib.audit.utility") | ||
configure.scan("guillotina.contrib.audit.subscriber") | ||
configure.scan("guillotina.contrib.audit.api") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from guillotina import configure | ||
from guillotina.api.service import Service | ||
from guillotina.component import query_utility | ||
from guillotina.contrib.audit.interfaces import IAuditUtility | ||
from guillotina.interfaces import IContainer | ||
|
||
|
||
@configure.service( | ||
context=IContainer, | ||
method="GET", | ||
permission="guillotina.AccessAudit", | ||
name="@audit", | ||
summary="Get the audit entry logs", | ||
responses={"200": {"description": "Get the audit entry logs", "schema": {"properties": {}}}}, | ||
) | ||
class AuditGET(Service): | ||
async def __call__(self): | ||
audit_utility = query_utility(IAuditUtility) | ||
return await audit_utility.query_audit(self.request.query) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# -*- coding: utf-8 -*- | ||
from guillotina import configure | ||
from guillotina.addons import Addon | ||
from guillotina.component import query_utility | ||
from guillotina.contrib.audit.interfaces import IAuditUtility | ||
|
||
|
||
@configure.addon(name="audit", title="Guillotina Audit using ES") | ||
class ImageAddon(Addon): | ||
@classmethod | ||
async def install(cls, container, request): | ||
audit_utility = query_utility(IAuditUtility) | ||
await audit_utility.create_index() | ||
|
||
@classmethod | ||
async def uninstall(cls, container, request): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from guillotina.async_util import IAsyncUtility | ||
|
||
|
||
class IAuditUtility(IAsyncUtility): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from guillotina import configure | ||
from guillotina.component import query_utility | ||
from guillotina.contrib.audit.interfaces import IAuditUtility | ||
from guillotina.interfaces import IObjectAddedEvent | ||
from guillotina.interfaces import IObjectModifiedEvent | ||
from guillotina.interfaces import IObjectRemovedEvent | ||
from guillotina.interfaces import IResource | ||
|
||
|
||
@configure.subscriber(for_=(IResource, IObjectAddedEvent), priority=1001) # after indexing | ||
async def audit_object_added(obj, event): | ||
audit = query_utility(IAuditUtility) | ||
audit.log_entry(obj, event) | ||
|
||
|
||
@configure.subscriber(for_=(IResource, IObjectModifiedEvent), priority=1001) # after indexing | ||
async def audit_object_modified(obj, event): | ||
audit = query_utility(IAuditUtility) | ||
audit.log_entry(obj, event) | ||
|
||
|
||
@configure.subscriber(for_=(IResource, IObjectRemovedEvent), priority=1001) # after indexing | ||
async def audit_object_removed(obj, event): | ||
audit = query_utility(IAuditUtility) | ||
audit.log_entry(obj, event) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# -*- coding: utf-8 -*- | ||
from datetime import timezone | ||
from elasticsearch import AsyncElasticsearch | ||
from elasticsearch.exceptions import RequestError | ||
from guillotina import app_settings | ||
from guillotina.interfaces import IObjectAddedEvent | ||
from guillotina.interfaces import IObjectModifiedEvent | ||
from guillotina.interfaces import IObjectRemovedEvent | ||
from guillotina.utils.auth import get_authenticated_user | ||
from guillotina.utils.content import get_content_path | ||
|
||
import asyncio | ||
import datetime | ||
import json | ||
import logging | ||
|
||
|
||
logger = logging.getLogger("guillotina_audit") | ||
|
||
|
||
class AuditUtility: | ||
def __init__(self, settings=None, loop=None): | ||
self._settings = settings | ||
self.index = self._settings.get("index_name", "audit") | ||
self.loop = loop | ||
|
||
async def initialize(self, app): | ||
self.async_es = AsyncElasticsearch( | ||
loop=self.loop, **app_settings.get("elasticsearch", {}).get("connection_settings") | ||
) | ||
|
||
async def create_index(self): | ||
settings = {"settings": self.default_settings(), "mappings": self.default_mappings()} | ||
try: | ||
await self.async_es.indices.create(self.index, settings) | ||
except RequestError: | ||
logger.error("An exception occurred when creating index", exc_info=True) | ||
|
||
def default_settings(self): | ||
return { | ||
"analysis": { | ||
"analyzer": {"path_analyzer": {"tokenizer": "path_tokenizer"}}, | ||
"tokenizer": {"path_tokenizer": {"type": "path_hierarchy", "delimiter": "/"}}, | ||
"filter": {}, | ||
"char_filter": {}, | ||
} | ||
} | ||
|
||
def default_mappings(self): | ||
return { | ||
"dynamic": False, | ||
"properties": { | ||
"path": {"type": "text", "store": True, "analyzer": "path_analyzer"}, | ||
"type_name": {"type": "keyword", "store": True}, | ||
"uuid": {"type": "keyword", "store": True}, | ||
"action": {"type": "keyword", "store": True}, | ||
"creator": {"type": "keyword"}, | ||
"creation_date": {"type": "date", "store": True}, | ||
"payload": {"type": "text", "store": True}, | ||
}, | ||
} | ||
|
||
def log_entry(self, obj, event): | ||
document = {} | ||
user = get_authenticated_user() | ||
if IObjectModifiedEvent.providedBy(event): | ||
document["action"] = "modified" | ||
document["creation_date"] = obj.modification_date | ||
document["payload"] = json.dumps(event.payload) | ||
elif IObjectAddedEvent.providedBy(event): | ||
document["action"] = "added" | ||
document["creation_date"] = obj.creation_date | ||
elif IObjectRemovedEvent.providedBy(event): | ||
document["action"] = "removed" | ||
document["creation_date"] = datetime.datetime.now(timezone.utc) | ||
document["path"] = get_content_path(obj) | ||
document["creator"] = user.id | ||
document["type_name"] = obj.type_name | ||
document["uuid"] = obj.uuid | ||
coroutine = self.async_es.index(index=self.index, body=document) | ||
asyncio.create_task(coroutine) | ||
|
||
async def query_audit(self, params={}): | ||
if params == {}: | ||
query = {"query": {"match_all": {}}} | ||
else: | ||
query = {"query": {"bool": {"must": []}}} | ||
for field, value in params.items(): | ||
if ( | ||
field.endswith("__gte") | ||
or field.endswith("__lte") | ||
or field.endswith("__gt") | ||
or field.endswith("__lt") | ||
): | ||
field_parsed = field.split("__")[0] | ||
operator = field.split("__")[1] | ||
query["query"]["bool"]["must"].append({"range": {field_parsed: {operator: value}}}) | ||
else: | ||
query["query"]["bool"]["must"].append({"match": {field: value}}) | ||
return await self.async_es.search(index=self.index, body=query) | ||
|
||
async def close(self): | ||
if self.loop is not None: | ||
asyncio.run_coroutine_threadsafe(self.async_es.close(), self.loop) | ||
else: | ||
await self.async_es.close() | ||
|
||
async def finalize(self, app): | ||
await self.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from datetime import datetime | ||
from datetime import timedelta | ||
from guillotina.component import query_utility | ||
from guillotina.contrib.audit.interfaces import IAuditUtility | ||
|
||
import asyncio | ||
import json | ||
import pytest | ||
|
||
|
||
pytestmark = pytest.mark.asyncio | ||
|
||
|
||
@pytest.mark.app_settings({"applications": ["guillotina", "guillotina.contrib.audit"]}) | ||
async def test_audit_basic(es_requester): | ||
async with es_requester as requester: | ||
response, status = await requester("POST", "/db/guillotina/@addons", data=json.dumps({"id": "audit"})) | ||
assert status == 200 | ||
audit_utility = query_utility(IAuditUtility) | ||
# Let's check the index has been created | ||
resp = await audit_utility.async_es.indices.get_alias() | ||
assert "audit" in resp | ||
resp = await audit_utility.async_es.indices.get_mapping(index="audit") | ||
assert "path" in resp["audit"]["mappings"]["properties"] | ||
response, status = await requester( | ||
"POST", "/db/guillotina/", data=json.dumps({"@type": "Item", "id": "foo_item"}) | ||
) | ||
assert status == 201 | ||
await asyncio.sleep(2) | ||
resp, status = await requester("GET", "/db/guillotina/@audit") | ||
assert status == 200 | ||
assert len(resp["hits"]["hits"]) == 2 | ||
assert resp["hits"]["hits"][0]["_source"]["action"] == "added" | ||
assert resp["hits"]["hits"][0]["_source"]["type_name"] == "Container" | ||
assert resp["hits"]["hits"][0]["_source"]["creator"] == "root" | ||
|
||
assert resp["hits"]["hits"][1]["_source"]["action"] == "added" | ||
assert resp["hits"]["hits"][1]["_source"]["type_name"] == "Item" | ||
assert resp["hits"]["hits"][1]["_source"]["creator"] == "root" | ||
|
||
response, status = await requester("DELETE", "/db/guillotina/foo_item") | ||
await asyncio.sleep(2) | ||
resp, status = await requester("GET", "/db/guillotina/@audit") | ||
assert status == 200 | ||
assert len(resp["hits"]["hits"]) == 3 | ||
resp, status = await requester("GET", "/db/guillotina/@audit?action=removed") | ||
assert status == 200 | ||
assert len(resp["hits"]["hits"]) == 1 | ||
resp, status = await requester("GET", "/db/guillotina/@audit?action=removed&type_name=Item") | ||
assert status == 200 | ||
assert len(resp["hits"]["hits"]) == 1 | ||
resp, status = await requester("GET", "/db/guillotina/@audit?action=added&type_name=Item") | ||
assert status == 200 | ||
assert len(resp["hits"]["hits"]) == 1 | ||
assert resp["hits"]["hits"][0]["_source"]["type_name"] == "Item" | ||
resp, status = await requester("GET", "/db/guillotina/@audit?action=added&type_name=Container") | ||
assert status == 200 | ||
assert len(resp["hits"]["hits"]) == 1 | ||
assert resp["hits"]["hits"][0]["_source"]["type_name"] == "Container" | ||
creation_date = resp["hits"]["hits"][0]["_source"]["creation_date"] | ||
datetime_obj = datetime.strptime(creation_date, "%Y-%m-%dT%H:%M:%S.%f%z") | ||
new_creation_date = datetime_obj - timedelta(seconds=1) | ||
new_creation_date = new_creation_date.strftime("%Y-%m-%dT%H:%M:%S.%f%z") | ||
resp, status = await requester( | ||
"GET", | ||
f"/db/guillotina/@audit?action=added&type_name=Container&creation_date__gte={new_creation_date}", | ||
) # noqa | ||
assert status == 200 | ||
assert len(resp["hits"]["hits"]) == 1 | ||
resp, status = await requester( | ||
"GET", | ||
f"/db/guillotina/@audit?action=added&type_name=Container&creation_date__lte={new_creation_date}", | ||
) # noqa | ||
assert len(resp["hits"]["hits"]) == 0 | ||
assert status == 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.