mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(iam): add GetAccountAuthorizationDetails (#4208)
Returns every user, group and role in the account, plus every local (customer-managed) policy and every AWS-managed policy actually attached to or used as a permissions boundary by something in the account: the entity lists themselves, their inline and attached policies, group membership, instance profiles, permissions boundaries, and each managed policy's version list. AttachmentCount and PermissionsBoundaryUsageCount are computed by scanning the account's own users, groups and roles rather than read off IamPolicy.getAttachmentCount(): for an AWS-managed policy that field is a single object shared process-wide across every account (attachUserPolicy et al. mutate the same catalog entry every account resolves), so trusting it would leak one account's attachments into another's response. Filed separately as #4205, since fixing it properly means changing the attach/detach methods themselves, well beyond this action. Also fixes two related, smaller gaps found while building this: - CreateInstanceProfile silently dropped a Tags parameter at creation, unlike every other Create* action. - GetInstanceProfile, CreateInstanceProfile and ListInstanceProfilesForRole never serialized an instance profile's own tags into the response. ListInstanceProfiles keeps omitting them: its own operation documentation, like ListRoles's, says it returns a subset of attributes and does not return tags, even though the InstanceProfile shape itself carries no such exclusion note. Policy documents are returned as plain JSON, not URL-encoded as AWS documents them for this action: matches every other IAM action that returns a policy document, none of which URL-encode either. Filter, MaxItems and Marker are not honored; the response always includes everything.
This commit is contained in:
1 parent
d30a744b66
commit
1fa5534e6c
6 files changed
+621
-7
No files matched your search
+184
@@ -0,0 +1,184 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
|
||||
/**
|
||||
* Verifies GetAccountAuthorizationDetails against the real wire shape (UserDetail, GroupDetail,
|
||||
* RoleDetail, ManagedPolicyDetail). IAM state is shared across the whole test suite and this
|
||||
* action returns everything unfiltered, so every assertion locates its own entities by name or
|
||||
* ARN with a Groovy {@code find}, rather than asserting on the response as a whole.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class GetAccountAuthorizationDetailsIntegrationTest {
|
||||
|
||||
private static final String IAM_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request";
|
||||
|
||||
private static final String TRUST_POLICY =
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
|
||||
+ "\"Principal\":{\"Service\":\"ec2.amazonaws.com\"},\"Action\":\"sts:AssumeRole\"}]}";
|
||||
|
||||
private static final String POLICY_DOCUMENT =
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
|
||||
+ "\"Action\":\"s3:GetObject\",\"Resource\":\"*\"}]}";
|
||||
|
||||
private static io.restassured.specification.RequestSpecification iam(String action) {
|
||||
return given().header("Authorization", IAM_AUTH).formParam("Action", action);
|
||||
}
|
||||
|
||||
private static String suffix() {
|
||||
return Long.toString(System.nanoTime(), 36);
|
||||
}
|
||||
|
||||
@Test
|
||||
void userDetailIncludesInlinePolicyGroupMembershipAttachedPolicyAndBoundary() {
|
||||
String s = suffix();
|
||||
String user = "aad-user-" + s;
|
||||
String group = "aad-group-" + s;
|
||||
|
||||
iam("CreateUser").formParam("UserName", user)
|
||||
.when().post("/").then().statusCode(200);
|
||||
iam("PutUserPolicy").formParam("UserName", user)
|
||||
.formParam("PolicyName", "inline").formParam("PolicyDocument", POLICY_DOCUMENT)
|
||||
.when().post("/").then().statusCode(200);
|
||||
iam("CreateGroup").formParam("GroupName", group)
|
||||
.when().post("/").then().statusCode(200);
|
||||
iam("AddUserToGroup").formParam("GroupName", group).formParam("UserName", user)
|
||||
.when().post("/").then().statusCode(200);
|
||||
iam("AttachUserPolicy").formParam("UserName", user)
|
||||
.formParam("PolicyArn", "arn:aws:iam::aws:policy/ReadOnlyAccess")
|
||||
.when().post("/").then().statusCode(200);
|
||||
String boundaryArn = iam("CreatePolicy").formParam("PolicyName", "aad-boundary-" + s)
|
||||
.formParam("PolicyDocument", POLICY_DOCUMENT)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.extract().path("CreatePolicyResponse.CreatePolicyResult.Policy.Arn");
|
||||
iam("PutUserPermissionsBoundary").formParam("UserName", user).formParam("PermissionsBoundary", boundaryArn)
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
iam("GetAccountAuthorizationDetails")
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.contentType("application/xml")
|
||||
.body("GetAccountAuthorizationDetailsResponse.GetAccountAuthorizationDetailsResult"
|
||||
+ ".UserDetailList.member.find { it.UserName == '" + user + "' }.UserPolicyList.member.PolicyName",
|
||||
equalTo("inline"))
|
||||
.body("GetAccountAuthorizationDetailsResponse.GetAccountAuthorizationDetailsResult"
|
||||
+ ".UserDetailList.member.find { it.UserName == '" + user + "' }.GroupList.member",
|
||||
equalTo(group))
|
||||
.body("GetAccountAuthorizationDetailsResponse.GetAccountAuthorizationDetailsResult"
|
||||
+ ".UserDetailList.member.find { it.UserName == '" + user + "' }"
|
||||
+ ".AttachedManagedPolicies.member.PolicyArn",
|
||||
equalTo("arn:aws:iam::aws:policy/ReadOnlyAccess"))
|
||||
.body("GetAccountAuthorizationDetailsResponse.GetAccountAuthorizationDetailsResult"
|
||||
+ ".UserDetailList.member.find { it.UserName == '" + user + "' }"
|
||||
+ ".PermissionsBoundary.PermissionsBoundaryArn",
|
||||
equalTo(boundaryArn))
|
||||
.body("GetAccountAuthorizationDetailsResponse.GetAccountAuthorizationDetailsResult"
|
||||
+ ".UserDetailList.member.find { it.UserName == '" + user + "' }"
|
||||
+ ".PermissionsBoundary.PermissionsBoundaryType",
|
||||
equalTo("Policy"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void groupDetailIncludesInlineAndAttachedPolicies() {
|
||||
String s = suffix();
|
||||
String group = "aad-group2-" + s;
|
||||
|
||||
iam("CreateGroup").formParam("GroupName", group)
|
||||
.when().post("/").then().statusCode(200);
|
||||
iam("PutGroupPolicy").formParam("GroupName", group)
|
||||
.formParam("PolicyName", "inline").formParam("PolicyDocument", POLICY_DOCUMENT)
|
||||
.when().post("/").then().statusCode(200);
|
||||
iam("AttachGroupPolicy").formParam("GroupName", group)
|
||||
.formParam("PolicyArn", "arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess")
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
iam("GetAccountAuthorizationDetails")
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.body("GetAccountAuthorizationDetailsResponse.GetAccountAuthorizationDetailsResult"
|
||||
+ ".GroupDetailList.member.find { it.GroupName == '" + group + "' }"
|
||||
+ ".GroupPolicyList.member.PolicyName",
|
||||
equalTo("inline"))
|
||||
.body("GetAccountAuthorizationDetailsResponse.GetAccountAuthorizationDetailsResult"
|
||||
+ ".GroupDetailList.member.find { it.GroupName == '" + group + "' }"
|
||||
+ ".AttachedManagedPolicies.member.PolicyArn",
|
||||
equalTo("arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void roleDetailIncludesAssumeRolePolicyInstanceProfileAndAttachedPolicy() {
|
||||
String s = suffix();
|
||||
String role = "aad-role-" + s;
|
||||
String profile = "aad-profile-" + s;
|
||||
|
||||
iam("CreateRole").formParam("RoleName", role).formParam("Path", "/")
|
||||
.formParam("AssumeRolePolicyDocument", TRUST_POLICY)
|
||||
.when().post("/").then().statusCode(200);
|
||||
iam("CreateInstanceProfile").formParam("InstanceProfileName", profile)
|
||||
.when().post("/").then().statusCode(200);
|
||||
iam("AddRoleToInstanceProfile").formParam("InstanceProfileName", profile).formParam("RoleName", role)
|
||||
.when().post("/").then().statusCode(200);
|
||||
iam("AttachRolePolicy").formParam("RoleName", role)
|
||||
.formParam("PolicyArn", "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole")
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
iam("GetAccountAuthorizationDetails")
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.body("GetAccountAuthorizationDetailsResponse.GetAccountAuthorizationDetailsResult"
|
||||
+ ".RoleDetailList.member.find { it.RoleName == '" + role + "' }"
|
||||
+ ".InstanceProfileList.member.InstanceProfileName",
|
||||
equalTo(profile))
|
||||
.body("GetAccountAuthorizationDetailsResponse.GetAccountAuthorizationDetailsResult"
|
||||
+ ".RoleDetailList.member.find { it.RoleName == '" + role + "' }"
|
||||
+ ".AttachedManagedPolicies.member.PolicyArn",
|
||||
equalTo("arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"))
|
||||
// RoleDetail's own AssumeRolePolicyDocument, not just the one embedded via
|
||||
// InstanceProfileList's role subset.
|
||||
.body("GetAccountAuthorizationDetailsResponse.GetAccountAuthorizationDetailsResult"
|
||||
+ ".RoleDetailList.member.find { it.RoleName == '" + role + "' }.AssumeRolePolicyDocument",
|
||||
containsString("ec2.amazonaws.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void localPolicyIncludesVersionListAndAttachmentCountScopedToThisAccount() {
|
||||
String s = suffix();
|
||||
String policyName = "aad-policy-" + s;
|
||||
String user = "aad-policy-user-" + s;
|
||||
|
||||
String arn = iam("CreatePolicy").formParam("PolicyName", policyName)
|
||||
.formParam("PolicyDocument", POLICY_DOCUMENT)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.extract().path("CreatePolicyResponse.CreatePolicyResult.Policy.Arn");
|
||||
iam("CreatePolicyVersion").formParam("PolicyArn", arn)
|
||||
.formParam("PolicyDocument",
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
|
||||
+ "\"Action\":\"s3:PutObject\",\"Resource\":\"*\"}]}")
|
||||
.formParam("SetAsDefault", "true")
|
||||
.when().post("/").then().statusCode(200);
|
||||
iam("CreateUser").formParam("UserName", user)
|
||||
.when().post("/").then().statusCode(200);
|
||||
iam("AttachUserPolicy").formParam("UserName", user).formParam("PolicyArn", arn)
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
iam("GetAccountAuthorizationDetails")
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.body("GetAccountAuthorizationDetailsResponse.GetAccountAuthorizationDetailsResult"
|
||||
+ ".Policies.member.find { it.Arn == '" + arn + "' }.AttachmentCount",
|
||||
equalTo("1"))
|
||||
.body("GetAccountAuthorizationDetailsResponse.GetAccountAuthorizationDetailsResult"
|
||||
+ ".Policies.member.find { it.Arn == '" + arn + "' }.PolicyVersionList.member.VersionId",
|
||||
hasItem("v2"))
|
||||
.body("GetAccountAuthorizationDetailsResponse.GetAccountAuthorizationDetailsResult"
|
||||
+ ".Policies.member.find { it.Arn == '" + arn + "' }.PolicyVersionList.member.Document",
|
||||
hasItem(containsString("PutObject")));
|
||||
}
|
||||
}
|
||||
+88
@@ -282,4 +282,92 @@ class IamResourceTagsInResponseTest {
|
||||
.body(not(containsString("<Tags>")))
|
||||
.body(not(containsString("profile-must-not-be-listed")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlike Role, User and Policy, AWS documents no listing-subset exclusion for
|
||||
* InstanceProfile: ListInstanceProfiles uses the same InstanceProfile shape as
|
||||
* CreateInstanceProfile and GetInstanceProfile, so its own tags (not the embedded role's)
|
||||
* belong on all three. CreateInstanceProfile also accepts Tags at creation time, which it
|
||||
* previously silently dropped.
|
||||
*/
|
||||
@Test
|
||||
void createAndGetInstanceProfileEchoTheirOwnTags() {
|
||||
String profile = "TagEchoProfile";
|
||||
|
||||
iam("CreateInstanceProfile")
|
||||
.formParam("InstanceProfileName", profile)
|
||||
.formParam("Tags.member.1.Key", "Environment")
|
||||
.formParam("Tags.member.1.Value", "dev")
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<Key>Environment</Key>"))
|
||||
.body(containsString("<Value>dev</Value>"));
|
||||
|
||||
iam("GetInstanceProfile")
|
||||
.formParam("InstanceProfileName", profile)
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<Key>Environment</Key>"))
|
||||
.body(containsString("<Value>dev</Value>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getInstanceProfileOmitsTheTagsElementEntirelyWhenUntagged() {
|
||||
String profile = "UntaggedEchoProfile";
|
||||
|
||||
iam("CreateInstanceProfile")
|
||||
.formParam("InstanceProfileName", profile)
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
iam("GetInstanceProfile")
|
||||
.formParam("InstanceProfileName", profile)
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.body(not(containsString("<Tags>")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listInstanceProfilesOmitsTagsEvenForATaggedProfile() {
|
||||
String profile = "ListSubsetProfile";
|
||||
|
||||
iam("CreateInstanceProfile")
|
||||
.formParam("InstanceProfileName", profile)
|
||||
.formParam("Tags.member.1.Key", "ListProfilesMarker")
|
||||
.formParam("Tags.member.1.Value", "must-not-be-listed")
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
// ListInstanceProfiles documents itself as a listing subset, the same note ListRoles
|
||||
// carries: "this operation does not return tags, even though they are an attribute of
|
||||
// the returned object."
|
||||
iam("ListInstanceProfiles")
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<InstanceProfileName>" + profile + "</InstanceProfileName>"))
|
||||
.body(not(containsString("<Tags>")))
|
||||
.body(not(containsString("must-not-be-listed")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listInstanceProfilesForRoleAndGetAccountAuthorizationDetailsStillIncludeInstanceProfileTags() {
|
||||
String s = Long.toString(System.nanoTime(), 36);
|
||||
String role = "ListForRoleTagRole-" + s;
|
||||
String profile = "ListForRoleTagProfile-" + s;
|
||||
|
||||
iam("CreateRole").formParam("RoleName", role).formParam("Path", "/")
|
||||
.formParam("AssumeRolePolicyDocument", TRUST_POLICY)
|
||||
.when().post("/").then().statusCode(200);
|
||||
iam("CreateInstanceProfile")
|
||||
.formParam("InstanceProfileName", profile)
|
||||
.formParam("Tags.member.1.Key", "ListForRoleMarker")
|
||||
.formParam("Tags.member.1.Value", "must-be-listed")
|
||||
.when().post("/").then().statusCode(200);
|
||||
iam("AddRoleToInstanceProfile").formParam("InstanceProfileName", profile).formParam("RoleName", role)
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
// Unlike ListInstanceProfiles, this operation carries no listing-subset note.
|
||||
iam("ListInstanceProfilesForRole").formParam("RoleName", role)
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.body(containsString("must-be-listed"));
|
||||
}
|
||||
}
|
||||
@@ -1953,4 +1953,106 @@ class IamServiceTest {
|
||||
iamService.createAccountAlias("a".repeat(63));
|
||||
assertEquals("a".repeat(63), iamService.getAccountAlias().orElseThrow());
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// GetAccountAuthorizationDetails
|
||||
// =========================================================================
|
||||
|
||||
@Test
|
||||
void accountAuthorizationDetailsAlwaysIncludesLocalPoliciesEvenUnattached() {
|
||||
IamPolicy unattached = iamService.createPolicy("aad-unattached", "/", null,
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"s3:GetObject\",\"Resource\":\"*\"}]}",
|
||||
null);
|
||||
|
||||
IamService.AccountAuthorizationDetails details = iamService.getAccountAuthorizationDetails();
|
||||
|
||||
assertTrue(details.policies().stream().anyMatch(p -> p.getArn().equals(unattached.getArn())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void accountAuthorizationDetailsExcludesAnAwsManagedPolicyNotAttachedToAnything() {
|
||||
IamService.AccountAuthorizationDetails details = iamService.getAccountAuthorizationDetails();
|
||||
|
||||
// A freshly constructed IamService has attached nothing, so a well-known AWS-managed
|
||||
// policy this test never touches must not appear.
|
||||
assertTrue(details.policies().stream()
|
||||
.noneMatch(p -> p.getArn().equals("arn:aws:iam::aws:policy/AdministratorAccess")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void accountAuthorizationDetailsIncludesAnAwsManagedPolicyOnceAttachedToAUser() {
|
||||
iamService.createUser("aad-user", "/");
|
||||
iamService.attachUserPolicy("aad-user", "arn:aws:iam::aws:policy/AdministratorAccess");
|
||||
|
||||
IamService.AccountAuthorizationDetails details = iamService.getAccountAuthorizationDetails();
|
||||
|
||||
assertTrue(details.policies().stream()
|
||||
.anyMatch(p -> p.getArn().equals("arn:aws:iam::aws:policy/AdministratorAccess")));
|
||||
}
|
||||
|
||||
/**
|
||||
* AttachmentCount is computed by scanning this account's own users, groups and roles, not
|
||||
* read off {@code IamPolicy.getAttachmentCount()}: for an AWS-managed policy that field is
|
||||
* shared process-wide across every {@code IamService} instance's account, so a second,
|
||||
* independent instance attaching the same ARN must not change what this instance reports.
|
||||
*/
|
||||
@Test
|
||||
void attachmentCountIsScopedToThisAccountNotTheSharedAwsManagedPolicyObject() {
|
||||
String arn = "arn:aws:iam::aws:policy/AdministratorAccess";
|
||||
iamService.createUser("aad-scoped-user", "/");
|
||||
iamService.attachUserPolicy("aad-scoped-user", arn);
|
||||
|
||||
IamService other = iamService(false);
|
||||
other.createUser("other-account-user", "/");
|
||||
other.attachUserPolicy("other-account-user", arn);
|
||||
other.createUser("other-account-user-2", "/");
|
||||
other.attachUserPolicy("other-account-user-2", arn);
|
||||
|
||||
IamService.AccountAuthorizationDetails details = iamService.getAccountAuthorizationDetails();
|
||||
|
||||
assertEquals(1, details.attachmentCounts().get(arn));
|
||||
}
|
||||
|
||||
@Test
|
||||
void attachmentCountTalliesAcrossUsersGroupsAndRoles() {
|
||||
String arn = "arn:aws:iam::aws:policy/ReadOnlyAccess";
|
||||
iamService.createUser("aad-tally-user", "/");
|
||||
iamService.attachUserPolicy("aad-tally-user", arn);
|
||||
iamService.createGroup("aad-tally-group", "/");
|
||||
iamService.attachGroupPolicy("aad-tally-group", arn);
|
||||
iamService.createRole("aad-tally-role", "/", "{}", null, 0, null);
|
||||
iamService.attachRolePolicy("aad-tally-role", arn);
|
||||
|
||||
IamService.AccountAuthorizationDetails details = iamService.getAccountAuthorizationDetails();
|
||||
|
||||
assertEquals(3, details.attachmentCounts().get(arn));
|
||||
}
|
||||
|
||||
@Test
|
||||
void permissionsBoundaryUsageCountTalliesAcrossUsersAndRoles() {
|
||||
IamPolicy boundary = iamService.createPolicy("aad-boundary", "/", null,
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"*\",\"Resource\":\"*\"}]}",
|
||||
null);
|
||||
iamService.createUser("aad-boundary-user", "/");
|
||||
iamService.putUserPermissionsBoundary("aad-boundary-user", boundary.getArn());
|
||||
iamService.createRole("aad-boundary-role", "/", "{}", null, 0, null);
|
||||
iamService.putRolePermissionsBoundary("aad-boundary-role", boundary.getArn());
|
||||
|
||||
IamService.AccountAuthorizationDetails details = iamService.getAccountAuthorizationDetails();
|
||||
|
||||
assertEquals(2, details.permissionsBoundaryUsageCounts().get(boundary.getArn()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void accountAuthorizationDetailsListsEveryUserGroupAndRole() {
|
||||
iamService.createUser("aad-list-user", "/");
|
||||
iamService.createGroup("aad-list-group", "/");
|
||||
iamService.createRole("aad-list-role", "/", "{}", null, 0, null);
|
||||
|
||||
IamService.AccountAuthorizationDetails details = iamService.getAccountAuthorizationDetails();
|
||||
|
||||
assertTrue(details.users().stream().anyMatch(u -> u.getUserName().equals("aad-list-user")));
|
||||
assertTrue(details.groups().stream().anyMatch(g -> g.getGroupName().equals("aad-list-group")));
|
||||
assertTrue(details.roles().stream().anyMatch(r -> r.getRoleName().equals("aad-list-role")));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user