Skip to content

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

fix(waf): CreateIPSet and CreateRuleGroup refuse a duplicate name (#2331)

Kiran Malsettyd3e2ddd4bf4f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 92352422..bed1d67b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
- **Route 53 — VPC associations for private hosted zones** — `AssociateVPCWithHostedZone`, `DisassociateVPCFromHostedZone` and `ListHostedZonesByVPC` (paged with `MaxItems` and `NextToken`). They refuse a public zone (`PublicZoneVPCAssociation`), a VPC already associated with a zone of the same name (`ConflictingDomainExists`), a VPC the zone doesn't have (404 `VPCAssociationNotFound`) and the zone's last VPC (`LastVPCAssociation`). Contributed by @kharabasz.
### Fixed
+- **WAF v2 — `CreateIPSet` and `CreateRuleGroup` refuse a duplicate name** — a second IP set or rule group with the name and scope of an existing one is now refused with `WAFDuplicateItemException`, as AWS does and as `CreateWebACL` and `CreateRegexPatternSet` already did. Both stored the duplicate silently, so a client that retries a create after losing the response ended up with two. Contributed by @OS-kiranmalsetty.
- **Route 53 — private hosted zones keep their VPC** — `CreateHostedZone` with a `VPC` made a public zone and dropped the VPC, so Terraform's `aws_route53_zone` with a `vpc` block planned to add it again on every run; the zone is now private, the response carries the `VPC` instead of a delegation set, also on a `CallerReference` replay, and `GetHostedZone` returns its `VPCs`. Contributed by @kharabasz.
- **ELBv2 — listener `SslPolicy` and certificate** — `CreateListener` and `ModifyListener` dropped `SslPolicy` and `Certificates`, so `DescribeListeners` returned neither and Terraform re-planned `ssl_policy` on every HTTPS listener; HTTPS and TLS listeners now keep and return both, the policy defaults to `ELBSecurityPolicy-2016-08`, a listener moved to HTTP or TCP drops them, and more than one default certificate answers `ValidationError`. Contributed by @kharabasz.
- **SQS — `MD5OfMessageAttributes` of filtered attributes** — `ReceiveMessage` with `MessageAttributeNames` selecting some of a message's attributes returned only those attributes but the digest of all of them, so SDKs that validate `MD5OfMessageAttributes` rejected the response; the digest is now computed over the attributes returned. Contributed by @Xevos117.
diff --git a/ministack/services/waf.py b/ministack/services/waf.py
index dd0519fd..628455ea 100644
--- a/ministack/services/waf.py
+++ b/ministack/services/waf.py
@@ -561,6 +561,9 @@ def _list_resources_for_web_acl(data):
def _create_ip_set(data):
name = data.get("Name", "")
scope = _normalize_scope(data.get("Scope", "REGIONAL"))
+ for existing in _values_for_scope(_ip_sets, scope):
+ if existing["Name"] == name and existing.get("Scope", scope) == scope:
+ return _waf_err("WAFDuplicateItemException", f"An IPSet with name '{name}' already exists.")
uid = new_uuid()
lock_token = new_uuid()
arn = _ipset_arn(name, uid, scope)
@@ -706,6 +709,9 @@ def _list_regex_pattern_sets(data):
def _create_rule_group(data):
name = data.get("Name", "")
scope = _normalize_scope(data.get("Scope", "REGIONAL"))
+ for existing in _values_for_scope(_rule_groups, scope):
+ if existing["Name"] == name and existing.get("Scope", scope) == scope:
+ return _waf_err("WAFDuplicateItemException", f"A RuleGroup with name '{name}' already exists.")
uid = new_uuid()
lock_token = new_uuid()
arn = _rg_arn(name, uid, scope)
diff --git a/tests/test_waf.py b/tests/test_waf.py
index 134cd1b6..6714ec48 100644
--- a/tests/test_waf.py
+++ b/tests/test_waf.py
@@ -978,3 +978,33 @@ def test_wafv2_get_web_acl_validates_id_shape_before_existence(wafv2):
Name="nope", Scope="REGIONAL", Id=str(_uuid_mod.uuid4())
)
assert exc.value.response["Error"]["Code"] == "WAFNonexistentItemException"
+
+
+def test_waf_ip_set_and_rule_group_names_are_unique_per_scope(wafv2):
+ sfx = _uuid_mod.uuid4().hex[:8]
+ ip_name, rg_name = f"dup-ipset-{sfx}", f"dup-rg-{sfx}"
+ ip = wafv2.create_ip_set(Name=ip_name, Scope="REGIONAL", IPAddressVersion="IPV4", Addresses=[])["Summary"]
+ rg = wafv2.create_rule_group(
+ Name=rg_name, Scope="REGIONAL", Capacity=10, Rules=[],
+ VisibilityConfig={"SampledRequestsEnabled": False, "CloudWatchMetricsEnabled": False, "MetricName": "m"},
+ )["Summary"]
+
+ # AWS refuses a second one with the same name and scope, so a client that retries a create sees the error.
+ with pytest.raises(wafv2.exceptions.WAFDuplicateItemException):
+ wafv2.create_ip_set(Name=ip_name, Scope="REGIONAL", IPAddressVersion="IPV4", Addresses=[])
+ with pytest.raises(wafv2.exceptions.WAFDuplicateItemException):
+ wafv2.create_rule_group(
+ Name=rg_name, Scope="REGIONAL", Capacity=10, Rules=[],
+ VisibilityConfig={"SampledRequestsEnabled": False, "CloudWatchMetricsEnabled": False, "MetricName": "m"},
+ )
+ assert [s["Id"] for s in wafv2.list_ip_sets(Scope="REGIONAL")["IPSets"] if s["Name"] == ip_name] == [ip["Id"]]
+ assert [s["Id"] for s in wafv2.list_rule_groups(Scope="REGIONAL")["RuleGroups"] if s["Name"] == rg_name] == [rg["Id"]]
+
+ # the name is only taken within its scope
+ other = wafv2.create_ip_set(Name=ip_name, Scope="CLOUDFRONT", IPAddressVersion="IPV4", Addresses=[])["Summary"]
+ assert other["Id"] != ip["Id"]
+ wafv2.delete_ip_set(Name=ip_name, Scope="CLOUDFRONT", Id=other["Id"], LockToken=other["LockToken"])
+
+ # deleting frees the name
+ wafv2.delete_ip_set(Name=ip_name, Scope="REGIONAL", Id=ip["Id"], LockToken=ip["LockToken"])
+ wafv2.create_ip_set(Name=ip_name, Scope="REGIONAL", IPAddressVersion="IPV4", Addresses=[])