mirror of
https://github.com/floci-io/floci.git
synced 2026-11-26 23:29:28 -05:00
fix(cloudformation): preserve security group identity and rule ownership (#7430)
Retain an unnamed security group across inline-rule updates and snapshot every EC2 revoke criterion before mutating stored permissions. Preserve immutable-property replacement and rules owned by other stacks. Share DeleteStack followed by awaitStackDeleted in CfnStackWaits for the security group and composite alarm fixtures. Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
e27aa382e1
commit
7a7fef93d9
7 files changed
+230
-19
No files matched your search
+7
-3
@@ -68,7 +68,7 @@ public class Ec2SecurityGroupCfnProvisioner implements CfnResourceProvisioner {
|
||||
String region = ctx.region();
|
||||
String groupName = ctx.resolveOptional(props, "GroupName");
|
||||
if (groupName == null || groupName.isBlank()) {
|
||||
groupName = ctx.generatePhysicalName(r.getLogicalId(), 255, false);
|
||||
groupName = null;
|
||||
}
|
||||
String description = ctx.resolveOptional(props, "GroupDescription");
|
||||
if (description == null || description.isBlank()) {
|
||||
@@ -78,8 +78,12 @@ public class Ec2SecurityGroupCfnProvisioner implements CfnResourceProvisioner {
|
||||
// provision() re-runs for every resource on every update. Re-creating an unchanged group
|
||||
// would mint a new group id (and collide on the name whenever the VPC id is stable), so
|
||||
// reuse the group this resource already points at.
|
||||
SecurityGroup reconciled = existingSecurityGroupToReconcile(ctx.priorPhysicalId(), groupName, description,
|
||||
vpcId, region);
|
||||
// An omitted name keeps the generated name, unless this update removed an explicit one.
|
||||
SecurityGroup reconciled = groupName == null && ctx.previouslyDeclared("GroupName") ? null
|
||||
: existingSecurityGroupToReconcile(ctx.priorPhysicalId(), groupName, description, vpcId, region);
|
||||
if (reconciled == null && groupName == null) {
|
||||
groupName = ctx.generatePhysicalName(r.getLogicalId(), 255, false);
|
||||
}
|
||||
final SecurityGroup sg = reconciled != null
|
||||
? reconciled
|
||||
: ec2Service.createSecurityGroup(region, groupName, description, vpcId);
|
||||
|
||||
@@ -8163,11 +8163,14 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
* <p>Returns the permissions that remain.
|
||||
*/
|
||||
private List<IpPermission> revokeSources(List<IpPermission> existing, List<IpPermission> toRemove) {
|
||||
// A caller can pass permissions stored on the group. Freeze all removal criteria before
|
||||
// mutating any of them; an emptied source list otherwise becomes a wildcard for later rules.
|
||||
List<IpPermission> removals = toRemove.stream().map(Ec2Service::copyPermissionSources).toList();
|
||||
List<IpPermission> remaining = new ArrayList<>();
|
||||
for (IpPermission perm : existing) {
|
||||
boolean dropWholePermission = false;
|
||||
boolean hadSources = hasSources(perm);
|
||||
for (IpPermission removal : toRemove) {
|
||||
for (IpPermission removal : removals) {
|
||||
if (!sameProtocolAndPorts(perm, removal)) {
|
||||
continue;
|
||||
}
|
||||
@@ -8175,8 +8178,6 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
dropWholePermission = true;
|
||||
break;
|
||||
}
|
||||
// authorize stores the caller's IpPermission object, so a revoke can name the very
|
||||
// instance held on the group. Snapshot the values before mutating either list.
|
||||
List<String> cidrs = removal.getIpRanges().stream().map(IpRange::getCidrIp).toList();
|
||||
List<String> cidrsV6 = removal.getIpv6Ranges().stream().map(Ipv6Range::getCidrIpv6).toList();
|
||||
List<String> lists = removal.getPrefixListIds().stream()
|
||||
@@ -8197,6 +8198,18 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
return remaining;
|
||||
}
|
||||
|
||||
private static IpPermission copyPermissionSources(IpPermission permission) {
|
||||
IpPermission copy = new IpPermission();
|
||||
copy.setIpProtocol(permission.getIpProtocol());
|
||||
copy.setFromPort(permission.getFromPort());
|
||||
copy.setToPort(permission.getToPort());
|
||||
copy.setIpRanges(new ArrayList<>(permission.getIpRanges()));
|
||||
copy.setIpv6Ranges(new ArrayList<>(permission.getIpv6Ranges()));
|
||||
copy.setPrefixListIds(new ArrayList<>(permission.getPrefixListIds()));
|
||||
copy.setUserIdGroupPairs(new ArrayList<>(permission.getUserIdGroupPairs()));
|
||||
return copy;
|
||||
}
|
||||
|
||||
private boolean sameProtocolAndPorts(IpPermission a, IpPermission b) {
|
||||
String protocol = SecurityGroupPolicy.normalizeProtocol(a.getIpProtocol());
|
||||
if (!Objects.equals(protocol, SecurityGroupPolicy.normalizeProtocol(b.getIpProtocol()))) {
|
||||
|
||||
Reference in new issue
Block a user