Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(example): support custom string in redact_sensitive script #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 12 additions & 10 deletions examples/redact_sensitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

Issues/improvements:
- If an event matches the sensitive string, only the sensitive field will be redacted (so if the title matches but not the URL, the URL will remain unredacted)
- One might not want to redact to the non-informative 'REDACTED', but instead to a string with secret meaning.
- No preview of the events/strings to be redacted.
"""

Expand All @@ -24,7 +23,6 @@

aw: ActivityWatchClient

REDACTED = "REDACTED"
DRYRUN = True


Expand Down Expand Up @@ -59,6 +57,10 @@ def main():
input("Enter a regex indicating sensitive content: ").lower()
)

new_string = input("Enter the string used to replace sensitive content: ")
if not new_string:
new_string = "REDACTED"

print("")
if DRYRUN:
print(
Expand All @@ -74,12 +76,12 @@ def main():
for bucket_id in buckets.keys():
if bucket_id.startswith("aw-watcher-afk"):
continue
_redact_bucket(bucket_id, pattern)
_redact_bucket(bucket_id, pattern, new_string)
else:
_redact_bucket(bid_to_redact, pattern)
_redact_bucket(bid_to_redact, pattern, new_string)


def _redact_bucket(bucket_id: str, pattern: Union[str, Pattern]):
def _redact_bucket(bucket_id: str, pattern: Union[str, Pattern], replace: str):
print(f"\nChecking bucket: {bucket_id}")

global aw
Expand All @@ -91,13 +93,13 @@ def _redact_bucket(bucket_id: str, pattern: Union[str, Pattern]):
return

yes_redact = input(
f"Do you want to replace all the matching strings with '{REDACTED}'? (y/N): "
f"Do you want to replace all the matching strings with '{replace}'? (y/N): "
)
if yes_redact == "y":
for e in events:
if e.id in sensitive_ids:
e_before = e
e = _redact_event(e, pattern)
e = _redact_event(e, pattern, replace)
print(f"\nData before: {e_before.data}")
print(f"Data after: {e.data}")

Expand All @@ -121,16 +123,16 @@ def _check_event(e: Event, pattern: Union[str, Pattern]) -> bool:
return False


def _redact_event(e: Event, pattern: Union[str, Pattern]) -> Event:
def _redact_event(e: Event, pattern: Union[str, Pattern], replace: str) -> Event:
e = deepcopy(e)
for k, v in e.data.items():
if isinstance(v, str):
if isinstance(pattern, str):
if pattern in v.lower():
e.data[k] = REDACTED
e.data[k] = replace
else:
if pattern.findall(v.lower()):
e.data[k] = REDACTED
e.data[k] = replace
return e


Expand Down