Skip to content

Commit

Permalink
feat: update regex to include more chars (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
tushar5526 authored Jun 18, 2024
1 parent 25e9ebf commit dc0ed06
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
32 changes: 19 additions & 13 deletions server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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})"
)

Expand Down Expand Up @@ -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 = (
Expand Down
30 changes: 16 additions & 14 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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):
Expand All @@ -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",
]


Expand Down Expand Up @@ -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):
Expand All @@ -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",
Expand Down

0 comments on commit dc0ed06

Please sign in to comment.