Files
floci/tools/aws/regen_query_services.py
T
Priamo Germosen 30d8d3ff5c fix(core): route a Query action by its service's botocore model (#5879)
A Query request without a usable credential scope is routed by its
Action and Version, and IAM enforcement authorizes it under the service
that routing picks. The names each service routed were kept by hand in
AwsQueryController and had drifted from what the services serve: 170
names Floci dispatches, across eight services, reached no service and
fell through to SQS. They answered UnsupportedOperation in SQS's
namespace and were authorized as SQS actions.

Each Query service's API versions and operations now come from its
botocore models: tools/aws/regen_query_services.py writes them, from the
pinned botocore, to aws/query-services.json, and make aws-data-check
fails when the file is stale. Every name a service defines reaches it,
implemented here or not. Floci keeps the list of services and their
order, Cognito's names, which no Query model holds, and ElastiCache's
ValidateIamAuthToken, which no model defines.

A name several services define goes to the first of them when the
request names no Version. Ten such names get a pinned owner instead,
the service that serves them here, where the order would pick an
earlier one that does not. The routing parity test checks every
dispatched action with and without a Version and no longer needs its
list of known gaps.
2026-10-17 10:17:18 -05:00

127 lines
5.3 KiB
Python

#!/usr/bin/env python3
"""Regenerate src/main/resources/aws/query-services.json from botocore's models.
A Query request that arrives without a usable credential scope is routed by its `Action` and the
`Version` it declares (`AwsQueryController.inferServiceFromAction`), and IAM enforcement authorizes
it under the service that routing picks. Routing therefore needs, for every Query service Floci
serves, the API versions its clients declare and every operation its model defines. This script
keeps exactly that, read from the service models, so a name a service defines reaches it whether or
not Floci implements it yet: an unimplemented one answers from the right service instead of falling
through to SQS.
Which services there are, and which botocore models each one serves, is Floci's choice and stays in
SERVICES below: the protocol field cannot pick them (SQS's model is JSON only now, CloudWatch lists
three protocols), RDS's scope also serves DocumentDB and Neptune, and Elastic Load Balancing serves
both of its APIs. The order the services are tried in is `AwsQueryController`'s.
The models are botocore's `<model>/<apiVersion>/service-2.json` (Apache-2.0,
https://github.com/boto/botocore), the latest API version of each. They are located, in order, at
`--botocore-data`, the `local/aws/botocore` checkout, then the installed `botocore` package, which
`requirements.txt` pins so CI regenerates the same bytes.
Run from anywhere in the repo:
python3 tools/aws/regen_query_services.py # rewrite the vendored file in place
python3 tools/aws/regen_query_services.py --check # exit 1 when the vendored file is stale
"""
from __future__ import annotations
import argparse
import gzip
import json
import sys
from pathlib import Path
from regen_partitions import resolve_botocore_data, strip_source
REPO_ROOT = Path(__file__).resolve().parents[2]
OUTPUT = REPO_ROOT / "src/main/resources/aws/query-services.json"
# Floci's Query service name, as the credential scope names it, and the botocore models it serves.
SERVICES: dict[str, tuple[str, ...]] = {
"autoscaling": ("autoscaling",),
"cloudformation": ("cloudformation",),
"ec2": ("ec2",),
"elasticache": ("elasticache",),
"elasticbeanstalk": ("elasticbeanstalk",),
"elasticloadbalancing": ("elbv2", "elb"),
"email": ("ses",),
"iam": ("iam",),
"monitoring": ("cloudwatch",),
"rds": ("rds", "docdb", "neptune"),
"redshift": ("redshift",),
"sns": ("sns",),
"sqs": ("sqs",),
"sts": ("sts",),
}
def latest_model_dir(data: Path, model: str) -> Path:
"""The model's directory for its latest API version, which is what current SDKs send."""
versions = sorted(path for path in (data / model).iterdir() if path.is_dir())
if not versions:
raise SystemExit(f"error: no API version of {model} under {data}")
return versions[-1]
def load_model(directory: Path) -> dict:
plain = directory / "service-2.json"
if plain.exists():
return json.loads(plain.read_text(encoding="utf-8"))
compressed = plain.with_name(plain.name + ".gz")
if compressed.exists():
with gzip.open(compressed, "rt", encoding="utf-8") as f:
return json.load(f)
raise SystemExit(f"error: {plain} not found")
def build(models: dict[str, list[dict]], provenance: str) -> dict:
"""The vendored document from each service's loaded models."""
services = {}
for name in sorted(models):
versions = sorted({model["metadata"]["apiVersion"] for model in models[name]})
operations = sorted({operation for model in models[name] for operation in model["operations"]})
services[name] = {"apiVersions": versions, "operations": operations}
return {
"_source": {
"generator": "tools/aws/regen_query_services.py",
"botocore": provenance,
},
"services": services,
}
def load_models(data: Path) -> dict[str, list[dict]]:
return {name: [load_model(latest_model_dir(data, model)) for model in models]
for name, models in SERVICES.items()}
def render(doc: dict) -> str:
return json.dumps(doc, indent=2, sort_keys=False) + "\n"
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--check", action="store_true", help="exit 1 when the vendored file differs from a fresh generation")
parser.add_argument("--botocore-data", type=Path, help="botocore data directory")
parser.add_argument("--output", type=Path, default=OUTPUT, help=argparse.SUPPRESS)
args = parser.parse_args(argv)
data, provenance = resolve_botocore_data(args.botocore_data)
fresh = render(build(load_models(data), provenance))
if args.check:
current = args.output.read_text(encoding="utf-8") if args.output.exists() else ""
if strip_source(current) != strip_source(fresh):
print(f"{args.output} is stale (generated from {provenance}).", file=sys.stderr)
return 1
return 0
args.output.write_text(fresh, encoding="utf-8")
shown = args.output.relative_to(REPO_ROOT) if args.output.is_relative_to(REPO_ROOT) else args.output
print(f"wrote {shown} from {provenance}")
return 0
if __name__ == "__main__":
sys.exit(main())