fix(cloudformation): update an EKS cluster's logging, authentication mode and endpoint access in place (#8871)

* fix(cloudformation): update an EKS cluster's logging, authentication mode and endpoint access in place
* test(cloudformation): pin an EKS cluster's config updated in place and rolled back
* fix(cloudformation): fail an EKS cluster update that drops its subnet or security group list
* test(cloudformation): pin a refused EKS subnet or security group change
* fix(cloudformation): restore only the EKS cluster settings an update changed
* test(cloudformation): pin an EKS rollback that keeps settings the update did not change
This commit is contained in:
Oleksandr Nikolaichuk authored and GitHub committed 2026-11-21 09:37:14 -04:00
1 parent 896a5cde34
commit 9b19f17fe7
6 files changed
+282 -10

No files matched your search

@@ -12,6 +12,7 @@ import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Stream;
import static io.floci.aws.services.cloudformation.CfnStackTestSupport.createUpdateChangeSet;
@@ -322,6 +323,68 @@ class CloudFormationEksIntegrationTest {
}
}
@Test
void aClustersConfigIsRestoredWhenAnUpdateRollsBackAndChangedInPlaceWhenItSucceeds() {
String stack = "cfn-eks-cfgupd-" + Long.toString(System.nanoTime(), 36);
try (CloudFormationClient client = AwsTestClients.create(
CloudFormationClient.builder(), "test", Region.US_EAST_1)) {
CfnStackWaits.createStack(client, stack, configStack(false, false), "CREATE_COMPLETE");
String id = physicalId(client, stack, "Subject");
CfnStackWaits.updateStack(client, stack, configStack(true, true), "UPDATE_ROLLBACK_COMPLETE");
JsonPath cluster = given().get("/clusters/" + physicalId(client, stack, "Subject")).jsonPath();
assertEquals(true, cluster.getBoolean("cluster.resourcesVpcConfig.endpointPublicAccess"));
assertEquals("API_AND_CONFIG_MAP", cluster.getString("cluster.accessConfig.authenticationMode"));
assertEquals(List.of(), cluster.getList("cluster.logging.clusterLogging.findAll { it.enabled == true }"));
CfnStackWaits.updateStack(client, stack, configStack(true, false), "UPDATE_COMPLETE");
assertEquals(id, physicalId(client, stack, "Subject"));
cluster = given().get("/clusters/" + id).jsonPath();
assertEquals(false, cluster.getBoolean("cluster.resourcesVpcConfig.endpointPublicAccess"));
assertEquals(true, cluster.getBoolean("cluster.resourcesVpcConfig.endpointPrivateAccess"));
assertEquals(List.of("api"),
cluster.getList("cluster.logging.clusterLogging.find { it.enabled == true }.types"));
assertEquals("API", cluster.getString("cluster.accessConfig.authenticationMode"));
} finally {
CfnStackWaits.deleteStack(stack);
}
}
@Test
void aRefusedSubnetOrSecurityGroupChangeRollsTheStackBackAndAReorderingIsNoChange() {
String stack = "cfn-eks-net-" + Long.toString(System.nanoTime(), 36);
// {subnets, security groups, expected status}: a replaced subnet and a dropped list are changes, an order is not
String[][] steps = {{"SubA, SubC", "Sg", "UPDATE_ROLLBACK_COMPLETE"},
{"SubA, SubB", "", "UPDATE_ROLLBACK_COMPLETE"}, {"SubB, SubA", "Sg", "UPDATE_COMPLETE"}};
try (CloudFormationClient client = AwsTestClients.create(
CloudFormationClient.builder(), "test", Region.US_EAST_1)) {
CfnStackWaits.createStack(client, stack, netStack("SubA, SubB", "Sg"), "CREATE_COMPLETE");
JsonPath before = given().get("/clusters/" + physicalId(client, stack, "Subject")).jsonPath();
for (String[] step : steps) {
CfnStackWaits.updateStack(client, stack, netStack(step[0], step[1]), step[2]);
JsonPath after = given().get("/clusters/" + physicalId(client, stack, "Subject")).jsonPath();
for (String list : List.of("subnetIds", "securityGroupIds")) {
assertEquals(before.getList("cluster.resourcesVpcConfig." + list).stream().sorted().toList(),
after.getList("cluster.resourcesVpcConfig." + list).stream().sorted().toList(), step[0] + step[1]);
}
}
} finally {
CfnStackWaits.deleteStack(stack);
}
}
private static String netStack(String subnets, String groups) {
Function<String, String> refs = names -> names.isEmpty() ? "" : "{\"Ref\": \""
+ names.replace(", ", "\"}, {\"Ref\": \"") + "\"}";
return "{\"Resources\": {\"Vpc\": {\"Type\": \"AWS::EC2::VPC\", \"Properties\": {\"CidrBlock\": \"10.7.0.0/16\"}},"
+ "\"Sg\": {\"Type\": \"AWS::EC2::SecurityGroup\", \"Properties\": {\"GroupDescription\": \"eks\","
+ " \"VpcId\": {\"Ref\": \"Vpc\"}}}," + subnet("SubA", "10.7.1.0/24", "us-east-1a")
+ subnet("SubB", "10.7.2.0/24", "us-east-1b") + subnet("SubC", "10.7.3.0/24", "us-east-1c")
+ "\"Subject\": {\"Type\": \"AWS::EKS::Cluster\", \"Properties\": {" + ROLE
+ "\"ResourcesVpcConfig\": {\"SubnetIds\": [" + refs.apply(subnets) + "]"
+ (groups.isEmpty() ? "" : ", \"SecurityGroupIds\": [" + refs.apply(groups) + "]") + "}}}}}";
}
@Test
void aClusterNamingAMissingSubnetFailsTheStack() {
String stack = "cfn-eks-nosub-" + Long.toString(System.nanoTime(), 36);
@@ -354,6 +417,20 @@ class CloudFormationEksIntegrationTest {
+ " \"EndpointPrivateAccess\": true}";
}
/** The cluster of the config test: its original settings, or the updated ones; a failing resource follows it when asked. */
private static String configStack(boolean updated, boolean fails) {
String settings = updated
? "\"EndpointPublicAccess\": false, \"EndpointPrivateAccess\": true}, \"Logging\": {\"ClusterLogging\": "
+ "{\"EnabledTypes\": [{\"Type\": \"api\"}]}}, \"AccessConfig\": {\"AuthenticationMode\": \"API\"}"
: "\"EndpointPublicAccess\": true}, \"AccessConfig\": {\"AuthenticationMode\": \"API_AND_CONFIG_MAP\"}";
return "{\"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
+ "\"ResourcesVpcConfig\": {\"SubnetIds\": [{\"Ref\": \"SubA\"}, {\"Ref\": \"SubB\"}], " + settings
+ "}}" + (fails ? CfnStackWaits.failingResourceAfter("Subject") : "") + "}}";
}
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 + "\"}]");
@@ -10,6 +10,7 @@ import io.floci.aws.services.eks.model.CertificateAuthority;
import io.floci.aws.services.eks.model.Cluster;
import io.floci.aws.services.eks.model.ClusterIdentity;
import io.floci.aws.services.eks.model.CreateClusterRequest;
import io.floci.aws.services.eks.model.Logging;
import io.floci.aws.services.eks.model.Nodegroup;
import io.floci.aws.services.eks.model.OidcIdentity;
import org.junit.jupiter.api.Test;
@@ -18,6 +19,7 @@ import org.mockito.ArgumentCaptor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
@@ -69,6 +71,30 @@ class EksCfnProvisionerTest {
r.getAttributes().get("OpenIdConnectIssuerUrl"));
}
@Test
void aTagsOnlyUpdateRollsBackNoClusterConfig() throws Exception {
// A tags-only update saves no cluster config, so its rollback writes none back over another caller's.
Cluster created = new Cluster();
created.setName("prod");
created.setArn("arn:aws:eks:us-east-1:000000000000:cluster/prod");
when(eks.createCluster(any(CreateClusterRequest.class))).thenReturn(created);
StackResource r = resource("AWS::EKS::Cluster", "Control");
provisioner.provision(r, props("{\"Name\": \"prod\", \"RoleArn\": \"arn:aws:iam::000000000000:role/eks\"}"),
ctx());
created.setTags(new HashMap<>(Map.of("team", "a")));
created.setLogging(new Logging());
when(eks.describeCluster("prod")).thenReturn(created);
JsonNode before = props("{\"Name\": \"prod\", \"RoleArn\": \"arn:aws:iam::000000000000:role/eks\", "
+ "\"Tags\": [{\"Key\": \"team\", \"Value\": \"a\"}]}");
JsonNode after = props("{\"Name\": \"prod\", \"RoleArn\": \"arn:aws:iam::000000000000:role/eks\", "
+ "\"Tags\": [{\"Key\": \"team\", \"Value\": \"b\"}]}");
ProvisionContext update = new ProvisionContext(ctx().engine(), "us-east-1", "000000000000", "my-stack",
"prod", event -> { }, before, PropertyChanges.between("AWS::EKS::Cluster", true, before, after));
provisioner.provision(r, after, update);
assertTrue(provisioner.rollbackUpdate(r));
verify(eks, never()).restoreClusterConfig(any(), any(), any(), any());
}
@Test
void absentClusterNameIsGeneratedFromStackAndLogicalId() throws Exception {
Cluster created = new Cluster();