Skip to content

Commit dc6f276

Browse files
author
Trong Nhan Mai
authored
refactor!: remove --config-path from CLI (#844)
Signed-off-by: Trong Nhan Mai <[email protected]>
1 parent edfe06e commit dc6f276

File tree

28 files changed

+147
-459
lines changed

28 files changed

+147
-459
lines changed

docs/source/pages/cli_usage/command_analyze.rst

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Usage
2121
2222
usage: ./run_macaron.sh analyze
2323
[-h] [-sbom SBOM_PATH] [-purl PURL] [-rp REPO_PATH] [-b BRANCH]
24-
[-d DIGEST] [-pe PROVENANCE_EXPECTATION] [-c CONFIG_PATH]
24+
[-d DIGEST] [-pe PROVENANCE_EXPECTATION]
2525
[--skip-deps] [-g TEMPLATE_PATH]
2626
2727
-------
@@ -62,10 +62,6 @@ Options
6262

6363
The path to the provenance file in in-toto format.
6464

65-
.. option:: -c CONFIG_PATH, --config-path CONFIG_PATH
66-
67-
The path to the user configuration.
68-
6965
.. option:: --skip-deps
7066

7167
Skip automatic dependency analysis.

scripts/release_scripts/run_macaron.sh

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,6 @@ if [[ $command == "analyze" ]]; then
320320
arg_prov_file="$2"
321321
shift
322322
;;
323-
-c|--config-path)
324-
arg_config_path="$2"
325-
shift
326-
;;
327323
-g|--template-path)
328324
arg_template_path="$2"
329325
shift
@@ -414,16 +410,6 @@ if [[ -n "${arg_template_path:-}" ]]; then
414410
mount_file "-g/--template-path" "$template_path" "$template_path_in_container" "ro,Z"
415411
fi
416412

417-
# Determine the config path to be mounted into ${MACARON_WORKSPACE}/config/${file_name}
418-
if [[ -n "${arg_config_path:-}" ]]; then
419-
config_path="${arg_config_path}"
420-
file_name="$(basename "${config_path}")"
421-
config_path_in_container="${MACARON_WORKSPACE}/config/${file_name}"
422-
423-
argv_command+=("--config-path" "$config_path_in_container")
424-
mount_file "-c/--config-path" "$config_path" "$config_path_in_container" "ro,Z"
425-
fi
426-
427413
# Determine the sbom path to be mounted into ${MACARON_WORKSPACE}/sbom/${file_name}
428414
if [[ -n "${arg_sbom_path:-}" ]]; then
429415
sbom_path="${arg_sbom_path}"

src/macaron/__main__.py

Lines changed: 39 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from macaron.config.global_config import global_config
1919
from macaron.errors import ConfigurationError
2020
from macaron.output_reporter.reporter import HTMLReporter, JSONReporter, PolicyReporter
21-
from macaron.parsers.yaml.loader import YamlLoader
2221
from macaron.policy_engine.policy_engine import run_policy_engine, show_prelude
2322
from macaron.slsa_analyzer.analyzer import Analyzer
2423
from macaron.slsa_analyzer.git_service import GIT_SERVICES
@@ -32,22 +31,14 @@
3231

3332
def analyze_slsa_levels_single(analyzer_single_args: argparse.Namespace) -> None:
3433
"""Run the SLSA checks against a single target repository."""
35-
if not (analyzer_single_args.repo_path or analyzer_single_args.package_url or analyzer_single_args.config_path):
36-
# We don't mention --config-path as a possible option in this log message as it going to be move soon.
37-
# See: https://github.com/oracle/macaron/issues/417
34+
if not (analyzer_single_args.repo_path or analyzer_single_args.package_url):
3835
logger.error(
3936
"""Analysis target missing. Please provide a package url (PURL) and/or repo path.
4037
Examples of a PURL can be seen at https://github.com/package-url/purl-spec:
4138
pkg:github/micronaut-projects/micronaut-core."""
4239
)
4340
sys.exit(os.EX_USAGE)
4441

45-
if analyzer_single_args.config_path and (analyzer_single_args.package_url or analyzer_single_args.repo_path):
46-
# TODO: revisit when the config-path option is moved.
47-
# See: https://github.com/oracle/macaron/issues/417
48-
logger.error("Cannot provide both config path and (package url (PURL) and/or repo path).")
49-
sys.exit(os.EX_USAGE)
50-
5142
# Set provenance expectation path.
5243
if analyzer_single_args.provenance_expectation is not None:
5344
if not os.path.exists(analyzer_single_args.provenance_expectation):
@@ -89,55 +80,45 @@ def analyze_slsa_levels_single(analyzer_single_args: argparse.Namespace) -> None
8980
analyzer.reporters.append(JSONReporter())
9081

9182
run_config = {}
92-
if analyzer_single_args.config_path:
93-
# Get user config from yaml file
94-
loaded_config = YamlLoader.load(analyzer_single_args.config_path)
95-
if loaded_config is None:
96-
logger.error("The input yaml config at %s is invalid.", analyzer_single_args.config_path)
97-
sys.exit(os.EX_DATAERR)
98-
else:
99-
run_config = loaded_config
100-
else:
101-
repo_path = analyzer_single_args.repo_path
102-
purl = analyzer_single_args.package_url
103-
branch = analyzer_single_args.branch
104-
digest = analyzer_single_args.digest
105-
106-
if repo_path and purl:
107-
# To provide the purl together with the repository path, the user must specify the commit digest unless the
108-
# purl has a version.
109-
try:
110-
purl_object = PackageURL.from_string(purl)
111-
except ValueError as error:
112-
logger.debug("Could not parse PURL: %s", error)
113-
sys.exit(os.EX_USAGE)
114-
if not (purl_object.version or digest):
115-
logger.error(
116-
"Please provide the commit digest for the repo at %s that matches to the PURL string %s. Or "
117-
"include the version in the PURL",
118-
repo_path,
119-
purl,
120-
)
121-
sys.exit(os.EX_USAGE)
83+
repo_path = analyzer_single_args.repo_path
84+
purl = analyzer_single_args.package_url
85+
branch = analyzer_single_args.branch
86+
digest = analyzer_single_args.digest
87+
88+
if repo_path and purl:
89+
# To provide the purl together with the repository path, the user must specify the commit digest unless the
90+
# purl has a version.
91+
try:
92+
purl_object = PackageURL.from_string(purl)
93+
except ValueError as error:
94+
logger.debug("Could not parse PURL: %s", error)
95+
sys.exit(os.EX_USAGE)
96+
if not (purl_object.version or digest):
97+
logger.error(
98+
"Please provide the commit digest for the repo at %s that matches to the PURL string %s. Or "
99+
"include the version in the PURL",
100+
repo_path,
101+
purl,
102+
)
103+
sys.exit(os.EX_USAGE)
122104

123-
# We need to use empty strings when the input values are of None type. This is because this dictionary will be
124-
# passed into the Configuration instance, where the existing values in Configuration.options are replaced by
125-
# whatever we assign it here. Technically, the data in ``Configuration`` class are not limited to only strings.
126-
# Therefore, it could be cases where the ``purl`` field is initialized as an empty string in the constructor
127-
# of the Configuration class, but if `` analyzer_single_args.package_url`` is None, the ``purl`` field is set
128-
# to None in the Configuration instance.
129-
# This inconsistency could cause potential issues when Macaron handles those inputs.
130-
# TODO: improve the implementation of ``Configuration`` class to avoid such inconsistencies.
131-
run_config = {
132-
"target": {
133-
"id": purl or repo_path or "",
134-
"purl": purl or "",
135-
"path": repo_path or "",
136-
"branch": branch or "",
137-
"digest": digest or "",
138-
},
139-
"dependencies": [],
105+
# We need to use empty strings when the input values are of None type. This is because this dictionary will be
106+
# passed into the Configuration instance, where the existing values in Configuration.options are replaced by
107+
# whatever we assign it here. Technically, the data in ``Configuration`` class are not limited to only strings.
108+
# Therefore, it could be cases where the ``purl`` field is initialized as an empty string in the constructor
109+
# of the Configuration class, but if `` analyzer_single_args.package_url`` is None, the ``purl`` field is set
110+
# to None in the Configuration instance.
111+
# This inconsistency could cause potential issues when Macaron handles those inputs.
112+
# TODO: improve the implementation of ``Configuration`` class to avoid such inconsistencies.
113+
run_config = {
114+
"target": {
115+
"id": purl or repo_path or "",
116+
"purl": purl or "",
117+
"path": repo_path or "",
118+
"branch": branch or "",
119+
"digest": digest or "",
140120
}
121+
}
141122

142123
prov_payload = None
143124
if analyzer_single_args.provenance_file:
@@ -325,15 +306,6 @@ def main(argv: list[str] | None = None) -> None:
325306
# Use Macaron to analyze one single repository.
326307
single_analyze_parser = sub_parser.add_parser(name="analyze")
327308

328-
# We make the mutually exclusive usage of --config-path and --repo-path optional
329-
# so that the user can provide the --package-url separately while keeping the current behavior of Macaron.
330-
# Note that if the user provides both --package-url and --config-path, we will still raise an error,
331-
# which is handled within the ``analyze_slsa_levels_single`` method.
332-
# When we remove the --config-path option, we can remove this group and instead add all relevant
333-
# options in the analyze command through ``single_analyze_parser``.
334-
# See: https://github.com/oracle/macaron/issues/417
335-
group = single_analyze_parser.add_mutually_exclusive_group(required=False)
336-
337309
single_analyze_parser.add_argument(
338310
"-sbom",
339311
"--sbom-path",
@@ -343,7 +315,7 @@ def main(argv: list[str] | None = None) -> None:
343315
help=("The path to the SBOM of the analysis target."),
344316
)
345317

346-
group.add_argument(
318+
single_analyze_parser.add_argument(
347319
"-rp",
348320
"--repo-path",
349321
required=False,
@@ -398,15 +370,6 @@ def main(argv: list[str] | None = None) -> None:
398370
help=("The path to the provenance file in in-toto format."),
399371
)
400372

401-
group.add_argument(
402-
"-c",
403-
"--config-path",
404-
required=False,
405-
type=str,
406-
default="",
407-
help=("The path to the user configuration."),
408-
)
409-
410373
single_analyze_parser.add_argument(
411374
"--skip-deps",
412375
required=False,

src/macaron/config/README.md

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/macaron/config/target_config.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
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
"""This module contains the Configuration class for the target analyzed repository."""
55

66
import logging
7-
import os
87
from typing import Any
98

10-
import yamale
11-
from yamale.schema import Schema
12-
139
logger: logging.Logger = logging.getLogger(__name__)
1410

15-
_SCHEMA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "target_config_schema.yaml")
16-
17-
TARGET_CONFIG_SCHEMA: Schema = yamale.make_schema(_SCHEMA_DIR)
1811
"""The schema for the target configuration yaml file."""
1912

2013

src/macaron/config/target_config_schema.yaml

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/macaron/dependency_analyzer/cyclonedx.py

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
from macaron.output_reporter.scm import SCMStatus
3232
from macaron.repo_finder.repo_finder import find_repo
3333
from macaron.repo_finder.repo_validator import find_valid_repository_url
34-
from macaron.slsa_analyzer.git_url import get_repo_full_name_from_url
3534

3635
logger: logging.Logger = logging.getLogger(__name__)
3736

@@ -259,46 +258,26 @@ def add_latest_version(
259258
logger.error("Could not parse dependency version number: %s", error)
260259

261260
@staticmethod
262-
def merge_configs(
263-
config_deps: list[Configuration], resolved_deps: dict[str, DependencyInfo]
264-
) -> list[Configuration]:
265-
"""Merge the resolved dependencies into the manual config dependencies.
266-
267-
Manual configuration entries are prioritized over the automatically resolved dependencies.
261+
def to_configs(resolved_deps: dict[str, DependencyInfo]) -> list[Configuration]:
262+
"""Convert the resolved dependencies into the format used by the Analyzer.
268263
269264
Parameters
270265
----------
271-
config_deps : list[Configuration]
272-
Dependencies defined in the configuration file.
273266
resolved_deps : dict[str, DependencyInfo]
274267
The automatically resolved dependencies.
275268
276269
Returns
277270
-------
278271
list[Configuration]
279-
The result list contains the merged dependencies.
272+
The dependency list to be used by the Analyzer.
280273
"""
281-
merged_deps: list[Configuration] = []
282-
if config_deps:
283-
for dep in config_deps:
284-
dep.set_value("available", SCMStatus.AVAILABLE)
285-
merged_deps.append(dep)
286-
287274
if not resolved_deps:
288-
return merged_deps
275+
return []
276+
277+
config_list: list[Configuration] = []
289278

290279
for key, value in resolved_deps.items():
291-
duplicate = False
292-
if config_deps:
293-
for m_dep in config_deps:
294-
m_repo = get_repo_full_name_from_url(m_dep.get_value("path"))
295-
a_repo = get_repo_full_name_from_url(value.get("url", ""))
296-
if m_repo and m_repo == a_repo:
297-
duplicate = True
298-
break
299-
if duplicate:
300-
continue
301-
merged_deps.append(
280+
config_list.append(
302281
Configuration(
303282
{
304283
"id": key,
@@ -312,7 +291,7 @@ def merge_configs(
312291
)
313292
)
314293

315-
return merged_deps
294+
return config_list
316295

317296
@staticmethod
318297
def tool_valid(tool: str) -> bool:

src/macaron/slsa_analyzer/analyzer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ def run(
155155
The return status code.
156156
"""
157157
main_config = Configuration(user_config.get("target", {}))
158-
deps_config: list[Configuration] = [Configuration(dep) for dep in user_config.get("dependencies", [])]
159158
deps_resolved: dict[str, DependencyInfo] = {}
160159

161160
# Get a single session once for the whole analysis.
@@ -194,7 +193,7 @@ def run(
194193
deps_resolved = DependencyAnalyzer.resolve_dependencies(main_record.context, sbom_path)
195194

196195
# Merge the automatically resolved dependencies with the manual configuration.
197-
deps_config = DependencyAnalyzer.merge_configs(deps_config, deps_resolved)
196+
deps_config = DependencyAnalyzer.to_configs(deps_resolved)
198197

199198
# Create a report instance with the record of the main repo.
200199
report = Report(main_record)

tests/dependency_analyzer/configurations/jackson_databind_config.yaml

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/dependency_analyzer/configurations/micronaut_test_config_branch_commit.yaml

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)