feat(iam): dispatch GetMFADevice, AcquireRole and GetRoleTemplateVersion (#6453)

The last three operations the IAM model publishes and Floci did not dispatch.
All three can only ever answer NoSuchEntity here, and that is the argument for
dispatching them rather than against it: a 404 both their API pages model beats
the UnsupportedOperation 400 an undispatched action answers, which no SDK maps
to anything for these operations.

GetMFADevice reads a FIDO security key. The reference says of its serial number
that "For this API, we only accept FIDO security key ARNs", and nothing in the
IAM API creates such a device: CreateVirtualMFADevice is the only operation that
mints one here and it mints an mfa/ device, so there is never a FIDO key to
find. A virtual device's serial is refused the same way, deliberately, since
answering with it would hand back a device AWS would not return for the same
request. The page models NoSuchEntity and ServiceFailure and nothing else, so
there is no validation answer to prefer over the 404.

Role templates are AWS's own. The user guide has role manager using "AWS-managed
role templates" and a role created from one with iam:AcquireRole and the
template's ARN, and the model agrees from the other side: GetRoleTemplateVersion
is the only operation that names a template and there is no create, list, update
or delete, so nothing a client can call puts a template into an account. Both
operations answer NoSuchEntity naming the ARN they were handed.
RoleTemplateDisabled is the other refusal AcquireRole models and it is not this
one: it reads "the specified role template is disabled", which is a statement
about a template that exists.

That this is a reading, and what would change it, is written into
docs/services/iam.md rather than left in a commit message. The guide publishes
the template ARNs but not their bodies, so a published body would turn those
ARNs into answers and leave the 404 for every other, and RoleManager/Enabled,
role manager's own switch, is a separate question this does not read.

The declared constraints are read before the lookup, as #6402 put them
elsewhere in this package: SerialNumber to serialNumberType's 9 to 256 and its
pattern, UserName to userNameType whenever the request carries the member, a
blank one included, the member being present whatever it holds, TemplateArn to
arnType's 20 to 2048, the two minor versions to being integers, answering
InvalidInput for a value that is not a number, and ReplacementValues to the 30
entries it declares. The ARN check is #6402's, read through one helper now
rather than copied, and the member path validation messages spell is a helper
too.

role-template leaves ResourceArnBuilder.UNRESOLVED_TYPES, which is empty now.
GetRoleTemplateVersion carries the template's ARN, so the type resolves like any
other the request names, in the aws account rather than the caller's.
AcquireRole stays on *, AWS's service reference publishing no action for it.

Eleven tests. Five on GetMFADevice: the FIDO serial reported missing rather
than unsupported, a virtual device refused and still listed afterwards, the
serial's length and pattern as counted violations, a user the account does not
hold reported instead of the device, and a blank UserName read as a value
rather than as absent, which breaks both of userNameType's constraints when it
is empty and its pattern when it is whitespace. Six on the templates: both
operations' 404s, TemplateArn under its length and absent on each, a minor
version that is not a number on each, and 31 replacement values refused where
30 reach the 404. The unresolved-types test lost a guard an empty map would
fail and gained the claim that emptied it.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-24 20:22:23 -05:00
1 parent 55b060b891
commit 32663a24ad
9 files changed
+566 -16

No files matched your search

@@ -145,7 +145,8 @@ class IamActionResourcesTest {
"access-report",
// Derived from an id the request carries.
"delegation-request",
// Only on actions the handler does not dispatch.
// Named by an ARN the request carries, in the aws account rather than the
// caller's.
"role-template"),
inUse,
"AWS has changed which resource types IAM actions are authorized against; "
@@ -295,9 +295,11 @@ class ResourceArnBuilderTest {
}
/**
* A policy, MFA device, SAML provider or OIDC provider is named by an ARN the request already
* carries, so it is passed through. Rebuilding it from parts would only invent a second
* spelling of the resource the caller already named.
* A policy, MFA device, SAML provider, OIDC provider or role template is named by an ARN the
* request already carries, so it is passed through. Rebuilding it from parts would only invent
* a second spelling of the resource the caller already named, and a role template's ARN could
* not be rebuilt at all: it names the {@code aws} account rather than the caller's, and
* carries a major version after a colon.
*/
@Test
void iamPassesThroughAnArnTheRequestAlreadyCarries() {
@@ -317,6 +319,10 @@ class ResourceArnBuilderTest {
assertEquals("arn:aws:iam::000000000000:user/bob",
resourceOf(builder, "Action=SimulatePrincipalPolicy"
+ "&PolicySourceArn=arn%3Aaws%3Aiam%3A%3A000000000000%3Auser%2Fbob"));
assertEquals("arn:aws:iam::aws:role-template/ec2.amazonaws.com/AmazonEC2RoleforSSM:1",
resourceOf(builder, "Action=GetRoleTemplateVersion"
+ "&TemplateArn=arn%3Aaws%3Aiam%3A%3Aaws%3Arole-template%2F"
+ "ec2.amazonaws.com%2FAmazonEC2RoleforSSM%3A1"));
}
/**
@@ -0,0 +1,164 @@
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 two role template operations over the wire. Both answer {@code NoSuchEntity} for every ARN,
* and that is the faithful answer rather than a stub: role templates are AWS's own, the user guide
* having role manager use "AWS-managed role templates", and the model publishes no operation that
* creates, lists, updates or deletes one, so no account holds a template to read or to acquire a
* role from.
*
* <p>What these hold is that the 404 is reached the way AWS reaches it. The declared constraints
* are read first, so a malformed request is the 400 the validation ahead of the operation answers,
* and the refusal names the template rather than something else. Dispatching them at all is what
* the tests are really for: before this, both answered {@code UnsupportedOperation} 400, which no
* SDK models for either operation, where the 404 is modelled by both.
*
* <p>If AWS ever publishes the template bodies the guide's ARN directory names, these answers are
* what would change: the ARNs in that directory would have a version to return, and only those.
*/
@QuarkusTest
class RoleTemplateIntegrationTest {
private static final String IAM_AUTH =
"AWS4-HMAC-SHA256 Credential=test/20261024/us-east-1/iam/aws4_request";
/** One of the template ARNs the user guide's directory publishes, as a client would send it. */
private static final String TEMPLATE_ARN =
"arn:aws:iam::aws:role-template/ec2.amazonaws.com/AmazonEC2RoleforSSM:1";
private static RequestSpecification iam(String action) {
return given().header("Authorization", IAM_AUTH).formParam("Action", action);
}
@Test
void readingATemplateVersionIsReportedMissingRatherThanUnsupported() {
iam("GetRoleTemplateVersion")
.formParam("TemplateArn", TEMPLATE_ARN)
.when().post("/").then()
.statusCode(404)
.body("ErrorResponse.Error.Code", equalTo("NoSuchEntity"))
.body("ErrorResponse.Error.Message",
equalTo("The role template with ARN " + TEMPLATE_ARN + " cannot be found."))
.body(not(containsString("UnsupportedOperation")));
}
@Test
void acquiringARoleFromATemplateIsReportedMissingRatherThanUnsupported() {
// No state assertion to make here: the role's name comes from the template, so a request
// that is refused for naming no template names nothing to look up afterwards either.
iam("AcquireRole")
.formParam("TemplateArn", TEMPLATE_ARN)
.when().post("/").then()
.statusCode(404)
.body("ErrorResponse.Error.Code", equalTo("NoSuchEntity"))
.body("ErrorResponse.Error.Message",
equalTo("The role template with ARN " + TEMPLATE_ARN + " cannot be found."))
.body(not(containsString("UnsupportedOperation")));
}
/**
* {@code TemplateArn} is {@code arnType}, min 20, on both operations, and a value under it is
* refused on its shape before anything is looked for. The member is required, so an absent one
* is the null violation rather than the 404.
*/
@Test
void aTemplateArnOutsideItsDeclaredLengthIsAValidationError() {
for (String action : new String[] {"GetRoleTemplateVersion", "AcquireRole"}) {
iam(action).formParam("TemplateArn", "arn:aws:iam::aws")
.when().post("/").then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo("1 validation error detected: Value "
+ "at 'templateArn' failed to satisfy constraint: Member must have length "
+ "greater than or equal to 20"))
.body(not(containsString("NoSuchEntity")));
iam(action)
.when().post("/").then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo("1 validation error detected: Value "
+ "null at 'templateArn' failed to satisfy constraint: Member must not be "
+ "null"))
.body(not(containsString("NoSuchEntity")));
}
}
/**
* Each operation names its minor version differently, {@code MinorVersion} on the read and
* {@code TemplateMinorVersion} on the acquire, and both are declared integers with no bounds,
* so a value that is not a number is {@code InvalidInput} as it is for {@code MaxItems}.
*/
@Test
void aMinorVersionThatIsNotANumberIsRefusedBeforeTheTemplateIsLookedFor() {
iam("GetRoleTemplateVersion")
.formParam("TemplateArn", TEMPLATE_ARN)
.formParam("MinorVersion", "latest")
.when().post("/").then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("InvalidInput"))
.body("ErrorResponse.Error.Message",
equalTo("The value latest at 'minorVersion' is not a number."));
iam("AcquireRole")
.formParam("TemplateArn", TEMPLATE_ARN)
.formParam("TemplateMinorVersion", "latest")
.when().post("/").then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("InvalidInput"))
.body("ErrorResponse.Error.Message",
equalTo("The value latest at 'templateMinorVersion' is not a number."));
// A number is carried through to the 404, so the check is on the value and not on the
// member being present.
iam("GetRoleTemplateVersion")
.formParam("TemplateArn", TEMPLATE_ARN)
.formParam("MinorVersion", "3")
.when().post("/").then()
.statusCode(404)
.body("ErrorResponse.Error.Code", equalTo("NoSuchEntity"));
}
/**
* {@code ReplacementValues} is declared with at most 30 entries. The Query form spells a map
* as {@code ReplacementValues.entry.N.key}, and 31 of them is a length violation ahead of the
* lookup rather than a 404 that ignored them.
*/
@Test
void moreReplacementValuesThanTheDeclaredMaximumIsAValidationError() {
RequestSpecification request = iam("AcquireRole").formParam("TemplateArn", TEMPLATE_ARN);
for (int entry = 1; entry <= 31; entry++) {
request.formParam("ReplacementValues.entry." + entry + ".key", "Parameter" + entry)
.formParam("ReplacementValues.entry." + entry + ".value.Values.member.1", "v");
}
request.when().post("/").then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo("1 validation error detected: Value at "
+ "'replacementValues' failed to satisfy constraint: Member must have length "
+ "less than or equal to 30"));
}
@Test
void thirtyReplacementValuesAreCarriedThroughToTheMissingTemplate() {
RequestSpecification request = iam("AcquireRole").formParam("TemplateArn", TEMPLATE_ARN);
for (int entry = 1; entry <= 30; entry++) {
request.formParam("ReplacementValues.entry." + entry + ".key", "Parameter" + entry)
.formParam("ReplacementValues.entry." + entry + ".value.Values.member.1", "v");
}
request.when().post("/").then()
.statusCode(404)
.body("ErrorResponse.Error.Code", equalTo("NoSuchEntity"));
}
}
@@ -22,6 +22,11 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
* <p>It matters because the failure is silent and fail-open. Dispatching such an action without
* teaching the resolver its type leaves the action evaluated against {@code *}, so a policy
* statement naming the resource never applies, which is the gap this builder exists to close.
*
* <p>The map is empty since {@code GetRoleTemplateVersion} was dispatched, so the first test
* holds nothing today and the second holds what emptied it: the type that used to be declared is
* resolved from the ARN the request carries. That one is the regression worth a test, since an
* empty map asserts nothing and dropping the parameter would put the action back on {@code *}.
*/
@QuarkusTest
class UnresolvedResourceTypesIntegrationTest {
@@ -31,8 +36,6 @@ class UnresolvedResourceTypesIntegrationTest {
@Test
void everyTypeDeclaredUnresolvedIsCarriedOnlyByActionsNobodyDispatches() {
assertFalse(ResourceArnBuilder.UNRESOLVED_TYPES.isEmpty(),
"nothing is declared unresolved any more, so this test has nothing to hold");
for (Map.Entry<String, String> declared : ResourceArnBuilder.UNRESOLVED_TYPES.entrySet()) {
Set<String> actions = actionsCarrying(declared.getKey());
assertFalse(actions.isEmpty(), declared.getKey()
@@ -55,6 +58,33 @@ class UnresolvedResourceTypesIntegrationTest {
}
}
/**
* {@code role-template} was the last declared-unresolved type, and dispatching
* {@code GetRoleTemplateVersion} is what took it out: the action carries the template's ARN,
* so the type resolves like any other the request names. Held over the wire as well as in the
* table, because the action being dispatched is half of the claim.
*/
@Test
void theTypeThatWasDeclaredUnresolvedIsResolvedFromTheArnTheRequestCarries() {
assertEquals("TemplateArn",
ResourceArnBuilder.IAM_ARN_PARAMETERS.get("GetRoleTemplateVersion"),
"GetRoleTemplateVersion no longer resolves its template from the ARN it carries, "
+ "so role-template is back to * unless it is declared unresolved again");
assertEquals(List.of("role-template"),
IamActionResources.typesOf("GetRoleTemplateVersion"),
"AWS has changed which resource type GetRoleTemplateVersion is authorized "
+ "against, so the parameter above may name the wrong thing");
Response response = given().header("Authorization", IAM_AUTH)
.formParam("Action", "GetRoleTemplateVersion")
.formParam("TemplateArn", "arn:aws:iam::aws:role-template/ec2.amazonaws.com/X:1")
.when().post("/");
assertEquals(404, response.statusCode(),
"GetRoleTemplateVersion is not dispatched any more, so role-template belongs in "
+ "UNRESOLVED_TYPES again rather than being resolved here");
assertEquals("NoSuchEntity", response.xmlPath().getString("ErrorResponse.Error.Code"));
}
private static String reason(Map.Entry<String, String> declared, String action) {
return action + " is dispatched now, so '" + declared.getKey() + "' is no longer a type "
+ "only undispatched actions carry (" + declared.getValue() + "). Resolve it in "
@@ -14,6 +14,7 @@ import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -723,4 +724,127 @@ class VirtualMfaIntegrationTest {
.statusCode(409)
.body("ErrorResponse.Error.Code", equalTo("LimitExceeded"));
}
/**
* {@code GetMFADevice} reads a FIDO security key, which nothing in the IAM API creates, so the
* answer is always {@code NoSuchEntity}. That is still the answer AWS's own SDKs model for the
* operation, where refusing it outright is not, which is the whole reason it is dispatched.
*/
@Test
void aFidoKeySerialIsReportedMissingRatherThanUnsupported() {
iam("GetMFADevice")
.formParam("SerialNumber", "arn:aws:iam::000000000000:u2f/root/testFidoKey-R1234")
.when().post("/").then()
.statusCode(404)
.body("ErrorResponse.Error.Code", equalTo("NoSuchEntity"))
.body("ErrorResponse.Error.Message", equalTo("The MFA device with serial number "
+ "arn:aws:iam::000000000000:u2f/root/testFidoKey-R1234 cannot be found."));
}
/**
* A virtual device is refused the same way, deliberately. The reference says of this
* operation's serial number that "we only accept FIDO security key ARNs", and the device
* created here is an {@code mfa/} one, so answering with it would hand back a device AWS would
* not return for the same request.
*/
@Test
void aVirtualDeviceIsNotReadableAsAFidoKey() {
Device device = createDevice("mfa-fido-read-" + suffix());
iam("GetMFADevice")
.formParam("SerialNumber", device.serialNumber())
.when().post("/").then()
.statusCode(404)
.body("ErrorResponse.Error.Code", equalTo("NoSuchEntity"));
// And it is still the user's device, listed as before: the refusal reads nothing out and
// changes nothing.
String userName = createUser("mfa-fido-read-user-" + suffix());
enable(userName, device);
iam("ListMFADevices").formParam("UserName", userName)
.when().post("/").then()
.statusCode(200)
.body("ListMFADevicesResponse.ListMFADevicesResult.MFADevices.member.SerialNumber",
equalTo(device.serialNumber()));
}
/**
* The declared shapes are read before the device is looked for, as the validation ahead of the
* operation does. {@code serialNumberType} is 9 to 256 with a pattern, so a short serial and
* one carrying a character outside it are each a counted violation, and a value breaking both
* is two.
*/
@Test
void aSerialOutsideItsDeclaredShapeIsAValidationError() {
iam("GetMFADevice").formParam("SerialNumber", "u2f/root")
.when().post("/").then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo("1 validation error detected: Value at "
+ "'serialNumber' failed to satisfy constraint: Member must have length "
+ "greater than or equal to 9"));
iam("GetMFADevice").formParam("SerialNumber", "u2f root key!")
.when().post("/").then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo("1 validation error detected: Value at "
+ "'serialNumber' failed to satisfy constraint: Member must satisfy regular "
+ "expression pattern: [\\w+=/:,.@-]+"));
iam("GetMFADevice").formParam("SerialNumber", "u2f key!")
.when().post("/").then()
.statusCode(400)
.body("ErrorResponse.Error.Message", startsWith("2 validation errors detected"));
}
/**
* A {@code UserName} the request carries is read as a value even when it is blank, because the
* member is present whatever it holds: an empty name breaks both of {@code userNameType}'s
* constraints and a whitespace one breaks its pattern. Reading a blank name as no name would
* answer for the device under a name AWS would never accept, and would skip the user
* resolution the request asked for.
*/
@Test
void aBlankUserNameIsReadAsAValueRatherThanAsAbsent() {
iam("GetMFADevice")
.formParam("SerialNumber", "arn:aws:iam::000000000000:u2f/root/testFidoKey-R1234")
.formParam("UserName", "")
.when().post("/").then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo("2 validation errors detected: Value at "
+ "'userName' failed to satisfy constraint: Member must have length greater "
+ "than or equal to 1; Value at 'userName' failed to satisfy constraint: "
+ "Member must satisfy regular expression pattern: [\\w+=,.@-]+"));
iam("GetMFADevice")
.formParam("SerialNumber", "arn:aws:iam::000000000000:u2f/root/testFidoKey-R1234")
.formParam("UserName", " ")
.when().post("/").then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo("1 validation error detected: Value at "
+ "'userName' failed to satisfy constraint: Member must satisfy regular "
+ "expression pattern: [\\w+=,.@-]+"));
}
/**
* The user is resolved when the request names one, because an absent user is the same
* {@code NoSuchEntity} under a message of its own, and the user this operation reads a device
* for is the one half of the request that can exist.
*/
@Test
void aUserTheAccountDoesNotHoldIsReportedBeforeTheDevice() {
String missing = "mfa-fido-nobody-" + suffix();
iam("GetMFADevice")
.formParam("SerialNumber", "arn:aws:iam::000000000000:u2f/root/testFidoKey-R1234")
.formParam("UserName", missing)
.when().post("/").then()
.statusCode(404)
.body("ErrorResponse.Error.Code", equalTo("NoSuchEntity"))
.body("ErrorResponse.Error.Message",
equalTo("The user with name " + missing + " cannot be found."));
}
}