Skip to content

Commit 90e33ef

Browse files
AbrilRBSmemsharded
authored andcommitted
Add --context={build,host} filter to conan audit scan (conan-io#18976)
* Add context filter to conan scan * Rename to both * Update to noth have a both argument * Simplify testing
1 parent 5894829 commit 90e33ef

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

conan/api/subapi/audit.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import base64
55

6+
from conan.api.output import ConanOutput
67
from conan.internal.api.audit.providers import ConanCenterProvider, PrivateProvider
78
from conan.errors import ConanException
89
from conan.internal.api.remotes.encrypt import encode, decode
@@ -28,12 +29,14 @@ def __init__(self, conan_api):
2829
}
2930

3031
@staticmethod
31-
def scan(deps_graph, provider):
32+
def scan(deps_graph, provider, context=None):
3233
"""
3334
Scan a given recipe for vulnerabilities in its dependencies.
3435
"""
3536
refs = sorted(set(RecipeReference.loads(f"{node.ref.name}/{node.ref.version}")
36-
for node in deps_graph.nodes[1:]), key=lambda ref: ref.name)
37+
for node in deps_graph.nodes[1:]
38+
if context is None or node.context == context),
39+
key=lambda ref: ref.name)
3740
return provider.get_cves(refs)
3841

3942
@staticmethod

conan/cli/commands/audit.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ def audit_scan(conan_api: ConanAPI, parser, subparser, *args) -> dict:
6060
help="Set threshold for severity level to raise an error. "
6161
"By default raises an error for any critical CVSS (9.0 or higher). "
6262
" Use 100.0 to disable it.")
63+
subparser.add_argument("--context", help="Context to scan, by default both contexts are scanned "
64+
"if not specified",
65+
choices=["host", "build"], default=None)
6366

6467
_add_provider_arg(subparser)
6568
args = parser.parse_args(*args)
@@ -97,7 +100,7 @@ def audit_scan(conan_api: ConanAPI, parser, subparser, *args) -> dict:
97100

98101
provider = conan_api.audit.get_provider(args.provider or CONAN_CENTER_AUDIT_PROVIDER_NAME)
99102

100-
scan_result = conan_api.audit.scan(deps_graph, provider)
103+
scan_result = conan_api.audit.scan(deps_graph, provider, args.context)
101104
_parse_error_threshold(scan_result, args.severity_level)
102105
return scan_result
103106

test/integration/command/test_audit.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,3 +479,23 @@ def test_parse_error_crash_when_no_edges():
479479
assert "conan_error" in scan_result
480480
assert "zlib/1.2.11" in scan_result["conan_error"]
481481
assert "7.0" in scan_result["conan_error"]
482+
483+
484+
@pytest.mark.parametrize("package_context", ["build", "host"])
485+
@pytest.mark.parametrize("filter_context", ["build", "host", None])
486+
def test_audit_scan_context_filter(package_context, filter_context):
487+
tc = TestClient(light=True)
488+
489+
tc.save({"conanfile.py": GenConanfile("zlib", "1.2.11")})
490+
tc.run("export .")
491+
tc.run("audit provider auth conancenter --token=valid_token")
492+
493+
requires = "requires" if package_context == "host" else "tool-requires"
494+
context = "" if filter_context is None else f"--context={filter_context}"
495+
496+
with proxy_response(200, {}):
497+
tc.run(f"audit scan --{requires}=zlib/1.2.11 {context}")
498+
if filter_context is None or filter_context == package_context:
499+
assert "Requesting vulnerability info for: zlib/1.2.11" in tc.out
500+
else:
501+
assert "Requesting vulnerability info for: zlib/1.2.11" not in tc.out

0 commit comments

Comments
 (0)