mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(iam,lambda): return resource tags in Get and Create responses (#2262)
* fix(iam,lambda): return resource tags in Get and Create responses
IAM stored role, policy and user tags and served them through
ListRoleTags/ListPolicyTags/ListUserTags, but never serialized them into
the Create/Get responses. Lambda did the same with GetFunction. The AWS
SDKs and the Terraform provider read tags off those responses rather than
by calling List*Tags, so every tagged role, policy, user and function read
back untagged: apply succeeds and then plan never converges.
The inclusion is asymmetric, and getting that wrong deviates from AWS in
the opposite direction. IAM's resource-listing operations deliberately
return a subset of attributes:
ListRoles "does not return the following attributes, even though they
are an attribute of the returned object: PermissionsBoundary,
RoleLastUsed, Tags"
ListUsers same, for PermissionsBoundary and Tags
ListPolicies "this operation does not return tags"
roleXml, userXml and policyXml therefore take a detailed flag, following
the pattern #2155 established for the policy Description. Description on a
role stays unconditional: it is absent from the ListRoles exclusion list.
For policies one flag governs both Description and Tags, since ListPolicies
excludes them on identical grounds.
Detail call sites are CreateRole, GetRole, CreateServiceLinkedRole,
CreateUser, GetUser, CreatePolicy and GetPolicy. Listing call sites are
ListRoles, ListUsers, ListPolicies and GetGroup, plus the roles embedded in
an instance profile — GetInstanceProfile's own example response shows those
as a subset.
tagsElement emits nothing rather than an empty <Tags/> for an untagged
resource, which would otherwise read back as a tag set the caller never
configured.
Lambda needs no such gate: ListFunctions returns FunctionConfiguration,
which has no Tags field at all.
Closes #2199
* test(iam,lambda): cover the tag round trip and the listing-subset rule
Adds a GetFunction case to the Lambda tag test, ordered after the untag so
it also pins that a removed tag is reflected rather than served stale.
Adds a tagged role and a tagged policy to the Terraform compatibility
config, with cases for the positive round trip, the ListRoles/ListPolicies
subset negatives, and a clean second plan. The re-plan assertion is the one
that catches this drift class: tags that do not survive the round trip
apply cleanly and only show up later as perpetual drift.
This commit is contained in:
1 parent
c12f3fae7d
commit
b71bfc46b6
6 files changed
+459
-17
No files matched your search
@@ -199,7 +199,7 @@ public class IamQueryHandler {
|
||||
IamUser user = iamService.createUser(userName, path);
|
||||
if (!tags.isEmpty()) iamService.tagUser(userName, tags);
|
||||
user = iamService.getUser(userName);
|
||||
String result = new XmlBuilder().start("User").raw(userXml(user)).end("User").build();
|
||||
String result = new XmlBuilder().start("User").raw(userXml(user, true)).end("User").build();
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateUser", AwsNamespaces.IAM, result)).build();
|
||||
}
|
||||
|
||||
@@ -225,7 +225,7 @@ public class IamQueryHandler {
|
||||
// UserName is optional per the IAM model: it defaults to the user owning the
|
||||
// access key that signed the request.
|
||||
IamUser user = iamService.getUser(resolveUserName(params, authorization));
|
||||
String result = new XmlBuilder().start("User").raw(userXml(user)).end("User").build();
|
||||
String result = new XmlBuilder().start("User").raw(userXml(user, true)).end("User").build();
|
||||
return Response.ok(AwsQueryResponse.envelope("GetUser", AwsNamespaces.IAM, result)).build();
|
||||
}
|
||||
|
||||
@@ -240,7 +240,7 @@ public class IamQueryHandler {
|
||||
List<IamUser> userList = iamService.listUsers(pathPrefix);
|
||||
var xml = new XmlBuilder().start("Users");
|
||||
for (IamUser u : userList) {
|
||||
xml.start("member").raw(userXml(u)).end("member");
|
||||
xml.start("member").raw(userXml(u, false)).end("member");
|
||||
}
|
||||
xml.end("Users").elem("IsTruncated", false);
|
||||
return Response.ok(AwsQueryResponse.envelope("ListUsers", AwsNamespaces.IAM, xml.build())).build();
|
||||
@@ -458,7 +458,7 @@ public class IamQueryHandler {
|
||||
.start("Group").raw(groupXml(group)).end("Group")
|
||||
.start("Users");
|
||||
for (IamUser u : members) {
|
||||
xml.start("member").raw(userXml(u)).end("member");
|
||||
xml.start("member").raw(userXml(u, false)).end("member");
|
||||
}
|
||||
xml.end("Users").elem("IsTruncated", false);
|
||||
return Response.ok(AwsQueryResponse.envelope("GetGroup", AwsNamespaces.IAM, xml.build())).build();
|
||||
@@ -511,13 +511,13 @@ public class IamQueryHandler {
|
||||
int maxSession = getIntParam(params, "MaxSessionDuration", 3600);
|
||||
Map<String, String> tags = extractTags(params);
|
||||
IamRole role = iamService.createRole(roleName, path, trustPolicy, description, maxSession, tags);
|
||||
String result = new XmlBuilder().start("Role").raw(roleXml(role)).end("Role").build();
|
||||
String result = new XmlBuilder().start("Role").raw(roleXml(role, true)).end("Role").build();
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateRole", AwsNamespaces.IAM, result)).build();
|
||||
}
|
||||
|
||||
private Response handleGetRole(MultivaluedMap<String, String> params) {
|
||||
IamRole role = iamService.getRole(getParam(params, "RoleName"));
|
||||
String result = new XmlBuilder().start("Role").raw(roleXml(role)).end("Role").build();
|
||||
String result = new XmlBuilder().start("Role").raw(roleXml(role, true)).end("Role").build();
|
||||
return Response.ok(AwsQueryResponse.envelope("GetRole", AwsNamespaces.IAM, result)).build();
|
||||
}
|
||||
|
||||
@@ -531,7 +531,7 @@ public class IamQueryHandler {
|
||||
getParam(params, "AWSServiceName"),
|
||||
getParam(params, "CustomSuffix"),
|
||||
getParam(params, "Description"));
|
||||
String result = new XmlBuilder().start("Role").raw(roleXml(role)).end("Role").build();
|
||||
String result = new XmlBuilder().start("Role").raw(roleXml(role, true)).end("Role").build();
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateServiceLinkedRole", AwsNamespaces.IAM, result)).build();
|
||||
}
|
||||
|
||||
@@ -551,7 +551,7 @@ public class IamQueryHandler {
|
||||
List<IamRole> roleList = iamService.listRoles(getParam(params, "PathPrefix"));
|
||||
var xml = new XmlBuilder().start("Roles");
|
||||
for (IamRole r : roleList) {
|
||||
xml.start("member").raw(roleXml(r)).end("member");
|
||||
xml.start("member").raw(roleXml(r, false)).end("member");
|
||||
}
|
||||
xml.end("Roles").elem("IsTruncated", false);
|
||||
return Response.ok(AwsQueryResponse.envelope("ListRoles", AwsNamespaces.IAM, xml.build())).build();
|
||||
@@ -1030,13 +1030,21 @@ public class IamQueryHandler {
|
||||
// XML serialization helpers
|
||||
// =========================================================================
|
||||
|
||||
private String userXml(IamUser u) {
|
||||
/**
|
||||
* {@code detailed} is per-operation, not per-user: ListUsers documents that "IAM
|
||||
* resource-listing operations return a subset of the available attributes for the resource.
|
||||
* This operation does not return the following attributes, even though they are an attribute
|
||||
* of the returned object: PermissionsBoundary, Tags". GetGroup likewise lists its members
|
||||
* without tags.
|
||||
*/
|
||||
private String userXml(IamUser u, boolean detailed) {
|
||||
return new XmlBuilder()
|
||||
.elem("Path", u.getPath())
|
||||
.elem("UserName", u.getUserName())
|
||||
.elem("UserId", u.getUserId())
|
||||
.elem("Arn", u.getArn())
|
||||
.elem("CreateDate", isoDate(u.getCreateDate()))
|
||||
.raw(detailed ? tagsElement(u.getTags()) : "")
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -1050,7 +1058,15 @@ public class IamQueryHandler {
|
||||
.build();
|
||||
}
|
||||
|
||||
private String roleXml(IamRole r) {
|
||||
/**
|
||||
* {@code detailed} is per-operation, not per-role: ListRoles documents that "IAM
|
||||
* resource-listing operations return a subset of the available attributes for the resource.
|
||||
* This operation does not return the following attributes, even though they are an attribute
|
||||
* of the returned object: PermissionsBoundary, RoleLastUsed, Tags". {@code Description} is
|
||||
* deliberately not gated — it is absent from that exclusion list. Roles embedded in an
|
||||
* instance profile are a subset too, as GetInstanceProfile's own example response shows.
|
||||
*/
|
||||
private String roleXml(IamRole r, boolean detailed) {
|
||||
return new XmlBuilder()
|
||||
.elem("Path", r.getPath())
|
||||
.elem("RoleName", r.getRoleName())
|
||||
@@ -1060,16 +1076,19 @@ public class IamQueryHandler {
|
||||
.elem("MaxSessionDuration", (long) r.getMaxSessionDuration())
|
||||
.elem("AssumeRolePolicyDocument", r.getAssumeRolePolicyDocument())
|
||||
.elem("Description", r.getDescription())
|
||||
.raw(detailed ? tagsElement(r.getTags()) : "")
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@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.
|
||||
* {@code detailed} 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. ListPolicies excludes
|
||||
* {@code Tags} on the same grounds ("this operation does not return tags"), so one flag
|
||||
* governs both.
|
||||
*/
|
||||
private String policyXml(IamPolicy p, boolean includeDescription) {
|
||||
private String policyXml(IamPolicy p, boolean detailed) {
|
||||
return new XmlBuilder()
|
||||
.elem("PolicyName", p.getPolicyName())
|
||||
.elem("PolicyId", p.getPolicyId())
|
||||
@@ -1080,7 +1099,8 @@ public class IamQueryHandler {
|
||||
.elem("IsAttachable", true)
|
||||
.elem("CreateDate", isoDate(p.getCreateDate()))
|
||||
.elem("UpdateDate", isoDate(p.getUpdateDate()))
|
||||
.elem("Description", includeDescription ? p.getDescription() : null)
|
||||
.elem("Description", detailed ? p.getDescription() : null)
|
||||
.raw(detailed ? tagsElement(p.getTags()) : "")
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -1115,7 +1135,7 @@ public class IamQueryHandler {
|
||||
for (String roleName : p.getRoleNames()) {
|
||||
try {
|
||||
IamRole role = iamService.getRole(roleName);
|
||||
xml.start("member").raw(roleXml(role)).end("member");
|
||||
xml.start("member").raw(roleXml(role, false)).end("member");
|
||||
} catch (AwsException ignored) {}
|
||||
}
|
||||
return xml.end("Roles").build();
|
||||
@@ -1140,6 +1160,22 @@ public class IamQueryHandler {
|
||||
return xml.end("PolicyNames").elem("IsTruncated", false).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the {@code <Tags>} wrapper for a resource, or nothing when it has no tags.
|
||||
*
|
||||
* <p>The AWS SDKs and the Terraform provider read a role's, policy's or user's tags off the
|
||||
* Get/Create response rather than by calling List*Tags, so omitting this element makes every
|
||||
* tagged resource read back untagged and diff on every plan. Emitting nothing rather than an
|
||||
* empty {@code <Tags/>} keeps an untagged resource from reading back as having an empty tag
|
||||
* set, which would be a diff of its own.
|
||||
*/
|
||||
private String tagsElement(Map<String, String> tags) {
|
||||
if (tags == null || tags.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
return new XmlBuilder().start("Tags").raw(tagsXml(tags)).end("Tags").build();
|
||||
}
|
||||
|
||||
private String tagsXml(Map<String, String> tags) {
|
||||
var xml = new XmlBuilder();
|
||||
for (var entry : tags.entrySet()) {
|
||||
|
||||
@@ -104,6 +104,17 @@ public class LambdaController {
|
||||
code.put("RepositoryType", "S3");
|
||||
}
|
||||
|
||||
// GetFunction carries the function's tags alongside Configuration and Code. Clients read
|
||||
// them from here rather than calling ListTags, so omitting the field makes a tagged
|
||||
// function read back untagged and diff on every plan. Unlike IAM, Lambda has no
|
||||
// list-subset rule to gate this on — ListFunctions returns FunctionConfiguration, which
|
||||
// has no Tags field at all.
|
||||
Map<String, String> tags = fn.getTags();
|
||||
if (tags != null && !tags.isEmpty()) {
|
||||
ObjectNode tagsNode = root.putObject("Tags");
|
||||
tags.forEach(tagsNode::put);
|
||||
}
|
||||
|
||||
return Response.ok(root).build();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user