mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(route53): normalize record name case identity (#6374)
Normalize Route53 record identity without mutating records before batch commit. Reuse the shared ASCII record-name normalization for alias comparison, as requested in maintainer review. Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
d2f06bdd5b
commit
18910223e1
8 files changed
+224
-26
No files matched your search
+2
-1
@@ -173,7 +173,8 @@ public class Route53CfnProvisioner implements CfnResourceProvisioner {
|
||||
String zoneId, String name, String type, String setIdentifier) {
|
||||
return ctx.isUpdate()
|
||||
&& zoneId.equals(priorAttributes.get(RECORD_ZONE_ATTR))
|
||||
&& Route53Service.normalizeName(name).equals(Route53Service.normalizeName(ctx.priorPhysicalId()))
|
||||
&& Route53Service.normalizeRecordName(name)
|
||||
.equals(Route53Service.normalizeRecordName(ctx.priorPhysicalId()))
|
||||
&& type.equals(priorAttributes.get(RECORD_TYPE_ATTR))
|
||||
&& Objects.equals(priorAttributes.get(RECORD_SET_ID_ATTR), setIdentifier);
|
||||
}
|
||||
|
||||
@@ -430,17 +430,17 @@ public class Route53Service implements Resettable {
|
||||
recordStore.get(zoneId).orElse(List.of()));
|
||||
|
||||
records.sort((a, b) -> {
|
||||
int cmp = a.getName().compareTo(b.getName());
|
||||
int cmp = normalizeRecordName(a.getName()).compareTo(normalizeRecordName(b.getName()));
|
||||
if (cmp != 0) return cmp;
|
||||
return a.getType().compareTo(b.getType());
|
||||
});
|
||||
|
||||
if (startName != null && !startName.isEmpty()) {
|
||||
String normalizedStart = normalizeName(startName);
|
||||
String normalizedStart = normalizeRecordName(startName);
|
||||
final String finalStartType = startType;
|
||||
records = records.stream()
|
||||
.filter(r -> {
|
||||
int cmp = r.getName().compareTo(normalizedStart);
|
||||
int cmp = normalizeRecordName(r.getName()).compareTo(normalizedStart);
|
||||
if (cmp > 0) return true;
|
||||
if (cmp == 0 && finalStartType != null && !finalStartType.isEmpty()) {
|
||||
return r.getType().compareTo(finalStartType) >= 0;
|
||||
@@ -889,6 +889,18 @@ public class Route53Service implements Resettable {
|
||||
return name.endsWith(".") ? name : name + ".";
|
||||
}
|
||||
|
||||
public static String normalizeRecordName(String name) {
|
||||
String normalized = normalizeName(name);
|
||||
if (normalized == null) {
|
||||
return null;
|
||||
}
|
||||
StringBuilder result = new StringBuilder(normalized.length());
|
||||
for (int i = 0; i < normalized.length(); i++) {
|
||||
result.append(asciiLower(normalized.charAt(i)));
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
private static String generateZoneId() {
|
||||
StringBuilder sb = new StringBuilder("Z");
|
||||
for (int i = 0; i < 14; i++) {
|
||||
@@ -936,7 +948,7 @@ public class Route53Service implements Resettable {
|
||||
}
|
||||
|
||||
private boolean isApexSoaOrNs(ResourceRecordSet rrs, String zoneName) {
|
||||
return rrs.getName().equals(zoneName) &&
|
||||
return normalizeRecordName(rrs.getName()).equals(normalizeRecordName(zoneName)) &&
|
||||
("SOA".equals(rrs.getType()) || "NS".equals(rrs.getType()));
|
||||
}
|
||||
|
||||
@@ -975,23 +987,29 @@ public class Route53Service implements Resettable {
|
||||
|
||||
private void applyChange(String action, ResourceRecordSet rrs, List<ResourceRecordSet> current) {
|
||||
switch (action) {
|
||||
case "CREATE" -> current.add(rrs);
|
||||
case "CREATE" -> {
|
||||
ResourceRecordSet normalized = new ResourceRecordSet(rrs);
|
||||
normalized.setName(normalizeRecordName(rrs.getName()));
|
||||
current.add(normalized);
|
||||
}
|
||||
case "DELETE" -> current.remove(deleteTarget(current, rrs));
|
||||
case "UPSERT" -> {
|
||||
current.removeIf(r -> sameRecord(r, rrs));
|
||||
current.add(rrs);
|
||||
ResourceRecordSet normalized = new ResourceRecordSet(rrs);
|
||||
normalized.setName(normalizeRecordName(rrs.getName()));
|
||||
current.add(normalized);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether two record sets share an identity: name, type and set identifier. Route 53 treats a
|
||||
* name with or without the trailing dot as the same, and requires a set identifier of at least one
|
||||
* name with ASCII case differences or without the trailing dot as the same, and requires a set identifier of at least one
|
||||
* character, so a blank one is none. That also matches a record an earlier CloudFormation
|
||||
* provisioner stored with the template's undotted Name or empty SetIdentifier.
|
||||
*/
|
||||
public static boolean sameRecord(ResourceRecordSet a, ResourceRecordSet b) {
|
||||
return normalizeName(a.getName()).equals(normalizeName(b.getName()))
|
||||
return normalizeRecordName(a.getName()).equals(normalizeRecordName(b.getName()))
|
||||
&& a.getType().equals(b.getType())
|
||||
&& Objects.equals(blankToNull(a.getSetIdentifier()), blankToNull(b.getSetIdentifier()));
|
||||
}
|
||||
@@ -1038,20 +1056,7 @@ public class Route53Service implements Resettable {
|
||||
}
|
||||
|
||||
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;
|
||||
return Objects.equals(normalizeRecordName(a), normalizeRecordName(b));
|
||||
}
|
||||
|
||||
private static char asciiLower(char c) {
|
||||
|
||||
+13
@@ -20,6 +20,19 @@ public class ResourceRecordSet {
|
||||
|
||||
public ResourceRecordSet() {}
|
||||
|
||||
public ResourceRecordSet(ResourceRecordSet source) {
|
||||
name = source.name;
|
||||
type = source.type;
|
||||
ttl = source.ttl;
|
||||
records = source.records;
|
||||
aliasTarget = source.aliasTarget;
|
||||
weight = source.weight;
|
||||
region = source.region;
|
||||
setIdentifier = source.setIdentifier;
|
||||
failover = source.failover;
|
||||
healthCheckId = source.healthCheckId;
|
||||
}
|
||||
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user