From 1cbf58a8aec57bd2d405f37f52e5b3ae0c9b83e9 Mon Sep 17 00:00:00 2001 From: Jimmy Liu Jia <91264074+JimmyLiuJia@users.noreply.github.com> Date: Mon, 2 Nov 2026 10:24:43 +0800 Subject: [PATCH] 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 --- docs/services/iam.md | 11 ++-- .../floci/services/iam/IamService.java | 18 +++++- .../IamServiceLinkedRoleIntegrationTest.java | 63 +++++++++++++++++-- tools/aws/regen_iam_shapes.py | 7 ++- 4 files changed, 84 insertions(+), 15 deletions(-) diff --git a/docs/services/iam.md b/docs/services/iam.md index dc77d2e678..d1ba87c572 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -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` diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java index 496e101769..32c34f285c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java +++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java @@ -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()) { diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamServiceLinkedRoleIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamServiceLinkedRoleIntegrationTest.java index f6eb53a6af..b0c0f2987b 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/IamServiceLinkedRoleIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamServiceLinkedRoleIntegrationTest.java @@ -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) diff --git a/tools/aws/regen_iam_shapes.py b/tools/aws/regen_iam_shapes.py index 969a7761ab..db26fe711c 100644 --- a/tools/aws/regen_iam_shapes.py +++ b/tools/aws/regen_iam_shapes.py @@ -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`,