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
@@ -183,6 +183,13 @@ public class Ec2ContainerManager {
|
||||
}
|
||||
}
|
||||
|
||||
/** Gives a protected ENI's firewall the IPv6 addresses the ENI holds now; nothing when enforcement is off. */
|
||||
public void updateIpv6Addresses(String eniId, List<String> addresses) {
|
||||
if (firewallManager != null && firewallManager.enabled()) {
|
||||
firewallManager.updateIpv6Addresses(eniId, addresses);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateSecurityGroups(String eniId, Set<String> groupIds,
|
||||
Map<String, SecurityGroup> groups,
|
||||
Map<String, List<String>> prefixLists) {
|
||||
@@ -211,13 +218,14 @@ public class Ec2ContainerManager {
|
||||
|| !ALIASES.matches(labels, LABEL_OWNER, ownerIdentity())) {
|
||||
throw new IllegalStateException("EC2 workload network namespace is not Floci protected");
|
||||
}
|
||||
String address = helper.getNetworkSettings().getNetworks().values().stream()
|
||||
.map(network -> network.getIpAddress()).filter(ip -> ip != null && !ip.isBlank())
|
||||
.findFirst().orElseThrow(() -> new IllegalStateException("EC2 firewall helper has no Docker IP"));
|
||||
return new ProtectedNamespace(helperId, address);
|
||||
Map<String, ContainerNetwork> networks = helper.getNetworkSettings().getNetworks();
|
||||
String address = SecurityGroupFirewallManager.firstAddress(networks, ContainerNetwork::getIpAddress)
|
||||
.orElseThrow(() -> new IllegalStateException("EC2 firewall helper has no Docker IP"));
|
||||
return new ProtectedNamespace(helperId, address,
|
||||
SecurityGroupFirewallManager.firstAddress(networks, ContainerNetwork::getGlobalIPv6Address).orElse(null));
|
||||
}
|
||||
|
||||
private record ProtectedNamespace(String helperId, String transportAddress) {}
|
||||
private record ProtectedNamespace(String helperId, String transportAddress, String transportIpv6Address) {}
|
||||
|
||||
/**
|
||||
* The container whose network namespace this one joined through a {@code container:} network
|
||||
@@ -243,9 +251,9 @@ public class Ec2ContainerManager {
|
||||
String logical = instance.getLogicalPrivateIpAddress() != null
|
||||
? instance.getLogicalPrivateIpAddress() : eni.getPrivateIpAddress();
|
||||
firewallManager.register(new SecurityGroupNftCompiler.Endpoint(regionResolver.getAccountId(), region,
|
||||
instance.getVpcId(), eni.getNetworkInterfaceId(), logical, address,
|
||||
instance.getSecurityGroups().stream().map(GroupIdentifier::getGroupId)
|
||||
.collect(Collectors.toSet()), groups), helperId, prefixLists);
|
||||
instance.getVpcId(), eni.getNetworkInterfaceId(), logical, eni.getIpv6Addresses(), address,
|
||||
namespace.transportIpv6Address(), instance.getSecurityGroups().stream()
|
||||
.map(GroupIdentifier::getGroupId).collect(Collectors.toSet()), groups), helperId, prefixLists);
|
||||
}
|
||||
|
||||
@Inject
|
||||
@@ -666,8 +674,8 @@ public class Ec2ContainerManager {
|
||||
namespace = firewallManager.createNamespace("ec2", instanceId,
|
||||
regionResolver.getAccountId(), region, Optional.empty(), Map.of(22, sshHostPort));
|
||||
firewallManager.register(new SecurityGroupNftCompiler.Endpoint(regionResolver.getAccountId(),
|
||||
region, instance.getVpcId(), eniId, eni.getPrivateIpAddress(),
|
||||
namespace.transportAddress(),
|
||||
region, instance.getVpcId(), eniId, eni.getPrivateIpAddress(), eni.getIpv6Addresses(),
|
||||
namespace.transportAddress(), namespace.transportIpv6Address(),
|
||||
instance.getSecurityGroups().stream().map(g -> g.getGroupId())
|
||||
.collect(Collectors.toSet()), groups),
|
||||
namespace.helperId(), prefixLists);
|
||||
|
||||
@@ -16492,6 +16492,19 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
}
|
||||
requireIpv6Source(named, count);
|
||||
ensureDefaultResources(region);
|
||||
return withResourceLock(instanceGroupLocks, ipv6LockKey(region, networkInterfaceId),
|
||||
() -> assignIpv6AddressesLocked(region, networkInterfaceId, requested, count));
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores the addresses first, which keeps them from every other interface, then gives them to the interface's
|
||||
* firewall; a firewall that refuses them gets them back out of the store, so what is stored is what the caller
|
||||
* is told.
|
||||
*/
|
||||
private List<String> assignIpv6AddressesLocked(String region, String networkInterfaceId, List<String> requested,
|
||||
Integer count) {
|
||||
boolean named = requested != null && !requested.isEmpty();
|
||||
Ipv6Change change;
|
||||
synchronized (attachmentTopologyLock(region)) {
|
||||
synchronized (privateIpAllocationLock) {
|
||||
Ipv6Holder holder = ipv6Holder(region, networkInterfaceId);
|
||||
@@ -16505,14 +16518,23 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
? claimRequestedIpv6(region, subnet, requested, held).stream()
|
||||
.filter(address -> !held.contains(address)).toList()
|
||||
: allocateIpv6(region, subnet, count);
|
||||
List<String> updated = new ArrayList<>(held);
|
||||
updated.addAll(added);
|
||||
if (!added.isEmpty()) {
|
||||
List<String> updated = new ArrayList<>(held);
|
||||
updated.addAll(added);
|
||||
holder.store(updated);
|
||||
}
|
||||
return added;
|
||||
change = new Ipv6Change(added, updated, held);
|
||||
}
|
||||
}
|
||||
try {
|
||||
publishIpv6Change(networkInterfaceId, change);
|
||||
} catch (RuntimeException e) {
|
||||
if (!change.changed().isEmpty()) {
|
||||
restoreIpv6Addresses(region, networkInterfaceId, change.before());
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
return change.changed();
|
||||
}
|
||||
|
||||
/** UnassignIpv6Addresses: returns only the named addresses the interface held, which it no longer does. */
|
||||
@@ -16521,20 +16543,68 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
throw new AwsException("MissingParameter", "The request must contain the parameter Ipv6Addresses", 400);
|
||||
}
|
||||
ensureDefaultResources(region);
|
||||
return withResourceLock(instanceGroupLocks, ipv6LockKey(region, networkInterfaceId),
|
||||
() -> unassignIpv6AddressesLocked(region, networkInterfaceId, addresses));
|
||||
}
|
||||
|
||||
/** The firewall first, as a group change does: if it refuses the smaller identity, nothing is stored. */
|
||||
private List<String> unassignIpv6AddressesLocked(String region, String networkInterfaceId,
|
||||
List<String> addresses) {
|
||||
Ipv6Change change;
|
||||
synchronized (attachmentTopologyLock(region)) {
|
||||
synchronized (privateIpAllocationLock) {
|
||||
Ipv6Holder holder = ipv6Holder(region, networkInterfaceId);
|
||||
List<String> held = holder.addresses();
|
||||
List<String> held = ipv6Holder(region, networkInterfaceId).addresses();
|
||||
List<String> removed = addresses.stream().map(address -> canonicalIpv6(address).orElse(address))
|
||||
.filter(held::contains).distinct().toList();
|
||||
if (!removed.isEmpty()) {
|
||||
List<String> updated = new ArrayList<>(held);
|
||||
updated.removeAll(removed);
|
||||
holder.store(updated);
|
||||
}
|
||||
return removed;
|
||||
List<String> updated = new ArrayList<>(held);
|
||||
updated.removeAll(removed);
|
||||
change = new Ipv6Change(removed, updated, held);
|
||||
}
|
||||
}
|
||||
publishIpv6Change(networkInterfaceId, change);
|
||||
if (!change.changed().isEmpty()) {
|
||||
synchronized (attachmentTopologyLock(region)) {
|
||||
synchronized (privateIpAllocationLock) {
|
||||
ipv6Holder(region, networkInterfaceId).store(change.held());
|
||||
}
|
||||
}
|
||||
}
|
||||
return change.changed();
|
||||
}
|
||||
|
||||
/**
|
||||
* IPv6 changes to one interface take this lock through their firewall update, so two of them reach the firewall
|
||||
* in the order they were stored.
|
||||
*/
|
||||
private String ipv6LockKey(String region, String networkInterfaceId) {
|
||||
return "ipv6::" + callerAccountId() + "::" + key(region, networkInterfaceId);
|
||||
}
|
||||
|
||||
/** Puts back the addresses an interface held before a change its firewall refused; nothing if it is gone. */
|
||||
private void restoreIpv6Addresses(String region, String networkInterfaceId, List<String> before) {
|
||||
synchronized (attachmentTopologyLock(region)) {
|
||||
synchronized (privateIpAllocationLock) {
|
||||
try {
|
||||
ipv6Holder(region, networkInterfaceId).store(before);
|
||||
} catch (AwsException gone) {
|
||||
LOG.debugv("Not restoring IPv6 addresses of {0}: {1}", networkInterfaceId, gone.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** What an IPv6 call changed, the addresses the interface holds after it, and those it held before. */
|
||||
private record Ipv6Change(List<String> changed, List<String> held, List<String> before) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gives the interface's security-group firewall the addresses it holds now, outside the stores' locks as group
|
||||
* changes reach it, so peers' rules naming an IPv6 range match it on them. Nothing when nothing changed.
|
||||
*/
|
||||
private void publishIpv6Change(String networkInterfaceId, Ipv6Change change) {
|
||||
if (!change.changed().isEmpty() && securityGroupEnforcementEnabled() && !config.services().ec2().mock()) {
|
||||
containerManager.updateIpv6Addresses(networkInterfaceId, change.held());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.github.dockerjava.api.async.ResultCallback;
|
||||
import com.github.dockerjava.api.command.InspectContainerResponse;
|
||||
import com.github.dockerjava.api.exception.NotFoundException;
|
||||
import com.github.dockerjava.api.model.Container;
|
||||
import com.github.dockerjava.api.model.ContainerNetwork;
|
||||
import com.github.dockerjava.api.model.Frame;
|
||||
import com.github.dockerjava.api.model.Info;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
@@ -33,6 +34,7 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Function;
|
||||
|
||||
/** Owns the protected namespace and the Floci-only nftables table for each managed ENI. */
|
||||
@ApplicationScoped
|
||||
@@ -147,12 +149,13 @@ public class SecurityGroupFirewallManager {
|
||||
String helperId = null;
|
||||
try {
|
||||
helperId = lifecycleManager.createAndStart(spec).containerId();
|
||||
String ip = dockerClient.inspectContainerCmd(helperId).exec().getNetworkSettings()
|
||||
.getNetworks().values().stream().map(n -> n.getIpAddress())
|
||||
.filter(address -> address != null && !address.isBlank())
|
||||
.findFirst().orElseThrow(() -> new IllegalStateException("Firewall helper has no Docker IP"));
|
||||
Map<String, ContainerNetwork> networks = dockerClient.inspectContainerCmd(helperId).exec()
|
||||
.getNetworkSettings().getNetworks();
|
||||
String ip = firstAddress(networks, ContainerNetwork::getIpAddress)
|
||||
.orElseThrow(() -> new IllegalStateException("Firewall helper has no Docker IP"));
|
||||
apply(helperId, SecurityGroupNftCompiler.initialRuleset());
|
||||
return new Namespace(helperId, ip);
|
||||
return new Namespace(helperId, ip, firstAddress(networks, ContainerNetwork::getGlobalIPv6Address)
|
||||
.orElse(null));
|
||||
} catch (Exception e) {
|
||||
if (helperId != null) {
|
||||
lifecycleManager.removeIfExists(helperId);
|
||||
@@ -223,13 +226,31 @@ public class SecurityGroupFirewallManager {
|
||||
quarantine(current.helperId());
|
||||
throw new IllegalArgumentException("Protected endpoint needs valid security groups");
|
||||
}
|
||||
SecurityGroupNftCompiler.Endpoint updated = new SecurityGroupNftCompiler.Endpoint(
|
||||
identity.accountId(), identity.region(), identity.vpcId(), identity.eniId(),
|
||||
identity.logicalAddress(), identity.transportAddress(), Set.copyOf(groupIds), attached);
|
||||
SecurityGroupNftCompiler.Endpoint updated = identity.withPolicy(Set.copyOf(groupIds), attached);
|
||||
endpoints.put(eniId, new ProtectedEndpoint(updated, current.helperId(), Map.copyOf(prefixLists)));
|
||||
reconcileAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gives a protected ENI the IPv6 addresses it holds now, after AssignIpv6Addresses or UnassignIpv6Addresses,
|
||||
* so peers' rules naming an IPv6 range match it on those addresses. Does nothing for an ENI not protected.
|
||||
*/
|
||||
public synchronized void updateIpv6Addresses(String eniId, List<String> addresses) {
|
||||
ProtectedEndpoint current = endpoints.get(eniId);
|
||||
if (current == null) {
|
||||
return;
|
||||
}
|
||||
endpoints.put(eniId, new ProtectedEndpoint(current.endpoint().withIpv6Addresses(addresses),
|
||||
current.helperId(), current.prefixLists()));
|
||||
try {
|
||||
reconcileAll();
|
||||
} catch (RuntimeException e) {
|
||||
// The ENI keeps the addresses the caller is told it still holds.
|
||||
endpoints.computeIfPresent(eniId, (id, updated) -> current);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void reconcileAll() {
|
||||
for (Map.Entry<String, ProtectedEndpoint> entry : new ArrayList<>(endpoints.entrySet())) {
|
||||
boolean running;
|
||||
@@ -273,9 +294,7 @@ public class SecurityGroupFirewallManager {
|
||||
endpoints.values().forEach(endpoint -> quarantine(endpoint.helperId()));
|
||||
throw new IllegalStateException("A protected endpoint's security group disappeared");
|
||||
}
|
||||
SecurityGroupNftCompiler.Endpoint updated = new SecurityGroupNftCompiler.Endpoint(
|
||||
identity.accountId(), identity.region(), identity.vpcId(), identity.eniId(),
|
||||
identity.logicalAddress(), identity.transportAddress(), identity.groupIds(), attached);
|
||||
SecurityGroupNftCompiler.Endpoint updated = identity.withPolicy(identity.groupIds(), attached);
|
||||
endpoints.put(entry.getKey(), new ProtectedEndpoint(updated, current.helperId(),
|
||||
Map.copyOf(prefixLists)));
|
||||
}
|
||||
@@ -373,7 +392,19 @@ public class SecurityGroupFirewallManager {
|
||||
}
|
||||
}
|
||||
|
||||
public record Namespace(String helperId, String transportAddress) {}
|
||||
/** The address on any attached Docker network that {@code pick} gives, the first one set. */
|
||||
static Optional<String> firstAddress(Map<String, ContainerNetwork> networks,
|
||||
Function<ContainerNetwork, String> pick) {
|
||||
return networks == null ? Optional.empty() : networks.values().stream().map(pick)
|
||||
.filter(address -> address != null && !address.isBlank()).findFirst();
|
||||
}
|
||||
|
||||
/** {@code transportIpv6Address} is null when the helper's Docker network has no IPv6. */
|
||||
public record Namespace(String helperId, String transportAddress, String transportIpv6Address) {
|
||||
public Namespace(String helperId, String transportAddress) {
|
||||
this(helperId, transportAddress, null);
|
||||
}
|
||||
}
|
||||
private record ProtectedEndpoint(SecurityGroupNftCompiler.Endpoint endpoint, String helperId,
|
||||
Map<String, List<String>> prefixLists) {}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import io.floci.aws.services.ec2.model.SecurityGroup;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@@ -17,12 +18,77 @@ public final class SecurityGroupNftCompiler {
|
||||
|
||||
private SecurityGroupNftCompiler() {}
|
||||
|
||||
/**
|
||||
* A managed ENI: its logical addresses (the ENI's own, which security-group rules are written against) and the
|
||||
* Docker transport addresses its traffic actually carries. An ENI may hold several IPv6 addresses, all carried
|
||||
* by the helper's one Docker IPv6 address, which is null on a Docker network without IPv6.
|
||||
*/
|
||||
public record Endpoint(String accountId, String region, String vpcId, String eniId,
|
||||
String logicalAddress, String transportAddress, Set<String> groupIds,
|
||||
List<SecurityGroup> groups) {
|
||||
SecurityGroupPolicy.Peer peer() {
|
||||
return new SecurityGroupPolicy.Peer(logicalAddress, accountId, vpcId, groupIds);
|
||||
String logicalAddress, List<String> logicalIpv6Addresses,
|
||||
String transportAddress, String transportIpv6Address,
|
||||
Set<String> groupIds, List<SecurityGroup> groups) {
|
||||
|
||||
public Endpoint(String accountId, String region, String vpcId, String eniId,
|
||||
String logicalAddress, String transportAddress, Set<String> groupIds,
|
||||
List<SecurityGroup> groups) {
|
||||
this(accountId, region, vpcId, eniId, logicalAddress, List.of(), transportAddress, null, groupIds,
|
||||
groups);
|
||||
}
|
||||
|
||||
public Endpoint {
|
||||
logicalIpv6Addresses = logicalIpv6Addresses == null ? List.of() : List.copyOf(logicalIpv6Addresses);
|
||||
}
|
||||
|
||||
/** The same ENI identity with the policy the manager re-resolved. */
|
||||
public Endpoint withPolicy(Set<String> groupIds, List<SecurityGroup> groups) {
|
||||
return new Endpoint(accountId, region, vpcId, eniId, logicalAddress, logicalIpv6Addresses,
|
||||
transportAddress, transportIpv6Address, groupIds, groups);
|
||||
}
|
||||
|
||||
/** The same ENI and policy, holding {@code addresses} as its IPv6 addresses now. */
|
||||
public Endpoint withIpv6Addresses(List<String> addresses) {
|
||||
return new Endpoint(accountId, region, vpcId, eniId, logicalAddress, addresses, transportAddress,
|
||||
transportIpv6Address, groupIds, groups);
|
||||
}
|
||||
|
||||
SecurityGroupPolicy.Peer peer(String address) {
|
||||
return new SecurityGroupPolicy.Peer(address, accountId, vpcId, groupIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* The (logical, transport) pairs a peer's rules are written for. An IPv6 address pairs only with the IPv6
|
||||
* transport: emitted against the IPv4 one, a rule allowing a peer's IPv6 range would admit IPv4 traffic no
|
||||
* permission allows.
|
||||
*/
|
||||
List<AddressIdentity> identities() {
|
||||
List<AddressIdentity> identities = new ArrayList<>();
|
||||
if (literal(logicalAddress) && literal(transportAddress)) {
|
||||
identities.add(new AddressIdentity(logicalAddress, transportAddress));
|
||||
}
|
||||
if (literal(transportIpv6Address)) {
|
||||
for (String address : logicalIpv6Addresses) {
|
||||
if (literal(address)) {
|
||||
identities.add(new AddressIdentity(address, transportIpv6Address));
|
||||
}
|
||||
}
|
||||
}
|
||||
return identities;
|
||||
}
|
||||
|
||||
/** Every Docker address the endpoint's traffic can carry, whether or not an identity uses it. */
|
||||
List<String> transports() {
|
||||
List<String> transports = new ArrayList<>(2);
|
||||
if (literal(transportAddress)) {
|
||||
transports.add(transportAddress);
|
||||
}
|
||||
if (literal(transportIpv6Address)) {
|
||||
transports.add(transportIpv6Address);
|
||||
}
|
||||
return transports;
|
||||
}
|
||||
}
|
||||
|
||||
record AddressIdentity(String logical, String transport) {
|
||||
}
|
||||
|
||||
public static String initialRuleset() {
|
||||
@@ -66,7 +132,7 @@ public final class SecurityGroupNftCompiler {
|
||||
|
||||
List<Endpoint> managed = peers == null ? List.of() : peers.stream()
|
||||
.filter(peer -> !target.eniId().equals(peer.eniId()))
|
||||
.filter(peer -> peer.transportAddress() != null && literal(peer.transportAddress()))
|
||||
.filter(peer -> !peer.transports().isEmpty())
|
||||
.toList();
|
||||
|
||||
for (boolean egress : new boolean[]{false, true}) {
|
||||
@@ -84,19 +150,28 @@ public final class SecurityGroupNftCompiler {
|
||||
continue;
|
||||
}
|
||||
for (Endpoint peer : managed) {
|
||||
if (target.accountId().equals(peer.accountId())
|
||||
&& target.region().equals(peer.region())
|
||||
&& target.vpcId().equals(peer.vpcId())
|
||||
&& SecurityGroupPolicy.matchesPeer(group, permission, peer.peer(), prefixLists)) {
|
||||
appendRule(rules, chain, addressField, peer.transportAddress(), protocol);
|
||||
if (!target.accountId().equals(peer.accountId()) || !target.region().equals(peer.region())
|
||||
|| !target.vpcId().equals(peer.vpcId())) {
|
||||
continue;
|
||||
}
|
||||
for (AddressIdentity identity : peer.identities()) {
|
||||
if (protocolFitsFamily(permission, identity.transport())
|
||||
&& SecurityGroupPolicy.matchesPeer(group, permission, peer.peer(identity.logical()),
|
||||
prefixLists)) {
|
||||
appendRule(rules, chain, addressField, identity.transport(), protocol);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// A managed peer must never fall through to a broad external CIDR rule
|
||||
// evaluated against its Docker bridge IP instead of its logical ENI address.
|
||||
for (Endpoint peer : managed) {
|
||||
appendRule(rules, chain, addressField, peer.transportAddress(), "drop");
|
||||
// Every transport a managed peer has, its IPv6 one included when its ENI holds no IPv6 address, so its
|
||||
// traffic on that address cannot reach a broad IPv6 CIDR rule either.
|
||||
Set<String> transports = new LinkedHashSet<>();
|
||||
managed.forEach(peer -> transports.addAll(peer.transports()));
|
||||
for (String transport : transports) {
|
||||
appendRule(rules, chain, addressField, transport, "drop");
|
||||
}
|
||||
for (SecurityGroup group : target.groups()) {
|
||||
List<IpPermission> permissions = egress
|
||||
@@ -139,6 +214,20 @@ public final class SecurityGroupNftCompiler {
|
||||
.append(expression).append('\n');
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a permission's protocol can be written against {@code address}'s family: ICMP is IPv4's and ICMPv6 is
|
||||
* IPv6's, and nftables refuses an {@code icmp} header match beside an {@code ip6} address, and the reverse.
|
||||
*/
|
||||
private static boolean protocolFitsFamily(IpPermission permission, String address) {
|
||||
String protocol = permission.getIpProtocol() == null ? "" : permission.getIpProtocol().toLowerCase(Locale.ROOT);
|
||||
boolean ipv6 = address.indexOf(':') >= 0;
|
||||
return switch (protocol) {
|
||||
case "icmp", "1" -> !ipv6;
|
||||
case "icmpv6", "58" -> ipv6;
|
||||
default -> true;
|
||||
};
|
||||
}
|
||||
|
||||
private static String protocolExpression(IpPermission permission) {
|
||||
String protocol = permission.getIpProtocol();
|
||||
if (protocol == null) {
|
||||
|
||||
@@ -1370,8 +1370,9 @@ public class EcsContainerManager {
|
||||
.stream().map(PrefixListEntry::getCidr).toList()));
|
||||
}
|
||||
firewallManager.register(new SecurityGroupNftCompiler.Endpoint(regionResolver.getAccountId(),
|
||||
region, eni.getVpcId(), eniId, eni.getPrivateIpAddress(),
|
||||
namespace.transportAddress(), Set.copyOf(groupIds), groups), namespace.helperId(), prefixLists);
|
||||
region, eni.getVpcId(), eniId, eni.getPrivateIpAddress(), eni.getIpv6Addresses(),
|
||||
namespace.transportAddress(), namespace.transportIpv6Address(), Set.copyOf(groupIds), groups),
|
||||
namespace.helperId(), prefixLists);
|
||||
task.setNetworkInterfaceId(eniId);
|
||||
task.setPrivateIpAddress(eni.getPrivateIpAddress());
|
||||
return new PreparedNetwork(eni, namespace);
|
||||
|
||||
Reference in new issue
Block a user