|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""CycloneDX 1.6 CBOM helpers for scan-cbom.yml. |
| 3 | +
|
| 4 | + cbom.py rust CARGO_LOCK OUT crypto crates in a Cargo.lock -> CBOM |
| 5 | + cbom.py merge NAME VERSION OUT IN... merge CBOMs into one |
| 6 | + cbom.py label IN REF [PLATFORM] [INDEX] tag every finding with the scanned image |
| 7 | + cbom.py stamp IN NAME=VALUE... set metadata properties |
| 8 | + cbom.py gate IN [OUT] strip key values, fail on secret material |
| 9 | +""" |
| 10 | +import json |
| 11 | +import sys |
| 12 | +import tomllib |
| 13 | +import uuid |
| 14 | +from urllib.parse import quote |
| 15 | +from datetime import datetime, timezone |
| 16 | + |
| 17 | +# cbomkit has no Rust scanner. Map the crypto crates Cedarling resolves to the |
| 18 | +# algorithms/protocols they provide; 0 = broken by a cryptographically relevant |
| 19 | +# quantum computer (Shor). |
| 20 | +SIG = ["keygen", "sign", "verify"] |
| 21 | +RUST_CRATES = { |
| 22 | + "ed25519-dalek": [("Ed25519", {"primitive": "signature", "curve": "Ed25519", "cryptoFunctions": SIG, "nistQuantumSecurityLevel": 0})], |
| 23 | + "p256": [("ECDSA-P256", {"primitive": "signature", "curve": "secp256r1", "cryptoFunctions": SIG, "nistQuantumSecurityLevel": 0})], |
| 24 | + "p384": [("ECDSA-P384", {"primitive": "signature", "curve": "secp384r1", "cryptoFunctions": SIG, "nistQuantumSecurityLevel": 0})], |
| 25 | + "rsa": [("RSA", {"primitive": "signature", "cryptoFunctions": SIG, "nistQuantumSecurityLevel": 0})], |
| 26 | + "sha2": [(n, {"primitive": "hash", "cryptoFunctions": ["digest"]}) for n in ("SHA-256", "SHA-384", "SHA-512")], |
| 27 | + "sha3": [(n, {"primitive": "hash", "cryptoFunctions": ["digest"]}) for n in ("SHA3-256", "SHA3-512")], |
| 28 | + "hmac": [("HMAC", {"primitive": "mac", "cryptoFunctions": ["tag"]})], |
| 29 | + "hkdf": [("HKDF", {"primitive": "kdf", "cryptoFunctions": ["keyderive"]})], |
| 30 | +} |
| 31 | +RUST_PROTOCOLS = {"rustls": [("TLSv1.2", "1.2"), ("TLSv1.3", "1.3")]} |
| 32 | +RUST_PROVIDERS = {"ring", "aws-lc-rs"} |
| 33 | + |
| 34 | +CRATES_IO = {"registry+https://github.com/rust-lang/crates.io-index", "sparse+https://index.crates.io/"} |
| 35 | + |
| 36 | +BLOCKED = {"private-key", "secret-key", "shared-secret", "seed", "password", "credential", "token", "key"} |
| 37 | +# gitleaks generic-api-key false positive on stock JDK config (no value is captured). |
| 38 | +ALLOWED = {("key", "generic-api-key", "/conf/security/java.security")} |
| 39 | + |
| 40 | + |
| 41 | +def bom(components, dependencies, meta_component=None): |
| 42 | + meta = {"timestamp": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")} |
| 43 | + if meta_component: |
| 44 | + meta["component"] = meta_component |
| 45 | + return { |
| 46 | + "$schema": "http://cyclonedx.org/schema/bom-1.6.schema.json", |
| 47 | + "bomFormat": "CycloneDX", |
| 48 | + "specVersion": "1.6", |
| 49 | + "serialNumber": f"urn:uuid:{uuid.uuid4()}", |
| 50 | + "version": 1, |
| 51 | + "metadata": meta, |
| 52 | + "components": components, |
| 53 | + "dependencies": dependencies, |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | +def rust(lock_path, out): |
| 58 | + with open(lock_path, "rb") as f: |
| 59 | + packages = tomllib.load(f).get("package", []) |
| 60 | + components, dependencies, assets = [], [], {} |
| 61 | + for pkg in sorted(packages, key=lambda p: (p["name"], p["version"], p.get("source", ""))): |
| 62 | + name, version, source = pkg["name"], pkg["version"], pkg.get("source", "") |
| 63 | + if name not in RUST_CRATES and name not in RUST_PROTOCOLS and name not in RUST_PROVIDERS: |
| 64 | + continue |
| 65 | + ref = f"pkg:cargo/{name}@{version}" |
| 66 | + if source.startswith("git+"): |
| 67 | + ref += "?vcs_url=" + quote(source.split("#")[0], safe="") |
| 68 | + elif source and source not in CRATES_IO: |
| 69 | + ref += "?repository_url=" + quote(source.split("+", 1)[-1], safe="") |
| 70 | + components.append({"type": "library", "bom-ref": ref, "name": name, "version": version, "purl": ref}) |
| 71 | + provides = [] |
| 72 | + for algo, props in RUST_CRATES.get(name, []): |
| 73 | + aref = f"crypto/algorithm/{algo}" |
| 74 | + assets.setdefault(aref, {"type": "cryptographic-asset", "bom-ref": aref, "name": algo, |
| 75 | + "cryptoProperties": {"assetType": "algorithm", "algorithmProperties": props}}) |
| 76 | + provides.append(aref) |
| 77 | + for proto, ver in RUST_PROTOCOLS.get(name, []): |
| 78 | + pref = f"crypto/protocol/{proto}" |
| 79 | + assets.setdefault(pref, {"type": "cryptographic-asset", "bom-ref": pref, "name": proto, |
| 80 | + "cryptoProperties": {"assetType": "protocol", |
| 81 | + "protocolProperties": {"type": "tls", "version": ver}}}) |
| 82 | + provides.append(pref) |
| 83 | + if provides: |
| 84 | + dependencies.append({"ref": ref, "provides": provides}) |
| 85 | + components.extend(assets.values()) |
| 86 | + write(out, bom(components, dependencies)) |
| 87 | + |
| 88 | + |
| 89 | +def merge(name, version, out, inputs): |
| 90 | + # Allocated first so an input root carrying the same ref is not duplicated. |
| 91 | + aggregate = f"pkg:github/JanssenProject/jans@{version}#{name}" |
| 92 | + components, seen, deps, by_ref = [], {aggregate}, {}, {} |
| 93 | + for path in inputs: |
| 94 | + with open(path) as f: |
| 95 | + doc = json.load(f) |
| 96 | + root = (doc.get("metadata") or {}).get("component") |
| 97 | + # Keep each input's root so its dependency edges still resolve. |
| 98 | + for c in ([root] if root and root.get("bom-ref") else []) + doc.get("components", []): |
| 99 | + key = c.get("bom-ref") or json.dumps(c, sort_keys=True) |
| 100 | + if key in seen: |
| 101 | + # Same finding from another input (e.g. platform): keep its provenance too. |
| 102 | + kept = by_ref.get(key) |
| 103 | + if kept is not None: |
| 104 | + props = kept.setdefault("properties", []) |
| 105 | + props.extend(p for p in c.get("properties", []) if p not in props) |
| 106 | + continue |
| 107 | + seen.add(key) |
| 108 | + if c.get("bom-ref"): |
| 109 | + by_ref[key] = c |
| 110 | + components.append(c) |
| 111 | + for d in doc.get("dependencies", []): |
| 112 | + cur = deps.setdefault(d["ref"], {"ref": d["ref"]}) |
| 113 | + for key in ("dependsOn", "provides"): |
| 114 | + if d.get(key): |
| 115 | + cur[key] = sorted(set(cur.get(key, [])) | set(d[key])) |
| 116 | + meta = {"type": "application", "bom-ref": aggregate, "name": name, "version": version} |
| 117 | + write(out, bom(components, list(deps.values()), meta)) |
| 118 | + |
| 119 | + |
| 120 | +def walk(components): |
| 121 | + for c in components: |
| 122 | + yield c |
| 123 | + yield from walk(c.get("components", [])) |
| 124 | + |
| 125 | + |
| 126 | +def label(path, image, platform="", index=""): |
| 127 | + with open(path) as f: |
| 128 | + doc = json.load(f) |
| 129 | + props = [{"name": "jans:image", "value": image}] |
| 130 | + if platform: |
| 131 | + props.append({"name": "jans:platform", "value": platform}) |
| 132 | + for c in walk(doc.get("components", [])): |
| 133 | + c.setdefault("properties", []).extend(props) |
| 134 | + meta = doc.setdefault("metadata", {}) |
| 135 | + old = meta.get("component") |
| 136 | + if old and old.get("bom-ref"): |
| 137 | + # Keep the scanner's root so its dependency edges still resolve. |
| 138 | + doc["components"].append(old) |
| 139 | + name, _, digest = image.partition("@") |
| 140 | + container = {"type": "container", "bom-ref": image, "name": name, "version": digest or name, |
| 141 | + "properties": props + ([{"name": "jans:image-index", "value": index}] if index else [])} |
| 142 | + meta["component"] = container |
| 143 | + write(path, doc) |
| 144 | + |
| 145 | + |
| 146 | +def stamp(path, pairs): |
| 147 | + with open(path) as f: |
| 148 | + doc = json.load(f) |
| 149 | + new = dict(p.split("=", 1) for p in pairs) |
| 150 | + meta = doc.setdefault("metadata", {}) |
| 151 | + meta["properties"] = [p for p in meta.get("properties", []) if p.get("name") not in new] + [ |
| 152 | + {"name": k, "value": v} for k, v in new.items()] |
| 153 | + write(path, doc) |
| 154 | + |
| 155 | + |
| 156 | +def allowed(kind, name, locs): |
| 157 | + return bool(locs) and all( |
| 158 | + any(kind == a and name == n and loc.endswith(sfx) for a, n, sfx in ALLOWED) for loc in locs) |
| 159 | + |
| 160 | + |
| 161 | +def gate(path, out=None): |
| 162 | + with open(path) as f: |
| 163 | + doc = json.load(f) |
| 164 | + blocked = [] |
| 165 | + root = (doc.get("metadata") or {}).get("component") |
| 166 | + for c in walk(([root] if root else []) + doc.get("components", [])): |
| 167 | + rcm = (c.get("cryptoProperties") or {}).get("relatedCryptoMaterialProperties") |
| 168 | + if not rcm: |
| 169 | + continue |
| 170 | + # Never publish key material, even public keys: bulky and unneeded for an inventory. |
| 171 | + rcm.pop("value", None) |
| 172 | + locs = [o.get("location") or "" for o in (c.get("evidence") or {}).get("occurrences", [])] |
| 173 | + kind = rcm.get("type") |
| 174 | + if kind in BLOCKED and not allowed(kind, c.get("name"), locs): |
| 175 | + blocked.append(f"{kind}: {c.get('name')} at {', '.join(filter(None, locs)) or 'unknown'}") |
| 176 | + write(out or path, doc) |
| 177 | + if blocked: |
| 178 | + print(f"::error::{len(blocked)} secret crypto material finding(s); not publishing", file=sys.stderr) |
| 179 | + for b in blocked: |
| 180 | + print(f" {b}", file=sys.stderr) |
| 181 | + sys.exit(1) |
| 182 | + |
| 183 | + |
| 184 | +def write(path, doc): |
| 185 | + with open(path, "w") as f: |
| 186 | + json.dump(doc, f, indent=1) |
| 187 | + f.write("\n") |
| 188 | + print(f"{path}: {len(doc.get('components', []))} components") |
| 189 | + |
| 190 | + |
| 191 | +if __name__ == "__main__": |
| 192 | + cmd, args = sys.argv[1], sys.argv[2:] |
| 193 | + if cmd == "rust": |
| 194 | + rust(*args) |
| 195 | + elif cmd == "merge": |
| 196 | + merge(args[0], args[1], args[2], args[3:]) |
| 197 | + elif cmd == "label": |
| 198 | + label(*args) |
| 199 | + elif cmd == "stamp": |
| 200 | + stamp(args[0], args[1:]) |
| 201 | + elif cmd == "gate": |
| 202 | + gate(*args) |
| 203 | + else: |
| 204 | + sys.exit(__doc__) |
0 commit comments