diff --git a/docs/services/iam.md b/docs/services/iam.md index 087d5bf742..abe96cc3af 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -778,6 +778,36 @@ rejected with `ValidationError`. The integer parameters (`MinimumPasswordLength` a malformed integer or a value other than `true`/`false` (case-insensitive) returns `ValidationError` rather than silently falling back to a default. +#### What a refused password answers + +A password the policy refuses is `PasswordPolicyViolation` 400 with a sender fault, and the message +names one rule and no more, even where the password breaks several. The sentence is +`Password should have `, with no period at the end: + +| Rule | What the refusal says | +|---|---| +| `MinimumPasswordLength` | `Password should have a minimum length of ` | +| `RequireUppercaseCharacters` | `Password should have at least one uppercase letter` | +| `RequireLowercaseCharacters` | `Password should have at least one lowercase letter` | +| `RequireNumbers` | `Password should have at least one number` | +| `RequireSymbols` | `Password should have at least one symbol` | + +Two of the five are recorded against a real account, both on `CreateLoginProfile`: the length on +2026-10-29, where the policy's own configured minimum is substituted and nothing else in the +sentence varies, and the symbol on 2026-11-11, which is wholly fixed text and names no symbol set, +where this emulator used to enumerate one. The three character classes left carry no recording and +take the same shape, which is a reading rather than a measurement. + +Which rule a refusal names when a password breaks more than one is recorded nowhere either, each +recording having used a password that broke exactly one rule, so the order is fixed here and +stated: the length first, then the character classes in the order the policy declares them. A +password that is both too short and has no symbol is answered the length. + +The reuse window is the one refusal of the six that keeps a sentence of this emulator's own, +`Password does not conform to the account password policy. It must not be the current password or +one of the previous passwords.`, because AWS publishes no wording for a +reused password and nothing records one. A recording moves that one sentence and nothing else. + ### OIDC Identity Providers | Action | Description | @@ -866,8 +896,9 @@ returns `EntityAlreadyExists`; `Get`/`Update`/`DeleteLoginProfile` on a user wit wholesale replace. A password must be 1–128 characters from AWS's documented password character class, and when the account has an [account password policy](#account-password-policy) set, it is also checked against that policy's length and character-class requirements, with -`PasswordPolicyViolation` returned on either action if it doesn't comply, naming the rule the -password broke, which is also what `ChangePassword` answers for a new password. The password itself +`PasswordPolicyViolation` returned on either action if it doesn't comply, naming the one rule the +password broke in [the sentence AWS uses](#what-a-refused-password-answers), which is also what +`ChangePassword` answers for a new password. The password itself 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 diff --git a/src/main/java/io/floci/aws/services/iam/IamService.java b/src/main/java/io/floci/aws/services/iam/IamService.java index 30a5a6c445..67df59cee6 100644 --- a/src/main/java/io/floci/aws/services/iam/IamService.java +++ b/src/main/java/io/floci/aws/services/iam/IamService.java @@ -3306,6 +3306,10 @@ public class IamService implements SessionAccountLookup, ResourceProvider { * prevented from reusing", with no value meaning "IAM users are not prevented from reusing * previous passwords", so the window is that many superseded passwords. * + *

The sentence is this package's own and stays that way: AWS publishes no wording for a + * reused password and nothing records one, where the five rules of {@link PasswordRule} carry + * two recordings between them. A recording moves this one site. + * *

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 @@ -3366,27 +3370,61 @@ public class IamService implements SessionAccountLookup, ResourceProvider { } } + /** + * One rule of the account password policy, named the way AWS names it. A refusal carries one + * of these and no more, even where the password breaks several, so the rules are checked in + * the order a refusal names them: the length first, then the character classes in the order + * the policy declares them. + * + *

Two of the five are captured from a real account, both {@code PasswordPolicyViolation} + * 400 with a sender fault and both on {@code CreateLoginProfile}: the length on 2026-10-29, + * with the policy's own minimum substituted, and the symbol on 2026-11-11, which is wholly + * fixed text and names no symbol set. The three character classes left carry no recording and + * take the same shape, which is this emulator's reading rather than a measurement, and so is + * the order above. Neither recorded sentence ends in a period. + */ + private enum PasswordRule { + MINIMUM_LENGTH, + UPPERCASE, + LOWERCASE, + NUMBER, + SYMBOL; + + private String sentenceFor(AccountPasswordPolicy policy) { + return switch (this) { + case MINIMUM_LENGTH -> "a minimum length of " + policy.getMinimumPasswordLength(); + case UPPERCASE -> "at least one uppercase letter"; + case LOWERCASE -> "at least one lowercase letter"; + case NUMBER -> "at least one number"; + case SYMBOL -> "at least one symbol"; + }; + } + + private boolean isBrokenBy(String password, AccountPasswordPolicy policy) { + return switch (this) { + case MINIMUM_LENGTH -> password.length() < policy.getMinimumPasswordLength(); + case UPPERCASE -> policy.isRequireUppercaseCharacters() + && password.chars().noneMatch(Character::isUpperCase); + case LOWERCASE -> policy.isRequireLowercaseCharacters() + && password.chars().noneMatch(Character::isLowerCase); + case NUMBER -> policy.isRequireNumbers() + && password.chars().noneMatch(Character::isDigit); + case SYMBOL -> policy.isRequireSymbols() + && password.chars().allMatch(Character::isLetterOrDigit); + }; + } + + private AwsException refusal(AccountPasswordPolicy policy) { + return new AwsException("PasswordPolicyViolation", + "Password should have " + sentenceFor(policy), 400); + } + } + private void checkPasswordAgainstPolicy(String password, AccountPasswordPolicy policy) { - List violations = new ArrayList<>(); - if (password.length() < policy.getMinimumPasswordLength()) { - violations.add("a minimum length of " + policy.getMinimumPasswordLength()); - } - if (policy.isRequireUppercaseCharacters() && password.chars().noneMatch(Character::isUpperCase)) { - violations.add("at least one uppercase letter"); - } - if (policy.isRequireLowercaseCharacters() && password.chars().noneMatch(Character::isLowerCase)) { - violations.add("at least one lowercase letter"); - } - if (policy.isRequireNumbers() && password.chars().noneMatch(Character::isDigit)) { - violations.add("at least one number"); - } - if (policy.isRequireSymbols() && password.chars().allMatch(Character::isLetterOrDigit)) { - violations.add("at least one non-alphanumeric character"); - } - if (!violations.isEmpty()) { - throw new AwsException("PasswordPolicyViolation", - "Password does not conform to the account password policy. It must contain: " - + String.join(", ", violations) + ".", 400); + for (PasswordRule rule : PasswordRule.values()) { + if (rule.isBrokenBy(password, policy)) { + throw rule.refusal(policy); + } } } diff --git a/src/test/java/io/floci/aws/services/iam/IamPasswordRuleWordingIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/IamPasswordRuleWordingIntegrationTest.java new file mode 100644 index 0000000000..5dfdee3565 --- /dev/null +++ b/src/test/java/io/floci/aws/services/iam/IamPasswordRuleWordingIntegrationTest.java @@ -0,0 +1,218 @@ +package io.floci.aws.services.iam; + +import io.floci.aws.testing.PartitionCleanup; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.ValidatableResponse; +import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import java.util.Map; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.not; + +/** + * What a password the account policy refuses is answered, over the wire and on all three + * operations that store one. + * + *

Two of the sentences are recorded against a real account, both on {@code CreateLoginProfile}: + * the length on 2026-10-29 and the symbol on 2026-11-11. The three character classes left carry + * no recording and take the same shape, and so does the order a refusal picks a rule in, which no + * recording covers either because each used a password that broke exactly one rule. + * + *

Runs in an account of its own, like the other IAM classes that set account-wide state: the + * emulator is shared, and a password policy left visible to another class changes what that + * class's passwords are held to. + */ +@QuarkusTest +class IamPasswordRuleWordingIntegrationTest { + + private static final String ACCOUNT = "000000000915"; + private static final String REGION = "us-east-1"; + /** Clears every rule of the policy below, so a refusal is the rule a case leaves broken. */ + private static final String CONFORMING = "L0ngEn0ugh$ecret!!!!"; + + @RegisterExtension + final PartitionCleanup cleanup = new PartitionCleanup(); + + /** Recorded: the configured minimum is substituted and nothing else in the sentence varies. */ + @Test + void aShortPasswordIsAnsweredTheRecordedLengthSentence() { + policyRequiring(Map.of("MinimumPasswordLength", "20")); + String userName = user("short"); + + refusal("CreateLoginProfile", userName, "Sh0rt$") + .body("ErrorResponse.Error.Message", + equalTo("Password should have a minimum length of 20")); + + // The refusal stores nothing, so the profile is still there to be created. + admin("CreateLoginProfile", Map.of("UserName", userName, "Password", CONFORMING)) + .statusCode(200); + cleanup.register(() -> admin("DeleteLoginProfile", Map.of("UserName", userName)) + .statusCode(200)); + } + + /** Recorded: wholly fixed text, naming no symbol set, where this package used to list one. */ + @Test + void aPasswordWithNoSymbolIsAnsweredTheRecordedSymbolSentence() { + policyRequiring(Map.of("MinimumPasswordLength", "6", "RequireSymbols", "true")); + + refusal("CreateLoginProfile", user("symbol"), "N0Symb0lsHere") + .body("ErrorResponse.Error.Message", + equalTo("Password should have at least one symbol")); + } + + /** The three with no recording, each one the only rule its password breaks. */ + @Test + void theThreeCharacterClassesTakeTheSameShape() { + policyRequiring(Map.of("MinimumPasswordLength", "6", "RequireUppercaseCharacters", "true", + "RequireLowercaseCharacters", "true", "RequireNumbers", "true")); + + refusal("CreateLoginProfile", user("upper"), "n0uppercase!") + .body("ErrorResponse.Error.Message", + equalTo("Password should have at least one uppercase letter")); + refusal("CreateLoginProfile", user("lower"), "N0LOWERCASE!") + .body("ErrorResponse.Error.Message", + equalTo("Password should have at least one lowercase letter")); + refusal("CreateLoginProfile", user("number"), "NoNumbersHere!") + .body("ErrorResponse.Error.Message", + equalTo("Password should have at least one number")); + } + + /** + * One rule and no more, which is what makes the order matter: a password breaking four of + * them is told about the length, and the other three are not appended to the sentence. + */ + @Test + void aPasswordBreakingSeveralRulesIsAnsweredTheFirstOfThem() { + policyRequiring(Map.of("MinimumPasswordLength", "20", "RequireUppercaseCharacters", "true", + "RequireNumbers", "true", "RequireSymbols", "true")); + + refusal("CreateLoginProfile", user("several"), "short") + .body("ErrorResponse.Error.Message", + equalTo("Password should have a minimum length of 20")) + .body("ErrorResponse.Error.Message", not(containsString("uppercase"))) + .body("ErrorResponse.Error.Message", not(containsString("symbol"))) + .body("ErrorResponse.Error.Message", not(containsString("number"))); + } + + /** The administrator's own reset answers what the creation does. */ + @Test + void updateLoginProfileAnswersTheSameSentence() { + policyRequiring(Map.of("MinimumPasswordLength", "20")); + String userName = userWithAConsolePassword("update"); + + refusal("UpdateLoginProfile", userName, "Sh0rt$") + .body("ErrorResponse.Error.Message", + equalTo("Password should have a minimum length of 20")); + } + + /** + * The self-service half answers it too, which the capture of 2026-10-31 leaves to the model: + * a wrong old password is {@code AccessDenied} and names both causes, where a new password the + * policy refuses is told the rule it broke. + */ + @Test + void changePasswordAnswersTheSameSentence() { + policyRequiring(Map.of("MinimumPasswordLength", "20", "RequireSymbols", "true")); + String userName = userWithAConsolePassword("change"); + String accessKeyId = accessKeyFor(userName); + + iam(accessKeyId, "ChangePassword", + Map.of("OldPassword", CONFORMING, "NewPassword", "N0Symb0lsHereButL0ngEnough")) + .statusCode(400) + .body("ErrorResponse.Error.Code", equalTo("PasswordPolicyViolation")) + .body("ErrorResponse.Error.Type", equalTo("Sender")) + .body("ErrorResponse.Error.Message", + equalTo("Password should have at least one symbol")); + } + + /** + * The reuse window is the one refusal here that keeps a sentence of this package's own, AWS + * publishing no wording for a reused password and nothing recording one. Pinned so a + * recording moves this sentence rather than arriving unnoticed. + */ + @Test + void aReusedPasswordKeepsThisPackagesOwnSentence() { + policyRequiring(Map.of("MinimumPasswordLength", "6", "PasswordReusePrevention", "3")); + String userName = userWithAConsolePassword("reuse"); + + refusal("UpdateLoginProfile", userName, CONFORMING) + .body("ErrorResponse.Error.Message", + equalTo("Password does not conform to the account password policy. It must" + + " not be the current password or one of the previous 3" + + " passwords.")); + } + + /** The control: a password that breaks no rule is stored, so none of the above is a blanket refusal. */ + @Test + void aConformingPasswordIsStillStored() { + policyRequiring(Map.of("MinimumPasswordLength", "20", "RequireUppercaseCharacters", "true", + "RequireLowercaseCharacters", "true", "RequireNumbers", "true", + "RequireSymbols", "true")); + String userName = user("conforming"); + + admin("CreateLoginProfile", Map.of("UserName", userName, "Password", CONFORMING)) + .statusCode(200); + cleanup.register(() -> admin("DeleteLoginProfile", Map.of("UserName", userName)) + .statusCode(200)); + } + + /** A policy for this case alone, removed again when the case ends. */ + private void policyRequiring(Map rules) { + admin("UpdateAccountPasswordPolicy", rules).statusCode(200); + cleanup.register(() -> admin("DeleteAccountPasswordPolicy", Map.of()).statusCode(200)); + } + + private ValidatableResponse refusal(String action, String userName, String password) { + return admin(action, Map.of("UserName", userName, "Password", password)) + .statusCode(400) + .body("ErrorResponse.Error.Code", equalTo("PasswordPolicyViolation")) + .body("ErrorResponse.Error.Type", equalTo("Sender")); + } + + private String user(String label) { + String userName = "password-rule-" + label; + admin("CreateUser", Map.of("UserName", userName)).statusCode(200); + cleanup.register(() -> admin("DeleteUser", Map.of("UserName", userName)).statusCode(200)); + return userName; + } + + private String userWithAConsolePassword(String label) { + String userName = user(label); + admin("CreateLoginProfile", Map.of("UserName", userName, "Password", CONFORMING)) + .statusCode(200); + cleanup.register(() -> admin("DeleteLoginProfile", Map.of("UserName", userName)) + .statusCode(200)); + return userName; + } + + private String accessKeyFor(String userName) { + String accessKeyId = admin("CreateAccessKey", Map.of("UserName", userName)).statusCode(200) + .extract().path("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey" + + ".AccessKeyId"); + cleanup.register(() -> admin("DeleteAccessKey", + Map.of("UserName", userName, "AccessKeyId", accessKeyId)).statusCode(200)); + return accessKeyId; + } + + private ValidatableResponse admin(String action, Map params) { + return iam(ACCOUNT, action, params); + } + + private static ValidatableResponse iam(String accessKeyId, String action, + Map params) { + RequestSpecification spec = given() + .header("Authorization", "AWS4-HMAC-SHA256 Credential=" + accessKeyId + + "/20261012/" + REGION + "/iam/aws4_request, SignedHeaders=host, " + + "Signature=abc") + .contentType("application/x-www-form-urlencoded") + .formParam("Action", action) + .formParam("Version", "2010-05-08"); + params.forEach(spec::formParam); + return spec.when().post("/").then(); + } +}