From 35c2d25238d480be8335626eef995cc551b454a5 Mon Sep 17 00:00:00 2001 From: Andrei Neagu Date: Tue, 24 Sep 2024 14:51:52 +0200 Subject: [PATCH 1/4] added credentails stripping to vcs string --- .../service-integration/requirements/_base.in | 1 + .../requirements/_base.txt | 5 ++++ .../service_integration/cli/_compose_spec.py | 11 +++++++- .../tests/test_cli__compose_spec.py | 27 +++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 packages/service-integration/tests/test_cli__compose_spec.py diff --git a/packages/service-integration/requirements/_base.in b/packages/service-integration/requirements/_base.in index fee8aa856e24..6e288d49e0bf 100644 --- a/packages/service-integration/requirements/_base.in +++ b/packages/service-integration/requirements/_base.in @@ -13,3 +13,4 @@ jsonschema # pytest-plugin pytest # pytest-plugin pyyaml typer[all] +yarl diff --git a/packages/service-integration/requirements/_base.txt b/packages/service-integration/requirements/_base.txt index 131e231b5370..b745227bd5f9 100644 --- a/packages/service-integration/requirements/_base.txt +++ b/packages/service-integration/requirements/_base.txt @@ -35,6 +35,7 @@ idna==3.7 # via # email-validator # requests + # yarl iniconfig==2.0.0 # via pytest jinja2==3.1.4 @@ -57,6 +58,8 @@ markupsafe==2.1.5 # via jinja2 mdurl==0.1.2 # via markdown-it-py +multidict==6.1.0 + # via yarl orjson==3.10.7 # via # -c requirements/../../../packages/models-library/requirements/../../../requirements/constraints.txt @@ -121,3 +124,5 @@ urllib3==2.2.2 # -c requirements/../../../requirements/constraints.txt # docker # requests +yarl==1.12.1 + # via -r requirements/_base.in diff --git a/packages/service-integration/src/service_integration/cli/_compose_spec.py b/packages/service-integration/src/service_integration/cli/_compose_spec.py index 117a4afa5efb..0f6d9f756b60 100644 --- a/packages/service-integration/src/service_integration/cli/_compose_spec.py +++ b/packages/service-integration/src/service_integration/cli/_compose_spec.py @@ -8,6 +8,7 @@ import yaml from models_library.utils.labels_annotations import to_labels from rich.console import Console +from yarl import URL from ..compose_spec_model import ComposeSpecification from ..errors import UndefinedOciImageSpecError @@ -34,9 +35,17 @@ def _run_git(*args) -> str: ).stdout.strip() +def _strip_credentials(url: str) -> str: + yarl_url = URL(url) + if yarl_url.is_absolute(): + stripped_url = URL(url).with_user(None).with_password(None) + return f"{stripped_url}" + return url + + def _run_git_or_empty_string(*args) -> str: try: - return _run_git(*args) + return _strip_credentials(_run_git(*args)) except FileNotFoundError as err: error_console.print( "WARNING: Defaulting label to emtpy string", diff --git a/packages/service-integration/tests/test_cli__compose_spec.py b/packages/service-integration/tests/test_cli__compose_spec.py new file mode 100644 index 000000000000..a6c28111825c --- /dev/null +++ b/packages/service-integration/tests/test_cli__compose_spec.py @@ -0,0 +1,27 @@ +import pytest +from service_integration.cli._compose_spec import _strip_credentials + + +@pytest.mark.parametrize( + "url, expected_url", + [ + ( + "schema.veshttps://user:password@example.com/some/repo.git", + "schema.veshttps://example.com/some/repo.git", + ), + ( + "https://user:password@example.com/some/repo.git", + "https://example.com/some/repo.git", + ), + ( + "ssh://user:password@example.com/some/repo.git", + "ssh://example.com/some/repo.git", + ), + ( + "git@git.speag.com:some/repo.git", + "git@git.speag.com:some/repo.git", + ), + ], +) +def test__strip_credentials(url: str, expected_url: str): + assert _strip_credentials(url) == expected_url From b330d8b886c0ce164bcbde99b84263455922cdf9 Mon Sep 17 00:00:00 2001 From: Andrei Neagu Date: Tue, 24 Sep 2024 14:54:21 +0200 Subject: [PATCH 2/4] only apply to VCS --- .../src/service_integration/cli/_compose_spec.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/service-integration/src/service_integration/cli/_compose_spec.py b/packages/service-integration/src/service_integration/cli/_compose_spec.py index 0f6d9f756b60..2d43b9ca8c1e 100644 --- a/packages/service-integration/src/service_integration/cli/_compose_spec.py +++ b/packages/service-integration/src/service_integration/cli/_compose_spec.py @@ -45,7 +45,7 @@ def _strip_credentials(url: str) -> str: def _run_git_or_empty_string(*args) -> str: try: - return _strip_credentials(_run_git(*args)) + return _run_git(*args) except FileNotFoundError as err: error_console.print( "WARNING: Defaulting label to emtpy string", @@ -127,8 +127,8 @@ def create_docker_compose_image_spec( extra_labels[f"{LS_LABEL_PREFIX}.vcs-ref"] = _run_git_or_empty_string( "rev-parse", "HEAD" ) - extra_labels[f"{LS_LABEL_PREFIX}.vcs-url"] = _run_git_or_empty_string( - "config", "--get", "remote.origin.url" + extra_labels[f"{LS_LABEL_PREFIX}.vcs-url"] = _strip_credentials( + _run_git_or_empty_string("config", "--get", "remote.origin.url") ) return create_image_spec( From d489f19c9f03f795fd19de100bdc8660c2a1b7d8 Mon Sep 17 00:00:00 2001 From: Andrei Neagu Date: Tue, 24 Sep 2024 15:06:57 +0200 Subject: [PATCH 3/4] refactor --- .../src/service_integration/cli/_compose_spec.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/service-integration/src/service_integration/cli/_compose_spec.py b/packages/service-integration/src/service_integration/cli/_compose_spec.py index 2d43b9ca8c1e..a42936c36959 100644 --- a/packages/service-integration/src/service_integration/cli/_compose_spec.py +++ b/packages/service-integration/src/service_integration/cli/_compose_spec.py @@ -36,8 +36,7 @@ def _run_git(*args) -> str: def _strip_credentials(url: str) -> str: - yarl_url = URL(url) - if yarl_url.is_absolute(): + if (yarl_url := URL(url)) and yarl_url.is_absolute(): stripped_url = URL(url).with_user(None).with_password(None) return f"{stripped_url}" return url From 589cb99300670220ce5633f8cd7d9ed8015484dc Mon Sep 17 00:00:00 2001 From: Andrei Neagu Date: Tue, 24 Sep 2024 15:07:36 +0200 Subject: [PATCH 4/4] does not raise on any string --- packages/service-integration/tests/test_cli__compose_spec.py | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/service-integration/tests/test_cli__compose_spec.py b/packages/service-integration/tests/test_cli__compose_spec.py index a6c28111825c..5fe98689a14e 100644 --- a/packages/service-integration/tests/test_cli__compose_spec.py +++ b/packages/service-integration/tests/test_cli__compose_spec.py @@ -21,6 +21,7 @@ "git@git.speag.com:some/repo.git", "git@git.speag.com:some/repo.git", ), + ("any_str", "any_str"), ], ) def test__strip_credentials(url: str, expected_url: str):