|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +import sys |
| 6 | + |
| 7 | + |
| 8 | +def get_expected_arch(platform): |
| 9 | + """Map platform string to expected architecture value in SBOM""" |
| 10 | + platform_to_arch = { |
| 11 | + "linux/386": "386", |
| 12 | + "linux/amd64": "amd64", |
| 13 | + "linux/arm/v5": "arm", |
| 14 | + "linux/arm/v7": "arm", |
| 15 | + "linux/arm64/v8": "arm64", |
| 16 | + "linux/ppc64le": "ppc64le", |
| 17 | + "linux/riscv64": "riscv64", |
| 18 | + "linux/s390x": "s390x" |
| 19 | + } |
| 20 | + |
| 21 | + if platform not in platform_to_arch: |
| 22 | + raise ValueError(f"Unknown platform: {platform}") |
| 23 | + |
| 24 | + return platform_to_arch[platform] |
| 25 | + |
| 26 | + |
| 27 | +def extract_arch_from_sbom(sbom_file): |
| 28 | + """Extract architecture from SBOM metadata""" |
| 29 | + try: |
| 30 | + with open(sbom_file, 'r') as f: |
| 31 | + sbom = json.load(f) |
| 32 | + |
| 33 | + properties = sbom.get('metadata', {}).get('component', {}).get('properties', []) |
| 34 | + |
| 35 | + for prop in properties: |
| 36 | + if prop.get('name') == 'amazon:inspector:sbom_generator:image_arch': |
| 37 | + return prop.get('value') |
| 38 | + |
| 39 | + raise ValueError("Architecture property not found in SBOM") |
| 40 | + |
| 41 | + except Exception as e: |
| 42 | + raise ValueError(f"Failed to parse SBOM: {e}") |
| 43 | + |
| 44 | + |
| 45 | +def main(): |
| 46 | + parser = argparse.ArgumentParser(description='Validate SBOM architecture matches expected platform') |
| 47 | + parser.add_argument('--platform', required=True, help='Expected platform (e.g., linux/amd64)') |
| 48 | + parser.add_argument('--sbom', required=True, help='Path to SBOM file') |
| 49 | + |
| 50 | + args = parser.parse_args() |
| 51 | + |
| 52 | + try: |
| 53 | + expected_arch = get_expected_arch(args.platform) |
| 54 | + actual_arch = extract_arch_from_sbom(args.sbom) |
| 55 | + |
| 56 | + print(f"Platform: {args.platform}") |
| 57 | + print(f"Expected arch: {expected_arch}") |
| 58 | + print(f"Actual arch: {actual_arch}") |
| 59 | + |
| 60 | + if actual_arch != expected_arch: |
| 61 | + print(f" Architecture mismatch for platform {args.platform}") |
| 62 | + print(f" Expected: {expected_arch}") |
| 63 | + print(f" Found: {actual_arch}") |
| 64 | + sys.exit(1) |
| 65 | + |
| 66 | + print(f"Architecture validation passed: {actual_arch} matches expected {expected_arch}") |
| 67 | + |
| 68 | + except Exception as e: |
| 69 | + print(f"Validation failed: {e}") |
| 70 | + sys.exit(1) |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == '__main__': |
| 74 | + main() |
0 commit comments