mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(ec2): enforce security groups on ENI IPv6 addresses (#8593)
With security-group enforcement on, a managed ENI's identity was its IPv4 address alone, so a rule naming an IPv6 range never matched a peer on the IPv6 addresses its ENI now holds. Each protected endpoint now carries its ENI's IPv6 addresses next to the IPv4 one, and its helper's Docker IPv6 address next to the IPv4 transport. The compiler writes a peer's rules per identity: a rule matching one of its IPv6 addresses admits it on the IPv6 transport only, never on the IPv4 one, and on a Docker network without IPv6 there is no IPv6 identity to match. Every transport a peer has is dropped after its rules, as the IPv4 one already was. The EC2 and ECS registrations pass the ENI's IPv6 addresses, the firewall manager reads the helper's Docker IPv6 address, and AssignIpv6Addresses and UnassignIpv6Addresses update the ENI's firewall identity. Every Docker address a managed peer has is dropped after its rules, its IPv6 one included when its ENI holds no IPv6 address, and an ICMP rule is written only for the family it belongs to. IPv6 changes to one ENI serialize through their firewall update; an assign the firewall refuses is taken back out of the store, and an unassign reaches the firewall before it is stored. The README now documents enforcement: what it filters, that it needs rootful Docker with Linux containers, how to turn it on, and that an application port opened after launch is not published while it is on. Part of #2564.
This commit is contained in:
1 parent
6fd20879d5
commit
3e52c94e39
9 files changed
+490
-47
No files matched your search
@@ -12,6 +12,7 @@ import io.floci.aws.services.ec2.model.SecurityGroup;
|
||||
import io.floci.aws.services.ec2.model.SecurityGroupRule;
|
||||
import io.floci.aws.services.ec2.model.Tag;
|
||||
import io.floci.aws.services.ec2.model.UserIdGroupPair;
|
||||
import io.floci.aws.services.ec2.model.Vpc;
|
||||
import io.floci.aws.services.ec2.model.VpcEndpoint;
|
||||
import io.floci.aws.services.ec2.portforward.Ec2PortForwardManager;
|
||||
import io.floci.aws.testing.InMemoryStorageFactory;
|
||||
@@ -748,6 +749,118 @@ class Ec2SecurityGroupPublicationRaceTest {
|
||||
anySet());
|
||||
}
|
||||
|
||||
/** A dual-stack subnet's interface, with enforcement on and the container manager a mock. */
|
||||
private record Ipv6Fixture(Ec2Service service, Ec2ContainerManager manager, String region, String eni,
|
||||
String block) {
|
||||
List<String> held() {
|
||||
return service.describeNetworkInterfaces(region, List.of(eni), Map.of()).networkInterfaces().getFirst()
|
||||
.getIpv6Addresses();
|
||||
}
|
||||
}
|
||||
|
||||
private static Ipv6Fixture ipv6Fixture() {
|
||||
TestStores factory = new TestStores();
|
||||
EmulatorConfig config = mockConfig();
|
||||
Ec2ContainerManager manager = mock(Ec2ContainerManager.class);
|
||||
Ec2Service service = service(factory, config, manager);
|
||||
String region = "us-east-1";
|
||||
Vpc vpc = service.createVpc(region, "10.0.0.0/16", false, true);
|
||||
String block = vpc.getIpv6CidrBlockAssociationSet().getFirst().getIpv6CidrBlock();
|
||||
String subnet = service.createSubnet(region, vpc.getVpcId(), "10.0.1.0/24", region + "a", null,
|
||||
block.substring(0, block.indexOf('/')) + "/64").getSubnetId();
|
||||
String eni = service.createNetworkInterface(region, subnet, null, null, List.of(), List.of(), List.of())
|
||||
.getNetworkInterfaceId();
|
||||
enableEnforcement(config);
|
||||
return new Ipv6Fixture(service, manager, region, eni, block);
|
||||
}
|
||||
|
||||
// Catches: an IPv6 address change never reaching the interface's firewall, so peers' rules naming an IPv6 range
|
||||
// keep missing it; a call that changes nothing reaches it with nothing.
|
||||
@Test
|
||||
void ipv6AddressChangesReachTheInterfaceFirewall() {
|
||||
Ipv6Fixture fixture = ipv6Fixture();
|
||||
|
||||
List<String> assigned = fixture.service().assignIpv6Addresses(fixture.region(), fixture.eni(), null, 1);
|
||||
verify(fixture.manager()).updateIpv6Addresses(fixture.eni(), assigned);
|
||||
String block = fixture.block();
|
||||
fixture.service().unassignIpv6Addresses(fixture.region(), fixture.eni(),
|
||||
List.of(block.substring(0, block.indexOf("::")) + "::ffff"));
|
||||
fixture.service().unassignIpv6Addresses(fixture.region(), fixture.eni(), assigned);
|
||||
|
||||
verify(fixture.manager()).updateIpv6Addresses(fixture.eni(), List.of());
|
||||
verify(fixture.manager(), times(2)).updateIpv6Addresses(anyString(), anyList());
|
||||
}
|
||||
|
||||
// Catches: an assign the firewall refuses answering an error while its addresses stay assigned.
|
||||
@Test
|
||||
void anAssignTheFirewallRefusesLeavesNoAddress() {
|
||||
Ipv6Fixture fixture = ipv6Fixture();
|
||||
IllegalStateException failure = new IllegalStateException("Owned firewall failure");
|
||||
doThrow(failure).when(fixture.manager()).updateIpv6Addresses(anyString(), anyList());
|
||||
|
||||
assertSame(failure, assertThrows(IllegalStateException.class,
|
||||
() -> fixture.service().assignIpv6Addresses(fixture.region(), fixture.eni(), null, 1)));
|
||||
|
||||
assertEquals(List.of(), fixture.held());
|
||||
}
|
||||
|
||||
// Catches: an unassign the firewall refuses answering an error while its addresses are gone from the store.
|
||||
@Test
|
||||
void anUnassignTheFirewallRefusesKeepsTheAddresses() {
|
||||
Ipv6Fixture fixture = ipv6Fixture();
|
||||
List<String> assigned = fixture.service().assignIpv6Addresses(fixture.region(), fixture.eni(), null, 1);
|
||||
doThrow(new IllegalStateException("Owned firewall failure")).when(fixture.manager())
|
||||
.updateIpv6Addresses(anyString(), anyList());
|
||||
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> fixture.service().unassignIpv6Addresses(fixture.region(), fixture.eni(), assigned));
|
||||
|
||||
assertEquals(assigned, fixture.held());
|
||||
}
|
||||
|
||||
// Catches: two IPv6 changes on one interface reaching the firewall in another order than they were stored, so
|
||||
// the firewall keeps an older address list than the interface holds.
|
||||
@Test
|
||||
void ipv6ChangesOnOneInterfaceReachTheFirewallInOrder() throws Exception {
|
||||
Ipv6Fixture fixture = ipv6Fixture();
|
||||
CountDownLatch publishing = new CountDownLatch(1);
|
||||
CountDownLatch release = new CountDownLatch(1);
|
||||
AtomicReference<List<String>> firewall = new AtomicReference<>();
|
||||
doAnswer(invocation -> {
|
||||
List<String> addresses = invocation.getArgument(1);
|
||||
if (firewall.get() == null) {
|
||||
publishing.countDown();
|
||||
assertTrue(release.await(5, TimeUnit.SECONDS));
|
||||
}
|
||||
firewall.set(List.copyOf(addresses));
|
||||
return null;
|
||||
}).when(fixture.manager()).updateIpv6Addresses(anyString(), anyList());
|
||||
ExecutorService executor = Executors.newFixedThreadPool(2);
|
||||
try {
|
||||
Future<?> first = executor.submit(() ->
|
||||
fixture.service().assignIpv6Addresses(fixture.region(), fixture.eni(), null, 1));
|
||||
assertTrue(publishing.await(5, TimeUnit.SECONDS), "First publication must reach the firewall");
|
||||
AtomicReference<Thread> secondThread = new AtomicReference<>();
|
||||
Future<?> second = executor.submit(() -> {
|
||||
secondThread.set(Thread.currentThread());
|
||||
fixture.service().assignIpv6Addresses(fixture.region(), fixture.eni(), null, 1);
|
||||
});
|
||||
awaitBlockedOrDone(secondThread, second);
|
||||
boolean secondOvertook = second.isDone();
|
||||
release.countDown();
|
||||
first.get(5, TimeUnit.SECONDS);
|
||||
second.get(5, TimeUnit.SECONDS);
|
||||
|
||||
assertFalse(secondOvertook, "A second change on the interface must wait through the first publication");
|
||||
assertEquals(2, fixture.held().size());
|
||||
assertEquals(fixture.held(), firewall.get());
|
||||
} finally {
|
||||
release.countDown();
|
||||
executor.shutdownNow();
|
||||
assertTrue(executor.awaitTermination(5, TimeUnit.SECONDS));
|
||||
}
|
||||
}
|
||||
|
||||
private static String interfaceGroup(Ec2Service service, String region, String eni) {
|
||||
return service.describeNetworkInterfaces(region, List.of(eni), Map.of()).networkInterfaces().getFirst()
|
||||
.getGroups().getFirst().getGroupId();
|
||||
|
||||
@@ -2,7 +2,9 @@ package io.floci.aws.services.ec2;
|
||||
|
||||
import io.floci.aws.services.ec2.model.IpPermission;
|
||||
import io.floci.aws.services.ec2.model.IpRange;
|
||||
import io.floci.aws.services.ec2.model.Ipv6Range;
|
||||
import io.floci.aws.services.ec2.model.SecurityGroup;
|
||||
import io.floci.aws.services.ec2.model.UserIdGroupPair;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
@@ -87,6 +89,123 @@ class SecurityGroupNftCompilerTest {
|
||||
assertFalse(nft.contains("daddr 172.18.0.2 accept"), "only DNS is allowed to the resolver");
|
||||
}
|
||||
|
||||
/** A group allowing tcp/443 from {@code cidrIpv6} only. */
|
||||
private static SecurityGroup ipv6Group(String cidrIpv6) {
|
||||
SecurityGroup group = new SecurityGroup();
|
||||
group.setVpcId("vpc-1");
|
||||
group.setOwnerId("000000000000");
|
||||
IpPermission permission = new IpPermission();
|
||||
permission.setIpProtocol("tcp");
|
||||
permission.setFromPort(443);
|
||||
permission.setToPort(443);
|
||||
Ipv6Range range = new Ipv6Range();
|
||||
range.setCidrIpv6(cidrIpv6);
|
||||
permission.getIpv6Ranges().add(range);
|
||||
group.getIpPermissions().add(permission);
|
||||
return group;
|
||||
}
|
||||
|
||||
private static SecurityGroupNftCompiler.Endpoint dualStack(String eni, String logical, List<String> logicalIpv6,
|
||||
String transport, String transportIpv6,
|
||||
SecurityGroup group) {
|
||||
return new SecurityGroupNftCompiler.Endpoint("000000000000", "us-east-1", "vpc-1", eni, logical, logicalIpv6,
|
||||
transport, transportIpv6, Set.of("sg-1"), List.of(group));
|
||||
}
|
||||
|
||||
// Catches: a rule allowing a peer's IPv6 range never matching it, because only its IPv4 address was an identity.
|
||||
@Test
|
||||
void aRuleOnAnIpv6RangeMatchesThePeerOnItsIpv6Transport() {
|
||||
SecurityGroup group = ipv6Group("2600:1f18:0:a600::/64");
|
||||
SecurityGroupNftCompiler.Endpoint target = dualStack("eni-a", "10.0.0.2", List.of("2600:1f18:0:a600::10"),
|
||||
"172.17.0.2", "fd00:d0c::2", group);
|
||||
SecurityGroupNftCompiler.Endpoint peer = dualStack("eni-b", "10.0.0.3",
|
||||
List.of("2600:1f18:0:a600::20", "2600:1f18:0:a600::21"), "172.17.0.3", "fd00:d0c::3", group);
|
||||
|
||||
String nft = SecurityGroupNftCompiler.compile(target, List.of(peer), Map.of());
|
||||
|
||||
assertTrue(nft.contains("ingress ip6 saddr fd00:d0c::3 tcp dport 443 accept"));
|
||||
// An IPv6-only permission never admits the peer's IPv4 transport.
|
||||
assertFalse(nft.contains("ip saddr 172.17.0.3 tcp dport 443 accept"));
|
||||
assertTrue(nft.contains("ingress ip saddr 172.17.0.3 drop"));
|
||||
assertTrue(nft.contains("ingress ip6 saddr fd00:d0c::3 drop"));
|
||||
}
|
||||
|
||||
// Catches: a peer's IPv6 address paired with its IPv4 transport when its Docker network has no IPv6, which
|
||||
// would let IPv4 traffic through on a rule that only names an IPv6 range.
|
||||
@Test
|
||||
void aPeerWithoutAnIpv6TransportGetsNoIpv6Rule() {
|
||||
SecurityGroup group = ipv6Group("2600:1f18:0:a600::/64");
|
||||
SecurityGroupNftCompiler.Endpoint target = dualStack("eni-a", "10.0.0.2", List.of(), "172.17.0.2", null,
|
||||
group);
|
||||
SecurityGroupNftCompiler.Endpoint peer = dualStack("eni-b", "10.0.0.3", List.of("2600:1f18:0:a600::20"),
|
||||
"172.17.0.3", null, group);
|
||||
|
||||
String nft = SecurityGroupNftCompiler.compile(target, List.of(peer), Map.of());
|
||||
|
||||
assertFalse(nft.contains("172.17.0.3 tcp dport 443 accept"));
|
||||
assertTrue(nft.contains("ingress ip saddr 172.17.0.3 drop"));
|
||||
}
|
||||
|
||||
// Catches: an IPv4-only ENI on an IPv6-enabled Docker network sending on its helper's IPv6 address past the
|
||||
// managed-peer drop, straight into a broad IPv6 CIDR rule.
|
||||
@Test
|
||||
void aPeersIpv6TransportIsDroppedEvenWithoutAnIpv6Address() {
|
||||
SecurityGroup group = ipv6Group("::/0");
|
||||
SecurityGroupNftCompiler.Endpoint target = dualStack("eni-a", "10.0.0.2", List.of(), "172.17.0.2",
|
||||
"fd00:d0c::2", group);
|
||||
SecurityGroupNftCompiler.Endpoint ipv4Only = dualStack("eni-b", "10.0.0.3", List.of(), "172.17.0.3",
|
||||
"fd00:d0c::3", group);
|
||||
|
||||
String nft = SecurityGroupNftCompiler.compile(target, List.of(ipv4Only), Map.of());
|
||||
|
||||
assertTrue(nft.contains("ingress ip6 saddr fd00:d0c::3 drop"));
|
||||
assertTrue(nft.indexOf("ingress ip6 saddr fd00:d0c::3 drop")
|
||||
< nft.indexOf("ingress ip6 saddr ::/0 tcp dport 443 accept"));
|
||||
}
|
||||
|
||||
// Catches: a group-reference ICMP rule written against a peer's IPv6 transport, which nftables refuses
|
||||
// ("ip6 saddr ... icmp type"), failing the whole policy.
|
||||
@Test
|
||||
void anIcmpGroupRuleIsWrittenOnlyForThePeersIpv4Identity() {
|
||||
SecurityGroup group = new SecurityGroup();
|
||||
group.setVpcId("vpc-1");
|
||||
group.setOwnerId("000000000000");
|
||||
IpPermission echo = new IpPermission();
|
||||
echo.setIpProtocol("icmp");
|
||||
echo.setFromPort(8);
|
||||
echo.setToPort(0);
|
||||
UserIdGroupPair self = new UserIdGroupPair();
|
||||
self.setGroupId("sg-1");
|
||||
echo.getUserIdGroupPairs().add(self);
|
||||
group.getIpPermissions().add(echo);
|
||||
SecurityGroupNftCompiler.Endpoint target = dualStack("eni-a", "10.0.0.2", List.of("2600:1f18:0:a600::10"),
|
||||
"172.17.0.2", "fd00:d0c::2", group);
|
||||
SecurityGroupNftCompiler.Endpoint peer = dualStack("eni-b", "10.0.0.3", List.of("2600:1f18:0:a600::20"),
|
||||
"172.17.0.3", "fd00:d0c::3", group);
|
||||
|
||||
String nft = SecurityGroupNftCompiler.compile(target, List.of(peer), Map.of());
|
||||
|
||||
assertTrue(nft.contains("ingress ip saddr 172.17.0.3 icmp type 8 icmp code 0 accept"));
|
||||
assertFalse(nft.contains("ip6 saddr fd00:d0c::3 icmp"));
|
||||
}
|
||||
|
||||
// Catches: an address change leaving the endpoint's identity as it was registered.
|
||||
@Test
|
||||
void anEndpointTakesNewIpv6AddressesKeepingItsPolicy() {
|
||||
SecurityGroup group = ipv6Group("2600:1f18:0:a600::/64");
|
||||
SecurityGroupNftCompiler.Endpoint peer = dualStack("eni-b", "10.0.0.3", List.of(), "172.17.0.3",
|
||||
"fd00:d0c::3", group);
|
||||
SecurityGroupNftCompiler.Endpoint target = dualStack("eni-a", "10.0.0.2", List.of(), "172.17.0.2",
|
||||
"fd00:d0c::2", group);
|
||||
|
||||
String before = SecurityGroupNftCompiler.compile(target, List.of(peer), Map.of());
|
||||
String after = SecurityGroupNftCompiler.compile(target,
|
||||
List.of(peer.withIpv6Addresses(List.of("2600:1f18:0:a600::20"))), Map.of());
|
||||
|
||||
assertFalse(before.contains("ip6 saddr fd00:d0c::3 tcp dport 443 accept"));
|
||||
assertTrue(after.contains("ip6 saddr fd00:d0c::3 tcp dport 443 accept"));
|
||||
}
|
||||
|
||||
private static SecurityGroupNftCompiler.Endpoint endpoint(String eni, String logical,
|
||||
String transport, SecurityGroup group) {
|
||||
return new SecurityGroupNftCompiler.Endpoint("000000000000", "us-east-1", "vpc-1",
|
||||
|
||||
Reference in new issue
Block a user