Skip to content

Commit 71accbf

Browse files
authored
fix: rename domain to hostname in Git service configuration (#453)
Rename the attribute `domain` to `hostname` in Git service sections in Macaron's .ini config. --------- Signed-off-by: Nathan Nguyen <[email protected]>
1 parent b31e62d commit 71accbf

File tree

9 files changed

+69
-67
lines changed

9 files changed

+69
-67
lines changed

docs/source/pages/using.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ Analyzing a repository on a self-hosted GitLab instance
7878

7979
To analyze a repository on a self-hosted GitLab instance, you need to do the following:
8080

81-
- Add the following ``[git_service.gitlab.self_hosted]`` section into your ``.ini`` config. In the default .ini configuration (generated using ``macaron dump-default`` -- :ref:`see instructions <action_dump_defaults>`), there is already this section commented out. You can start by un-commenting this section and modifying the ``domain`` value with the domain of your self-hosted GitLab instance.
81+
- Add the following ``[git_service.gitlab.self_hosted]`` section into your ``.ini`` config. In the default .ini configuration (generated using ``macaron dump-default`` -- :ref:`see instructions <action_dump_defaults>`), there is already this section commented out. You can start by un-commenting this section and modifying the ``hostname`` value with the hostname of your self-hosted GitLab instance.
8282

8383
.. code-block:: ini
8484
8585
# Access to a self-hosted GitLab instance (e.g. your organization's self-hosted GitLab instance).
8686
# If this section is enabled, an access token must be provided through the ``MCN_SELF_HOSTED_GITLAB_TOKEN`` environment variable.
8787
# The `read_repository` permission is required for this token.
8888
[git_service.gitlab.self_hosted]
89-
domain = internal.gitlab.org
89+
hostname = internal.gitlab.org
9090
9191
- Obtain a GitLab access token having at least the ``read_repository`` permission and store it into the ``MCN_SELF_HOSTED_GITLAB_TOKEN`` environment variable. For more detailed instructions, see `GitLab documentation <https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html#create-a-personal-access-token>`_.
9292

@@ -100,7 +100,7 @@ To simplify the examples, we use the same configurations as above if needed (e.g
100100

101101
.. code-block::
102102
103-
pkg:<git_service_domain>/<organization>/<name>
103+
pkg:<git_service_hostname>/<organization>/<name>
104104
105105
The list bellow shows examples for the corresponding PURL strings for different git repositories:
106106

src/macaron/config/defaults.ini

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,25 @@ parent_limit = 10
5959
artifact_ignore_list =
6060

6161
# Git services that Macaron has access to clone repositories.
62-
# For security purposes, Macaron will only clone repositories from the domains specified.
62+
# For security purposes, Macaron will only clone repositories from the hostnames specified.
6363

6464
# Access to GitHub is required in most case for Macaron to analyse not only the main
6565
# repo but also its dependencies.
6666
[git_service.github]
67-
domain = github.com
67+
hostname = github.com
6868

6969
# Access to public GitLab (gitlab.com).
7070
# An optional access token can be provided through the `MCN_GITLAB_TOKEN` environment variable.
7171
# This access token is optional, only necessary when you need to clone private repositories.
7272
# The `read_repository` permission is required for this token.
7373
[git_service.gitlab.publicly_hosted]
74-
domain = gitlab.com
74+
hostname = gitlab.com
7575

7676
# Access to a self-hosted GitLab instance (e.g. your organization's self-hosted GitLab instance).
7777
# If this section is enabled, an access token must be provided through the `MCN_SELF_HOSTED_GITLAB_TOKEN` environment variable.
7878
# The `read_repository` permission is required for this token.
7979
# [git_service.gitlab.self_hosted]
80-
# domain = example.org
80+
# hostname = example.org
8181

8282
# This is the spec for trusted Maven build tools.
8383
[builder.maven]

src/macaron/slsa_analyzer/analyzer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ def add_component(
624624
The component is already analyzed in the same session.
625625
"""
626626
# Note: the component created in this function will be added to the database.
627-
available_domains = [git_service.domain for git_service in GIT_SERVICES if git_service.domain]
627+
available_domains = [git_service.hostname for git_service in GIT_SERVICES if git_service.hostname]
628628
try:
629629
analysis_target = Analyzer.to_analysis_target(config, available_domains)
630630
except InvalidPURLError as error:

src/macaron/slsa_analyzer/git_service/base_git_service.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ def __init__(self, name: str) -> None:
2424
The name of the git service.
2525
"""
2626
self.name = name
27-
self.domain: str | None = None
27+
self.hostname: str | None = None
2828

2929
@abstractmethod
3030
def load_defaults(self) -> None:
3131
"""Load the values for this git service from the ini configuration."""
3232

33-
def load_domain(self, section_name: str) -> str | None:
34-
"""Load the domain of the git service from the ini configuration section ``section_name``.
33+
def load_hostname(self, section_name: str) -> str | None:
34+
"""Load the hostname of the git service from the ini configuration section ``section_name``.
3535
3636
The section may or may not be available in the configuration. In both cases,
3737
the method should not raise ``ConfigurationError``.
3838
3939
Meanwhile, if the section is present but there is a schema violation (e.g. a key such as
40-
``domain`` is missing), this method will raise a ``ConfigurationError``.
40+
``hostname`` is missing), this method will raise a ``ConfigurationError``.
4141
4242
Parameters
4343
----------
@@ -47,7 +47,7 @@ def load_domain(self, section_name: str) -> str | None:
4747
Returns
4848
-------
4949
str | None
50-
The domain. This can be ``None`` if the git service section is not found in
50+
The hostname. This can be ``None`` if the git service section is not found in
5151
the ini configuration file, meaning the user does not enable the
5252
corresponding git service.
5353
@@ -61,17 +61,17 @@ def load_domain(self, section_name: str) -> str | None:
6161
# to have all available git services in the ini config.
6262
return None
6363
section = defaults[section_name]
64-
domain = section.get("domain")
65-
if not domain:
64+
hostname = section.get("hostname")
65+
if not hostname:
6666
raise ConfigurationError(
67-
f'The "domain" key is missing in section [{section_name}] of the .ini configuration file.'
67+
f'The "hostname" key is missing in section [{section_name}] of the .ini configuration file.'
6868
)
69-
return domain
69+
return hostname
7070

7171
def is_detected(self, url: str) -> bool:
7272
"""Check if the remote repo at the given ``url`` is hosted on this git service.
7373
74-
This check is done by checking the URL of the repo against the domain of this
74+
This check is done by checking the URL of the repo against the hostname of this
7575
git service.
7676
7777
Parameters
@@ -84,12 +84,12 @@ def is_detected(self, url: str) -> bool:
8484
bool
8585
True if the repo is indeed hosted on this git service.
8686
"""
87-
if self.domain is None:
87+
if self.hostname is None:
8888
return False
8989
return (
9090
git_url.parse_remote_url(
9191
url,
92-
allowed_git_service_domains=[self.domain],
92+
allowed_git_service_hostnames=[self.hostname],
9393
)
9494
is not None
9595
)

src/macaron/slsa_analyzer/git_service/github.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def load_defaults(self) -> None:
2929
If there is an error loading the configuration.
3030
"""
3131
try:
32-
self.domain = self.load_domain(section_name="git_service.github")
32+
self.hostname = self.load_hostname(section_name="git_service.github")
3333
except ConfigurationError as error:
3434
raise error
3535

src/macaron/slsa_analyzer/git_service/gitlab.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ def construct_clone_url(self, url: str) -> str:
6565
CloneError
6666
If there is an error parsing the URL.
6767
"""
68-
if not self.domain:
68+
if not self.hostname:
6969
# This should not happen.
70-
logger.debug("Cannot clone with a Git service having no domain.")
70+
logger.debug("Cannot clone with a Git service having no hostname.")
7171
raise CloneError(f"Cannot clone the repo '{url}' due to an internal error.")
7272

7373
url_parse_result = git_url.parse_remote_url(
7474
url,
75-
allowed_git_service_domains=[self.domain],
75+
allowed_git_service_hostnames=[self.hostname],
7676
)
7777
if not url_parse_result:
7878
raise CloneError(
@@ -83,9 +83,9 @@ def construct_clone_url(self, url: str) -> str:
8383
# https://docs.gitlab.com/ee/gitlab-basics/start-using-git.html#clone-using-a-token
8484
access_token = os.environ.get(self.access_token_env_name)
8585
if access_token:
86-
clone_url_netloc = f"oauth2:{access_token}@{self.domain}"
86+
clone_url_netloc = f"oauth2:{access_token}@{self.hostname}"
8787
else:
88-
clone_url_netloc = self.domain
88+
clone_url_netloc = self.hostname
8989

9090
clone_url = urlunparse(
9191
ParseResult(
@@ -241,17 +241,17 @@ def load_defaults(self) -> None:
241241
If there is an error loading the configuration.
242242
"""
243243
try:
244-
self.domain = self.load_domain(section_name="git_service.gitlab.self_hosted")
244+
self.hostname = self.load_hostname(section_name="git_service.gitlab.self_hosted")
245245
except ConfigurationError as error:
246246
raise error
247247

248-
if not self.domain:
248+
if not self.hostname:
249249
return
250250

251251
if not os.environ.get(self.access_token_env_name):
252252
raise ConfigurationError(
253253
f"Environment variable '{self.access_token_env_name}' is not set "
254-
+ f"for private GitLab service '{self.domain}'."
254+
+ f"for private GitLab service '{self.hostname}'."
255255
)
256256

257257

@@ -274,6 +274,6 @@ def load_defaults(self) -> None:
274274
If there is an error loading the configuration.
275275
"""
276276
try:
277-
self.domain = self.load_domain(section_name="git_service.gitlab.publicly_hosted")
277+
self.hostname = self.load_hostname(section_name="git_service.gitlab.publicly_hosted")
278278
except ConfigurationError as error:
279279
raise error

src/macaron/slsa_analyzer/git_url.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -485,11 +485,11 @@ def get_remote_origin_of_local_repo(git_obj: Git) -> str:
485485
logger.error("Error occurs while processing the remote URL of repo %s.", git_obj.project_name)
486486
return ""
487487

488-
_, _, domain = url_parse_result.netloc.rpartition("@")
488+
_, _, hostname = url_parse_result.netloc.rpartition("@")
489489

490490
new_url_parse_result = urllib.parse.ParseResult(
491491
scheme=url_parse_result.scheme,
492-
netloc=domain,
492+
netloc=hostname,
493493
path=url_parse_result.path,
494494
params=url_parse_result.params,
495495
query=url_parse_result.query,
@@ -548,7 +548,9 @@ def get_remote_vcs_url(url: str, clean_up: bool = True) -> str:
548548
return url_as_str
549549

550550

551-
def parse_remote_url(url: str, allowed_git_service_domains: list[str] | None = None) -> urllib.parse.ParseResult | None:
551+
def parse_remote_url(
552+
url: str, allowed_git_service_hostnames: list[str] | None = None
553+
) -> urllib.parse.ParseResult | None:
552554
"""Verify if the given repository path is a valid vcs.
553555
554556
This method converts the url to a ``https://`` url and return a
@@ -559,8 +561,8 @@ def parse_remote_url(url: str, allowed_git_service_domains: list[str] | None = N
559561
----------
560562
url: str
561563
The path of the repository to check.
562-
allowed_git_service_domains: list[str] | None
563-
The list of allowed git service domains.
564+
allowed_git_service_hostnames: list[str] | None
565+
The list of allowed git service hostnames.
564566
If this is ``None``, fall back to the ``.ini`` configuration.
565567
(Default: None).
566568
@@ -574,8 +576,8 @@ def parse_remote_url(url: str, allowed_git_service_domains: list[str] | None = N
574576
>>> parse_remote_url("ssh://[email protected]:7999/owner/org.git")
575577
ParseResult(scheme='https', netloc='github.com', path='owner/org.git', params='', query='', fragment='')
576578
"""
577-
if allowed_git_service_domains is None:
578-
allowed_git_service_domains = get_allowed_git_service_domains(defaults)
579+
if allowed_git_service_hostnames is None:
580+
allowed_git_service_hostnames = get_allowed_git_service_hostnames(defaults)
579581

580582
try:
581583
# Remove prefixes, such as "scm:" and "git:".
@@ -596,7 +598,7 @@ def parse_remote_url(url: str, allowed_git_service_domains: list[str] | None = N
596598

597599
# e.g., https://github.com/owner/project.git
598600
if parsed_url.scheme in ("http", "https", "ftp", "ftps", "git+https"):
599-
if parsed_url.netloc not in allowed_git_service_domains:
601+
if parsed_url.netloc not in allowed_git_service_hostnames:
600602
return None
601603
path_params = parsed_url.path.strip("/").split("/")
602604
if len(path_params) < 2:
@@ -613,7 +615,7 @@ def parse_remote_url(url: str, allowed_git_service_domains: list[str] | None = N
613615
user_host, _, port = parsed_url.netloc.partition(":")
614616
user, _, host = user_host.rpartition("@")
615617

616-
if not user or host not in allowed_git_service_domains:
618+
if not user or host not in allowed_git_service_hostnames:
617619
return None
618620

619621
path = ""
@@ -641,7 +643,7 @@ def parse_remote_url(url: str, allowed_git_service_domains: list[str] | None = N
641643
if not user_host or not port_path:
642644
return None
643645
user, _, host = user_host.rpartition("@")
644-
if not user or host not in allowed_git_service_domains:
646+
if not user or host not in allowed_git_service_hostnames:
645647
return None
646648

647649
path = ""
@@ -677,16 +679,16 @@ def parse_remote_url(url: str, allowed_git_service_domains: list[str] | None = N
677679
return None
678680

679681

680-
def get_allowed_git_service_domains(config: ConfigParser) -> list[str]:
681-
"""Load allowed git service domains from ini configuration.
682+
def get_allowed_git_service_hostnames(config: ConfigParser) -> list[str]:
683+
"""Load allowed git service hostnames from ini configuration.
682684
683685
Some notes for future improvements:
684686
685687
The fact that this method is here is not ideal.
686688
687689
Q: Why do we need this method here in this ``git_url`` module in the first place?
688690
A: A number of functions in this module also do "URL validation" as part of their logic.
689-
This requires loading in the allowed git service domains from the ini config.
691+
This requires loading in the allowed git service hostnames from the ini config.
690692
691693
Q: Why don't we use the ``GIT_SERVICES`` list from the ``macaron.slsa_analyzer.git_service``
692694
instead of having this second place of loading git service configuration?
@@ -697,18 +699,18 @@ def get_allowed_git_service_domains(config: ConfigParser) -> list[str]:
697699
section_name for section_name in config.sections() if section_name.startswith("git_service")
698700
]
699701

700-
allowed_git_service_domains = []
702+
allowed_git_service_hostnames = []
701703

702704
for section_name in git_service_section_names:
703705
git_service_section = config[section_name]
704706

705-
domain = git_service_section.get("domain")
706-
if not domain:
707+
hostname = git_service_section.get("hostname")
708+
if not hostname:
707709
continue
708710

709-
allowed_git_service_domains.append(domain)
711+
allowed_git_service_hostnames.append(hostname)
710712

711-
return allowed_git_service_domains
713+
return allowed_git_service_hostnames
712714

713715

714716
def get_repo_dir_name(url: str, sanitize: bool = True) -> str:

tests/slsa_analyzer/git_service/test_gitlab.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test_construct_clone_url_with_token(repo_url: str, clone_url: str) -> None:
5959
pytest.param(
6060
"""
6161
[git_service.gitlab.self_hosted]
62-
domain = internal.gitlab.org
62+
hostname = internal.gitlab.org
6363
""",
6464
"https://internal.gitlab.org/owner/repo.git",
6565
"https://oauth2:[email protected]/owner/repo.git",
@@ -68,7 +68,7 @@ def test_construct_clone_url_with_token(repo_url: str, clone_url: str) -> None:
6868
pytest.param(
6969
"""
7070
[git_service.gitlab.self_hosted]
71-
domain = internal.gitlab.org
71+
hostname = internal.gitlab.org
7272
""",
7373
"https://internal.gitlab.org/owner/repo",
7474
"https://oauth2:[email protected]/owner/repo",
@@ -98,7 +98,7 @@ def test_self_hosted_gitlab_without_env_set(tmp_path: Path) -> None:
9898
"""Test if the ``load_defaults`` method raises error if the required env variable is not set."""
9999
user_config_input = """
100100
[git_service.gitlab.self_hosted]
101-
domain = internal.gitlab.org
101+
hostname = internal.gitlab.org
102102
"""
103103
user_config_path = os.path.join(tmp_path, "config.ini")
104104
with open(user_config_path, "w", encoding="utf-8") as user_config_file:
@@ -178,7 +178,7 @@ def test_origin_remote_url_masking(self_hosted_gitlab: Git, expected_origin_url:
178178
"""
179179
user_config_input = """
180180
[git_service.gitlab.self_hosted]
181-
domain = internal.gitlab.org
181+
hostname = internal.gitlab.org
182182
"""
183183
user_config_path = os.path.join(tmp_path, "config.ini")
184184
with open(user_config_path, "w", encoding="utf-8") as user_config_file:

0 commit comments

Comments
 (0)