mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(ses): implement v1 ReorderReceiptRuleSet and CloneReceiptRuleSet (#3176)
* feat(ses): implement v1 ReorderReceiptRuleSet and CloneReceiptRuleSet Completes the SES v1 receipt management surface with the two remaining rule-set actions, both reproducing wire behavior probed against real AWS (us-east-1, 2026-09). ReorderReceiptRuleSet demands an exact permutation of the set's rules, with the probed error precedence: rule-set existence first, then a duplicate request name (InvalidParameterValue "Multiple positions found for rule", even when the duplicated name is not in the set), then set rules absent from the request (listed in set order), then request names not in the set (RuleDoesNotExist, first in request order). RuleNames members validate at the Smithy layer under the bare 'ruleNames' path with a nested list-member constraint whose length cap is 100; there is no service-level "Not a valid ruleName" stage, so a 65-char name lands on RuleDoesNotExist. A failed reorder leaves the order unchanged. CloneReceiptRuleSet deep-copies the source's rules under a new creation timestamp; the active flag stays behind. Name validation is two-layer for both members: the Smithy pattern combines violations with originalRuleSetName listed first, and the service-level 64-char check reports the target before the source. An existing target wins over a missing source (AlreadyExists before RuleSetDoesNotExist). Covered by unit tests, Query-protocol integration tests, and AWS SDK compatibility tests run against a live local instance. * fix(ses): parse ReorderReceiptRuleSet RuleNames with the probed AWS index grammar Review flagged that a sparse Query list (RuleNames.member.1 plus member.3) was silently truncated at the first missing index. Probing real SES with raw signed Query requests showed the string-list grammar differs from the receipt-action struct-list grammar: a gap is padded with empty members (which then fail the ruleNames Smithy constraint, so nothing is ever truncated) up to a skip of 10, counted from the previous index or from 1 for a leading gap; a larger gap is MalformedInput "Excessively sparse input would skip N list elements" with a comma-grouped count; an index above 214748369 (exact boundary, found by bisection) is MalformedInput "Index N is illegal"; index 0, a leading-zero spelling, and a non-numeric token are each their own probed MalformedInput; and a repeated index keeps its first value. The same probing showed the ruleNames Smithy violation carries no member index and appears at most once however many members are malformed, so the service now emits it once instead of per member. Covered by wire-level integration tests for each grammar case and a unit test for the single-violation behavior.
This commit is contained in:
1 parent
438ae619fd
commit
f485122ffc
10 files changed
+692
-10
No files matched your search
@@ -577,6 +577,7 @@ public class AwsQueryController {
|
||||
"PutConfigurationSetDeliveryOptions",
|
||||
"CreateReceiptRuleSet", "DescribeReceiptRuleSet", "ListReceiptRuleSets",
|
||||
"DeleteReceiptRuleSet", "SetActiveReceiptRuleSet", "DescribeActiveReceiptRuleSet",
|
||||
"ReorderReceiptRuleSet", "CloneReceiptRuleSet",
|
||||
"CreateReceiptRule", "DescribeReceiptRule", "UpdateReceiptRule",
|
||||
"DeleteReceiptRule", "SetReceiptRulePosition",
|
||||
"CreateReceiptFilter", "ListReceiptFilters", "DeleteReceiptFilter"
|
||||
|
||||
@@ -133,6 +133,8 @@ public class SesQueryHandler {
|
||||
case "DeleteReceiptRuleSet" -> handleDeleteReceiptRuleSet(params, region);
|
||||
case "SetActiveReceiptRuleSet" -> handleSetActiveReceiptRuleSet(params, region);
|
||||
case "DescribeActiveReceiptRuleSet" -> handleDescribeActiveReceiptRuleSet(region);
|
||||
case "ReorderReceiptRuleSet" -> handleReorderReceiptRuleSet(params, region);
|
||||
case "CloneReceiptRuleSet" -> handleCloneReceiptRuleSet(params, region);
|
||||
case "CreateReceiptRule" -> handleCreateReceiptRule(params, region);
|
||||
case "DescribeReceiptRule" -> handleDescribeReceiptRule(params, region);
|
||||
case "UpdateReceiptRule" -> handleUpdateReceiptRule(params, region);
|
||||
@@ -1032,6 +1034,75 @@ public class SesQueryHandler {
|
||||
"DescribeActiveReceiptRuleSet", AwsNamespaces.SES, xml.build())).build();
|
||||
}
|
||||
|
||||
private Response handleReorderReceiptRuleSet(MultivaluedMap<String, String> params, String region) {
|
||||
sesService.reorderReceiptRuleSet(getParam(params, "RuleSetName"),
|
||||
parseReorderRuleNames(params), region);
|
||||
return Response.ok(AwsQueryResponse.envelopeEmptyResult(
|
||||
"ReorderReceiptRuleSet", AwsNamespaces.SES)).build();
|
||||
}
|
||||
|
||||
// Probed boundary of the RuleNames index grammar: 214748369 is the largest index the parser
|
||||
// accepts (larger is MalformedInput "Index N is illegal"), found by bisection; it is near but
|
||||
// not at Integer.MAX_VALUE / 10, so it is pinned as a literal rather than derived.
|
||||
private static final int MAX_RULE_NAMES_INDEX = 214748369;
|
||||
private static final int MAX_RULE_NAMES_INDEX_SKIP = 10;
|
||||
|
||||
/**
|
||||
* Parses the ReorderReceiptRuleSet RuleNames flattened string list with the probed AWS
|
||||
* grammar, which differs from the receipt-action struct list: an index gap is padded with
|
||||
* empty members (up to a skip of {@value MAX_RULE_NAMES_INDEX_SKIP}, counted from the
|
||||
* previous index, or from 1 for a leading gap), which then fail the ruleNames Smithy
|
||||
* constraint downstream; a larger gap, index 0, a leading-zero spelling, a non-numeric
|
||||
* token, and an index beyond {@value MAX_RULE_NAMES_INDEX} are each their own probed
|
||||
* MalformedInput. A repeated index keeps its first value.
|
||||
*/
|
||||
private List<String> parseReorderRuleNames(MultivaluedMap<String, String> params) {
|
||||
String prefix = "RuleNames.member.";
|
||||
Map<Integer, String> byIndex = new java.util.TreeMap<>();
|
||||
for (String key : params.keySet()) {
|
||||
if (!key.startsWith(prefix)) {
|
||||
continue;
|
||||
}
|
||||
String token = key.substring(prefix.length());
|
||||
if (token.isEmpty() || !token.chars().allMatch(c -> c >= '0' && c <= '9')) {
|
||||
throw new AwsException("MalformedInput", "Start of list found where not expected", 400);
|
||||
}
|
||||
if ("0".equals(token)) {
|
||||
throw new AwsException("MalformedInput", "0 is not a valid index", 400);
|
||||
}
|
||||
if (token.charAt(0) == '0') {
|
||||
throw new AwsException("MalformedInput", "Value found where not expected", 400);
|
||||
}
|
||||
if (token.length() > 9 || Integer.parseInt(token) > MAX_RULE_NAMES_INDEX) {
|
||||
throw new AwsException("MalformedInput", "Index " + token + " is illegal", 400);
|
||||
}
|
||||
byIndex.putIfAbsent(Integer.parseInt(token), getParam(params, key));
|
||||
}
|
||||
List<String> names = new ArrayList<>();
|
||||
int previous = 0;
|
||||
for (Map.Entry<Integer, String> member : byIndex.entrySet()) {
|
||||
int index = member.getKey();
|
||||
int skip = index - Math.max(previous, 1);
|
||||
if (skip > MAX_RULE_NAMES_INDEX_SKIP) {
|
||||
throw new AwsException("MalformedInput", String.format(java.util.Locale.ROOT,
|
||||
"Excessively sparse input would skip %,d list elements", skip), 400);
|
||||
}
|
||||
while (names.size() < index - 1) {
|
||||
names.add("");
|
||||
}
|
||||
names.add(member.getValue());
|
||||
previous = index;
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
private Response handleCloneReceiptRuleSet(MultivaluedMap<String, String> params, String region) {
|
||||
sesService.cloneReceiptRuleSet(getParam(params, "RuleSetName"),
|
||||
getParam(params, "OriginalRuleSetName"), region);
|
||||
return Response.ok(AwsQueryResponse.envelopeEmptyResult(
|
||||
"CloneReceiptRuleSet", AwsNamespaces.SES)).build();
|
||||
}
|
||||
|
||||
private Response handleCreateReceiptRule(MultivaluedMap<String, String> params, String region) {
|
||||
sesService.createReceiptRule(getParam(params, "RuleSetName"), parseReceiptRule(params),
|
||||
getParam(params, "After"), region);
|
||||
|
||||
@@ -19,6 +19,7 @@ import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Predicate;
|
||||
@@ -49,6 +50,9 @@ public class SesReceiptRuleService {
|
||||
// Re-verify against live SES (not the model, which can't confirm it) if these ever need to change.
|
||||
private static final Pattern RULE_SET_NAME_CHARS = Pattern.compile("^[a-zA-Z0-9_.-]+$");
|
||||
private static final int MAX_RULES_PER_SET = 200;
|
||||
// The Smithy cap on a ReorderReceiptRuleSet RuleNames member (probed 2026-09: distinct from
|
||||
// the 64-char service-level name checks; see ruleNamesMemberViolation).
|
||||
private static final int MAX_REORDER_RULE_NAME_LENGTH = 100;
|
||||
private static final int MAX_ACTIONS_PER_RULE = 10;
|
||||
// Probed: a value that is not a well-formed topic/function ARN (a bare name, or an ARN with
|
||||
// missing segments) gets the "Invalid ..." message before any existence lookup. There is no
|
||||
@@ -282,6 +286,92 @@ public class SesReceiptRuleService {
|
||||
LOG.infov("Positioned SES receipt rule {0} in rule set {1} ({2})", ruleName, ruleSetName, region);
|
||||
}
|
||||
|
||||
public void reorderReceiptRuleSet(String ruleSetName, List<String> ruleNames, String region) {
|
||||
if (ruleSetName == null) {
|
||||
throw new AwsException("InvalidParameterValue", "RuleSetName is required.", 400);
|
||||
}
|
||||
List<String> violations = new ArrayList<>();
|
||||
collectRuleNameParamViolations(ruleSetName, "ruleSetName", violations);
|
||||
// Probed: RuleNames members validate under the bare 'ruleNames' path with a nested
|
||||
// list-member constraint whose length cap is 100 (not the 64 of the service-level
|
||||
// name checks), and there is no "Not a valid ruleName" stage after it: a 65-char
|
||||
// pattern-valid name falls through to RuleDoesNotExist. The violation carries no
|
||||
// member index and appears at most once, however many members are malformed.
|
||||
if (ruleNames.stream().anyMatch(name -> name.isEmpty()
|
||||
|| name.length() > MAX_REORDER_RULE_NAME_LENGTH
|
||||
|| !RULE_SET_NAME_CHARS.matcher(name).matches())) {
|
||||
violations.add(ruleNamesMemberViolation());
|
||||
}
|
||||
throwViolations(violations);
|
||||
requireValidRuleSetName(ruleSetName);
|
||||
synchronized (receiptRuleSetLock) {
|
||||
ReceiptRuleSet ruleSet = requireRuleSet(ruleSetName, region);
|
||||
// Probed precedence, matching a build-the-position-map-first algorithm: a duplicate
|
||||
// request name is rejected before anything else (even when the duplicated name is not
|
||||
// in the set), then set rules absent from the request, then request names not in the
|
||||
// set (first in request order).
|
||||
Map<String, Integer> positions = new HashMap<>();
|
||||
for (int i = 0; i < ruleNames.size(); i++) {
|
||||
if (positions.putIfAbsent(ruleNames.get(i), i) != null) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"Multiple positions found for rule: " + ruleNames.get(i), 400);
|
||||
}
|
||||
}
|
||||
List<ReceiptRule> rules = ruleSet.getRules();
|
||||
List<String> missing = rules.stream()
|
||||
.map(ReceiptRule::getName)
|
||||
.filter(name -> !positions.containsKey(name))
|
||||
.toList();
|
||||
if (!missing.isEmpty()) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"Positions for rules not found: " + String.join(", ", missing), 400);
|
||||
}
|
||||
if (positions.size() > rules.size()) {
|
||||
for (String name : ruleNames) {
|
||||
if (rules.stream().noneMatch(r -> name.equals(r.getName()))) {
|
||||
throw new AwsException("RuleDoesNotExist", "Rule does not exist: " + name, 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
List<ReceiptRule> reordered = new ArrayList<>(rules);
|
||||
reordered.sort(Comparator.comparingInt(rule -> positions.get(rule.getName())));
|
||||
replaceRules(ruleSet, reordered, ruleSetName, region);
|
||||
}
|
||||
LOG.infov("Reordered SES receipt rule set {0} in region {1}", ruleSetName, region);
|
||||
}
|
||||
|
||||
public ReceiptRuleSet cloneReceiptRuleSet(String ruleSetName, String originalRuleSetName, String region) {
|
||||
if (ruleSetName == null) {
|
||||
throw new AwsException("InvalidParameterValue", "RuleSetName is required.", 400);
|
||||
}
|
||||
if (originalRuleSetName == null) {
|
||||
throw new AwsException("InvalidParameterValue", "OriginalRuleSetName is required.", 400);
|
||||
}
|
||||
List<String> violations = new ArrayList<>();
|
||||
// Probed: with both members malformed, the combined message lists originalRuleSetName
|
||||
// before ruleSetName; the service-level checks then run target before source.
|
||||
collectRuleNameParamViolations(originalRuleSetName, "originalRuleSetName", violations);
|
||||
collectRuleNameParamViolations(ruleSetName, "ruleSetName", violations);
|
||||
throwViolations(violations);
|
||||
requireValidRuleSetName(ruleSetName);
|
||||
requireValidRuleSetName(originalRuleSetName);
|
||||
// The clone gets its own creation time and starts inactive; neither is copied (probed:
|
||||
// cloning the active rule set leaves the clone inactive and the active set unchanged).
|
||||
ReceiptRuleSet clone = new ReceiptRuleSet(ruleSetName, Instant.now(clock));
|
||||
synchronized (receiptRuleSetLock) {
|
||||
if (receiptRuleSetStore.get(receiptRuleSetKey(region, ruleSetName)).isPresent()) {
|
||||
// Probed: an existing target wins over a missing source.
|
||||
throw new AwsException("AlreadyExists", "Rule set already exists: " + ruleSetName, 400);
|
||||
}
|
||||
ReceiptRuleSet original = requireRuleSet(originalRuleSetName, region);
|
||||
clone.setRules(original.getRules().stream().map(ReceiptRule::copy).toList());
|
||||
receiptRuleSetStore.put(receiptRuleSetKey(region, ruleSetName), clone);
|
||||
}
|
||||
LOG.infov("Cloned SES receipt rule set {0} from {1} in region {2}",
|
||||
ruleSetName, originalRuleSetName, region);
|
||||
return clone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Swaps in a freshly built rules list and persists the set. Mutations never touch the list a
|
||||
* previously returned rule set holds: the storage backends hand out live object references, so
|
||||
@@ -443,6 +533,18 @@ public class SesReceiptRuleService {
|
||||
+ "Member must satisfy enum value set: " + valueSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* The one violation shape a malformed ReorderReceiptRuleSet RuleNames member produces,
|
||||
* probed: empty, longer than 100, and pattern-breaking names all get this same nested
|
||||
* list-member constraint under the bare 'ruleNames' path, with no member index.
|
||||
*/
|
||||
private static String ruleNamesMemberViolation() {
|
||||
return "Value at 'ruleNames' failed to satisfy constraint: Member must satisfy constraint: "
|
||||
+ "[Member must have length less than or equal to " + MAX_REORDER_RULE_NAME_LENGTH
|
||||
+ ", Member must have length greater than or equal to 1, "
|
||||
+ "Member must satisfy regular expression pattern: ^[a-zA-Z0-9_.-]+$]";
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates every resource an action points at against the local emulator, reproducing the
|
||||
* checks real SES runs on create/update: any TopicArn must be a publishable SNS topic, an
|
||||
@@ -768,6 +870,15 @@ public class SesReceiptRuleService {
|
||||
"1 validation error detected: Value at 'ruleSetName' failed to satisfy constraint: "
|
||||
+ "Member must satisfy regular expression pattern: ^[a-zA-Z0-9_.-]+$", 400);
|
||||
}
|
||||
requireValidRuleSetName(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* The service-level rule set name check that runs after the Smithy pattern passes. Reorder
|
||||
* and clone reach it directly: their empty-name case is a Smithy length violation on the
|
||||
* wire (probed), not the "RuleSetName is required." of {@link #requireRuleSetName}.
|
||||
*/
|
||||
private static void requireValidRuleSetName(String name) {
|
||||
if (name.length() > 64
|
||||
|| !Character.isLetterOrDigit(name.charAt(0))
|
||||
|| !Character.isLetterOrDigit(name.charAt(name.length() - 1))) {
|
||||
|
||||
@@ -1155,6 +1155,14 @@ public class SesService {
|
||||
return receiptRuleService.describeActiveReceiptRuleSet(region);
|
||||
}
|
||||
|
||||
public void reorderReceiptRuleSet(String ruleSetName, List<String> ruleNames, String region) {
|
||||
receiptRuleService.reorderReceiptRuleSet(ruleSetName, ruleNames, region);
|
||||
}
|
||||
|
||||
public ReceiptRuleSet cloneReceiptRuleSet(String ruleSetName, String originalRuleSetName, String region) {
|
||||
return receiptRuleService.cloneReceiptRuleSet(ruleSetName, originalRuleSetName, region);
|
||||
}
|
||||
|
||||
// The bounce-sender check needs identity state; the rule domain receives it as a predicate
|
||||
// bound here, keeping SesIdentityService out of its constructor surface.
|
||||
|
||||
|
||||
@@ -75,6 +75,13 @@ public class ReceiptAction {
|
||||
return getProperties().get(name);
|
||||
}
|
||||
|
||||
/** Deep copy; the property map is mutable. */
|
||||
public ReceiptAction copy() {
|
||||
ReceiptAction copy = new ReceiptAction(type);
|
||||
copy.properties = new LinkedHashMap<>(getProperties());
|
||||
return copy;
|
||||
}
|
||||
|
||||
public boolean is(String actionType) {
|
||||
return actionType.equals(type);
|
||||
}
|
||||
|
||||
@@ -82,4 +82,16 @@ public class ReceiptRule {
|
||||
public void setActions(List<ReceiptAction> actions) {
|
||||
this.actions = actions;
|
||||
}
|
||||
|
||||
/** Deep copy, so a cloned rule set never aliases the mutable state of the original's rules. */
|
||||
public ReceiptRule copy() {
|
||||
ReceiptRule copy = new ReceiptRule();
|
||||
copy.name = name;
|
||||
copy.enabled = enabled;
|
||||
copy.tlsPolicy = tlsPolicy;
|
||||
copy.scanEnabled = scanEnabled;
|
||||
copy.recipients = new ArrayList<>(getRecipients());
|
||||
copy.actions = new ArrayList<>(getActions().stream().map(ReceiptAction::copy).toList());
|
||||
return copy;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user