From 6ff7b0fdd951f6b78b9e0292caaab9b2a5b91180 Mon Sep 17 00:00:00 2001 From: Allen Sanborn Date: Sat, 28 Nov 2026 07:14:58 -0600 Subject: [PATCH] fix(iam): match AWS wording for missing and duplicate entity errors (#9098) IAM's wording for missing and duplicate entities now matches what AWS answers. A duplicate CreatePolicy is "A policy called already exists. Duplicate names are not allowed.", and a permissions boundary that names no policy, on CreateUser, CreateRole, PutUserPermissionsBoundary or PutRolePermissionsBoundary, answers "Scope ARN: does not exist or is not attachable.". A version id that names nothing on a policy that exists is named inside the attach sentence on GetPolicyVersion, DeletePolicyVersion and SetDefaultPolicyVersion, inline policies read "The user|group|role policy with name cannot be found.", and instance profiles read "Instance Profile ...". The wording tests share IamWordingRequests, which registers every create with PartitionCleanup. --- docs/services/iam.md | 16 ++- .../io/floci/aws/services/iam/IamService.java | 54 ++++---- .../iam/IamEntityMessagesIntegrationTest.java | 69 +++++++++++ ...mPolicyNotFoundWordingIntegrationTest.java | 115 +++++------------- .../aws/services/iam/IamWordingRequests.java | 100 +++++++++++++++ 5 files changed, 240 insertions(+), 114 deletions(-) create mode 100644 src/test/java/io/floci/aws/services/iam/IamEntityMessagesIntegrationTest.java create mode 100644 src/test/java/io/floci/aws/services/iam/IamWordingRequests.java diff --git a/docs/services/iam.md b/docs/services/iam.md index e352b195ad..de7aaa27a2 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -503,14 +503,18 @@ group or role. `ListEntitiesForPolicy` answers the second, on 2026-10-25, and so exist or is not attachable.` The attach operations answer the second as well, which is where an AWS-managed name under the wrong path lands. +A permissions boundary that names no policy answers a third sentence, `Scope ARN: does not +exist or is not attachable.`, and a version id that names nothing on a policy that exists is +named inside the attach sentence, `Policy version does not exist or is not +attachable.` (`GetPolicyVersion`, `DeletePolicyVersion`, `SetDefaultPolicyVersion`), both asserted +by the AWS-verified evaluation suite. A duplicate `CreatePolicy` is `A policy called already +exists. Duplicate names are not allowed.`; inline policies read `The user|group|role policy with +name cannot be found.`; instance profiles are `Instance Profile ...`. + The operations no recording covers answer with their own family, which is this emulator's reading rather than a measurement. `UntagPolicy` and `ListPolicyTags` read with `TagPolicy`, and -`DeletePolicyVersion`, `SetDefaultPolicyVersion` and a permissions boundary that names no policy -read with the attach family. `DeletePolicy` reads with `GetPolicy`. One case stays as it was -because nothing records it and no family settles it: a version id that names nothing on a policy -that exists keeps `Policy version does not exist.`, which is about the version rather than the -ARN. A recording for any of those moves that operation alone, the sentences being sited per -operation in `IamService.PolicyNotFound`. +`DeletePolicy` reads with `GetPolicy`. A recording for any of those moves that operation alone, +the sentences being sited per operation in `IamService.PolicyNotFound`. ### Three rules a policy document is held to 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 5ba61796b5..83cd84ed7b 100644 --- a/src/main/java/io/floci/aws/services/iam/IamService.java +++ b/src/main/java/io/floci/aws/services/iam/IamService.java @@ -1022,7 +1022,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider { public IamUser createUser(String userName, String path, String permissionsBoundaryArn) { if (permissionsBoundaryArn != null) { permissionsBoundaryArn = - requirePolicy(permissionsBoundaryArn, PolicyNotFound.ATTACHABLE).getArn(); + requirePolicy(permissionsBoundaryArn, PolicyNotFound.BOUNDARY).getArn(); } synchronized (resourceNameLock) { if (containsNameIgnoreCase(users, IamUser::getUserName, userName)) { @@ -2021,7 +2021,8 @@ public class IamService implements SessionAccountLookup, ResourceProvider { .anyMatch(existingName -> existingName != null && existingName.equalsIgnoreCase(policyName)); if (nameTaken) { throw new AwsException("EntityAlreadyExists", - "Policy " + arn + " already exists.", 409); + "A policy called " + policyName + + " already exists. Duplicate names are not allowed.", 409); } String policyId = "ANPA" + randomId(16); IamPolicy policy = new IamPolicy(policyId, policyName, normalizedPath, arn, description, document); @@ -2046,19 +2047,22 @@ public class IamService implements SessionAccountLookup, ResourceProvider { } /** - * The two sentences IAM answers for a managed policy ARN it cannot resolve. They are not + * The three sentences IAM answers for a managed policy ARN it cannot resolve. They are not * interchangeable and the operation decides which it answers, each recorded against a real * account: {@code GetPolicy} and {@code TagPolicy} answer {@link #PLAIN}, recorded 2026-10-26, * as do the three detach operations, recorded 2026-10-29, while the attach family answers * {@link #ATTACHABLE}, recorded on {@code ListEntitiesForPolicy} 2026-10-25 and on * {@code CreatePolicyVersion}, {@code GetPolicyVersion} and {@code ListPolicyVersions} - * 2026-10-29. The clause is what tells the two apart: one says the ARN names nothing, the - * other that it names nothing this call could attach, and AWS answers the second for both of - * its own cases deliberately, so a caller cannot tell them apart either. + * 2026-10-29. The clause is what tells the first two apart: one says the ARN names nothing, + * the other that it names nothing this call could attach, and AWS answers the second for both + * of its own cases deliberately, so a caller cannot tell them apart either. {@link #BOUNDARY} + * is the third, answered for a permissions boundary on a user or role, and a version id that + * names nothing on a policy that exists is named inside {@link #ATTACHABLE}; the AWS Emulator + * Evaluation Suite asserts both wordings. * *

The operations no recording covers answer with their own family, which is this emulator's - * reading rather than a measurement: the tag readers with {@code TagPolicy}, the remaining - * version operations and a permissions boundary with the attach family. + * reading rather than a measurement: the tag readers with {@code TagPolicy}, and + * {@code DeletePolicy} with {@code GetPolicy}. * {@code docs/services/iam.md} records which is which and what a later recording moves. */ private enum PolicyNotFound { @@ -2067,7 +2071,10 @@ public class IamService implements SessionAccountLookup, ResourceProvider { PLAIN("Policy %s was not found."), /** {@code Policy does not exist or is not attachable.} */ - ATTACHABLE("Policy %s does not exist or is not attachable."); + ATTACHABLE("Policy %s does not exist or is not attachable."), + + /** {@code Scope ARN: does not exist or is not attachable.}, a permissions boundary. */ + BOUNDARY("Scope ARN: %s does not exist or is not attachable."); private final String template; @@ -2143,7 +2150,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider { private String requireBoundaryPolicy(String permissionsBoundaryArn) { return registeredPartnerBoundary(permissionsBoundaryArn) ? permissionsBoundaryArn - : requirePolicy(permissionsBoundaryArn, PolicyNotFound.ATTACHABLE).getArn(); + : requirePolicy(permissionsBoundaryArn, PolicyNotFound.BOUNDARY).getArn(); } private int managedPolicyAttachmentCount(String policyArn) { @@ -2510,8 +2517,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider { .orElseThrow(() -> PolicyNotFound.ATTACHABLE.of(policyArn, versionId)); PolicyVersion version = policy.getVersions().get(versionId); if (version == null) { - throw new AwsException("NoSuchEntity", - "Policy version " + versionId + " does not exist.", 404); + throw PolicyNotFound.ATTACHABLE.of(policyArn, versionId); } return version; } @@ -2527,8 +2533,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider { Map versions = policy.getVersions(); synchronized (versions) { if (!versions.containsKey(versionId)) { - throw new AwsException("NoSuchEntity", - "Policy version " + versionId + " does not exist.", 404); + throw PolicyNotFound.ATTACHABLE.of(policyArn, versionId); } versions.remove(versionId); } @@ -2551,8 +2556,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider { Map versions = policy.getVersions(); synchronized (versions) { if (!versions.containsKey(versionId)) { - throw new AwsException("NoSuchEntity", - "Policy version " + versionId + " does not exist.", 404); + throw PolicyNotFound.ATTACHABLE.of(policyArn, versionId); } versions.values().forEach(v -> v.setDefaultVersion(false)); versions.get(versionId).setDefaultVersion(true); @@ -2704,7 +2708,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider { .map(arn -> requirePolicy(arn, PolicyNotFound.ATTACHABLE)) .map(IamPolicy::getArn).distinct().toList(); if (boundaryArn != null) { - boundaryArn = requirePolicy(boundaryArn, PolicyNotFound.ATTACHABLE).getArn(); + boundaryArn = requirePolicy(boundaryArn, PolicyNotFound.BOUNDARY).getArn(); } Map checkedInline = new LinkedHashMap<>(); for (Map.Entry entry : inlinePolicies.entrySet()) { @@ -2761,7 +2765,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider { String doc = user.getInlinePolicies().get(policyName); if (doc == null) { throw new AwsException("NoSuchEntity", - "Policy " + policyName + " not found for user " + userName + ".", 404); + "The user policy with name " + policyName + " cannot be found.", 404); } return doc; } @@ -2771,7 +2775,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider { IamUser user = getUser(userName); if (user.getInlinePolicies().remove(policyName) == null) { throw new AwsException("NoSuchEntity", - "Policy " + policyName + " not found for user " + userName + ".", 404); + "The user policy with name " + policyName + " cannot be found.", 404); } invalidateInlinePolicyOwner("User", user.getUserId(), policyName); users.put(userName, user); @@ -2806,7 +2810,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider { String doc = group.getInlinePolicies().get(policyName); if (doc == null) { throw new AwsException("NoSuchEntity", - "Policy " + policyName + " not found for group " + groupName + ".", 404); + "The group policy with name " + policyName + " cannot be found.", 404); } return doc; } @@ -2816,7 +2820,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider { IamGroup group = getGroup(groupName); if (group.getInlinePolicies().remove(policyName) == null) { throw new AwsException("NoSuchEntity", - "Policy " + policyName + " not found for group " + groupName + ".", 404); + "The group policy with name " + policyName + " cannot be found.", 404); } invalidateInlinePolicyOwner("Group", group.getGroupId(), policyName); groups.put(groupName, group); @@ -3029,7 +3033,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider { String doc = role.getInlinePolicies().get(policyName); if (doc == null) { throw new AwsException("NoSuchEntity", - "Policy " + policyName + " not found for role " + roleName + ".", 404); + "The role policy with name " + policyName + " cannot be found.", 404); } return doc; } @@ -3041,7 +3045,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider { requireNotServiceLinked(role, roleName); if (role.getInlinePolicies().remove(policyName) == null) { throw new AwsException("NoSuchEntity", - "Policy " + policyName + " not found for role " + roleName + ".", 404); + "The role policy with name " + policyName + " cannot be found.", 404); } invalidateInlinePolicyOwner("Role", role.getRoleId(), policyName); roles.put(roleName, role); @@ -3179,7 +3183,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider { if (containsNameIgnoreCase( instanceProfiles, InstanceProfile::getInstanceProfileName, instanceProfileName)) { throw new AwsException("EntityAlreadyExists", - "Instance profile " + instanceProfileName + " already exists.", 409); + "Instance Profile " + instanceProfileName + " already exists.", 409); } String profileId = "AIPA" + randomId(16); String normalizedPath = normalizePath(path); @@ -3219,7 +3223,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider { public InstanceProfile getInstanceProfile(String instanceProfileName) { return instanceProfiles.get(instanceProfileName) .orElseThrow(() -> new AwsException("NoSuchEntity", - "Instance profile " + instanceProfileName + " cannot be found.", 404)); + "Instance Profile " + instanceProfileName + " cannot be found.", 404)); } public void deleteInstanceProfile(String instanceProfileName) { diff --git a/src/test/java/io/floci/aws/services/iam/IamEntityMessagesIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/IamEntityMessagesIntegrationTest.java new file mode 100644 index 0000000000..8d934a0e3c --- /dev/null +++ b/src/test/java/io/floci/aws/services/iam/IamEntityMessagesIntegrationTest.java @@ -0,0 +1,69 @@ +package io.floci.aws.services.iam; + +import io.floci.aws.testing.PartitionCleanup; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import static io.floci.aws.services.iam.IamWordingRequests.DOCUMENT; +import static io.floci.aws.services.iam.IamWordingRequests.expectError; +import static io.floci.aws.services.iam.IamWordingRequests.expectNotFound; +import static io.floci.aws.services.iam.IamWordingRequests.iam; +import static io.floci.aws.services.iam.IamWordingRequests.unique; + +/** + * Pins the exact error codes and messages IAM returns for duplicate policies, inline policies and instance + * profiles; the not-found wording of managed policies, their versions and permissions boundaries is pinned in + * {@link IamPolicyNotFoundWordingIntegrationTest}. + */ +@QuarkusTest +class IamEntityMessagesIntegrationTest { + + @RegisterExtension + final PartitionCleanup cleanup = new PartitionCleanup(); + + private final IamWordingRequests requests = new IamWordingRequests(cleanup); + + @Test + void aDuplicatePolicyNameIsRefused() { + String name = unique("duplicate-policy"); + requests.createPolicy(name); + expectError(iam("CreatePolicy").formParam("PolicyName", name).formParam("PolicyDocument", DOCUMENT), + 409, "EntityAlreadyExists", + "A policy called " + name + " already exists. Duplicate names are not allowed."); + } + + @Test + void inlinePolicyMessages() { + String user = requests.createUser(); + String group = requests.createGroup(); + String role = requests.createRole(); + for (String action : new String[] {"GetUserPolicy", "DeleteUserPolicy"}) { + expectNotFound(iam(action).formParam("UserName", user).formParam("PolicyName", "nope"), + "The user policy with name nope cannot be found."); + } + for (String action : new String[] {"GetGroupPolicy", "DeleteGroupPolicy"}) { + expectNotFound(iam(action).formParam("GroupName", group).formParam("PolicyName", "nope"), + "The group policy with name nope cannot be found."); + } + for (String action : new String[] {"GetRolePolicy", "DeleteRolePolicy"}) { + expectNotFound(iam(action).formParam("RoleName", role).formParam("PolicyName", "nope"), + "The role policy with name nope cannot be found."); + } + } + + @Test + void instanceProfileMessages() { + String name = requests.createInstanceProfile(); + expectError(iam("CreateInstanceProfile").formParam("InstanceProfileName", name), + 409, "EntityAlreadyExists", "Instance Profile " + name + " already exists."); + String missing = unique("missing-profile"); + String message = "Instance Profile " + missing + " cannot be found."; + expectNotFound(iam("GetInstanceProfile").formParam("InstanceProfileName", missing), message); + expectNotFound(iam("DeleteInstanceProfile").formParam("InstanceProfileName", missing), message); + expectNotFound(iam("TagInstanceProfile").formParam("InstanceProfileName", missing) + .formParam("Tags.member.1.Key", "k").formParam("Tags.member.1.Value", "v"), message); + expectNotFound(iam("UntagInstanceProfile").formParam("InstanceProfileName", missing) + .formParam("TagKeys.member.1", "k"), message); + } +} diff --git a/src/test/java/io/floci/aws/services/iam/IamPolicyNotFoundWordingIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/IamPolicyNotFoundWordingIntegrationTest.java index 010a0efb2c..a152831c51 100644 --- a/src/test/java/io/floci/aws/services/iam/IamPolicyNotFoundWordingIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/iam/IamPolicyNotFoundWordingIntegrationTest.java @@ -2,17 +2,19 @@ package io.floci.aws.services.iam; import io.floci.aws.testing.PartitionCleanup; import io.quarkus.test.junit.QuarkusTest; -import io.restassured.specification.RequestSpecification; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; -import static io.restassured.RestAssured.given; -import static org.hamcrest.Matchers.equalTo; +import static io.floci.aws.services.iam.IamWordingRequests.DOCUMENT; +import static io.floci.aws.services.iam.IamWordingRequests.expectNotFound; +import static io.floci.aws.services.iam.IamWordingRequests.iam; +import static io.floci.aws.services.iam.IamWordingRequests.unique; /** - * The two sentences IAM answers for a managed policy ARN it cannot resolve, one per operation: - * {@code was not found.} where the ARN names nothing, and {@code does not exist or is not - * attachable.} where it names nothing the call could attach. The recorded operations are pinned + * The sentences IAM answers for a managed policy ARN it cannot resolve, one per operation: + * {@code was not found.} where the ARN names nothing, {@code does not exist or is not + * attachable.} where it names nothing the call could attach, and {@code Scope ARN: does not + * exist or is not attachable.} for a permissions boundary. The recorded operations are pinned * here with the ones that follow them, and so is the clause the detach paths used to add. * *

{@code docs/services/iam.md} carries the recordings and the readings. @@ -20,12 +22,6 @@ import static org.hamcrest.Matchers.equalTo; @QuarkusTest class IamPolicyNotFoundWordingIntegrationTest { - private static final String IAM_AUTH = - "AWS4-HMAC-SHA256 Credential=test/20261118/us-east-1/iam/aws4_request"; - - private static final String CODE = "ErrorResponse.Error.Code"; - private static final String MESSAGE = "ErrorResponse.Error.Message"; - /** Well formed, this account's, and naming no policy. */ private static final String ABSENT = "arn:aws:iam::000000000000:policy/not-found-wording"; @@ -34,44 +30,10 @@ class IamPolicyNotFoundWordingIntegrationTest { private static final String ATTACHABLE = "Policy " + ABSENT + " does not exist or is not attachable."; - private static final String DOCUMENT = "{\"Version\":\"2012-10-17\",\"Statement\":" - + "[{\"Effect\":\"Allow\",\"Action\":\"s3:GetObject\",\"Resource\":\"*\"}]}"; - @RegisterExtension final PartitionCleanup cleanup = new PartitionCleanup(); - private static RequestSpecification iam(String action) { - return given().header("Authorization", IAM_AUTH).formParam("Action", action); - } - - private static String unique(String prefix) { - return prefix + "-" + System.nanoTime(); - } - - private static void expectNotFound(RequestSpecification request, String message) { - request.when().post("/").then().statusCode(404) - .body(CODE, equalTo("NoSuchEntity")) - .body(MESSAGE, equalTo(message)); - } - - private String createUser() { - String userName = unique("not-found-user"); - iam("CreateUser").formParam("UserName", userName).when().post("/").then().statusCode(200); - cleanup.register(() -> iam("DeleteUser").formParam("UserName", userName) - .when().post("/").then().statusCode(200)); - return userName; - } - - private String createPolicy() { - String policyName = unique("not-found-policy"); - String arn = iam("CreatePolicy").formParam("PolicyName", policyName) - .formParam("PolicyDocument", DOCUMENT) - .when().post("/").then().statusCode(200) - .extract().path("CreatePolicyResponse.CreatePolicyResult.Policy.Arn"); - cleanup.register(() -> iam("DeletePolicy").formParam("PolicyArn", arn) - .when().post("/").then().statusCode(200)); - return arn; - } + private final IamWordingRequests requests = new IamWordingRequests(cleanup); /** The two reads the plain sentence is recorded on, and the delete that reads with them. */ @Test @@ -87,7 +49,7 @@ class IamPolicyNotFoundWordingIntegrationTest { /** The attachment family, and the version operations that read with it. */ @Test void theAttachmentFamilyAnswersTheSentenceCarryingTheClause() { - expectNotFound(iam("AttachUserPolicy").formParam("UserName", createUser()) + expectNotFound(iam("AttachUserPolicy").formParam("UserName", requests.createUser()) .formParam("PolicyArn", ABSENT), ATTACHABLE); expectNotFound(iam("ListEntitiesForPolicy").formParam("PolicyArn", ABSENT), ATTACHABLE); expectNotFound(iam("ListPolicyVersions").formParam("PolicyArn", ABSENT), ATTACHABLE); @@ -111,62 +73,49 @@ class IamPolicyNotFoundWordingIntegrationTest { */ @Test void detachingAPolicyThatIsNotAttachedNamesNoEntity() { - String arn = createPolicy(); - String userName = createUser(); - expectNotFound(iam("DetachUserPolicy").formParam("UserName", userName) + String arn = requests.createPolicy(); + expectNotFound(iam("DetachUserPolicy").formParam("UserName", requests.createUser()) .formParam("PolicyArn", arn), "Policy " + arn + " was not found."); - - String groupName = unique("not-found-group"); - iam("CreateGroup").formParam("GroupName", groupName).when().post("/").then().statusCode(200); - cleanup.register(() -> iam("DeleteGroup").formParam("GroupName", groupName) - .when().post("/").then().statusCode(200)); - expectNotFound(iam("DetachGroupPolicy").formParam("GroupName", groupName) + expectNotFound(iam("DetachGroupPolicy").formParam("GroupName", requests.createGroup()) .formParam("PolicyArn", arn), "Policy " + arn + " was not found."); - - String roleName = unique("not-found-role"); - iam("CreateRole").formParam("RoleName", roleName) - .formParam("AssumeRolePolicyDocument", "{\"Version\":\"2012-10-17\",\"Statement\":" - + "[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"ec2.amazonaws.com\"}," - + "\"Action\":\"sts:AssumeRole\"}]}") - .when().post("/").then().statusCode(200); - cleanup.register(() -> iam("DeleteRole").formParam("RoleName", roleName) - .when().post("/").then().statusCode(200)); - expectNotFound(iam("DetachRolePolicy").formParam("RoleName", roleName) + expectNotFound(iam("DetachRolePolicy").formParam("RoleName", requests.createRole()) .formParam("PolicyArn", arn), "Policy " + arn + " was not found."); } /** Detaching an ARN that names nothing at all answers the same sentence. */ @Test void detachingAnAbsentPolicyAnswersTheSameSentence() { - expectNotFound(iam("DetachUserPolicy").formParam("UserName", createUser()) + expectNotFound(iam("DetachUserPolicy").formParam("UserName", requests.createUser()) .formParam("PolicyArn", ABSENT), PLAIN); } - /** - * A permissions boundary that names no policy reads with the attach family, which is the - * reading rather than a recording. - */ + /** A permissions boundary that names no policy answers the Scope ARN sentence, as recorded. */ @Test - void aPermissionsBoundaryThatNamesNoPolicyReadsWithTheAttachFamily() { + void aPermissionsBoundaryThatNamesNoPolicyAnswersTheScopeSentence() { + String scope = "Scope ARN: " + ABSENT + " does not exist or is not attachable."; expectNotFound(iam("CreateUser").formParam("UserName", unique("boundary-user")) - .formParam("PermissionsBoundary", ABSENT), ATTACHABLE); + .formParam("PermissionsBoundary", ABSENT), scope); + expectNotFound(iam("PutUserPermissionsBoundary").formParam("UserName", requests.createUser()) + .formParam("PermissionsBoundary", ABSENT), scope); + expectNotFound(iam("PutRolePermissionsBoundary").formParam("RoleName", requests.createRole()) + .formParam("PermissionsBoundary", ABSENT), scope); } - /** - * The one case left as it was: a version id that names nothing on a policy that exists is - * about the version rather than the ARN, and nothing records it. - */ + /** A version id that names nothing on a policy that exists is named inside the attach sentence. */ @Test - void aVersionThatNamesNothingOnAPolicyThatExistsIsUnchanged() { - expectNotFound(iam("GetPolicyVersion").formParam("PolicyArn", createPolicy()) - .formParam("VersionId", "v7"), "Policy version v7 does not exist."); + void aVersionThatNamesNothingOnAPolicyThatExistsAnswersTheAttachSentence() { + String arn = requests.createPolicy(); + for (String action : new String[] {"GetPolicyVersion", "DeletePolicyVersion", "SetDefaultPolicyVersion"}) { + expectNotFound(iam(action).formParam("PolicyArn", arn).formParam("VersionId", "v7"), + "Policy " + arn + " version v7 does not exist or is not attachable."); + } } /** The control: a policy that exists still reads, attaches and detaches. */ @Test void aPolicyThatExistsIsUntouched() { - String arn = createPolicy(); - String userName = createUser(); + String arn = requests.createPolicy(); + String userName = requests.createUser(); iam("GetPolicy").formParam("PolicyArn", arn).when().post("/").then().statusCode(200); iam("AttachUserPolicy").formParam("UserName", userName).formParam("PolicyArn", arn) .when().post("/").then().statusCode(200); diff --git a/src/test/java/io/floci/aws/services/iam/IamWordingRequests.java b/src/test/java/io/floci/aws/services/iam/IamWordingRequests.java new file mode 100644 index 0000000000..678e543fff --- /dev/null +++ b/src/test/java/io/floci/aws/services/iam/IamWordingRequests.java @@ -0,0 +1,100 @@ +package io.floci.aws.services.iam; + +import io.floci.aws.testing.PartitionCleanup; +import io.restassured.specification.RequestSpecification; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; + +/** + * IAM Query requests for the tests that pin IAM's error wording. RestAssured is used deliberately so the + * raw wire messages are asserted, not SDK-rewritten ones. Each create registers its delete with the test's + * {@link PartitionCleanup}, so nothing a test makes outlives it in the shared emulator. + */ +final class IamWordingRequests { + + private static final String IAM_AUTH = + "AWS4-HMAC-SHA256 Credential=test/20261118/us-east-1/iam/aws4_request"; + + private static final String CODE = "ErrorResponse.Error.Code"; + private static final String MESSAGE = "ErrorResponse.Error.Message"; + + static final String DOCUMENT = "{\"Version\":\"2012-10-17\",\"Statement\":" + + "[{\"Effect\":\"Allow\",\"Action\":\"s3:GetObject\",\"Resource\":\"*\"}]}"; + + private static final String TRUST = "{\"Version\":\"2012-10-17\",\"Statement\":" + + "[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"ec2.amazonaws.com\"}," + + "\"Action\":\"sts:AssumeRole\"}]}"; + + private final PartitionCleanup cleanup; + + IamWordingRequests(PartitionCleanup cleanup) { + this.cleanup = cleanup; + } + + static RequestSpecification iam(String action) { + return given().header("Authorization", IAM_AUTH).formParam("Action", action); + } + + static String unique(String prefix) { + return prefix + "-" + System.nanoTime(); + } + + static void expectError(RequestSpecification request, int status, String code, String message) { + request.when().post("/").then().statusCode(status) + .body(CODE, equalTo(code)) + .body(MESSAGE, equalTo(message)); + } + + static void expectNotFound(RequestSpecification request, String message) { + expectError(request, 404, "NoSuchEntity", message); + } + + String createUser() { + String userName = unique("wording-user"); + iam("CreateUser").formParam("UserName", userName).when().post("/").then().statusCode(200); + cleanup.register(() -> iam("DeleteUser").formParam("UserName", userName) + .when().post("/").then().statusCode(200)); + return userName; + } + + String createGroup() { + String groupName = unique("wording-group"); + iam("CreateGroup").formParam("GroupName", groupName).when().post("/").then().statusCode(200); + cleanup.register(() -> iam("DeleteGroup").formParam("GroupName", groupName) + .when().post("/").then().statusCode(200)); + return groupName; + } + + String createRole() { + String roleName = unique("wording-role"); + iam("CreateRole").formParam("RoleName", roleName).formParam("AssumeRolePolicyDocument", TRUST) + .when().post("/").then().statusCode(200); + cleanup.register(() -> iam("DeleteRole").formParam("RoleName", roleName) + .when().post("/").then().statusCode(200)); + return roleName; + } + + String createPolicy() { + return createPolicy(unique("wording-policy")); + } + + String createPolicy(String policyName) { + String arn = iam("CreatePolicy").formParam("PolicyName", policyName) + .formParam("PolicyDocument", DOCUMENT) + .when().post("/").then().statusCode(200) + .extract().path("CreatePolicyResponse.CreatePolicyResult.Policy.Arn"); + cleanup.register(() -> iam("DeletePolicy").formParam("PolicyArn", arn) + .when().post("/").then().statusCode(200)); + return arn; + } + + String createInstanceProfile() { + String instanceProfileName = unique("wording-profile"); + iam("CreateInstanceProfile").formParam("InstanceProfileName", instanceProfileName) + .when().post("/").then().statusCode(200); + cleanup.register(() -> iam("DeleteInstanceProfile").formParam("InstanceProfileName", instanceProfileName) + .when().post("/").then().statusCode(200)); + return instanceProfileName; + } +}