diff --git a/dreadnode_cli/agent/cli.py b/dreadnode_cli/agent/cli.py index 36088c8..fc7625f 100644 --- a/dreadnode_cli/agent/cli.py +++ b/dreadnode_cli/agent/cli.py @@ -255,7 +255,8 @@ 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") @@ -263,7 +264,13 @@ def push( 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() diff --git a/dreadnode_cli/agent/docker.py b/dreadnode_cli/agent/docker.py index 35e82c6..86cd320 100644 --- a/dreadnode_cli/agent/docker.py +++ b/dreadnode_cli/agent/docker.py @@ -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 diff --git a/dreadnode_cli/agent/tests/test_docker.py b/dreadnode_cli/agent/tests/test_docker.py index 979ce5c..b66a02a 100644 --- a/dreadnode_cli/agent/tests/test_docker.py +++ b/dreadnode_cli/agent/tests/test_docker.py @@ -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"