mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 09:22:49 -05:00
fix(cloudformation): create an EKS cluster with its VPC, logging and access settings (#8747)
As the next step of #6606, an AWS::EKS::Cluster is now created with its ResourcesVpcConfig, Logging and AccessConfig, a replacement included, and ClusterSecurityGroupId resolves through Fn::GetAtt. A template naming a subnet that does not exist fails the stack, as on AWS, and an EnabledTypes entry with no Type fails the resource. An in-place change to the three is logged and not applied until Floci has UpdateClusterConfig. CreateCluster now also refuses subnets from two VPCs, a PublicAccessCidrs entry that is not a CIDR, and an IPv6 block on an IPv4 cluster, and runs every check before it creates the cluster security group, so a refused request leaves no group behind for a stack's VPC rollback.
This commit is contained in:
1 parent
252a6012b3
commit
f082117a85
8 files changed
+198
-40
No files matched your search
+65
-7
@@ -10,6 +10,7 @@ import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static io.floci.aws.services.cloudformation.CfnStackTestSupport.createUpdateChangeSet;
|
||||
import static io.floci.aws.services.cloudformation.CfnStackTestSupport.executeAndAwaitUpdate;
|
||||
@@ -20,6 +21,7 @@ import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* End-to-end check that CloudFormation provisions EKS resources for real (into EksService) rather
|
||||
@@ -232,13 +234,17 @@ class CloudFormationEksIntegrationTest {
|
||||
|
||||
@Test
|
||||
void aBlankEncryptionConfigFailsTheClusterInsteadOfCreatingItUnencrypted() {
|
||||
String stack = "cfn-eks-blank-" + Long.toString(System.nanoTime(), 36);
|
||||
try (CloudFormationClient client = AwsTestClients.create(
|
||||
CloudFormationClient.builder(), "test", Region.US_EAST_1)) {
|
||||
CfnStackWaits.createStack(client, stack, subjectStack("", "", "AWS::EKS::Cluster",
|
||||
ROLE + "\"EncryptionConfig\": \"\""), "ROLLBACK_COMPLETE");
|
||||
} finally {
|
||||
CfnStackWaits.deleteStack(stack);
|
||||
for (String blank : List.of("\"EncryptionConfig\": \"\"",
|
||||
"\"Logging\": {\"ClusterLogging\": {\"EnabledTypes\": [{}]}}",
|
||||
"\"Logging\": {\"ClusterLogging\": {\"EnabledTypes\": [{\"Type\": \"\"}]}}")) {
|
||||
String stack = "cfn-eks-blank-" + Long.toString(System.nanoTime(), 36);
|
||||
try (CloudFormationClient client = AwsTestClients.create(
|
||||
CloudFormationClient.builder(), "test", Region.US_EAST_1)) {
|
||||
CfnStackWaits.createStack(client, stack, subjectStack("", "", "AWS::EKS::Cluster",
|
||||
ROLE + blank), "ROLLBACK_COMPLETE");
|
||||
} finally {
|
||||
CfnStackWaits.deleteStack(stack);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,10 +268,62 @@ class CloudFormationEksIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aClusterHonoursItsVpcLoggingAndAccessSettings() {
|
||||
String stack = "cfn-eks-cfg-" + Long.toString(System.nanoTime(), 36);
|
||||
String template = "{\"Resources\": {"
|
||||
+ "\"Vpc\": {\"Type\": \"AWS::EC2::VPC\", \"Properties\": {\"CidrBlock\": \"10.7.0.0/16\"}},"
|
||||
+ subnet("SubA", "10.7.1.0/24", "us-east-1a") + subnet("SubB", "10.7.2.0/24", "us-east-1b")
|
||||
+ "\"Subject\": {\"Type\": \"AWS::EKS::Cluster\", \"Properties\": {" + ROLE
|
||||
+ vpcConfig("{\"Ref\": \"SubA\"}, {\"Ref\": \"SubB\"}") + ", " + SETTINGS + "}}}}";
|
||||
try (CloudFormationClient client = AwsTestClients.create(
|
||||
CloudFormationClient.builder(), "test", Region.US_EAST_1)) {
|
||||
CfnStackWaits.createStack(client, stack, template, "CREATE_COMPLETE");
|
||||
JsonPath cluster = given().get("/clusters/" + physicalId(client, stack, "Subject")).jsonPath();
|
||||
assertEquals(false, cluster.getBoolean("cluster.resourcesVpcConfig.endpointPublicAccess"));
|
||||
assertEquals(true, cluster.getBoolean("cluster.resourcesVpcConfig.endpointPrivateAccess"));
|
||||
assertEquals(Stream.of(physicalId(client, stack, "SubA"), physicalId(client, stack, "SubB")).sorted().toList(),
|
||||
cluster.getList("cluster.resourcesVpcConfig.subnetIds").stream().sorted().toList());
|
||||
assertEquals(List.of("api", "audit"), cluster.getList(
|
||||
"cluster.logging.clusterLogging.find { it.enabled == true }.types").stream().sorted().toList());
|
||||
assertEquals("API", cluster.getString("cluster.accessConfig.authenticationMode"));
|
||||
} finally {
|
||||
CfnStackWaits.deleteStack(stack);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aClusterNamingAMissingSubnetFailsTheStack() {
|
||||
String stack = "cfn-eks-nosub-" + Long.toString(System.nanoTime(), 36);
|
||||
String template = "{\"Resources\": {\"Subject\": {\"Type\": \"AWS::EKS::Cluster\", \"Properties\": {"
|
||||
+ ROLE + vpcConfig("\"subnet-0000000000000000f\"") + "}}}}";
|
||||
try (CloudFormationClient client = AwsTestClients.create(
|
||||
CloudFormationClient.builder(), "test", Region.US_EAST_1)) {
|
||||
client.createStack(request -> request.stackName(stack).templateBody(template));
|
||||
String status = CfnStackWaits.awaitTerminal(stack).status();
|
||||
assertTrue(List.of("CREATE_FAILED", "ROLLBACK_COMPLETE").contains(status), status);
|
||||
} finally {
|
||||
CfnStackWaits.deleteStack(stack);
|
||||
}
|
||||
}
|
||||
|
||||
private static final String ROLE = "\"RoleArn\": \"arn:aws:iam::000000000000:role/eks-set\", ";
|
||||
private static final String NETWORK = "\"KubernetesNetworkConfig\": {\"IpFamily\": \"ipv4\","
|
||||
+ " \"ServiceIpv4Cidr\": \"172.20.0.0/16\"}";
|
||||
|
||||
private static final String SETTINGS = "\"Logging\": {\"ClusterLogging\": {\"EnabledTypes\": ["
|
||||
+ "{\"Type\": \"api\"}, {\"Type\": \"audit\"}]}}, \"AccessConfig\": {\"AuthenticationMode\": \"API\"}";
|
||||
|
||||
private static String subnet(String id, String cidr, String zone) {
|
||||
return "\"" + id + "\": {\"Type\": \"AWS::EC2::Subnet\", \"Properties\": {\"VpcId\": {\"Ref\": \"Vpc\"},"
|
||||
+ " \"CidrBlock\": \"" + cidr + "\", \"AvailabilityZone\": \"" + zone + "\"}},";
|
||||
}
|
||||
|
||||
private static String vpcConfig(String subnetIds) {
|
||||
return "\"ResourcesVpcConfig\": {\"SubnetIds\": [" + subnetIds + "], \"EndpointPublicAccess\": false,"
|
||||
+ " \"EndpointPrivateAccess\": true}";
|
||||
}
|
||||
|
||||
private static String cluster(String role, String team) {
|
||||
return subjectStack("", "", "AWS::EKS::Cluster", "\"RoleArn\": \"arn:aws:iam::000000000000:role/" + role
|
||||
+ "\", \"Tags\": [{\"Key\": \"team\", \"Value\": \"" + team + "\"}]");
|
||||
|
||||
@@ -60,6 +60,7 @@ import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -1795,18 +1796,57 @@ class EksServiceTest {
|
||||
&& p.getUserIdGroupPairs().stream().anyMatch(u -> sgId.equals(u.getGroupId())));
|
||||
assertTrue(hasSelfEgress, "Security group should have self-referencing outbound egress rule for EFA");
|
||||
|
||||
int groupsBefore = ec2Service.describeSecurityGroups("us-east-1", List.of(), List.of(), Map.of()).size();
|
||||
KubernetesNetworkConfig badFamily = new KubernetesNetworkConfig();
|
||||
badFamily.setIpFamily("ipv5");
|
||||
CreateClusterRequest bad = new CreateClusterRequest();
|
||||
bad.setName("cluster-sg-bad-family");
|
||||
bad.setRoleArn("arn:aws:iam::000000000000:role/eks-role");
|
||||
bad.setResourcesVpcConfig(vpcConfig);
|
||||
bad.setKubernetesNetworkConfig(badFamily);
|
||||
assertEquals("InvalidParameterException",
|
||||
assertThrows(AwsException.class, () -> service.createCluster(bad)).getErrorCode());
|
||||
assertEquals(groupsBefore,
|
||||
ec2Service.describeSecurityGroups("us-east-1", List.of(), List.of(), Map.of()).size());
|
||||
String otherVpc = ec2Service.createVpc("us-east-1", "10.50.0.0/16", false).getVpcId();
|
||||
String otherSubnet = ec2Service.createSubnet("us-east-1", otherVpc, "10.50.1.0/24", "us-east-1b").getSubnetId();
|
||||
Map<String, Consumer<CreateClusterRequest>> rejected = new LinkedHashMap<>();
|
||||
rejected.put("ipFamily ipv5", r -> r.setKubernetesNetworkConfig(network("ipv5", null)));
|
||||
rejected.put("service CIDR 8.8.8.0/24", r -> r.setKubernetesNetworkConfig(network("ipv4", "8.8.8.0/24")));
|
||||
rejected.put("unknown log type", r -> r.setLogging(new Logging(List.of(new LogSetup(List.of("bogus"), true)))));
|
||||
rejected.put("subnets in two VPCs", r -> vpcConfig.setSubnetIds(List.of(subnetId, otherSubnet)));
|
||||
rejected.put("public CIDR /33", r -> vpcConfig.setPublicAccessCidrs(List.of("10.0.0.0/33")));
|
||||
rejected.put("IPv6 public CIDR on ipv4", r -> vpcConfig.setPublicAccessCidrs(List.of("::/0")));
|
||||
int row = 0;
|
||||
List<String> failed = new ArrayList<>();
|
||||
for (Map.Entry<String, Consumer<CreateClusterRequest>> entry : rejected.entrySet()) {
|
||||
vpcConfig.setSubnetIds(List.of(subnetId));
|
||||
vpcConfig.setPublicAccessCidrs(null);
|
||||
CreateClusterRequest bad = new CreateClusterRequest();
|
||||
bad.setName("cluster-sg-bad-" + row++);
|
||||
bad.setRoleArn("arn:aws:iam::000000000000:role/eks-role");
|
||||
bad.setResourcesVpcConfig(vpcConfig);
|
||||
entry.getValue().accept(bad);
|
||||
int groupsBefore = ec2Service.describeSecurityGroups("us-east-1", List.of(), List.of(), Map.of()).size();
|
||||
try {
|
||||
service.createCluster(bad);
|
||||
failed.add(entry.getKey() + ": accepted");
|
||||
} catch (AwsException e) {
|
||||
boolean clean = groupsBefore
|
||||
== ec2Service.describeSecurityGroups("us-east-1", List.of(), List.of(), Map.of()).size();
|
||||
if (!"InvalidParameterException".equals(e.getErrorCode()) || !clean) {
|
||||
failed.add(entry.getKey() + ": " + e.getErrorCode() + (clean ? "" : ", security group left"));
|
||||
}
|
||||
}
|
||||
}
|
||||
vpcConfig.setSubnetIds(List.of(subnetId));
|
||||
vpcConfig.setPublicAccessCidrs(List.of("::/0"));
|
||||
CreateClusterRequest v6 = new CreateClusterRequest();
|
||||
v6.setName("cluster-sg-ipv6");
|
||||
v6.setRoleArn("arn:aws:iam::000000000000:role/eks-role");
|
||||
v6.setResourcesVpcConfig(vpcConfig);
|
||||
v6.setKubernetesNetworkConfig(network("ipv6", null));
|
||||
try {
|
||||
service.createCluster(v6);
|
||||
} catch (AwsException e) {
|
||||
failed.add("ipv6 with ::/0: " + e.getErrorCode());
|
||||
}
|
||||
assertEquals(List.of(), failed);
|
||||
}
|
||||
|
||||
private static KubernetesNetworkConfig network(String family, String serviceCidr) {
|
||||
KubernetesNetworkConfig network = new KubernetesNetworkConfig();
|
||||
network.setIpFamily(family);
|
||||
network.setServiceIpv4Cidr(serviceCidr);
|
||||
return network;
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user