Light, fluffy, and always free - The AWS Local Emulator alternative
feat(iam): add ListInstanceProfileTags (#4140)
Priamo Germosen2f5aa9970112
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/IamTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/IamTest.javaindex 20a49a9da4..2f51fb5a7f 100644--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/IamTest.java+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/IamTest.java@@ -57,6 +57,8 @@ import software.amazon.awssdk.services.iam.model.ListAttachedUserPoliciesResponsimport software.amazon.awssdk.services.iam.model.ListEntitiesForPolicyResponse;import software.amazon.awssdk.services.iam.model.ListGroupsForUserRequest;import software.amazon.awssdk.services.iam.model.ListGroupsForUserResponse;+import software.amazon.awssdk.services.iam.model.ListInstanceProfileTagsRequest;+import software.amazon.awssdk.services.iam.model.ListInstanceProfileTagsResponse;import software.amazon.awssdk.services.iam.model.ListInstanceProfilesResponse;import software.amazon.awssdk.services.iam.model.ListRolePoliciesRequest;import software.amazon.awssdk.services.iam.model.ListRolePoliciesResponse;@@ -71,7 +73,10 @@ import software.amazon.awssdk.services.iam.model.RemoveRoleFromInstanceProfileReimport software.amazon.awssdk.services.iam.model.RemoveUserFromGroupRequest;import software.amazon.awssdk.services.iam.model.SimulatePrincipalPolicyRequest;import software.amazon.awssdk.services.iam.model.StatusType;+import software.amazon.awssdk.services.iam.model.Tag;+import software.amazon.awssdk.services.iam.model.TagInstanceProfileRequest;import software.amazon.awssdk.services.iam.model.TagUserRequest;+import software.amazon.awssdk.services.iam.model.UntagInstanceProfileRequest;import software.amazon.awssdk.services.iam.model.UntagUserRequest;import software.amazon.awssdk.services.iam.model.UpdateAccessKeyRequest;import software.amazon.awssdk.services.iam.model.UpdateLoginProfileRequest;@@ -540,6 +545,32 @@ class IamTest {.anyMatch(p -> INSTANCE_PROFILE_NAME.equals(p.instanceProfileName()));}+ @Test+ @Order(44)+ void tagInstanceProfile() {+ iam.tagInstanceProfile(TagInstanceProfileRequest.builder()+ .instanceProfileName(INSTANCE_PROFILE_NAME)+ .tags(Tag.builder().key("env").value("sdk-test").build())+ .build());+ }++ @Test+ @Order(45)+ void listInstanceProfileTags() {+ ListInstanceProfileTagsResponse response = iam.listInstanceProfileTags(+ ListInstanceProfileTagsRequest.builder().instanceProfileName(INSTANCE_PROFILE_NAME).build());++ assertThat(response.tags())+ .anyMatch(t -> "env".equals(t.key()));+ }++ @Test+ @Order(46)+ void untagInstanceProfile() {+ iam.untagInstanceProfile(UntagInstanceProfileRequest.builder()+ .instanceProfileName(INSTANCE_PROFILE_NAME).tagKeys("env").build());+ }+// ── Login Profile ──────────────────────────────────────────────────@Testdiff --git a/docs/services/iam.md b/docs/services/iam.mdindex 855db4ba7e..e3776d965c 100644--- a/docs/services/iam.md+++ b/docs/services/iam.md@@ -140,6 +140,7 @@ type.| ListInstanceProfilesForRole | Lists instance profiles associated with a role. || TagInstanceProfile | Adds tags to an instance profile. || UntagInstanceProfile | Removes tags from an instance profile. |+| ListInstanceProfileTags | Lists tags stored for an instance profile. |### Access Keysdiff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.javaindex 3f98365fc0..5248c67c6a 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java@@ -102,6 +102,7 @@ public class AwsQueryController {"TagUser", "UntagUser", "ListUserTags","TagRole", "UntagRole", "ListRoleTags","TagPolicy", "UntagPolicy", "ListPolicyTags",+ "TagInstanceProfile", "UntagInstanceProfile", "ListInstanceProfileTags","CreateLoginProfile", "GetLoginProfile", "DeleteLoginProfile", "UpdateLoginProfile","GenerateCredentialReport", "GetCredentialReport","GetAccountSummary", "GetAccountAuthorizationDetails",diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamQueryHandler.javaindex 43b41b3a17..9b2c370016 100644--- a/src/main/java/io/github/hectorvent/floci/services/iam/IamQueryHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamQueryHandler.java@@ -25,6 +25,7 @@ import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;+import java.util.TreeMap;import java.util.stream.Stream;/**@@ -122,6 +123,7 @@ public class IamQueryHandler {case "UntagRole" -> handleUntagRole(params);case "TagInstanceProfile" -> handleTagInstanceProfile(params);case "UntagInstanceProfile" -> handleUntagInstanceProfile(params);+ case "ListInstanceProfileTags" -> handleListInstanceProfileTags(params);case "ListRoleTags" -> handleListRoleTags(params);// Managed Policies@@ -1522,4 +1524,13 @@ public class IamQueryHandler {iamService.untagInstanceProfile(getParam(params, "InstanceProfileName"), extractTagKeys(params));return Response.ok(AwsQueryResponse.envelopeNoResult("UntagInstanceProfile", AwsNamespaces.IAM)).build();}++ private Response handleListInstanceProfileTags(MultivaluedMap<String, String> params) {+ String instanceProfileName = getParam(params, "InstanceProfileName");+ // AWS documents the result as sorted by tag key.+ Map<String, String> tags = new TreeMap<>(iamService.listInstanceProfileTags(instanceProfileName));+ String result = new XmlBuilder().start("Tags").raw(tagsXml(tags)).end("Tags")+ .elem("IsTruncated", false).build();+ return Response.ok(AwsQueryResponse.envelope("ListInstanceProfileTags", AwsNamespaces.IAM, result)).build();+ }}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.javaindex d6409e5588..2c0bac45f0 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@@ -2633,4 +2633,8 @@ public class IamService implements SessionAccountLookup, ResourceProvider {tagKeys.forEach(profile.getTags()::remove);instanceProfiles.put(instanceProfileName, profile);}++ public Map<String, String> listInstanceProfileTags(String instanceProfileName) {+ return getInstanceProfile(instanceProfileName).getTags();+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/InstanceProfileTagsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/InstanceProfileTagsIntegrationTest.javaindex 6b50326c74..4abadbc268 100644--- a/src/test/java/io/github/hectorvent/floci/services/iam/InstanceProfileTagsIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/iam/InstanceProfileTagsIntegrationTest.java@@ -7,6 +7,8 @@ import java.util.UUID;import static io.restassured.RestAssured.given;import static org.hamcrest.Matchers.containsString;+import static org.hamcrest.Matchers.not;+import static org.junit.jupiter.api.Assertions.assertTrue;/*** Verifies TagInstanceProfile and UntagInstanceProfile wire compatibility:@@ -197,4 +199,124 @@ class InstanceProfileTagsIntegrationTest {.then().statusCode(400).body(containsString("ValidationError"));}++ @Test+ void listInstanceProfileTagsReturnsTagsPreviouslySet() {+ String profile = "list-test-" + UUID.randomUUID().toString().substring(0, 8);+ createProfile(profile);++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "TagInstanceProfile")+ .formParam("InstanceProfileName", profile)+ .formParam("Tags.member.1.Key", "team")+ .formParam("Tags.member.1.Value", "platform")+ .header("Authorization", auth(ACCOUNT, "iam"))+ .when().post("/")+ .then().statusCode(200);++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "ListInstanceProfileTags")+ .formParam("InstanceProfileName", profile)+ .header("Authorization", auth(ACCOUNT, "iam"))+ .when().post("/")+ .then().statusCode(200)+ .body(containsString("<Key>team</Key>"))+ .body(containsString("<Value>platform</Value>"));+ }++ /** AWS documents ListInstanceProfileTags as returning results sorted by tag key. */+ @Test+ void listInstanceProfileTagsAreSortedByKey() {+ String profile = "list-sort-" + UUID.randomUUID().toString().substring(0, 8);+ createProfile(profile);++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "TagInstanceProfile")+ .formParam("InstanceProfileName", profile)+ .formParam("Tags.member.1.Key", "zebra")+ .formParam("Tags.member.1.Value", "1")+ .formParam("Tags.member.2.Key", "alpha")+ .formParam("Tags.member.2.Value", "2")+ .header("Authorization", auth(ACCOUNT, "iam"))+ .when().post("/")+ .then().statusCode(200);++ String body = given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "ListInstanceProfileTags")+ .formParam("InstanceProfileName", profile)+ .header("Authorization", auth(ACCOUNT, "iam"))+ .when().post("/")+ .then().statusCode(200)+ .extract().asString();++ assertTrue(body.indexOf("<Key>alpha</Key>") < body.indexOf("<Key>zebra</Key>"),+ "expected alpha before zebra in: " + body);+ }++ @Test+ void listInstanceProfileTagsOnUntaggedProfileReturnsEmptyList() {+ String profile = "list-empty-" + UUID.randomUUID().toString().substring(0, 8);+ createProfile(profile);++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "ListInstanceProfileTags")+ .formParam("InstanceProfileName", profile)+ .header("Authorization", auth(ACCOUNT, "iam"))+ .when().post("/")+ .then().statusCode(200)+ .body(not(containsString("<Key>")));+ }++ @Test+ void listInstanceProfileTagsRoutedViaActionFallbackWhenAuthHeaderAbsent() {+ // Exercises AwsQueryController.inferServiceFromAction -> IAM_ACTIONS dispatch+ // when no Authorization header is present (no SigV4 credential scope to resolve).+ // The whole scenario stays unauthenticated so the create and the later calls+ // land in the same account context.+ String profile = "list-fallback-" + UUID.randomUUID().toString().substring(0, 8);++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "CreateInstanceProfile")+ .formParam("InstanceProfileName", profile)+ .when().post("/")+ .then().statusCode(200);++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "TagInstanceProfile")+ .formParam("InstanceProfileName", profile)+ .formParam("Tags.member.1.Key", "team")+ .formParam("Tags.member.1.Value", "platform")+ .when().post("/")+ .then().statusCode(200);++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "ListInstanceProfileTags")+ .formParam("InstanceProfileName", profile)+ .when().post("/")+ .then().statusCode(200)+ .body(containsString("ListInstanceProfileTagsResponse"))+ .body(containsString("<Key>team</Key>"));+ }++ @Test+ void listInstanceProfileTagsOnNonexistentProfileReturnsNoSuchEntity() {+ String profile = "list-no-such-" + UUID.randomUUID().toString().substring(0, 8);++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "ListInstanceProfileTags")+ .formParam("InstanceProfileName", profile)+ .header("Authorization", auth(ACCOUNT, "iam"))+ .when().post("/")+ .then().statusCode(404)+ .body(containsString("NoSuchEntity"));+ }}