mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(iam): return PermissionsBoundary from GetRole/GetUser and accept it on CreateRole/CreateUser (#4964)
CreateRole and CreateUser ignored the PermissionsBoundary parameter, and
roleXml/userXml never serialized a stored boundary, so GetRole/GetUser
(and the Create responses) returned none even after
Put{Role,User}PermissionsBoundary. Only GetAccountAuthorizationDetails
emitted it.
Emit the boundary on the detailed fragment only: ListRoles/ListUsers
document that they do not return PermissionsBoundary. The boundary is
validated before the role/user is created, so an unknown policy leaves
nothing behind. Existing createRole/createUser signatures are kept as
overloads for the CloudFormation and service-linked-role callers.
This commit is contained in:
1 parent
a4c952db54
commit
3e2d310eae
3 files changed
+194
-4
No files matched your search
@@ -278,7 +278,7 @@ public class IamQueryHandler {
|
||||
String userName = getParam(params, "UserName");
|
||||
String path = getParam(params, "Path");
|
||||
Map<String, String> tags = extractTags(params, true);
|
||||
IamUser user = iamService.createUser(userName, path);
|
||||
IamUser user = iamService.createUser(userName, path, getParam(params, "PermissionsBoundary"));
|
||||
if (!tags.isEmpty()) iamService.tagUser(userName, tags);
|
||||
user = iamService.getUser(userName);
|
||||
String result = new XmlBuilder().start("User").raw(userXml(user, true)).end("User").build();
|
||||
@@ -915,7 +915,8 @@ public class IamQueryHandler {
|
||||
String description = getParam(params, "Description");
|
||||
int maxSession = getIntParam(params, "MaxSessionDuration", 3600);
|
||||
Map<String, String> tags = extractTags(params, true);
|
||||
IamRole role = iamService.createRole(roleName, path, trustPolicy, description, maxSession, tags);
|
||||
IamRole role = iamService.createRole(roleName, path, trustPolicy, description, maxSession, tags,
|
||||
getParam(params, "PermissionsBoundary"));
|
||||
String result = new XmlBuilder().start("Role").raw(roleXml(role, true)).end("Role").build();
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateRole", AwsNamespaces.IAM, result)).build();
|
||||
}
|
||||
@@ -1118,7 +1119,6 @@ public class IamQueryHandler {
|
||||
}
|
||||
return xml.end("GroupList")
|
||||
.raw(attachedManagedPoliciesXml(iamService.listAttachedUserPolicies(u.getUserName(), null)))
|
||||
.raw(permissionsBoundaryXml(u.getPermissionsBoundaryArn()))
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -2143,6 +2143,7 @@ public class IamQueryHandler {
|
||||
.elem("UserId", u.getUserId())
|
||||
.elem("Arn", u.getArn())
|
||||
.elem("CreateDate", isoDate(u.getCreateDate()))
|
||||
.raw(detailed ? permissionsBoundaryXml(u.getPermissionsBoundaryArn()) : "")
|
||||
.raw(detailed ? tagsElement(u.getTags()) : "")
|
||||
.build();
|
||||
}
|
||||
@@ -2175,6 +2176,7 @@ public class IamQueryHandler {
|
||||
.elem("MaxSessionDuration", (long) r.getMaxSessionDuration())
|
||||
.elem("AssumeRolePolicyDocument", r.getAssumeRolePolicyDocument())
|
||||
.elem("Description", r.getDescription())
|
||||
.raw(detailed ? permissionsBoundaryXml(r.getPermissionsBoundaryArn()) : "")
|
||||
.raw(detailed ? tagsElement(r.getTags()) : "")
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -460,6 +460,13 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
// =========================================================================
|
||||
|
||||
public IamUser createUser(String userName, String path) {
|
||||
return createUser(userName, path, null);
|
||||
}
|
||||
|
||||
public IamUser createUser(String userName, String path, String permissionsBoundaryArn) {
|
||||
if (permissionsBoundaryArn != null) {
|
||||
requirePolicy(permissionsBoundaryArn); // validate before anything is created
|
||||
}
|
||||
synchronized (resourceNameLock) {
|
||||
if (containsNameIgnoreCase(users, IamUser::getUserName, userName)) {
|
||||
throw new AwsException("EntityAlreadyExists",
|
||||
@@ -469,6 +476,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
String normalizedPath = normalizePath(path);
|
||||
String arn = iamArn("user", normalizedPath, userName);
|
||||
IamUser user = new IamUser(userId, userName, normalizedPath, arn);
|
||||
user.setPermissionsBoundaryArn(permissionsBoundaryArn);
|
||||
users.put(userName, user);
|
||||
LOG.infov("Created IAM user: {0}", userName);
|
||||
return user;
|
||||
@@ -747,6 +755,15 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
|
||||
public IamRole createRole(String roleName, String path, String assumeRolePolicyDocument,
|
||||
String description, int maxSessionDuration, Map<String, String> tags) {
|
||||
return createRole(roleName, path, assumeRolePolicyDocument, description, maxSessionDuration, tags, null);
|
||||
}
|
||||
|
||||
public IamRole createRole(String roleName, String path, String assumeRolePolicyDocument,
|
||||
String description, int maxSessionDuration, Map<String, String> tags,
|
||||
String permissionsBoundaryArn) {
|
||||
if (permissionsBoundaryArn != null) {
|
||||
requirePolicy(permissionsBoundaryArn); // validate before anything is created
|
||||
}
|
||||
synchronized (resourceNameLock) {
|
||||
if (containsNameIgnoreCase(roles, IamRole::getRoleName, roleName)) {
|
||||
throw new AwsException("EntityAlreadyExists",
|
||||
@@ -757,6 +774,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
String arn = iamArn("role", normalizedPath, roleName);
|
||||
IamRole role = new IamRole(roleId, roleName, normalizedPath, arn, assumeRolePolicyDocument);
|
||||
role.setDescription(description);
|
||||
role.setPermissionsBoundaryArn(permissionsBoundaryArn);
|
||||
if (maxSessionDuration > 0) {
|
||||
role.setMaxSessionDuration(maxSessionDuration);
|
||||
}
|
||||
|
||||
@@ -1389,5 +1389,175 @@ class IamIntegrationTest {
|
||||
+ ".member.find { it.EvalActionName == 'dynamodb:GetItem' }.EvalDecision",
|
||||
equalTo("allowed"));
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// PermissionsBoundary round-trip (CreateRole/CreateUser accept it; Get* return it; List* don't)
|
||||
// =========================================================================
|
||||
|
||||
private static final String IAM_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20261001/us-east-1/iam/aws4_request";
|
||||
|
||||
private static String createBoundaryPolicy(String name) {
|
||||
return given()
|
||||
.formParam("Action", "CreatePolicy")
|
||||
.formParam("PolicyName", name)
|
||||
.formParam("PolicyDocument", POLICY_DOCUMENT)
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.extract().path("CreatePolicyResponse.CreatePolicyResult.Policy.Arn");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(80)
|
||||
void createRoleWithPermissionsBoundaryIsReturnedByGetRole() {
|
||||
String boundary = createBoundaryPolicy("pb-create-role-boundary");
|
||||
given()
|
||||
.formParam("Action", "CreateRole")
|
||||
.formParam("RoleName", "pb-create-role")
|
||||
.formParam("Path", "/pb-list/")
|
||||
.formParam("AssumeRolePolicyDocument", TRUST_POLICY)
|
||||
.formParam("PermissionsBoundary", boundary)
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.body("CreateRoleResponse.CreateRoleResult.Role.PermissionsBoundary.PermissionsBoundaryArn",
|
||||
equalTo(boundary));
|
||||
|
||||
given()
|
||||
.formParam("Action", "GetRole")
|
||||
.formParam("RoleName", "pb-create-role")
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.body("GetRoleResponse.GetRoleResult.Role.PermissionsBoundary.PermissionsBoundaryArn",
|
||||
equalTo(boundary))
|
||||
.body("GetRoleResponse.GetRoleResult.Role.PermissionsBoundary.PermissionsBoundaryType",
|
||||
equalTo("Policy"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(81)
|
||||
void putRolePermissionsBoundaryIsReturnedByGetRole() {
|
||||
String boundary = createBoundaryPolicy("pb-put-role-boundary");
|
||||
given()
|
||||
.formParam("Action", "CreateRole")
|
||||
.formParam("RoleName", "pb-put-role")
|
||||
.formParam("AssumeRolePolicyDocument", TRUST_POLICY)
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
given()
|
||||
.formParam("Action", "PutRolePermissionsBoundary")
|
||||
.formParam("RoleName", "pb-put-role")
|
||||
.formParam("PermissionsBoundary", boundary)
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
given()
|
||||
.formParam("Action", "GetRole")
|
||||
.formParam("RoleName", "pb-put-role")
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.body("GetRoleResponse.GetRoleResult.Role.PermissionsBoundary.PermissionsBoundaryArn",
|
||||
equalTo(boundary));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(82)
|
||||
void createUserWithPermissionsBoundaryIsReturnedByGetUser() {
|
||||
String boundary = createBoundaryPolicy("pb-create-user-boundary");
|
||||
given()
|
||||
.formParam("Action", "CreateUser")
|
||||
.formParam("UserName", "pb-create-user")
|
||||
.formParam("Path", "/pb-list/")
|
||||
.formParam("PermissionsBoundary", boundary)
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.body("CreateUserResponse.CreateUserResult.User.PermissionsBoundary.PermissionsBoundaryArn",
|
||||
equalTo(boundary));
|
||||
|
||||
given()
|
||||
.formParam("Action", "GetUser")
|
||||
.formParam("UserName", "pb-create-user")
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.body("GetUserResponse.GetUserResult.User.PermissionsBoundary.PermissionsBoundaryArn",
|
||||
equalTo(boundary));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(83)
|
||||
void putUserPermissionsBoundaryIsReturnedByGetUser() {
|
||||
String boundary = createBoundaryPolicy("pb-put-user-boundary");
|
||||
given()
|
||||
.formParam("Action", "CreateUser")
|
||||
.formParam("UserName", "pb-put-user")
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
given()
|
||||
.formParam("Action", "PutUserPermissionsBoundary")
|
||||
.formParam("UserName", "pb-put-user")
|
||||
.formParam("PermissionsBoundary", boundary)
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
given()
|
||||
.formParam("Action", "GetUser")
|
||||
.formParam("UserName", "pb-put-user")
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.body("GetUserResponse.GetUserResult.User.PermissionsBoundary.PermissionsBoundaryArn",
|
||||
equalTo(boundary));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(84)
|
||||
void listRolesAndListUsersOmitPermissionsBoundary() {
|
||||
// Orders 80 and 82 created a bounded role and user under /pb-list/. ListRoles and
|
||||
// ListUsers document that they do not return PermissionsBoundary.
|
||||
given()
|
||||
.formParam("Action", "ListRoles")
|
||||
.formParam("PathPrefix", "/pb-list/")
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.body("ListRolesResponse.ListRolesResult.Roles.member.RoleName", equalTo("pb-create-role"))
|
||||
.body(not(containsString("PermissionsBoundary")));
|
||||
|
||||
given()
|
||||
.formParam("Action", "ListUsers")
|
||||
.formParam("PathPrefix", "/pb-list/")
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.body("ListUsersResponse.ListUsersResult.Users.member.UserName", equalTo("pb-create-user"))
|
||||
.body(not(containsString("PermissionsBoundary")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(85)
|
||||
void createRoleWithUnknownPermissionsBoundaryCreatesNothing() {
|
||||
given()
|
||||
.formParam("Action", "CreateRole")
|
||||
.formParam("RoleName", "pb-bad-role")
|
||||
.formParam("AssumeRolePolicyDocument", TRUST_POLICY)
|
||||
.formParam("PermissionsBoundary", "arn:aws:iam::000000000000:policy/pb-does-not-exist")
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then()
|
||||
.statusCode(404)
|
||||
.body(containsString("NoSuchEntity"));
|
||||
|
||||
given()
|
||||
.formParam("Action", "GetRole")
|
||||
.formParam("RoleName", "pb-bad-role")
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.when().post("/").then()
|
||||
.statusCode(404);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user