feat(clients): armor: Model Armor, SCC agent findings, GuardedModel, discovery-checked call table
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Checks src/google/armor/calls.h against Google's Discovery documents.
|
||||
|
||||
Fails (exit 1) when a declared call (rpc id, HTTP method, path, query
|
||||
parameter), a declared schema field, or a declared enum value is absent from
|
||||
the live Discovery document. Stdlib only, no credentials needed.
|
||||
|
||||
python3 -I src/google/armor/tools/check_discovery.py # fetch live
|
||||
python3 -I src/google/armor/tools/check_discovery.py --cache DIR # read DIR/<service>-<version>.json
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import urllib.request
|
||||
|
||||
HEADER = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "calls.h")
|
||||
ROW = re.compile(r'^\s*\{((?:"[^"]*"(?:,\s*)?)+)\},\s*$')
|
||||
STR = re.compile(r'"([^"]*)"')
|
||||
|
||||
|
||||
def rows():
|
||||
calls, schemas, enums = [], [], []
|
||||
for line in open(HEADER, encoding="utf-8"):
|
||||
m = ROW.match(line)
|
||||
if not m:
|
||||
continue
|
||||
cells = STR.findall(m.group(1))
|
||||
{6: calls, 4: schemas, 5: enums}.get(len(cells), []).append(cells)
|
||||
return calls, schemas, enums
|
||||
|
||||
|
||||
def load(service, version, cache):
|
||||
if cache:
|
||||
path = os.path.join(cache, "%s-%s.json" % (service, version))
|
||||
with open(path, encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
url = "https://%s.googleapis.com/$discovery/rest?version=%s" % (service, version)
|
||||
with urllib.request.urlopen(url, timeout=60) as r:
|
||||
return json.load(r)
|
||||
|
||||
|
||||
def methods(doc):
|
||||
out = {}
|
||||
|
||||
def walk(res):
|
||||
for sub in res.get("resources", {}).values():
|
||||
walk(sub)
|
||||
for m in res.get("methods", {}).values():
|
||||
out[m["id"]] = m
|
||||
|
||||
walk(doc)
|
||||
return out
|
||||
|
||||
|
||||
def main():
|
||||
cache = None
|
||||
if "--cache" in sys.argv:
|
||||
cache = sys.argv[sys.argv.index("--cache") + 1]
|
||||
calls, schemas, enums = rows()
|
||||
if not calls or not schemas or not enums:
|
||||
print("FAIL: no rows parsed from calls.h")
|
||||
return 1
|
||||
docs, errors = {}, []
|
||||
|
||||
def doc(service, version):
|
||||
key = (service, version)
|
||||
if key not in docs:
|
||||
docs[key] = load(service, version, cache)
|
||||
return docs[key]
|
||||
|
||||
index = {}
|
||||
for service, version, rpc, http, path, query in calls:
|
||||
d = doc(service, version)
|
||||
idx = index.setdefault((service, version), methods(d))
|
||||
m = idx.get(rpc)
|
||||
if not m:
|
||||
errors.append("call missing: " + rpc)
|
||||
continue
|
||||
if m["httpMethod"] != http:
|
||||
errors.append("%s: http %s != declared %s" % (rpc, m["httpMethod"], http))
|
||||
if m["path"] != path:
|
||||
errors.append("%s: path %s != declared %s" % (rpc, m["path"], path))
|
||||
for q in filter(None, query.split(",")):
|
||||
if q not in m.get("parameters", {}):
|
||||
errors.append("%s: query parameter %s missing" % (rpc, q))
|
||||
for service, version, schema, fields in schemas:
|
||||
s = doc(service, version)["schemas"].get(schema)
|
||||
if not s:
|
||||
errors.append("schema missing: " + schema)
|
||||
continue
|
||||
for f in fields.split(","):
|
||||
if f not in s.get("properties", {}):
|
||||
errors.append("%s: field %s missing" % (schema, f))
|
||||
for service, version, schema, field, values in enums:
|
||||
s = doc(service, version)["schemas"].get(schema, {})
|
||||
p = s.get("properties", {}).get(field)
|
||||
if not p or "enum" not in p:
|
||||
errors.append("%s.%s: no enum" % (schema, field))
|
||||
continue
|
||||
for v in values.split(","):
|
||||
if v not in p["enum"]:
|
||||
errors.append("%s.%s: value %s missing" % (schema, field, v))
|
||||
for e in errors:
|
||||
print("FAIL:", e)
|
||||
print("%s: %d calls, %d schemas, %d enums checked, %d problems"
|
||||
% ("FAIL" if errors else "OK", len(calls), len(schemas), len(enums), len(errors)))
|
||||
return 1 if errors else 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user