mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(ec2): enforce VPC IPv4 CIDR block association restrictions (#5510)
* test(ec2): keep subnet range test secondary block in 10.0.0.0/8 * test(ec2): add same-range secondary blocks in subnet containment tests * fix(ec2): enforce VPC IPv4 CIDR block association restrictions * refactor(ec2): add Cidr4.rfc1918Range for the containing private range * fix(ec2): classify VPC CIDR ranges by containment, not overlap * test(ec2): drop the /0 primary case now that CreateVpc refuses it
This commit is contained in:
1 parent
4da5143456
commit
ac5f0bd95f
6 files changed
+183
-13
No files matched your search
@@ -4872,6 +4872,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
synchronized (attachmentTopologyLock(region)) {
|
||||
synchronized (lockFor(key(region, vpcId))) {
|
||||
Vpc vpc = getRequiredVpc(region, vpcId);
|
||||
rejectRestrictedVpcCidrAssociation(vpc, cidrBlock);
|
||||
VpcCidrBlockAssociation assoc = new VpcCidrBlockAssociation(
|
||||
"vpc-cidr-assoc-" + randomHex(8), cidrBlock);
|
||||
vpc.getCidrBlockAssociationSet().add(assoc);
|
||||
@@ -4881,6 +4882,56 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The VPC User Guide's "IPv4 CIDR block association restrictions" table. What a VPC may add
|
||||
* depends on the range its primary block came from: a VPC created in one RFC 1918 range
|
||||
* cannot add a block from another RFC 1918 range or from 198.19.0.0/16, and a VPC created with
|
||||
* a block from 198.19.0.0/16, from 100.64.0.0/10 or from publicly routable space cannot add an
|
||||
* RFC 1918 block, nor (outside 198.19.0.0/16 itself) one from 198.19.0.0/16. Two narrower rows
|
||||
* apply within a range: a 172.16.0.0/12 VPC cannot add from 172.31.0.0/16, and a 10.0.0.0/8
|
||||
* VPC with any block in 10.0.0.0/15 cannot add from 10.0.0.0/16. AWS answers each with
|
||||
* InvalidVpc.Range. Requests whose block does not parse are left to the checks that already
|
||||
* handle them.
|
||||
*/
|
||||
private void rejectRestrictedVpcCidrAssociation(Vpc vpc, String cidrBlock) {
|
||||
Optional<Cidr4> primary = Cidr4.parse(vpc.getCidrBlock());
|
||||
Optional<Cidr4> requested = Cidr4.parse(cidrBlock);
|
||||
if (primary.isEmpty() || requested.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Optional<Cidr4> primaryRange = primary.get().rfc1918Range();
|
||||
Optional<Cidr4> requestedRange = requested.get().rfc1918Range();
|
||||
boolean benchmarking = Ipv4Cidrs.overlaps(cidrBlock, "198.19.0.0/16");
|
||||
boolean restricted;
|
||||
if (primaryRange.isPresent()) {
|
||||
String range = primaryRange.get().toString();
|
||||
restricted = (requestedRange.isPresent() && !requestedRange.equals(primaryRange))
|
||||
|| benchmarking
|
||||
|| ("172.16.0.0/12".equals(range) && Ipv4Cidrs.overlaps(cidrBlock, "172.31.0.0/16"))
|
||||
|| ("10.0.0.0/8".equals(range) && Ipv4Cidrs.overlaps(cidrBlock, "10.0.0.0/16")
|
||||
&& vpcHasIpv4BlockIn(vpc, "10.0.0.0/15"));
|
||||
} else if (Ipv4Cidrs.contains("198.19.0.0/16", vpc.getCidrBlock())) {
|
||||
restricted = requestedRange.isPresent();
|
||||
} else {
|
||||
restricted = requestedRange.isPresent() || benchmarking;
|
||||
}
|
||||
if (restricted) {
|
||||
throw new AwsException("InvalidVpc.Range", "The CIDR '" + cidrBlock + "' is restricted. Use a CIDR "
|
||||
+ "from the same private address range as the current VPC CIDR, or use a publicly-routable "
|
||||
+ "CIDR. For additional restrictions, see "
|
||||
+ "https://docs.aws.amazon.com/vpc/latest/userguide/vpc-cidr-blocks.html", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean vpcHasIpv4BlockIn(Vpc vpc, String range) {
|
||||
return Ipv4Cidrs.overlaps(vpc.getCidrBlock(), range)
|
||||
|| vpc.getCidrBlockAssociationSet().stream()
|
||||
.filter(association -> "associated".equalsIgnoreCase(association.getCidrBlockState()))
|
||||
.map(VpcCidrBlockAssociation::getCidrBlock)
|
||||
.filter(Ipv4Cidrs::isIpv4)
|
||||
.anyMatch(block -> Ipv4Cidrs.overlaps(block, range));
|
||||
}
|
||||
|
||||
/**
|
||||
* Associates an Amazon-provided IPv6 block with an existing VPC: the AssociateVpcCidrBlock
|
||||
* half of {@code AmazonProvidedIpv6CidrBlock}, which the Terraform provider issues when
|
||||
|
||||
@@ -104,12 +104,20 @@ public final class Cidr4 {
|
||||
}
|
||||
|
||||
public boolean isRfc1918() {
|
||||
return rfc1918Range().isPresent();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the RFC 1918 range that wholly contains this block, or empty when it sits outside
|
||||
* all three. A block that only straddles one, such as 0.0.0.0/0, is not in it.
|
||||
*/
|
||||
public Optional<Cidr4> rfc1918Range() {
|
||||
for (Cidr4 range : RFC_1918) {
|
||||
if (range.contains(this)) {
|
||||
return true;
|
||||
return Optional.of(range);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user