mirror of
https://github.com/floci-io/floci.git
synced 2026-11-28 16:11:48 -05:00
fix(cloudformation): honour PermissionsBoundary on AWS::IAM::Role and AWS::IAM::User (#5266)
* fix(cloudformation): honour PermissionsBoundary on AWS::IAM::Role and AWS::IAM::User Both provisioners ignored the PermissionsBoundary property: create called the overloads without a boundary, and an update that adopts the existing entity never touched it. Create now passes the resolved boundary to createRole/createUser. On the adoption path a changed boundary is put, and one the template dropped is deleted (only when the entity currently has one). If a later step fails, the prior boundary is restored alongside the existing trust-policy/path rollback. * fix(cloudformation): treat a blank PermissionsBoundary as absent Fn::If with Ref AWS::NoValue resolves to an empty string, which the CDK bootstrap template uses for CloudFormationExecutionRole when no boundary is configured. The role and user provisioners passed that through and failed with "Policy does not exist." Resolve the property with resolveOrDefault so a blank value means no boundary. * fix(cloudformation): ID-verify permissions boundary writes on adopted IAM roles and users The boundary write and its rollback restore went through name-only IamService methods, so a role or user deleted and recreated under the same name after adoption could receive them; a rollback restoring "no boundary" would strip the replacement's boundary. Add put/delete permissions boundary overloads that take the expected immutable ID, as updateAssumeRolePolicy and updateUser already do, and pass the adopted ID for both the primary write and the restore. * fix(iam): verify role IDs under the name lock on boundary and trust-policy writes The ID-verified role writes looked the role up and wrote it back outside resourceNameLock, while createRole holds it. A role deleted and recreated between the lookup and roles.put got the stale object written over it, which is the case the ID check exists to stop. They now take the lock, as the user-side writes already do. updateUser and updateAssumeRolePolicy call requireSameUser and requireSameRole instead of repeating the check inline. * test(iam): stub the role lookup GetRole now makes roleResponsesWriteMillisecondTimestamps builds a mock IamService and stubbed getRole, but handleGetRole reads the role through getValidatedRole, which #5391 added so every role action validates its RoleName first. An unstubbed method on a Mockito mock answers null, so the handler rendered a null role and all nine parameterized rows failed with a NullPointerException on getPath(). Neither change is wrong on its own base. #5110 added this test and #5391 moved the lookup it exercises, and each was green where it was written. Together on main the class is red. The stub names getValidatedRole because that is what the handler calls. Nothing else moves: the handler's other role paths still read getRole, and so do the CloudFormation provisioner tests that stub it. --------- Co-authored-by: Priamo <pgermosen@gmail.com>
This commit is contained in:
1 parent
23e6fce6ef
commit
7d71df6ac9
9 files changed
+521
-67
No files matched your search
+34
-1
@@ -18,6 +18,7 @@ import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -70,6 +71,8 @@ public class IamRoleCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
String description = ctx.resolveOptional(props, "Description");
|
||||
List<String> managedPolicyArns = ctx.resolveStringList(props, "ManagedPolicyArns");
|
||||
// Blank means absent: Fn::If with Ref AWS::NoValue resolves to "" (CDK bootstrap does this).
|
||||
String permissionsBoundary = ctx.resolveOrDefault(props, "PermissionsBoundary", null);
|
||||
|
||||
IamRole role;
|
||||
boolean createdRole = false;
|
||||
@@ -80,8 +83,11 @@ public class IamRoleCfnProvisioner implements CfnResourceProvisioner {
|
||||
// replacement that took the name in the meantime.
|
||||
String priorAssumeRolePolicyDocument = null;
|
||||
String priorAssumeRoleId = null;
|
||||
// The adopted role's boundary before this attempt, restored on failure like the trust policy.
|
||||
String priorPermissionsBoundary = null;
|
||||
try {
|
||||
role = iamService.createRole(resolvedRoleName, path, assumeDoc, description, 3600, Map.of());
|
||||
role = iamService.createRole(resolvedRoleName, path, assumeDoc, description, 3600, Map.of(),
|
||||
permissionsBoundary);
|
||||
createdRole = true;
|
||||
r.getAttributes().put(CfnRollback.ROLLBACK_OWNED_ATTR, "true");
|
||||
} catch (AwsException e) {
|
||||
@@ -114,6 +120,7 @@ public class IamRoleCfnProvisioner implements CfnResourceProvisioner {
|
||||
// meant for the role this stack actually owns.
|
||||
priorAssumeRolePolicyDocument = role.getAssumeRolePolicyDocument();
|
||||
priorAssumeRoleId = existingRoleId;
|
||||
priorPermissionsBoundary = role.getPermissionsBoundaryArn();
|
||||
iamService.updateAssumeRolePolicy(resolvedRoleName, assumeDoc, existingRoleId);
|
||||
}
|
||||
|
||||
@@ -138,7 +145,21 @@ public class IamRoleCfnProvisioner implements CfnResourceProvisioner {
|
||||
LinkedHashSet<String> inlineWrittenByThisAttempt = new LinkedHashSet<>();
|
||||
final String documentToRestore = priorAssumeRolePolicyDocument;
|
||||
final String roleIdToRestore = priorAssumeRoleId;
|
||||
final String boundaryToRestore = priorPermissionsBoundary;
|
||||
boolean boundaryChangedByThisAttempt = false;
|
||||
try {
|
||||
// createRole() only applies PermissionsBoundary on first create. On adoption, set a
|
||||
// changed boundary, and remove one only if the role has one and the template dropped it.
|
||||
// ID-verified like the trust-policy write, here and in the rollback restore.
|
||||
if (!createdRole && !Objects.equals(permissionsBoundary, boundaryToRestore)) {
|
||||
if (permissionsBoundary != null) {
|
||||
iamService.putRolePermissionsBoundary(resolvedRoleName, permissionsBoundary, roleIdToRestore);
|
||||
} else {
|
||||
iamService.deleteRolePermissionsBoundary(resolvedRoleName, roleIdToRestore);
|
||||
}
|
||||
boundaryChangedByThisAttempt = true;
|
||||
}
|
||||
|
||||
for (String policyArn : managedPolicyArns) {
|
||||
iamService.attachRolePolicy(resolvedRoleName, policyArn);
|
||||
if (!originalPolicyArns.contains(policyArn)) {
|
||||
@@ -239,6 +260,18 @@ public class IamRoleCfnProvisioner implements CfnResourceProvisioner {
|
||||
cleanupSucceeded = false;
|
||||
}
|
||||
}
|
||||
if (boundaryChangedByThisAttempt) {
|
||||
if (!CfnRollback.attemptIamCleanup(failure,
|
||||
"restore prior permissions boundary on role " + resolvedRoleName, () -> {
|
||||
if (boundaryToRestore == null) {
|
||||
iamService.deleteRolePermissionsBoundary(resolvedRoleName, roleIdToRestore);
|
||||
} else {
|
||||
iamService.putRolePermissionsBoundary(resolvedRoleName, boundaryToRestore, roleIdToRestore);
|
||||
}
|
||||
})) {
|
||||
cleanupSucceeded = false;
|
||||
}
|
||||
}
|
||||
if (documentToRestore != null) {
|
||||
// ID-verified, same as the primary write above: if the role was deleted and
|
||||
// recreated under this name in between, this must not write the restored document
|
||||
|
||||
+33
-1
@@ -18,6 +18,7 @@ import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -65,13 +66,16 @@ public class IamUserCfnProvisioner implements CfnResourceProvisioner {
|
||||
List<String> managedPolicyArns = ctx.resolveStringList(props, "ManagedPolicyArns");
|
||||
List<String> groups = ctx.resolveStringList(props, "Groups");
|
||||
Map<String, String> tags = ctx.resolveTags(props, "Tags");
|
||||
// Blank means absent: Fn::If with Ref AWS::NoValue resolves to "" (CDK bootstrap does this).
|
||||
String permissionsBoundary = ctx.resolveOrDefault(props, "PermissionsBoundary", null);
|
||||
|
||||
IamUser user;
|
||||
boolean createdUser = false;
|
||||
String priorPath = null;
|
||||
String priorUserId = null;
|
||||
String priorPermissionsBoundary = null;
|
||||
try {
|
||||
user = iamService.createUser(resolvedUserName, resolvedPath);
|
||||
user = iamService.createUser(resolvedUserName, resolvedPath, permissionsBoundary);
|
||||
createdUser = true;
|
||||
r.getAttributes().put(CfnRollback.ROLLBACK_OWNED_ATTR, "true");
|
||||
} catch (AwsException e) {
|
||||
@@ -87,6 +91,7 @@ public class IamUserCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
priorPath = user.getPath();
|
||||
priorUserId = existingUserId;
|
||||
priorPermissionsBoundary = user.getPermissionsBoundaryArn();
|
||||
if (!resolvedPath.equals(user.getPath())) {
|
||||
iamService.updateUser(resolvedUserName, null, resolvedPath, existingUserId);
|
||||
user = iamService.getUser(resolvedUserName);
|
||||
@@ -130,8 +135,22 @@ public class IamUserCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
final String pathToRestore = priorPath;
|
||||
final String userIdToRestore = priorUserId;
|
||||
final String boundaryToRestore = priorPermissionsBoundary;
|
||||
boolean boundaryChangedByThisAttempt = false;
|
||||
|
||||
try {
|
||||
// createUser() only applies PermissionsBoundary on first create. On adoption, set a
|
||||
// changed boundary, and remove one only if the user has one and the template dropped it.
|
||||
// ID-verified like the path write, here and in the rollback restore.
|
||||
if (!createdUser && !Objects.equals(permissionsBoundary, boundaryToRestore)) {
|
||||
if (permissionsBoundary != null) {
|
||||
iamService.putUserPermissionsBoundary(resolvedUserName, permissionsBoundary, userIdToRestore);
|
||||
} else {
|
||||
iamService.deleteUserPermissionsBoundary(resolvedUserName, userIdToRestore);
|
||||
}
|
||||
boundaryChangedByThisAttempt = true;
|
||||
}
|
||||
|
||||
for (String groupName : groups) {
|
||||
iamService.addUserToGroup(groupName, resolvedUserName);
|
||||
if (!originalGroups.contains(groupName)) {
|
||||
@@ -277,6 +296,19 @@ public class IamUserCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
}
|
||||
|
||||
if (boundaryChangedByThisAttempt) {
|
||||
if (!CfnRollback.attemptIamCleanup(failure,
|
||||
"restore prior permissions boundary on user " + resolvedUserName, () -> {
|
||||
if (boundaryToRestore == null) {
|
||||
iamService.deleteUserPermissionsBoundary(resolvedUserName, userIdToRestore);
|
||||
} else {
|
||||
iamService.putUserPermissionsBoundary(resolvedUserName, boundaryToRestore, userIdToRestore);
|
||||
}
|
||||
})) {
|
||||
cleanupSucceeded = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (pathToRestore != null) {
|
||||
if (!CfnRollback.attemptIamCleanup(failure, "restore prior path on user " + resolvedUserName,
|
||||
() -> iamService.updateUser(resolvedUserName, null, pathToRestore, userIdToRestore))) {
|
||||
|
||||
@@ -784,11 +784,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
public void updateUser(String userName, String newUserName, String newPath, String expectedUserId) {
|
||||
synchronized (resourceNameLock) {
|
||||
IamUser user = getUser(userName);
|
||||
if (expectedUserId != null && !expectedUserId.equals(user.getUserId())) {
|
||||
throw new AwsException("EntityAlreadyExists",
|
||||
"User " + userName + " was replaced by a different user of the same name; "
|
||||
+ "refusing to apply an update meant for the original user.", 409);
|
||||
}
|
||||
requireSameUser(user, userName, expectedUserId);
|
||||
if (newUserName != null && !newUserName.equals(userName)) {
|
||||
boolean nameTaken = resourcesInCurrentAccount(users)
|
||||
.filter(existing -> !existing.getUserId().equals(user.getUserId()))
|
||||
@@ -1288,14 +1284,12 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
*/
|
||||
public void updateAssumeRolePolicy(String roleName, String policyDocument, String expectedRoleId) {
|
||||
validateRoleName(roleName);
|
||||
IamRole role = getRole(roleName);
|
||||
if (expectedRoleId != null && !expectedRoleId.equals(role.getRoleId())) {
|
||||
throw new AwsException("EntityAlreadyExists",
|
||||
"Role " + roleName + " was replaced by a different role of the same name; "
|
||||
+ "refusing to apply an update meant for the original role.", 409);
|
||||
synchronized (resourceNameLock) {
|
||||
IamRole role = getRole(roleName);
|
||||
requireSameRole(role, roleName, expectedRoleId);
|
||||
role.setAssumeRolePolicyDocument(policyDocument);
|
||||
roles.put(roleName, role);
|
||||
}
|
||||
role.setAssumeRolePolicyDocument(policyDocument);
|
||||
roles.put(roleName, role);
|
||||
}
|
||||
|
||||
public void tagRole(String roleName, Map<String, String> newTags) {
|
||||
@@ -4869,47 +4863,103 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
// =========================================================================
|
||||
|
||||
public void putUserPermissionsBoundary(String userName, String permissionsBoundaryArn) {
|
||||
putUserPermissionsBoundary(userName, permissionsBoundaryArn, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link #putUserPermissionsBoundary(String, String)}, but refuses the write if the user
|
||||
* named here is no longer the one with {@code expectedUserId}, like
|
||||
* {@link #updateUser(String, String, String, String)}. A null ID skips the check.
|
||||
*/
|
||||
public void putUserPermissionsBoundary(String userName, String permissionsBoundaryArn, String expectedUserId) {
|
||||
requirePolicy(permissionsBoundaryArn); // validate policy exists
|
||||
IamUser user = getUser(userName);
|
||||
user.setPermissionsBoundaryArn(permissionsBoundaryArn);
|
||||
users.put(userName, user);
|
||||
synchronized (resourceNameLock) {
|
||||
IamUser user = getUser(userName);
|
||||
requireSameUser(user, userName, expectedUserId);
|
||||
user.setPermissionsBoundaryArn(permissionsBoundaryArn);
|
||||
users.put(userName, user);
|
||||
}
|
||||
LOG.infov("Set permissions boundary for user {0}: {1}", userName, permissionsBoundaryArn);
|
||||
}
|
||||
|
||||
public void deleteUserPermissionsBoundary(String userName) {
|
||||
IamUser user = getUser(userName);
|
||||
if (user.getPermissionsBoundaryArn() == null) {
|
||||
throw new AwsException("NoSuchEntity",
|
||||
"User " + userName + " does not have a permissions boundary.", 404);
|
||||
deleteUserPermissionsBoundary(userName, null);
|
||||
}
|
||||
|
||||
/** ID-verified like {@link #putUserPermissionsBoundary(String, String, String)}. */
|
||||
public void deleteUserPermissionsBoundary(String userName, String expectedUserId) {
|
||||
synchronized (resourceNameLock) {
|
||||
IamUser user = getUser(userName);
|
||||
requireSameUser(user, userName, expectedUserId);
|
||||
if (user.getPermissionsBoundaryArn() == null) {
|
||||
throw new AwsException("NoSuchEntity",
|
||||
"User " + userName + " does not have a permissions boundary.", 404);
|
||||
}
|
||||
user.setPermissionsBoundaryArn(null);
|
||||
users.put(userName, user);
|
||||
}
|
||||
user.setPermissionsBoundaryArn(null);
|
||||
users.put(userName, user);
|
||||
LOG.infov("Deleted permissions boundary for user: {0}", userName);
|
||||
}
|
||||
|
||||
public void putRolePermissionsBoundary(String roleName, String permissionsBoundaryArn) {
|
||||
putRolePermissionsBoundary(roleName, permissionsBoundaryArn, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link #putRolePermissionsBoundary(String, String)}, but refuses the write if the role
|
||||
* named here is no longer the one with {@code expectedRoleId}, like
|
||||
* {@link #updateAssumeRolePolicy(String, String, String)}. A null ID skips the check.
|
||||
*/
|
||||
public void putRolePermissionsBoundary(String roleName, String permissionsBoundaryArn, String expectedRoleId) {
|
||||
validateRoleName(roleName);
|
||||
IamRole role = getRole(roleName);
|
||||
requireNotServiceLinked(role, roleName);
|
||||
requirePolicy(permissionsBoundaryArn); // validate policy exists
|
||||
role.setPermissionsBoundaryArn(permissionsBoundaryArn);
|
||||
roles.put(roleName, role);
|
||||
synchronized (resourceNameLock) {
|
||||
IamRole role = getRole(roleName);
|
||||
requireSameRole(role, roleName, expectedRoleId);
|
||||
requireNotServiceLinked(role, roleName);
|
||||
requirePolicy(permissionsBoundaryArn); // validate policy exists
|
||||
role.setPermissionsBoundaryArn(permissionsBoundaryArn);
|
||||
roles.put(roleName, role);
|
||||
}
|
||||
LOG.infov("Set permissions boundary for role {0}: {1}", roleName, permissionsBoundaryArn);
|
||||
}
|
||||
|
||||
public void deleteRolePermissionsBoundary(String roleName) {
|
||||
deleteRolePermissionsBoundary(roleName, null);
|
||||
}
|
||||
|
||||
/** ID-verified like {@link #putRolePermissionsBoundary(String, String, String)}. */
|
||||
public void deleteRolePermissionsBoundary(String roleName, String expectedRoleId) {
|
||||
validateRoleName(roleName);
|
||||
IamRole role = getRole(roleName);
|
||||
requireNotServiceLinked(role, roleName);
|
||||
if (role.getPermissionsBoundaryArn() == null) {
|
||||
throw new AwsException("NoSuchEntity",
|
||||
"Role " + roleName + " does not have a permissions boundary.", 404);
|
||||
synchronized (resourceNameLock) {
|
||||
IamRole role = getRole(roleName);
|
||||
requireSameRole(role, roleName, expectedRoleId);
|
||||
requireNotServiceLinked(role, roleName);
|
||||
if (role.getPermissionsBoundaryArn() == null) {
|
||||
throw new AwsException("NoSuchEntity",
|
||||
"Role " + roleName + " does not have a permissions boundary.", 404);
|
||||
}
|
||||
role.setPermissionsBoundaryArn(null);
|
||||
roles.put(roleName, role);
|
||||
}
|
||||
role.setPermissionsBoundaryArn(null);
|
||||
roles.put(roleName, role);
|
||||
LOG.infov("Deleted permissions boundary for role: {0}", roleName);
|
||||
}
|
||||
|
||||
private static void requireSameUser(IamUser user, String userName, String expectedUserId) {
|
||||
if (expectedUserId != null && !expectedUserId.equals(user.getUserId())) {
|
||||
throw new AwsException("EntityAlreadyExists",
|
||||
"User " + userName + " was replaced by a different user of the same name; "
|
||||
+ "refusing to apply an update meant for the original user.", 409);
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireSameRole(IamRole role, String roleName, String expectedRoleId) {
|
||||
if (expectedRoleId != null && !expectedRoleId.equals(role.getRoleId())) {
|
||||
throw new AwsException("EntityAlreadyExists",
|
||||
"Role " + roleName + " was replaced by a different role of the same name; "
|
||||
+ "refusing to apply an update meant for the original role.", 409);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a user for policy collection. Given an ARN, the lookup is scoped to the account the
|
||||
* ARN itself names: two accounts can hold a same-named user, and evaluating account A's ARN
|
||||
|
||||
Reference in new issue
Block a user