fix(elb): assign the VPC default security group to a Classic load balancer created without one (#7100)

This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-11-02 14:50:12 -04:00
1 parent fccc81b61d
commit cb093f87cb
9 files changed
+223 -27

No files matched your search

@@ -7791,6 +7791,17 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
.collect(Collectors.toList());
}
/**
* The VPC's {@code default} security group, the one EC2 creates with the VPC. A VPC holds at most
* one, since a second group of the same name is rejected as a duplicate. Empty if it was deleted;
* an unknown VPC fails the way {@link #getSecurityGroupsForVpc} does.
*/
public Optional<SecurityGroup> defaultSecurityGroupOf(String region, String vpcId) {
return getSecurityGroupsForVpc(region, vpcId, List.of()).stream()
.filter(sg -> "default".equals(sg.getGroupName()))
.findFirst();
}
public void deleteSecurityGroup(String region, String groupId) {
ensureDefaultResources(region);
if (securityGroups.get(key(region, groupId)).isEmpty()) {
@@ -14538,13 +14549,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
sgIdentifiers.add(new GroupIdentifier(sg.getGroupId(), sg.getGroupName()));
}
} else {
SecurityGroup defaultSg = securityGroups.scan(k -> k.startsWith(region + "::")).stream()
.filter(group -> subnet.getVpcId().equals(group.getVpcId())
&& "default".equals(group.getGroupName()))
.findFirst().orElse(null);
if (defaultSg != null) {
sgIdentifiers.add(new GroupIdentifier(defaultSg.getGroupId(), defaultSg.getGroupName()));
}
defaultSecurityGroupOf(region, subnet.getVpcId()).ifPresent(defaultSg ->
sgIdentifiers.add(new GroupIdentifier(defaultSg.getGroupId(), defaultSg.getGroupName())));
}
// An explicit address is checked the way RunInstances checks its PrivateIpAddress: it must be
@@ -423,12 +423,14 @@ public class ElbClassicQueryHandler {
xml.start("member").elem("InstanceId", instanceId).end("member");
}
xml.end("Instances")
.raw(healthCheckXml(lb.getHealthCheck()))
.start("SourceSecurityGroup")
.elem("OwnerAlias", lb.getSourceSecurityGroupOwnerAlias())
.elem("GroupName", lb.getSourceSecurityGroupName())
.end("SourceSecurityGroup")
.start("SecurityGroups");
.raw(healthCheckXml(lb.getHealthCheck()));
if (lb.getSourceSecurityGroupName() != null) {
xml.start("SourceSecurityGroup")
.elem("OwnerAlias", lb.getSourceSecurityGroupOwnerAlias())
.elem("GroupName", lb.getSourceSecurityGroupName())
.end("SourceSecurityGroup");
}
xml.start("SecurityGroups");
for (String sg : lb.getSecurityGroups()) {
xml.elem("member", sg);
}
@@ -229,7 +229,6 @@ public class ElbClassicService implements ServerCertificateReferenceProvider {
lb.setCreatedTime(Instant.now());
lb.setListeners(new ArrayList<>(listeners));
lb.setSecurityGroups(securityGroups != null ? new ArrayList<>(securityGroups) : new ArrayList<>());
populateSourceSecurityGroup(region, lb);
lb.setHealthCheck(ClassicHealthCheck.defaults());
if (tags != null && !tags.isEmpty()) {
lb.setTags(new LinkedHashMap<>(tags));
@@ -246,6 +245,14 @@ public class ElbClassicService implements ServerCertificateReferenceProvider {
} else if (availabilityZones != null) {
lb.setAvailabilityZones(new ArrayList<>(availabilityZones));
}
if (lb.getVpcId() != null && lb.getSecurityGroups().isEmpty()) {
// AWS associates a security group with a VPC load balancer created without one; for a
// non-default VPC that is the VPC's default group. (In a default VPC AWS creates a
// default_elb_<id> group instead; not modelled, see docs/services/elb-classic.md.)
ec2Service.defaultSecurityGroupOf(region, lb.getVpcId())
.ifPresent(sg -> lb.getSecurityGroups().add(sg.getGroupId()));
}
populateSourceSecurityGroup(region, lb);
whileCertificatesHeld(listeners, () -> {
// The certificate check again, and this is the one that counts. The one above it
@@ -7402,7 +7402,9 @@ public class RdsService implements Resettable, ResourceProvider {
endpoint.setVpcId(vpcId);
endpoint.setVpcSubnetIds(vpcSubnetIds);
endpoint.setVpcSecurityGroupIds(vpcSecurityGroupIds == null || vpcSecurityGroupIds.isEmpty()
? defaultSecurityGroupOf(vpcId, effectiveRegion) : vpcSecurityGroupIds);
? ec2Service.defaultSecurityGroupOf(effectiveRegion, vpcId).map(sg -> List.of(sg.getGroupId()))
.orElse(List.of())
: vpcSecurityGroupIds);
endpoint.setCreatedDate(Instant.now());
endpoint.setTargetRole(role);
endpoint.setEndpointNetworkType(networkType);
@@ -7528,13 +7530,6 @@ public class RdsService implements Resettable, ResourceProvider {
return endpoint;
}
private List<String> defaultSecurityGroupOf(String vpcId, String region) {
return ec2Service.getSecurityGroupsForVpc(region, vpcId, Map.of()).stream()
.filter(group -> "default".equals(group.getGroupName()))
.map(group -> group.getGroupId())
.toList();
}
private List<DbProxyEndpoint> proxyEndpointsOf(String region, String dbProxyName) {
String prefix = effectiveRegion(region) + "::";
return proxyEndpoints.scan(key -> key.startsWith(prefix)).stream()