feat(iam): read a Query list's constraints from the model (#6625)

The Query walk the managed-policy family reads its constraints through read
scalars only, and refused an operation whose input carries a list when the
vendored file loaded. It reads a list now, and a structure, which is what the
rest of this track is gated on: 34 of IAM's 180 operations carry a list or a
map, and 6 of STS's 11 do.

A list arrives numbered under its own name, Audience.member.1 or the bare
Audience.1, and a member of a list of structures carries its fields under that
prefix, as Tags.member.1.Key does. Each is named in a message the way this
package already names one, filter.1.member and tags.1.member.key, under the
index the request spelled rather than the position the member sits at, which is
what #6478 settled. The count is held to the list's own bounds with the value
left out of the message, which is the form checkListLength and the audience
check both already answer: quoting it would echo every member of the list back.
A list of no members is how an absent list arrives on this protocol, so a
required one reads as null rather than as a list breaking a minimum of 1, and
only a minimum above 1 is reachable at all. A structure member arrives as
TaskPolicyArn.Arn and is named taskPolicyArn.arn, and a request naming none of
its fields is a request without the member.

GetAccountAuthorizationDetails is the operation this wave opts in, and the one
where the model adds refusals rather than rewording an answer already given:
the handler reads none of its three declared members, so a Filter outside the
model's enum, a MaxItems outside 1 to 1000 and a Marker breaking its length or
its pattern are all answered with a 200 today. They are a 400 ValidationError
now, which is the ordering #6402 settled for this package.

A map is still refused when the file loads, and so is anything nested deeper
than a list of structures. The refusal is the point rather than a gap: this
package answers a map against the whole map today, Map keys must satisfy
constraint on PutAccountProperties, where the protocol numbers a map's entries
as Properties.entry.N.key, and no source here says which of the two AWS
answers. Reading one would reword a merged answer on the strength of a guess.

The reader the walk needs went into AwsQueryLists rather than into a third
copy. That file already reads a list's values, and StsQueryHandler holds a
private copy that captures the prefix and the index for the same reason this
one does; the STS wave is where that copy goes.

A number too large for a long is a number all the same, which the range check
reads as one now. It compared through a long before, so a MaxItems of
9223372036854775808 failed to parse, read as no number at all and reached the
operation past a maximum of 1000. The bound it breaks is one that fits in a
long either way, so the comparison carries no width of its own, and the value is
written as the number reads rather than as the request spelled it, a leading
plus or zero gone, which is what a parsed value already answered.

Seven unit tests cover the new forms, two of them on the reader, which is where
one of them found that a key naming its own member read as a member numbered
after it: the index is a number or the key is not a member's. Four wire tests
cover the operation. Removing the list branch fails four of the six list and
structure tests and two of the four wire tests: the two that stay green are
MaxItems and Marker, scalars the walk already read, so they prove the operation
was opted in rather than the walk.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-26 19:17:09 -05:00
1 parent 7c0ee92480
commit 0ec28aee68
10 files changed
+604 -64

No files matched your search

@@ -4,6 +4,7 @@ import jakarta.ws.rs.core.MultivaluedMap;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
@@ -38,6 +39,38 @@ public final class AwsQueryLists {
.toList();
}
/**
* One member of a Query list: the prefix its own parameters are named under, and the index the
* request numbered it with, read back as it was spelled rather than as the number it reads as.
*/
public record IndexedMember(String prefix, String index) {
}
/**
* The members of {@code list}, in the order their indexes read, each with the prefix its own
* parameters are named under. Unlike {@link #values}, a member of a list of structures counts:
* its fields arrive under the prefix rather than at it, as {@code Tags.member.1.Key} does, and
* a member present with an empty value is still a member.
*
* <p>The index is kept as the request spelled it, {@code 007} and all, and a segment that is no
* number is not one: with a null {@code member}, {@code SubnetIds.SubnetIdentifier.4} is a list
* naming its own member rather than a member numbered {@code SubnetIdentifier}, and this reads
* the generic and bare forms only, as {@link #memberKeys} does.
*/
public static List<IndexedMember> members(MultivaluedMap<String, String> params, String list,
String member) {
String names = member == null ? "" : "|\\." + Pattern.quote(member);
Pattern keys = Pattern.compile("(" + Pattern.quote(list) + "(?:\\.member" + names
+ ")?\\.(\\d+))(?:\\..*)?");
return params.keySet().stream()
.map(keys::matcher)
.filter(Matcher::matches)
.map(matcher -> new IndexedMember(matcher.group(1), matcher.group(2)))
.distinct()
.sorted(Comparator.comparingInt(one -> memberIndex(one.prefix())))
.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('.');
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.ws.rs.core.MultivaluedMap;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
@@ -35,10 +36,19 @@ import java.util.regex.Pattern;
* member present and empty is a value: the protocol carries it, so it breaks its minimum length
* rather than reading as absent.
*
* <p>Scalars only for now. An operation whose input shape carries a list or a map is refused when
* the file loads rather than passed over quietly, since reading one means reading
* {@code Name.member.N} and {@code Name.entry.N.key} and naming each index as the request spelled
* it.
* <p>A list arrives numbered under its own name, {@code Audience.member.1} or the bare
* {@code Audience.1}, and a member of a list of structures carries its fields under that prefix,
* {@code Tags.member.1.Key}. Each is named in a message the way this package already names it:
* {@code audience.1.member} and {@code tags.1.member.key}, with the index the request spelled
* rather than the position the member sits at. A list of no members is how an absent list arrives
* on this protocol, so a required one reads as null rather than as a list breaking its minimum, and
* the count is held to the list's own bounds with the value left out of the message. A structure
* member arrives as {@code TaskPolicyArn.Arn} and is named {@code taskPolicyArn.arn}.
*
* <p>A map is refused when the file loads rather than passed over quietly: this package answers one
* today in a form of its own, {@code Map keys must satisfy constraint: [...]} against the whole map,
* and which of the two forms AWS answers for a Query map is a question no source here settles. So is
* anything nested deeper than a list of structures or a structure of scalars.
*/
public final class QueryConstraintCheck {
@@ -47,6 +57,9 @@ public final class QueryConstraintCheck {
/** The shape types a Query parameter can carry on its own, without an index or an entry. */
private static final Set<String> SCALAR_TYPES = Set.of("string", "integer", "long", "boolean");
/** A number as the protocol writes one, of any width: the model's bounds are what hold it. */
private static final Pattern INTEGER = Pattern.compile("[+-]?\\d+");
private final JsonNode operations;
private final JsonNode shapes;
private final Map<String, Pattern> patterns = new HashMap<>();
@@ -60,7 +73,7 @@ public final class QueryConstraintCheck {
}
}
for (Map.Entry<String, JsonNode> operation : operations.properties()) {
requireScalarMembers(operation.getKey(), operation.getValue().asText());
requireReadableMembers(operation.getKey(), operation.getValue().asText());
}
}
@@ -74,16 +87,50 @@ public final class QueryConstraintCheck {
return new QueryConstraintCheck(model);
}
private void requireScalarMembers(String operation, String inputShape) {
private void requireReadableMembers(String operation, String inputShape) {
for (Map.Entry<String, JsonNode> member : shapes.path(inputShape).path("members").properties()) {
String type = shapes.path(member.getValue().asText()).path("type").asText();
requireReadableMember(operation, member.getKey(), member.getValue().asText());
}
}
private void requireReadableMember(String operation, String member, String shapeName) {
String type = shapes.path(shapeName).path("type").asText();
if (SCALAR_TYPES.contains(type)) {
return;
}
switch (type) {
case "list" -> requireReadableElement(operation, member, shapes.path(shapeName).path("member").asText());
case "structure" -> requireScalarFields(operation, member, shapeName);
default -> throw refusal(operation, member, type);
}
}
private void requireReadableElement(String operation, String member, String elementShape) {
String type = shapes.path(elementShape).path("type").asText();
if (SCALAR_TYPES.contains(type)) {
return;
}
if ("structure".equals(type)) {
requireScalarFields(operation, member, elementShape);
return;
}
throw refusal(operation, member, "list of " + type);
}
private void requireScalarFields(String operation, String member, String structure) {
for (Map.Entry<String, JsonNode> field : shapes.path(structure).path("members").properties()) {
String type = shapes.path(field.getValue().asText()).path("type").asText();
if (!SCALAR_TYPES.contains(type)) {
throw new IllegalStateException(operation + "'s " + member.getKey() + " is a " + type
+ ", which this check does not read yet");
throw refusal(operation, member + "." + field.getKey(), type);
}
}
}
private static IllegalStateException refusal(String operation, String member, String type) {
return new IllegalStateException(operation + "'s " + member + " is a " + type
+ ", which this check does not read yet");
}
/**
* Throws the Query protocol's {@code ValidationError} when {@code params} breaks a constraint
* the model declares for {@code operation}. An operation the vendored file does not carry
@@ -102,24 +149,90 @@ public final class QueryConstraintCheck {
if (!shape.hasNonNull("members")) {
return List.of();
}
return structureViolations(shape, "", "", params);
}
/**
* The violations the members of {@code structure} carry, in the order the model lists them.
* {@code prefix} is what the request names them under and {@code path} what a message calls
* them: both are empty at the top level, and name the member above inside a structure or inside
* a list's member.
*/
private List<String> structureViolations(JsonNode structure, String prefix, String path,
MultivaluedMap<String, String> params) {
Set<String> required = new LinkedHashSet<>();
shape.path("required").forEach(name -> required.add(name.asText()));
structure.path("required").forEach(name -> required.add(name.asText()));
List<String> violations = new ArrayList<>();
for (Map.Entry<String, JsonNode> member : shape.path("members").properties()) {
for (Map.Entry<String, JsonNode> member : structure.path("members").properties()) {
String name = member.getKey();
String value = params == null ? null : params.getFirst(name);
String path = memberPath(name);
if (value == null) {
if (required.contains(name)) {
violations.add(SmithyValidationErrors.notNull(path));
String shapeName = member.getValue().asText();
String memberPath = path + memberPath(name);
String param = prefix + name;
boolean mandatory = required.contains(name);
JsonNode shape = shapes.path(shapeName);
switch (shape.path("type").asText()) {
case "list" -> violations.addAll(listViolations(shape, param, memberPath, mandatory, params));
case "structure" -> violations.addAll(nestedViolations(shape, param, memberPath, mandatory, params));
default -> {
String value = params == null ? null : params.getFirst(param);
if (value == null) {
if (mandatory) {
violations.add(SmithyValidationErrors.notNull(memberPath));
}
} else {
violations.addAll(violations(shapeName, value, memberPath));
}
}
continue;
}
violations.addAll(violations(member.getValue().asText(), value, path));
}
return violations;
}
/**
* A list's members, held to the list's own bounds and then each to its element's. A list of no
* members is how an absent list arrives, which is what makes a minimum of 1 unreachable: only a
* list asking for two or more members can report one.
*/
private List<String> listViolations(JsonNode shape, String param, String path, boolean required,
MultivaluedMap<String, String> params) {
List<AwsQueryLists.IndexedMember> members = params == null
? List.of()
: AwsQueryLists.members(params, param, null);
if (members.isEmpty()) {
return required ? List.of(SmithyValidationErrors.notNull(path)) : List.of();
}
List<String> violations = new ArrayList<>();
violations.addAll(single(SmithyValidationErrors.sizeOutOfRange(path, members.size(),
shape.path("min").asInt(0), shape.path("max").asInt(Integer.MAX_VALUE))));
String elementShape = shape.path("member").asText();
boolean structure = "structure".equals(shapes.path(elementShape).path("type").asText());
for (AwsQueryLists.IndexedMember member : members) {
String memberPath = path + "." + member.index() + ".member";
if (structure) {
violations.addAll(structureViolations(shapes.path(elementShape),
member.prefix() + ".", memberPath + ".", params));
} else {
String value = params.getFirst(member.prefix());
violations.addAll(violations(elementShape, value == null ? "" : value, memberPath));
}
}
return violations;
}
/**
* A structure member, whose fields arrive under its own name. A request naming none of them is
* a request without the member at all, so a required one reads as null.
*/
private List<String> nestedViolations(JsonNode shape, String param, String path, boolean required,
MultivaluedMap<String, String> params) {
boolean present = params != null
&& params.keySet().stream().anyMatch(key -> key.startsWith(param + "."));
if (!present) {
return required ? List.of(SmithyValidationErrors.notNull(path)) : List.of();
}
return structureViolations(shape, param + ".", path + ".", params);
}
/** The member's name as a validation message spells it, which is the request's with a lower
* first letter. */
private static String memberPath(String name) {
@@ -158,17 +271,28 @@ public final class QueryConstraintCheck {
* A value that is no number is left to the operation: this package answers one with
* {@code InvalidParameterValue} rather than with a constraint violation, and which member
* reports it that way is the operation's own reading.
*
* <p>A number too large for a {@code long} is a number all the same, and the bound it breaks is
* one that fits in one, so the range is compared without a width of its own. The value is
* written as the number reads rather than as the request spelled it, a leading zero or plus
* gone, which is what a parsed value already answered.
*/
private static String numberViolation(JsonNode shape, String value, String path) {
long number;
try {
number = Long.parseLong(value.trim());
} catch (NumberFormatException expected) {
String text = value.trim();
if (!INTEGER.matcher(text).matches()) {
// Not a number at all, so no range of the model's is what it breaks.
return null;
}
return SmithyValidationErrors.outOfRange(path, number,
shape.path("min").asLong(Long.MIN_VALUE), shape.path("max").asLong(Long.MAX_VALUE));
BigInteger number = new BigInteger(text);
long min = shape.path("min").asLong(Long.MIN_VALUE);
long max = shape.path("max").asLong(Long.MAX_VALUE);
if (number.compareTo(BigInteger.valueOf(min)) < 0) {
return SmithyValidationErrors.valueTooSmall(path, number.toString(), min);
}
if (number.compareTo(BigInteger.valueOf(max)) > 0) {
return SmithyValidationErrors.valueTooLarge(path, number.toString(), max);
}
return null;
}
private static String enumViolation(JsonNode shape, String value, String path) {
@@ -75,16 +75,29 @@ public final class SmithyValidationErrors {
/** The violation for a number outside {@code min} to {@code max}, or {@code null} for one inside. */
public static String outOfRange(String path, long value, long min, long max) {
if (value < min) {
return "Value '" + value + "' at '" + path + "' failed to satisfy constraint: "
+ "Member must have value greater than or equal to " + min;
return valueTooSmall(path, Long.toString(value), min);
}
if (value > max) {
return "Value '" + value + "' at '" + path + "' failed to satisfy constraint: "
+ "Member must have value less than or equal to " + max;
return valueTooLarge(path, Long.toString(value), max);
}
return null;
}
/**
* The violation for a number below {@code min}, with the value already written: a number too
* large for a {@code long} is still a number, and the bound it breaks is one that fits in one.
*/
public static String valueTooSmall(String path, String value, long min) {
return "Value '" + value + "' at '" + path + "' failed to satisfy constraint: "
+ "Member must have value greater than or equal to " + min;
}
/** The violation for a number above {@code max}, with the value already written. */
public static String valueTooLarge(String path, String value, long max) {
return "Value '" + value + "' at '" + path + "' failed to satisfy constraint: "
+ "Member must have value less than or equal to " + max;
}
/** The violation for a string outside its length bounds, or {@code null} for one inside. */
public static String lengthOutOfRange(String path, String value, int min, int max) {
int length = value.codePointCount(0, value.length());
@@ -98,6 +111,24 @@ public final class SmithyValidationErrors {
return null;
}
/**
* The violation for a list holding a member count outside its bounds, or {@code null} for one
* inside. The offending value is left out, which is the form this package already answers for a
* tag list and for an audience: quoting it would echo every member of the list back in the
* message.
*/
public static String sizeOutOfRange(String path, int size, int min, int max) {
if (size < min) {
return "Value at '" + path + "' failed to satisfy constraint: "
+ "Member must have length greater than or equal to " + min;
}
if (size > max) {
return "Value at '" + path + "' failed to satisfy constraint: "
+ "Member must have length less than or equal to " + max;
}
return null;
}
/** Also used by services that reject whitespace-only input as an empty member. */
public static String lengthTooShort(String path, String value, int min) {
return "Value '" + value + "' at '" + path + "' failed to satisfy constraint: "
@@ -11,11 +11,13 @@ import jakarta.ws.rs.core.MultivaluedMap;
* ValidationError} whether or not the thing it names exists, the ordering #6402 settled for this
* package.
*
* <p>The vendored file carries one wave of operations rather than all 180, and grows a wave at a
* time: the managed-policy family first, where no operation checks a declared constraint by hand,
* so the model adds the refusals AWS answers without rewording an answer already given.
* {@code AttachRolePolicy} and {@code DetachRolePolicy} are the exception and the parity case,
* {@code IamService.validateRoleName} already answering {@code roleName} in the same form.
* <p>The vendored file carries a wave of operations rather than all 180, and grows a wave at a
* time, each one chosen where no operation checks a declared constraint by hand, so the model adds
* the refusals AWS answers without rewording an answer already given. The managed-policy family
* came first, with {@code AttachRolePolicy} and {@code DetachRolePolicy} as the exception and the
* parity case, {@code IamService.validateRoleName} already answering {@code roleName} in the same
* form. {@code GetAccountAuthorizationDetails} is the first read through the list walk, its
* {@code Filter}, {@code MaxItems} and {@code Marker} having been read by nothing at all.
*/
final class IamRequestShapes {