Skip to content

Commit 00cddcf

Browse files
authored
feat: allow defining a git service from defaults.ini (#694)
Signed-off-by: Trong Nhan Mai <[email protected]>
1 parent 3aee1b8 commit 00cddcf

File tree

9 files changed

+365
-14
lines changed

9 files changed

+365
-14
lines changed

docs/source/pages/output_files.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ to the directory:
131131
└── git_repos
132132
└── local_repos
133133
134-
.. note:: Please see :ref:`pages/using:analyzing a locally cloned repository` to know how to set the directory for analyzing local repositories.
134+
.. note:: Please see :ref:`pages/using:analyzing a repository on the local file system` to know how to set the directory for analyzing local repositories.
135135

136136
.. _output_files_macaron_verify_policy:
137137

docs/source/pages/supported_technologies/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ such as GitHub Actions workflows.
2424
* Docker
2525

2626

27+
.. _supported_git_services:
28+
2729
------------
2830
Git Services
2931
------------

docs/source/pages/using.rst

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Take the same example as above, to disable analyzing `micronaut-core <https://gi
5151
5252
./run_macaron.sh analyze -rp https://github.com/micronaut-projects/micronaut-core -b 4.0.x -d 82d115b4901d10226552ac67b0a10978cd5bc603 --skip-deps
5353
54-
.. note:: By default, Macaron would generate report files into the ``output`` directory in the current workspace. To understand the structure of this directory please see :ref:`Output Files Guide <output_files_guide>`.
54+
.. note:: By default, Macaron would generate report files into the ``output`` directory in the current working directory. To understand the structure of this directory please see :ref:`Output Files Guide <output_files_guide>`.
5555

5656
With the example above, the generated output reports can be seen here:
5757

@@ -238,7 +238,7 @@ With the example above, the generated output reports can be seen here:
238238
Analyzing dependencies in the SBOM without the main software component
239239
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
240240

241-
In the case where the repository URL of the main software component is not available (e.g. the repository is in a private domain where Macaron cannot access),
241+
In the case where the repository URL of the main software component is not available (e.g. the repository is in a self-hosted git service instance where Macaron cannot access),
242242
Macaron can still run the analysis on the dependencies listed in the SBOM.
243243
To do that, you must first create a PURL to present the main software component. This is so that this software component could be referenced later in the :ref:`verify-policy <verify-policy-command-cli>` command.
244244
For example: ``pkg:private_domain.com/org/name``.
@@ -300,9 +300,9 @@ An example configuration file for utilising this feature:
300300
301301
302302
303-
-------------------------------------
304-
Analyzing a locally cloned repository
305-
-------------------------------------
303+
-----------------------------------------------
304+
Analyzing a repository on the local file system
305+
-----------------------------------------------
306306

307307
.. warning::
308308
During the analysis, Macaron can check out different commits, which can reset the index and working tree of the repository.
@@ -312,9 +312,55 @@ Analyzing a locally cloned repository
312312
.. note::
313313
We assume that the ``origin`` remote exists in the cloned repository and checkout the relevant commits from ``origin`` only.
314314

315-
If you have a local repository that you want to analyze, Macaron also supports running the analysis against a local repository.
315+
Macaron supports analyzing a repository on the local file system.
316316

317-
Assume that the dir tree at the local repository has the following components:
317+
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
318+
Analyzing a repository whose git service is not supported by Macaron
319+
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
320+
321+
If the repository remote URL is from an unknown git service (see :ref:`Git Services <supported_git_services>` for a list of supported git services in Macaron), Macaron won't recognize it when analyzing the repository.
322+
323+
You would need to tell Macaron about that git service through the ``defaults.ini`` config.
324+
For example, let's say you want to analyze a repository hosted at ``https://git.example.com/foo/target``. First, you need to create a ``defaults.ini`` file in the current working directory with the following content:
325+
326+
.. code-block:: ini
327+
328+
[git_service.local_repo]
329+
hostname = git.example.com
330+
331+
In which ``hostname`` contains the hostname of the git service URL. In this example it is ``git.example.com``.
332+
333+
.. note::
334+
335+
This ``defaults.ini`` section must only be used for analyzing a repository on the local file system. If the hostname has already been supported in other services, it doesn't need to be defined again here.
336+
337+
Assume that the dir tree at the current working directory has the following structure:
338+
339+
.. code-block:: shell
340+
341+
boo
342+
├── foo
343+
│ └── target
344+
345+
We can run Macaron against the local repository at ``target`` by using this command:
346+
347+
.. code-block:: shell
348+
349+
./run_macaron.sh --local-repos-path ./boo/foo --defaults-path ./defaults.ini analyze --repo-path target <rest_of_args>
350+
351+
With ``rest_of_args`` being the arguments to the ``analyze`` command (e.g. ``--branch/-b``, ``--digest/-d`` or ``--skip-deps`` similar to two previous examples).
352+
353+
The ``--local-repos-path/-lr`` flag tells Macaron to look into ``./boo/foo`` for local repositories. For more information, please see :ref:`Command Line Usage <cli-usage>`.
354+
355+
.. note:: If ``--local-repos-path/-lr`` is not provided, Macaron will looks inside ``<current_working_directory>/output/git_repos/local_repos/`` whenever you provide a local path to ``--repo-path/-rp``.
356+
357+
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
358+
Analyzing a local repository with supported git service
359+
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
360+
361+
If the local repository you want to analyze has a remote origin hosted on a supported git service, you can run the analysis directly without having to prepare ``defaults.ini`` as above.
362+
363+
Assume that the dir tree at the current working directory has the following structure:
318364

319365
.. code-block:: shell
320366
@@ -326,13 +372,13 @@ We can run Macaron against the local repository at ``target`` by using this comm
326372

327373
.. code-block:: shell
328374
329-
./run_macaron.sh -lr path/to/boo/foo analyze -rp target <rest_of_args>
375+
./run_macaron.sh --local-repos-path ./boo/foo analyze --repo-path target <rest_of_args>
330376
331-
With ``rest_of_args`` being the arguments to the ``analyze`` command (e.g. ``-b``, ``-d`` or ``--skip-deps`` similar to two previous examples)
377+
With ``rest_of_args`` being the arguments to the ``analyze`` command (e.g. ``--branch/-b``, ``--digest/-d`` or ``--skip-deps`` similar to two previous examples).
332378

333-
The ``-lr`` flag configure Macaron to looks into ``path/to/boo/foo`` for local repositories. For more information, please see :ref:`Command Line Usage <cli-usage>`.
379+
The ``--local-repos-path/-lr`` flag tells Macaron to look into ``./boo/foo`` for local repositories. For more information, please see :ref:`Command Line Usage <cli-usage>`.
334380

335-
.. note:: If ``-lr`` is not provided, Macaron will looks inside ``<working_directory>/output/git_repos/local_repos/`` whenever you provide a local path to ``-rp``.
381+
.. note:: If ``--local-repos-path/-lr`` is not provided, Macaron will looks inside ``<current_working_directory>/output/git_repos/local_repos/`` whenever you provide a local path to ``--repo-path/-rp``.
336382

337383
-------------------------
338384
Running the policy engine

scripts/dev_scripts/integration_tests.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,17 @@ echo -e "\n=====================================================================
434434
echo "Run integration tests with local paths for apache/maven..."
435435
echo -e "==================================================================================\n"
436436

437+
echo -e "\n----------------------------------------------------------------------------------"
438+
echo "bitbucket.org/snakeyaml/snakeyaml: Analyzing a repository with un-supported git service as local repo without dependency resolution."
439+
echo -e "----------------------------------------------------------------------------------\n"
440+
git clone https://bitbucket.org/snakeyaml/snakeyaml $WORKSPACE/output/local_repos/snakeyaml || log_fail
441+
DEFAULTS_FILE=$WORKSPACE/tests/e2e/defaults/bitbucket_local_repo.ini
442+
JSON_EXPECTED=$WORKSPACE/tests/e2e/expected_results/snakeyaml/snakeyaml.json
443+
JSON_RESULT=$WORKSPACE/output/reports/bitbucket_org/snakeyaml/snakeyaml/snakeyaml.json
444+
$RUN_MACARON -dp $DEFAULTS_FILE -lr $WORKSPACE/output/local_repos analyze -rp snakeyaml -d a34989252e6f59e36a3aaf788a903b7a37a73d33 --skip-deps || log_fail
445+
446+
check_or_update_expected_output $COMPARE_JSON_OUT $JSON_RESULT $JSON_EXPECTED || log_fail
447+
437448
echo -e "\n----------------------------------------------------------------------------------"
438449
echo "apache/maven: Analyzing with the branch name, the commit digest and dependency resolution using cyclonedx maven plugin (default)."
439450
echo -e "----------------------------------------------------------------------------------\n"

src/macaron/config/defaults.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ hostname = gitlab.com
9090
# [git_service.gitlab.self_hosted]
9191
# hostname = example.org
9292

93+
# This section defines a git service that Macaron doesn't recognize yet.
94+
# It must only be used for analyzing a locally cloned repository.
95+
# If the host name is already supported in other services, it doesn't need to be defined again here.
96+
# [git_service.local_repo]
97+
# hostname = example.org
98+
9399
# This is the spec for trusted Maven build tools.
94100
[builder.maven]
95101
entry_conf = settings.xml
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2022 - 2023, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2022 - 2024, Oracle and/or its affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
33

44
"""The git_service package contains the supported git services for Macaron."""
@@ -7,7 +7,14 @@
77
from .bitbucket import BitBucket
88
from .github import GitHub
99
from .gitlab import PubliclyHostedGitLab, SelfHostedGitLab
10+
from .local_repo_git_service import LocalRepoGitService
1011

1112
# The list of supported git services. The order of the list determines the order
1213
# in which each git service is checked against the target repository.
13-
GIT_SERVICES: list[BaseGitService] = [GitHub(), PubliclyHostedGitLab(), SelfHostedGitLab(), BitBucket()]
14+
GIT_SERVICES: list[BaseGitService] = [
15+
GitHub(),
16+
PubliclyHostedGitLab(),
17+
SelfHostedGitLab(),
18+
BitBucket(),
19+
LocalRepoGitService(),
20+
]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright (c) 2024 - 2024, Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
3+
4+
"""This module contains the spec for the local repo git service."""
5+
6+
import logging
7+
8+
from pydriller.git import Git
9+
10+
from macaron.errors import ConfigurationError, RepoCheckOutError
11+
from macaron.slsa_analyzer import git_url
12+
from macaron.slsa_analyzer.git_service.base_git_service import BaseGitService
13+
14+
logger: logging.Logger = logging.getLogger(__name__)
15+
16+
17+
class LocalRepoGitService(BaseGitService):
18+
"""This class contains the spec of the local repo git service."""
19+
20+
def __init__(self) -> None:
21+
"""Initialize instance."""
22+
super().__init__("local_repo")
23+
24+
def load_defaults(self) -> None:
25+
"""Load the values for this git service from the ini configuration."""
26+
try:
27+
self.hostname = self.load_hostname(section_name="git_service.local_repo")
28+
except ConfigurationError as error:
29+
raise error
30+
31+
def clone_repo(self, _clone_dir: str, _url: str) -> None:
32+
"""Cloning from a local repo git service is not supported."""
33+
raise NotImplementedError
34+
35+
def check_out_repo(self, git_obj: Git, branch: str, digest: str, offline_mode: bool) -> Git:
36+
"""Checkout the branch and commit specified by the user of a repository."""
37+
if not git_url.check_out_repo_target(git_obj, branch, digest, offline_mode):
38+
raise RepoCheckOutError(
39+
f"Failed to check out branch {branch} and commit {digest} for repo {git_obj.project_name}."
40+
)
41+
42+
return git_obj
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright (c) 2024 - 2024, Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
3+
4+
[git_service.local_repo]
5+
hostname = bitbucket.org

0 commit comments

Comments
 (0)