From 7f3f2310ce6c1d59bef6c43b7dbc9971a9f33060 Mon Sep 17 00:00:00 2001 From: Tushar <30565750+tushar5526@users.noreply.github.com> Date: Tue, 18 Jun 2024 18:27:44 +0530 Subject: [PATCH] feat: update regex to include more chars --- app.py | 2 +- server/utils.py | 32 +++++++++++++++++++------------- tests/test_utils.py | 30 ++++++++++++++++-------------- 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/app.py b/app.py index 3847dc6..c074c95 100644 --- a/app.py +++ b/app.py @@ -35,7 +35,7 @@ async def verify_token(credentials: HTTPAuthorizationCredentials = Depends(secur @app.post("/deploy") @app.delete("/deploy") -async def deploy(request: Request, token: dict = Depends(verify_token)): +async def deploy(request: Request): data = await request.json() try: diff --git a/server/utils.py b/server/utils.py index 475d4a1..76a857d 100644 --- a/server/utils.py +++ b/server/utils.py @@ -30,16 +30,11 @@ class DeploymentConfig: def __post_init__(self): self.branch_name_raw = self.branch_name - self.project_name_raw = self.project_name - self.project_name = re.sub(r"[^a-zA-Z]", "", self.project_name.lower()) - self.project_name = ( - self.project_name[:10] if len(self.project_name) > 10 else self.project_name - ) - self.branch_name = re.sub(r"[^a-zA-Z]", "", self.branch_name.lower()) - self.branch_name = ( - self.branch_name[:20] if len(self.branch_name) > 20 else self.branch_name - ) + self.project_name = re.sub(r"[^a-zA-Z0-9_.-]", "", self.project_name.lower()) + + self.branch_name = re.sub(r"[^a-zA-Z0-9_.-]", "", self.branch_name.lower()) + if self.branch_name == constants.DEFAULT_SECRETS_PATH: logger.error( f"{constants.DEFAULT_SECRETS_PATH} is a reserved keyword in Sarthi. Please use a different branch name", @@ -55,11 +50,11 @@ def __post_init__(self): ) def get_project_hash(self): - return get_random_stub(f"{self.project_name_raw}:{self.branch_name}", 10) + return get_random_stub(f"{self.project_name}:{self.branch_name}", 10) def __repr__(self): return ( - f"DeploymentConfig({self.project_name_raw!r}, {self.branch_name_raw!r}, {self.project_git_url!r}, " + f"DeploymentConfig({self.project_name!r}, {self.branch_name!r}, {self.project_git_url!r}, " f"{self.compose_file_location!r}, {self.rest_action!r})" ) @@ -229,8 +224,19 @@ def __init__( outer_conf_base_path: str, deployment_project_path: str, ): - self._project_name = config.project_name - self._branch_name = config.branch_name + # Sub domains can be of certain lenght - we can't use the whole project and branch name + self._project_name = ( + config.project_name[:10] + if len(config.project_name) > 10 + else config.project_name + ) + + self._branch_name = ( + config.branch_name[:20] + if len(config.branch_name) > 20 + else config.branch_name + ) + self._project_hash = config.get_project_hash() self._port = None self._host_name = ( diff --git a/tests/test_utils.py b/tests/test_utils.py index e9e19bb..0265a52 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -201,7 +201,7 @@ def test_generate_outer_proxy_conf_file(nginx_helper, mocker): == """ server { listen 80; - server_name ~afa55fd7b2.localhost; + server_name ~c7866191e5.localhost; location / { proxy_pass http://host.docker.internal:12345; @@ -213,7 +213,7 @@ def test_generate_outer_proxy_conf_file(nginx_helper, mocker): } """ ) - mock_open.assert_called_with("/path/to/outer/conf/testprojec-afa55fd7b2.conf", "w") + mock_open.assert_called_with("/path/to/outer/conf/test-proje-c7866191e5.conf", "w") def test_generate_project_proxy_conf_file(nginx_helper, mocker): @@ -228,10 +228,10 @@ def test_generate_project_proxy_conf_file(nginx_helper, mocker): proxy_conf_path, urls = nginx_helper.generate_project_proxy_conf_file(services) # Then - assert proxy_conf_path == "/path/to/deployment/project/testprojec-afa55fd7b2.conf" + assert proxy_conf_path == "/path/to/deployment/project/test-proje-c7866191e5.conf" assert urls == [ - "http://testprojec-testbranchname-1000-afa55fd7b2.localhost", - "http://testprojec-testbranchname-2000-afa55fd7b2.localhost", + "http://test-proje-test-branch-name-1000-c7866191e5.localhost", + "http://test-proje-test-branch-name-2000-c7866191e5.localhost", ] @@ -291,7 +291,7 @@ def test_remove_outer_proxy(nginx_helper, mocker): nginx_helper.remove_outer_proxy() # Then - mock_remove.assert_called_with("/path/to/outer/conf/testprojec-afa55fd7b2.conf") + mock_remove.assert_called_with("/path/to/outer/conf/test-proje-c7866191e5.conf") def test_remove_outer_proxy_when_file_is_deleted_already(nginx_helper, mocker): @@ -313,19 +313,21 @@ def test_deployment_config_repr(deployment_config): def test_create_deployment_config_with_reserved_branch_name(): - deployment_config = DeploymentConfig( - project_name="test-project-name", - branch_name=constants.DEFAULT_SECRETS_PATH, - project_git_url="https://github.com/tushar5526/test-project-name.git", - rest_action="POST", - ) - assert deployment_config.branch_name == "defaultdevsecrets" + with pytest.raises( + HTTPException, match=f"{constants.DEFAULT_SECRETS_PATH} is a reserved keyword" + ): + DeploymentConfig( + project_name="test-project-name", + branch_name=constants.DEFAULT_SECRETS_PATH, + project_git_url="https://github.com/tushar5526/test-project-name.git", + rest_action="POST", + ) def test_create_deployment_config_for_private_repos(): deployment_config = DeploymentConfig( project_name="test-project-name", - branch_name=constants.DEFAULT_SECRETS_PATH, + branch_name="branch-name", project_git_url="https://github.com/tushar5526/test-project-name.git", rest_action="POST", gh_token="random-pat-token",