-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvalidate_signatures.py
More file actions
executable file
·430 lines (357 loc) · 14.9 KB
/
validate_signatures.py
File metadata and controls
executable file
·430 lines (357 loc) · 14.9 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/usr/bin/env python3
"""
Schema Signature Validation Utility
This script validates all signed schemas in the codebase, checking signature
integrity and compatibility with the verification system.
Usage:
python scripts/validate_signatures.py
python scripts/validate_signatures.py --search-paths src tests
python scripts/validate_signatures.py --output validation_report.json
"""
import argparse
import ast
import importlib.util
import json
import logging
import sys
from pathlib import Path
from typing import Any
from collections.abc import Callable
# Add src to path for imports
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
from mockloop_mcp.schemapin.signing import SchemaSigner
from mockloop_mcp.schemapin.decorators import (
get_tool_signature,
get_tool_domain,
get_tool_schema,
get_tool_public_key
)
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
class SignatureValidator:
"""Validates schema signatures in the codebase."""
def __init__(self, search_paths: list[Path] | None = None):
"""Initialize signature validator."""
self.search_paths = search_paths or [Path("src")]
self.validation_results: list[dict[str, Any]] = []
self.valid_count = 0
self.invalid_count = 0
self.unsigned_count = 0
self.error_count = 0
def validate_all_signatures(self) -> dict[str, Any]:
"""Validate all signed schemas in the search paths."""
logger.info(f"Validating signatures in paths: {self.search_paths}")
signed_tools = self._discover_signed_tools()
if not signed_tools:
logger.warning("No signed tools found")
return self._generate_summary()
logger.info(f"Found {len(signed_tools)} signed tools")
for tool_info in signed_tools:
try:
result = self._validate_tool_signature(tool_info)
self.validation_results.append(result)
if result["status"] == "valid":
self.valid_count += 1
elif result["status"] == "invalid":
self.invalid_count += 1
elif result["status"] == "unsigned":
self.unsigned_count += 1
else:
self.error_count += 1
except Exception as e:
logger.exception(f"Error validating {tool_info['name']}")
self.error_count += 1
self.validation_results.append({
"tool_name": tool_info["name"],
"status": "error",
"error": str(e),
"file_path": tool_info["file_path"]
})
return self._generate_summary()
def _discover_signed_tools(self) -> list[dict[str, Any]]:
"""Discover all signed tools in the search paths."""
signed_tools = []
for search_path in self.search_paths:
if not search_path.exists():
logger.warning(f"Search path does not exist: {search_path}")
continue
signed_tools.extend(self._scan_directory(search_path))
return signed_tools
def _scan_directory(self, directory: Path) -> list[dict[str, Any]]:
"""Recursively scan directory for signed tools."""
signed_tools = []
for file_path in directory.rglob("*.py"):
if file_path.name.startswith("__"):
continue
try:
tools = self._scan_file(file_path)
signed_tools.extend(tools)
except Exception as e:
logger.debug(f"Error scanning {file_path}: {e}")
return signed_tools
def _scan_file(self, file_path: Path) -> list[dict[str, Any]]:
"""Scan a Python file for signed tools."""
signed_tools = []
try:
# Read and parse the file
with open(file_path, encoding='utf-8') as f:
content = f.read()
tree = ast.parse(content)
# Look for function definitions with @signed_tool decorator
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef) and self._has_signed_tool_decorator(node):
tool_info = {
"name": node.name,
"file_path": str(file_path),
"line_number": node.lineno,
"is_async": isinstance(node, ast.AsyncFunctionDef),
}
signed_tools.append(tool_info)
except Exception as e:
logger.debug(f"Error parsing {file_path}: {e}")
return signed_tools
def _has_signed_tool_decorator(self, node: ast.FunctionDef) -> bool:
"""Check if function has @signed_tool decorator."""
for decorator in node.decorator_list:
if (isinstance(decorator, ast.Call) and
isinstance(decorator.func, ast.Name) and
decorator.func.id == "signed_tool"):
return True
return False
def _validate_tool_signature(self, tool_info: dict[str, Any]) -> dict[str, Any]:
"""Validate signature for a single tool."""
tool_name = tool_info["name"]
try:
# Load the function
func = self._load_function_from_file(tool_info["file_path"], tool_name)
if not func:
return {
"tool_name": tool_name,
"status": "error",
"error": "Could not load function",
"file_path": tool_info["file_path"]
}
# Check if function has signature metadata
signature = get_tool_signature(func)
domain = get_tool_domain(func)
schema = get_tool_schema(func)
public_key = get_tool_public_key(func)
if not signature:
return {
"tool_name": tool_name,
"status": "unsigned",
"error": "No signature found in function metadata",
"file_path": tool_info["file_path"]
}
# Validate signature integrity
validation_result = self._validate_signature_integrity(
schema, signature, domain, public_key
)
return {
"tool_name": tool_name,
"status": "valid" if validation_result["valid"] else "invalid",
"domain": domain,
"signature_length": len(signature) if signature else 0,
"schema_hash": validation_result.get("schema_hash"),
"integrity_check": validation_result,
"file_path": tool_info["file_path"],
"line_number": tool_info["line_number"]
}
except Exception as e:
logger.exception(f"Error validating {tool_name}")
return {
"tool_name": tool_name,
"status": "error",
"error": str(e),
"file_path": tool_info["file_path"]
}
def _validate_signature_integrity(self, schema: dict[str, Any] | None,
signature: str | None, domain: str | None,
public_key: str | None) -> dict[str, Any]:
"""Validate signature integrity using basic checks."""
if not schema or not signature:
return {
"valid": False,
"error": "Missing schema or signature"
}
try:
# Basic format checks
if not signature or len(signature) < 10:
return {
"valid": False,
"error": "Invalid signature format"
}
# Check if signature is base64 encoded
import base64
try:
decoded = base64.b64decode(signature)
if len(decoded) < 32: # Minimum signature length
return {
"valid": False,
"error": "Signature too short"
}
except Exception:
return {
"valid": False,
"error": "Invalid base64 signature"
}
# Generate schema hash for comparison
temp_signer = SchemaSigner(private_key_content="dummy") # Won't be used
canonical_schema = temp_signer.canonicalize_schema(schema)
schema_hash = temp_signer.hash_schema(canonical_schema)
return {
"valid": True,
"schema_hash": schema_hash.hex(),
"signature_format": "base64",
"signature_length": len(decoded),
"domain": domain,
"has_public_key": bool(public_key)
}
except Exception as e:
return {
"valid": False,
"error": f"Validation error: {e}"
}
def _load_function_from_file(self, file_path: str, function_name: str) -> Callable | None:
"""Load a function from a Python file."""
try:
spec = importlib.util.spec_from_file_location("module", file_path)
if not spec or not spec.loader:
return None
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return getattr(module, function_name, None)
except Exception as e:
logger.debug(f"Error loading function {function_name} from {file_path}: {e}")
return None
def _generate_summary(self) -> dict[str, Any]:
"""Generate validation summary."""
total_tools = len(self.validation_results)
return {
"status": "completed",
"total_tools": total_tools,
"valid_signatures": self.valid_count,
"invalid_signatures": self.invalid_count,
"unsigned_tools": self.unsigned_count,
"validation_errors": self.error_count,
"signature_coverage": (self.valid_count / total_tools) * 100 if total_tools > 0 else 0,
"validation_success_rate": (self.valid_count / (self.valid_count + self.invalid_count)) * 100
if (self.valid_count + self.invalid_count) > 0 else 0,
"results": self.validation_results,
"issues": self._identify_issues()
}
def _identify_issues(self) -> list[dict[str, Any]]:
"""Identify common issues from validation results."""
issues = []
# Check for unsigned tools
unsigned_tools = [r for r in self.validation_results if r["status"] == "unsigned"]
if unsigned_tools:
issues.append({
"type": "unsigned_tools",
"severity": "warning",
"count": len(unsigned_tools),
"description": f"{len(unsigned_tools)} tools are not signed",
"tools": [t["tool_name"] for t in unsigned_tools]
})
# Check for invalid signatures
invalid_tools = [r for r in self.validation_results if r["status"] == "invalid"]
if invalid_tools:
issues.append({
"type": "invalid_signatures",
"severity": "error",
"count": len(invalid_tools),
"description": f"{len(invalid_tools)} tools have invalid signatures",
"tools": [t["tool_name"] for t in invalid_tools]
})
# Check for validation errors
error_tools = [r for r in self.validation_results if r["status"] == "error"]
if error_tools:
issues.append({
"type": "validation_errors",
"severity": "error",
"count": len(error_tools),
"description": f"{len(error_tools)} tools had validation errors",
"tools": [t["tool_name"] for t in error_tools]
})
return issues
def main():
"""Main entry point."""
parser = argparse.ArgumentParser(
description="Validate all signed schemas in the codebase",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
%(prog)s
%(prog)s --search-paths src tests
%(prog)s --output validation_report.json --verbose
"""
)
parser.add_argument(
"--search-paths",
nargs="+",
default=["src"],
help="Paths to search for signed tools (default: src)"
)
parser.add_argument(
"--output",
help="Output file for validation results (JSON format)"
)
parser.add_argument(
"--verbose",
action="store_true",
help="Enable verbose logging"
)
parser.add_argument(
"--show-issues-only",
action="store_true",
help="Only show tools with issues"
)
args = parser.parse_args()
if args.verbose:
logging.getLogger().setLevel(logging.DEBUG)
# Convert search paths to Path objects
search_path_objects = [Path(p) for p in args.search_paths]
try:
# Validate signatures
validator = SignatureValidator(search_path_objects)
results = validator.validate_all_signatures()
# Output results
logger.info("Signature validation completed:")
logger.info(f" Total tools: {results['total_tools']}")
logger.info(f" Valid signatures: {results['valid_signatures']}")
logger.info(f" Invalid signatures: {results['invalid_signatures']}")
logger.info(f" Unsigned tools: {results['unsigned_tools']}")
logger.info(f" Validation errors: {results['validation_errors']}")
logger.info(f" Signature coverage: {results['signature_coverage']:.1f}%")
logger.info(f" Validation success rate: {results['validation_success_rate']:.1f}%")
# Show issues
if results['issues']:
logger.warning("Issues found:")
for issue in results['issues']:
logger.warning(f" {issue['severity'].upper()}: {issue['description']}")
if args.verbose:
for tool in issue['tools']:
logger.warning(f" - {tool}")
# Filter results if requested
if args.show_issues_only:
results['results'] = [
r for r in results['results']
if r['status'] in ['invalid', 'unsigned', 'error']
]
# Save results to file if specified
if args.output:
with open(args.output, 'w', encoding='utf-8') as f:
json.dump(results, f, indent=2)
logger.info(f"Results saved to {args.output}")
# Exit with error code if any issues
if results['invalid_signatures'] > 0 or results['validation_errors'] > 0:
sys.exit(1)
except Exception:
logger.exception("Signature validation failed")
sys.exit(1)
if __name__ == "__main__":
main()