mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(route53): populate record change IAM conditions (#8297)
* fix(route53): populate record change IAM conditions * fix(route53): align DNS identity and preserve XML validation errors * fix(route53): read record change conditions from the generated table
This commit is contained in:
1 parent
d5b7a11f89
commit
e2712cf344
19 files changed
+846
-156
No files matched your search
@@ -103,6 +103,7 @@ public class IamConditionContextResolver {
|
||||
private final Instance<CloudWatchLogsCrossAccountService> crossAccountService;
|
||||
private final Instance<IamService> iamService;
|
||||
private final Instance<LambdaWebService> lambdaWebService;
|
||||
private final Instance<RequestConditionSpelling> requestConditionSpellings;
|
||||
private final RequestContext requestContext;
|
||||
private final EmulatorConfig config;
|
||||
private final CurrentVertxRequest currentVertxRequest;
|
||||
@@ -121,12 +122,13 @@ public class IamConditionContextResolver {
|
||||
Instance<CloudWatchLogsCrossAccountService> crossAccountService,
|
||||
Instance<IamService> iamService,
|
||||
Instance<LambdaWebService> lambdaWebService,
|
||||
Instance<RequestConditionSpelling> requestConditionSpellings,
|
||||
RequestContext requestContext,
|
||||
EmulatorConfig config,
|
||||
CurrentVertxRequest currentVertxRequest,
|
||||
ResolvedServiceCatalog serviceCatalog) {
|
||||
this(dynamoDbFacade, ec2Service, s3Service, logsService, crossAccountService, iamService,
|
||||
lambdaWebService, requestContext,
|
||||
lambdaWebService, requestConditionSpellings, requestContext,
|
||||
config, currentVertxRequest, serviceCatalog, Clock.systemUTC());
|
||||
}
|
||||
|
||||
@@ -137,6 +139,7 @@ public class IamConditionContextResolver {
|
||||
Instance<CloudWatchLogsCrossAccountService> crossAccountService,
|
||||
Instance<IamService> iamService,
|
||||
Instance<LambdaWebService> lambdaWebService,
|
||||
Instance<RequestConditionSpelling> requestConditionSpellings,
|
||||
RequestContext requestContext,
|
||||
EmulatorConfig config,
|
||||
CurrentVertxRequest currentVertxRequest,
|
||||
@@ -148,6 +151,7 @@ public class IamConditionContextResolver {
|
||||
this.crossAccountService = crossAccountService;
|
||||
this.iamService = iamService;
|
||||
this.lambdaWebService = lambdaWebService;
|
||||
this.requestConditionSpellings = requestConditionSpellings;
|
||||
this.requestContext = requestContext;
|
||||
this.config = config;
|
||||
this.currentVertxRequest = currentVertxRequest;
|
||||
@@ -164,9 +168,7 @@ public class IamConditionContextResolver {
|
||||
case "iam" -> iamConditionContext(action, ctx);
|
||||
case "logs" -> logsConditionContext(action, ctx);
|
||||
case "lambda" -> lambdaConditionContext(action, ctx);
|
||||
case "redshift" -> RequestConditionKeys.requestKeys(action).isEmpty() ? null
|
||||
: requestConditions(action, formParameters(ctx)::get);
|
||||
default -> null;
|
||||
default -> requestMemberConditions(action, ctx);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -322,20 +324,82 @@ public class IamConditionContextResolver {
|
||||
}
|
||||
|
||||
/**
|
||||
* The keys a request for {@code action} carries in its own members, such as a Redshift DbUser or an
|
||||
* OpenTunnel remotePort, read through {@code parameter} from the table in
|
||||
* {@code request-condition-keys.json}; null when it carries none.
|
||||
* The keys a request for {@code action} carries in its own members, such as an OpenTunnel remotePort, read
|
||||
* through {@code parameter} from the table in {@code request-condition-keys.json}; null when it carries none.
|
||||
*/
|
||||
static Map<String, List<String>> requestConditions(String action, Function<String, String> parameter) {
|
||||
return requestConditions(action, member -> {
|
||||
String value = parameter.apply(member);
|
||||
return value == null ? List.of() : List.of(value);
|
||||
}, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* The keys a request for {@code action} carries in its own members, such as a Redshift DbUser or the record names
|
||||
* of a Route 53 change batch, read as its namespace's protocol carries them (a Query form, an XML body), from the
|
||||
* table in {@code request-condition-keys.json}; null when it carries none, or is sent in a protocol no reader here
|
||||
* reads yet.
|
||||
*/
|
||||
private Map<String, List<String>> requestMemberConditions(String action, ContainerRequestContext ctx) {
|
||||
List<String> keys = RequestConditionKeys.requestKeys(action);
|
||||
if (keys.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
String namespace = action.substring(0, action.indexOf(':'));
|
||||
String protocol = RequestConditionKeys.protocol(namespace);
|
||||
Function<String, List<String>> values;
|
||||
if ("query".equals(protocol) || "ec2".equals(protocol)) {
|
||||
Map<String, String> form = formParameters(ctx);
|
||||
values = member -> form.containsKey(member) ? List.of(form.get(member)) : List.of();
|
||||
} else if ("rest-xml".equals(protocol)) {
|
||||
List<String> members = keys.stream()
|
||||
.flatMap(key -> RequestConditionKeys.members(action, key).stream()).toList();
|
||||
Map<String, List<String>> read = XmlParser.valuesAt(bodyText(ctx), members);
|
||||
if (read == null) {
|
||||
// The handler refuses a body it cannot read the same elements from, so nothing is checked on it.
|
||||
return null;
|
||||
}
|
||||
values = member -> read.getOrDefault(member, List.of());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return requestConditions(action, values, requestConditionSpelling(namespace));
|
||||
}
|
||||
|
||||
/**
|
||||
* Every value {@code values} reads for each member the table names for each key {@code action} takes, spelled by
|
||||
* {@code spelling} as the namespace's service spells the member's shape; null when there are none.
|
||||
*/
|
||||
private static Map<String, List<String>> requestConditions(String action, Function<String, List<String>> values,
|
||||
RequestConditionSpelling spelling) {
|
||||
Map<String, List<String>> conditions = new LinkedHashMap<>();
|
||||
for (String key : RequestConditionKeys.requestKeys(action)) {
|
||||
for (String member : RequestConditionKeys.members(action, key)) {
|
||||
putIfPresent(conditions, key, parameter.apply(member));
|
||||
String shape = RequestConditionKeys.shape(action, member);
|
||||
for (String value : values.apply(member)) {
|
||||
if (value != null && !value.isBlank()) {
|
||||
conditions.computeIfAbsent(key, ignored -> new ArrayList<>())
|
||||
.add(spelling == null ? value : spelling.spell(shape, value));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return conditions.isEmpty() ? null : conditions;
|
||||
}
|
||||
|
||||
/** The registered spelling of the namespace's request members; null when there is none. */
|
||||
private RequestConditionSpelling requestConditionSpelling(String namespace) {
|
||||
if (requestConditionSpellings == null || requestConditionSpellings.isUnsatisfied()) {
|
||||
return null;
|
||||
}
|
||||
for (RequestConditionSpelling spelling : requestConditionSpellings) {
|
||||
if (spelling.namespace().equals(namespace)) {
|
||||
return spelling;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Map<String, List<String>> withGlobalContext(Map<String, List<String>> serviceContext,
|
||||
String resourceArn, String region,
|
||||
String accountId, String resourceOwnerAccountId) {
|
||||
|
||||
@@ -47,4 +47,16 @@ final class RequestConditionKeys {
|
||||
TABLE.path("members").path(action).properties().forEach(key -> keys.add(key.getKey()));
|
||||
return keys;
|
||||
}
|
||||
|
||||
/** The protocol a namespace's requests are sent in, as botocore gives it ({@code rest-xml}); null without one. */
|
||||
static String protocol(String namespace) {
|
||||
JsonNode protocol = TABLE.path("protocols").path(namespace);
|
||||
return protocol.isTextual() ? protocol.asText() : null;
|
||||
}
|
||||
|
||||
/** The botocore shape of the request member at {@code member} of {@code action}; null when the table has none. */
|
||||
static String shape(String action, String member) {
|
||||
JsonNode shape = TABLE.path("memberShapes").path(action).path(member);
|
||||
return shape.isTextual() ? shape.asText() : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package io.floci.aws.core.common;
|
||||
|
||||
/**
|
||||
* Spells a value a request member carries the way its service spells the member's botocore shape, for the condition
|
||||
* keys IAM reads from request members (Route 53 spells a {@code DNSName} as its record names are spelled, so
|
||||
* {@code route53:ChangeResourceRecordSetsNormalizedRecordNames} carries the name as AWS normalizes it).
|
||||
*
|
||||
* <p>Implemented by the service whose shapes have a spelling of their own; looked up lazily by
|
||||
* {@code IamConditionContextResolver} through {@code Instance<RequestConditionSpelling>} for the namespace a request
|
||||
* is of, so IAM never depends on that service directly. Which keys an action takes, which members carry them and the
|
||||
* members' shapes all come from the generated {@code request-condition-keys.json}: a spelling only answers for the
|
||||
* shape the table gives, never for a key or an action.</p>
|
||||
*/
|
||||
public interface RequestConditionSpelling {
|
||||
|
||||
/** The namespace of the requests whose members it spells, as the table names it ({@code route53}). */
|
||||
String namespace();
|
||||
|
||||
/** {@code value} as the service spells a member of {@code shape}; {@code value} itself for a shape it does not. */
|
||||
String spell(String shape, String value);
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import java.io.ByteArrayInputStream;
|
||||
import java.io.StringReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -134,6 +135,47 @@ public final class XmlParser {
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* The text of every element at each of {@code paths}, in document order. A path names elements by their local
|
||||
* names below the root element, joined by dots ({@code ChangeBatch.Changes.Change.Action}), so an element of the
|
||||
* same name elsewhere is not read. Null when the body is empty or does not parse, or when an element at a path
|
||||
* holds elements rather than text, which a handler reading it with {@link XMLStreamReader#getElementText}
|
||||
* refuses too.
|
||||
*/
|
||||
public static Map<String, List<String>> valuesAt(String xml, Collection<String> paths) {
|
||||
if (xml == null || xml.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
Map<String, List<String>> values = new LinkedHashMap<>();
|
||||
List<String> open = new ArrayList<>();
|
||||
boolean rootOpen = false;
|
||||
try {
|
||||
XMLStreamReader r = FACTORY.createXMLStreamReader(new StringReader(xml));
|
||||
while (r.hasNext()) {
|
||||
int event = r.next();
|
||||
if (event == XMLStreamConstants.START_ELEMENT) {
|
||||
if (!rootOpen) {
|
||||
rootOpen = true;
|
||||
continue;
|
||||
}
|
||||
open.add(r.getLocalName());
|
||||
String path = String.join(".", open);
|
||||
if (paths.contains(path)) {
|
||||
values.computeIfAbsent(path, ignored -> new ArrayList<>()).add(r.getElementText());
|
||||
open.removeLast();
|
||||
}
|
||||
} else if (event == XMLStreamConstants.END_ELEMENT && !open.isEmpty()) {
|
||||
open.removeLast();
|
||||
}
|
||||
}
|
||||
r.close();
|
||||
} catch (Exception e) {
|
||||
LOG.debugv("Ignoring malformed XML during parse: {0}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the text content of every element whose local name matches {@code elementName}.
|
||||
*
|
||||
|
||||
@@ -29,8 +29,11 @@ import javax.xml.stream.XMLStreamReader;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Deque;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -42,6 +45,15 @@ public class Route53Controller {
|
||||
private static final String XML = "application/xml";
|
||||
|
||||
private static final Set<String> CHANGE_ACTIONS = Set.of("CREATE", "DELETE", "UPSERT");
|
||||
/** Where a change batch's elements sit below the request's root, as Route 53's schema nests them. */
|
||||
private static final String CHANGES = "ChangeBatch/Changes";
|
||||
private static final String CHANGE = CHANGES + "/Change";
|
||||
private static final String RECORD_SET = CHANGE + "/ResourceRecordSet";
|
||||
private static final String RESOURCE_RECORD = RECORD_SET + "/ResourceRecords/ResourceRecord";
|
||||
private static final String ALIAS_TARGET = RECORD_SET + "/AliasTarget";
|
||||
private static final Set<String> RECORD_SET_TEXT = Set.of("Name", "Type", "TTL", "SetIdentifier",
|
||||
"MultiValueAnswer", "Weight", "Region", "Failover", "HealthCheckId");
|
||||
private static final Set<String> ALIAS_TARGET_TEXT = Set.of("HostedZoneId", "DNSName", "EvaluateTargetHealth");
|
||||
|
||||
@Inject
|
||||
Route53Service service;
|
||||
@@ -1013,163 +1025,120 @@ public class Route53Controller {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads each element only where Route 53's schema puts it below the request's root, so an element of the same
|
||||
* name anywhere else (a {@code Name} inside a {@code GeoLocation}) is skipped, and refuses a second copy of an
|
||||
* element a change, record set or alias target holds once. IAM reads the batch's condition keys from these same
|
||||
* elements, through the request members the generated condition key table names, so both read one batch.
|
||||
*/
|
||||
private void parseChangeBatchInto(String body, List<Map<String, Object>> result) {
|
||||
try {
|
||||
XMLStreamReader r = XmlParser.newStreamReader(body);
|
||||
Deque<String> open = new ArrayDeque<>();
|
||||
boolean rootOpen = false;
|
||||
String currentAction = null;
|
||||
ResourceRecordSet currentRrs = null;
|
||||
List<ResourceRecord> currentRecords = null;
|
||||
AliasTarget currentAlias = null;
|
||||
int depth = 0;
|
||||
String currentElement = null;
|
||||
boolean inChangeBatch = false;
|
||||
boolean inChange = false;
|
||||
boolean inRrs = false;
|
||||
boolean inResourceRecords = false;
|
||||
boolean inAlias = false;
|
||||
boolean inHealthCheckConfig = false;
|
||||
Set<String> changeRead = new HashSet<>();
|
||||
Set<String> recordSetRead = new HashSet<>();
|
||||
Set<String> aliasRead = new HashSet<>();
|
||||
|
||||
while (r.hasNext()) {
|
||||
int event = r.next();
|
||||
if (event == XMLStreamConstants.START_ELEMENT) {
|
||||
currentElement = r.getLocalName();
|
||||
switch (currentElement) {
|
||||
case "ChangeBatch" -> inChangeBatch = true;
|
||||
case "Change" -> {
|
||||
if (inChangeBatch) {
|
||||
inChange = true;
|
||||
if (!rootOpen) {
|
||||
rootOpen = true;
|
||||
continue;
|
||||
}
|
||||
String name = r.getLocalName();
|
||||
switch (String.join("/", open)) {
|
||||
case CHANGES -> {
|
||||
if ("Change".equals(name)) {
|
||||
currentAction = null;
|
||||
currentRrs = null;
|
||||
currentRecords = null;
|
||||
changeRead.clear();
|
||||
}
|
||||
}
|
||||
case "Action" -> {
|
||||
if (inChange && !inRrs) {
|
||||
case CHANGE -> {
|
||||
if ("Action".equals(name)) {
|
||||
readOnce(changeRead, name);
|
||||
currentAction = r.getElementText();
|
||||
if (!CHANGE_ACTIONS.contains(currentAction)) {
|
||||
throw new AwsException("InvalidInput",
|
||||
"Invalid value '" + currentAction + "' at 'Action' failed to satisfy "
|
||||
+ "constraint: Member must satisfy enum value set.", 400);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
case "ResourceRecordSet" -> {
|
||||
if (inChange) {
|
||||
inRrs = true;
|
||||
if ("ResourceRecordSet".equals(name)) {
|
||||
readOnce(changeRead, name);
|
||||
currentRrs = new ResourceRecordSet();
|
||||
currentRecords = new ArrayList<>();
|
||||
recordSetRead.clear();
|
||||
}
|
||||
}
|
||||
case "ResourceRecords" -> { if (inRrs) inResourceRecords = true; }
|
||||
case "AliasTarget" -> {
|
||||
if (inRrs) {
|
||||
inAlias = true;
|
||||
case RECORD_SET -> {
|
||||
if (RECORD_SET_TEXT.contains(name)) {
|
||||
readOnce(recordSetRead, name);
|
||||
readRecordSetText(currentRrs, name, r.getElementText());
|
||||
continue;
|
||||
}
|
||||
if ("ResourceRecords".equals(name)) {
|
||||
readOnce(recordSetRead, name);
|
||||
} else if ("AliasTarget".equals(name)) {
|
||||
readOnce(recordSetRead, name);
|
||||
currentAlias = new AliasTarget();
|
||||
aliasRead.clear();
|
||||
}
|
||||
}
|
||||
case "Name" -> {
|
||||
if (inRrs && !inAlias) {
|
||||
String n = r.getElementText();
|
||||
if (n != null && !n.endsWith(".")) n = n + ".";
|
||||
if (currentRrs != null) currentRrs.setName(n);
|
||||
}
|
||||
}
|
||||
case "Type" -> {
|
||||
if (inRrs && !inAlias && !inHealthCheckConfig && currentRrs != null) {
|
||||
currentRrs.setType(r.getElementText());
|
||||
}
|
||||
}
|
||||
case "TTL" -> {
|
||||
if (inRrs && currentRrs != null) {
|
||||
String ttl = r.getElementText();
|
||||
try {
|
||||
currentRrs.setTtl(Long.parseLong(ttl));
|
||||
} catch (NumberFormatException e) {
|
||||
throw new AwsException("InvalidInput",
|
||||
"Invalid value '" + ttl + "' at 'TTL' failed to satisfy constraint: "
|
||||
+ "Member must be a valid long.", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
case "Value" -> {
|
||||
if (inResourceRecords && currentRecords != null) {
|
||||
case RESOURCE_RECORD -> {
|
||||
if ("Value".equals(name)) {
|
||||
currentRecords.add(new ResourceRecord(r.getElementText()));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
case "SetIdentifier" -> {
|
||||
if (inRrs && currentRrs != null) currentRrs.setSetIdentifier(r.getElementText());
|
||||
}
|
||||
case "MultiValueAnswer" -> {
|
||||
if (inRrs && currentRrs != null) {
|
||||
String multiValueAnswer = r.getElementText();
|
||||
if (!"true".equals(multiValueAnswer) && !"false".equals(multiValueAnswer)) {
|
||||
throw new AwsException("InvalidInput",
|
||||
"MultiValueAnswer must be true or false.", 400);
|
||||
case ALIAS_TARGET -> {
|
||||
if (ALIAS_TARGET_TEXT.contains(name)) {
|
||||
readOnce(aliasRead, name);
|
||||
String text = r.getElementText();
|
||||
switch (name) {
|
||||
case "HostedZoneId" -> currentAlias.setHostedZoneId(text);
|
||||
case "DNSName" -> currentAlias.setDnsName(text);
|
||||
default -> currentAlias.setEvaluateTargetHealth("true".equalsIgnoreCase(text));
|
||||
}
|
||||
currentRrs.setMultiValueAnswer(Boolean.valueOf(multiValueAnswer));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
case "Weight" -> {
|
||||
if (inRrs && currentRrs != null) {
|
||||
String weight = r.getElementText();
|
||||
try {
|
||||
currentRrs.setWeight(Long.parseLong(weight));
|
||||
} catch (NumberFormatException e) {
|
||||
throw new AwsException("InvalidInput",
|
||||
"Invalid value '" + weight + "' at 'Weight' failed to satisfy "
|
||||
+ "constraint: Member must be a valid long.", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
case "Region" -> {
|
||||
if (inRrs && !inAlias && currentRrs != null) currentRrs.setRegion(r.getElementText());
|
||||
}
|
||||
case "Failover" -> {
|
||||
if (inRrs && currentRrs != null) currentRrs.setFailover(r.getElementText());
|
||||
}
|
||||
case "HealthCheckId" -> {
|
||||
if (inRrs && !inHealthCheckConfig && currentRrs != null) {
|
||||
currentRrs.setHealthCheckId(r.getElementText());
|
||||
}
|
||||
}
|
||||
case "HostedZoneId" -> {
|
||||
if (inAlias && currentAlias != null) currentAlias.setHostedZoneId(r.getElementText());
|
||||
}
|
||||
case "DNSName" -> {
|
||||
if (inAlias && currentAlias != null) currentAlias.setDnsName(r.getElementText());
|
||||
}
|
||||
case "EvaluateTargetHealth" -> {
|
||||
if (inAlias && currentAlias != null) {
|
||||
currentAlias.setEvaluateTargetHealth(
|
||||
"true".equalsIgnoreCase(r.getElementText()));
|
||||
}
|
||||
default -> {
|
||||
}
|
||||
}
|
||||
} else if (event == XMLStreamConstants.END_ELEMENT) {
|
||||
switch (r.getLocalName()) {
|
||||
case "ResourceRecords" -> inResourceRecords = false;
|
||||
case "AliasTarget" -> {
|
||||
if (inAlias && currentRrs != null && currentAlias != null) {
|
||||
open.addLast(name);
|
||||
} else if (event == XMLStreamConstants.END_ELEMENT && !open.isEmpty()) {
|
||||
String name = open.pollLast();
|
||||
switch (String.join("/", open)) {
|
||||
case RECORD_SET -> {
|
||||
if ("AliasTarget".equals(name)) {
|
||||
currentRrs.setAliasTarget(currentAlias);
|
||||
currentAlias = null;
|
||||
}
|
||||
inAlias = false;
|
||||
currentAlias = null;
|
||||
}
|
||||
case "ResourceRecordSet" -> {
|
||||
if (inRrs && currentRrs != null) {
|
||||
case CHANGE -> {
|
||||
if ("ResourceRecordSet".equals(name)) {
|
||||
if (currentRrs.getName() == null || currentRrs.getType() == null) {
|
||||
throw new AwsException("InvalidInput",
|
||||
"ResourceRecordSet is missing a required Name or Type element.", 400);
|
||||
}
|
||||
boolean hasRecords = currentRecords != null && !currentRecords.isEmpty();
|
||||
boolean hasRecords = !currentRecords.isEmpty();
|
||||
if (hasRecords) {
|
||||
currentRrs.setRecords(currentRecords);
|
||||
}
|
||||
requireExactlyOneRecordShape(currentAction, currentRrs, hasRecords);
|
||||
}
|
||||
inRrs = false;
|
||||
}
|
||||
case "Change" -> {
|
||||
if (inChange) {
|
||||
case CHANGES -> {
|
||||
if ("Change".equals(name)) {
|
||||
if (currentAction == null || currentRrs == null) {
|
||||
throw new AwsException("InvalidInput",
|
||||
"Change is missing a required Action or ResourceRecordSet element.",
|
||||
@@ -1179,11 +1148,12 @@ public class Route53Controller {
|
||||
change.put("action", currentAction);
|
||||
change.put("rrs", currentRrs);
|
||||
result.add(change);
|
||||
currentAction = null;
|
||||
currentRrs = null;
|
||||
currentRecords = null;
|
||||
}
|
||||
inChange = false;
|
||||
currentAction = null;
|
||||
currentRrs = null;
|
||||
currentRecords = null;
|
||||
}
|
||||
default -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1196,6 +1166,43 @@ public class Route53Controller {
|
||||
}
|
||||
}
|
||||
|
||||
/** Sets the record set's {@code element} from its text, refusing a TTL, weight or answer flag that is not one. */
|
||||
private static void readRecordSetText(ResourceRecordSet rrs, String element, String text) {
|
||||
switch (element) {
|
||||
case "Name" -> rrs.setName(text.endsWith(".") ? text : text + ".");
|
||||
case "Type" -> rrs.setType(text);
|
||||
case "TTL" -> rrs.setTtl(longElement(element, text));
|
||||
case "SetIdentifier" -> rrs.setSetIdentifier(text);
|
||||
case "MultiValueAnswer" -> {
|
||||
if (!"true".equals(text) && !"false".equals(text)) {
|
||||
throw new AwsException("InvalidInput", "MultiValueAnswer must be true or false.", 400);
|
||||
}
|
||||
rrs.setMultiValueAnswer(Boolean.valueOf(text));
|
||||
}
|
||||
case "Weight" -> rrs.setWeight(longElement(element, text));
|
||||
case "Region" -> rrs.setRegion(text);
|
||||
case "Failover" -> rrs.setFailover(text);
|
||||
default -> rrs.setHealthCheckId(text);
|
||||
}
|
||||
}
|
||||
|
||||
private static long longElement(String element, String text) {
|
||||
try {
|
||||
return Long.parseLong(text);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new AwsException("InvalidInput", "Invalid value '" + text + "' at '" + element
|
||||
+ "' failed to satisfy constraint: Member must be a valid long.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
/** Records {@code element} as read in the element holding it, refusing a second copy, as Route 53's schema does. */
|
||||
private static void readOnce(Set<String> read, String element) {
|
||||
if (!read.add(element)) {
|
||||
throw new AwsException("InvalidInput", "Invalid XML ; cvc-complex-type.2.4.a: Invalid content was found "
|
||||
+ "starting with element '{\"" + NS + "\":" + element + "}'.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private HealthCheckConfig parseHealthCheckConfig(String body) {
|
||||
HealthCheckConfig cfg = new HealthCheckConfig();
|
||||
cfg.setType(XmlParser.extractFirst(body, "Type", null));
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package io.floci.aws.services.route53;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* A DNS name as Route 53 spells it (Route 53 Developer Guide, "DNS domain name format"): letters lowercase however
|
||||
* they were given, as letters or as escape codes, and every byte other than a to z, 0 to 9, hyphen, underscore and
|
||||
* the dot between labels as a three-digit octal escape code. A record's identity and the record names IAM reads as
|
||||
* condition values both take this spelling, so a name given in escape codes names the record its letters name.
|
||||
*/
|
||||
public final class Route53RecordName {
|
||||
|
||||
private Route53RecordName() {}
|
||||
|
||||
/**
|
||||
* The name as {@code route53:ChangeResourceRecordSetsNormalizedRecordNames} carries it (Route 53 Developer Guide,
|
||||
* "Normalizing the Route 53 condition key values"): without the final dot, and with {@code *} as {@code \052}.
|
||||
*/
|
||||
public static String conditionName(String name) {
|
||||
return spell(name, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* The name a record is stored and matched under: with the final dot, and with {@code *} kept as given, the
|
||||
* spelling Floci's DNS server matches wildcard records by.
|
||||
*/
|
||||
public static String recordName(String name) {
|
||||
if (name == null || name.isEmpty()) {
|
||||
return name;
|
||||
}
|
||||
return spell(name, true) + ".";
|
||||
}
|
||||
|
||||
private static String spell(String name, boolean literalAsterisk) {
|
||||
String source = name.endsWith(".") ? name.substring(0, name.length() - 1) : name;
|
||||
byte[] bytes = source.getBytes(StandardCharsets.UTF_8);
|
||||
StringBuilder spelled = new StringBuilder(bytes.length);
|
||||
for (int index = 0; index < bytes.length; index++) {
|
||||
int value = bytes[index] & 0xFF;
|
||||
boolean escaped = value == '\\' && isEscapeCode(bytes, index + 1);
|
||||
if (escaped) {
|
||||
value = (bytes[index + 1] - '0') * 64 + (bytes[index + 2] - '0') * 8 + (bytes[index + 3] - '0');
|
||||
index += 3;
|
||||
}
|
||||
if (value >= 'A' && value <= 'Z') {
|
||||
value += 'a' - 'A';
|
||||
}
|
||||
if (value >= 'a' && value <= 'z' || value >= '0' && value <= '9' || value == '-' || value == '_'
|
||||
|| value == '*' && literalAsterisk || value == '.' && !escaped) {
|
||||
spelled.append((char) value);
|
||||
} else {
|
||||
spelled.append(String.format(Locale.ROOT, "\\%03o", value));
|
||||
}
|
||||
}
|
||||
return spelled.toString();
|
||||
}
|
||||
|
||||
/** Whether the three bytes from {@code start} are the octal digits of a byte, {@code 000} to {@code 377}. */
|
||||
private static boolean isEscapeCode(byte[] bytes, int start) {
|
||||
return start + 2 < bytes.length && bytes[start] >= '0' && bytes[start] <= '3'
|
||||
&& bytes[start + 1] >= '0' && bytes[start + 1] <= '7'
|
||||
&& bytes[start + 2] >= '0' && bytes[start + 2] <= '7';
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.DomainNames;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.RequestConditionSpelling;
|
||||
import io.floci.aws.core.common.Resettable;
|
||||
import io.floci.aws.core.common.dns.DnsLookupHelper;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
@@ -40,7 +41,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@ApplicationScoped
|
||||
public class Route53Service implements Resettable {
|
||||
public class Route53Service implements Resettable, RequestConditionSpelling {
|
||||
|
||||
private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
private static final SecureRandom RANDOM = new SecureRandom();
|
||||
@@ -1114,15 +1115,7 @@ public class Route53Service implements Resettable {
|
||||
}
|
||||
|
||||
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();
|
||||
return Route53RecordName.recordName(name);
|
||||
}
|
||||
|
||||
private static String generateZoneId() {
|
||||
@@ -1308,4 +1301,16 @@ public class Route53Service implements Resettable {
|
||||
privateZoneNameCounts.clear();
|
||||
publicZoneNameCounts.clear();
|
||||
}
|
||||
|
||||
/** The namespace whose request members this spells, as the generated condition key table names it. */
|
||||
@Override
|
||||
public String namespace() {
|
||||
return "route53";
|
||||
}
|
||||
|
||||
/** A {@code DNSName} as a record name is spelled for a condition key; a member of any other shape as given. */
|
||||
@Override
|
||||
public String spell(String shape, String value) {
|
||||
return "DNSName".equals(shape) ? Route53RecordName.conditionName(value) : value;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user