Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(route53): compare alias target DNS names canonically (#6357)

Andre Kurait1f31f5bf48bb
diff --git a/compatibility-tests/sdk-test-python/tests/test_route53_alias_target_comparison.py b/compatibility-tests/sdk-test-python/tests/test_route53_alias_target_comparison.py
new file mode 100644
index 0000000000..78d0bda28f
--- /dev/null
+++ b/compatibility-tests/sdk-test-python/tests/test_route53_alias_target_comparison.py
@@ -0,0 +1,85 @@
+"""Alias deletion compares equivalent DNS spellings while retaining value checks."""
+import copy
+import uuid
+
+import boto3
+import pytest
+from botocore.exceptions import ClientError
+
+
+def route53(aws_config, client_config):
+ return boto3.client("route53", config=client_config, **aws_config)
+
+
+def zone(route53):
+ name = "alias-" + uuid.uuid4().hex[:12] + ".example.test"
+ zone_id = route53.create_hosted_zone(Name=name, CallerReference=name)["HostedZone"]["Id"]
+ try:
+ yield zone_id, name
+ finally:
+ records = route53.list_resource_record_sets(HostedZoneId=zone_id)["ResourceRecordSets"]
+ changes = [{"Action": "DELETE", "ResourceRecordSet": record}
+ for record in records if record["Type"] not in ("NS", "SOA")]
+ if changes:
+ route53.change_resource_record_sets(HostedZoneId=zone_id, ChangeBatch={"Changes": changes})
+ route53.delete_hosted_zone(Id=zone_id)
+ with pytest.raises(ClientError) as missing:
+ route53.get_hosted_zone(Id=zone_id)
+ assert missing.value.response["Error"]["Code"] == "NoSuchHostedZone"
+
+
[email protected]("variant", ["add_dot", "remove_dot", "case_and_dot", "different_name",
+ "different_zone", "different_health", "unicode_kelvin",
+ "unicode_long_s", "unicode_dotted_i"])
+def test_alias_delete_dns_spelling_and_atomic_mismatch_rejection(route53, zone, variant):
+ zone_id, zone_name = zone
+ target_name = "target." + zone_name
+ target = {"Name": target_name, "Type": "A", "TTL": 60,
+ "ResourceRecords": [{"Value": "192.0.2.21"}]}
+ alias = {"Name": "alias." + zone_name, "Type": "A", "SetIdentifier": "blue", "Weight": 1,
+ "AliasTarget": {"HostedZoneId": zone_id.rsplit("/", 1)[-1],
+ "DNSName": target_name + ("." if variant == "remove_dot" else ""),
+ "EvaluateTargetHealth": False}}
+ unicode_targets = {"unicode_kelvin": ("\u212a", "k"), "unicode_long_s": ("\u017f", "s"),
+ "unicode_dotted_i": ("\u0130", "i")}
+ if variant in unicode_targets:
+ alias["AliasTarget"]["DNSName"] = unicode_targets[variant][0] + "." + zone_name
+ route53.change_resource_record_sets(
+ HostedZoneId=zone_id,
+ ChangeBatch={"Changes": [{"Action": "CREATE", "ResourceRecordSet": record}
+ for record in (target, alias)]},
+ )
+ requested = copy.deepcopy(alias)
+ if variant == "add_dot":
+ requested["AliasTarget"]["DNSName"] += "."
+ elif variant == "remove_dot":
+ requested["AliasTarget"]["DNSName"] = target_name
+ elif variant == "case_and_dot":
+ requested["AliasTarget"]["DNSName"] = target_name.upper() + "."
+ elif variant == "different_name":
+ requested["AliasTarget"]["DNSName"] = "other." + zone_name
+ elif variant == "different_zone":
+ requested["AliasTarget"]["HostedZoneId"] = "ZOTHER"
+ elif variant in unicode_targets:
+ requested["AliasTarget"]["DNSName"] = unicode_targets[variant][1] + "." + zone_name
+ else:
+ requested["AliasTarget"]["EvaluateTargetHealth"] = True
+ marker = {"Name": "marker." + zone_name, "Type": "TXT", "TTL": 60,
+ "ResourceRecords": [{"Value": '"propagation-marker"'}]}
+ batch = {"Changes": [{"Action": "UPSERT", "ResourceRecordSet": marker},
+ {"Action": "DELETE", "ResourceRecordSet": requested}]}
+ before = route53.list_resource_record_sets(HostedZoneId=zone_id)["ResourceRecordSets"]
+ if variant.startswith("different_") or variant in unicode_targets:
+ with pytest.raises(ClientError) as rejected:
+ route53.change_resource_record_sets(HostedZoneId=zone_id, ChangeBatch=batch)
+ assert rejected.value.response["Error"]["Code"] == "InvalidChangeBatch"
+ assert rejected.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400
+ assert route53.list_resource_record_sets(HostedZoneId=zone_id)["ResourceRecordSets"] == before
+ else:
+ route53.change_resource_record_sets(HostedZoneId=zone_id, ChangeBatch=batch)
+ after = route53.list_resource_record_sets(HostedZoneId=zone_id)["ResourceRecordSets"]
+ assert all(record["Name"].rstrip(".") != alias["Name"] for record in after)
+ assert any(record["Name"].rstrip(".") == marker["Name"] for record in after)
+ assert any(record["Name"].rstrip(".") == target_name for record in after)
diff --git a/docs/services/route53.md b/docs/services/route53.md
index 9b00f7480e..354e1955ad 100644
--- a/docs/services/route53.md
+++ b/docs/services/route53.md
@@ -43,6 +43,7 @@ Route53 emulation supporting hosted zones, resource record sets, health checks,
- `DeleteHostedZone` fails with `HostedZoneNotEmpty` if the zone contains records other than the apex SOA and NS.
- `ChangeResourceRecordSets` validates all changes atomically before applying any.
- Supported change actions: `CREATE`, `UPSERT`, `DELETE`.
+- Alias deletion treats target DNS names without regard to ASCII letter case and accepts an optional trailing dot. The target hosted-zone ID, health-evaluation setting, and other record values must still match; a rejected change batch does not apply its other changes.
- Hosted zone IDs are returned with the `/hostedzone/` prefix in XML responses (e.g. `/hostedzone/Z1PA6795UKMFR9`). The AWS SDK strips this prefix client-side.
- Health check IDs are plain UUIDs without a prefix.
- `CreateHealthCheck` with a `CallerReference` already in use returns that health check when the settings match, so an SDK retry succeeds, and fails with `HealthCheckAlreadyExists` when they differ. A deleted health check's reference can be reused at once; AWS keeps refusing it for some days.
diff --git a/src/main/java/io/github/hectorvent/floci/services/route53/Route53Service.java b/src/main/java/io/github/hectorvent/floci/services/route53/Route53Service.java
index 49fced477b..89f2196c42 100644
--- a/src/main/java/io/github/hectorvent/floci/services/route53/Route53Service.java
+++ b/src/main/java/io/github/hectorvent/floci/services/route53/Route53Service.java
@@ -1033,10 +1033,31 @@ public class Route53Service implements Resettable {
return a == b;
}
return Objects.equals(a.getHostedZoneId(), b.getHostedZoneId())
- && Objects.equals(a.getDnsName(), b.getDnsName())
+ && aliasDnsNamesMatch(a.getDnsName(), b.getDnsName())
&& a.isEvaluateTargetHealth() == b.isEvaluateTargetHealth();
}
+ private static boolean aliasDnsNamesMatch(String a, String b) {
+ if (a == null || b == null) {
+ return a == b;
+ }
+ String left = normalizeName(a);
+ String right = normalizeName(b);
+ if (left.length() != right.length()) {
+ return false;
+ }
+ for (int i = 0; i < left.length(); i++) {
+ if (asciiLower(left.charAt(i)) != asciiLower(right.charAt(i))) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private static char asciiLower(char c) {
+ return c >= 'A' && c <= 'Z' ? (char) (c + ('a' - 'A')) : c;
+ }
+
private static boolean recordValuesMatch(List<ResourceRecord> a, List<ResourceRecord> b) {
List<String> av = a == null ? List.of() : a.stream().map(ResourceRecord::getValue).sorted().toList();
List<String> bv = b == null ? List.of() : b.stream().map(ResourceRecord::getValue).sorted().toList();
diff --git a/src/test/java/io/github/hectorvent/floci/services/route53/Route53ServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/route53/Route53ServiceTest.java
index cc21c1524a..9dbed52c3e 100644
--- a/src/test/java/io/github/hectorvent/floci/services/route53/Route53ServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/route53/Route53ServiceTest.java
@@ -13,6 +13,8 @@ import io.github.hectorvent.floci.services.route53.model.ResourceRecord;
import io.github.hectorvent.floci.services.route53.model.ResourceRecordSet;
import io.github.hectorvent.floci.services.route53.model.VpcAssociation;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.Mockito;
import java.util.ArrayList;
@@ -194,6 +196,70 @@ class Route53ServiceTest {
assertEquals("target1.example.com.", records.get(0).getAliasTarget().getDnsName());
}
+ @ParameterizedTest
+ @CsvSource({"target.example.com,target.example.com.",
+ "target.example.com.,target.example.com",
+ "Target.Example.COM,target.example.com.", "\u212a.Example.COM,\u212a.example.com."})
+ void aliasDeletionAcceptsEquivalentDnsSpelling(String storedName, String requestedName) {
+ Route53Service service = newService();
+ String zoneId = service.createHostedZone("example.com.", "ref-alias-spelling", null, null).zone().getId();
+ service.changeResourceRecordSets(zoneId,
+ List.of(change("CREATE", aliasRecord("alias.example.com.", storedName, false))), null);
+
+ service.changeResourceRecordSets(zoneId, List.of(
+ change("UPSERT", aRecord("marker.example.com.", 60, "192.0.2.21")),
+ change("DELETE", aliasRecord("alias.example.com.", requestedName, false))), null);
+
+ List<ResourceRecordSet> records = service.listResourceRecordSets(zoneId, null, null, 100);
+ assertFalse(records.stream().anyMatch(record -> "alias.example.com.".equals(record.getName())));
+ assertTrue(records.stream().anyMatch(record -> "marker.example.com.".equals(record.getName())));
+ }
+
+ @ParameterizedTest
+ @CsvSource({"\u212a.example.com,k.example.com", "\u017f.example.com,s.example.com",
+ "\u0130.example.com,i.example.com", "\u0131.example.com,I.example.com",
+ "\u00c4.example.com,\u00e4.example.com"})
+ void aliasDeletionRejectsUnicodeCaseEquivalenceAtomically(String storedName, String requestedName) {
+ Route53Service service = newService();
+ String zoneId = service.createHostedZone("example.com.", "ref-alias-unicode", null, null).zone().getId();
+ service.changeResourceRecordSets(zoneId,
+ List.of(change("CREATE", aliasRecord("alias.example.com.", storedName, false))), null);
+
+ AwsException error = assertThrows(AwsException.class, () -> service.changeResourceRecordSets(zoneId,
+ List.of(change("UPSERT", aRecord("marker.example.com.", 60, "192.0.2.21")),
+ change("DELETE", aliasRecord("alias.example.com.", requestedName, false))), null));
+
+ assertEquals("InvalidChangeBatch", error.getErrorCode());
+ List<ResourceRecordSet> records = service.listResourceRecordSets(zoneId, null, null, 100);
+ assertFalse(records.stream().anyMatch(record -> "marker.example.com.".equals(record.getName())));
+ assertEquals(storedName, records.stream().filter(record -> "alias.example.com.".equals(record.getName()))
+ .findFirst().orElseThrow().getAliasTarget().getDnsName());
+ }
+
+ @ParameterizedTest
+ @CsvSource({"ZOTHER,false", "Z1ALIASZONE,true"})
+ void aliasDeletionStillRejectsOtherTargetDifferencesAtomically(String zone, boolean health) {
+ Route53Service service = newService();
+ String zoneId = service.createHostedZone("example.com.", "ref-alias-mismatch", null, null).zone().getId();
+ service.changeResourceRecordSets(zoneId,
+ List.of(change("CREATE", aliasRecord("alias.example.com.", "target.example.com", false))), null);
+ ResourceRecordSet requested = aliasRecord("alias.example.com.", "TARGET.example.com.", health);
+ requested.getAliasTarget().setHostedZoneId(zone);
+
+ AwsException error = assertThrows(AwsException.class, () -> service.changeResourceRecordSets(zoneId,
+ List.of(change("UPSERT", aRecord("marker.example.com.", 60, "192.0.2.21")),
+ change("DELETE", requested)), null));
+
+ assertEquals("InvalidChangeBatch", error.getErrorCode());
+ List<ResourceRecordSet> records = service.listResourceRecordSets(zoneId, null, null, 100);
+ assertFalse(records.stream().anyMatch(record -> "marker.example.com.".equals(record.getName())));
+ ResourceRecordSet retained = records.stream().filter(record -> "alias.example.com.".equals(record.getName()))
+ .findFirst().orElseThrow();
+ assertEquals("target.example.com", retained.getAliasTarget().getDnsName());
+ assertEquals("Z1ALIASZONE", retained.getAliasTarget().getHostedZoneId());
+ assertFalse(retained.getAliasTarget().isEvaluateTargetHealth());
+ }
+
@Test
void deleteFailsWhenSetIdentifierDoesNotMatch() {
Route53Service service = newService();