fix(iam): match service-linked role name validation (#7235)

* fix(iam): match service-linked role name validation

Match the recorded AWS ValidationError for malformed AWSServiceName while preserving the documented length and character constraints.

Refs #6042 (item 1)

* fix(iam): align service-linked role validation
This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-11-01 22:24:43 -04:00
1 parent 268d231690
commit 1cbf58a8ae
4 files changed
+84 -15

No files matched your search

+7 -4
View File
@@ -291,10 +291,13 @@ the emulator rather than by a request, a seeded configuration or a role template
in those same words.
Some refusals stay as this service answers them, and the members behind them are left out of the
vendored shapes so the walk cannot reword them. A service-linked role's principal and suffix answer
`InvalidInput` rather than a validation error at all, and so does an empty `ServiceNamespaces` on
`ListPoliciesGrantingServiceAccess`. An account alias carries AWS's own sentence about its own
bounds, which is why neither alias operation is read at all. For `statusType` on the four credential
vendored shapes so the walk cannot reword them. `CreateServiceLinkedRole` validates its required
`AWSServiceName` through IAM's validation layer: missing, empty, overlong and pattern-invalid values
answer `ValidationError` with the corresponding member constraint. The pattern error follows a
captured AWS response; the other cases follow IAM's validation conventions. `CustomSuffix` remains
`InvalidInput`, as does an empty `ServiceNamespaces` on `ListPoliciesGrantingServiceAccess`. An
account alias carries AWS's own sentence about its own bounds, which is why neither alias operation
is read at all. For `statusType` on the four credential
updates, a missing `Status` answers `Member must not be null`; an invalid value answers `Member must
satisfy enum value set: [Expired, Active, Inactive]` without echoing that value. These wordings
follow the newer AWS capture. `Encoding` on `GetSSHPublicKey`
@@ -1423,9 +1423,21 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
* {@code _}, and that attribute forces replacement, so a name without one never converges.
*/
public IamRole createServiceLinkedRole(String awsServiceName, String customSuffix, String description) {
if (awsServiceName == null || !SERVICE_PRINCIPAL_PATTERN.matcher(awsServiceName).matches()) {
throw new AwsException("InvalidInput",
"AWSServiceName must be 1-128 characters matching [\\w+=,.@-], for example es.amazonaws.com.", 400); // partition-literal: AWS's own message text
if (awsServiceName == null) {
throw SmithyValidationErrors.validationError(SmithyValidationErrors.notNull("aWSServiceName"));
}
String constraint;
if (awsServiceName.isEmpty()) {
constraint = "Member must have length greater than or equal to 1";
} else if (awsServiceName.length() > 128) {
constraint = "Member must have length less than or equal to 128";
} else if (!SERVICE_PRINCIPAL_PATTERN.matcher(awsServiceName).matches()) {
constraint = "Member must satisfy regular expression pattern: [\\w+=.@-]*";
} else {
constraint = null;
}
if (constraint != null) {
throw IamValidationErrors.validationError("aWSServiceName", awsServiceName, constraint);
}
if (customSuffix != null && !customSuffix.isEmpty()
&& !CUSTOM_SUFFIX_PATTERN.matcher(customSuffix).matches()) {
@@ -155,7 +155,10 @@ class IamServiceLinkedRoleIntegrationTest {
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("InvalidInput"));
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo(
"1 validation error detected: Value null at 'aWSServiceName' failed to satisfy constraint: "
+ "Member must not be null"));
}
@Test
@@ -319,12 +322,28 @@ class IamServiceLinkedRoleIntegrationTest {
equalTo("AWSServiceRoleForAutoScaling_CustomResource"));
}
/** AWS constrains AWSServiceName to [\w+=,.@-]{1,128}; anything else must not reach storage. */
@Test
@Order(15)
void aServiceNameWithSpacesMatchesAwsValidationError() {
given()
.formParam("Action", "CreateServiceLinkedRole")
.formParam("AWSServiceName", "not a service principal")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo(
"1 validation error detected: Value at 'aWSServiceName' failed to satisfy constraint: "
+ "Member must satisfy regular expression pattern: [\\w+=.@-]*"));
}
/** The documented name pattern rejects these characters before any role lookup. */
@Test
@Order(15)
void aServiceNameOutsideTheAwsPatternIsRejected() {
for (String bad : new String[]{"x\",\"AWS\":\"*", "a\"b.amazonaws.com", " ", "a/b.amazonaws.com",
"x".repeat(129)}) {
for (String bad : new String[]{"x\",\"AWS\":\"*", "a\"b.amazonaws.com", " ", "a/b.amazonaws.com"}) {
given()
.formParam("Action", "CreateServiceLinkedRole")
.formParam("AWSServiceName", bad)
@@ -333,10 +352,44 @@ class IamServiceLinkedRoleIntegrationTest {
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("InvalidInput"));
.body("ErrorResponse.Error.Code", equalTo("ValidationError"));
}
}
@Test
@Order(15)
void anEmptyServiceNameUsesIamValidationError() {
given()
.formParam("Action", "CreateServiceLinkedRole")
.formParam("AWSServiceName", "")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo(
"1 validation error detected: Value at 'aWSServiceName' failed to satisfy constraint: "
+ "Member must have length greater than or equal to 1"));
}
@Test
@Order(15)
void aServiceNameLongerThanTheDocumentedMaximumIsRejected() {
given()
.formParam("Action", "CreateServiceLinkedRole")
.formParam("AWSServiceName", "x".repeat(129))
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo(
"1 validation error detected: Value at 'aWSServiceName' failed to satisfy constraint: "
+ "Member must have length less than or equal to 128"));
}
/** The deletion task is account-scoped storage, so another account must not resolve it. */
@Test
@Order(16)
+4 -3
View File
@@ -93,9 +93,10 @@ REQUIRED_BY_HAND = (
# Members an operation answers in a wording the walk would move, each with the reason it keeps.
#
# A service-linked role's principal and suffix answer `InvalidInput` rather than a validation error
# at all. `ServiceNamespaces` on `ListPoliciesGrantingServiceAccess` answers `InvalidInput` too
# when the list arrives empty. `statusType` is answered without the offending value or the
# `CreateServiceLinkedRole` keeps its principal and suffix in this hand-written list because the
# service method applies the IAM validation helper to `AWSServiceName`, while `CustomSuffix` still
# answers `InvalidInput`. `ServiceNamespaces` on `ListPoliciesGrantingServiceAccess` answers
# `InvalidInput` too when the list arrives empty. `statusType` is answered without the value or the
# permitted set, on all four operations that take it, which is what the recorded refusal carries,
# and `Encoding` on `GetSSHPublicKey` is answered with the modelled `UnrecognizedPublicKeyEncoding`
# rather than as a constraint at all. The simulators answer an empty `ActionNames`,