Skip to content

armgit / floci

Public Imported from GitHub
0

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

refactor(core): move the Query list member pattern into a shared helper (#6306)

Priamo Germosen27caa823496a
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryLists.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryLists.java
new file mode 100644
index 0000000000..287240144f
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryLists.java
@@ -0,0 +1,54 @@
+package io.github.hectorvent.floci.core.common;
+
+import jakarta.ws.rs.core.MultivaluedMap;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.regex.Pattern;
+
+/**
+ * Reads a list from an AWS Query request. The protocol numbers a list's members under the list's
+ * name, in the generic {@code List.member.N} form, or as {@code List.Name.N} when the list's shape
+ * names its member (the real Redshift SDK sends {@code SubnetIds.SubnetIdentifier.N}, not
+ * {@code SubnetIds.member.N}). A bare {@code List.N} is read too. Every N is read, in order, with no
+ * stop at a gap.
+ */
+public final class AwsQueryLists {
+
+ private AwsQueryLists() {
+ }
+
+ /**
+ * The keys of the members of {@code list}, in the generic form or under {@code member}, the name
+ * its shape gives them; with a null {@code member}, the generic form only.
+ */
+ public static Pattern memberKeys(String list, String member) {
+ String names = member == null ? "\\.member" : "\\.member|\\." + Pattern.quote(member);
+ return Pattern.compile(Pattern.quote(list) + "(" + names + ")?\\.\\d+");
+ }
+
+ /** The non-blank values of the members of {@code list}, as {@link #memberKeys} reads them, in order. */
+ public static List<String> values(MultivaluedMap<String, String> params, String list, String member) {
+ Pattern keys = memberKeys(list, member);
+ return params.keySet().stream()
+ .filter(key -> keys.matcher(key).matches())
+ .sorted(Comparator.comparingInt(AwsQueryLists::memberIndex))
+ .map(params::getFirst)
+ .filter(value -> value != null && !value.isBlank())
+ .toList();
+ }
+
+ /** The number a member's key ends in, or {@link Integer#MAX_VALUE} for a key that ends in none. */
+ public static int memberIndex(String key) {
+ int lastDot = key.lastIndexOf('.');
+ if (lastDot < 0 || lastDot == key.length() - 1) {
+ return Integer.MAX_VALUE;
+ }
+ try {
+ return Integer.parseInt(key.substring(lastDot + 1));
+ } catch (NumberFormatException expected) {
+ // Not a number, or one too large for an index: the key sorts after every member.
+ return Integer.MAX_VALUE;
+ }
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/PassedRoles.java b/src/main/java/io/github/hectorvent/floci/core/common/PassedRoles.java
index 58cc0ed14b..c5c63593b8 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/PassedRoles.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/PassedRoles.java
@@ -3,7 +3,6 @@ package io.github.hectorvent.floci.core.common;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
-import io.github.hectorvent.floci.services.redshift.RedshiftQueryHandler;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
@@ -372,12 +371,15 @@ final class PassedRoles {
}
}
- /** The roles a Redshift request lists under {@code list}, every numbered member the Redshift handler reads. */
+ /**
+ * The roles a Redshift request lists under {@code list}, every member the Redshift handler reads: the
+ * list's shape names them {@code IamRoleArn}.
+ */
private static List<PassedRole> redshiftRoles(ContainerRequestContext ctx, String list) {
- String member = RedshiftQueryHandler.memberKeyRegex(list);
+ Pattern member = AwsQueryLists.memberKeys(list, "IamRoleArn");
List<String> roles = new ArrayList<>();
IamConditionContextResolver.formParameters(ctx).forEach((key, value) -> {
- if (key.matches(member)) {
+ if (member.matcher(key).matches()) {
roles.add(value);
}
});
diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java
index 2a993c5600..f4d2d1e143 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java
@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsErrorMessages;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsNamespaces;
+import io.github.hectorvent.floci.core.common.AwsQueryLists;
import io.github.hectorvent.floci.core.common.AwsQueryResponse;
import io.github.hectorvent.floci.core.common.XmlBuilder;
import io.github.hectorvent.floci.services.docdb.DocDbQueryHandler;
@@ -3533,51 +3534,35 @@ public class RdsQueryHandler {
}
private static List<String> memberList(MultivaluedMap<String, String> params, String baseName) {
- return params.keySet().stream()
- .filter(key -> key.matches(memberKeyRegex(baseName)))
- .sorted(java.util.Comparator.comparingInt(RdsQueryHandler::numericSuffix))
- .map(params::getFirst)
- .filter(value -> value != null && !value.isBlank())
- .toList();
+ return AwsQueryLists.values(params, baseName, memberName(baseName));
}
private static boolean hasMemberKeys(MultivaluedMap<String, String> params, String baseName) {
- return params.keySet().stream().anyMatch(key -> key.matches(memberKeyRegex(baseName)));
+ Pattern keys = AwsQueryLists.memberKeys(baseName, memberName(baseName));
+ return params.keySet().stream().anyMatch(key -> keys.matcher(key).matches());
}
- private static String memberKeyRegex(String baseName) {
- String quoted = java.util.regex.Pattern.quote(baseName);
+ /** The name the list {@code baseName}'s shape gives its members, or null for a list whose shape names none. */
+ private static String memberName(String baseName) {
return switch (baseName) {
- case "SubnetIds" -> quoted + "(\\.member|\\.SubnetIdentifier)?\\.\\d+";
- case "VpcSecurityGroupIds" -> quoted + "(\\.member|\\.VpcSecurityGroupId)?\\.\\d+";
- case "OptionsToRemove" -> quoted + "(\\.member|\\.OptionName)?\\.\\d+";
- case "ValuesToAdd", "ValuesToRemove" -> quoted + "(\\.member|\\.AttributeValue)?\\.\\d+";
+ case "SubnetIds" -> "SubnetIdentifier";
+ case "VpcSecurityGroupIds" -> "VpcSecurityGroupId";
+ case "OptionsToRemove" -> "OptionName";
+ case "ValuesToAdd", "ValuesToRemove" -> "AttributeValue";
default -> {
// Option configurations nest their membership lists under an indexed prefix,
// so the alternate member names have to be matched on the suffix.
if (baseName.endsWith("VpcSecurityGroupMemberships")) {
- yield quoted + "(\\.member|\\.VpcSecurityGroupId)?\\.\\d+";
+ yield "VpcSecurityGroupId";
}
if (baseName.endsWith("DBSecurityGroupMemberships")) {
- yield quoted + "(\\.member|\\.DBSecurityGroupName)?\\.\\d+";
+ yield "DBSecurityGroupName";
}
- yield quoted + "(\\.member)?\\.\\d+";
+ yield null;
}
};
}
- private static int numericSuffix(String key) {
- int dot = key.lastIndexOf('.');
- if (dot < 0 || dot == key.length() - 1) {
- return Integer.MAX_VALUE;
- }
- try {
- return Integer.parseInt(key.substring(dot + 1));
- } catch (NumberFormatException e) {
- return Integer.MAX_VALUE;
- }
- }
-
private static List<DbProxyAuth> parseProxyAuth(MultivaluedMap<String, String> params) {
List<DbProxyAuth> auth = new java.util.ArrayList<>();
for (int index = 1; ; index++) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandler.java
index 042a59c46f..31c8544b50 100644
--- a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandler.java
@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsNamespaces;
+import io.github.hectorvent.floci.core.common.AwsQueryLists;
import io.github.hectorvent.floci.core.common.IamEnforcementFilter;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.RegionResolver;
@@ -1332,7 +1333,7 @@ public class RedshiftQueryHandler {
List<String> values = params.keySet().stream()
.filter(candidate -> candidate.matches(
"Filters\\.DescribeIntegrationsFilter\\." + index + "\\.Values\\.Value\\.\\d+"))
- .sorted(Comparator.comparingInt(RedshiftQueryHandler::numericSuffix))
+ .sorted(Comparator.comparingInt(AwsQueryLists::memberIndex))
.map(params::getFirst)
.toList();
byIndex.put(index, new RedshiftService.IntegrationFilter(name, values));
@@ -1353,46 +1354,26 @@ public class RedshiftQueryHandler {
}
private static List<String> memberList(MultivaluedMap<String, String> params, String baseName) {
- return params.keySet().stream()
- .filter(key -> key.matches(memberKeyRegex(baseName)))
- .sorted(Comparator.comparingInt(RedshiftQueryHandler::numericSuffix))
- .map(params::getFirst)
- .filter(value -> value != null && !value.isBlank())
- .toList();
+ return AwsQueryLists.values(params, baseName, memberName(baseName));
}
/**
- * The keys of the numbered members of the list {@code baseName}. The AWS Query protocol accepts both
- * the generic ".member.N" form and each shape's own locationName (e.g. the real Redshift SDK sends
- * "SubnetIds.SubnetIdentifier.N", not "SubnetIds.member.N"). The {@code iam:PassRole} check reads a
- * cluster's roles through it, so it reads the same keys.
+ * The name the list {@code baseName}'s shape gives its members, which the real Redshift SDK sends
+ * ("SubnetIds.SubnetIdentifier.N", not "SubnetIds.member.N"), or null for a list whose shape names none.
*/
- public static String memberKeyRegex(String baseName) {
- String quoted = Pattern.quote(baseName);
+ private static String memberName(String baseName) {
return switch (baseName) {
- case "SubnetIds" -> quoted + "(\\.member|\\.SubnetIdentifier)?\\.\\d+";
- case "VpcSecurityGroupIds" -> quoted + "(\\.member|\\.VpcSecurityGroupId)?\\.\\d+";
- case "IamRoles", "AddIamRoles", "RemoveIamRoles" -> quoted + "(\\.member|\\.IamRoleArn)?\\.\\d+";
- case "TagKeys" -> quoted + "(\\.member|\\.TagKey)?\\.\\d+";
- case "TagValues" -> quoted + "(\\.member|\\.TagValue)?\\.\\d+";
- case "ScheduleDefinitions" -> quoted + "(\\.member|\\.ScheduleDefinition)?\\.\\d+";
- case "DbGroups" -> quoted + "(\\.member|\\.DbGroup)?\\.\\d+";
- default -> quoted + "(\\.member)?\\.\\d+";
+ case "SubnetIds" -> "SubnetIdentifier";
+ case "VpcSecurityGroupIds" -> "VpcSecurityGroupId";
+ case "IamRoles", "AddIamRoles", "RemoveIamRoles" -> "IamRoleArn";
+ case "TagKeys" -> "TagKey";
+ case "TagValues" -> "TagValue";
+ case "ScheduleDefinitions" -> "ScheduleDefinition";
+ case "DbGroups" -> "DbGroup";
+ default -> null;
};
}
- private static int numericSuffix(String key) {
- int lastDot = key.lastIndexOf('.');
- if (lastDot < 0 || lastDot == key.length() - 1) {
- return Integer.MAX_VALUE;
- }
- try {
- return Integer.parseInt(key.substring(lastDot + 1));
- } catch (NumberFormatException e) {
- return Integer.MAX_VALUE;
- }
- }
-
private static List<Parameter> parseParameters(MultivaluedMap<String, String> params) {
Map<String, Parameter> parsed = new LinkedHashMap<>();
for (String key : params.keySet().stream().sorted().toList()) {
@@ -1416,7 +1397,7 @@ public class RedshiftQueryHandler {
parsed.putIfAbsent(prefix, new Parameter(name, params.getFirst(prefix + ".ParameterValue")));
}
return parsed.entrySet().stream()
- .sorted(Comparator.comparingInt(entry -> numericSuffix(entry.getKey())))
+ .sorted(Comparator.comparingInt(entry -> AwsQueryLists.memberIndex(entry.getKey())))
.map(Map.Entry::getValue).toList();
}
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryListsTest.java b/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryListsTest.java
new file mode 100644
index 0000000000..4396f9837d
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryListsTest.java
@@ -0,0 +1,60 @@
+package io.github.hectorvent.floci.core.common;
+
+import jakarta.ws.rs.core.MultivaluedHashMap;
+import jakarta.ws.rs.core.MultivaluedMap;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.regex.Pattern;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class AwsQueryListsTest {
+
+ @Test
+ void aMemberKeyIsGenericNamedByItsShapeOrBare() {
+ Pattern keys = AwsQueryLists.memberKeys("IamRoles", "IamRoleArn");
+
+ assertTrue(keys.matcher("IamRoles.member.1").matches());
+ assertTrue(keys.matcher("IamRoles.IamRoleArn.2").matches());
+ assertTrue(keys.matcher("IamRoles.10").matches());
+ assertFalse(keys.matcher("IamRoles.Other.1").matches());
+ assertFalse(keys.matcher("AddIamRoles.member.1").matches());
+ assertFalse(keys.matcher("IamRolesExtra.1").matches());
+ assertFalse(keys.matcher("IamRoles.member.1.Arn").matches());
+ assertFalse(keys.matcher("IamRoles").matches());
+ }
+
+ @Test
+ void aListWhoseShapeNamesNoMemberTakesOnlyTheGenericForm() {
+ Pattern keys = AwsQueryLists.memberKeys("LogExports", null);
+
+ assertTrue(keys.matcher("LogExports.member.1").matches());
+ assertTrue(keys.matcher("LogExports.1").matches());
+ assertFalse(keys.matcher("LogExports.LogExport.1").matches());
+ }
+
+ @Test
+ void valuesAreEveryNonBlankMemberInNumericOrderPastAGap() {
+ MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
+ params.putSingle("SubnetIds.SubnetIdentifier.10", "tenth");
+ params.putSingle("SubnetIds.member.2", "second");
+ params.putSingle("SubnetIds.5", " ");
+ params.putSingle("SubnetIds.1", "first");
+ params.putSingle("VpcSecurityGroupIds.member.3", "other list");
+
+ assertEquals(List.of("first", "second", "tenth"),
+ AwsQueryLists.values(params, "SubnetIds", "SubnetIdentifier"));
+ assertEquals(List.of(), AwsQueryLists.values(params, "TagKeys", "TagKey"));
+ }
+
+ @Test
+ void aMemberIndexIsTheNumberTheKeyEndsIn() {
+ assertEquals(12, AwsQueryLists.memberIndex("Filters.Filter.3.Values.Value.12"));
+ assertEquals(Integer.MAX_VALUE, AwsQueryLists.memberIndex("Name"));
+ assertEquals(Integer.MAX_VALUE, AwsQueryLists.memberIndex("List.member."));
+ assertEquals(Integer.MAX_VALUE, AwsQueryLists.memberIndex("List.member.99999999999"));
+ }
+}