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

@@ -234,6 +234,58 @@ class Ec2ServiceTest {
}
@Test
void defaultSecurityGroupOfReturnsTheGroupNamedDefaultInThatVpc() {
Ec2Service service = mockModeService();
String region = "us-east-1";
String vpcId = service.createVpc(region, "10.94.0.0/16", false).getVpcId();
service.createSecurityGroup(region, "web", "not the default", vpcId);
SecurityGroup expected = service.describeSecurityGroups(region, List.of(), List.of(),
Map.of("vpc-id", List.of(vpcId), "group-name", List.of("default"))).getFirst();
SecurityGroup found = service.defaultSecurityGroupOf(region, vpcId).orElseThrow();
assertEquals(expected.getGroupId(), found.getGroupId());
assertEquals(vpcId, found.getVpcId());
assertEquals("default", found.getGroupName());
assertNotEquals(service.defaultSecurityGroupOf(region, service.resolveDefaultVpcId(region))
.orElseThrow().getGroupId(), found.getGroupId());
}
@Test
void createNetworkInterfaceWithoutGroupsGetsItsVpcsDefaultGroup() {
Ec2Service service = mockModeService();
String region = "us-east-1";
String vpcId = service.createVpc(region, "10.94.0.0/16", false).getVpcId();
Subnet subnet = service.createSubnet(region, vpcId, "10.94.1.0/24", region + "a");
NetworkInterface eni = service.createNetworkInterface(region, subnet.getSubnetId(), null, null,
List.of(), List.of(), List.of());
assertEquals(List.of(service.defaultSecurityGroupOf(region, vpcId).orElseThrow().getGroupId()),
eni.getGroups().stream().map(GroupIdentifier::getGroupId).toList());
}
@Test
void defaultSecurityGroupOfIsEmptyOnceTheDefaultGroupIsGone() {
Ec2Service service = mockModeService();
String region = "us-east-1";
String vpcId = service.createVpc(region, "10.94.0.0/16", false).getVpcId();
service.deleteSecurityGroup(region, service.defaultSecurityGroupOf(region, vpcId).orElseThrow().getGroupId());
assertTrue(service.defaultSecurityGroupOf(region, vpcId).isEmpty());
}
@Test
void defaultSecurityGroupOfRejectsAnUnknownVpc() {
Ec2Service service = mockModeService();
AwsException error = assertThrows(AwsException.class,
() -> service.defaultSecurityGroupOf("us-east-1", "vpc-0123456789abcdef0"));
assertEquals("InvalidVpcID.NotFound", error.getErrorCode());
}
@ParameterizedTest
@ValueSource(booleans = {false, true})
void createNetworkInterfaceRejectsMissingGroupsBeforeAllocatingAddresses(boolean includeExistingGroup) {
@@ -622,4 +622,74 @@ class ElbClassicIntegrationTest {
+ ".LoadBalancerDescriptions.member.SourceSecurityGroup.GroupName",
equalTo("elb-source-sg"));
}
@Test
@Order(20)
void vpcLoadBalancerCreatedWithoutSecurityGroupsGetsTheVpcDefaultGroup() {
String account = "444444444444";
String ec2Auth = "AWS4-HMAC-SHA256 Credential=" + account
+ "/20260427/us-east-1/ec2/aws4_request";
String elbAuth = "AWS4-HMAC-SHA256 Credential=" + account
+ "/20260427/us-east-1/elasticloadbalancing/aws4_request";
String vpcId = given()
.formParam("Action", "CreateVpc").formParam("Version", "2016-11-15")
.formParam("CidrBlock", "10.77.0.0/16")
.header("Authorization", ec2Auth)
.when().post("/")
.then().statusCode(200)
.extract().xmlPath().getString("CreateVpcResponse.vpc.vpcId");
String subnetId = given()
.formParam("Action", "CreateSubnet").formParam("Version", "2016-11-15")
.formParam("VpcId", vpcId).formParam("CidrBlock", "10.77.1.0/24")
.formParam("AvailabilityZone", "us-east-1a")
.header("Authorization", ec2Auth)
.when().post("/")
.then().statusCode(200)
.extract().xmlPath().getString("CreateSubnetResponse.subnet.subnetId");
// A second, non-default group in the VPC, so an unfiltered lookup cannot pass by luck.
given()
.formParam("Action", "CreateSecurityGroup").formParam("Version", "2016-11-15")
.formParam("GroupName", "elb-other").formParam("GroupDescription", "other")
.formParam("VpcId", vpcId)
.header("Authorization", ec2Auth)
.when().post("/")
.then().statusCode(200);
String defaultGroupId = given()
.formParam("Action", "DescribeSecurityGroups").formParam("Version", "2016-11-15")
.formParam("Filter.1.Name", "vpc-id").formParam("Filter.1.Value.1", vpcId)
.formParam("Filter.2.Name", "group-name").formParam("Filter.2.Value.1", "default")
.header("Authorization", ec2Auth)
.when().post("/")
.then().statusCode(200)
.extract().xmlPath().getString(
"DescribeSecurityGroupsResponse.securityGroupInfo.item.groupId");
assertTrue(defaultGroupId != null && defaultGroupId.startsWith("sg-"), defaultGroupId);
String name = "classic-elb-vpc-default-sg";
given()
.formParam("Action", "CreateLoadBalancer").formParam("Version", V1)
.formParam("LoadBalancerName", name)
.formParam("Subnets.member.1", subnetId)
.formParam("Listeners.member.1.Protocol", "HTTP")
.formParam("Listeners.member.1.LoadBalancerPort", "80")
.formParam("Listeners.member.1.InstanceProtocol", "HTTP")
.formParam("Listeners.member.1.InstancePort", "8080")
.header("Authorization", elbAuth)
.when().post("/")
.then().statusCode(200);
String described = "DescribeLoadBalancersResponse.DescribeLoadBalancersResult"
+ ".LoadBalancerDescriptions.member.";
given()
.formParam("Action", "DescribeLoadBalancers").formParam("Version", V1)
.formParam("LoadBalancerNames.member.1", name)
.header("Authorization", elbAuth)
.when().post("/")
.then().statusCode(200)
.body(described + "VPCId", equalTo(vpcId))
.body(described + "SecurityGroups.member.size()", equalTo(1))
.body(described + "SecurityGroups.member", equalTo(defaultGroupId))
.body(described + "SourceSecurityGroup.OwnerAlias", equalTo(account))
.body(described + "SourceSecurityGroup.GroupName", equalTo("default"));
}
}
@@ -214,4 +214,66 @@ class RdsProxyEndpointIntegrationTest {
query("DescribeDBProxyEndpoints").when().post("/").then().statusCode(200)
.body(not(containsString("<member>")));
}
@Test
@Order(7)
void anEndpointCreatedWithoutSecurityGroupsGetsItsVpcsDefaultGroup() {
String vpcId = ec2("CreateVpc").formParam("CidrBlock", "10.78.0.0/16")
.when().post("/").then().statusCode(200)
.extract().xmlPath().getString("CreateVpcResponse.vpc.vpcId");
String subnetA = subnet(vpcId, "10.78.1.0/24", "a");
String subnetB = subnet(vpcId, "10.78.2.0/24", "b");
// A second, non-default group in the VPC, so an unfiltered lookup cannot pass by luck.
ec2("CreateSecurityGroup").formParam("GroupName", "proxy-other")
.formParam("GroupDescription", "other").formParam("VpcId", vpcId)
.when().post("/").then().statusCode(200);
String defaultGroupId = ec2("DescribeSecurityGroups")
.formParam("Filter.1.Name", "vpc-id").formParam("Filter.1.Value.1", vpcId)
.formParam("Filter.2.Name", "group-name").formParam("Filter.2.Value.1", "default")
.when().post("/").then().statusCode(200)
.extract().xmlPath().getString("DescribeSecurityGroupsResponse.securityGroupInfo.item.groupId");
assertTrue(defaultGroupId != null && defaultGroupId.startsWith("sg-"), defaultGroupId);
String proxy = "pep-default-sg";
query("CreateDBProxy")
.formParam("DBProxyName", proxy)
.formParam("EngineFamily", "POSTGRESQL")
.formParam("RoleArn", "arn:aws:iam::000000000000:role/proxy")
.formParam("VpcSubnetIds.member.1", subnetA)
.formParam("VpcSubnetIds.member.2", subnetB)
.formParam("VpcSecurityGroupIds.member.1", "sg-proxy")
.formParam("Auth.member.1.AuthScheme", "SECRETS")
.formParam("Auth.member.1.SecretArn",
"arn:aws:secretsmanager:" + REGION + ":000000000000:secret:db-AbCdEf")
.when().post("/").then().statusCode(200);
query("CreateDBProxyEndpoint")
.formParam("DBProxyName", proxy)
.formParam("DBProxyEndpointName", "no-groups")
.formParam("VpcSubnetIds.member.1", subnetA)
.formParam("VpcSubnetIds.member.2", subnetB)
.when().post("/").then().statusCode(200);
query("DescribeDBProxyEndpoints").formParam("DBProxyEndpointName", "no-groups")
.when().post("/").then().statusCode(200)
.body(MEMBERS + ".VpcId", equalTo(vpcId))
.body(MEMBERS + ".VpcSecurityGroupIds.member.size()", equalTo(1))
.body(MEMBERS + ".VpcSecurityGroupIds.member", equalTo(defaultGroupId));
query("DeleteDBProxy").formParam("DBProxyName", proxy).when().post("/").then().statusCode(200);
}
private static RequestSpecification ec2(String action) {
return given().header("Authorization",
"AWS4-HMAC-SHA256 Credential=test/20261019/" + REGION + "/ec2/aws4_request")
.contentType(URLENC)
.formParam("Action", action)
.formParam("Version", "2016-11-15");
}
private static String subnet(String vpcId, String cidr, String azSuffix) {
return ec2("CreateSubnet").formParam("VpcId", vpcId).formParam("CidrBlock", cidr)
.formParam("AvailabilityZone", REGION + azSuffix)
.when().post("/").then().statusCode(200)
.extract().xmlPath().getString("CreateSubnetResponse.subnet.subnetId");
}
}
@@ -11514,11 +11514,9 @@ class RdsServiceTest {
SecurityGroup defaultGroup = new SecurityGroup();
defaultGroup.setGroupId("sg-vpc-default");
defaultGroup.setGroupName("default");
SecurityGroup otherGroup = new SecurityGroup();
otherGroup.setGroupId("sg-vpc-other");
otherGroup.setGroupName("other");
when(ec2Service.getSecurityGroupsForVpc("us-east-1", proxy.getVpcId(), Map.of()))
.thenReturn(List.of(otherGroup, defaultGroup));
// Picking the group out of the VPC is Ec2Service's job, covered in Ec2ServiceTest.
when(ec2Service.defaultSecurityGroupOf("us-east-1", proxy.getVpcId()))
.thenReturn(Optional.of(defaultGroup));
DbProxyEndpoint endpoint = rdsService.createDbProxyEndpoint("us-east-1", "app-proxy", "ep",
PROXY_SUBNET_IDS, List.of(), null, null, Map.of("env", "dev"));