fix(iam): answer an assumed-role session's ChangePassword as AWS does (#9181)

ChangePassword answered 400 InvalidUserType to every caller with no
stored user behind the signing key, reading the CLI reference's sentence
for account root credentials. An assumed-role session is not that
caller: recorded against a real account on 2026-10-29, it is answered
NoSuchEntity 404, a code this operation's model declares alongside
InvalidUserType, so the two are not one case and that session was being
given root's error.

The recording is the whole of the change. That session answers the
missing key, with the message this package already gives wherever a
signing key resolves to no user, which keeps one situation to one
wording rather than minting a second. Every other caller keeps
InvalidUserType: a session AssumeRoot minted acts as the account root,
which is what that error is published for however root signs, and what
AWS answers the remaining session kinds is not recorded.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-22 01:32:18 -04:00
1 parent 48b95fc8c7
commit eb6998ed2e
3 files changed
+236 -11

No files matched your search

+11 -2
View File
@@ -904,8 +904,17 @@ is never echoed back by any of these actions, matching AWS.
`ChangePassword` is the self-service half of `UpdateLoginProfile`. It takes no `UserName` and acts
only on the user whose access key signed the request, which is what the API reference means by "the
IAM user who is calling this operation", and the account root password is not affected by it. A
caller with no IAM user behind its key, which is Floci's stand-in for account root credentials, is
refused with `InvalidUserType`, the error the CLI reference documents for that case.
caller with no IAM user behind its key is refused with `InvalidUserType`, the error the CLI
reference documents for account root credentials, which is what such a key stands in for here.
One of those callers is answered differently. An assumed-role session is refused `NoSuchEntity`
404, recorded against a real account on 2026-10-29, a code this operation's model declares
alongside `InvalidUserType`; the recording names the code and not the message, so the message is
the one Floci already answers wherever a signing key resolves to no user, `The Access Key with id
<id> cannot be found.` An assumed role is not root, which is what that error is published for, and
the recording is the whole of what Floci reads into it: a session `AssumeRoot` minted acts as the
account root and keeps root's error, and what AWS answers the remaining session kinds is not
recorded, so they keep it as well.
`OldPassword` must match the stored password, and a mismatch answers `AccessDenied` with status
403 and "Either the new password does not conform to the account password policy or the old
@@ -408,8 +408,7 @@ public class IamQueryHandler {
"No access key could be determined from the request.", 404);
}
return iamService.findUserNameByAccessKeyId(accessKeyId)
.orElseThrow(() -> new AwsException("NoSuchEntity",
"The Access Key with id " + accessKeyId + " cannot be found.", 404));
.orElseThrow(() -> accessKeyNotFound(accessKeyId));
}
/**
@@ -640,7 +639,8 @@ public class IamQueryHandler {
* the CLI reference adds that it "can be called by IAM users only. If this command is called
* using AWS account (root) credentials, the command returns an InvalidUserType error". A
* signing key that belongs to no stored user is Floci's stand-in for exactly that caller, so
* it answers the same way rather than reporting the key as missing.
* it answers the same way rather than reporting the key as missing; an assumed-role session
* answers {@code NoSuchEntity}, which {@link #callingIamUser} sets out.
*/
private Response handleChangePassword(MultivaluedMap<String, String> params,
String authorization) {
@@ -656,16 +656,52 @@ public class IamQueryHandler {
/**
* The IAM user whose access key signed the request, for the one operation that acts on the
* caller and gives no way to name anyone else. Unlike {@link #resolveUserName}, a key with no
* user behind it is not a missing entity here: the caller is not an IAM user, which is the
* case {@code InvalidUserType} is published for.
* caller and gives no way to name anyone else. A key with no user behind it is not one caller
* but several, and AWS does not answer them all the same way.
*
* <p>Credentials that belong to no stored user are Floci's stand-in for account root, and the
* CLI reference publishes that case: the operation "can be called by IAM users only. If this
* command is called using AWS account (root) credentials, the command returns an
* InvalidUserType error". That stays the answer for every caller but the one recorded below,
* a session minted for the account root included: the error is published for root, however
* root signs, and {@code AssumeRoot} mints a session that acts as the account root.
*
* <p>An assumed-role session is the recorded exception. Taken against a real account on
* 2026-10-29, {@code ChangePassword} under one answers {@code NoSuchEntity} 404, a code this
* operation's model declares alongside {@code InvalidUserType}, so the two are not one case
* and that session must not be given root's error. The recording names the code and not the
* sentence, so the sentence is {@link #accessKeyNotFound}'s, the one this package already
* answers wherever a signing key resolves to no user, which keeps one situation to one
* wording. What AWS answers the other session kinds is not recorded, so they keep the error
* they had.
*/
private String callingIamUser(String authorization) {
String accessKeyId = accountResolver.extractAccessKeyId(authorization);
return iamService.findUserNameByAccessKeyId(accessKeyId)
.orElseThrow(() -> new AwsException("InvalidUserType",
"The request was rejected because the type of user for the transaction "
+ "was incorrect.", 400));
.orElseThrow(() -> signedByAnAssumedRole(accessKeyId)
? accessKeyNotFound(accessKeyId)
: new AwsException("InvalidUserType",
"The request was rejected because the type of user for the "
+ "transaction was incorrect.", 400));
}
/**
* Whether an assumed-role session signed the request, the one caller with no stored user
* behind it that is answered a missing key rather than root's error. The principal type is
* what separates them rather than the key's own prefix, which every session shares: a
* privileged session and a session the root credential minted each report the account,
* being the root they act as.
*/
private boolean signedByAnAssumedRole(String accessKeyId) {
return iamService.resolveCallerArns(accessKeyId)
.filter(caller -> IamService.CallerArns.TYPE_ASSUMED_ROLE.equals(caller.principalType()))
.isPresent();
}
/** The error a signing key that resolves to no stored user answers, wherever one is read. */
private static AwsException accessKeyNotFound(String accessKeyId) {
return new AwsException("NoSuchEntity",
"The Access Key with id " + accessKeyId + " cannot be found.", 404);
}
private String loginProfileXml(LoginProfile profile) {
@@ -0,0 +1,180 @@
package io.floci.aws.services.iam;
import io.floci.aws.testing.ValidateSignaturesProfile;
import io.floci.aws.testutil.StsRequests.Credentials;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.response.Response;
import jakarta.inject.Inject;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static io.floci.aws.testutil.StsRequests.request;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Which caller {@code ChangePassword} refuses with which error. The operation names nobody, so the
* signing credential is the whole of the question, and a credential with no IAM user behind it is
* two callers rather than one.
*
* <p>Recorded against a real account on 2026-10-29: an assumed-role session is answered
* {@code NoSuchEntity} 404, where the CLI reference publishes {@code InvalidUserType} for account
* root credentials. That recording is the whole of the change, so every other caller with no
* stored user behind its key is here to hold its own error still. Driven through the signed
* transport because that is the only way the handler sees a session credential at all.
*/
@QuarkusTest
@TestProfile(ValidateSignaturesProfile.class)
class IamChangePasswordCallerTypeIntegrationTest {
private static final String ACCOUNT = "000000000000";
private static final Credentials ROOT = new Credentials("test", "test", null);
private static final String PASSWORD = "Sup3r$ecret!";
private static final String NEW_PASSWORD = "An0ther$ecret!";
/** Unchanged: the sentence the CLI reference's case keeps. */
private static final String ROOT_REFUSAL = "The request was rejected because the type of user "
+ "for the transaction was incorrect.";
private final List<String> sessions = new ArrayList<>();
private final List<Runnable> cleanup = new ArrayList<>();
@Inject
IamService iam;
@AfterEach
void cleanup() {
sessions.forEach(key -> iam.unregisterSession(ACCOUNT, key));
for (int i = cleanup.size() - 1; i >= 0; i--) {
cleanup.get(i).run();
}
}
/** The recorded caller: an assumed role is not root, so it is not answered root's error. */
@Test
void anAssumedRoleSessionIsNoSuchEntity() throws Exception {
Credentials session = assumedRoleSession();
changePassword(session)
.then().statusCode(404)
.body("ErrorResponse.Error.Code", equalTo("NoSuchEntity"))
.body("ErrorResponse.Error.Type", equalTo("Sender"))
.body("ErrorResponse.Error.Message",
equalTo("The Access Key with id " + session.key() + " cannot be found."));
}
/**
* The account credential's own session is root's session: {@code InvalidUserType} is published
* for root however root signs, so this caller keeps it. A privileged session, the one
* {@code AssumeRoot} mints, reports the same principal as this one and so takes the same
* branch; what that operation needs set up to answer at all is its own test's work.
*/
@Test
void aSessionTheAccountCredentialMintedKeepsInvalidUserType() throws Exception {
Credentials session = credentials(
request(ROOT, "sts", "GetSessionToken", Map.of()), "GetSessionToken");
changePassword(session)
.then().statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("InvalidUserType"))
.body("ErrorResponse.Error.Message", equalTo(ROOT_REFUSAL));
}
/**
* A session an IAM user minted carries no stored key either, and no recording of its own. It
* is not the recorded caller, so this change leaves it the error it already answered.
*/
@Test
void aSessionAnIamUserMintedKeepsInvalidUserType() throws Exception {
Credentials user = userWithAConsolePassword("session-token");
Credentials session = credentials(
request(user, "sts", "GetSessionToken", Map.of()), "GetSessionToken");
changePassword(session)
.then().statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("InvalidUserType"))
.body("ErrorResponse.Error.Message", equalTo(ROOT_REFUSAL));
}
/** The account credential stands in for root, and that case is unchanged. */
@Test
void theAccountCredentialIsStillInvalidUserType() throws Exception {
changePassword(ROOT)
.then().statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("InvalidUserType"))
.body("ErrorResponse.Error.Type", equalTo("Sender"))
.body("ErrorResponse.Error.Message", equalTo(ROOT_REFUSAL));
}
/** The control: the caller the operation is for still changes its own password. */
@Test
void anIamUserStillChangesItsOwnPassword() throws Exception {
Credentials user = userWithAConsolePassword("own-password");
changePassword(user).then().statusCode(200);
}
private Response changePassword(Credentials caller) throws Exception {
return request(caller, "iam", "ChangePassword",
Map.of("OldPassword", PASSWORD, "NewPassword", NEW_PASSWORD));
}
private Credentials assumedRoleSession() throws Exception {
String role = "change-password-caller-" + UUID.randomUUID();
Response created = request(ROOT, "iam", "CreateRole", Map.of("RoleName", role,
"AssumeRolePolicyDocument", "{\"Version\":\"2012-10-17\",\"Statement\":[]}"));
created.then().statusCode(200);
String arn = created.xmlPath().getString("CreateRoleResponse.CreateRoleResult.Role.Arn");
assertNotNull(arn);
cleanup.add(() -> remove("DeleteRole", Map.of("RoleName", role)));
Credentials session = credentials(request(ROOT, "sts", "AssumeRole",
Map.of("RoleArn", arn, "RoleSessionName", "caller")), "AssumeRole");
assertTrue(session.key().startsWith("ASIA"));
return session;
}
private Credentials userWithAConsolePassword(String label) throws Exception {
// Short of a UUID: a user name is 64 characters at most and the label is already long.
String name = "cp-" + label + "-" + Long.toHexString(System.nanoTime());
request(ROOT, "iam", "CreateUser", Map.of("UserName", name)).then().statusCode(200);
cleanup.add(() -> remove("DeleteUser", Map.of("UserName", name)));
request(ROOT, "iam", "CreateLoginProfile", Map.of("UserName", name, "Password", PASSWORD))
.then().statusCode(200);
cleanup.add(() -> remove("DeleteLoginProfile", Map.of("UserName", name)));
Response key = request(ROOT, "iam", "CreateAccessKey", Map.of("UserName", name));
key.then().statusCode(200);
String prefix = "CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.";
String accessKeyId = key.xmlPath().getString(prefix + "AccessKeyId");
cleanup.add(() -> remove("DeleteAccessKey",
Map.of("UserName", name, "AccessKeyId", accessKeyId)));
return new Credentials(accessKeyId, key.xmlPath().getString(prefix + "SecretAccessKey"),
null);
}
private Credentials credentials(Response response, String action) {
response.then().statusCode(200);
String prefix = action + "Response." + action + "Result.Credentials.";
String key = response.xmlPath().getString(prefix + "AccessKeyId");
sessions.add(key);
Credentials result = new Credentials(key,
response.xmlPath().getString(prefix + "SecretAccessKey"),
response.xmlPath().getString(prefix + "SessionToken"));
assertNotNull(result.secret());
assertNotNull(result.token());
return result;
}
private void remove(String action, Map<String, String> params) {
try {
request(ROOT, "iam", action, params).then().statusCode(200);
} catch (Exception failure) {
throw new AssertionError("Fixture cleanup failed: " + action, failure);
}
}
}