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

Fix username anonymization #70

Merged
merged 3 commits into from
Aug 30, 2023
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
10 changes: 7 additions & 3 deletions ansible_anonymizer/anonymizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,16 @@ def hide_user_name(block: str) -> str:
}

def _rewrite(m: re.Match[str]) -> str:
user = m.group("user_name") if m.group("user_name") in known_users else "ano-user"
user = (
m.group("user_name")
if (m.group("user_name") in known_users or is_jinja2_expression(m.group("user_name")))
else "ano-user"
)
return m.group("before") + user

user_regexes = [
r"(?P<before>[c-z]:\\users\\)(?P<user_name>\w{,255})",
r"(?P<before>/(home|Users)/)(?P<user_name>[a-z0-9_-]{,255})",
r"(?P<before>[c-z]:\\users\\)(?P<user_name>(\w|{{\s*.*?\s*}}){,255})",
r"(?P<before>/(home|Users)/)(?P<user_name>([a-z0-9_-]|{{\s*.*?\s*}}){,255})",
]
for regex in user_regexes:
block = re.sub(regex, _rewrite, block, flags=flags)
Expand Down
30 changes: 30 additions & 0 deletions tests/test_anonymizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,36 @@ def test_anonymize_text_block_user_name():
assert hide_user_name(dedent(source)) == dedent(expectation)


def test_anonymize_text_block_username_for_linux_path():
assert (
anonymize_text_block("path: /home/kaisersoze/.ssh/authorized_keys")
== "path: /home/ano-user/.ssh/authorized_keys"
)


def test_anonymize_text_block_username_for_windows_path():
assert (
anonymize_text_block("path: C:\\users\\kaisersoze\\test")
== "path: C:\\users\\ano-user\\test"
)


def test_anonymize_text_block_username_as_jinja_template_for_linux_path():
assert (
anonymize_text_block(
"path: /home/{{ admin_username | default('azureuser') }}/.ssh/authorized_keys"
)
== "path: /home/{{ admin_username | default('azureuser') }}/.ssh/authorized_keys"
)


def test_anonymize_text_block_username_as_jinja_template_for_windows_path():
assert (
anonymize_text_block("path: c:\\users\\{{ admin_username | default('azureuser') }}\\test")
== "path: c:\\users\\{{ admin_username | default('azureuser') }}\\test"
)


def test_anonymize_field():
field = "my_field"
value = " a "
Expand Down