mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
feat(iam): read the managed-policy family's constraints from the model (#6580)
The declared constraints on IAM's managed-policy operations are read from the pinned model rather than from a hand-written check, through the vendored data tools/aws/regen_iam_shapes.py generates and a Query walk of it. Twelve operations go first: GetPolicy, DeletePolicy, the four policy-version operations, and the six attach and detach pairs. The vendored file carries that wave rather than all 180 operations, as the ECR and Resource Groups files carry theirs, and grows a wave at a time. The machinery was already here for the JSON protocols. request_shapes build_constraints vendors required members, lengths, patterns and enums; RequestConstraintCheck walks a JSON body against them; SmithyValidationErrors already writes the awsQuery wording. What was missing is the walk a Query request needs, which is why QueryConstraintCheck sits beside RequestConstraintCheck rather than inside it: a JSON body arrives as a tree, where a Query request arrives as flat parameters named after the members that carry them, and the violations are collected into one "N validation errors detected" message in the model's member order instead of the first one being thrown. A test sends two broken members in the opposite order to the model's and gets the model's order back, so the ordering is the model's rather than the wire's. What changes for a caller is the status, in the direction #6402 settled for this package: a PolicyArn under arnType's 20 characters, a VersionId outside policyVersionIdType's pattern or an absent required member used to reach the store and answer NoSuchEntity 404, where AWS's validation layer runs ahead of the operation and answers ValidationError 400. AttachRolePolicy's RoleName is the parity case for every value but one: IamService.validateRoleName already answered it from the same bounds and the same pattern, and for a name outside its pattern the message is unchanged by the model taking it over, which a test pins. An empty name is that one value, and the one place the wording moves rather than the status. validateRoleName stops at the first constraint an empty name breaks and reports the minimum alone, where the model also reports the pattern it breaks, so these twelve operations answer two violations where they answered one. That is the reading #6402 merged for a value breaking two of its constraints, and it makes validateRoleName the drift rather than the reference. The operations still on the hand-written check keep the single violation until a later wave takes them over, which a test holds still on CreateRole so that the wave which does has to settle the wording first. Two answers are the package's own rather than new. A member breaking two constraints is reported twice, which is what #6402 merged for a ServiceSpecificCredentialId that breaks both its length and its pattern, so an empty PolicyDocument reports its minimum and its pattern. The offending value stays quoted, as SmithyValidationErrors and validateRoleName already quote it; whether current IAM quotes it at all is an open question, and reading the constraints in one place is what would make that one helper to change rather than every message in the package. Scalars only so far. An operation whose input shape carries a list or a map is refused when the vendored file loads rather than passed over with its constraints unread, since reading one means reading Name.member.N and Name.entry.N.key and naming each index as the request spelled it. A boolean is declared and holds no constraint to be read against, so a SetAsDefault that is neither true nor false still reaches the operation, which reads anything but true as false. A test pins that rather than changing it, since what AWS answers for a malformed boolean is unrecorded here.
This commit is contained in:
1 parent
4b33002784
commit
bb48983b9c
11 files changed
+1019
No files matched your search
@@ -25,6 +25,8 @@
|
||||
# by tools/aws/regen_ecs_shapes.py.
|
||||
# src/main/resources/aws/ecr-request-shapes.json, the ECR request constraints, is generated
|
||||
# from botocore's ECR model by tools/aws/regen_ecr_shapes.py.
|
||||
# src/main/resources/aws/iam-request-shapes.json, the IAM request constraints of the operations
|
||||
# read from the model, is generated from botocore's IAM model by tools/aws/regen_iam_shapes.py.
|
||||
# src/main/resources/aws/emr-serverless-shapes.json, the shapes the Step Functions emr-serverless
|
||||
# integrations recase against, is generated from botocore's EMR Serverless model by
|
||||
# tools/aws/regen_emr_serverless_shapes.py.
|
||||
@@ -230,6 +232,7 @@ aws-data-sync: ## Regenerate src/main/resources/aws/*.json and the IAM managed-p
|
||||
$(PYTHON) tools/aws/regen_redshift_serverless_shapes.py
|
||||
$(PYTHON) tools/aws/regen_resource_groups_shapes.py
|
||||
$(PYTHON) tools/aws/regen_ecr_shapes.py
|
||||
$(PYTHON) tools/aws/regen_iam_shapes.py
|
||||
$(PYTHON) tools/aws/regen_apigateway_types.py
|
||||
$(PYTHON) tools/aws/regen_sesv2_shapes.py
|
||||
$(PYTHON) tools/aws/regen_apigateway_shapes.py
|
||||
@@ -258,6 +261,12 @@ aws-data-check: ## CI gate: the vendored AWS data must match a fresh generation
|
||||
echo " Run 'make aws-data-sync' and commit the result."; \
|
||||
exit 1; \
|
||||
}
|
||||
@$(PYTHON) tools/aws/regen_iam_shapes.py --check || { \
|
||||
echo ""; \
|
||||
echo "error: src/main/resources/aws/iam-request-shapes.json is out of date."; \
|
||||
echo " Run 'make aws-data-sync' and commit the result."; \
|
||||
exit 1; \
|
||||
}
|
||||
@$(PYTHON) tools/aws/regen_redshift_serverless_shapes.py --check || { \
|
||||
echo ""; \
|
||||
echo "error: src/main/resources/aws/redshift-serverless-request-shapes.json is out of date."; \
|
||||
|
||||
@@ -154,6 +154,23 @@ outside 1 to 1000. `PathPrefix` and `Scope` filter before paging, and roles and
|
||||
policies are listed in name order so a `Marker` resumes where the previous page stopped.
|
||||
`ListPolicies` does not yet apply `OnlyAttached` or `PolicyUsageFilter`.
|
||||
|
||||
The declared constraints on the managed-policy family are read from the model rather than from a
|
||||
hand-written check, `tools/aws/regen_iam_shapes.py` vendoring them for `GetPolicy`, `DeletePolicy`,
|
||||
`CreatePolicyVersion`, `GetPolicyVersion`, `DeletePolicyVersion`, `SetDefaultPolicyVersion` and the
|
||||
six attach and detach operations. A `PolicyArn` is held to `arnType`'s 20 to 2048 characters, a
|
||||
`VersionId` to `policyVersionIdType`'s pattern, a `PolicyDocument` to its 1 to 131072 characters and
|
||||
its pattern, and each principal name to its own bound, which for a role is the 1 to 64 characters of
|
||||
`[\w+=,.@-]` this service already answered. The check runs ahead of the operation, so a value that
|
||||
breaks a constraint is a 400 `ValidationError` where it used to reach the store and answer
|
||||
`NoSuchEntity` 404, the ordering the rest of this package already follows. A member sent empty is a
|
||||
member the request carries, so it breaks its own constraints rather than reading as absent, and a
|
||||
member breaking two of them is reported twice, as a `ServiceSpecificCredentialId` breaking both of
|
||||
its own already is. That is the one place the wording moves rather than only the status: a name sent
|
||||
empty breaks its minimum and its pattern at once, so these operations report both, where the
|
||||
hand-written check the rest of the service still uses stops at the first and reports the minimum
|
||||
alone. A boolean member publishes no constraint to hold it to, so a `SetAsDefault` that is neither
|
||||
`true` nor `false` reaches the operation, which reads anything but `true` as false.
|
||||
|
||||
### Permission Boundaries
|
||||
|
||||
| Action | Description |
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Checks an AWS Query request against the constraints a {@code tools/aws/regen_*_shapes.py}
|
||||
* generator vendors from botocore through {@code request_shapes.build_constraints}: the members a
|
||||
* structure requires, a string's length, pattern and enum, and a number's range. It reads the same
|
||||
* vendored shape {@link RequestConstraintCheck} reads for the JSON protocols, and walks it
|
||||
* differently because the requests differ: a JSON body arrives as a tree, where a Query request
|
||||
* arrives as flat form parameters named after the members that carry them.
|
||||
*
|
||||
* <p>Every violation is collected rather than the first one thrown, which is what AWS's validation
|
||||
* layer answers: "N validation errors detected" with the violations in the order the model lists
|
||||
* the members, and a member that breaks two constraints reported twice. That is the reading #6402
|
||||
* merged for a `ServiceSpecificCredentialId` breaking both its length and its pattern, and
|
||||
* `docs/services/iam.md` carries it.
|
||||
*
|
||||
* <p>The offending value is quoted, which is what {@link SmithyValidationErrors#lengthOutOfRange}
|
||||
* and this package's `validateRoleName` already answer. Whether current IAM quotes it at all is
|
||||
* what DRIFT-13 asks, and the point of reading the constraints here is that one capture would
|
||||
* change one helper rather than every message in the package.
|
||||
*
|
||||
* <p>A member the request leaves out is absent, and only a required member is reported for one. A
|
||||
* 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.
|
||||
*/
|
||||
public final class QueryConstraintCheck {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
/** 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");
|
||||
|
||||
private final JsonNode operations;
|
||||
private final JsonNode shapes;
|
||||
private final Map<String, Pattern> patterns = new HashMap<>();
|
||||
|
||||
private QueryConstraintCheck(JsonNode model) {
|
||||
this.operations = model.path("operations");
|
||||
this.shapes = model.path("shapes");
|
||||
for (Map.Entry<String, JsonNode> shape : shapes.properties()) {
|
||||
if (shape.getValue().has("pattern")) {
|
||||
patterns.put(shape.getKey(), Pattern.compile(shape.getValue().get("pattern").asText()));
|
||||
}
|
||||
}
|
||||
for (Map.Entry<String, JsonNode> operation : operations.properties()) {
|
||||
requireScalarMembers(operation.getKey(), operation.getValue().asText());
|
||||
}
|
||||
}
|
||||
|
||||
/** The vendored resource {@code name}; {@code description} names it in a failure. */
|
||||
public static QueryConstraintCheck load(String name, String description) {
|
||||
return VendoredResources.read(name, description,
|
||||
input -> new QueryConstraintCheck(MAPPER.readTree(input)));
|
||||
}
|
||||
|
||||
public static QueryConstraintCheck of(JsonNode model) {
|
||||
return new QueryConstraintCheck(model);
|
||||
}
|
||||
|
||||
private void requireScalarMembers(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();
|
||||
if (!SCALAR_TYPES.contains(type)) {
|
||||
throw new IllegalStateException(operation + "'s " + member.getKey() + " 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
|
||||
* passes, which is how the file grows one wave at a time.
|
||||
*/
|
||||
public void check(String operation, MultivaluedMap<String, String> params) {
|
||||
List<String> violations = violations(operation, params);
|
||||
if (!violations.isEmpty()) {
|
||||
throw SmithyValidationErrors.validationError(violations);
|
||||
}
|
||||
}
|
||||
|
||||
/** The violations {@code params} carries for {@code operation}, in the model's member order. */
|
||||
public List<String> violations(String operation, MultivaluedMap<String, String> params) {
|
||||
JsonNode shape = shapes.path(operations.path(operation).asText());
|
||||
if (!shape.hasNonNull("members")) {
|
||||
return List.of();
|
||||
}
|
||||
Set<String> required = new LinkedHashSet<>();
|
||||
shape.path("required").forEach(name -> required.add(name.asText()));
|
||||
List<String> violations = new ArrayList<>();
|
||||
for (Map.Entry<String, JsonNode> member : shape.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));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
violations.addAll(violations(member.getValue().asText(), value, path));
|
||||
}
|
||||
return violations;
|
||||
}
|
||||
|
||||
/** 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) {
|
||||
return Character.toLowerCase(name.charAt(0)) + name.substring(1);
|
||||
}
|
||||
|
||||
/** Every constraint {@code value} breaks, in the order a shape declares them. */
|
||||
private List<String> violations(String shapeName, String value, String path) {
|
||||
JsonNode shape = shapes.path(shapeName);
|
||||
String type = shape.path("type").asText();
|
||||
if ("integer".equals(type) || "long".equals(type)) {
|
||||
return single(numberViolation(shape, value, path));
|
||||
}
|
||||
if (!"string".equals(type)) {
|
||||
return List.of();
|
||||
}
|
||||
List<String> violations = new ArrayList<>();
|
||||
String length = SmithyValidationErrors.lengthOutOfRange(path, value,
|
||||
shape.path("min").asInt(0), shape.path("max").asInt(Integer.MAX_VALUE));
|
||||
if (length != null) {
|
||||
violations.add(length);
|
||||
}
|
||||
Pattern pattern = patterns.get(shapeName);
|
||||
if (pattern != null && !pattern.matcher(value).matches()) {
|
||||
violations.add(SmithyValidationErrors.pattern(path, value, pattern.pattern()));
|
||||
}
|
||||
violations.addAll(single(enumViolation(shape, value, path)));
|
||||
return violations;
|
||||
}
|
||||
|
||||
private static List<String> single(String violation) {
|
||||
return violation == null ? List.of() : List.of(violation);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
private static String numberViolation(JsonNode shape, String value, String path) {
|
||||
long number;
|
||||
try {
|
||||
number = Long.parseLong(value.trim());
|
||||
} catch (NumberFormatException expected) {
|
||||
// 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));
|
||||
}
|
||||
|
||||
private static String enumViolation(JsonNode shape, String value, String path) {
|
||||
if (!shape.has("enum")) {
|
||||
return null;
|
||||
}
|
||||
List<String> allowed = new ArrayList<>();
|
||||
shape.path("enum").forEach(choice -> allowed.add(choice.asText()));
|
||||
return allowed.contains(value) ? null : SmithyValidationErrors.notInEnum(value, path, allowed);
|
||||
}
|
||||
}
|
||||
@@ -104,6 +104,12 @@ public final class SmithyValidationErrors {
|
||||
+ "Member must have length greater than or equal to " + min;
|
||||
}
|
||||
|
||||
/** The violation for a string that does not match the pattern its shape declares. */
|
||||
public static String pattern(String path, String value, String pattern) {
|
||||
return "Value '" + value + "' at '" + path + "' failed to satisfy constraint: "
|
||||
+ "Member must satisfy regular expression pattern: " + pattern;
|
||||
}
|
||||
|
||||
/** The violation for a value outside its enum, listing the allowed values in the model's order. */
|
||||
public static String notInEnum(String value, String path, List<String> allowed) {
|
||||
return "Value '" + value + "' at '" + path + "' failed to satisfy constraint: "
|
||||
|
||||
@@ -144,6 +144,7 @@ public class IamQueryHandler {
|
||||
LOG.debugv("IAM action: {0}", action);
|
||||
|
||||
try {
|
||||
IamRequestShapes.check(action, params);
|
||||
return switch (action) {
|
||||
// Users
|
||||
case "CreateUser" -> handleCreateUser(params);
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.QueryConstraintCheck;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
|
||||
/**
|
||||
* The declared constraints IAM reads from its model rather than from a hand-written check, as
|
||||
* {@code tools/aws/regen_iam_shapes.py} vendors them into
|
||||
* {@code aws/iam-request-shapes.json}. The check runs ahead of the operation, which is where AWS's
|
||||
* validation layer runs: a request that breaks a declared constraint is a 400 {@code
|
||||
* 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.
|
||||
*/
|
||||
final class IamRequestShapes {
|
||||
|
||||
private static final QueryConstraintCheck CHECK =
|
||||
QueryConstraintCheck.load("aws/iam-request-shapes.json", "IAM request shapes");
|
||||
|
||||
private IamRequestShapes() {
|
||||
}
|
||||
|
||||
/** Throws {@code ValidationError} when the request breaks a constraint the model declares. */
|
||||
static void check(String action, MultivaluedMap<String, String> params) {
|
||||
CHECK.check(action, params);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
{
|
||||
"_source": {
|
||||
"generator": "tools/aws/regen_iam_shapes.py",
|
||||
"botocore": "botocore 1.43.113 (installed package)"
|
||||
},
|
||||
"operations": {
|
||||
"AttachGroupPolicy": "AttachGroupPolicyRequest",
|
||||
"AttachRolePolicy": "AttachRolePolicyRequest",
|
||||
"AttachUserPolicy": "AttachUserPolicyRequest",
|
||||
"DetachGroupPolicy": "DetachGroupPolicyRequest",
|
||||
"DetachRolePolicy": "DetachRolePolicyRequest",
|
||||
"DetachUserPolicy": "DetachUserPolicyRequest",
|
||||
"GetPolicy": "GetPolicyRequest",
|
||||
"DeletePolicy": "DeletePolicyRequest",
|
||||
"CreatePolicyVersion": "CreatePolicyVersionRequest",
|
||||
"GetPolicyVersion": "GetPolicyVersionRequest",
|
||||
"DeletePolicyVersion": "DeletePolicyVersionRequest",
|
||||
"SetDefaultPolicyVersion": "SetDefaultPolicyVersionRequest"
|
||||
},
|
||||
"shapes": {
|
||||
"AttachGroupPolicyRequest": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
"GroupName",
|
||||
"PolicyArn"
|
||||
],
|
||||
"members": {
|
||||
"GroupName": "groupNameType",
|
||||
"PolicyArn": "arnType"
|
||||
}
|
||||
},
|
||||
"AttachRolePolicyRequest": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
"RoleName",
|
||||
"PolicyArn"
|
||||
],
|
||||
"members": {
|
||||
"RoleName": "roleNameType",
|
||||
"PolicyArn": "arnType"
|
||||
}
|
||||
},
|
||||
"AttachUserPolicyRequest": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
"UserName",
|
||||
"PolicyArn"
|
||||
],
|
||||
"members": {
|
||||
"UserName": "userNameType",
|
||||
"PolicyArn": "arnType"
|
||||
}
|
||||
},
|
||||
"CreatePolicyVersionRequest": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
"PolicyArn",
|
||||
"PolicyDocument"
|
||||
],
|
||||
"members": {
|
||||
"PolicyArn": "arnType",
|
||||
"PolicyDocument": "policyDocumentType",
|
||||
"SetAsDefault": "booleanType"
|
||||
}
|
||||
},
|
||||
"DeletePolicyRequest": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
"PolicyArn"
|
||||
],
|
||||
"members": {
|
||||
"PolicyArn": "arnType"
|
||||
}
|
||||
},
|
||||
"DeletePolicyVersionRequest": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
"PolicyArn",
|
||||
"VersionId"
|
||||
],
|
||||
"members": {
|
||||
"PolicyArn": "arnType",
|
||||
"VersionId": "policyVersionIdType"
|
||||
}
|
||||
},
|
||||
"DetachGroupPolicyRequest": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
"GroupName",
|
||||
"PolicyArn"
|
||||
],
|
||||
"members": {
|
||||
"GroupName": "groupNameType",
|
||||
"PolicyArn": "arnType"
|
||||
}
|
||||
},
|
||||
"DetachRolePolicyRequest": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
"RoleName",
|
||||
"PolicyArn"
|
||||
],
|
||||
"members": {
|
||||
"RoleName": "roleNameType",
|
||||
"PolicyArn": "arnType"
|
||||
}
|
||||
},
|
||||
"DetachUserPolicyRequest": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
"UserName",
|
||||
"PolicyArn"
|
||||
],
|
||||
"members": {
|
||||
"UserName": "userNameType",
|
||||
"PolicyArn": "arnType"
|
||||
}
|
||||
},
|
||||
"GetPolicyRequest": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
"PolicyArn"
|
||||
],
|
||||
"members": {
|
||||
"PolicyArn": "arnType"
|
||||
}
|
||||
},
|
||||
"GetPolicyVersionRequest": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
"PolicyArn",
|
||||
"VersionId"
|
||||
],
|
||||
"members": {
|
||||
"PolicyArn": "arnType",
|
||||
"VersionId": "policyVersionIdType"
|
||||
}
|
||||
},
|
||||
"SetDefaultPolicyVersionRequest": {
|
||||
"type": "structure",
|
||||
"required": [
|
||||
"PolicyArn",
|
||||
"VersionId"
|
||||
],
|
||||
"members": {
|
||||
"PolicyArn": "arnType",
|
||||
"VersionId": "policyVersionIdType"
|
||||
}
|
||||
},
|
||||
"arnType": {
|
||||
"type": "string",
|
||||
"min": 20,
|
||||
"max": 2048
|
||||
},
|
||||
"booleanType": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"groupNameType": {
|
||||
"type": "string",
|
||||
"min": 1,
|
||||
"max": 128,
|
||||
"pattern": "[\\w+=,.@-]+"
|
||||
},
|
||||
"policyDocumentType": {
|
||||
"type": "string",
|
||||
"min": 1,
|
||||
"max": 131072,
|
||||
"pattern": "[\\u0009\\u000A\\u000D\\u0020-\\u00FF]+"
|
||||
},
|
||||
"policyVersionIdType": {
|
||||
"type": "string",
|
||||
"pattern": "v[1-9][0-9]*(\\.[A-Za-z0-9-]*)?"
|
||||
},
|
||||
"roleNameType": {
|
||||
"type": "string",
|
||||
"min": 1,
|
||||
"max": 64,
|
||||
"pattern": "[\\w+=,.@-]+"
|
||||
},
|
||||
"userNameType": {
|
||||
"type": "string",
|
||||
"min": 1,
|
||||
"max": 64,
|
||||
"pattern": "[\\w+=,.@-]+"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class QueryConstraintCheckTest {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
private static final String MODEL = """
|
||||
{
|
||||
"operations": {"Put": "PutRequest", "Take": "TakeRequest"},
|
||||
"shapes": {
|
||||
"PutRequest": {"type": "structure", "required": ["Name", "Arn"], "members": {
|
||||
"Name": "nameType", "Arn": "arnType", "Status": "statusType",
|
||||
"Age": "ageType", "Default": "booleanType"}},
|
||||
"TakeRequest": {"type": "structure", "members": {"Name": "nameType"}},
|
||||
"nameType": {"type": "string", "min": 1, "max": 8, "pattern": "[\\\\w+=,.@-]+"},
|
||||
"arnType": {"type": "string", "min": 20, "max": 2048},
|
||||
"statusType": {"type": "string", "enum": ["Active", "Inactive"]},
|
||||
"ageType": {"type": "integer", "min": 1, "max": 30},
|
||||
"booleanType": {"type": "boolean"}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
private static QueryConstraintCheck check() throws Exception {
|
||||
return QueryConstraintCheck.of(MAPPER.readTree(MODEL));
|
||||
}
|
||||
|
||||
private static MultivaluedMap<String, String> params(String... pairs) {
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
for (int pair = 0; pair < pairs.length; pair += 2) {
|
||||
params.putSingle(pairs[pair], pairs[pair + 1]);
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
/** Every required member the request leaves out, in the order the model lists the members. */
|
||||
@Test
|
||||
void theRequiredMembersAreReportedTogetherInTheModelsOrder() throws Exception {
|
||||
List<String> violations = check().violations("Put", params());
|
||||
|
||||
assertEquals(List.of(
|
||||
"Value null at 'name' failed to satisfy constraint: Member must not be null",
|
||||
"Value null at 'arn' failed to satisfy constraint: Member must not be null"),
|
||||
violations);
|
||||
}
|
||||
|
||||
/**
|
||||
* A member sent empty is a member the request carries, so what it breaks are its own
|
||||
* constraints, where one that never arrived is the member reported as null. An empty value
|
||||
* breaks both the minimum length and a pattern that asks for a character, and both are
|
||||
* reported, as #6402 settled for an id breaking two constraints.
|
||||
*/
|
||||
@Test
|
||||
void aMemberSentEmptyBreaksItsConstraintsRatherThanReadingAsAbsent() throws Exception {
|
||||
List<String> violations = check().violations("Take", params("Name", ""));
|
||||
|
||||
assertEquals(List.of(
|
||||
"Value '' at 'name' failed to satisfy constraint: "
|
||||
+ "Member must have length greater than or equal to 1",
|
||||
"Value '' at 'name' failed to satisfy constraint: "
|
||||
+ "Member must satisfy regular expression pattern: [\\w+=,.@-]+"),
|
||||
violations);
|
||||
assertEquals(List.of(), check().violations("Take", params()), "Name is optional here");
|
||||
}
|
||||
|
||||
/** Length, pattern and enum, each reported by the member that breaks it. */
|
||||
@Test
|
||||
void aStringIsHeldToItsLengthItsPatternAndItsEnum() throws Exception {
|
||||
assertEquals(List.of("Value 'abcdefghi' at 'name' failed to satisfy constraint: "
|
||||
+ "Member must have length less than or equal to 8"),
|
||||
check().violations("Take", params("Name", "abcdefghi")),
|
||||
"nine word characters break the length and nothing else");
|
||||
|
||||
assertEquals(List.of("Value 'two w' at 'name' failed to satisfy constraint: "
|
||||
+ "Member must satisfy regular expression pattern: [\\w+=,.@-]+"),
|
||||
check().violations("Take", params("Name", "two w")),
|
||||
"a space inside the length breaks the pattern and nothing else");
|
||||
|
||||
assertEquals(List.of(
|
||||
"Value 'two words' at 'name' failed to satisfy constraint: "
|
||||
+ "Member must have length less than or equal to 8",
|
||||
"Value 'two words' at 'name' failed to satisfy constraint: "
|
||||
+ "Member must satisfy regular expression pattern: [\\w+=,.@-]+"),
|
||||
check().violations("Take", params("Name", "two words")),
|
||||
"a value breaking both is reported twice");
|
||||
|
||||
assertEquals(List.of("Value 'Pending' at 'status' failed to satisfy constraint: "
|
||||
+ "Member must satisfy enum value set: [Active, Inactive]"),
|
||||
check().violations("Put", params("Name", "ok", "Arn", "a".repeat(20),
|
||||
"Status", "Pending")));
|
||||
}
|
||||
|
||||
/** A number is held to its range, and one that is no number at all is left to the operation. */
|
||||
@Test
|
||||
void aNumberIsHeldToItsRangeAndANonNumberIsLeftToTheOperation() throws Exception {
|
||||
MultivaluedMap<String, String> tooOld = params("Name", "ok", "Arn", "a".repeat(20), "Age", "31");
|
||||
assertEquals(List.of("Value '31' at 'age' failed to satisfy constraint: "
|
||||
+ "Member must have value less than or equal to 30"),
|
||||
check().violations("Put", tooOld));
|
||||
|
||||
MultivaluedMap<String, String> words = params("Name", "ok", "Arn", "a".repeat(20), "Age", "soon");
|
||||
assertEquals(List.of(), check().violations("Put", words));
|
||||
}
|
||||
|
||||
/** A boolean declares nothing a request can break, so nothing is checked for one. */
|
||||
@Test
|
||||
void aBooleanIsNotChecked() throws Exception {
|
||||
assertEquals(List.of(), check().violations("Put",
|
||||
params("Name", "ok", "Arn", "a".repeat(20), "Default", "yes")));
|
||||
}
|
||||
|
||||
/** The file carries one wave of operations, so an operation outside it passes untouched. */
|
||||
@Test
|
||||
void anOperationTheFileDoesNotCarryPasses() throws Exception {
|
||||
assertEquals(List.of(), check().violations("Delete", params("Name", "two words")));
|
||||
}
|
||||
|
||||
/** The protocol's code and the Smithy count, from the same violations. */
|
||||
@Test
|
||||
void checkThrowsTheQueryProtocolsValidationError() throws Exception {
|
||||
AwsException thrown = assertThrows(AwsException.class,
|
||||
() -> check().check("Put", params("Name", "two w")));
|
||||
|
||||
assertEquals("ValidationError", thrown.getErrorCode());
|
||||
assertEquals(400, thrown.getHttpStatus());
|
||||
assertEquals("2 validation errors detected: Value 'two w' at 'name' failed to satisfy"
|
||||
+ " constraint: Member must satisfy regular expression pattern: [\\w+=,.@-]+;"
|
||||
+ " Value null at 'arn' failed to satisfy constraint: Member must not be null",
|
||||
thrown.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* A list or a map is read from numbered parameters rather than from one, so an operation
|
||||
* carrying either is refused when the file loads rather than passed over without its
|
||||
* constraints.
|
||||
*/
|
||||
@Test
|
||||
void anOperationCarryingAListIsRefusedWhenTheFileLoads() throws Exception {
|
||||
JsonNode model = MAPPER.readTree("""
|
||||
{
|
||||
"operations": {"Tag": "TagRequest"},
|
||||
"shapes": {
|
||||
"TagRequest": {"type": "structure", "members": {"Tags": "tagList"}},
|
||||
"tagList": {"type": "list", "member": "nameType", "max": 50},
|
||||
"nameType": {"type": "string", "min": 1}
|
||||
}
|
||||
}
|
||||
""");
|
||||
|
||||
IllegalStateException thrown =
|
||||
assertThrows(IllegalStateException.class, () -> QueryConstraintCheck.of(model));
|
||||
|
||||
assertTrue(thrown.getMessage().contains("Tag's Tags is a list"), thrown.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* The vendored IAM file itself, read the way the handler reads it. Loading it is the check on
|
||||
* it: every operation's members have to be scalars and every pattern has to be one Java's
|
||||
* engine compiles, and a wave that breaks either would otherwise surface as a failure on the
|
||||
* first IAM request rather than here. Naming the twelve operations keeps the wave's own
|
||||
* contract, so an operation renamed or dropped in the model is a failure with a name on it.
|
||||
*/
|
||||
@Test
|
||||
void theVendoredIamFileLoadsAndCarriesTheOperationsThisWaveClaims() {
|
||||
QueryConstraintCheck iam =
|
||||
QueryConstraintCheck.load("aws/iam-request-shapes.json", "IAM request shapes");
|
||||
|
||||
List<String> wave = List.of("AttachGroupPolicy", "AttachRolePolicy", "AttachUserPolicy",
|
||||
"DetachGroupPolicy", "DetachRolePolicy", "DetachUserPolicy", "GetPolicy",
|
||||
"DeletePolicy", "CreatePolicyVersion", "GetPolicyVersion", "DeletePolicyVersion",
|
||||
"SetDefaultPolicyVersion");
|
||||
for (String operation : wave) {
|
||||
assertTrue(iam.violations(operation, params()).stream()
|
||||
.allMatch(violation -> violation.contains("must not be null")),
|
||||
operation + " reported something other than its absent required members");
|
||||
assertTrue(iam.violations(operation, params()).size() >= 1,
|
||||
operation + " resolved to no required members, so its shape did not resolve");
|
||||
}
|
||||
|
||||
assertEquals(List.of("Value 'v1' at 'policyArn' failed to satisfy constraint: Member must"
|
||||
+ " have length greater than or equal to 20"),
|
||||
iam.violations("GetPolicy", params("PolicyArn", "v1")));
|
||||
}
|
||||
}
|
||||
+233
@@ -0,0 +1,233 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
/**
|
||||
* The declared constraints the managed-policy family is now read against, from the model rather
|
||||
* than from a hand-written check. What they change is the status: a value that breaks a declared
|
||||
* constraint used to reach the store and answer {@code NoSuchEntity}, where AWS's validation layer
|
||||
* runs ahead of the operation and answers {@code ValidationError}, which is the ordering #6402
|
||||
* settled for this package.
|
||||
*
|
||||
* <p>{@code AttachRolePolicy}'s {@code RoleName} is the parity case rather than a change:
|
||||
* {@code IamService.validateRoleName} already answered it in this form, from the same bounds and
|
||||
* the same pattern, and the message is unchanged by the model taking it over.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class IamRequestConstraintsIntegrationTest {
|
||||
|
||||
private static final String IAM_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20261025/us-east-1/iam/aws4_request";
|
||||
|
||||
/** 20 characters, the shortest value `arnType` accepts, and no policy that exists. */
|
||||
private static final String SHORTEST_ARN = "arn:aws:iam::1:p/abc";
|
||||
|
||||
private static final String TRUST_POLICY =
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
|
||||
+ "\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Action\":\"sts:AssumeRole\"}]}";
|
||||
|
||||
private static final String CODE = "ErrorResponse.Error.Code";
|
||||
private static final String MESSAGE = "ErrorResponse.Error.Message";
|
||||
|
||||
private static RequestSpecification iam(String action) {
|
||||
return given().header("Authorization", IAM_AUTH).formParam("Action", action);
|
||||
}
|
||||
|
||||
/** `arnType` is 20 to 2048, so a shorter ARN is the 400 rather than the lookup's 404. */
|
||||
@Test
|
||||
void anArnUnderItsMinimumIsRefusedAheadOfTheLookup() {
|
||||
iam("GetPolicy")
|
||||
.formParam("PolicyArn", "arn:aws:iam::1:p/ab")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(CODE, equalTo("ValidationError"))
|
||||
.body(MESSAGE, equalTo("1 validation error detected: Value 'arn:aws:iam::1:p/ab' at"
|
||||
+ " 'policyArn' failed to satisfy constraint: Member must have length greater"
|
||||
+ " than or equal to 20"))
|
||||
.body(not(containsString("NoSuchEntity")));
|
||||
|
||||
// The same ARN one character longer clears the constraint and reaches the lookup.
|
||||
iam("GetPolicy")
|
||||
.formParam("PolicyArn", SHORTEST_ARN)
|
||||
.when().post("/").then().statusCode(404)
|
||||
.body(CODE, equalTo("NoSuchEntity"));
|
||||
}
|
||||
|
||||
/** A required member the request leaves out is the validation layer's answer, not the store's. */
|
||||
@Test
|
||||
void aRequiredMemberLeftOutIsReportedAsNull() {
|
||||
iam("DeletePolicy").when().post("/").then().statusCode(400)
|
||||
.body(CODE, equalTo("ValidationError"))
|
||||
.body(MESSAGE, equalTo("1 validation error detected: Value null at 'policyArn' failed"
|
||||
+ " to satisfy constraint: Member must not be null"));
|
||||
}
|
||||
|
||||
/** `policyVersionIdType` publishes a pattern and no length, so a bare number breaks it. */
|
||||
@Test
|
||||
void aVersionIdOutsideItsPatternIsRefused() {
|
||||
iam("GetPolicyVersion")
|
||||
.formParam("PolicyArn", SHORTEST_ARN)
|
||||
.formParam("VersionId", "1")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(MESSAGE, equalTo("1 validation error detected: Value '1' at 'versionId' failed to"
|
||||
+ " satisfy constraint: Member must satisfy regular expression pattern:"
|
||||
+ " v[1-9][0-9]*(\\.[A-Za-z0-9-]*)?"));
|
||||
|
||||
iam("DeletePolicyVersion")
|
||||
.formParam("PolicyArn", SHORTEST_ARN)
|
||||
.formParam("VersionId", "v1")
|
||||
.when().post("/").then().statusCode(404)
|
||||
.body(CODE, equalTo("NoSuchEntity"));
|
||||
}
|
||||
|
||||
/**
|
||||
* A document sent empty is a member that is present, so what it breaks are its own
|
||||
* constraints: the minimum length of 1 and a pattern that asks for a character, reported as
|
||||
* two violations the way #6402 reports an id that breaks both of its.
|
||||
*/
|
||||
@Test
|
||||
void anEmptyPolicyDocumentBreaksItsLengthAndItsPattern() {
|
||||
iam("CreatePolicyVersion")
|
||||
.formParam("PolicyArn", SHORTEST_ARN)
|
||||
.formParam("PolicyDocument", "")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(MESSAGE, containsString("2 validation errors detected: Value '' at"
|
||||
+ " 'policyDocument' failed to satisfy constraint: Member must have length"
|
||||
+ " greater than or equal to 1"))
|
||||
.body(MESSAGE, containsString("Value '' at 'policyDocument' failed to satisfy"
|
||||
+ " constraint: Member must satisfy regular expression pattern:"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Both broken members are reported in one message, in the order the model lists them rather
|
||||
* than the order the request sent them. The second half is what makes that an assertion: the
|
||||
* request puts `PolicyArn` first and the answer still leads with `userName`, which the model
|
||||
* lists first. Whether AWS orders them that way is GEN-2's capture to confirm; what is pinned
|
||||
* here is that the order comes from the model and not from the wire.
|
||||
*/
|
||||
@Test
|
||||
void twoBrokenMembersAreReportedTogetherInTheModelsOrder() {
|
||||
String expected = "2 validation errors detected: Value '" + "a".repeat(65) + "' at"
|
||||
+ " 'userName' failed to satisfy constraint: Member must have length less than"
|
||||
+ " or equal to 64; Value 'short' at 'policyArn' failed to satisfy constraint:"
|
||||
+ " Member must have length greater than or equal to 20";
|
||||
|
||||
iam("AttachUserPolicy")
|
||||
.formParam("UserName", "a".repeat(65))
|
||||
.formParam("PolicyArn", "short")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(MESSAGE, equalTo(expected));
|
||||
|
||||
iam("AttachUserPolicy")
|
||||
.formParam("PolicyArn", "short")
|
||||
.formParam("UserName", "a".repeat(65))
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(MESSAGE, equalTo(expected));
|
||||
}
|
||||
|
||||
/**
|
||||
* The parity case: the message for a role name outside its pattern is the one
|
||||
* {@code validateRoleName} already answered, from the same bounds, so what changed is where it
|
||||
* is read rather than what it says.
|
||||
*/
|
||||
@Test
|
||||
void aRoleNameOutsideItsPatternKeepsTheMessageTheServiceAlreadyAnswered() {
|
||||
iam("AttachRolePolicy")
|
||||
.formParam("RoleName", "two words")
|
||||
.formParam("PolicyArn", SHORTEST_ARN)
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(CODE, equalTo("ValidationError"))
|
||||
.body(MESSAGE, equalTo("1 validation error detected: Value 'two words' at 'roleName'"
|
||||
+ " failed to satisfy constraint: Member must satisfy regular expression"
|
||||
+ " pattern: [\\w+=,.@-]+"));
|
||||
}
|
||||
|
||||
/**
|
||||
* An empty name is the one input where reading the model changes what this service answers
|
||||
* rather than only when it answers. {@code validateRoleName} stops at the first constraint an
|
||||
* empty value breaks, so it reports the minimum length alone; the model reports the pattern it
|
||||
* also breaks, which is how #6402 reports an id breaking two of its own, and how
|
||||
* {@code GetMFADevice} already answers an empty {@code UserName}. The operations still on the
|
||||
* hand-written check keep the single violation until a later wave takes them over, and
|
||||
* {@code CreateRole} below is what holds that boundary still.
|
||||
*
|
||||
* <p>The order of the two is this package's, matching {@code GetMFADevice}, rather than a
|
||||
* claim about AWS: the captured messages in the Identity Center guide show length before
|
||||
* pattern in one and pattern before length in another, so an exact order is stricter than any
|
||||
* source states.
|
||||
*/
|
||||
@Test
|
||||
void anEmptyRoleNameBreaksTwoConstraintsWhereTheHandWrittenCheckStopsAtOne() {
|
||||
iam("AttachRolePolicy")
|
||||
.formParam("RoleName", "")
|
||||
.formParam("PolicyArn", SHORTEST_ARN)
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(CODE, equalTo("ValidationError"))
|
||||
.body(MESSAGE, equalTo("2 validation errors detected: Value '' at 'roleName' failed to"
|
||||
+ " satisfy constraint: Member must have length greater than or equal to 1;"
|
||||
+ " Value '' at 'roleName' failed to satisfy constraint: Member must satisfy"
|
||||
+ " regular expression pattern: [\\w+=,.@-]+"));
|
||||
|
||||
// The same empty name on an operation this wave leaves alone, reported the older way.
|
||||
iam("CreateRole")
|
||||
.formParam("RoleName", "")
|
||||
.formParam("AssumeRolePolicyDocument", TRUST_POLICY)
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(MESSAGE, equalTo("1 validation error detected: Value '' at 'roleName' failed to"
|
||||
+ " satisfy constraint: Member must have length greater than or equal to 1"));
|
||||
}
|
||||
|
||||
/**
|
||||
* A name of one space is a character long, so it clears the minimum and breaks only the
|
||||
* pattern. It is worth pinning because this package elsewhere reads a blank required member as
|
||||
* absent, and the model does not: the request carries it, so it is a value.
|
||||
*/
|
||||
@Test
|
||||
void aBlankRoleNameBreaksOnlyItsPatternRatherThanReadingAsAbsent() {
|
||||
iam("DetachRolePolicy")
|
||||
.formParam("RoleName", " ")
|
||||
.formParam("PolicyArn", SHORTEST_ARN)
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(MESSAGE, equalTo("1 validation error detected: Value ' ' at 'roleName' failed to"
|
||||
+ " satisfy constraint: Member must satisfy regular expression pattern:"
|
||||
+ " [\\w+=,.@-]+"));
|
||||
}
|
||||
|
||||
/** The other end of `arnType`: 2048 characters are accepted and 2049 are not. */
|
||||
@Test
|
||||
void anArnAtItsMaximumIsAcceptedAndOneCharacterLongerIsNot() {
|
||||
iam("GetPolicy")
|
||||
.formParam("PolicyArn", "arn:aws:iam::1:p/" + "a".repeat(2031))
|
||||
.when().post("/").then().statusCode(404)
|
||||
.body(CODE, equalTo("NoSuchEntity"));
|
||||
|
||||
iam("GetPolicy")
|
||||
.formParam("PolicyArn", "arn:aws:iam::1:p/" + "a".repeat(2032))
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(CODE, equalTo("ValidationError"))
|
||||
.body(MESSAGE, containsString("at 'policyArn' failed to satisfy constraint: Member must"
|
||||
+ " have length less than or equal to 2048"))
|
||||
.body(not(containsString("NoSuchEntity")));
|
||||
}
|
||||
|
||||
/**
|
||||
* A boolean is declared and not checked: the model names no constraint for one, so a value
|
||||
* that is neither true nor false reaches the operation, which reads anything but "true" as
|
||||
* false. Pinned rather than fixed, since what AWS answers for one is unrecorded here.
|
||||
*/
|
||||
@Test
|
||||
void aBooleanOutsideTrueAndFalseIsLeftToTheOperation() {
|
||||
iam("CreatePolicyVersion")
|
||||
.formParam("PolicyArn", SHORTEST_ARN)
|
||||
.formParam("PolicyDocument", "{}")
|
||||
.formParam("SetAsDefault", "maybe")
|
||||
.when().post("/").then().statusCode(404)
|
||||
.body(CODE, equalTo("NoSuchEntity"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Vendor IAM request constraints from botocore's Apache-2.0 service model.
|
||||
|
||||
`IamRequestShapes` checks the operations listed here against the vendored file through the shared
|
||||
`QueryConstraintCheck`: required members, lengths, patterns and enum values, as the model states
|
||||
them. The list grows an operation at a time, each one taken off the hand-written checks in the
|
||||
same change, so a wave is always reviewable against the messages it replaces.
|
||||
|
||||
Run from anywhere in the repo:
|
||||
python3 tools/aws/regen_iam_shapes.py # rewrite the vendored file in place
|
||||
python3 tools/aws/regen_iam_shapes.py --check # exit 1 when the vendored file is stale
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from regen_partitions import resolve_botocore_data, strip_source
|
||||
from request_shapes import REPO_ROOT, build_constraints, load_model
|
||||
|
||||
OUTPUT = REPO_ROOT / "src/main/resources/aws/iam-request-shapes.json"
|
||||
MODEL = Path("iam/2010-05-08")
|
||||
GENERATOR = "tools/aws/regen_iam_shapes.py"
|
||||
|
||||
# The managed-policy family: every member a scalar, and no operation here checks a declared
|
||||
# constraint by hand today, so the vendored shapes add the refusals AWS answers without rewording
|
||||
# anything already answered. `AttachRolePolicy` and `DetachRolePolicy` are the exception and the
|
||||
# parity case: `IamService.validateRoleName` already answers `roleName` in Smithy's form.
|
||||
OPERATIONS = (
|
||||
"AttachGroupPolicy", "AttachRolePolicy", "AttachUserPolicy",
|
||||
"DetachGroupPolicy", "DetachRolePolicy", "DetachUserPolicy",
|
||||
"GetPolicy", "DeletePolicy",
|
||||
"CreatePolicyVersion", "GetPolicyVersion", "DeletePolicyVersion", "SetDefaultPolicyVersion",
|
||||
)
|
||||
|
||||
|
||||
def build(model: dict, provenance: str) -> dict:
|
||||
return build_constraints(model, OPERATIONS, provenance, GENERATOR)
|
||||
|
||||
|
||||
def render(doc: dict) -> str:
|
||||
return json.dumps(doc, indent=2) + "\n"
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
parser.add_argument("--check", action="store_true", help="exit 1 when the vendored file differs from a fresh generation")
|
||||
parser.add_argument("--botocore-data", type=Path, help="botocore data directory")
|
||||
parser.add_argument("--output", type=Path, default=OUTPUT, help=argparse.SUPPRESS)
|
||||
args = parser.parse_args(argv)
|
||||
data, provenance = resolve_botocore_data(args.botocore_data)
|
||||
fresh = render(build(load_model(data, MODEL), provenance))
|
||||
if args.check:
|
||||
current = args.output.read_text(encoding="utf-8") if args.output.exists() else ""
|
||||
if strip_source(current) != strip_source(fresh):
|
||||
print(f"{args.output} is stale (generated from {provenance}).", file=sys.stderr)
|
||||
return 1
|
||||
return 0
|
||||
args.output.write_text(fresh, encoding="utf-8")
|
||||
shown = args.output.relative_to(REPO_ROOT) if args.output.is_relative_to(REPO_ROOT) else args.output
|
||||
print(f"wrote {shown} from {provenance}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,88 @@
|
||||
"""Tests for regen_iam_shapes.
|
||||
|
||||
Run with: pytest tools/aws -q (or: make aws-data-test)
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
import regen_iam_shapes as r
|
||||
|
||||
|
||||
MODEL = {
|
||||
"operations": {
|
||||
"GetPolicy": {"input": {"shape": "GetPolicyRequest"}, "output": {"shape": "GetPolicyResponse"}},
|
||||
"ListPolicies": {"input": {"shape": "ListPoliciesRequest"}},
|
||||
},
|
||||
"shapes": {
|
||||
"GetPolicyRequest": {"type": "structure", "required": ["PolicyArn"], "members": {
|
||||
"PolicyArn": {"shape": "arnType"},
|
||||
}},
|
||||
"GetPolicyResponse": {"type": "structure", "members": {"Policy": {"shape": "arnType"}}},
|
||||
"ListPoliciesRequest": {"type": "structure", "members": {"MaxItems": {"shape": "maxItemsType"}}},
|
||||
"arnType": {"type": "string", "min": 20, "max": 2048},
|
||||
"maxItemsType": {"type": "integer", "min": 1, "max": 1000},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def test_keeps_only_the_listed_operations_with_their_constraints(monkeypatch):
|
||||
monkeypatch.setattr(r, "OPERATIONS", ("GetPolicy",))
|
||||
|
||||
doc = r.build(MODEL, "botocore test")
|
||||
|
||||
assert doc["_source"] == {"generator": "tools/aws/regen_iam_shapes.py", "botocore": "botocore test"}
|
||||
assert doc["operations"] == {"GetPolicy": "GetPolicyRequest"}
|
||||
assert "GetPolicyResponse" not in doc["shapes"]
|
||||
assert "maxItemsType" not in doc["shapes"]
|
||||
assert doc["shapes"]["GetPolicyRequest"] == {
|
||||
"type": "structure", "required": ["PolicyArn"], "members": {"PolicyArn": "arnType"}}
|
||||
assert doc["shapes"]["arnType"] == {"type": "string", "min": 20, "max": 2048}
|
||||
|
||||
|
||||
def test_a_misspelled_operation_is_refused_rather_than_skipped(monkeypatch):
|
||||
monkeypatch.setattr(r, "OPERATIONS", ("GetPolciy",))
|
||||
|
||||
with pytest.raises(KeyError):
|
||||
r.build(MODEL, "botocore test")
|
||||
|
||||
|
||||
def test_the_vendored_file_carries_every_listed_operation():
|
||||
doc = json.loads(r.OUTPUT.read_text(encoding="utf-8"))
|
||||
|
||||
assert sorted(doc["operations"]) == sorted(r.OPERATIONS)
|
||||
for operation, shape in doc["operations"].items():
|
||||
assert shape in doc["shapes"], operation
|
||||
|
||||
|
||||
def test_check_ignores_provenance(tmp_path, monkeypatch):
|
||||
monkeypatch.setattr(r, "OPERATIONS", ("GetPolicy",))
|
||||
output = tmp_path / "shapes.json"
|
||||
output.write_text(r.render(r.build(MODEL, "botocore 0.0.1 (local/aws/botocore)")), encoding="utf-8")
|
||||
monkeypatch.setattr(r, "resolve_botocore_data", lambda explicit: (tmp_path, "--botocore-data /elsewhere"))
|
||||
monkeypatch.setattr(r, "load_model", lambda data, model: MODEL)
|
||||
|
||||
assert r.main(["--check", "--output", str(output)]) == 0
|
||||
|
||||
|
||||
def test_check_reports_a_changed_constraint(tmp_path, monkeypatch):
|
||||
monkeypatch.setattr(r, "OPERATIONS", ("GetPolicy",))
|
||||
output = tmp_path / "shapes.json"
|
||||
changed = {**MODEL, "shapes": {**MODEL["shapes"], "arnType": {"type": "string", "min": 1, "max": 2048}}}
|
||||
output.write_text(r.render(r.build(changed, "botocore test")), encoding="utf-8")
|
||||
monkeypatch.setattr(r, "resolve_botocore_data", lambda explicit: (tmp_path, "botocore test"))
|
||||
monkeypatch.setattr(r, "load_model", lambda data, model: MODEL)
|
||||
|
||||
assert r.main(["--check", "--output", str(output)]) == 1
|
||||
|
||||
|
||||
def test_output_is_stable(monkeypatch):
|
||||
monkeypatch.setattr(r, "OPERATIONS", ("GetPolicy", "ListPolicies"))
|
||||
|
||||
first = r.render(r.build(MODEL, "botocore test"))
|
||||
second = r.render(r.build(MODEL, "botocore test"))
|
||||
|
||||
assert first == second
|
||||
assert first.endswith("\n")
|
||||
Reference in new issue
Block a user