mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(ec2): support ModifyNetworkInterfaceAttribute (#8532)
ModifyNetworkInterfaceAttribute was not implemented, so a call to change
an interface's security groups, description, source/destination check
or DeleteOnTermination was refused, and with security-group enforcement
on an interface's groups could only change through ModifyInstanceAttribute.
The action now changes a standalone interface or one an instance was
launched with. The group list is read as SecurityGroupId.N, the name the
SDKs and the CLI send. On an instance's primary interface the groups and
the source/destination check are the instance's too. With enforcement
on, a group change reaches the interface's firewall before it is stored,
under the instance's lock, as ModifyInstanceAttribute does, so a policy
that cannot be applied leaves the groups as they were, and the ports the
instance's groups publish are reconciled after a primary-interface change.
The change re-checks the interface and its attachment in the critical
section that stores it, and AttachNetworkInterface and
DetachNetworkInterface now update the standalone record and the instance's
copy under the same topology lock.
ModifyInstanceAttribute no longer overwrites every interface's groups: on
an instance with more than one interface, a group or SourceDestCheck change
answers InvalidInstanceID ("There are multiple interfaces attached to
instance ... Please specify an interface ID for the operation instead."),
the code and message AWS returns, as the Terraform AWS provider quotes them.
The members Floci does not apply (EnablePrimaryIpv6, EnaSrdSpecification,
ConnectionTrackingSpecification, AssociatePublicIpAddress,
AssociatedSubnetIds and the attachment's ENA queue counts) answer
UnsupportedOperation, as ReplaceRoute refuses a target it cannot store,
rather than true with nothing changed.
Part of #2564.
This commit is contained in:
1 parent
a015fd9fb8
commit
82b7547f0a
6 files changed
+586
-60
No files matched your search
+181
-7
@@ -9,18 +9,27 @@ import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.ec2.Ec2Client;
|
||||
import software.amazon.awssdk.services.ec2.model.AttributeBooleanValue;
|
||||
import software.amazon.awssdk.services.ec2.model.AttributeValue;
|
||||
import software.amazon.awssdk.services.ec2.model.ConnectionTrackingSpecificationRequest;
|
||||
import software.amazon.awssdk.services.ec2.model.Ec2Exception;
|
||||
import software.amazon.awssdk.services.ec2.model.EnaSrdSpecification;
|
||||
import software.amazon.awssdk.services.ec2.model.Filter;
|
||||
import software.amazon.awssdk.services.ec2.model.GroupIdentifier;
|
||||
import software.amazon.awssdk.services.ec2.model.Instance;
|
||||
import software.amazon.awssdk.services.ec2.model.InstanceNetworkInterfaceSpecification;
|
||||
import software.amazon.awssdk.services.ec2.model.ModifyNetworkInterfaceAttributeRequest;
|
||||
import software.amazon.awssdk.services.ec2.model.NetworkInterface;
|
||||
import software.amazon.awssdk.services.ec2.model.NetworkInterfaceAttachmentChanges;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class Ec2InstanceGroupSynchronizationIntegrationTest {
|
||||
@@ -37,7 +46,8 @@ class Ec2InstanceGroupSynchronizationIntegrationTest {
|
||||
|
||||
assertEquals(List.of(fixture.replacement()), groupIds(networkInterface(fixture).groups()));
|
||||
Instance instance = instance(fixture);
|
||||
assertEquals(List.of(fixture.replacement()), groupIds(instance.securityGroups()));
|
||||
assertEquals(List.of(primaryAtLaunch ? fixture.replacement() : fixture.original()),
|
||||
groupIds(instance.securityGroups()));
|
||||
assertEquals(List.of(fixture.replacement()), groupIds(instance.networkInterfaces().stream()
|
||||
.filter(eni -> fixture.eni().equals(eni.networkInterfaceId())).findFirst().orElseThrow().groups()));
|
||||
NetworkInterface after = networkInterface(fixture);
|
||||
@@ -51,8 +61,10 @@ class Ec2InstanceGroupSynchronizationIntegrationTest {
|
||||
Fixture fixture = fixture(primaryAtLaunch);
|
||||
replace(fixture, fixture.replacement());
|
||||
fixture.client().detachNetworkInterface(request -> request.attachmentId(fixture.attachment()));
|
||||
// Leave no instance reference to the replacement: only the standalone ENI protects it.
|
||||
replace(fixture, fixture.original());
|
||||
// Leave no instance reference to the replacement: only the standalone ENI protects it. The instance now
|
||||
// has at most one interface, so the instance-level change is one AWS accepts.
|
||||
fixture.client().modifyInstanceAttribute(request -> request.instanceId(fixture.instance())
|
||||
.groups(fixture.original()));
|
||||
|
||||
Ec2Exception error = assertThrows(Ec2Exception.class, () -> fixture.client()
|
||||
.deleteSecurityGroup(request -> request.groupId(fixture.replacement())));
|
||||
@@ -66,6 +78,12 @@ class Ec2InstanceGroupSynchronizationIntegrationTest {
|
||||
void replacementReleasesThePreviousGroupForDeletion(boolean primaryAtLaunch) {
|
||||
Fixture fixture = fixture(primaryAtLaunch);
|
||||
replace(fixture, fixture.replacement());
|
||||
if (!primaryAtLaunch) {
|
||||
// The launch-created primary holds the old group too; on an instance with two interfaces each is
|
||||
// changed on its own.
|
||||
fixture.client().modifyNetworkInterfaceAttribute(request -> request
|
||||
.networkInterfaceId(primaryInterfaceId(fixture)).groups(fixture.replacement()));
|
||||
}
|
||||
|
||||
fixture.client().deleteSecurityGroup(request -> request.groupId(fixture.original()));
|
||||
|
||||
@@ -74,7 +92,7 @@ class Ec2InstanceGroupSynchronizationIntegrationTest {
|
||||
|
||||
@Test
|
||||
void invalidReplacementLeavesBothRepresentationsUnchanged() {
|
||||
Fixture fixture = fixture(false);
|
||||
Fixture fixture = fixture(true);
|
||||
NetworkInterface before = networkInterface(fixture);
|
||||
List<GroupIdentifier> beforeGroups = instance(fixture).securityGroups();
|
||||
|
||||
@@ -102,6 +120,148 @@ class Ec2InstanceGroupSynchronizationIntegrationTest {
|
||||
assertEquals(before, networkInterface(fixture));
|
||||
}
|
||||
|
||||
// Catches: ModifyNetworkInterfaceAttribute reading the group list under a name the SDKs do not send (they
|
||||
// send SecurityGroupId.N), so the call reports success and changes nothing.
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void modifyNetworkInterfaceAttributeReplacesTheInterfaceGroups(boolean primaryAtLaunch) {
|
||||
Fixture fixture = fixture(primaryAtLaunch);
|
||||
|
||||
fixture.client().modifyNetworkInterfaceAttribute(request -> request.networkInterfaceId(fixture.eni())
|
||||
.groups(fixture.replacement()));
|
||||
|
||||
assertEquals(List.of(fixture.replacement()), groupIds(networkInterface(fixture).groups()));
|
||||
// Only the primary interface's groups are the instance's.
|
||||
assertEquals(List.of(primaryAtLaunch ? fixture.replacement() : fixture.original()),
|
||||
groupIds(instance(fixture).securityGroups()));
|
||||
}
|
||||
|
||||
// Catches: an instance's launch-created primary interface, which has no standalone record, answering
|
||||
// InvalidNetworkInterfaceID.NotFound, or taking the groups on only one of its two copies.
|
||||
@Test
|
||||
void theLaunchCreatedPrimaryInterfaceTakesTheGroups() {
|
||||
Fixture fixture = fixture(false);
|
||||
String primary = primaryInterfaceId(fixture);
|
||||
|
||||
fixture.client().modifyNetworkInterfaceAttribute(request -> request.networkInterfaceId(primary)
|
||||
.groups(fixture.replacement()));
|
||||
|
||||
assertEquals(List.of(fixture.replacement()), groupIds(fixture.client().describeNetworkInterfaces(
|
||||
request -> request.networkInterfaceIds(primary)).networkInterfaces().getFirst().groups()));
|
||||
assertEquals(List.of(fixture.replacement()), groupIds(instance(fixture).securityGroups()));
|
||||
}
|
||||
|
||||
// Catches: the attributes other than groups, which Terraform sends through the same action one per call,
|
||||
// being accepted and dropped.
|
||||
@Test
|
||||
void modifyNetworkInterfaceAttributeAppliesTheOtherAttributes() {
|
||||
Fixture fixture = fixture(false);
|
||||
|
||||
fixture.client().modifyNetworkInterfaceAttribute(request -> request.networkInterfaceId(fixture.eni())
|
||||
.description(AttributeValue.builder().value("changed description").build()));
|
||||
fixture.client().modifyNetworkInterfaceAttribute(request -> request.networkInterfaceId(fixture.eni())
|
||||
.sourceDestCheck(AttributeBooleanValue.builder().value(false).build()));
|
||||
fixture.client().modifyNetworkInterfaceAttribute(request -> request.networkInterfaceId(fixture.eni())
|
||||
.attachment(NetworkInterfaceAttachmentChanges.builder().attachmentId(fixture.attachment())
|
||||
.deleteOnTermination(true).build()));
|
||||
|
||||
NetworkInterface changed = networkInterface(fixture);
|
||||
assertEquals("changed description", changed.description());
|
||||
assertFalse(changed.sourceDestCheck());
|
||||
assertTrue(changed.attachment().deleteOnTermination());
|
||||
// Back to false, so terminating the instance during cleanup leaves the interface to be deleted.
|
||||
fixture.client().modifyNetworkInterfaceAttribute(request -> request.networkInterfaceId(fixture.eni())
|
||||
.attachment(NetworkInterfaceAttachmentChanges.builder().attachmentId(fixture.attachment())
|
||||
.deleteOnTermination(false).build()));
|
||||
assertFalse(networkInterface(fixture).attachment().deleteOnTermination());
|
||||
}
|
||||
|
||||
// Catches: an instance-level group or source/destination check change on an instance with several
|
||||
// interfaces overwriting every interface, groups a secondary interface was given on its own included, where
|
||||
// AWS refuses the call and asks for the interface instead.
|
||||
@Test
|
||||
void anInstanceWithSeveralInterfacesRefusesInstanceLevelChanges() {
|
||||
Fixture fixture = fixture(false);
|
||||
fixture.client().modifyNetworkInterfaceAttribute(request -> request.networkInterfaceId(fixture.eni())
|
||||
.groups(fixture.replacement()));
|
||||
NetworkInterface before = networkInterface(fixture);
|
||||
|
||||
Ec2Exception groups = assertThrows(Ec2Exception.class, () -> fixture.client().modifyInstanceAttribute(
|
||||
request -> request.instanceId(fixture.instance()).groups(fixture.original())));
|
||||
Ec2Exception sourceDestCheck = assertThrows(Ec2Exception.class, () -> fixture.client()
|
||||
.modifyInstanceAttribute(request -> request.instanceId(fixture.instance())
|
||||
.sourceDestCheck(AttributeBooleanValue.builder().value(false).build())));
|
||||
|
||||
for (Ec2Exception error : List.of(groups, sourceDestCheck)) {
|
||||
assertEquals("InvalidInstanceID", error.awsErrorDetails().errorCode());
|
||||
assertEquals("There are multiple interfaces attached to instance '" + fixture.instance()
|
||||
+ "'. Please specify an interface ID for the operation instead.", error.awsErrorDetails().errorMessage());
|
||||
}
|
||||
assertEquals(before, networkInterface(fixture));
|
||||
assertEquals(List.of(fixture.original()), groupIds(instance(fixture).securityGroups()));
|
||||
}
|
||||
|
||||
// Catches: a member the action does not apply answering true with nothing changed; Terraform sends
|
||||
// EnablePrimaryIpv6 through this action for aws_instance.
|
||||
@Test
|
||||
void membersTheActionDoesNotApplyAreRefused() {
|
||||
Fixture fixture = fixture(false);
|
||||
List<Consumer<ModifyNetworkInterfaceAttributeRequest.Builder>> unapplied = List.of(
|
||||
request -> request.enablePrimaryIpv6(true),
|
||||
request -> request.associatePublicIpAddress(true),
|
||||
request -> request.associatedSubnetIds("subnet-00000000000000000"),
|
||||
request -> request.enaSrdSpecification(EnaSrdSpecification.builder().enaSrdEnabled(true).build()),
|
||||
request -> request.connectionTrackingSpecification(ConnectionTrackingSpecificationRequest.builder()
|
||||
.tcpEstablishedTimeout(120).build()),
|
||||
request -> request.attachment(NetworkInterfaceAttachmentChanges.builder()
|
||||
.attachmentId(fixture.attachment()).enaQueueCount(2).build()),
|
||||
request -> request.attachment(NetworkInterfaceAttachmentChanges.builder()
|
||||
.attachmentId(fixture.attachment()).defaultEnaQueueCount(true).build()));
|
||||
NetworkInterface before = networkInterface(fixture);
|
||||
|
||||
for (Consumer<ModifyNetworkInterfaceAttributeRequest.Builder> member : unapplied) {
|
||||
Ec2Exception error = assertThrows(Ec2Exception.class, () -> fixture.client()
|
||||
.modifyNetworkInterfaceAttribute(request -> member.accept(request.networkInterfaceId(fixture.eni()))));
|
||||
assertEquals("UnsupportedOperation", error.awsErrorDetails().errorCode());
|
||||
}
|
||||
assertEquals(before, networkInterface(fixture));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnknownGroupLeavesTheInterfaceUnchanged() {
|
||||
Fixture fixture = fixture(false);
|
||||
NetworkInterface before = networkInterface(fixture);
|
||||
|
||||
Ec2Exception error = assertThrows(Ec2Exception.class, () -> fixture.client().modifyNetworkInterfaceAttribute(
|
||||
request -> request.networkInterfaceId(fixture.eni()).groups(fixture.replacement(),
|
||||
"sg-00000000000000000")));
|
||||
|
||||
assertEquals("InvalidGroup.NotFound", error.awsErrorDetails().errorCode());
|
||||
assertEquals(before, networkInterface(fixture));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAttachmentTheInterfaceDoesNotHaveIsRefused() {
|
||||
Fixture fixture = fixture(false);
|
||||
|
||||
Ec2Exception error = assertThrows(Ec2Exception.class, () -> fixture.client().modifyNetworkInterfaceAttribute(
|
||||
request -> request.networkInterfaceId(fixture.eni()).attachment(NetworkInterfaceAttachmentChanges
|
||||
.builder().attachmentId("eni-attach-00000000000000000").deleteOnTermination(true).build())));
|
||||
|
||||
assertEquals("InvalidAttachmentID.NotFound", error.awsErrorDetails().errorCode());
|
||||
assertFalse(networkInterface(fixture).attachment().deleteOnTermination());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnknownInterfaceIsNotFound() {
|
||||
Fixture fixture = fixture(false);
|
||||
|
||||
Ec2Exception error = assertThrows(Ec2Exception.class, () -> fixture.client().modifyNetworkInterfaceAttribute(
|
||||
request -> request.networkInterfaceId("eni-00000000000000000").groups(fixture.replacement())));
|
||||
|
||||
assertEquals("InvalidNetworkInterfaceID.NotFound", error.awsErrorDetails().errorCode());
|
||||
}
|
||||
|
||||
private Fixture fixture(boolean primaryAtLaunch) {
|
||||
Ec2Client client = client("555566667780", Region.US_WEST_2);
|
||||
String vpc = client.createVpc(request -> request.cidrBlock("10.232.0.0/16")).vpc().vpcId();
|
||||
@@ -145,7 +305,7 @@ class Ec2InstanceGroupSynchronizationIntegrationTest {
|
||||
client.detachNetworkInterface(request -> request.attachmentId(current.attachment().attachmentId()));
|
||||
}
|
||||
});
|
||||
return new Fixture(client, instance, eni, attachment, original, replacement);
|
||||
return new Fixture(client, instance, eni, attachment, original, replacement, primaryAtLaunch);
|
||||
}
|
||||
|
||||
private Ec2Client client(String account, Region region) {
|
||||
@@ -164,8 +324,17 @@ class Ec2InstanceGroupSynchronizationIntegrationTest {
|
||||
.description("owned instance group synchronization fixture")).groupId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gives the fixture interface {@code group} the way AWS allows: through the instance while it is the instance's
|
||||
* only interface, through the interface itself once the instance has another.
|
||||
*/
|
||||
private static void replace(Fixture fixture, String group) {
|
||||
fixture.client().modifyInstanceAttribute(request -> request.instanceId(fixture.instance()).groups(group));
|
||||
if (fixture.primaryAtLaunch()) {
|
||||
fixture.client().modifyInstanceAttribute(request -> request.instanceId(fixture.instance()).groups(group));
|
||||
} else {
|
||||
fixture.client().modifyNetworkInterfaceAttribute(request -> request.networkInterfaceId(fixture.eni())
|
||||
.groups(group));
|
||||
}
|
||||
}
|
||||
|
||||
private static NetworkInterface networkInterface(Fixture fixture) {
|
||||
@@ -178,10 +347,15 @@ class Ec2InstanceGroupSynchronizationIntegrationTest {
|
||||
.reservations().getFirst().instances().getFirst();
|
||||
}
|
||||
|
||||
private static String primaryInterfaceId(Fixture fixture) {
|
||||
return instance(fixture).networkInterfaces().stream()
|
||||
.filter(eni -> eni.attachment().deviceIndex() == 0).findFirst().orElseThrow().networkInterfaceId();
|
||||
}
|
||||
|
||||
private static List<String> groupIds(List<GroupIdentifier> groups) {
|
||||
return groups.stream().map(GroupIdentifier::groupId).toList();
|
||||
}
|
||||
|
||||
private record Fixture(Ec2Client client, String instance, String eni, String attachment,
|
||||
String original, String replacement) { }
|
||||
String original, String replacement, boolean primaryAtLaunch) { }
|
||||
}
|
||||
@@ -551,8 +551,7 @@ class Ec2SecurityGroupPublicationRaceTest {
|
||||
TestStores factory = new TestStores();
|
||||
EmulatorConfig config = mockConfig();
|
||||
Ec2ContainerManager manager = mock(Ec2ContainerManager.class);
|
||||
Ec2Service service = new Ec2Service(config, manager, mock(Ec2PortForwardManager.class),
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(), factory.storage);
|
||||
Ec2Service service = service(factory, config, manager);
|
||||
String region = "us-east-1";
|
||||
String vpc = service.createVpc(region, "10.0.0.0/16", false).getVpcId();
|
||||
String subnet = service.createSubnet(region, vpc, "10.0.1.0/24", region + "a").getSubnetId();
|
||||
@@ -562,12 +561,7 @@ class Ec2SecurityGroupPublicationRaceTest {
|
||||
Instance instance = service.runInstances(region, "ami-test", "t3.micro", 1, 1, null,
|
||||
List.of(original), subnet, null, List.of(), null, null).getInstances().getFirst();
|
||||
instance.setState(InstanceState.running());
|
||||
EmulatorConfig.NetworkConfig network = mock(EmulatorConfig.NetworkConfig.class);
|
||||
EmulatorConfig.SecurityGroupEnforcementConfig enforcement = mock(EmulatorConfig.SecurityGroupEnforcementConfig.class);
|
||||
when(config.network()).thenReturn(network);
|
||||
when(network.securityGroupEnforcement()).thenReturn(enforcement);
|
||||
when(enforcement.enabled()).thenReturn(true);
|
||||
when(config.services().ec2().mock()).thenReturn(false);
|
||||
enableEnforcement(config);
|
||||
CountDownLatch applied = new CountDownLatch(1);
|
||||
CountDownLatch release = new CountDownLatch(1);
|
||||
AtomicReference<Set<String>> firewall = new AtomicReference<>();
|
||||
@@ -620,8 +614,7 @@ class Ec2SecurityGroupPublicationRaceTest {
|
||||
TestStores factory = new TestStores();
|
||||
EmulatorConfig config = mockConfig();
|
||||
Ec2ContainerManager manager = mock(Ec2ContainerManager.class);
|
||||
Ec2Service service = new Ec2Service(config, manager, mock(Ec2PortForwardManager.class),
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(), factory.storage);
|
||||
Ec2Service service = service(factory, config, manager);
|
||||
String region = "us-east-1";
|
||||
String vpc = service.createVpc(region, "10.0.0.0/16", false).getVpcId();
|
||||
String subnet = service.createSubnet(region, vpc, "10.0.1.0/24", region + "a").getSubnetId();
|
||||
@@ -631,12 +624,7 @@ class Ec2SecurityGroupPublicationRaceTest {
|
||||
Instance instance = service.runInstances(region, "ami-test", "t3.micro", 1, 1, null,
|
||||
List.of(original), subnet, null, List.of(), null, null).getInstances().getFirst();
|
||||
instance.setState(InstanceState.running());
|
||||
EmulatorConfig.NetworkConfig network = mock(EmulatorConfig.NetworkConfig.class);
|
||||
EmulatorConfig.SecurityGroupEnforcementConfig enforcement = mock(EmulatorConfig.SecurityGroupEnforcementConfig.class);
|
||||
when(config.network()).thenReturn(network);
|
||||
when(network.securityGroupEnforcement()).thenReturn(enforcement);
|
||||
when(enforcement.enabled()).thenReturn(true);
|
||||
when(config.services().ec2().mock()).thenReturn(false);
|
||||
enableEnforcement(config);
|
||||
CountDownLatch updating = new CountDownLatch(1);
|
||||
CountDownLatch release = new CountDownLatch(1);
|
||||
IllegalStateException failure = new IllegalStateException("Owned firewall failure");
|
||||
@@ -668,13 +656,109 @@ class Ec2SecurityGroupPublicationRaceTest {
|
||||
}
|
||||
}
|
||||
|
||||
// Catches: ModifyNetworkInterfaceAttribute storing new groups without telling the interface's firewall, or
|
||||
// telling it only after they are stored, so the enforced policy and the reported groups disagree.
|
||||
@Test
|
||||
void interfaceGroupChangeReachesTheFirewallBeforeItIsStored() {
|
||||
TestStores factory = new TestStores();
|
||||
EmulatorConfig config = mockConfig();
|
||||
Ec2ContainerManager manager = mock(Ec2ContainerManager.class);
|
||||
Ec2Service service = service(factory, config, manager);
|
||||
String region = "us-east-1";
|
||||
String vpc = service.createVpc(region, "10.0.0.0/16", false).getVpcId();
|
||||
String subnet = service.createSubnet(region, vpc, "10.0.1.0/24", region + "a").getSubnetId();
|
||||
String original = service.createSecurityGroup(region, "eni-original", "original", vpc).getGroupId();
|
||||
String replacement = service.createSecurityGroup(region, "eni-replacement", "replacement", vpc).getGroupId();
|
||||
Instance instance = service.runInstances(region, "ami-test", "t3.micro", 1, 1, null,
|
||||
List.of(original), subnet, null, List.of(), null, null).getInstances().getFirst();
|
||||
String eni = instance.getNetworkInterfaces().getFirst().getNetworkInterfaceId();
|
||||
enableEnforcement(config);
|
||||
AtomicReference<String> groupsWhenApplied = new AtomicReference<>();
|
||||
doAnswer(invocation -> {
|
||||
groupsWhenApplied.set(interfaceGroup(service, region, eni));
|
||||
return null;
|
||||
}).when(manager).updateSecurityGroups(eq(eni), eq(Set.of(replacement)), anyMap(), anyMap());
|
||||
|
||||
service.modifyNetworkInterfaceAttribute(region, eni,
|
||||
new Ec2Service.NetworkInterfaceAttributeChange(List.of(replacement), null, null, null, null), false);
|
||||
|
||||
assertEquals(original, groupsWhenApplied.get(), "the firewall must hear of the change before it is stored");
|
||||
assertEquals(replacement, interfaceGroup(service, region, eni));
|
||||
}
|
||||
|
||||
// Catches: a firewall that refuses the change leaving the new groups stored, or their pending reference held,
|
||||
// so the replacement group can never be deleted.
|
||||
@Test
|
||||
void aFirewallFailureLeavesTheInterfaceGroupsAsTheyWere() {
|
||||
TestStores factory = new TestStores();
|
||||
EmulatorConfig config = mockConfig();
|
||||
Ec2ContainerManager manager = mock(Ec2ContainerManager.class);
|
||||
Ec2Service service = service(factory, config, manager);
|
||||
String region = "us-east-1";
|
||||
String vpc = service.createVpc(region, "10.0.0.0/16", false).getVpcId();
|
||||
String subnet = service.createSubnet(region, vpc, "10.0.1.0/24", region + "a").getSubnetId();
|
||||
String original = service.createSecurityGroup(region, "eni-kept", "kept", vpc).getGroupId();
|
||||
String replacement = service.createSecurityGroup(region, "eni-refused", "refused", vpc).getGroupId();
|
||||
Instance instance = service.runInstances(region, "ami-test", "t3.micro", 1, 1, null,
|
||||
List.of(original), subnet, null, List.of(), null, null).getInstances().getFirst();
|
||||
String eni = instance.getNetworkInterfaces().getFirst().getNetworkInterfaceId();
|
||||
enableEnforcement(config);
|
||||
IllegalStateException failure = new IllegalStateException("Owned firewall failure");
|
||||
doThrow(failure).when(manager).updateSecurityGroups(anyString(), anySet(), anyMap(), anyMap());
|
||||
|
||||
assertSame(failure, assertThrows(IllegalStateException.class, () -> service.modifyNetworkInterfaceAttribute(
|
||||
region, eni, new Ec2Service.NetworkInterfaceAttributeChange(List.of(replacement), null, null, null,
|
||||
null), false)));
|
||||
|
||||
assertEquals(original, interfaceGroup(service, region, eni));
|
||||
service.deleteSecurityGroup(region, replacement);
|
||||
}
|
||||
|
||||
// Catches: a group change on an instance's primary interface leaving the ports those groups publish as they
|
||||
// were, where ModifyInstanceAttribute reconciles them; a secondary interface's groups publish nothing.
|
||||
@Test
|
||||
void onlyAPrimaryInterfaceGroupChangeReconcilesThePublishedPorts() {
|
||||
TestStores factory = new TestStores();
|
||||
EmulatorConfig config = mockConfig();
|
||||
Ec2PortForwardManager ports = mock(Ec2PortForwardManager.class);
|
||||
Ec2Service service = service(factory, config, mock(Ec2ContainerManager.class), ports);
|
||||
String region = "us-east-1";
|
||||
String vpc = service.createVpc(region, "10.0.0.0/16", false).getVpcId();
|
||||
String subnet = service.createSubnet(region, vpc, "10.0.1.0/24", region + "a").getSubnetId();
|
||||
String original = service.createSecurityGroup(region, "ports-original", "original", vpc).getGroupId();
|
||||
String replacement = service.createSecurityGroup(region, "ports-replacement", "replacement", vpc).getGroupId();
|
||||
Instance instance = service.runInstances(region, "ami-test", "t3.micro", 1, 1, null,
|
||||
List.of(original), subnet, null, List.of(), null, null).getInstances().getFirst();
|
||||
instance.setState(InstanceState.running());
|
||||
instance.setDockerContainerId("container-ports");
|
||||
String primary = instance.getNetworkInterfaces().getFirst().getNetworkInterfaceId();
|
||||
String secondary = service.createNetworkInterface(region, subnet, null, null, List.of(), List.of(original),
|
||||
List.of()).getNetworkInterfaceId();
|
||||
service.attachNetworkInterface(region, secondary, instance.getInstanceId(), 1);
|
||||
when(config.services().ec2().mock()).thenReturn(false);
|
||||
when(config.services().ec2().publishSecurityGroupPorts()).thenReturn(true);
|
||||
|
||||
service.modifyNetworkInterfaceAttribute(region, secondary,
|
||||
new Ec2Service.NetworkInterfaceAttributeChange(List.of(replacement), null, null, null, null), false);
|
||||
verify(ports, never()).reconcile(any(), anySet());
|
||||
|
||||
service.modifyNetworkInterfaceAttribute(region, primary,
|
||||
new Ec2Service.NetworkInterfaceAttributeChange(List.of(replacement), null, null, null, null), false);
|
||||
verify(ports).reconcile(argThat(reconciled -> instance.getInstanceId().equals(reconciled.getInstanceId())),
|
||||
anySet());
|
||||
}
|
||||
|
||||
private static String interfaceGroup(Ec2Service service, String region, String eni) {
|
||||
return service.describeNetworkInterfaces(region, List.of(eni), Map.of()).networkInterfaces().getFirst()
|
||||
.getGroups().getFirst().getGroupId();
|
||||
}
|
||||
|
||||
@Test
|
||||
void concurrentFailedAssignmentsReleaseOnlyTheirOwnPendingReference() throws Exception {
|
||||
TestStores factory = new TestStores();
|
||||
EmulatorConfig config = mockConfig();
|
||||
Ec2ContainerManager manager = mock(Ec2ContainerManager.class);
|
||||
Ec2Service service = new Ec2Service(config, manager, mock(Ec2PortForwardManager.class),
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(), factory.storage);
|
||||
Ec2Service service = service(factory, config, manager);
|
||||
String region = "us-east-1";
|
||||
String vpc = service.createVpc(region, "10.0.0.0/16", false).getVpcId();
|
||||
String subnet = service.createSubnet(region, vpc, "10.0.1.0/24", region + "a").getSubnetId();
|
||||
@@ -683,12 +767,7 @@ class Ec2SecurityGroupPublicationRaceTest {
|
||||
List<Instance> instances = service.runInstances(region, "ami-test", "t3.micro", 2, 2, null,
|
||||
List.of(original), subnet, null, List.of(), null, null).getInstances();
|
||||
instances.forEach(instance -> instance.setState(InstanceState.running()));
|
||||
EmulatorConfig.NetworkConfig network = mock(EmulatorConfig.NetworkConfig.class);
|
||||
EmulatorConfig.SecurityGroupEnforcementConfig enforcement = mock(EmulatorConfig.SecurityGroupEnforcementConfig.class);
|
||||
when(config.network()).thenReturn(network);
|
||||
when(network.securityGroupEnforcement()).thenReturn(enforcement);
|
||||
when(enforcement.enabled()).thenReturn(true);
|
||||
when(config.services().ec2().mock()).thenReturn(false);
|
||||
enableEnforcement(config);
|
||||
CountDownLatch firstEntered = new CountDownLatch(1);
|
||||
CountDownLatch secondEntered = new CountDownLatch(1);
|
||||
CountDownLatch firstRelease = new CountDownLatch(1);
|
||||
@@ -880,10 +959,28 @@ class Ec2SecurityGroupPublicationRaceTest {
|
||||
}
|
||||
|
||||
private static Ec2Service service(TestStores factory) {
|
||||
return new Ec2Service(mockConfig(), mock(Ec2ContainerManager.class), mock(Ec2PortForwardManager.class),
|
||||
return service(factory, mockConfig(), mock(Ec2ContainerManager.class));
|
||||
}
|
||||
|
||||
private static Ec2Service service(TestStores factory, EmulatorConfig config, Ec2ContainerManager manager) {
|
||||
return service(factory, config, manager, mock(Ec2PortForwardManager.class));
|
||||
}
|
||||
|
||||
private static Ec2Service service(TestStores factory, EmulatorConfig config, Ec2ContainerManager manager,
|
||||
Ec2PortForwardManager ports) {
|
||||
return new Ec2Service(config, manager, ports,
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(), factory.storage);
|
||||
}
|
||||
|
||||
private static void enableEnforcement(EmulatorConfig config) {
|
||||
EmulatorConfig.NetworkConfig network = mock(EmulatorConfig.NetworkConfig.class);
|
||||
EmulatorConfig.SecurityGroupEnforcementConfig enforcement = mock(EmulatorConfig.SecurityGroupEnforcementConfig.class);
|
||||
when(config.network()).thenReturn(network);
|
||||
when(network.securityGroupEnforcement()).thenReturn(enforcement);
|
||||
when(enforcement.enabled()).thenReturn(true);
|
||||
when(config.services().ec2().mock()).thenReturn(false);
|
||||
}
|
||||
|
||||
private static EmulatorConfig mockConfig() {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
|
||||
|
||||
Reference in new issue
Block a user