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

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

policyXml() built the response for CreatePolicy, GetPolicy, and ListPolicies but never
emitted the Description element, even though IamPolicy stores it correctly and the
sibling roleXml() includes it for roles. Callers (e.g. Terraform's AWS provider) that
set a policy description see it as permanently missing on every read, which forces
destroy+recreate since IAM policy descriptions are immutable.

Fixes #2154

* fix(iam): stop ListPolicies from returning Description

policyXml() is shared by CreatePolicy, GetPolicy, and ListPolicies — the
previous fix added Description to all three, but AWS's own Policy model
documents that 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.

policyXml() now takes an explicit includeDescription flag: true at the
CreatePolicy/GetPolicy call sites, false in ListPolicies' member loop. Adds
a ListPolicies test asserting no member response ever carries a Description
element, so folding it back into the shared helper unconditionally — the
exact way this would regress — fails loudly.

Addresses review feedback from @pgermosen.

* fix(iam): make listPoliciesOmitsDescription assert the element, not the word

Match "<Description>" rather than the bare word "Description" to avoid a
false failure if a future policy's name/path happens to contain that
substring, and assert the created policy is actually present in the
response first, so the absence check can't pass vacuously against an
empty (or wrongly-filtered) member list.
This commit is contained in:
sheldon-nadarajah-sft authored and GitHub committed 2026-08-08 22:12:35 -04:00
1 parent c0f36ea604
commit 1150cfc657
2 files changed
+56 -5

No files matched your search

@@ -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();
}
@@ -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>")));
}
}