diff --git a/docs/services/iam.md b/docs/services/iam.md index c2f34e39ca..9add781560 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -3931,6 +3931,26 @@ Four deviations to be aware of: service-linked mark comes from the action that minted the role rather than from its path. Such a role stays fully modifiable, and `DeleteServiceLinkedRole` answers `NoSuchEntity` for it. +## Service-linked role refusals + +A service-linked role is immutable, and this package refuses eleven actions on one. Only one of +those refusals is recorded, and it is worded differently from the rest: `UpdateRole` changing +`MaxSessionDuration` answers `UnmodifiableEntity` 400 with ` is an AWS service linked +role, max session duration cannot be changed.`, recorded 2026-10-29, where the clause names the +change refused rather than the service the role is linked to. + +The other ten keep this package's own sentence, `Role is a service-linked role for +; request the change through that service.` Nothing records them, the clause AWS would +write for each cannot be inferred from the one that is recorded, and AWS may not refuse all of them +this way at all, so inventing ten clauses would put words in AWS's mouth. A recording for any of +them moves that action alone. The same guard stands on one path no IAM action reaches, the policy +replacement an IAM Identity Center permission set provisions a role through, which is why ten +actions carry the sentence where eleven call sites test the mark. + +The description is not one of the eleven: AWS documents it as editable on any role, and both +`UpdateRole` carrying only a description and `UpdateRoleDescription` are taken here, with the guard +standing on the duration. + ## Partitions IAM is global, so its ARNs take the partition of the region a request is signed for: 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 f61bff75f5..a9e3b8b15c 100644 --- a/src/main/java/io/floci/aws/services/iam/IamService.java +++ b/src/main/java/io/floci/aws/services/iam/IamService.java @@ -1525,11 +1525,40 @@ public class IamService implements SessionAccountLookup, ResourceProvider { * TagRole's reference says the role "can be a regular role or a service-linked role". * Within the IAM API, {@link #deleteServiceLinkedRole} is the only way to remove such a role. */ + /** + * The refusal AWS was recorded answering for the one change this package guards on + * {@code UpdateRole}: {@code is an AWS service linked role, max session duration + * cannot be changed.}, 400 {@code UnmodifiableEntity}, recorded 2026-10-29 with the role's own + * name substituted and the rest fixed. AWS words the clause after the change it refuses rather + * than after the service the role is linked to, which is what tells a caller what to stop + * asking for. + * + *

The other ten actions this package guards carry no recording of their own and keep + * {@link #requireNotServiceLinked}'s sentence, which is this emulator's own: the clause AWS + * would write for each of them is not a thing that can be inferred from this one, and some of + * those actions may not be refused by AWS at all. {@code docs/services/iam.md} says so rather + * than leaving ten invented clauses behind. The same guard also stands on + * {@link #replaceRolePolicies}, which no IAM action reaches, so it is not one of the ten. + */ + private static void requireMaxSessionDurationChangeable(IamRole role, String roleName) { + refuseIfServiceLinked(role, () -> roleName + + " is an AWS service linked role, max session duration cannot be changed."); + } + private static void requireNotServiceLinked(IamRole role, String roleName) { + refuseIfServiceLinked(role, () -> "Role " + roleName + " is a service-linked role for " + + linkedServicePrincipal(role) + "; request the change through that service."); + } + + /** + * The one place the mark is tested, each caller naming the sentence its own site answers, so a + * change to what counts as service-linked or to how it is refused is made once. The sentence is + * built only once the mark is there, since the linked service is read off a path only such a + * role carries. + */ + private static void refuseIfServiceLinked(IamRole role, Supplier refusal) { if (role.isServiceLinkedRole()) { - throw new AwsException("UnmodifiableEntity", - "Role " + roleName + " is a service-linked role for " + linkedServicePrincipal(role) - + "; request the change through that service.", 400); + throw new AwsException("UnmodifiableEntity", refusal.get(), 400); } } @@ -1691,7 +1720,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider { IamRole role = getRole(roleName); requireSameRole(role, roleName, expectedRoleId); if (maxSessionDuration > 0) { - requireNotServiceLinked(role, roleName); + requireMaxSessionDurationChangeable(role, roleName); } if (description != null) { role.setDescription(description); diff --git a/src/test/java/io/floci/aws/services/iam/IamServiceLinkedRoleIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/IamServiceLinkedRoleIntegrationTest.java index cf8c75dd12..818e451832 100644 --- a/src/test/java/io/floci/aws/services/iam/IamServiceLinkedRoleIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/iam/IamServiceLinkedRoleIntegrationTest.java @@ -644,6 +644,32 @@ class IamServiceLinkedRoleIntegrationTest { .statusCode(404); } + /** + * The sentence AWS was recorded answering for the one change this package guards on + * {@code UpdateRole}, with the role's own name and the rest fixed. The other actions guarded + * here carry no recording of their own and keep this emulator's sentence, which is why only + * this one is held to a message. + */ + @Test + @Order(35) + void theDurationRefusalCarriesTheRecordedSentence() { + createServiceLinkedRole("wordingprobe.amazonaws.com"); + String roleName = "AWSServiceRoleForWordingprobe"; + + given() + .formParam("Action", "UpdateRole") + .formParam("RoleName", roleName) + .formParam("MaxSessionDuration", "7200") + .header("Authorization", AUTH_HEADER) + .when() + .post("/") + .then() + .statusCode(400) + .body("ErrorResponse.Error.Code", equalTo("UnmodifiableEntity")) + .body("ErrorResponse.Error.Message", equalTo(roleName + + " is an AWS service linked role, max session duration cannot be changed.")); + } + private static void refusedAsUnmodifiable(String action, String... formParams) { RequestSpecification request = given().header("Authorization", AUTH_HEADER).formParam("Action", action); for (int i = 0; i < formParams.length; i += 2) {