Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(stepfunctions): do not expand acronyms in EC2 aws-sdk parameter names (#1272)

Maximilian Bandleb93fb6bb991b
diff --git a/ministack/services/stepfunctions.py b/ministack/services/stepfunctions.py
index 65193ea3..9572d0d0 100644
--- a/ministack/services/stepfunctions.py
+++ b/ministack/services/stepfunctions.py
@@ -4642,7 +4642,6 @@ _QUERY_PARAM_NAME_OVERRIDES = {
},
("ec2", "CreateSecurityGroup"): {
"Description": "GroupDescription",
- "VpcId": "VpcId",
},
}
@@ -4669,19 +4668,24 @@ def _sfn_key_to_api_name(name):
return "".join(t.upper() if t in _AWS_ACRONYMS else t for t in tokens)
-def _convert_params_to_api_names(data, name_overrides=None):
- """Recursively convert SFN SDK-style param names to AWS wire-format names."""
+def _convert_params_to_api_names(data, name_overrides=None, expand_acronyms=True):
+ """Recursively convert SFN SDK-style param names to AWS wire-format names.
+
+ ``expand_acronyms=False`` for services like EC2 whose wire names carry no uppercase acronym.
+ """
if isinstance(data, dict):
converted = {}
for key, value in data.items():
if name_overrides and key in name_overrides:
wire_key = name_overrides[key]
- else:
+ elif expand_acronyms:
wire_key = _sfn_key_to_api_name(key)
- converted[wire_key] = _convert_params_to_api_names(value, name_overrides)
+ else:
+ wire_key = key
+ converted[wire_key] = _convert_params_to_api_names(value, name_overrides, expand_acronyms)
return converted
if isinstance(data, list):
- return [_convert_params_to_api_names(item, name_overrides) for item in data]
+ return [_convert_params_to_api_names(item, name_overrides, expand_acronyms) for item in data]
return data
@@ -4921,7 +4925,8 @@ def _dispatch_aws_sdk_query(service_info, service_name, action, input_data):
# Convert SFN SDK-style param names (DbSubnetGroupName) to wire-format
# names (DBSubnetGroupName) before flattening to query params.
name_overrides = _QUERY_PARAM_NAME_OVERRIDES.get((service_key, pascal_action))
- wire_data = _convert_params_to_api_names(input_data, name_overrides)
+ # EC2 is the exception: its serialiser only capitalises the first letter
+ wire_data = _convert_params_to_api_names(input_data, name_overrides, expand_acronyms=service_key != "ec2")
form_params = {"Action": pascal_action}
if service_key == "ec2":
form_params.update(_flatten_ec2_query_params(wire_data, action=pascal_action))
diff --git a/tests/test_stepfunctions.py b/tests/test_stepfunctions.py
index 963e9bc8..c9b28d36 100644
--- a/tests/test_stepfunctions.py
+++ b/tests/test_stepfunctions.py
@@ -1991,6 +1991,102 @@ def test_sfn_aws_sdk_ec2_instances_return_sdk_shape(sfn_sync, ec2):
ec2.terminate_instances(InstanceIds=[instance_id])
+def test_sfn_aws_sdk_ec2_params_keep_their_sdk_names(sfn_sync, ec2):
+ """EC2 input params are passed through, never acronym-expanded."""
+ suffix = _uuid_mod.uuid4().hex[:8]
+ vpc_id = ec2.create_vpc(CidrBlock="10.43.0.0/16")["Vpc"]["VpcId"]
+ group_id = None
+ try:
+ # Effects are read back from EC2, never from the task output: the task reported SUCCEEDED
+ # while changing nothing. Each attribute is set to the opposite of its default, so an
+ # argument that never arrived cannot pass by accident.
+ for param, attribute, value in (("EnableDnsHostnames", "enableDnsHostnames", True),
+ ("EnableDnsSupport", "enableDnsSupport", False)):
+ _run_sdk_task(sfn_sync, f"sdk-{attribute.lower()}-{suffix}", "ec2:modifyVpcAttribute",
+ {"VpcId": vpc_id, param: {"Value": value}})
+ assert ec2.describe_vpc_attribute(
+ VpcId=vpc_id, Attribute=attribute)[param]["Value"] is value
+
+ # Skipping expansion must not skip override table (Description -> GroupDescription)
+ created = _run_sdk_task(sfn_sync, f"sdk-sg-{suffix}", "ec2:createSecurityGroup",
+ {"GroupName": f"sdk-sg-{suffix}", "Description": "param name check",
+ "VpcId": vpc_id})
+ group_id = created["GroupId"]
+ group = ec2.describe_security_groups(GroupIds=[group_id])["SecurityGroups"][0]
+ assert group["VpcId"] == vpc_id
+ assert group["Description"] == "param name check"
+
+ # A list param, whose wire name is the singular locationName (VpcIds -> VpcId.N). That
+ # rename comes from the list-naming path, so it has to survive the expansion being off.
+ described = _run_sdk_task(sfn_sync, f"sdk-vpcs-{suffix}", "ec2:describeVpcs",
+ {"VpcIds": [vpc_id]})
+ assert [v["VpcId"] for v in described["Vpcs"]] == [vpc_id]
+ finally:
+ if group_id:
+ ec2.delete_security_group(GroupId=group_id)
+ ec2.delete_vpc(VpcId=vpc_id)
+
+
+def test_sfn_aws_sdk_ec2_param_names_never_need_the_acronym_expansion():
+ """Every param name EC2 accepts, from botocore rather than the handful exercised above.
+
+ For each member the expansion would rename, the wire name is either the name already (so the
+ expansion breaks it) or a third spelling the list-naming path handles (VpcIds -> VpcId.N) --
+ never the expansion itself, so skipping it for EC2 cannot lose anything.
+ """
+ import botocore.session
+
+ from ministack.services.ec2 import _ACTION_MAP
+ from ministack.services.stepfunctions import _sfn_key_to_api_name
+
+ model = botocore.session.get_session().get_service_model("ec2")
+ operations = set(model.operation_names)
+ broken_by_expansion, needs_expansion = set(), set()
+
+ def walk(shape, seen):
+ if shape is None or id(shape) in seen:
+ return
+ seen.add(id(shape))
+ if shape.type_name == "structure":
+ for name, member in shape.members.items():
+ serialized = member.serialization.get("queryName") or member.serialization.get("name")
+ wire = serialized[0].upper() + serialized[1:] if serialized else name
+ expanded = _sfn_key_to_api_name(name)
+ if expanded != name:
+ if wire == name:
+ broken_by_expansion.add(name)
+ elif wire == expanded:
+ needs_expansion.add(name)
+ walk(member, seen)
+ elif shape.type_name == "list":
+ walk(shape.member, seen)
+ elif shape.type_name == "map":
+ walk(shape.value, seen)
+
+ for action in _ACTION_MAP:
+ if action in operations:
+ walk(model.operation_model(action).input_shape, set())
+ assert not needs_expansion, (
+ f"these EC2 members have the expansion for their wire name, so EC2 must not skip it: "
+ f"{sorted(needs_expansion)}")
+
+ # Make sure we found some: a walk that stops descending still finds 22
+ assert len(broken_by_expansion) >= 25
+
+ # Spot-checks with the expansion each one would have produced.
+ for name, would_become in (("VpcId", "VPCId"),
+ ("EnableDnsHostnames", "EnableDNSHostnames"),
+ ("KmsKeyId", "KMSKeyId"),
+ ("EbsOptimized", "EBSOptimized"),
+ ("IamInstanceProfile", "IAMInstanceProfile"),
+ ("NetworkAclId", "NetworkACLId"),
+ ("PeerVpcId", "PeerVPCId"),
+ ("Iops", "IOPS"),
+ ("DnsOptions", "DNSOptions")):
+ assert _sfn_key_to_api_name(name) == would_become # what EC2 would have been sent
+ assert name in broken_by_expansion, f"{name} is a wire name EC2 accepts as written"
+
+
def test_sfn_aws_sdk_ec2_vpc_calls_shape_nested_sets(sfn_sync, ec2):
"""VPC calls carry nested *Set structs, which the shaping has to descend without recursing."""
suffix = _uuid_mod.uuid4().hex[:8]