Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(iam): include policy description in GetPolicy/ListPolicies/CreatePolicy responses (#2155)

sheldon-nadarajah-sft1150cfc657d5
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.java
index f7b0497457..5d5f49d619 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
@@ -450,13 +450,13 @@ public class IamQueryHandler {
String document = getParam(params, "PolicyDocument");
Map<String, String> tags = extractTags(params);
IamPolicy policy = iamService.createPolicy(policyName, path, description, document, tags);
- String result = new XmlBuilder().start("Policy").raw(policyXml(policy)).end("Policy").build();
+ String result = new XmlBuilder().start("Policy").raw(policyXml(policy, true)).end("Policy").build();
return Response.ok(AwsQueryResponse.envelope("CreatePolicy", AwsNamespaces.IAM, result)).build();
}
private Response handleGetPolicy(MultivaluedMap<String, String> params) {
IamPolicy policy = iamService.getPolicy(getParam(params, "PolicyArn"));
- String result = new XmlBuilder().start("Policy").raw(policyXml(policy)).end("Policy").build();
+ String result = new XmlBuilder().start("Policy").raw(policyXml(policy, true)).end("Policy").build();
return Response.ok(AwsQueryResponse.envelope("GetPolicy", AwsNamespaces.IAM, result)).build();
}
@@ -470,7 +470,7 @@ public class IamQueryHandler {
getParam(params, "Scope"), getParam(params, "PathPrefix"));
var xml = new XmlBuilder().start("Policies");
for (IamPolicy p : policyList) {
- xml.start("member").raw(policyXml(p)).end("member");
+ xml.start("member").raw(policyXml(p, false)).end("member");
}
xml.end("Policies").elem("IsTruncated", false);
return Response.ok(AwsQueryResponse.envelope("ListPolicies", AwsNamespaces.IAM, xml.build())).build();
@@ -906,7 +906,13 @@ public class IamQueryHandler {
.build();
}
- private String policyXml(IamPolicy p) {
+ /**
+ * {@code includeDescription} is per-operation, not per-policy: AWS's own {@code Policy} model
+ * documents that {@code Description} "is included in the response to the GetPolicy operation.
+ * It is not included in the response to the ListPolicies operation" — CreatePolicy documents
+ * neither inclusion nor exclusion, so it's treated the same as GetPolicy.
+ */
+ private String policyXml(IamPolicy p, boolean includeDescription) {
return new XmlBuilder()
.elem("PolicyName", p.getPolicyName())
.elem("PolicyId", p.getPolicyId())
@@ -917,6 +923,7 @@ public class IamQueryHandler {
.elem("IsAttachable", true)
.elem("CreateDate", isoDate(p.getCreateDate()))
.elem("UpdateDate", isoDate(p.getUpdateDate()))
+ .elem("Description", includeDescription ? p.getDescription() : null)
.build();
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamIntegrationTest.java
index 1afb7123d6..0f96f9d3a3 100644
--- a/src/test/java/io/github/hectorvent/floci/services/iam/IamIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamIntegrationTest.java
@@ -708,6 +708,7 @@ class IamIntegrationTest {
.body("CreatePolicyResponse.CreatePolicyResult.Policy.PolicyName", equalTo("TestPolicy"))
.body("CreatePolicyResponse.CreatePolicyResult.Policy.PolicyId", startsWith("ANPA"))
.body("CreatePolicyResponse.CreatePolicyResult.Policy.DefaultVersionId", equalTo("v1"))
+ .body("CreatePolicyResponse.CreatePolicyResult.Policy.Description", equalTo("Test managed policy"))
.extract()
.path("CreatePolicyResponse.CreatePolicyResult.Policy.Arn");
}
@@ -724,7 +725,8 @@ class IamIntegrationTest {
.post("/")
.then()
.statusCode(200)
- .body("GetPolicyResponse.GetPolicyResult.Policy.PolicyName", equalTo("TestPolicy"));
+ .body("GetPolicyResponse.GetPolicyResult.Policy.PolicyName", equalTo("TestPolicy"))
+ .body("GetPolicyResponse.GetPolicyResult.Policy.Description", equalTo("Test managed policy"));
}
@Test
@@ -1073,4 +1075,46 @@ class IamIntegrationTest {
.body("ErrorResponse.Error.Code", equalTo("NoSuchEntity"))
.body("ErrorResponse.Error.Message", not(containsString("null")));
}
+
+ @Test
+ @Order(74)
+ void listPoliciesOmitsDescription() {
+ // AWS's own Policy model documents this explicitly: Description "is included in the
+ // response to the GetPolicy operation. It is not included in the response to the
+ // ListPolicies operation." Unlike GetPolicy/CreatePolicy (which do include it), no
+ // member returned by ListPolicies should ever carry a Description element — checking
+ // the raw response for the tag at all, rather than a specific policy's value, is what
+ // actually pins the shared-helper regression this guards against: folding the element
+ // back into policyXml() unconditionally would reintroduce it for every member, not just
+ // the one this test happens to create.
+ given()
+ .formParam("Action", "CreatePolicy")
+ .formParam("PolicyName", "ListPoliciesOmitCheckPolicy")
+ .formParam("Path", "/")
+ .formParam("PolicyDocument", POLICY_DOCUMENT)
+ .formParam("Description", "Should not appear in ListPolicies")
+ .header("Authorization",
+ "AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ // Asserting only the negative would pass vacuously if ListPolicies ever returned no
+ // members at all (a pagination bug, a scope-filter regression); the positive assertion
+ // proves the created policy is actually present before the absence check means anything.
+ // Matching the element itself, not the bare word "Description", also avoids colliding
+ // with a future policy name/path containing that substring.
+ given()
+ .formParam("Action", "ListPolicies")
+ .formParam("Scope", "Local")
+ .header("Authorization",
+ "AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("ListPoliciesOmitCheckPolicy"))
+ .body(not(containsString("<Description>")));
+ }
}