Skip to content

Commit

Permalink
Cleanup boto patching and include min/max (#3939)
Browse files Browse the repository at this point in the history
* Cleanup boto patching and include min/max
  • Loading branch information
kddejong authored Jan 31, 2025
1 parent 90af150 commit 02c6d24
Show file tree
Hide file tree
Showing 1,462 changed files with 18,680 additions and 7,629 deletions.
40 changes: 15 additions & 25 deletions scripts/boto/_automated_patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
"""

import json
import logging
from pathlib import Path
from typing import Any

from _helpers import load_schema_file
from _types import AllPatches, Patch, ResourcePatches

from cfnlint.schema.resolver import RefResolutionError, RefResolver

LOGGER = logging.getLogger("cfnlint")

skip = [
"account",
"chime",
Expand All @@ -33,8 +37,6 @@

skip_property_names = ["State"]

_fields = ["pattern", "enum"]

_visited_paths = []


Expand Down Expand Up @@ -125,9 +127,6 @@ def _nested_arrays(
resolver, schema_data, boto_data, array_shap_data, path, source
)
else:
# skip if we already have an enum or pattern
if any([schema_data.get(field) for field in _fields]):
return {}
return {
path: Patch(
source=source,
Expand Down Expand Up @@ -167,10 +166,6 @@ def _nested_objects(
except RefResolutionError:
return results

# skip if we already have an enum or pattern
if any([p_data.get(field) for field in _fields]):
continue

member_shape_name = member_data.get("shape")
member_shape = boto_data.get("shapes", {}).get(member_shape_name, {})

Expand All @@ -189,9 +184,6 @@ def _nested_objects(
)
)

if not any([member_shape.get(field) for field in _fields]):
continue

results[path] = Patch(
source=source,
shape=member_shape_name,
Expand All @@ -201,13 +193,13 @@ def _nested_objects(


def _per_resource_patch(
schema_data: dict[str, Any], boto_data: dict[str, Any], source: list[str]
resolver: RefResolver, boto_data: dict[str, Any], source: list[str]
) -> ResourcePatches:
results: ResourcePatches = {}
_, schema_data = resolver.resolve("/")
create_operations = get_schema_create_operations(schema_data)
shapes = {}

resolver = RefResolver.from_schema(schema_data)
for create_operation in create_operations:
shapes.update(get_shapes(boto_data, create_operation))
create_shape = (
Expand Down Expand Up @@ -252,21 +244,19 @@ def get_resource_patches(

resources = list(schema_path.glob(f"aws-{service_name}-*.json"))
if not resources:
print(f"No resource files found for {service_name}")
LOGGER.warning(f"No resource files found for {service_name}")

for resource in resources:
with open(resource, "r") as f:
schema_data = json.load(f)
ref_resolver = load_schema_file(resource)
_, schema_data = ref_resolver.resolve("/")

resource_type = schema_data.get("typeName", "")
if resource_type not in results:
results[resource_type] = {}
resource_type = schema_data.get("typeName", "")
if resource_type not in results:
results[resource_type] = {}

results[resource_type].update(
_per_resource_patch(
schema_data, boto_data, [service_dir.name, last_date]
)
)
results[resource_type].update(
_per_resource_patch(ref_resolver, boto_data, [service_dir.name, last_date])
)

return results

Expand Down
17 changes: 17 additions & 0 deletions scripts/boto/_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""

from __future__ import annotations

import json
from pathlib import Path

from cfnlint.schema.resolver import RefResolver


def load_schema_file(schema_path: Path) -> RefResolver:
with open(schema_path, "r") as f:
data = json.load(f)
return RefResolver.from_schema(data)
58 changes: 50 additions & 8 deletions scripts/boto/update_schemas_from_boto.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import regex as re
import requests
from _automated_patches import build_automated_patches
from _helpers import load_schema_file
from _manual_patches import patches
from _types import AllPatches, ResourcePatches

Expand All @@ -39,7 +40,7 @@ def configure_logging():
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)

LOGGER.setLevel(logging.INFO)
LOGGER.setLevel(logging.WARNING)
log_formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
Expand All @@ -52,10 +53,16 @@ def configure_logging():


def build_resource_type_patches(
dir: str, resource_name: str, resource_patches: ResourcePatches
boto_path: Path,
schema_path: Path,
resource_name: str,
resource_patches: ResourcePatches,
):
LOGGER.info(f"Applying patches for {resource_name}")

resolver = load_schema_file(
schema_path / f"{resource_name.lower().replace('::', '-')}.json"
)
resource_name = resource_name.lower().replace("::", "_")
output_path = Path("src/cfnlint/data/schemas/patches/extensions/all/")
resource_path = output_path / resource_name
Expand All @@ -71,13 +78,38 @@ def build_resource_type_patches(
service_path = (
["botocore-master/botocore/data"] + patch.source + ["service-2.json"]
)
with open(os.path.join(dir, *service_path), "r") as f:
_, schema_data = resolver.resolve(f"#{path}")
with open(os.path.join(boto_path, *service_path), "r") as f:
boto_d = json.load(f)

for field in ["enum", "pattern"]:
shape_type = boto_d.get("shapes", {}).get(patch.shape, {}).get("type")
for field in ["enum", "pattern", "max", "min"]:
value = boto_d.get("shapes", {}).get(patch.shape, {}).get(field)
if not value:
continue
if field in ["enum", "pattern"]:
if any(f in schema_data for f in ["enum", "pattern"]):
continue
elif field == "max":
if any(
f in schema_data for f in ["maxLength", "maxItems", "maximum"]
):
continue
if "pattern" in schema_data:
if re.match(
r"^.*\{[0-9]+,[0-9]+\}\$?$", schema_data["pattern"]
):
continue

elif field == "min":
if any(
f in schema_data for f in ["minLength", "minItems", "minimum"]
):
continue
if "pattern" in schema_data:
if re.match(
r"^.*\{[0-9]+,[0-9]+\}\$?$", schema_data["pattern"]
):
continue
if field == "pattern":
if value in [".*", "^.*$"]:
continue
Expand All @@ -92,6 +124,15 @@ def build_resource_type_patches(
)
)
continue
if field in ["max", "min"]:
if shape_type == "string":
field = "maxLength" if field == "max" else "minLength"
elif shape_type == "list":
field = "maxItems" if field == "max" else "minItems"
elif shape_type in ["integer", "number"]:
field = "maximum" if field == "max" else "minimum"
else:
continue
if value:
if patch.source[0] in exceptions:
if path in exceptions[patch.source[0]]:
Expand Down Expand Up @@ -123,12 +164,13 @@ def build_resource_type_patches(


def build_patches(
dir: str,
boto_path: Path,
schema_path: Path,
patches: AllPatches,
):
for resource_name, patch in patches.items():
build_resource_type_patches(
dir, resource_name=resource_name, resource_patches=patch
boto_path, schema_path, resource_name=resource_name, resource_patches=patch
)


Expand Down Expand Up @@ -159,7 +201,7 @@ def main():
else:
_patches[k][path] = patch

build_patches(boto_path, _patches)
build_patches(boto_path, schema_path, _patches)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"b7df0a841e1d85eb2ca55fae9b63a199\"", "url": "https://schema.cloudformation.eu-south-1.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"3b5d6ce396c9a98b72b7fc1127bc1abc\"", "url": "https://schema.cloudformation.eu-south-1.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"75e66d980dea62bf67981ffe569bbb47\"", "url": "https://schema.cloudformation.us-gov-east-1.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"40c9314343c1df29f23241aca99ff5c0\"", "url": "https://schema.cloudformation.us-gov-east-1.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"b4ea48ef051d441d6fe68349a08772e5\"", "url": "https://schema.cloudformation.us-gov-west-1.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"4d01654834ffd168b5611579eb8746bc\"", "url": "https://schema.cloudformation.us-gov-west-1.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"6d426c7baa8bf4703793694e05b7699a\"", "url": "https://schema.cloudformation.me-central-1.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"c0cee52c664bec22aa9cad20452c8be4\"", "url": "https://schema.cloudformation.me-central-1.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"4eff125b6a8d9a53bfd01cd720058a77\"", "url": "https://schema.cloudformation.eu-west-2.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"704f1b32b8ed2357b4efeb93a9633e36\"", "url": "https://schema.cloudformation.eu-west-2.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"b44fc7ac1956ad50020dcc9bce2f7e65\"", "url": "https://schema.cloudformation.af-south-1.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"5d90eede497d94dc5acbe3947c384f0f\"", "url": "https://schema.cloudformation.af-south-1.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"cccc338e071c166a131d1cdc9d74fab7\"", "url": "https://schema.cloudformation.us-west-1.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"11cd6603e5e474388338daeea4ec18c5\"", "url": "https://schema.cloudformation.us-west-1.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"2a5f184af1076cf4909dd7dfd66c1091\"", "url": "https://schema.cloudformation.eu-central-1.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"f1245202dc7d03e32366cf183a60f5bf\"", "url": "https://schema.cloudformation.eu-central-1.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"81e2ca053c22e5c8e6f33990c3c1683a\"", "url": "https://schema.cloudformation.ap-south-1.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"be46d4992c4b8b9d9363ed0f29d67bfc\"", "url": "https://schema.cloudformation.ap-south-1.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"5b4a791385a5b8095875163b14a84e12\"", "url": "https://schema.cloudformation.ap-southeast-4.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"8b09581590dc797e7f8b9966df079bcd\"", "url": "https://schema.cloudformation.ap-southeast-4.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"735d724f69e6c702c966f1bd667266dc\"", "url": "https://schema.cloudformation.us-east-2.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"416452f27037f3b97f425284a73bcf9c\"", "url": "https://schema.cloudformation.us-east-2.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"d05bdba3e6c8b67d31218a116456c75b\"", "url": "https://schema.cloudformation.ap-southeast-1.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"b5126a7d839bd20951fcafe4da854d38\"", "url": "https://schema.cloudformation.ap-southeast-1.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"26a4a4a9660b28ecb31a40aa4db8b791\"", "url": "https://schema.cloudformation.ap-southeast-3.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"6edf94664df234608592965308b6f6b7\"", "url": "https://schema.cloudformation.ap-southeast-3.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"d37369e7d40a074400df68c04fb64d28\"", "url": "https://schema.cloudformation.ap-east-1.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"174ddf0e8bbf351c26704e4e2337b16a\"", "url": "https://schema.cloudformation.ap-east-1.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"1faad9cae8146a0781efc270a2cf2ed3\"", "url": "https://schema.cloudformation.sa-east-1.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"3e62904c961fae6ced725b12783ef45d\"", "url": "https://schema.cloudformation.sa-east-1.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"5da9230365aca3d094395f17b18cb452\"", "url": "https://schema.cloudformation.ap-southeast-2.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"83baf7b0a3b0ee8c5f5ba852405d693e\"", "url": "https://schema.cloudformation.ap-southeast-2.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"2b0a5723c1a5e739eea87299385382f9\"", "url": "https://schema.cloudformation.eu-north-1.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"e292cb731d4b0b927a8d86a6301127ae\"", "url": "https://schema.cloudformation.eu-north-1.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"260acbfe6a05bd388bccf119e9747069\"", "url": "https://schema.cloudformation.eu-south-2.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"c75fbe544296584ef4d56b592fb87ed3\"", "url": "https://schema.cloudformation.eu-south-2.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"10d837e83b0dee3b5bdd8025d2e030f2\"", "url": "https://schema.cloudformation.ca-central-1.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"485c9eb6f7e1efdb2af30a9800b05fab\"", "url": "https://schema.cloudformation.ca-central-1.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"316784e70cf0510fea4b81b31ecd8f15\"", "url": "https://schema.cloudformation.eu-west-3.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"8fc65b5326dd82fb678168c25661e666\"", "url": "https://schema.cloudformation.eu-west-3.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"b4ec2276c5d96d861a33de8d7ef645f4\"", "url": "https://schema.cloudformation.ap-northeast-3.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"d89138fadb813a9999c4f07c383b48e7\"", "url": "https://schema.cloudformation.ap-northeast-3.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"47806c7293decd2a8dc0b6e7ec30b75f\"", "url": "https://schema.cloudformation.us-west-2.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"dd30b5012826298b344563720602a94b\"", "url": "https://schema.cloudformation.us-west-2.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"aec4bdf0cc84ce7f3a8486d83c3b41de\"", "url": "https://schema.cloudformation.ap-south-2.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"707ec23d98096f232e1f68967ce937bd\"", "url": "https://schema.cloudformation.ap-south-2.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"6e1fd9ae967709a2a2069c40a2db3aa2\"", "url": "https://schema.cloudformation.us-east-1.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"91387a48563a194dd6a2d7b943bd8005\"", "url": "https://schema.cloudformation.us-east-1.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"854a88a3967badd9ca15fab91e861ad3\"", "url": "https://schema.cloudformation.il-central-1.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"0ff66b9ec501bfa3f5235ad1b46b6901\"", "url": "https://schema.cloudformation.il-central-1.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"etag": "\"cee4b474888719e8896f94090ac6e16d\"", "url": "https://schema.cloudformation.ap-northeast-1.amazonaws.com/CloudformationSchema.zip"}
{"etag": "\"76471ec41ead11d47f95ac6393542b12\"", "url": "https://schema.cloudformation.ap-northeast-1.amazonaws.com/CloudformationSchema.zip"}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,15 @@
"op": "add",
"path": "/definitions/ArchiveRule/properties/RuleName/pattern",
"value": "[A-Za-z][A-Za-z0-9_.-]*"
},
{
"op": "add",
"path": "/definitions/ArchiveRule/properties/RuleName/maxLength",
"value": 255
},
{
"op": "add",
"path": "/definitions/ArchiveRule/properties/RuleName/minLength",
"value": 1
}
]
Loading

0 comments on commit 02c6d24

Please sign in to comment.