fix(iam): answer AWS's own errors for a malformed service-credential id, and for DeleteUser (#5239)

Three answers on the service-specific credential operations did not match what
AWS sends. All three are recorded against real AWS in LocalStack's IAM snapshots,
under @markers.aws.validated.

A ServiceSpecificCredentialId that does not match the model's [\w]+ was looked up
regardless, so it came back NoSuchEntity 404. AWS checks the format first and
answers ValidationError 400 quoting the pattern, on UpdateServiceSpecificCredential,
ResetServiceSpecificCredential and DeleteServiceSpecificCredential alike. A
malformed id and an unknown one are different mistakes, and they now get
different answers.

An id that does satisfy the pattern and names no credential of that user was
"The Service Specific Credential with id <id> cannot be found." AWS says "No such
credential <id> exists", still NoSuchEntity 404.

DeleteUser named the credential in its refusal: "must delete service-specific
credentials first." This case gets no message of its own on AWS, unlike the
access-key and policy cases beside it, and falls back to the generic "Cannot
delete entity, must remove referenced objects first.", DeleteConflict 409.

The API Reference publishes a length of 20 to 128 on the id as well as the
pattern, so both are now enforced, and an id breaking both is reported as two
validation errors rather than one, which is what the counted prefix is for. Only
the pattern message is recorded; the length wording is the form AWS uses for a
member carrying both bounds, which validateLoginProfilePassword already spells
the same way.

The same snapshots show an invalid Status answered with "1 validation error
detected: Value at 'status' failed to satisfy constraint: Member must satisfy
enum value set", naming neither the offending value nor the permitted set, where
the code named both. statusType is one shape, validated by one front end, and
UpdateSigningCertificate, UpdateSSHPublicKey and UpdateServiceSpecificCredential
all take it, so the recording of one is the rendering of all three. They had the
same message copied three times and now share a helper, placed ahead of all three
rather than inside one family's section. Elsewhere in the tree this message does
carry the permitted set, and those are other services with their own renderings,
so they are left alone.

UpdateSSHPublicKey had no test for a status outside the enum at all, and the two
that did have one asserted only the code, so the message none of them pinned
could have been reverted with the suite still green. All three now assert the
counted prefix and the absence of both the value and the set.

UpdateAccessKey takes the same shape and is further off: "Status must be Active
or Inactive." is not AWS's format at all, it refuses the Expired that statusType
allows, and it validates the status only after looking the key up, so a bad key
and a bad status answer NoSuchEntity where the front end would answer
ValidationError first. That is a behavioural change with its own tests to write,
so it is not folded in here.

Refs #5084
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-07 08:02:43 -05:00
1 parent a927c5aac6
commit 3a8f974554
6 files changed
+251 -32

No files matched your search

@@ -577,7 +577,13 @@ class ServiceSpecificCredentialIntegrationTest {
.formParam("ServiceSpecificCredentialId", id)
.formParam("Status", status)
.when().post("/").then().statusCode(400)
.body(containsString("ValidationError"));
.body(containsString("ValidationError"))
.body(containsString("1 validation error detected"))
.body(containsString("Member must satisfy enum value set"))
// AWS names neither the offending value nor the permitted set. The trailing colon
// is the tell for the set, and "Deleted" for the value.
.body(not(containsString("enum value set:")))
.body(not(containsString("Deleted")));
}
iam("UpdateServiceSpecificCredential")
@@ -597,6 +603,116 @@ class ServiceSpecificCredentialIntegrationTest {
.body(containsString("serviceSpecificCredentialId"));
}
/**
* A malformed id and an unknown one are different mistakes, and AWS answers them differently.
* The id is checked against the model's {@code [\w]+} before anything looks for it, so one
* carrying hyphens is a {@code ValidationError} quoting the pattern, while an id that
* satisfies the pattern and names no credential is {@code NoSuchEntity}. Both are recorded
* against real AWS in LocalStack's validated IAM snapshots, for all three operations.
*/
@Test
void aMalformedCredentialIdIsAValidationErrorRatherThanNotFound() {
String userName = user();
create(userName, CODECOMMIT);
for (String action : List.of("UpdateServiceSpecificCredential",
"ResetServiceSpecificCredential", "DeleteServiceSpecificCredential")) {
iam(action)
.formParam("UserName", userName)
.formParam("ServiceSpecificCredentialId",
"totally-wrong-credential-id-with-hyphens")
.formParam("Status", "Inactive")
.when().post("/").then().statusCode(400)
.body(containsString("ValidationError"))
.body(containsString("serviceSpecificCredentialId"))
.body(containsString("Member must satisfy regular expression pattern"))
.body(not(containsString("NoSuchEntity")));
}
}
/**
* The id's other published constraint is a length of 20 to 128. A short id satisfies
* {@code [\w]+} and still fails, and one that breaks both constraints is reported as two
* validation errors rather than one, which is what the counted prefix is for.
*/
@Test
void anIdOutsideItsPublishedLengthIsAValidationError() {
String userName = user();
create(userName, CODECOMMIT);
for (String id : List.of("tooshort", "W".repeat(129))) {
iam("DeleteServiceSpecificCredential")
.formParam("UserName", userName)
.formParam("ServiceSpecificCredentialId", id)
.when().post("/").then().statusCode(400)
.body(containsString("1 validation error detected"))
.body(containsString("Member must have length less than or equal to 128 and "
+ "greater than or equal to 20"))
.body(not(containsString("NoSuchEntity")));
}
// Short and outside the pattern at once: both constraints are reported.
iam("DeleteServiceSpecificCredential")
.formParam("UserName", userName)
.formParam("ServiceSpecificCredentialId", "has-hyphens")
.when().post("/").then().statusCode(400)
.body(containsString("2 validation errors detected"))
.body(containsString("Member must have length"))
.body(containsString("Member must satisfy regular expression pattern"));
// And an id of exactly the minimum length is accepted as well formed, so the bound is not
// off by one: it reaches the lookup and is reported missing rather than malformed.
iam("DeleteServiceSpecificCredential")
.formParam("UserName", userName)
.formParam("ServiceSpecificCredentialId", "A".repeat(20))
.when().post("/").then().statusCode(404)
.body(containsString("NoSuchEntity"));
}
@Test
void aWellFormedButUnknownCredentialIdIsReportedAsNoSuchCredential() {
String userName = user();
create(userName, CODECOMMIT);
for (String action : List.of("UpdateServiceSpecificCredential",
"ResetServiceSpecificCredential", "DeleteServiceSpecificCredential")) {
iam(action)
.formParam("UserName", userName)
.formParam("ServiceSpecificCredentialId", "satisfiesregexbutstillinvalid")
.formParam("Status", "Inactive")
.when().post("/").then().statusCode(404)
.body(containsString("NoSuchEntity"))
.body(containsString("No such credential satisfiesregexbutstillinvalid exists"))
.body(not(containsString("ValidationError")));
}
}
/**
* A live credential blocks {@code DeleteUser} with 409 {@code DeleteConflict}. This one has no
* message of its own: where access keys and policies each name themselves, AWS falls back to
* the generic referenced-objects wording here.
*/
@Test
void aLiveCredentialBlocksDeletingItsUser() {
String userName = user();
String id = create(userName, CODECOMMIT);
iam("DeleteUser").formParam("UserName", userName)
.when().post("/").then().statusCode(409)
.body(containsString("DeleteConflict"))
.body(containsString("Cannot delete entity, must remove referenced objects first."));
// Remove the credential and the same delete succeeds, so the credential is what blocked it
// rather than anything else the user still owns.
iam("DeleteServiceSpecificCredential")
.formParam("UserName", userName)
.formParam("ServiceSpecificCredentialId", id)
.when().post("/").then().statusCode(200);
iam("DeleteUser").formParam("UserName", userName)
.when().post("/").then().statusCode(200);
}
/**
* Without an Authorization header the Query controller infers the service from the action name
* alone, so these have to be in its IAM set or they fall through to SQS.
@@ -292,7 +292,11 @@ class ServiceSpecificCredentialStateTest {
}
}
/** AWS lists the service-specific credential among the items to remove before a user. */
/**
* AWS lists the service-specific credential among the items to remove before a user. Unlike the
* access-key and policy cases it gets no message of its own: the recorded answer is the generic
* referenced-objects wording, so the message deliberately does not name the credential.
*/
@Test
void aUserWithACredentialCannotBeDeleted() {
IamService service = newService(new InMemoryStorage<>(), ACCOUNT_A);
@@ -303,8 +307,8 @@ class ServiceSpecificCredentialStateTest {
AwsException refused = assertThrows(AwsException.class,
() -> service.deleteUser("blocked-user"));
assertEquals("DeleteConflict", refused.getErrorCode());
assertTrue(refused.getMessage().contains("service-specific credential"),
"the message should name what is in the way: " + refused.getMessage());
assertEquals("Cannot delete entity, must remove referenced objects first.",
refused.getMessage());
service.deleteServiceSpecificCredential("blocked-user", id);
service.deleteUser("blocked-user");
@@ -139,7 +139,13 @@ class SigningCertificateIntegrationTest {
.formParam("CertificateId", id)
.formParam("Status", "Revoked")
.when().post("/").then().statusCode(400)
.body(containsString("ValidationError"));
.body(containsString("ValidationError"))
// statusType is one shape across the three operations that take it, and AWS renders it
// without the offending value or the permitted set.
.body(containsString("1 validation error detected"))
.body(containsString("Member must satisfy enum value set"))
.body(not(containsString("enum value set:")))
.body(not(containsString("Revoked")));
}
@Test
@@ -217,6 +217,30 @@ class SshPublicKeyIntegrationTest {
}
}
/**
* And nothing outside the enum. {@code statusType} is one shape across the three operations
* that take it, so this is the same answer AWS gives on UpdateServiceSpecificCredential, where
* it was recorded: the offending value and the permitted set are both absent.
*/
@Test
void theStatusTakesNothingOutsideTheEnum() {
String userName = user();
String id = upload(userName, SshPublicKeys.toOpenSsh(generated()));
for (String status : List.of("inactive", "Revoked", "")) {
iam("UpdateSSHPublicKey")
.formParam("UserName", userName)
.formParam("SSHPublicKeyId", id)
.formParam("Status", status)
.when().post("/").then().statusCode(400)
.body(containsString("ValidationError"))
.body(containsString("1 validation error detected"))
.body(containsString("Member must satisfy enum value set"))
.body(not(containsString("enum value set:")))
.body(not(containsString("Revoked")));
}
}
@Test
void deleteRemovesIt() {
String userName = user();