Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

simplify removing UserInfo from events prior to logging #795

Merged
5 commits merged into from
Apr 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
57 changes: 21 additions & 36 deletions src/api-service/__app__/onefuzzlib/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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()