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

@@ -173,6 +173,13 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
private static final int ROLE_NAME_MAX_LENGTH = 64;
/** groupNameType / instanceProfileNameType: 1-128 characters of {@code [\w+=,.@-]}. */
private static final Pattern IAM_RESOURCE_NAME_PATTERN = Pattern.compile("[\\w+=,.@-]{1,128}");
/**
* serviceSpecificCredentialId, kept as the model spells it because the pattern is quoted
* back in the error message. AWS checks the format before it looks for the credential.
*/
private static final Pattern SERVICE_CREDENTIAL_ID_PATTERN = Pattern.compile("[\\w]+");
private static final int SERVICE_CREDENTIAL_ID_MIN_LENGTH = 20;
private static final int SERVICE_CREDENTIAL_ID_MAX_LENGTH = 128;
/** pathType: a bare slash, or a slash-delimited run of {@code !}-{@code ~}. */
private static final Pattern IAM_PATH_PATTERN = Pattern.compile("(/)|(/[\\x21-\\x7E]+/)");
private static final int IAM_PATH_MAX_LENGTH = 512;
@@ -727,11 +734,12 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
// order here and in UpdateUser, and nowhere else in more than one, so it cannot deadlock.
synchronized (serviceCredentialLock) {
// AWS lists these among the items to remove first, under their CodeCommit
// name: "Git credentials (DeleteServiceSpecificCredential)".
// name: "Git credentials (DeleteServiceSpecificCredential)". Unlike the
// cases above it has no message of its own: AWS falls back to the generic
// referenced-objects wording here.
if (!userServiceCredentials(userName).isEmpty()) {
throw new AwsException("DeleteConflict",
"Cannot delete entity, must delete service-specific credentials "
+ "first.", 409);
"Cannot delete entity, must remove referenced objects first.", 409);
}
synchronized (sshPublicKeyLock) {
if (!userSshPublicKeys(userName).isEmpty()) {
@@ -2727,6 +2735,24 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
return getServerCertificate(name).getTags();
}
// Credential status, shared by the three operations that take statusType
// =========================================================================
/**
* {@code statusType}, shared by UpdateSigningCertificate, UpdateSSHPublicKey and
* UpdateServiceSpecificCredential. One rendering serves all three because one front end
* validates one shape: the recording is of UpdateServiceSpecificCredential, and AWS names
* neither the offending value nor the permitted set, unlike this message elsewhere in the
* tree. Matched rather than made uniform.
*/
private void requireStatusFromEnum(String status) {
if (status == null || !CREDENTIAL_STATUSES.contains(status)) {
throw new AwsException("ValidationError",
"1 validation error detected: Value at 'status' failed to satisfy "
+ "constraint: Member must satisfy enum value set", 400);
}
}
// Signing certificates
// =========================================================================
@@ -2782,12 +2808,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
}
public void updateSigningCertificate(String userName, String certificateId, String status) {
if (status == null || !CREDENTIAL_STATUSES.contains(status)) {
throw new AwsException("ValidationError",
"Value '" + status + "' at 'status' failed to satisfy constraint: Member must "
+ "satisfy enum value set: [" + String.join(", ",
CREDENTIAL_STATUSES) + "]", 400);
}
requireStatusFromEnum(status);
synchronized (signingCertificateLock) {
SigningCertificate certificate = userSigningCertificate(userName, certificateId);
certificate.setStatus(status);
@@ -2951,12 +2972,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
}
public void updateSshPublicKey(String userName, String keyId, String status) {
if (status == null || !CREDENTIAL_STATUSES.contains(status)) {
throw new AwsException("ValidationError",
"Value '" + status + "' at 'status' failed to satisfy constraint: Member must "
+ "satisfy enum value set: [" + String.join(", ",
CREDENTIAL_STATUSES) + "]", 400);
}
requireStatusFromEnum(status);
synchronized (sshPublicKeyLock) {
SshPublicKey stored = userSshPublicKey(userName, keyId);
stored.setStatus(status);
@@ -3231,12 +3247,8 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
public void updateServiceSpecificCredential(String userName, String credentialId,
String status) {
if (status == null || !CREDENTIAL_STATUSES.contains(status)) {
throw new AwsException("ValidationError",
"Value '" + status + "' at 'status' failed to satisfy constraint: Member must "
+ "satisfy enum value set: [" + String.join(", ",
CREDENTIAL_STATUSES) + "]", 400);
}
requireCredentialIdFormat(credentialId);
requireStatusFromEnum(status);
synchronized (serviceCredentialLock) {
ServiceSpecificCredential credential = userServiceCredential(userName, credentialId);
credential.setStatus(status);
@@ -3250,6 +3262,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
*/
public ServiceSpecificCredential resetServiceSpecificCredential(String userName,
String credentialId) {
requireCredentialIdFormat(credentialId);
synchronized (serviceCredentialLock) {
ServiceSpecificCredential credential = userServiceCredential(userName, credentialId);
if (LONG_TERM_API_KEY_SERVICES.contains(credential.getServiceName())) {
@@ -3263,23 +3276,66 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
}
public void deleteServiceSpecificCredential(String userName, String credentialId) {
requireCredentialIdFormat(credentialId);
synchronized (serviceCredentialLock) {
ServiceSpecificCredential credential = userServiceCredential(userName, credentialId);
serviceCredentials.delete(credential.getServiceSpecificCredentialId());
}
}
/**
* The id has to satisfy the model before anything looks for it: AWS answers a malformed one
* with a {@code ValidationError} naming the constraint it broke, and only a well-formed id
* that matches nothing gets {@code NoSuchEntity}. Different mistakes, different answers.
*
* <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
* way.
*/
private void requireCredentialIdFormat(String credentialId) {
String id = credentialId == null ? "" : credentialId;
List<String> violations = new ArrayList<>();
if (id.length() < SERVICE_CREDENTIAL_ID_MIN_LENGTH
|| id.length() > SERVICE_CREDENTIAL_ID_MAX_LENGTH) {
violations.add("Member must have length less than or equal to "
+ SERVICE_CREDENTIAL_ID_MAX_LENGTH + " and greater than or equal to "
+ SERVICE_CREDENTIAL_ID_MIN_LENGTH);
}
if (!SERVICE_CREDENTIAL_ID_PATTERN.matcher(id).matches()) {
violations.add("Member must satisfy regular expression pattern: [\\w]+");
}
if (violations.isEmpty()) {
return;
}
StringBuilder message = new StringBuilder()
.append(violations.size())
.append(violations.size() == 1 ? " validation error detected: "
: " validation errors detected: ");
for (int i = 0; i < violations.size(); i++) {
if (i > 0) {
message.append("; ");
}
message.append("Value at 'serviceSpecificCredentialId' failed to satisfy constraint: ")
.append(violations.get(i));
}
throw new AwsException("ValidationError", message.toString(), 400);
}
/** AWS's wording for an id that matches the pattern and no credential. */
private AwsException noSuchCredential(String credentialId) {
return new AwsException("NoSuchEntity",
"No such credential " + credentialId + " exists", 404);
}
/** A credential of that id belonging to that user, reported missing when it belongs elsewhere. */
private ServiceSpecificCredential userServiceCredential(String userName, String credentialId) {
getUser(userName); // validates existence
ServiceSpecificCredential credential = serviceCredentials.get(credentialId)
.orElseThrow(() -> new AwsException("NoSuchEntity",
"The Service Specific Credential with id " + credentialId
+ " cannot be found.", 404));
.orElseThrow(() -> noSuchCredential(credentialId));
if (!userName.equals(credential.getUserName())) {
throw new AwsException("NoSuchEntity",
"The Service Specific Credential with id " + credentialId
+ " cannot be found.", 404);
throw noSuchCredential(credentialId);
}
return credential;
}