# SPDX-License-Identifier: Apache-2.0 """Build the taxonomy and playbooks Parquet tables from a SOVA-OSS checkout. Usage: python scripts/build_tables.py PATH_TO_SOVA_OSS_CHECKOUT [--out data] The tables are built from the package data, not from the generated Markdown: - taxonomy: src/sova/contracts/data/attack-taxonomy-0.1.0.toml - playbooks: src/sova/methodology/data/playbook-seeds-0.1.0.json The external mappings are kept as identifiers. The taxonomy source also stores the title of each external entry; that title is text from the outside project and is left out of the table on purpose. Requires Python 3.11 or later and pyarrow. """ from __future__ import annotations import argparse import hashlib import json import re import sys import tomllib from collections import Counter from pathlib import Path import pyarrow as pa import pyarrow.parquet as pq TAXONOMY_FILE = Path("src/sova/contracts/data/attack-taxonomy-0.1.0.toml") PLAYBOOK_FILE = Path("src/sova/methodology/data/playbook-seeds-0.1.0.json") TAXON_ID = re.compile(r"^SOVA-ATK-[0-9]{3}$") MAPPING_KEY = re.compile(r"^[A-Z0-9-]+:.+$") TAXONOMY_SCHEMA = pa.schema( [ ("id", pa.string()), ("title", pa.string()), ("definition", pa.string()), ("status", pa.string()), ("replacement", pa.string()), ("in_standard_profile", pa.bool_()), ( "mappings", pa.list_( pa.struct( [ ("framework", pa.string()), ("catalog_version", pa.string()), ("identifier", pa.string()), ("relationship", pa.string()), ("url", pa.string()), ] ) ), ), ("taxonomy_id", pa.string()), ("taxonomy_version", pa.string()), ("taxonomy_status", pa.string()), ("taxonomy_owner", pa.string()), ("taxonomy_released", pa.string()), ] ) PLAYBOOK_SCHEMA = pa.schema( [ ("playbook_id", pa.string()), ("domain", pa.string()), ("slug", pa.string()), ("title", pa.string()), ("technique", pa.string()), ("oracle", pa.string()), ("risk", pa.int8()), ("tags", pa.list_(pa.string())), ("mappings", pa.list_(pa.string())), ("effects", pa.list_(pa.string())), ("catalog_id", pa.string()), ("catalog_version", pa.string()), ("catalog_released", pa.string()), ] ) def sha256_file(path: Path) -> str: return hashlib.sha256(path.read_bytes()).hexdigest() def taxonomy_rows(raw: dict) -> list[dict]: profile = list(raw["standard_profile"]) rows = [] for taxon in raw["taxon"]: identifier = str(taxon["id"]) if TAXON_ID.fullmatch(identifier) is None: raise SystemExit(f"unexpected taxon id: {identifier}") mappings = [ { "framework": str(item["framework"]), "catalog_version": str(item["catalog_version"]), "identifier": str(item["identifier"]), "relationship": str(item["relationship"]), "url": str(item["url"]), } for item in taxon.get("mapping", []) ] replacement = taxon.get("replacement") rows.append( { "id": identifier, "title": str(taxon["title"]), "definition": str(taxon["definition"]), "status": str(taxon["status"]), "replacement": None if replacement is None else str(replacement), "in_standard_profile": identifier in profile, "mappings": mappings, "taxonomy_id": str(raw["taxonomy_id"]), "taxonomy_version": str(raw["version"]), "taxonomy_status": str(raw["status"]), "taxonomy_owner": str(raw["owner"]), "taxonomy_released": str(raw["released"]), } ) identifiers = [row["id"] for row in rows] if len(identifiers) != len(set(identifiers)): raise SystemExit("duplicate taxon id") return rows def playbook_rows(raw: dict) -> list[dict]: if raw.get("artifactType") != "sova.playbook-seed-catalog": raise SystemExit("not a sova.playbook-seed-catalog file") rows = [] for domain, seeds in raw["domains"].items(): for seed in seeds: mappings = [str(item) for item in seed["mappings"]] for item in mappings: if MAPPING_KEY.fullmatch(item) is None: raise SystemExit(f"unexpected mapping key: {item}") rows.append( { "playbook_id": f"sova.playbook.{domain}.{seed['slug']}", "domain": str(domain), "slug": str(seed["slug"]), "title": str(seed["title"]), "technique": str(seed["technique"]), "oracle": str(seed["oracle"]), "risk": int(seed["risk"]), "tags": [str(item) for item in seed["tags"]], "mappings": mappings, "effects": [str(item) for item in seed.get("effects", [])], "catalog_id": str(raw["id"]), "catalog_version": str(raw["version"]), "catalog_released": str(raw["released"]), } ) identifiers = [row["playbook_id"] for row in rows] if len(identifiers) != len(set(identifiers)): raise SystemExit("duplicate playbook id") return rows def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("source", type=Path, help="path to a SOVA-OSS checkout") parser.add_argument( "--out", type=Path, default=Path(__file__).resolve().parent.parent / "data", help="output folder (default: data/ next to scripts/)", ) args = parser.parse_args(argv) taxonomy_path = args.source / TAXONOMY_FILE playbook_path = args.source / PLAYBOOK_FILE taxonomy_raw = tomllib.loads(taxonomy_path.read_text(encoding="utf-8")) playbook_raw = json.loads(playbook_path.read_text(encoding="utf-8")) taxonomy = taxonomy_rows(taxonomy_raw) playbooks = playbook_rows(playbook_raw) args.out.mkdir(parents=True, exist_ok=True) pq.write_table( pa.Table.from_pylist(taxonomy, schema=TAXONOMY_SCHEMA), args.out / "taxonomy.parquet", ) pq.write_table( pa.Table.from_pylist(playbooks, schema=PLAYBOOK_SCHEMA), args.out / "playbooks.parquet", ) print(f"source {TAXONOMY_FILE.as_posix()} sha256 {sha256_file(taxonomy_path)}") print(f"source {PLAYBOOK_FILE.as_posix()} sha256 {sha256_file(playbook_path)}") print( f"taxonomy rows {len(taxonomy)} " f"({taxonomy[0]['id']} to {taxonomy[-1]['id']}), " f"version {taxonomy_raw['version']}, released {taxonomy_raw['released']}, " f"status {taxonomy_raw['status']}, " f"mappings {sum(len(row['mappings']) for row in taxonomy)}" ) print( f"playbooks rows {len(playbooks)}, " f"catalogue version {playbook_raw['version']}, released {playbook_raw['released']}" ) for domain, count in Counter(row["domain"] for row in playbooks).items(): print(f" domain {domain} {count}") return 0 if __name__ == "__main__": sys.exit(main())