feat(iam): change the calling user's console password (#5546)

ChangePassword was the last IAM operation held back for a reason that turned
out not to hold. It was tracked as blocked on disambiguating a shared Query
action name by Version, because Cognito publishes the name too. In fact IAM
sits second in inferServiceFromAction's chain and Cognito ninth, and Cognito's
form path answers UnsupportedOperation to every request, so adding the name to
IAM_ACTIONS routes it to IAM and costs Cognito nothing. A request carrying a
credential scope never reaches that chain at all.

The operation takes no UserName. The API reference says it changes the password
of the IAM user who is calling, and 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", so a signing key
with no stored user behind it, which is Floci's stand-in for that caller,
answers InvalidUserType rather than reporting the key as missing. The resource
the request is evaluated against needed nothing new: ResourceArnBuilder already
resolves the signing user for an action whose UserName is absent, and the
vendored action map already authorizes ChangePassword against a user.

OldPassword is checked against the stored login profile before the new password
is held to the account password policy, so a caller who cannot prove the
current password learns nothing about what that policy requires. A mismatch is
the one refusal the reference leaves unnamed: every error it publishes for this
operation describes something else, so this answers InvalidParameterValue, the
common error type AWS publishes for a parameter value a service cannot accept.
A successful change clears PasswordResetRequired, which is what lets a user
whose password an administrator reset restore their own console access
programmatically, and it moves the password_last_changed the credential report
reads.

The passwordType shape check is now shared and names the member that failed,
because this operation has two password members and a caller told that
'password' failed would have to guess which. An absent member and an empty one
stay different answers.

Two things the login profile needed before a self-service change could be
trusted. The comparison and the write are one step under a new
loginProfileLock, which every login-profile writer takes: read apart from the
write, two requests presenting the same valid old password both pass the
comparison and both are told they set the password, while only the later write
survives, and a guard one writer skipped would leave a racing UpdateLoginProfile
free to overwrite a password a ChangePassword had just promised. And
PasswordReusePrevention is now enforced, on UpdateLoginProfile as well, the two
sharing one bounded history per profile: the reference defines the value as "the
number of previous passwords that IAM users are prevented from reusing" and
makes no window mean no prevention. The password the profile holds is refused
alongside the superseded ones, which the reference leaves open; read the other
way the smallest window the policy accepts would allow setting a password to the
value it already has, and would not refuse the change away and straight back
that the setting is for.

Twelve unit tests, one concurrency test and six integration tests, with each
guard probed by reverting it alone: dropping the old-password comparison, the
reset-flag clear, the policy check or the check order is caught by the test
written for it, and dropping the routing entry leaves the unsigned request
answered by Cognito, which is what the routing parity test says too. Dropping
the lock leaves five of twenty-four racing requests all told they succeeded with
one password stored, neutering the reuse check leaves both refusals silent, and
widening the window by one lets it reach a password that has left it. 1668
tests across all 98 IAM and query-routing classes pass at this commit.

docs/services/iam.md documents the operation, the judgements, and the one thing
Floci still does not model. AllowUsersToChangePassword is accepted and reported
but grants nothing:
the user guide says the option gives all users in the account access to
iam:ChangePassword for only their user and to iam:GetAccountPasswordPolicy,
applied at the account level rather than by attaching a policy, while Floci
authorizes both from identity policies alone, so under enforcement a caller
needs a policy naming their own user however the flag is set. The matrix row
moves 166 to 167.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-12 22:34:39 -05:00
1 parent 6b0d0bc378
commit 5ea632cc13
9 files changed
+683 -39

No files matched your search

@@ -104,6 +104,7 @@ public class AwsQueryController {
"TagPolicy", "UntagPolicy", "ListPolicyTags",
"TagInstanceProfile", "UntagInstanceProfile", "ListInstanceProfileTags",
"CreateLoginProfile", "GetLoginProfile", "DeleteLoginProfile", "UpdateLoginProfile",
"ChangePassword",
"GenerateCredentialReport", "GetCredentialReport",
"GetAccountSummary", "GetAccountAuthorizationDetails",
"SimulatePrincipalPolicy", "SimulateCustomPolicy",
@@ -130,6 +130,7 @@ public class IamQueryHandler {
case "GetLoginProfile" -> handleGetLoginProfile(params, authorization);
case "UpdateLoginProfile" -> handleUpdateLoginProfile(params);
case "DeleteLoginProfile" -> handleDeleteLoginProfile(params, authorization);
case "ChangePassword" -> handleChangePassword(params, authorization);
// Multi-factor authentication
case "CreateVirtualMFADevice" -> handleCreateVirtualMFADevice(params);
@@ -571,6 +572,40 @@ public class IamQueryHandler {
return Response.ok(AwsQueryResponse.envelopeNoResult("DeleteLoginProfile", AwsNamespaces.IAM)).build();
}
/**
* Changes the calling user's console password. The operation takes no {@code UserName}: the
* reference says it "changes the password of the IAM user who is calling this operation", and
* 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.
*/
private Response handleChangePassword(MultivaluedMap<String, String> params,
String authorization) {
// Both members are required, and read through getParam rather than requireParam because
// requireParam refuses a blank value where passwordType permits one: the pattern starts
// at \u0020, so a password of spaces is a value the model accepts. The shape check
// therefore stays with the pattern, which also names which of the two members failed.
iamService.changePassword(callingIamUser(authorization),
getParam(params, "OldPassword"), getParam(params, "NewPassword"));
return Response.ok(
AwsQueryResponse.envelopeNoResult("ChangePassword", AwsNamespaces.IAM)).build();
}
/**
* 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.
*/
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));
}
private String loginProfileXml(LoginProfile profile) {
return new XmlBuilder()
.elem("PasswordResetRequired", profile.isPasswordResetRequired())
@@ -387,6 +387,15 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
private final String seededAccountAlias;
/** Guards case-insensitive IAM name uniqueness checks and their corresponding writes. */
private final Object resourceNameLock = new Object();
/**
* Guards every check-then-write on a login profile. ChangePassword is the reason it has to
* exist: it reads the profile to compare the old password, then writes the new one, so two
* requests presenting the same valid old password would otherwise both pass the comparison
* and both report success while only the later write survived. Every writer takes it, since a
* guard one writer skips guarantees nothing: a racing UpdateLoginProfile would overwrite the
* password a ChangePassword had just been told it could set.
*/
private final Object loginProfileLock = new Object();
/**
* AWS-managed policies ({@code arn:<partition>:iam::aws:policy/...}), keyed by ARN, one map
@@ -888,11 +897,13 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
credential.getServiceSpecificCredentialId(), credential);
}
}
loginProfiles.get(userName).ifPresent(profile -> {
loginProfiles.delete(userName);
profile.setUserName(newUserName);
loginProfiles.put(newUserName, profile);
});
synchronized (loginProfileLock) {
loginProfiles.get(userName).ifPresent(profile -> {
loginProfiles.delete(userName);
profile.setUserName(newUserName);
loginProfiles.put(newUserName, profile);
});
}
for (AccessKey key : keysToMove) {
key.setUserName(newUserName);
accessKeys.put(key.getAccessKeyId(), key);
@@ -2338,13 +2349,19 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
/** A user holds at most one login profile: creating a second is EntityAlreadyExists. */
public LoginProfile createLoginProfile(String userName, String password, boolean passwordResetRequired) {
getUser(userName);
if (loginProfiles.get(userName).isPresent()) {
throw new AwsException("EntityAlreadyExists",
"Login Profile for User " + userName + " already exists.", 409);
LoginProfile profile;
synchronized (loginProfileLock) {
if (loginProfiles.get(userName).isPresent()) {
throw new AwsException("EntityAlreadyExists",
"Login Profile for User " + userName + " already exists.", 409);
}
validatePassword(password, "password");
// No profile to pass: a password being set for the first time replaces nothing, and a
// profile deleted and recreated takes its history with it.
checkPasswordPolicy(password, null);
profile = new LoginProfile(userName, password, passwordResetRequired);
loginProfiles.put(userName, profile);
}
validateLoginProfilePassword(password);
LoginProfile profile = new LoginProfile(userName, password, passwordResetRequired);
loginProfiles.put(userName, profile);
LOG.infov("Created IAM login profile for user: {0}", userName);
return profile;
}
@@ -2364,42 +2381,166 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
* clear the reset-required flag, and vice versa.
*/
public void updateLoginProfile(String userName, String password, Boolean passwordResetRequired) {
LoginProfile profile = getLoginProfile(userName);
if (password != null) {
validateLoginProfilePassword(password);
profile.setPassword(password);
profile.setPasswordLastChanged(Instant.now());
synchronized (loginProfileLock) {
LoginProfile profile = getLoginProfile(userName);
if (password != null) {
validatePassword(password, "password");
checkPasswordPolicy(password, profile);
recordSupersededPassword(profile);
profile.setPassword(password);
profile.setPasswordLastChanged(Instant.now());
}
if (passwordResetRequired != null) {
profile.setPasswordResetRequired(passwordResetRequired);
}
loginProfiles.put(userName, profile);
}
if (passwordResetRequired != null) {
profile.setPasswordResetRequired(passwordResetRequired);
}
loginProfiles.put(userName, profile);
LOG.infov("Updated IAM login profile for user: {0}", userName);
}
public void deleteLoginProfile(String userName) {
getUser(userName);
if (loginProfiles.get(userName).isEmpty()) {
throw new AwsException("NoSuchEntity",
"Login Profile for User " + userName + " cannot be found.", 404);
synchronized (loginProfileLock) {
if (loginProfiles.get(userName).isEmpty()) {
throw new AwsException("NoSuchEntity",
"Login Profile for User " + userName + " cannot be found.", 404);
}
loginProfiles.delete(userName);
}
loginProfiles.delete(userName);
LOG.infov("Deleted IAM login profile for user: {0}", userName);
}
/**
* Base wire validation shared by Create/UpdateLoginProfile, then, only when the account has
* ever set one (matching {@link #getAccountPasswordPolicy}'s own NoSuchEntity-by-default
* semantics), the account's password policy.
* Changes the password of the IAM user who is calling, which is the only user
* {@code ChangePassword} can act on: the reference says it "changes the password of the IAM
* user who is calling this operation" and that "the AWS account root user password is not
* affected by this operation". The caller is resolved from the signing access key before this
* is reached, so the user name here is already that user.
*
* <p>The old password is checked before the new one is held to the account's policy, so a
* caller who cannot prove the current password is refused on that alone and learns nothing
* about what the policy would have required.
*
* <p>The comparison and the write are one step under {@code loginProfileLock}, which is what
* makes the refusal above mean anything: read apart from the write, two requests presenting
* the same valid old password would both pass the comparison and both be told they had set
* the password, while only the later write survived.
*
* <p>A successful change clears {@code PasswordResetRequired}. The user guide's own reason for
* the "Password expiration requires administrator reset" option is that it stops users
* updating their own password through the console, and says that even then "IAM users with
* iam:ChangePassword permission and active access keys can reset their own IAM user console
* password programmatically", which is only true if the change itself clears the state the
* reset left behind.
*
* <p>An old password that does not match is the one refusal the reference leaves unnamed:
* every error it publishes for this operation describes something else, and the error it
* documents for a caller that is not an IAM user is {@code InvalidUserType}, raised by the
* handler. The common error types are additive per action, so this answers
* {@code InvalidParameterValue}, which is the published one for a parameter value the service
* cannot accept, with the same 400 as the operation's other sender faults.
*/
private void validateLoginProfilePassword(String password) {
if (password == null || !LOGIN_PROFILE_PASSWORD_PATTERN.matcher(password).matches()) {
throw new AwsException("ValidationError",
"1 validation error detected: Value at 'password' failed to satisfy constraint: "
+ "Member must have length less than or equal to 128 and greater than or "
+ "equal to 1", 400);
public void changePassword(String userName, String oldPassword, String newPassword) {
validatePassword(oldPassword, "oldPassword");
validatePassword(newPassword, "newPassword");
synchronized (loginProfileLock) {
LoginProfile profile = getLoginProfile(userName);
if (!oldPassword.equals(profile.getPassword())) {
throw new AwsException("InvalidParameterValue",
"The old password supplied is not the current password for user "
+ userName + ".", 400);
}
checkPasswordPolicy(newPassword, profile);
recordSupersededPassword(profile);
profile.setPassword(newPassword);
profile.setPasswordLastChanged(Instant.now());
profile.setPasswordResetRequired(false);
loginProfiles.put(userName, profile);
}
LOG.infov("Changed the IAM console password of user: {0}", userName);
}
/**
* The account's password policy applied to a password about to be stored, and only when the
* account has ever set one, matching {@link #getAccountPasswordPolicy}'s own
* NoSuchEntity-by-default semantics. {@code replacing} is the profile the password is being
* written over, or null when there is none to reuse a password from.
*/
private void checkPasswordPolicy(String password, LoginProfile replacing) {
getAccountPasswordPolicy().ifPresent(policy -> {
checkPasswordAgainstPolicy(password, policy);
if (replacing != null) {
checkPasswordNotReused(replacing, password, policy);
}
});
}
/**
* Refuses a password the account's reuse window still holds. The reference defines
* {@code PasswordReusePrevention} as "the number of previous passwords that IAM users are
* prevented from reusing", with no value meaning "IAM users are not prevented from reusing
* previous passwords", so the window is that many superseded passwords.
*
* <p>The password the profile currently holds is refused alongside them, which the reference
* does not spell out. Reading it the other way makes a window of 1 refuse nothing a user
* would attempt: it would leave setting a password to the value it already has allowed, and
* that is reuse in the only sense the setting exists to stop. Counting the current password
* first is also what makes the A to B and back to A case refusable at the smallest window the
* policy accepts.
*/
private static void checkPasswordNotReused(LoginProfile profile, String password,
AccountPasswordPolicy policy) {
Integer window = policy.getPasswordReusePrevention();
if (window == null) {
return;
}
List<String> held = profile.getPreviousPasswords();
List<String> refused = new ArrayList<>();
refused.add(profile.getPassword());
refused.addAll(held.subList(0, Math.min(window, held.size())));
if (refused.contains(password)) {
throw new AwsException("PasswordPolicyViolation",
"Password does not conform to the account password policy. It must not be the "
+ "current password or one of the previous " + window
+ " passwords.", 400);
}
}
/**
* Moves the password a profile is about to lose into its history, newest first. Bounded at the
* largest window {@code PasswordReusePrevention} accepts, since nothing can be asked about a
* password further back than that.
*/
private static void recordSupersededPassword(LoginProfile profile) {
List<String> held = profile.getPreviousPasswords();
held.add(0, profile.getPassword());
while (held.size() > PASSWORD_REUSE_PREVENTION_CEILING) {
held.remove(held.size() - 1);
}
}
/**
* {@code passwordType}, which every password member on every one of these operations shares.
* The member is named so the message is the one AWS gives for the parameter that failed:
* {@code ChangePassword} has two of them, and a caller told that 'password' failed has to
* guess which.
*
* <p>An absent member and an empty one are different answers. The pattern requires at least
* one character, so an empty value fails the constraint, where an absent one was never
* supplied at all.
*/
private static void validatePassword(String password, String member) {
if (password == null) {
throw new AwsException("ValidationError",
"1 validation error detected: Value null at '" + member
+ "' failed to satisfy constraint: Member must not be null", 400);
}
if (!LOGIN_PROFILE_PASSWORD_PATTERN.matcher(password).matches()) {
throw new AwsException("ValidationError",
"1 validation error detected: Value at '" + member
+ "' failed to satisfy constraint: Member must have length less than "
+ "or equal to 128 and greater than or equal to 1", 400);
}
getAccountPasswordPolicy().ifPresent(policy -> checkPasswordAgainstPolicy(password, policy));
}
private void checkPasswordAgainstPolicy(String password, AccountPasswordPolicy policy) {
@@ -3476,7 +3617,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
* <p>The API Reference publishes a length of 20 to 128 as well as the pattern, so both are
* checked, and both are reported when both are broken, in the order it prints them. The
* pattern message is the one recorded against AWS; the length wording is the form AWS uses for
* a member carrying both bounds, which {@link #validateLoginProfilePassword} spells the same
* a member carrying both bounds, which {@link #validatePassword} spells the same
* way.
*/
private void requireCredentialIdFormat(String credentialId) {
@@ -4,13 +4,16 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
/**
* A user's console (password) login profile. At most one per user, mirroring AWS: creating
* one when the user already has one is {@code EntityAlreadyExists}, and a user with no
* console password simply has no entry here. The password is stored in plain text, matching
* how {@link AccessKey} already stores its secret: this is an emulator, not a credential
* store, and AWS itself never echoes the password back on any of these actions.
* console password simply has no entry here. The password, and the history the reuse window is
* checked against, are stored in plain text, matching how {@link AccessKey} already stores its
* secret: this is an emulator, not a credential store, and AWS itself never echoes the password
* back on any of these actions.
*/
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
@@ -27,6 +30,13 @@ public class LoginProfile {
* a profile persisted before this field existed; callers fall back to {@link #createDate}.
*/
private Instant passwordLastChanged;
/**
* The passwords this profile held before the current one, most recent first, bounded at the 24
* {@code PasswordReusePrevention} is allowed to ask for. Recorded whether or not the account
* sets that policy, so switching it on has something to compare against, and empty for a
* profile persisted before this field existed.
*/
private List<String> previousPasswords = new ArrayList<>();
public LoginProfile() {}
@@ -54,4 +64,16 @@ public class LoginProfile {
public Instant getPasswordLastChanged() { return passwordLastChanged; }
public void setPasswordLastChanged(Instant passwordLastChanged) { this.passwordLastChanged = passwordLastChanged; }
public List<String> getPreviousPasswords() {
if (previousPasswords == null) {
previousPasswords = new ArrayList<>();
}
return previousPasswords;
}
public void setPreviousPasswords(List<String> previousPasswords) {
this.previousPasswords =
previousPasswords != null ? new ArrayList<>(previousPasswords) : new ArrayList<>();
}
}