fix(iam): answer three refusals with their captured codes (#7364)

Captures from a real account on issue 6042 answer three refusals this
emulator had written for itself, each wrong in its code or its status
rather than only in its wording.

ChangePassword answered InvalidParameterValue 400 naming the user for a
wrong old password. AWS answers AccessDenied 403 and "Either the new
password does not conform to the account password policy or the old
password was incorrect.", which is now what it sends, as captured. The
sentence names both of the operation's causes without saying which one
refused the call, so a caller who cannot prove the current password
learns nothing about the policy either.

That it names the policy half does not settle the policy half. The
capture covers a wrong old password with a conforming new one, nothing
records AWS answering it for a password the policy refuses, and the
model declares PasswordPolicyViolation on this operation for exactly
that case, so a new password the account policy refuses keeps that error
and the rule it names, as it does on CreateLoginProfile and
UpdateLoginProfile. The old password is still read first, so a request
with both halves wrong answers the capture.

PolicySourceArn on SimulatePrincipalPolicy and
GetContextKeysForPrincipalPolicy resolved an entity out of whatever
followed the last slash, so arn:aws:iam:user/bob simulated as the user
bob. It is now held to AwsArnUtils.isArn before either simulator
resolves it, answering InvalidInput "Invalid ARN provided in the
request: <value>". A valid ARN that names neither a user nor a role
still answers the entity-type refusal it answered before.

GetAccessKeyInfo answered InvalidParameterValue quoting an id the
account never issued. AWS answers ValidationError "Access key ID is not
valid.", for an AKIA and an ASIA id alike, which also settles that it
refuses such an id rather than decoding the account out of it.

The wire and unit tests pin the two halves of the password refusal
answering differently and the order they are read in, both simulators
against three unparseable values and a well-formed one, and the key
refusal over both id formats. One enforcement test had pinned the
unparseable ARN resolving, which the capture overturns.
docs/services/iam.md and docs/services/sts.md carry the three answers,
what each capture covers, and what it leaves open.

Three comments that described these answers before the captures are
corrected with them: handleGetAccessKeyInfo's Javadoc, which still
called its old refusal a documented deviation,
StsAccessKeyInfoIntegrationTest's class comment, which said the same,
and validatedPolicySourceArn's, which said a value clearing 20
characters and parsing as no ARN is left alone and that what AWS answers
for one is not recorded.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-03 17:14:20 -06:00
1 parent ad8cd14724
commit 96fafbd4dc
12 files changed
+246 -64

No files matched your search

@@ -2412,9 +2412,10 @@ public class IamQueryHandler {
* {@code ValidationError} naming the length, so a value too short to be any ARN is refused on
* its shape rather than resolved to whatever follows its last slash.
*
* <p>A value that clears 20 characters and still parses as no ARN is left alone: the member
* publishes a length and no pattern, and what AWS answers for one is not recorded. An absent
* or blank value is left alone too, the resolver below reporting that one.
* <p>A value that clears 20 characters and still parses as no ARN is left to the resolver
* below, which refuses it as {@code InvalidInput} on a capture of its own: the member publishes
* a length and no pattern, so there is no shape to hold it to here. An absent or blank value
* is left there too, that resolver reporting it.
*/
private String validatedPolicySourceArn(MultivaluedMap<String, String> params) {
String arn = getParam(params, "PolicySourceArn");
@@ -2823,12 +2823,11 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
* 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.
* <p>A mismatched old password answers {@link #passwordChangeRefused}, captured from a real
* account: the reference publishes no error for one, and none of the six this operation
* declares describes it. The error it does document for a caller that is not an IAM user is
* {@code InvalidUserType}, raised by the handler. A new password the account's policy refuses
* is the case the operation does declare an error for, and keeps it.
*/
public void changePassword(String userName, String oldPassword, String newPassword) {
validatePassword(oldPassword, "oldPassword");
@@ -2836,9 +2835,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
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);
throw passwordChangeRefused();
}
checkPasswordPolicy(newPassword, profile);
recordSupersededPassword(profile);
@@ -2850,11 +2847,33 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
LOG.infov("Changed the IAM console password of user: {0}", userName);
}
/**
* What a wrong old password is refused with, captured from a real account:
* {@code AccessDenied} 403 and "Either the new password does not conform to the account
* password policy or the old password was incorrect.". The sentence is AWS's own and is sent
* as captured, naming both of the operation's causes without saying which one refused the
* call, so a caller who cannot prove the current password learns nothing about the policy
* either.
*
* <p>That it names the policy half does not settle the policy half. The capture covers a wrong
* old password with a conforming new one, nothing records AWS answering this for a password
* the policy refuses, and the model declares {@code PasswordPolicyViolation} on this operation
* for exactly that case, so that is what the policy half keeps until a capture says otherwise.
*/
private static AwsException passwordChangeRefused() {
return new AwsException("AccessDenied",
"Either the new password does not conform to the account password policy or the "
+ "old password was incorrect.", 403);
}
/**
* 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.
*
* <p>Every caller that stores a password answers it the same way, {@code ChangePassword}
* included: {@code PasswordPolicyViolation} naming the rule that failed.
*/
private void checkPasswordPolicy(String password, LoginProfile replacing) {
getAccountPasswordPolicy().ifPresent(policy -> {
@@ -5741,6 +5760,13 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
if (principalArn == null || principalArn.isBlank()) {
throw new AwsException("ValidationError", "PolicySourceArn is required.", 400);
}
// Captured from a real account: a value that clears the member's 20-character minimum and
// parses as no ARN is refused rather than resolved out of whatever follows its last slash,
// which used to simulate `arn:aws:iam:user/bob` as the user `bob`.
if (!AwsArnUtils.isArn(principalArn)) {
throw new AwsException("InvalidInput",
"Invalid ARN provided in the request: " + principalArn, 400);
}
if (principalArn.contains(":user/")) {
String userName = principalArn.substring(principalArn.lastIndexOf('/') + 1);
List<String> identityPolicies = withAccountGrants
@@ -308,10 +308,12 @@ public class StsQueryHandler {
* the lookup takes the key whatever state it is in, which is why it is not
* {@code resolveAccountId}, the authentication path, that answers here.
*
* <p>A key Floci does not hold is the one answer the reference leaves open. It models no error
* <p>A key Floci does not hold is the one answer the reference leaves open: it models no error
* for it and says only that "providing a deleted access key might return an error that the key
* doesn't exist", so this answers {@code InvalidParameterValue}, the published common error
* nearest that sentence, and {@code docs/services/sts.md} records it as the deviation it is.
* doesn't exist". A capture from a real account answers {@code ValidationError} 400, "Access
* key ID is not valid.", for an {@code AKIA} and an {@code ASIA} id alike, which settles the
* shape of the operation as a refusal rather than a decode of the account out of the id.
* {@code docs/services/sts.md} carries the capture.
*/
private Response handleGetAccessKeyInfo(MultivaluedMap<String, String> params) {
Response validation = validateDeclaredConstraints("GetAccessKeyInfo", params);
@@ -321,8 +323,11 @@ public class StsQueryHandler {
String accessKeyId = getParam(params, "AccessKeyId");
Optional<String> account = iamService.findAccountForAccessKeyId(accessKeyId);
if (account.isEmpty()) {
return AwsQueryResponse.error("InvalidParameterValue",
"The access key " + accessKeyId + " does not exist.", AwsNamespaces.STS, 400);
// Captured from a real account against an AKIA and an ASIA id alike: AWS refuses an id
// it never issued rather than decoding the account out of it, and the refusal carries
// neither the id nor a validation-error count.
return AwsQueryResponse.error("ValidationError",
"Access key ID is not valid.", AwsNamespaces.STS, 400);
}
String result = new XmlBuilder().elem("Account", account.get()).build();
return Response.ok(AwsQueryResponse.envelope("GetAccessKeyInfo", AwsNamespaces.STS, result)).build();