feat(iam): read the MFA device a session is minted behind and publish its keys (#8602)

Enabling an MFA device recorded state and changed nothing.
aws:MultiFactorAuthPresent and aws:MultiFactorAuthAge reached no request, so a
policy conditioned on either could not behave as it does on AWS, and
GetSessionToken and AssumeRole accepted SerialNumber and TokenCode and ignored
both, so no session could satisfy such a condition in the first place.

Both keys are published now from the credentials a request was signed with,
which is where the guide puts their availability: a long-term key carries
neither, and a session carries the first one, true only where a device was
used. A federated-user session carries neither, the guide putting federated
identities with the long-term keys. They are written by the one writer that
already says who is calling, so the enforcement filter, the per-resource and
per-item contexts, trust policies and the paths that evaluate a policy
themselves all see them at once.

The two STS operations hold a presented code to the seed of the device its
serial names, require that device to be enabled for the signing identity, and
spend the step it matched, since RFC 6238 has a verifier refuse the second
attempt of a code for the same time step. A key IAM holds nowhere has its claim
taken as made: there is no identity behind it whose devices could be checked,
and this emulator already reads such a key as acting for itself. A claim that
fails is refused with AccessDenied, the outcome AWS documents.

How far the authentication reaches is AWS's own line. GetSessionToken
credentials "include MFA information", so that session is MFA-authenticated on
its own requests. AssumeRole credentials "do not include MFA information in the
context", and "once the user has assumed the role, no further MFA checks are
made", so the device decides the assume and stops there. The device a call
presents belongs to that request, so both the trust policy and the caller's own
permission to make the call see it; the claim is read once per request and held
on it, a code being spendable only once.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-16 15:00:51 -04:00
1 parent b2def4f421
commit 9de6cd74b8
14 files changed
+897 -73

No files matched your search

@@ -499,9 +499,12 @@ class IamServiceTest {
// The principal keys ride along from the same lookup: a role session is typed as an
// assumed role, its aws:userid is the stored role-id:session-name, and it has no
// aws:username, which the guide lists as not present for a role.
// It carries what its credentials say about MFA as well, which for a session minted
// without a device is the key present and false.
assertEquals(new IamService.CallerArns("arn:aws:sts::123456789012:assumed-role/TestRole/s",
"arn:aws:iam::123456789012:role/team/TestRole",
IamService.CallerArns.TYPE_ASSUMED_ROLE, "AROATESTROLEID:s", null),
IamService.CallerArns.TYPE_ASSUMED_ROLE, "AROATESTROLEID:s", null)
.withSessionMfa(new IamService.CallerArns.SessionMfa(false, 0)),
service.resolveCallerArns("ASIALIVESESSION").orElseThrow());
// An expired session answers for neither, never for one and not the other.
assertTrue(service.resolveCallerArns("ASIAEXPIREDSESSION").isEmpty());
@@ -743,7 +746,8 @@ class IamServiceTest {
// Acting as the user means carrying the user's principal keys, aws:username included:
// the guide gives one to an IAM user and to no other principal.
assertEquals(new IamService.CallerArns(alice.getArn(), alice.getArn(),
IamService.CallerArns.TYPE_USER, alice.getUserId(), "alice"),
IamService.CallerArns.TYPE_USER, alice.getUserId(), "alice")
.withSessionMfa(new IamService.CallerArns.SessionMfa(false, 0)),
service.resolveCallerArns("ASIASESSIONTOKEN").orElseThrow());
assertEquals(alice.getUserId(), service.resolveCallerUserId("ASIASESSIONTOKEN", "token").orElseThrow());
}
@@ -762,7 +766,8 @@ class IamServiceTest {
// present for the account root.
assertEquals(new IamService.CallerArns("arn:aws:iam::000000000000:root",
"arn:aws:iam::000000000000:root", IamService.CallerArns.TYPE_ACCOUNT,
"000000000000", null),
"000000000000", null)
.withSessionMfa(new IamService.CallerArns.SessionMfa(false, 0)),
service.resolveCallerArns("ASIAROOTSESSION").orElseThrow());
}
@@ -870,7 +875,9 @@ class IamServiceTest {
assertFederatedUserLimits(withPolicy.identityPolicies().getLast());
assertEquals(ALL_S3, withPolicy.sessionPolicyDocument());
// The same aws:userid both ways, since one derivation now serves both readers, and no
// aws:username: the guide lists it as not present for a federated user.
// aws:username: the guide lists it as not present for a federated user. No MFA state
// either, the same guide putting federated identities with the long-term keys, so this
// session publishes neither MFA key where every other session publishes the first.
assertEquals(new IamService.CallerArns(federatedArn, federatedArn,
IamService.CallerArns.TYPE_FEDERATED_USER, "000000000000:Bob", null),
service.resolveCallerArns("ASIAWITHPOLICY").orElseThrow());
@@ -0,0 +1,345 @@
package io.floci.aws.services.iam;
import io.floci.aws.core.common.Totp;
import io.floci.aws.testing.S3IamEnforcementProfile;
import io.floci.aws.testutil.S3RequestSigner;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.Base64;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
/**
* The two MFA condition keys, and the device a session is minted behind.
*
* <p>{@code aws:MultiFactorAuthPresent} says "whether multi-factor authentication (MFA) was used to
* validate the temporary security credentials that made the request", and both it and
* {@code aws:MultiFactorAuthAge} are "included in the request context only when the principal uses
* temporary credentials to make the request". A session minted without a device still carries the
* first one: "In these cases, the aws:MultiFactorAuthPresent key is present in the request and set
* to a value of false." A long-term key carries neither, which is why the guide recommends
* {@code BoolIfExists} for a deny and warns that plain {@code Bool} on false "is not a reliable way
* to check whether a request is authenticated using MFA".
*
* <p>The device reaches a session through {@code SerialNumber} and {@code TokenCode} on
* {@code GetSessionToken} and {@code AssumeRole}. A trust policy may require it, AWS's own example
* being {@code "Bool": {"aws:MultiFactorAuthPresent": true}}, and then "if the caller does not
* include valid MFA information, the request to assume the role is denied".
*
* <p>The two operations do not carry the authentication equally far, and AWS draws the line
* itself. "Because temporary credentials returned by GetSessionToken include MFA information, you
* can check for MFA in individual API operations made by the credentials", while "the temporary
* credentials returned by AssumeRole do not include MFA information in the context, so you cannot
* check individual API operations for MFA": the device decides the assume and stops there, since
* "once the user has assumed the role, no further MFA checks are made".
*/
@QuarkusTest
@TestProfile(S3IamEnforcementProfile.class)
class StsMfaEnforcementIntegrationTest {
private static final String ACCOUNT = "000000000000";
private static final String CODE = "ErrorResponse.Error.Code";
/** Allows the listing only where the request was authenticated with a device. */
private static final String LIST_WITH_MFA = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":["iam:ListUsers","sts:GetSessionToken","sts:AssumeRole"],
"Resource":"*","Condition":{"Bool":{"aws:MultiFactorAuthPresent":"true"}}},
{"Effect":"Allow","Action":["sts:GetSessionToken","sts:AssumeRole"],"Resource":"*"}]}""";
/** The guide's recommended shape: an allow, and a deny for anything not MFA-authenticated. */
private static final String LIST_DENIED_WITHOUT_MFA = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":["iam:ListUsers","sts:GetSessionToken"],"Resource":"*"},
{"Effect":"Deny","Action":"iam:ListUsers","Resource":"*",
"Condition":{"BoolIfExists":{"aws:MultiFactorAuthPresent":"false"}}}]}""";
/** Allows the assume only where the request that makes it was authenticated with a device. */
private static final String ASSUME_WITH_MFA = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"sts:AssumeRole","Resource":"*",
"Condition":{"Bool":{"aws:MultiFactorAuthPresent":"true"}}}]}""";
/** Allows the listing only while the authentication is recent. */
private static final String LIST_WHILE_FRESH = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":["iam:ListUsers","sts:GetSessionToken"],"Resource":"*",
"Condition":{"NumericLessThan":{"aws:MultiFactorAuthAge":"3600"}}},
{"Effect":"Allow","Action":"sts:GetSessionToken","Resource":"*"}]}""";
@Test
void aSessionMintedBehindADeviceIsTheOneThatCarriesThePresentKeyAsTrue() {
String userName = unique("mfa-present");
S3RequestSigner user = userWithPolicy(userName, LIST_WITH_MFA);
Device device = enabledDeviceFor(userName);
// The same user, the same policy, two sessions: the key is the only difference between them.
listUsers(sessionWith(user, device), 200);
listUsers(sessionWith(user, null), 403);
// And the long-term key itself, which publishes no key at all, so a plain Bool cannot match.
listUsers(user, 403);
}
@Test
void theDenyTheGuideRecommendsRefusesALongTermKeyAndAnMfaLessSession() {
String userName = unique("mfa-ifexists");
S3RequestSigner user = userWithPolicy(userName, LIST_DENIED_WITHOUT_MFA);
Device device = enabledDeviceFor(userName);
// "This combination of Deny, BoolIfExists, and false denies requests that are not
// authenticated using MFA... It also denies requests that are made using long-term
// credentials, such as AWS CLI or AWS API operations made using access keys."
listUsers(user, 403);
listUsers(sessionWith(user, null), 403);
listUsers(sessionWith(user, device), 200);
}
@Test
void theAgeIsPublishedOnlyWhereThereIsAnAuthenticationToMeasure() {
String userName = unique("mfa-age");
S3RequestSigner user = userWithPolicy(userName, LIST_WHILE_FRESH);
Device device = enabledDeviceFor(userName);
// The device was just used, so the age is inside the hour the policy allows.
listUsers(sessionWith(user, device), 200);
// A session that used no device publishes no age, and a Numeric comparison on an absent
// key does not match, so the same statement cannot allow it.
listUsers(sessionWith(user, null), 403);
}
@Test
void aCodeTheDeviceDidNotProduceAuthenticatesNothing() {
String userName = unique("mfa-code");
S3RequestSigner user = userWithPolicy(userName, LIST_WITH_MFA);
Device device = enabledDeviceFor(userName);
// "An incorrect MFA code causes the API to return an access denied error."
getSessionToken(user, device.serialNumber(), "000000").statusCode(403)
.body(CODE, equalTo("AccessDenied"));
// A device with no code proves nothing, and a code naming no device names nothing.
getSessionToken(user, device.serialNumber(), null).statusCode(403);
getSessionToken(user, null, device.codeNow()).statusCode(403);
// A device enabled for somebody else is not this caller's to present.
Device other = enabledDeviceFor(unique("mfa-other"));
getSessionToken(user, other.serialNumber(), other.codeNow()).statusCode(403);
}
@Test
void aTrustPolicyThatTestsForMfaTakesTheDeviceTheAssumePresents() {
String userName = unique("mfa-trust");
S3RequestSigner user = userWithPolicy(userName, LIST_WITH_MFA);
Device device = enabledDeviceFor(userName);
String roleName = unique("mfa-role");
createRoleRequiringMfa(roleName, userName);
// "If the caller does not include valid MFA information, the request to assume the role is
// denied", and a long-term key on its own carries none.
assumeRole(user, roleName, null, null).statusCode(403);
assumeRole(user, roleName, device.serialNumber(), device.codeNow()).statusCode(200);
}
@Test
void aRoleSessionAssumedWithADeviceStillCarriesNoAuthenticationOfItsOwn() {
String userName = unique("mfa-chain-user");
S3RequestSigner user = userWithPolicy(userName, LIST_WITH_MFA);
Device device = enabledDeviceFor(userName);
String roleName = unique("mfa-chain-role");
createRoleRequiringMfa(roleName, userName);
// The device got the caller through the trust policy, and that is as far as it goes: "the
// temporary credentials returned by AssumeRole do not include MFA information in the
// context, so you cannot check individual API operations for MFA". The role's own policy is
// the same MFA condition, and the session it minted cannot satisfy it.
S3RequestSigner roleSession = assumedSession(user, roleName, device);
listUsers(roleSession, 403);
}
@Test
void aCodeThatMintedASessionMintsNoSecondOne() {
String userName = unique("mfa-replay");
S3RequestSigner user = userWithPolicy(userName, LIST_WITH_MFA);
Device device = enabledDeviceFor(userName);
String code = device.codeNow();
getSessionToken(user, device.serialNumber(), code).statusCode(200);
// The code is still inside the window it was generated for, and spent: RFC 6238 has a
// verifier "MUST NOT accept the second attempt of the OTP generated for the same time
// step", so one code is one authentication.
getSessionToken(user, device.serialNumber(), code).statusCode(403)
.body(CODE, equalTo("AccessDenied"));
}
@Test
void thePolicyForTheAssumeItselfSeesTheDeviceTheCallPresents() {
String userName = unique("mfa-identity");
S3RequestSigner user = userWithPolicy(userName, ASSUME_WITH_MFA);
Device device = enabledDeviceFor(userName);
String roleName = unique("mfa-identity-role");
createRoleTrustingUser(roleName, userName);
// Nothing in the trust policy asks for MFA here: what does is the caller's own permission
// to call the operation, which is evaluated before the operation runs. AWS denies such a
// request when "users have not been MFA authenticated", and allows it on the device the
// call carries.
assumeRole(user, roleName, device.serialNumber(), device.codeNow()).statusCode(200);
assumeRole(user, roleName, null, null).statusCode(403);
}
@Test
void aSessionMintedBehindADeviceAssumesARoleThatRequiresOneWithoutPresentingItAgain() {
String userName = unique("mfa-carry");
S3RequestSigner user = userWithPolicy(userName, LIST_WITH_MFA);
Device device = enabledDeviceFor(userName);
String roleName = unique("mfa-carry-role");
createRoleRequiringMfa(roleName, userName);
// The session already authenticated with the device, so the assume it makes is an
// MFA-authenticated request though it names no device of its own.
assumeRole(sessionWith(user, device), roleName, null, null).statusCode(200);
// The session minted without one is refused by the same trust policy.
assumeRole(sessionWith(user, null), roleName, null, null).statusCode(403);
}
/** A virtual device and its seed, which is what produces the codes a client sends. */
private record Device(String serialNumber, String seed) {
String codeNow() {
return Totp.codeAt(seed, Totp.stepAt(Instant.now()));
}
}
private static void listUsers(S3RequestSigner caller, int status) {
given().filter(caller).formParam("Action", "ListUsers").formParam("Version", "2010-05-08")
.when().post("/").then().statusCode(status);
}
private static ValidatableResponse getSessionToken(S3RequestSigner caller, String serialNumber,
String tokenCode) {
RequestSpecification request = given().filter(caller).formParam("Action", "GetSessionToken");
if (serialNumber != null) {
request = request.formParam("SerialNumber", serialNumber);
}
if (tokenCode != null) {
request = request.formParam("TokenCode", tokenCode);
}
return request.when().post("/").then();
}
/** The caller's own session, minted behind {@code device} when one is given. */
private static S3RequestSigner sessionWith(S3RequestSigner caller, Device device) {
ExtractableResponse<Response> issued = (device == null
? getSessionToken(caller, null, null)
: getSessionToken(caller, device.serialNumber(), device.codeNow()))
.statusCode(200).extract();
String base = "GetSessionTokenResponse.GetSessionTokenResult.Credentials.";
return S3RequestSigner.signedAs(issued.path(base + "AccessKeyId"),
issued.path(base + "SecretAccessKey"), issued.path(base + "SessionToken"));
}
private static ValidatableResponse assumeRole(S3RequestSigner caller, String roleName,
String serialNumber, String tokenCode) {
RequestSpecification request = given().filter(caller).formParam("Action", "AssumeRole")
.formParam("RoleArn", "arn:aws:iam::" + ACCOUNT + ":role/" + roleName)
.formParam("RoleSessionName", "mfa-session");
if (serialNumber != null) {
request = request.formParam("SerialNumber", serialNumber);
}
if (tokenCode != null) {
request = request.formParam("TokenCode", tokenCode);
}
return request.when().post("/").then();
}
private static S3RequestSigner assumedSession(S3RequestSigner caller, String roleName, Device device) {
ExtractableResponse<Response> issued =
assumeRole(caller, roleName, device.serialNumber(), device.codeNow())
.statusCode(200).extract();
String base = "AssumeRoleResponse.AssumeRoleResult.Credentials.";
return S3RequestSigner.signedAs(issued.path(base + "AccessKeyId"),
issued.path(base + "SecretAccessKey"), issued.path(base + "SessionToken"));
}
/** A role only an MFA-authenticated {@code userName} may assume, holding the same condition. */
private static void createRoleRequiringMfa(String roleName, String userName) {
iam("CreateRole", "RoleName", roleName, "AssumeRolePolicyDocument", """
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
"Principal":{"AWS":"arn:aws:iam::%s:user/%s"},"Action":"sts:AssumeRole",
"Condition":{"Bool":{"aws:MultiFactorAuthPresent":"true"}}}]}"""
.formatted(ACCOUNT, userName));
iam("PutRolePolicy", "RoleName", roleName, "PolicyName", "inline",
"PolicyDocument", LIST_WITH_MFA);
}
/** A role {@code userName} may assume, with nothing in the trust policy about MFA. */
private static void createRoleTrustingUser(String roleName, String userName) {
iam("CreateRole", "RoleName", roleName, "AssumeRolePolicyDocument", """
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
"Principal":{"AWS":"arn:aws:iam::%s:user/%s"},"Action":"sts:AssumeRole"}]}"""
.formatted(ACCOUNT, userName));
}
/** A virtual device created and enabled for a fresh user, the way a client sets one up. */
private static Device enabledDeviceFor(String userName) {
if (!userExists(userName)) {
iam("CreateUser", "UserName", userName);
}
String base = "CreateVirtualMFADeviceResponse.CreateVirtualMFADeviceResult.VirtualMFADevice.";
ExtractableResponse<Response> created = given().header("Authorization", auth(ACCOUNT, "iam"))
.formParam("Action", "CreateVirtualMFADevice")
.formParam("VirtualMFADeviceName", userName + "-device")
.when().post("/").then().statusCode(200).extract();
String seed = new String(Base64.getDecoder().decode(
created.path(base + "Base32StringSeed").toString().trim()), StandardCharsets.UTF_8);
String serialNumber = created.path(base + "SerialNumber");
long step = Totp.stepAt(Instant.now());
iam("EnableMFADevice", "UserName", userName, "SerialNumber", serialNumber,
"AuthenticationCode1", Totp.codeAt(seed, step - 1),
"AuthenticationCode2", Totp.codeAt(seed, step));
return new Device(serialNumber, seed);
}
private static boolean userExists(String userName) {
return given().header("Authorization", auth(ACCOUNT, "iam"))
.formParam("Action", "GetUser").formParam("UserName", userName)
.when().post("/").then().extract().statusCode() == 200;
}
private static S3RequestSigner userWithPolicy(String userName, String policy) {
iam("CreateUser", "UserName", userName);
iam("PutUserPolicy", "UserName", userName, "PolicyName", "inline", "PolicyDocument", policy);
ExtractableResponse<Response> key = given().header("Authorization", auth(ACCOUNT, "iam"))
.formParam("Action", "CreateAccessKey").formParam("UserName", userName)
.when().post("/").then().statusCode(200).extract();
String base = "CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.";
return S3RequestSigner.signedAs(key.path(base + "AccessKeyId"), key.path(base + "SecretAccessKey"));
}
private static void iam(String action, String... params) {
RequestSpecification request = given().header("Authorization", auth(ACCOUNT, "iam"))
.formParam("Action", action);
for (int i = 0; i < params.length; i += 2) {
request = request.formParam(params[i], params[i + 1]);
}
request.when().post("/").then().statusCode(200);
}
private static String unique(String prefix) {
return prefix + "-" + UUID.randomUUID().toString().substring(0, 8);
}
private static String auth(String accessKeyId, String service) {
return "AWS4-HMAC-SHA256 Credential=" + accessKeyId + "/20261116/us-east-1/" + service
+ "/aws4_request, SignedHeaders=host, Signature=abc";
}
}
@@ -4,6 +4,7 @@ import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.AccountResolver;
import io.floci.aws.core.common.OidcIssuerKeyLookup;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.RequestContext;
import io.floci.aws.core.common.WebIdentityToken;
import io.floci.aws.core.common.WebIdentityTokenVerifier;
import io.floci.aws.services.iam.model.DelegationRequest;
@@ -119,6 +120,7 @@ class StsQueryHandlerTest {
delegation,
mock(CentralRootAccess.class),
tokenIssuer,
new RequestContext(),
clock);
}
@@ -969,7 +971,7 @@ class StsQueryHandlerTest {
new RegionResolver(REGION, "000000000000"), config, mock(AssumeRolePolicyEvaluator.class),
trust, verifier, keys, mock(SAMLProviderService.class), mock(SAMLTrustPolicyEvaluator.class),
mock(DelegationRequestService.class), mock(CentralRootAccess.class),
mock(WebIdentityTokenIssuer.class));
mock(WebIdentityTokenIssuer.class), new RequestContext());
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
params.putSingle("RoleArn", requestedRoleArn);
params.putSingle("RoleSessionName", "app");
@@ -49,10 +49,10 @@ class StsRequestConstraintsIntegrationTest {
}
/**
* {@code serialNumberType} and {@code tokenCodeType} are declared and were read by nothing, so
* a request naming either is now held to it. Both are reported together, in the order the model
* lists them, and a well-formed pair is still accepted and still unused: the MFA check itself
* is not built.
* {@code serialNumberType} and {@code tokenCodeType} are declared, so a request naming either
* is held to it. Both are reported together, in the order the model lists them, and the shapes
* are answered before any device is resolved: a pair that breaks them is a
* {@code ValidationError} rather than the refusal of a device that could not have existed.
*/
@Test
void theMfaMembersAreReadWhereNothingReadThemBefore() {
@@ -81,6 +81,11 @@ class StsRequestConstraintsIntegrationTest {
.body(MESSAGE, containsString("Value 'abcdef' at 'tokenCode' failed to satisfy"
+ " constraint: Member must satisfy regular expression pattern: [\\d]*"));
// A well-formed pair clears the declared shapes and reaches the device. This key is one
// IAM holds nowhere, so there is no identity behind it whose devices could be checked, and
// the claim is taken as made rather than refused: the same reading this emulator takes of
// such a key everywhere else. A key that does name an identity is held to its own devices,
// which is what StsMfaEnforcementIntegrationTest walks.
sts("GetSessionToken")
.formParam("SerialNumber", "arn:aws:iam::000000000000:mfa/user")
.formParam("TokenCode", "123456")