-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_governance_artifact.py
More file actions
executable file
·323 lines (263 loc) · 9.5 KB
/
Copy pathvalidate_governance_artifact.py
File metadata and controls
executable file
·323 lines (263 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env python3
"""Validator for enterprise AI governance artifact package."""
from __future__ import annotations
import argparse
import datetime
import hashlib
import importlib
import importlib.util
import json
from pathlib import Path
import re
import shlex
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import ParseError
import yaml
from governance_artifact_constants import (
DEFAULT_CICD,
DEFAULT_JSON,
DEFAULT_MANIFEST,
DEFAULT_REPORT,
DEFAULT_SCHEMA,
DEFAULT_YAML,
MANIFEST_TRACKED_FILES,
)
TOOL_VERSION = "1.2.0"
REQUIRED_TOP_LEVEL = [
"meta",
"pillars",
"regulatory_alignment",
"control_stack",
"cicd_policy_gates",
"kpis",
"control_catalog",
"deterministic_replay_workflow",
]
REQUIRED_CICD_GATES = {
"code_gate",
"data_gate",
"model_gate",
"risk_gate",
"compliance_gate",
"release_gate",
"runtime_gate",
}
def fail(msg: str) -> None:
print(f"ERROR: {msg}")
raise SystemExit(1)
def ensure_exists(path: Path) -> None:
if not path.exists():
fail(f"required file missing: {path}")
def load_yaml(path: Path) -> object:
return yaml.safe_load(path.read_text())
def load_json(path: Path) -> object:
return json.loads(path.read_text())
def validate_primary_artifact(data: dict) -> None:
if not isinstance(data, dict):
fail("artifact root must be a mapping")
missing = [k for k in REQUIRED_TOP_LEVEL if k not in data]
if missing:
fail(f"missing required top-level keys: {missing}")
if len(data["pillars"]) < 5:
fail("expected at least 5 pillars")
if len(data["regulatory_alignment"]) < 5:
fail("expected at least 5 regulatory alignments")
if len(data["cicd_policy_gates"]) < 5:
fail("expected at least 5 CI/CD policy gates")
if len(data["control_catalog"]) < 3:
fail("expected at least 3 controls in catalog")
if len(data["deterministic_replay_workflow"]) < 5:
fail("deterministic replay workflow too short")
for i, control in enumerate(data["control_catalog"], start=1):
for field in ("id", "domain", "requirement", "enforcement", "evidence"):
if field not in control:
fail(f"control[{i}] missing field: {field}")
meta = data["meta"]
if not re.match(r"^\d{4}-\d{2}-\d{2}$", str(meta.get("date", ""))):
fail("meta.date must be ISO format YYYY-MM-DD")
def validate_schema_contract(schema: dict) -> None:
if not isinstance(schema, dict):
fail("schema file must be a JSON object")
required = schema.get("required", [])
if not isinstance(required, list):
fail("schema.required must be a list")
missing = [k for k in REQUIRED_TOP_LEVEL if k not in required]
if missing:
fail(f"schema.required missing expected keys: {missing}")
def normalize_for_schema(value: object) -> object:
if isinstance(value, datetime.date):
return value.isoformat()
if isinstance(value, dict):
return {k: normalize_for_schema(v) for k, v in value.items()}
if isinstance(value, list):
return [normalize_for_schema(v) for v in value]
return value
def validate_against_schema(schema: dict, artifact: dict) -> None:
if importlib.util.find_spec("jsonschema") is None:
fail(
"jsonschema dependency missing. Install with: pip install -r requirements-dev.txt"
)
jsonschema = importlib.import_module("jsonschema")
exceptions = importlib.import_module("jsonschema.exceptions")
normalized = normalize_for_schema(artifact)
try:
jsonschema.validate(instance=normalized, schema=schema)
except exceptions.ValidationError as exc:
fail(f"schema validation failed: {exc.message}")
def validate_cicd_example(manifest: dict) -> None:
if not isinstance(manifest, dict):
fail("CI/CD example must be a mapping")
gates = manifest.get("required_gates", [])
if not isinstance(gates, list):
fail("required_gates must be a list")
gate_names = {item.get("name") for item in gates if isinstance(item, dict)}
missing = sorted(REQUIRED_CICD_GATES - gate_names)
if missing:
fail(f"CI/CD example missing required gates: {missing}")
export = manifest.get("policy_decision_export", {})
if export.get("sink") != "kafka":
fail("policy_decision_export.sink must be kafka")
def validate_report_template(path: Path) -> None:
text = path.read_text().strip()
wrapped = f"<root>{text}</root>"
try:
root = ET.fromstring(wrapped)
except ParseError as exc:
fail(f"report template XML is invalid: {exc}")
expected = ["title", "abstract", "content"]
tags = [child.tag for child in root]
if tags != expected:
fail(f"report template top-level tags must be {expected}, got {tags}")
def sha256_of(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as f:
for chunk in iter(lambda: f.read(8192), b""):
digest.update(chunk)
return digest.hexdigest()
def validate_manifest(root: Path, manifest_path: Path) -> None:
manifest = load_json(manifest_path)
if manifest.get("version") != 1:
fail("manifest version must be 1")
if manifest.get("algorithm") != "sha256":
fail("manifest algorithm must be sha256")
entries = manifest.get("entries", [])
if not isinstance(entries, list) or len(entries) == 0:
fail("manifest entries must be a non-empty list")
by_path: dict[str, str] = {}
for entry in entries:
rel = entry.get("path")
expected = entry.get("sha256")
if not rel or not expected:
fail("manifest entries require path and sha256")
if rel in by_path:
fail(f"manifest has duplicate path entry: {rel}")
by_path[rel] = expected
expected_paths = sorted(MANIFEST_TRACKED_FILES)
observed_paths = sorted(by_path.keys())
if observed_paths != expected_paths:
fail("manifest entries do not match expected tracked files")
for rel in expected_paths:
target = root / rel
if not target.exists():
fail(f"manifest references missing file: {rel}")
actual = sha256_of(target)
if actual != by_path[rel]:
fail(f"manifest hash mismatch for {rel}")
def validate_yaml_json_parity(
yaml_artifact: dict, json_artifact: dict, artifact_yaml: str, artifact_json: str
) -> None:
normalized_yaml = normalize_for_schema(yaml_artifact)
if normalized_yaml != json_artifact:
remediation = (
"YAML/JSON artifact mismatch: run "
"scripts/export_governance_artifact_json.py --root . "
f"--yaml {shlex.quote(artifact_yaml)} --json {shlex.quote(artifact_json)}"
)
fail(remediation)
def validate_package(
root: Path,
artifact_yaml: str,
artifact_json: str,
schema_file: str,
cicd_manifest: str,
report_template: str,
manifest_file: str,
skip_manifest: bool,
) -> None:
artifact_path = root / artifact_yaml
json_artifact_path = root / artifact_json
schema_path = root / schema_file
cicd_path = root / cicd_manifest
report_path = root / report_template
manifest_path = root / manifest_file
required_paths = [
artifact_path,
json_artifact_path,
schema_path,
cicd_path,
report_path,
]
if not skip_manifest:
required_paths.append(manifest_path)
for path in required_paths:
ensure_exists(path)
artifact = load_yaml(artifact_path)
json_artifact = load_json(json_artifact_path)
schema = load_json(schema_path)
cicd = load_yaml(cicd_path)
if not skip_manifest:
validate_manifest(root, manifest_path)
validate_primary_artifact(artifact)
validate_yaml_json_parity(artifact, json_artifact, artifact_yaml, artifact_json)
validate_schema_contract(schema)
validate_against_schema(schema, artifact)
validate_cicd_example(cicd)
validate_report_template(report_path)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Validate governance artifact package")
parser.add_argument("--root", default=".", help="Repository root path")
parser.add_argument(
"--yaml", default=DEFAULT_YAML, help="YAML artifact path relative to --root"
)
parser.add_argument(
"--json", default=DEFAULT_JSON, help="JSON artifact path relative to --root"
)
parser.add_argument(
"--schema", default=DEFAULT_SCHEMA, help="Schema path relative to --root"
)
parser.add_argument(
"--cicd", default=DEFAULT_CICD, help="CI/CD manifest path relative to --root"
)
parser.add_argument(
"--report",
default=DEFAULT_REPORT,
help="Report template path relative to --root",
)
parser.add_argument(
"--manifest", default=DEFAULT_MANIFEST, help="Manifest path relative to --root"
)
parser.add_argument(
"--skip-manifest", action="store_true", help="Skip manifest hash validation"
)
parser.add_argument(
"--version",
action="version",
version=f"validate_governance_artifact.py {TOOL_VERSION}",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
root = Path(args.root).resolve()
validate_package(
root,
args.yaml,
args.json,
args.schema,
args.cicd,
args.report,
args.manifest,
args.skip_manifest,
)
print("OK: enterprise AI governance package validation passed")
if __name__ == "__main__":
main()