forked from microsoft/agent-governance-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
198 lines (162 loc) · 5.67 KB
/
main.py
File metadata and controls
198 lines (162 loc) · 5.67 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
#!/usr/bin/env python3
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""
Agent Governance Toolkit CLI.
Commands:
verify Run OWASP ASI 2026 governance verification
integrity Verify or generate module integrity manifest
lint-policy Lint YAML policy files for common mistakes
"""
from __future__ import annotations
import argparse
from typing import Optional
import os
import sys
import json
def handle_error(e: Exception, output_json: bool = False, custom_msg: Optional[str] = None):
"""Centralized error handler for compliance CLI."""
is_known = isinstance(e, (IOError, ValueError, KeyError, PermissionError, FileNotFoundError))
if custom_msg:
err_msg = custom_msg
elif is_known:
err_msg = "A validation or file access error occurred."
else:
err_msg = "A governance processing error occurred."
if output_json:
print(json.dumps({"status": "fail" if not is_known else "error", "message": err_msg, "type": "ValidationError" if is_known else "InternalError"}, indent=2))
else:
print(f"Error: {err_msg}", file=sys.stderr)
def cmd_verify(args: argparse.Namespace) -> int:
"""Run governance verification."""
from agent_compliance.verify import GovernanceVerifier
try:
verifier = GovernanceVerifier()
attestation = verifier.verify()
if args.json:
print(attestation.to_json())
elif args.badge:
print(attestation.badge_markdown())
else:
print(attestation.summary())
return 0 if attestation.passed else 1
except Exception as e:
handle_error(e, args.json)
return 1
def cmd_integrity(args: argparse.Namespace) -> int:
"""Run integrity verification or generate manifest."""
from agent_compliance.integrity import IntegrityVerifier
try:
if args.generate and args.manifest:
print(
"Error: --manifest and --generate are mutually exclusive",
file=sys.stderr,
)
return 1
if args.generate:
verifier = IntegrityVerifier()
manifest = verifier.generate_manifest(args.generate)
print(f"Manifest written to {args.generate}")
print(f" Files hashed: {len(manifest['files'])}")
print(f" Functions hashed: {len(manifest['functions'])}")
return 0
if args.manifest and not os.path.exists(args.manifest):
print(
f"Error: manifest file not found: {args.manifest}",
file=sys.stderr,
)
return 1
verifier = IntegrityVerifier(manifest_path=args.manifest)
report = verifier.verify()
if args.json:
import json
print(json.dumps(report.to_dict(), indent=2))
else:
print(report.summary())
return 0 if report.passed else 1
except Exception as e:
handle_error(e, args.json)
return 1
def cmd_lint_policy(args: argparse.Namespace) -> int:
"""Lint YAML policy files for common mistakes."""
from agent_compliance.lint_policy import lint_path
try:
result = lint_path(args.path)
if args.json:
import json
print(json.dumps(result.to_dict(), indent=2))
else:
for msg in result.messages:
print(msg)
if result.messages:
print()
print(result.summary())
if args.strict and result.warnings:
return 1
return 0 if result.passed else 1
except Exception as e:
handle_error(e, args.json)
return 1
def main() -> int:
"""CLI entry point."""
parser = argparse.ArgumentParser(
prog="agent-compliance",
description="Agent Governance Toolkit — Compliance & Verification CLI",
)
subparsers = parser.add_subparsers(dest="command", help="Available commands")
# verify command
verify_parser = subparsers.add_parser(
"verify",
help="Run OWASP ASI 2026 governance verification",
)
verify_parser.add_argument(
"--json", action="store_true", help="Output JSON attestation"
)
verify_parser.add_argument(
"--badge", action="store_true", help="Output markdown badge only"
)
# integrity command
integrity_parser = subparsers.add_parser(
"integrity",
help="Verify or generate module integrity manifest",
)
integrity_parser.add_argument(
"--manifest", type=str, help="Path to integrity.json manifest to verify against"
)
integrity_parser.add_argument(
"--generate",
type=str,
metavar="OUTPUT_PATH",
help="Generate integrity manifest at the given path",
)
integrity_parser.add_argument(
"--json", action="store_true", help="Output JSON report"
)
# lint-policy command
lint_parser = subparsers.add_parser(
"lint-policy",
help="Lint YAML policy files for common mistakes",
)
lint_parser.add_argument(
"path", type=str, help="Path to a YAML policy file or directory"
)
lint_parser.add_argument(
"--json", action="store_true", help="Output JSON report"
)
lint_parser.add_argument(
"--strict",
action="store_true",
help="Treat warnings as errors (exit 1 if any warnings)",
)
args = parser.parse_args()
if args.command == "verify":
return cmd_verify(args)
elif args.command == "integrity":
return cmd_integrity(args)
elif args.command == "lint-policy":
return cmd_lint_policy(args)
else:
parser.print_help()
return 0
if __name__ == "__main__":
sys.exit(main())