diff --git a/src/api-service/__app__/onefuzzlib/events.py b/src/api-service/__app__/onefuzzlib/events.py index 716509c9d4..7546279376 100644 --- a/src/api-service/__app__/onefuzzlib/events.py +++ b/src/api-service/__app__/onefuzzlib/events.py @@ -6,7 +6,7 @@ import json import logging from queue import Empty, Queue -from typing import List, Optional, Set +from typing import Optional from onefuzztypes.events import Event, EventMessage, EventType, get_event_type from onefuzztypes.models import UserInfo @@ -37,53 +37,38 @@ def get_events() -> Optional[str]: def log_event(event: Event, event_type: EventType) -> None: scrubbed_event = filter_event(event, event_type) - logging.info("sending event: %s - %s", event_type, scrubbed_event) - + logging.info( + "sending event: %s - %s", event_type, scrubbed_event.json(exclude_none=True) + ) -def filter_event(event: Event, event_type: EventType) -> Event: +def filter_event(event: Event, event_type: EventType) -> BaseModel: clone_event = event.copy(deep=True) - filter_event_recurs(clone_event) - - return clone_event + filtered_event = filter_event_recurse(clone_event) + return filtered_event -def filter_event_recurs(clone_event: BaseModel, visited: Set[int] = set()) -> BaseModel: +def filter_event_recurse(entry: BaseModel) -> BaseModel: - if id(clone_event) in visited: - return clone_event - - visited.add(id(clone_event)) - - for field in clone_event.__fields__: - field_data = getattr(clone_event, field) + for field in entry.__fields__: + field_data = getattr(entry, field) if isinstance(field_data, UserInfo): - field_data = None - - elif isinstance(field_data, List): - - if len(field_data) > 0 and not isinstance(field_data[0], BaseModel): - continue - for data in field_data: - filter_event_recurs(data, visited) - + elif isinstance(field_data, list): + for (i, value) in enumerate(field_data): + if isinstance(value, BaseModel): + field_data[i] = filter_event_recurse(value) elif isinstance(field_data, dict): + for (key, value) in field_data.items(): + if isinstance(value, BaseModel): + field_data[key] = filter_event_recurse(value) + elif isinstance(field_data, BaseModel): + field_data = filter_event_recurse(field_data) - for key in field_data: - if not isinstance(field_data[key], BaseModel): - continue - filter_event_recurs(field_data[key], visited) - - else: - - if isinstance(field_data, BaseModel): - filter_event_recurs(field_data, visited) - - setattr(clone_event, field, field_data) + setattr(entry, field, field_data) - return clone_event + return entry def send_event(event: Event) -> None: diff --git a/src/api-service/tests/test_secret_filter.py b/src/api-service/tests/test_userinfo_filter.py similarity index 92% rename from src/api-service/tests/test_secret_filter.py rename to src/api-service/tests/test_userinfo_filter.py index 553d18b503..1b8a45845f 100644 --- a/src/api-service/tests/test_secret_filter.py +++ b/src/api-service/tests/test_userinfo_filter.py @@ -1,76 +1,76 @@ -#!/usr/bin/env python -# -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. - - -import unittest -from uuid import uuid4 - -from onefuzztypes.enums import ContainerType, TaskType -from onefuzztypes.events import EventTaskCreated, get_event_type -from onefuzztypes.models import ( - TaskConfig, - TaskContainers, - TaskDetails, - TaskPool, - UserInfo, -) -from onefuzztypes.primitives import Container, PoolName - -from __app__.onefuzzlib.events import filter_event - - -class TestSecretFilter(unittest.TestCase): - def test_secret_filter(self) -> None: - job_id = uuid4() - task_id = uuid4() - application_id = uuid4() - object_id = uuid4() - upn = "testalias@contoso.com" - - user_info = UserInfo( - application_id=application_id, object_id=object_id, upn=upn - ) - - task_config = TaskConfig( - job_id=job_id, - containers=[ - TaskContainers( - type=ContainerType.inputs, name=Container("test-container") - ) - ], - tags={}, - task=TaskDetails( - type=TaskType.libfuzzer_fuzz, - duration=12, - target_exe="fuzz.exe", - target_env={}, - target_options=[], - ), - pool=TaskPool(count=2, pool_name=PoolName("test-pool")), - ) - - test_event = EventTaskCreated( - job_id=job_id, - task_id=task_id, - config=task_config, - user_info=user_info, - ) - - control_test_event = EventTaskCreated( - job_id=job_id, - task_id=task_id, - config=task_config, - user_info=None, - ) - - test_event_type = get_event_type(test_event) - - scrubbed_test_event = filter_event(test_event, test_event_type) - - self.assertEqual(scrubbed_test_event, control_test_event) - - -if __name__ == "__main__": - unittest.main() +#!/usr/bin/env python +# +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + + +import unittest +from uuid import uuid4 + +from onefuzztypes.enums import ContainerType, TaskType +from onefuzztypes.events import EventTaskCreated, get_event_type +from onefuzztypes.models import ( + TaskConfig, + TaskContainers, + TaskDetails, + TaskPool, + UserInfo, +) +from onefuzztypes.primitives import Container, PoolName + +from __app__.onefuzzlib.events import filter_event + + +class TestUserInfoFilter(unittest.TestCase): + def test_user_info_filter(self) -> None: + job_id = uuid4() + task_id = uuid4() + application_id = uuid4() + object_id = uuid4() + upn = "testalias@contoso.com" + + user_info = UserInfo( + application_id=application_id, object_id=object_id, upn=upn + ) + + task_config = TaskConfig( + job_id=job_id, + containers=[ + TaskContainers( + type=ContainerType.inputs, name=Container("test-container") + ) + ], + tags={}, + task=TaskDetails( + type=TaskType.libfuzzer_fuzz, + duration=12, + target_exe="fuzz.exe", + target_env={}, + target_options=[], + ), + pool=TaskPool(count=2, pool_name=PoolName("test-pool")), + ) + + test_event = EventTaskCreated( + job_id=job_id, + task_id=task_id, + config=task_config, + user_info=user_info, + ) + + control_test_event = EventTaskCreated( + job_id=job_id, + task_id=task_id, + config=task_config, + user_info=None, + ) + + test_event_type = get_event_type(test_event) + + scrubbed_test_event = filter_event(test_event, test_event_type) + + self.assertEqual(scrubbed_test_event, control_test_event) + + +if __name__ == "__main__": + unittest.main()