Skip to content

Commit 8c2d4d7

Browse files
bluesentinelsecMichael Long
andauthored
Add platform argument for container image scans (#102)
* add --platform support for multi-arch containers * test multi-arch images on current branch * test actions against sbomgen 1.5.1-beta * fix --platform parsing error * fix platform parsing bug * test workflows on sbomgen latest (1.5.2) * Validate --platform input * Add more test cases, and revert workflow definitions * fix typo in platform arg --------- Co-authored-by: Michael Long <[email protected]>
1 parent d771038 commit 8c2d4d7

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

.github/workflows/test_containers.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ jobs:
3636
with:
3737
artifact_type: 'container'
3838
artifact_path: 'ubuntu:14.04'
39+
platform: "linux/arm64"
3940
display_vulnerability_findings: "enabled"
4041
sbomgen_version: "latest"
4142

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ inputs:
106106
required: False
107107
default: 600 # 10 minutes
108108

109+
platform:
110+
description: "Specifies the OS and CPU arch of the container image you wish to scan. Valid inputs are of the form 'os/cpu/variant' for example, 'linux/amd64', 'linux/arm64/v8', etc. If no platform is specified, the system will use the same platform as the host that is performing the scan. This argument only affects container image scans. Requires inspector-sbomgen 1.5.1 or later."
111+
required: False
112+
109113
outputs:
110114
artifact_sbom:
111115
description: "The filepath to the artifact's software bill of materials."

entrypoint/entrypoint/cli.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ def init(sys_argv=None) -> argparse.Namespace:
5151
parser.add_argument("--timeout", type=str, default="600",
5252
help="The amount of time in seconds that inspector-sbomgne will run. When this timeout is exceeded, sbomgen will gracefully conclude and present any findings discovered up to that point.")
5353

54+
parser.add_argument("--platform", type=str,
55+
help="Specifies the OS and CPU arch of the container image you wish to scan. Valid inputs are "
56+
"of the form 'os/cpu/variant' for example, 'linux/amd64', 'linux/arm64/v8', etc. If no platform is "
57+
"specified, the system will use the same platform as the host that is performing the "
58+
"scan. This argument only affects container image scans. Requires inspector-sbomgen "
59+
"1.5.1 or later.")
60+
5461
args = ""
5562
if sys_argv:
5663
args = parser.parse_args(sys_argv)

entrypoint/entrypoint/orchestrator.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import shutil
77
import sys
88
import tempfile
9+
import re
910

1011
from entrypoint import dockerfile, executor, exporter, installer, pkg_vuln
1112

@@ -195,6 +196,15 @@ def invoke_sbomgen(args) -> int:
195196
sbomgen_args.append("--skip-files")
196197
sbomgen_args.append(args.skip_files)
197198

199+
if args.artifact_type == "container":
200+
201+
if args.platform:
202+
platform_arg = args.platform.lower()
203+
if not is_valid_container_platform(platform_arg):
204+
logging.fatal(f"received invalid container image platform: '{args.platform}'. Platform should be of the form 'os/cpu/variant' such as 'linux/amd64' or 'linux/arm64/v8'")
205+
sbomgen_args.append("--platform")
206+
sbomgen_args.append(platform_arg)
207+
198208
ret = executor.invoke_command(sbomgen, sbomgen_args)
199209
if ret != 0:
200210
return ret
@@ -441,3 +451,9 @@ def require_true(expr: bool, msg: str):
441451
if not expr:
442452
logging.error(msg)
443453
exit(1)
454+
455+
def is_valid_container_platform(img_platform):
456+
# regex for detecting 'os/cpu/variant'
457+
# os/cpu are required whereas variant is optional
458+
pattern = r'^[^/]+/[^/]+(?:/[^/]+)?$'
459+
return bool(re.match(pattern, img_platform))

entrypoint/tests/test_orchestrator.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,22 @@ def test_get_sbomgen_arch(self):
185185
result = orchestrator.get_sbomgen_arch(each_test["input"])
186186
self.assertEqual(result, each_test["expected"])
187187

188+
def test_is_valid_container_platform(self):
189+
190+
test_cases = [
191+
# valid input
192+
{"input": "linux/amd64", "expected": True},
193+
{"input": "linux/arm64/v8", "expected": True},
194+
# test malformed input
195+
{"input": "linux", "expected": False},
196+
{"input": "garbage garbage garbage", "expected": False},
197+
{"input": "garbage / garbage / garbage /", "expected": False},
198+
{"input": "linux/amd64/slim/garbage", "expected": False},
199+
]
200+
201+
for each_test in test_cases:
202+
result = orchestrator.is_valid_container_platform(each_test["input"])
203+
self.assertEqual(result, each_test["expected"])
188204

189205
if __name__ == "__main__":
190206
unittest.main()

0 commit comments

Comments
 (0)