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: sanitizing and normalizing the username for docker registry URI compliance (ENG-690) #28

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
11 changes: 9 additions & 2 deletions dreadnode_cli/agent/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,22 @@ def push(
print()
print(f":wrench: Building agent from [b]{directory}[/] ...")
image = docker.build(directory, force_rebuild=rebuild)
agent_name = docker.sanitized_agent_name(agent_config.project_name)
agent_name = docker.sanitized_name(agent_config.project_name)
sanitized_user_name = docker.sanitized_name(server_config.username)

if not agent_name:
raise Exception("Failed to sanitize agent name, please use a different name")

elif agent_name != agent_config.project_name:
print(f":four_leaf_clover: Agent name normalized to [bold magenta]{agent_name}[/]")

repository = f"{registry}/{server_config.username}/agents/{agent_name}"
if not sanitized_user_name:
raise Exception("Failed to sanitize username")

elif sanitized_user_name != server_config.username:
print(f":four_leaf_clover: Username normalized to [bold magenta]{sanitized_user_name}[/]")

repository = f"{registry}/{sanitized_user_name}/agents/{agent_name}"
tag = tag or image.id[-8:]

print()
Expand Down
4 changes: 2 additions & 2 deletions dreadnode_cli/agent/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ def login(registry: str, username: str, password: str) -> None:
client.api.login(username=username, password=password, registry=registry)


def sanitized_agent_name(name: str) -> str:
def sanitized_name(name: str) -> str:
"""
Sanitizes an agent name to be used as a Docker repository name.
Sanitizes an agent or user name to be used in a Docker repository URI.
"""

# convert to lowercase
Expand Down
28 changes: 14 additions & 14 deletions dreadnode_cli/agent/tests/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,26 +176,26 @@ def test_get_registry_custom_platform_base_domain() -> None:
assert docker.get_registry(config) == "dev-registry.example.com"


def test_sanitized_agent_name() -> None:
def test_sanitized_name() -> None:
# Test basic name
assert docker.sanitized_agent_name("test-agent") == "test-agent"
assert docker.sanitized_agent_name("test- agent") == "test-agent"
assert docker.sanitized_agent_name("test_agent") == "test_agent"
assert docker.sanitized_name("test-agent") == "test-agent"
assert docker.sanitized_name("test- agent") == "test-agent"
assert docker.sanitized_name("test_agent") == "test_agent"

# Test spaces
assert docker.sanitized_agent_name("test agent") == "test-agent"
assert docker.sanitized_agent_name("test multiple spaces") == "test-multiple-spaces"
assert docker.sanitized_name("test agent") == "test-agent"
assert docker.sanitized_name("test multiple spaces") == "test-multiple-spaces"

# Test special characters
assert docker.sanitized_agent_name("test!@#$%^&*()agent") == "testagent"
assert docker.sanitized_agent_name("test_agent.123") == "test_agent123"
assert docker.sanitized_agent_name("test/agent\\path") == "testagentpath"
assert docker.sanitized_name("test!@#$%^&*()agent") == "testagent"
assert docker.sanitized_name("test_agent.123") == "test_agent123"
assert docker.sanitized_name("test/agent\\path") == "testagentpath"

# Test mixed case
assert docker.sanitized_agent_name("TestAgent") == "testagent"
assert docker.sanitized_agent_name("TEST AGENT") == "test-agent"
assert docker.sanitized_name("TestAgent") == "testagent"
assert docker.sanitized_name("TEST AGENT") == "test-agent"

# Test edge cases
assert docker.sanitized_agent_name(" spaced name ") == "spaced-name"
assert docker.sanitized_agent_name("!!!###") == ""
assert docker.sanitized_agent_name("123 456") == "123-456"
assert docker.sanitized_name(" spaced name ") == "spaced-name"
assert docker.sanitized_name("!!!###") == ""
assert docker.sanitized_name("123 456") == "123-456"
Loading