Files
floci/tools/aws/regen_redshift_serverless_shapes.py
T
Vũ Thế Huy 77384dc3de feat(redshift-serverless): validate requests against the AWS model constraints (#5641)
* feat(redshift-serverless): validate requests against the AWS model constraints

Generate the input constraints (required members, length, pattern, enum,
number range, list size) from botocore's Redshift Serverless model with
tools/aws/regen_redshift_serverless_shapes.py and vendor them under
src/main/resources/aws, like the DynamoDB request shapes. A new
RedshiftServerlessRequestShapes check runs before dispatch and answers a
ValidationException. Pagination members are left to the services, which keep
reporting InvalidPaginationException.

Move the request parsing and error helpers that the JSON handler, the usage
limit service and the service each carried a copy of into ServerlessSupport,
so the resource families that follow share one implementation.

* fix(redshift-serverless): address review findings on request validation

- Match a model pattern against the whole value, so an unanchored pattern such
  as the account id no longer accepts a value that merely contains a match.
- Count string lengths in Unicode code points, so a tag of 200 supplementary
  characters is not rejected for exceeding 256 UTF-16 units.
- Validate only the actions the handler implements, so a modeled but
  unsupported action keeps answering UnknownOperationException.
- Read every string member through ServerlessSupport.text and reject a tag key
  or value that is not a string, so a numeric nextToken or tag member is a
  ValidationException instead of being treated as absent or coerced.
- Regenerate the vendored shapes from botocore 1.43.113, the version
  tools/aws/requirements.txt pins.
- Cover the rejected-request case with a state check, and add the pattern, code
  point, page token and tag regressions.

* refactor(redshift-serverless): use VendoredResources, ServerlessSupport.notFound and update docs
2026-10-15 09:06:46 -04:00

121 lines
4.6 KiB
Python

#!/usr/bin/env python3
"""Regenerate src/main/resources/aws/redshift-serverless-request-shapes.json from botocore's model.
Redshift Serverless answers a request that breaks the model's constraints with a
ValidationException before it runs the operation. The Java side
(`RedshiftServerlessRequestShapes`) needs the same constraints, so this script keeps what that
check reads: the input shape of every operation, and for every shape reachable from one its type,
the shapes of its members, list elements or map values, its required members, its length or value
bounds (`min`/`max`), its `pattern` and its `enum`.
The model is botocore's `redshift-serverless/2021-04-21/service-2.json` (Apache-2.0,
https://github.com/boto/botocore). It is 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_redshift_serverless_shapes.py # rewrite the vendored file in place
python3 tools/aws/regen_redshift_serverless_shapes.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/redshift-serverless-request-shapes.json"
MODEL = Path("redshift-serverless/2021-04-21/service-2.json")
# Constraint keys copied verbatim from a shape; `min` and `max` bound the length of a string or
# list and the value of a number.
CONSTRAINTS = ("min", "max", "pattern", "enum")
def load_model(data: Path) -> dict:
plain = data / MODEL
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(model: dict, provenance: str) -> dict:
shapes = model["shapes"]
kept: dict[str, dict] = {}
def keep(name: str) -> None:
if name in kept:
return
shape = shapes[name]
kind = shape["type"]
entry: dict = {"type": kind}
kept[name] = entry
for key in CONSTRAINTS:
if key in shape:
entry[key] = shape[key]
if kind == "structure":
if shape.get("required"):
entry["required"] = list(shape["required"])
entry["members"] = {member: spec["shape"] for member, spec in shape.get("members", {}).items()}
for spec in shape.get("members", {}).values():
keep(spec["shape"])
elif kind == "list":
entry["member"] = shape["member"]["shape"]
keep(shape["member"]["shape"])
elif kind == "map":
entry["value"] = shape["value"]["shape"]
keep(shape["value"]["shape"])
operations = {}
for name, operation in sorted(model["operations"].items()):
if "input" in operation:
operations[name] = operation["input"]["shape"]
keep(operation["input"]["shape"])
return {
"_source": {
"generator": "tools/aws/regen_redshift_serverless_shapes.py",
"botocore": provenance,
},
"operations": operations,
"shapes": dict(sorted(kept.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_model(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")
print(f"wrote {args.output.relative_to(REPO_ROOT)} from {provenance}")
return 0
if __name__ == "__main__":
sys.exit(main())