Files
moloapp/src/google/workspace/tools/check_discovery.py
T

144 lines
5.9 KiB
Python

#!/usr/bin/env python3
"""Check src/google/workspace/call_table.cpp against Google's Discovery documents.
Fails (exit 1) when a row is absent from Discovery, a Discovery method has no
row, or method, path, parameter (name, location, required, type, repeated),
request schema, schema field, items key, media flag, base URL or scope list
differ. Kind sanity: every GET is a read, every DELETE is destructive, a
read that is not a GET is on the explicit allowlist. Python 3 stdlib only.
python3 -I src/google/workspace/tools/check_discovery.py
"""
import json
import os
import re
import sys
import urllib.request
HERE = os.path.dirname(os.path.abspath(__file__))
TABLE = os.path.join(HERE, "..", "call_table.cpp")
STR = re.compile(r'"((?:[^"\\]|\\.)*)"')
POST_READS = {"calendar.freebusy.query", "sheets.spreadsheets.getByDataFilter",
"sheets.spreadsheets.values.batchGetByDataFilter"}
def block(text, begin, end):
a, b = text.index(begin), text.index(end)
rows = []
for line in text[a:b].splitlines()[1:]:
s = line.strip()
if s.startswith("{") and s.endswith("},"):
rows.append([x.replace('\\"', '"').replace("\\\\", "\\") for x in STR.findall(s)])
return rows
def fetch(url):
with urllib.request.urlopen(url, timeout=60) as r:
return json.load(r)
def methods(node, out):
for m in node.get("methods", {}).values():
out[m["id"]] = m
for child in node.get("resources", {}).values():
methods(child, out)
def decode(spec):
out = {}
for item in filter(None, spec.split(",")):
name = re.split(r"[@!#?*]", item, maxsplit=1)[0]
out[name] = item[len(name):]
return out
def main():
text = open(TABLE).read()
apis = {r[0]: r for r in block(text, "// APIS-BEGIN", "// APIS-END")}
calls = block(text, "// CALLS-BEGIN", "// CALLS-END")
schemas = {(r[0], r[1]): r[2] for r in block(text, "// SCHEMAS-BEGIN", "// SCHEMAS-END")}
bad = []
rows_by_api = {}
for r in calls:
rows_by_api.setdefault(r[1], {})[r[0]] = r
total = 0
for name, (_, did, url, base, service, scopes, readonly, revision) in apis.items():
doc = fetch(url)
index = {}
methods(doc, index)
if (doc["rootUrl"] + doc["servicePath"]).rstrip("/") != base:
bad.append("%s: base url differs" % name)
want_scopes = set(doc.get("auth", {}).get("oauth2", {}).get("scopes", {}))
if set(scopes.split()) != want_scopes:
bad.append("%s: scope list differs (%s)" % (name, sorted(want_scopes ^ set(scopes.split()))))
if not set(readonly.split()) <= want_scopes:
bad.append("%s: readonly scope not in Discovery" % name)
rows = rows_by_api.get(name, {})
for rid in index:
if rid not in rows:
bad.append("%s: Discovery method %s has no row" % (name, rid))
for rid, (_, api, http, path, params, body, items, kind, flags) in rows.items():
total += 1
m = index.get(rid)
if not m:
bad.append("missing rpc " + rid)
continue
if m["httpMethod"] != http:
bad.append("%s: http %s != %s" % (rid, http, m["httpMethod"]))
if m["path"] != path:
bad.append("%s: path %s != %s" % (rid, path, m["path"]))
have = m.get("parameters", {})
mine = decode(params)
if set(have) != set(mine):
bad.append("%s: parameter names differ %s" % (rid, sorted(set(have) ^ set(mine))))
for pn, mark in mine.items():
p = have.get(pn)
if not p:
continue
if ("@" in mark) != (p.get("location") == "path"):
bad.append("%s: %s location" % (rid, pn))
if p.get("location") != "path" and ("!" in mark) != bool(p.get("required")):
bad.append("%s: %s required" % (rid, pn))
if ("#" in mark) != (p.get("type") in ("integer", "number")):
bad.append("%s: %s number" % (rid, pn))
if ("?" in mark) != (p.get("type") == "boolean"):
bad.append("%s: %s boolean" % (rid, pn))
if ("*" in mark) != bool(p.get("repeated")):
bad.append("%s: %s repeated" % (rid, pn))
ref = m.get("request", {}).get("$ref", "")
if ref != body:
bad.append("%s: request schema %r != %r" % (rid, body, ref))
if body:
props = ",".join(sorted(doc["schemas"][body].get("properties", {})))
if schemas.get((name, body)) != props:
bad.append("%s: schema %s fields differ" % (rid, body))
if items:
rprops = doc["schemas"].get(m.get("response", {}).get("$ref"), {}).get("properties", {})
if rprops.get(items, {}).get("type") != "array":
bad.append("%s: items key %s is not an array" % (rid, items))
f = ""
if m.get("supportsMediaUpload"):
f += "u"
if m.get("supportsMediaDownload"):
f += "d"
if "response" not in m:
f += "b"
if f != flags:
bad.append("%s: flags %r != %r" % (rid, flags, f))
if http == "GET" and kind != "r":
bad.append("%s: GET must be a read" % rid)
if http == "DELETE" and kind != "d":
bad.append("%s: DELETE must be destructive" % rid)
if kind == "r" and http != "GET" and rid not in POST_READS:
bad.append("%s: non-GET read not on the allowlist" % rid)
if bad:
print("\n".join(bad))
print("discovery MISMATCH: %d findings" % len(bad))
return 1
print("discovery ok: %d apis, %d calls, %d request schemas" % (len(apis), total, len(schemas)))
return 0
if __name__ == "__main__":
sys.exit(main())