Skip to content

Commit b46f836

Browse files
GitHKAndrei Neagu
authored andcommitted
🔒️ Strip credentials form image VCS (ITISFoundation#6433)
Co-authored-by: Andrei Neagu <[email protected]>
1 parent 04b118c commit b46f836

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

packages/service-integration/requirements/_base.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ jsonschema # pytest-plugin
1313
pytest # pytest-plugin
1414
pyyaml
1515
typer[all]
16+
yarl

packages/service-integration/requirements/_base.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ idna==3.7
3535
# via
3636
# email-validator
3737
# requests
38+
# yarl
3839
iniconfig==2.0.0
3940
# via pytest
4041
jinja2==3.1.4
@@ -57,6 +58,8 @@ markupsafe==2.1.5
5758
# via jinja2
5859
mdurl==0.1.2
5960
# via markdown-it-py
61+
multidict==6.1.0
62+
# via yarl
6063
orjson==3.10.7
6164
# via
6265
# -c requirements/../../../packages/models-library/requirements/../../../requirements/constraints.txt
@@ -121,3 +124,5 @@ urllib3==2.2.2
121124
# -c requirements/../../../requirements/constraints.txt
122125
# docker
123126
# requests
127+
yarl==1.12.1
128+
# via -r requirements/_base.in

packages/service-integration/src/service_integration/cli/_compose_spec.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import yaml
99
from models_library.utils.labels_annotations import to_labels
1010
from rich.console import Console
11+
from yarl import URL
1112

1213
from ..compose_spec_model import ComposeSpecification
1314
from ..errors import UndefinedOciImageSpecError
@@ -34,6 +35,13 @@ def _run_git(*args) -> str:
3435
).stdout.strip()
3536

3637

38+
def _strip_credentials(url: str) -> str:
39+
if (yarl_url := URL(url)) and yarl_url.is_absolute():
40+
stripped_url = URL(url).with_user(None).with_password(None)
41+
return f"{stripped_url}"
42+
return url
43+
44+
3745
def _run_git_or_empty_string(*args) -> str:
3846
try:
3947
return _run_git(*args)
@@ -118,8 +126,8 @@ def create_docker_compose_image_spec(
118126
extra_labels[f"{LS_LABEL_PREFIX}.vcs-ref"] = _run_git_or_empty_string(
119127
"rev-parse", "HEAD"
120128
)
121-
extra_labels[f"{LS_LABEL_PREFIX}.vcs-url"] = _run_git_or_empty_string(
122-
"config", "--get", "remote.origin.url"
129+
extra_labels[f"{LS_LABEL_PREFIX}.vcs-url"] = _strip_credentials(
130+
_run_git_or_empty_string("config", "--get", "remote.origin.url")
123131
)
124132

125133
return create_image_spec(
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
from service_integration.cli._compose_spec import _strip_credentials
3+
4+
5+
@pytest.mark.parametrize(
6+
"url, expected_url",
7+
[
8+
(
9+
"schema.veshttps://user:[email protected]/some/repo.git",
10+
"schema.veshttps://example.com/some/repo.git",
11+
),
12+
(
13+
"https://user:[email protected]/some/repo.git",
14+
"https://example.com/some/repo.git",
15+
),
16+
(
17+
"ssh://user:[email protected]/some/repo.git",
18+
"ssh://example.com/some/repo.git",
19+
),
20+
(
21+
"[email protected]:some/repo.git",
22+
"[email protected]:some/repo.git",
23+
),
24+
("any_str", "any_str"),
25+
],
26+
)
27+
def test__strip_credentials(url: str, expected_url: str):
28+
assert _strip_credentials(url) == expected_url

0 commit comments

Comments
 (0)