Skip to content

Commit 744335d

Browse files
author
Trong Nhan Mai
authored
feat: add exclude and include check in ini config (#254)
Signed-off-by: Trong Nhan Mai <[email protected]>
1 parent db3231f commit 744335d

File tree

20 files changed

+1389
-271
lines changed

20 files changed

+1389
-271
lines changed

scripts/dev_scripts/integration_tests.sh

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,17 @@ fi
577577
rm -rf "$SOURCE_REPO"
578578
rm -rf "$TARGET_REPO"
579579

580+
echo -e "\n----------------------------------------------------------------------------------"
581+
echo "Running the analysis with all checks excluded. This test should return an error code."
582+
echo -e "----------------------------------------------------------------------------------\n"
583+
$RUN_MACARON -dp tests/e2e/defaults/exclude_all_checks.ini analyze -rp https://github.com/apache/maven --skip-deps
584+
585+
if [ $? -eq 0 ];
586+
then
587+
echo -e "Expect non-zero status code but got $?."
588+
log_fail
589+
fi
590+
580591
echo -e "\n----------------------------------------------------------------------------------"
581592
echo "apache/maven: test analyzing without the environment variable GITHUB_TOKEN being set."
582593
echo -e "----------------------------------------------------------------------------------\n"
@@ -626,12 +637,24 @@ fi
626637

627638
# Testing the CUE provenance expectation verifier.
628639
echo -e "\n----------------------------------------------------------------------------------"
629-
echo "Test verifying CUE provenance expectation."
640+
echo "Test verifying CUE provenance expectation for ossf/scorecard"
641+
echo -e "----------------------------------------------------------------------------------\n"
642+
JSON_EXPECTED=$WORKSPACE/tests/e2e/expected_results/scorecard/scorecard.json
643+
JSON_RESULT=$WORKSPACE/output/reports/github/ossf/scorecard/scorecard.json
644+
DEFAULTS_FILE=$WORKSPACE/tests/e2e/defaults/scorecard.ini
645+
EXPECTATION_FILE=$WORKSPACE/tests/slsa_analyzer/provenance/expectations/cue/resources/valid_expectations/scorecard_PASS.cue
646+
$RUN_MACARON -dp $DEFAULTS_FILE analyze -pe $EXPECTATION_FILE -purl pkg:github/ossf/[email protected] --skip-deps || log_fail
647+
648+
check_or_update_expected_output $COMPARE_JSON_OUT $JSON_RESULT $JSON_EXPECTED || log_fail
649+
650+
echo -e "\n----------------------------------------------------------------------------------"
651+
echo "Test verifying CUE provenance expectation for slsa-verifier"
630652
echo -e "----------------------------------------------------------------------------------\n"
631653
JSON_EXPECTED=$WORKSPACE/tests/e2e/expected_results/slsa-verifier/slsa-verifier_cue_PASS.json
632654
JSON_RESULT=$WORKSPACE/output/reports/github_com/slsa-framework/slsa-verifier/slsa-verifier.json
633655
EXPECTATION_FILE=$WORKSPACE/tests/slsa_analyzer/provenance/expectations/cue/resources/valid_expectations/slsa_verifier_PASS.cue
634-
$RUN_MACARON analyze -pe $EXPECTATION_FILE -rp https://github.com/slsa-framework/slsa-verifier -b main -d fc50b662fcfeeeb0e97243554b47d9b20b14efac --skip-deps || log_fail
656+
DEFAULTS_FILE=$WORKSPACE/tests/e2e/defaults/slsa_verifier.ini
657+
$RUN_MACARON -dp $DEFAULTS_FILE analyze -pe $EXPECTATION_FILE -rp https://github.com/slsa-framework/slsa-verifier -b main -d fc50b662fcfeeeb0e97243554b47d9b20b14efac --skip-deps || log_fail
635658

636659
check_or_update_expected_output $COMPARE_JSON_OUT $JSON_RESULT $JSON_EXPECTED || log_fail
637660

@@ -648,16 +671,15 @@ check_or_update_expected_output $COMPARE_JSON_OUT $JSON_RESULT $JSON_EXPECTED ||
648671

649672
# Testing the Souffle policy engine.
650673
echo -e "\n----------------------------------------------------------------------------------"
651-
echo "Run policy CLI with slsa-verifier results."
674+
echo "Run policy CLI with scorecard results."
652675
echo -e "----------------------------------------------------------------------------------\n"
653676
RUN_POLICY="macaron verify-policy"
654-
POLICY_FILE=$WORKSPACE/tests/policy_engine/resources/policies/valid/slsa-verifier.dl
677+
POLICY_FILE=$WORKSPACE/tests/policy_engine/resources/policies/scorecard/scorecard.dl
655678
POLICY_RESULT=$WORKSPACE/output/policy_report.json
656-
POLICY_EXPECTED=$WORKSPACE/tests/policy_engine/expected_results/policy_report.json
679+
POLICY_EXPECTED=$WORKSPACE/tests/policy_engine/expected_results/scorecard/scorecard_policy_report.json
657680
VSA_RESULT=$WORKSPACE/output/vsa.intoto.jsonl
658-
VSA_PAYLOAD_EXPECTED=$WORKSPACE/tests/vsa/integration/github_slsa-framework_slsa-verifier/vsa_payload.json
681+
VSA_PAYLOAD_EXPECTED=$WORKSPACE/tests/vsa/integration/github_slsa-framework_scorecard/vsa_payload.json
659682

660-
# Run policy engine on the database and compare results.
661683
$RUN_POLICY -f $POLICY_FILE -d "$WORKSPACE/output/macaron.db" || log_fail
662684
check_or_update_expected_output $COMPARE_POLICIES $POLICY_RESULT $POLICY_EXPECTED || log_fail
663685
check_or_update_expected_output "$COMPARE_VSA" "$VSA_RESULT" "$VSA_PAYLOAD_EXPECTED" || log_fail

scripts/dev_scripts/integration_tests_docker.sh

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,27 +124,38 @@ $RUN_MACARON_SCRIPT analyze -pe $EXPECTATION_DIR -rp https://github.com/urllib3/
124124

125125
python $COMPARE_JSON_OUT $JSON_RESULT $JSON_EXPECTED || log_fail
126126

127+
echo -e "\n----------------------------------------------------------------------------------"
128+
echo "Test verifying CUE provenance expectation for ossf/scorecard"
129+
echo -e "----------------------------------------------------------------------------------\n"
130+
JSON_EXPECTED=$WORKSPACE/tests/e2e/expected_results/scorecard/scorecard.json
131+
JSON_RESULT=$WORKSPACE/output/reports/github/ossf/scorecard/scorecard.json
132+
DEFAULTS_FILE=$WORKSPACE/tests/e2e/defaults/scorecard.ini
133+
EXPECTATION_FILE=$WORKSPACE/tests/slsa_analyzer/provenance/expectations/cue/resources/valid_expectations/scorecard_PASS.cue
134+
$RUN_MACARON_SCRIPT -dp $DEFAULTS_FILE analyze -pe $EXPECTATION_FILE -purl pkg:github/ossf/[email protected] --skip-deps || log_fail
135+
136+
python $COMPARE_JSON_OUT $JSON_RESULT $JSON_EXPECTED || log_fail
137+
127138
echo -e "\n----------------------------------------------------------------------------------"
128139
echo "slsa-framework/slsa-verifier: Analyzing the repo path when automatic dependency resolution is skipped"
129140
echo "and CUE file is provided as expectation."
130141
echo -e "----------------------------------------------------------------------------------\n"
131142
JSON_EXPECTED=$WORKSPACE/tests/e2e/expected_results/slsa-verifier/slsa-verifier_cue_PASS.json
132143
JSON_RESULT=$WORKSPACE/output/reports/github_com/slsa-framework/slsa-verifier/slsa-verifier.json
133144
EXPECTATION_FILE=$WORKSPACE/tests/slsa_analyzer/provenance/expectations/cue/resources/valid_expectations/slsa_verifier_PASS.cue
134-
$RUN_MACARON_SCRIPT analyze -pe $EXPECTATION_FILE -rp https://github.com/slsa-framework/slsa-verifier -b main -d fc50b662fcfeeeb0e97243554b47d9b20b14efac --skip-deps || log_fail
145+
DEFAULTS_FILE=$WORKSPACE/tests/e2e/defaults/slsa_verifier.ini
146+
$RUN_MACARON_SCRIPT -dp $DEFAULTS_FILE analyze -pe $EXPECTATION_FILE -rp https://github.com/slsa-framework/slsa-verifier -b main -d fc50b662fcfeeeb0e97243554b47d9b20b14efac --skip-deps || log_fail
135147

136148
python $COMPARE_JSON_OUT $JSON_RESULT $JSON_EXPECTED || log_fail
137149

138150
echo -e "\n----------------------------------------------------------------------------------"
139-
echo "Run policy CLI with slsa-verifier results."
151+
echo "Run policy CLI with scorecard results."
140152
echo -e "----------------------------------------------------------------------------------\n"
141-
POLICY_FILE=$WORKSPACE/tests/policy_engine/resources/policies/valid/slsa-verifier.dl
153+
POLICY_FILE=$WORKSPACE/tests/policy_engine/resources/policies/scorecard/scorecard.dl
142154
POLICY_RESULT=$WORKSPACE/output/policy_report.json
143-
POLICY_EXPECTED=$WORKSPACE/tests/policy_engine/expected_results/policy_report.json
155+
POLICY_EXPECTED=$WORKSPACE/tests/policy_engine/expected_results/scorecard/scorecard_policy_report.json
144156
VSA_RESULT=$WORKSPACE/output/vsa.intoto.jsonl
145-
VSA_PAYLOAD_EXPECTED=$WORKSPACE/tests/vsa/integration/github_slsa-framework_slsa-verifier/vsa_payload.json
157+
VSA_PAYLOAD_EXPECTED=$WORKSPACE/tests/vsa/integration/github_slsa-framework_scorecard/vsa_payload.json
146158

147-
# Run policy engine on the database and compare results.
148159
$RUN_MACARON_SCRIPT verify-policy -f $POLICY_FILE -d "$WORKSPACE/output/macaron.db" || log_fail
149160
python $COMPARE_POLICIES $POLICY_RESULT $POLICY_EXPECTED || log_fail
150161
python "$COMPARE_VSA" "$VSA_RESULT" "$VSA_PAYLOAD_EXPECTED" || log_fail

src/macaron/config/defaults.ini

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,3 +479,39 @@ hostname = registry.npmjs.org
479479
# The attestation REST API.
480480
attestation_endpoint = -/npm/v1/attestations
481481
request_timeout = 20
482+
483+
# Configuration options for selecting the checks to run.
484+
# Both the exclude and include are defined as list of strings:
485+
# - The exclude list is used to specify the checks that will not run.
486+
# - The include list is used to specify the checks that should run. The checks that are not specified by "include" will be excluded.
487+
# The final list of checks to run will always be "include" minus "exclude".
488+
# Each element of a list is either the full ID of a check (e.g mcn_build_as_code_1) or a glob pattern
489+
# (https://docs.python.org/3/library/fnmatch.html) to match multiple check IDs (e.g "mcn_*" will match all default
490+
# Macaron checks).
491+
# Examples
492+
# 1. Exclude 1 check:
493+
# ```
494+
# [analysis.checks]
495+
# exclude = mcn_build_as_code_1
496+
# include = *
497+
# ```
498+
# 2. Exclude multiple checks:
499+
# ```
500+
# [analysis.checks]
501+
# exclude =
502+
# mcn_build_as_code_1
503+
# mcn_provenance_level_three_1
504+
# include = *
505+
# ```
506+
# 3. Exclude multiple checks that start with `mcn_provenance`:
507+
# ```
508+
# [analysis.checks]
509+
# exclude =
510+
# mcn_provenance*
511+
# include = *
512+
# ```
513+
[analysis.checks]
514+
# By default, we don't exclude any checks.
515+
exclude =
516+
# By default, we run all checks available.
517+
include = *

src/macaron/errors.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2023 - 2023, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2023 - 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 error classes for Macaron."""
@@ -54,3 +54,7 @@ class DuplicateError(MacaronError):
5454

5555
class InvalidHTTPResponseError(MacaronError):
5656
"""Happens when the HTTP response is invalid or unexpected."""
57+
58+
59+
class CheckRegistryError(MacaronError):
60+
"""The Check Registry Error class."""

src/macaron/output_reporter/results.py

Lines changed: 3 additions & 1 deletion
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
"""This module contains classes that represent the result of the Macaron analysis."""
@@ -142,6 +142,8 @@ def get_dict(self) -> dict:
142142
"metadata": {
143143
"timestamps": datetime.now().isoformat(sep=" ", timespec="seconds"),
144144
"has_passing_check": has_passing_check,
145+
"run_checks": registry.checks_to_run,
146+
"check_tree": registry.check_tree,
145147
},
146148
"target": self.context.get_dict() if self.context else {},
147149
"dependencies": self.get_dep_summary(),

src/macaron/output_reporter/templates/base_template.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,13 @@
286286
.active {
287287
display: block;
288288
}
289+
290+
.check-tree-node::before {
291+
content: "\2514";
292+
color: black;
293+
display: inline-block;
294+
margin-right: 6px;
295+
}
289296
</style>
290297
</head>
291298

@@ -683,6 +690,13 @@
683690
document.getElementById("check_report_content").classList.toggle("hidden");
684691
});
685692

693+
// Add a listener to toggle the display of run checks.
694+
let run_check_toggler = document.getElementById("run-checks");
695+
run_check_toggler.addEventListener("click", function() {
696+
this.classList.toggle("toggler-extend");
697+
document.getElementById("run-checks-tree").classList.toggle("hidden");
698+
});
699+
686700
// When loaded, expand all CI services.<n> elements
687701
setExpandState(document.querySelectorAll(".tree-view-nested-list > * > .caret"), true);
688702
setExpandState(document.querySelectorAll(".tree-view-nested-list > * > * > * > .caret"), true);

src/macaron/output_reporter/templates/macaron.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,31 @@
164164
{% endcall %}
165165
{% endmacro %}
166166

167+
{#
168+
The `check-tree-node` class will add a small symbol before the check ID
169+
indicating a non-root node. Therefore, we don't apply it for the checks without
170+
parent (i.e the first level checks).
171+
#}
172+
{% macro render_run_checks(check_tree, run_checks) %}
173+
<ul class="tree-view-nested-list">
174+
{% for key, next_level in check_tree.items() recursive %}
175+
<li>
176+
<div
177+
class="{% if loop.depth0 != 0 %}check-tree-node{% endif %}"
178+
{% if key not in run_checks %}
179+
style="text-decoration-line: line-through;"
180+
{% endif %}>
181+
{{ key }}
182+
</div>
183+
<ul>
184+
{{ loop(next_level.items()) | indent(2 * 4) }}
185+
</ul>
186+
</li>
187+
{% endfor %}
188+
</ul>
189+
{% endmacro %}
190+
191+
167192
{# -------------------------------------------- #}
168193

169194
{#
@@ -236,6 +261,13 @@
236261
<div class="table_caption toggler" id="check_report_title">Reports for Macaron checks</div>
237262
<div id="check_report_content" class="hidden">
238263
{% endif %}
264+
<div class="table_sub_caption toggler" id="run-checks">Run checks</div>
265+
<div id="run-checks-tree" class="hidden">
266+
{{ render_run_checks(metadata.check_tree, metadata.run_checks) | indent(4, first=true) }}
267+
</div>
268+
269+
<div class="table_sub_caption">Checks report</div>
270+
239271
{{ render_checks_report(target.checks.results) | indent(8) }}
240272
</div>
241273

src/macaron/slsa_analyzer/analyzer.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
# To load all checks into the registry
3535
from macaron.slsa_analyzer.checks import * # pylint: disable=wildcard-import,unused-wildcard-import # noqa: F401,F403
36-
from macaron.slsa_analyzer.checks.check_result import CheckResult, SkippedInfo
36+
from macaron.slsa_analyzer.checks.check_result import CheckResult
3737
from macaron.slsa_analyzer.ci_service import CI_SERVICES
3838
from macaron.slsa_analyzer.database_store import store_analyze_context_to_db
3939
from macaron.slsa_analyzer.git_service import GIT_SERVICES, BaseGitService
@@ -74,6 +74,12 @@ def __init__(self, output_path: str, build_log_path: str) -> None:
7474
logger.error("Cannot start the analysis. Exiting ...")
7575
sys.exit(1)
7676

77+
logger.info(
78+
"The following checks are excluded based on the user configuration: %s",
79+
[check for check in registry.get_all_checks_mapping() if check not in registry.checks_to_run],
80+
)
81+
logger.info("The following checks will be run: %s", registry.checks_to_run)
82+
7783
self.output_path = output_path
7884

7985
# Prepare the directory to store all the build logs in the
@@ -881,11 +887,7 @@ def perform_checks(self, analyze_ctx: AnalyzeContext) -> dict[str, CheckResult]:
881887
)
882888
)
883889

884-
# TODO: Get the list of skipped checks from user configuration
885-
skipped_checks: list[SkippedInfo] = []
886-
887-
results = registry.scan(analyze_ctx, skipped_checks)
888-
890+
results = registry.scan(analyze_ctx)
889891
return results
890892

891893

0 commit comments

Comments
 (0)