Light, fluffy, and always free - The AWS Local Emulator alternative
fix(eks): populate clusterSecurityGroupId on cluster responses (#3924)
Simone Primarosa51467dbc23cc
diff --git a/docs/services/eks.md b/docs/services/eks.mdindex e159594b6d..13cb29fd5e 100644--- a/docs/services/eks.md+++ b/docs/services/eks.md@@ -67,6 +67,39 @@ aws --endpoint-url http://localhost:4566 eks create-access-entry \--type EC2_LINUX```+## Cluster security group++When an EKS cluster is created with a VPC (either specified directly or resolved from subnets), Floci provisions a dedicated EC2 cluster security group and populates its ID in `cluster.resourcesVpcConfig.clusterSecurityGroupId` on both `CreateCluster` and `DescribeCluster` responses.++### Naming convention and description++The generated security group name follows the AWS pattern:++```+eks-cluster-sg-<cluster-name>-<suffix>+```++where `<suffix>` is an 8-character random hexadecimal string. The security group description is set to:++```+EKS created security group applied to ENI that is attached to EKS Control Plane master nodes, as well as any managed workloads.+```++### System tags++Floci tags the cluster security group with the standard AWS system tags:++- `Name`: `eks-cluster-sg-<cluster-name>-<suffix>`+- `kubernetes.io/cluster/<cluster-name>`: `owned`+- `aws:eks:cluster-name`: `<cluster-name>`++### Lifecycle management and rules++- **Creation**: The security group is created in the resolved cluster VPC when the cluster is created. Both real and mock clusters receive a security group. If a cluster is created without any VPC or subnets, `clusterSecurityGroupId` is omitted from the response. If a caller explicitly provides a `resourcesVpcConfig.vpcId` that does not exist in EC2, the request is rejected with `InvalidParameterException` (HTTP 400).+- **Deletion**: When the cluster is deleted via `DeleteCluster`, Floci deletes the associated cluster security group from EC2. If the security group was already removed out-of-band, the deletion succeeds idempotently without error.+- **Backfill**: Existing persisted clusters that were created before this feature receive an auto-generated cluster security group during startup backfill if their VPC is present.+- **Rules**: In accordance with AWS EKS behavior, the cluster security group includes a self-referencing inbound rule allowing all traffic from members of the same security group to enable control plane and node communication, a self-referencing outbound rule for Elastic Fabric Adapter (EFA) traffic, and the standard EC2 outbound rule.+## Modes### Mock mode (`mock: true`)diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksService.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksService.javaindex d6c99b0492..27ddc6386c 100644--- a/src/main/java/io/github/hectorvent/floci/services/eks/EksService.java+++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksService.java@@ -4,11 +4,16 @@ import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.AwsArnUtils;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.common.RegionResolver;+import io.github.hectorvent.floci.core.common.RequestScopes;import io.github.hectorvent.floci.core.common.TagHandler;import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;import io.github.hectorvent.floci.core.storage.StorageBackend;import io.github.hectorvent.floci.core.storage.StorageFactory;import io.github.hectorvent.floci.services.ec2.Ec2Service;+import io.github.hectorvent.floci.services.ec2.model.IpPermission;+import io.github.hectorvent.floci.services.ec2.model.SecurityGroup;+import io.github.hectorvent.floci.services.ec2.model.Tag;+import io.github.hectorvent.floci.services.ec2.model.UserIdGroupPair;import io.github.hectorvent.floci.services.eks.model.CertificateAuthority;import io.github.hectorvent.floci.services.eks.model.Cluster;import io.github.hectorvent.floci.services.eks.model.AccessConfig;@@ -35,19 +40,21 @@ import org.jboss.logging.Logger;import java.time.Instant;import java.util.ArrayList;import java.util.HashMap;+import java.util.HexFormat;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;import java.util.Optional;+import java.util.Set;import java.util.UUID;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;+import java.util.concurrent.ThreadLocalRandom;import java.util.concurrent.TimeUnit;import java.util.stream.Collectors;import io.github.hectorvent.floci.core.resource.ExplorerResource;import io.github.hectorvent.floci.core.resource.ResourceProvider;import io.github.hectorvent.floci.core.resource.SupportedResourceType;-import java.util.Set;@ApplicationScopedpublic class EksService implements TagHandler, ResourceProvider {@@ -58,6 +65,9 @@ public class EksService implements TagHandler, ResourceProvider {* qualifier relies on — see EksClusterManager#accountQualifiedName. */static final String CLUSTER_NAME_REGEX = "[0-9A-Za-z][A-Za-z0-9\\-_]*";+ private static final String CLUSTER_SG_DESCRIPTION =+ "EKS created security group applied to ENI that is attached to EKS Control Plane master nodes, as well as any managed workloads.";+private final StorageBackend<String, Cluster> storage;private final StorageBackend<String, Nodegroup> nodeGroupStorage;private final StorageBackend<String, FargateProfile> fargateProfileStorage;@@ -93,6 +103,7 @@ public class EksService implements TagHandler, ResourceProvider {@PostConstructpublic void init() {backfillOidcIdentities();+ backfillClusterSecurityGroups();if (!config.services().eks().mock()) {restorePersistedClusters();startReadinessPoller();@@ -201,7 +212,7 @@ public class EksService implements TagHandler, ResourceProvider {}}- private void putClusterForAccount(String accountId, Cluster cluster) {+ void putClusterForAccount(String accountId, Cluster cluster) {if (storage instanceof AccountAwareStorageBackend<Cluster> aware) {aware.putForAccount(accountId, cluster.getName(), cluster);return;@@ -209,6 +220,126 @@ public class EksService implements TagHandler, ResourceProvider {storage.put(cluster.getName(), cluster);}+ void deleteClusterForAccount(String accountId, String clusterName) {+ if (storage instanceof AccountAwareStorageBackend<Cluster> aware) {+ aware.deleteForAccount(accountId, clusterName);+ return;+ }+ storage.delete(clusterName);+ }++ private SecurityGroup createClusterSecurityGroup(String region, String clusterName, String vpcId) {+ String suffix = randomHex(8);+ String groupName = "eks-cluster-sg-" + clusterName + "-" + suffix;+ SecurityGroup sg = ec2Service.createSecurityGroup(region, groupName, CLUSTER_SG_DESCRIPTION, vpcId);+ try {+ List<Tag> tags = List.of(+ new Tag("Name", groupName),+ new Tag("kubernetes.io/cluster/" + clusterName, "owned"),+ new Tag("aws:eks:cluster-name", clusterName)+ );+ ec2Service.createTags(region, List.of(sg.getGroupId()), tags);++ UserIdGroupPair selfPair = new UserIdGroupPair();+ selfPair.setGroupId(sg.getGroupId());++ IpPermission selfIngress = new IpPermission();+ selfIngress.setIpProtocol("-1");+ selfIngress.setUserIdGroupPairs(List.of(selfPair));+ ec2Service.authorizeSecurityGroupIngress(region, sg.getGroupId(), List.of(selfIngress));++ IpPermission selfEgress = new IpPermission();+ selfEgress.setIpProtocol("-1");+ selfEgress.setUserIdGroupPairs(List.of(selfPair));+ ec2Service.authorizeSecurityGroupEgress(region, sg.getGroupId(), List.of(selfEgress));++ return sg;+ } catch (RuntimeException e) {+ try {+ ec2Service.deleteSecurityGroup(region, sg.getGroupId());+ } catch (Exception cleanupEx) {+ LOG.warnv("Failed to clean up cluster security group {0} after configuration failure: {1}",+ sg.getGroupId(), cleanupEx.getMessage());+ }+ throw e;+ }+ }++ private void deleteClusterSecurityGroup(Cluster cluster) {+ if (ec2Service == null || cluster.getResourcesVpcConfig() == null) {+ return;+ }+ String sgId = cluster.getResourcesVpcConfig().getClusterSecurityGroupId();+ if (sgId == null || sgId.isBlank()) {+ return;+ }+ String region = resolveClusterRegion(cluster);+ try {+ ec2Service.deleteSecurityGroup(region, sgId);+ } catch (AwsException e) {+ if ("InvalidGroup.NotFound".equals(e.getErrorCode())) {+ LOG.debugv("Cluster security group {0} already gone, treating as deleted", sgId);+ } else {+ throw e;+ }+ }+ }++ void backfillClusterSecurityGroups() {+ if (ec2Service == null) {+ return;+ }+ for (AccountAwareStorageBackend.AccountEntry<Cluster> entry : allClusterEntries()) {+ Cluster cluster = entry.value();+ String accountId = entry.accountId();+ if (cluster.getAccountId() == null) {+ cluster.setAccountId(accountId);+ }++ ResourcesVpcConfig vpcConfig = cluster.getResourcesVpcConfig();+ if (vpcConfig == null) {+ continue;+ }+ if (vpcConfig.getClusterSecurityGroupId() != null && !vpcConfig.getClusterSecurityGroupId().isBlank()) {+ continue;+ }+ if (vpcConfig.getVpcId() == null || vpcConfig.getVpcId().isBlank()) {+ continue;+ }++ try {+ RequestScopes.runAs(accountId, () -> {+ String region = resolveClusterRegion(cluster);+ SecurityGroup sg = createClusterSecurityGroup(region, cluster.getName(), vpcConfig.getVpcId());+ vpcConfig.setClusterSecurityGroupId(sg.getGroupId());+ putClusterForAccount(accountId, cluster);+ LOG.infov("Backfilled cluster security group {0} for existing EKS cluster {1} in account {2}",+ sg.getGroupId(), cluster.getName(), accountId);+ });+ } catch (Exception e) {+ LOG.warnv("Could not backfill cluster security group for existing EKS cluster {0} in account {1}: {2}",+ cluster.getName(), accountId, e.getMessage());+ }+ }+ }++ private String resolveClusterRegion(Cluster cluster) {+ if (cluster.getArn() != null && !cluster.getArn().isBlank()) {+ try {+ return AwsArnUtils.parse(cluster.getArn()).region();+ } catch (IllegalArgumentException ignored) {+ // Not a valid ARN, fall back to configured region+ }+ }+ return config != null ? config.defaultRegion() : regionResolver.getRegion();+ }++ private static String randomHex(int len) {+ byte[] bytes = new byte[(len + 1) / 2];+ ThreadLocalRandom.current().nextBytes(bytes);+ return HexFormat.of().formatHex(bytes).substring(0, len);+ }+@PreDestroypublic void shutdown() {poller.shutdownNow();@@ -261,34 +392,61 @@ public class EksService implements TagHandler, ResourceProvider {cluster.setCreatedAt(Instant.now());cluster.setVersion(request.getVersion() != null ? request.getVersion() : "1.29");cluster.setRoleArn(request.getRoleArn());- cluster.setResourcesVpcConfig(buildVpcConfigResponse(request.getResourcesVpcConfig(), resolvedVpcId));+ ResourcesVpcConfig vpcConfig = buildVpcConfigResponse(request.getResourcesVpcConfig(), resolvedVpcId);+ SecurityGroup clusterSg = null;+ if (ec2Service != null && !vpcConfig.getVpcId().isBlank()) {+ try {+ clusterSg = createClusterSecurityGroup(region, name, vpcConfig.getVpcId());+ vpcConfig.setClusterSecurityGroupId(clusterSg.getGroupId());+ } catch (AwsException e) {+ if ("InvalidVpcID.NotFound".equals(e.getErrorCode())) {+ throw new AwsException("InvalidParameterException",+ "VPC '" + vpcConfig.getVpcId() + "' does not exist", 400);+ }+ throw e;+ }+ }+ cluster.setResourcesVpcConfig(vpcConfig);cluster.setKubernetesNetworkConfig(buildNetworkConfig(request.getKubernetesNetworkConfig()));cluster.setStatus(ClusterStatus.CREATING);cluster.setTags(request.getTags() != null ? new HashMap<>(request.getTags()) : new HashMap<>());cluster.setPlatformVersion("eks.1");cluster.setCertificateAuthority(new CertificateAuthority(""));- String issuer = oidcService.newIssuerUrl(region);- cluster.setIdentity(new ClusterIdentity(new OidcIdentity(issuer)));- oidcService.ensureKey(name, issuer);+ try {+ String issuer = oidcService.newIssuerUrl(region);+ cluster.setIdentity(new ClusterIdentity(new OidcIdentity(issuer)));+ oidcService.ensureKey(name, issuer);++ if (config.services().eks().mock()) {+ markMetadataOnlyActive(cluster);+ } else {+ try {+ if (!clusterManager.tryStartCluster(cluster)) {+ // No Docker daemon: the k3s control plane cannot run, but the cluster record is+ // metadata that stands on its own. FAILED is reserved for provisioning errors+ // AWS would also report, and would strand every IaC apply that polls for ACTIVE.+ markMetadataOnlyActive(cluster);+ }+ } catch (Exception e) {+ LOG.errorv("Failed to start k3s container for cluster {0}: {1}", name, e.getMessage());+ cluster.setStatus(ClusterStatus.FAILED);+ }+ }- if (config.services().eks().mock()) {- markMetadataOnlyActive(cluster);- } else {- try {- if (!clusterManager.tryStartCluster(cluster)) {- // No Docker daemon: the k3s control plane cannot run, but the cluster record is- // metadata that stands on its own. FAILED is reserved for provisioning errors- // AWS would also report, and would strand every IaC apply that polls for ACTIVE.- markMetadataOnlyActive(cluster);+ storage.put(name, cluster);+ } catch (RuntimeException e) {+ if (clusterSg != null) {+ try {+ ec2Service.deleteSecurityGroup(region, clusterSg.getGroupId());+ } catch (Exception cleanupEx) {+ LOG.warnv("Failed to clean up cluster security group {0} after cluster creation failure: {1}",+ clusterSg.getGroupId(), cleanupEx.getMessage());}- } catch (Exception e) {- LOG.errorv("Failed to start k3s container for cluster {0}: {1}", name, e.getMessage());- cluster.setStatus(ClusterStatus.FAILED);}+ throw e;}- storage.put(name, cluster);return cluster;}@@ -329,6 +487,7 @@ public class EksService implements TagHandler, ResourceProvider {if (!config.services().eks().mock()) {clusterManager.stopCluster(cluster);}+ deleteClusterSecurityGroup(cluster);accessEntries.deleteClusterEntries(cluster);storage.delete(name);oidcService.deleteKey(name);diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/model/ResourcesVpcConfig.java b/src/main/java/io/github/hectorvent/floci/services/eks/model/ResourcesVpcConfig.javaindex 267d796914..0f6c363dee 100644--- a/src/main/java/io/github/hectorvent/floci/services/eks/model/ResourcesVpcConfig.java+++ b/src/main/java/io/github/hectorvent/floci/services/eks/model/ResourcesVpcConfig.java@@ -1,6 +1,7 @@package io.github.hectorvent.floci.services.eks.model;import com.fasterxml.jackson.annotation.JsonIgnoreProperties;+import com.fasterxml.jackson.annotation.JsonInclude;import com.fasterxml.jackson.annotation.JsonProperty;import io.quarkus.runtime.annotations.RegisterForReflection;@@ -17,6 +18,7 @@ public class ResourcesVpcConfig {private List<String> securityGroupIds;@JsonProperty("clusterSecurityGroupId")+ @JsonInclude(JsonInclude.Include.NON_NULL)private String clusterSecurityGroupId;@JsonProperty("vpcId")diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterSecurityGroupIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterSecurityGroupIntegrationTest.javanew file mode 100644index 0000000000..e7dbad6a87--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterSecurityGroupIntegrationTest.java@@ -0,0 +1,209 @@+package io.github.hectorvent.floci.services.eks;++import io.github.hectorvent.floci.core.common.RequestScopes;+import io.github.hectorvent.floci.services.ec2.Ec2Service;+import io.github.hectorvent.floci.services.ec2.model.SecurityGroup;+import io.github.hectorvent.floci.services.eks.model.Cluster;+import io.github.hectorvent.floci.services.eks.model.ClusterStatus;+import io.github.hectorvent.floci.services.eks.model.ResourcesVpcConfig;+import io.quarkus.test.junit.QuarkusTest;+import jakarta.inject.Inject;+import org.junit.jupiter.api.MethodOrderer;+import org.junit.jupiter.api.Order;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.TestMethodOrder;++import java.util.List;+import java.util.Map;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.containsString;+import static org.hamcrest.Matchers.equalTo;+import static org.hamcrest.Matchers.notNullValue;+import static org.hamcrest.Matchers.nullValue;+import static org.hamcrest.Matchers.startsWith;+import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertFalse;+import static org.junit.jupiter.api.Assertions.assertNotNull;+import static org.junit.jupiter.api.Assertions.assertNull;+import static org.junit.jupiter.api.Assertions.assertTrue;++@QuarkusTest+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)+class EksClusterSecurityGroupIntegrationTest {++ private static final String JSON = "application/json";+ private static final String CLUSTER = "sg-it-cluster";++ private static String vpcId;+ private static String subnetId;+ private static String clusterSecurityGroupId;++ @Inject+ EksService eksService;++ @Inject+ Ec2Service ec2Service;++ @Test+ @Order(1)+ void setupVpcAndSubnet() {+ vpcId = given()+ .formParam("Action", "CreateVpc")+ .formParam("CidrBlock", "10.0.0.0/16")+ .when().post("/")+ .then().statusCode(200)+ .extract().path("CreateVpcResponse.vpc.vpcId");++ subnetId = given()+ .formParam("Action", "CreateSubnet")+ .formParam("VpcId", vpcId)+ .formParam("CidrBlock", "10.0.1.0/24")+ .when().post("/")+ .then().statusCode(200)+ .extract().path("CreateSubnetResponse.subnet.subnetId");+ }++ @Test+ @Order(2)+ void createClusterProvisionsSecurityGroup() {+ clusterSecurityGroupId = given().contentType(JSON)+ .body("{\"name\":\"" + CLUSTER + "\",\"roleArn\":\"arn:aws:iam::000000000000:role/eks-role\","+ + "\"version\":\"1.29\","+ + "\"resourcesVpcConfig\":{\"subnetIds\":[\"" + subnetId + "\"]}}")+ .when().post("/clusters")+ .then().statusCode(200)+ .body("cluster.name", equalTo(CLUSTER))+ .body("cluster.resourcesVpcConfig.clusterSecurityGroupId", notNullValue())+ .extract().path("cluster.resourcesVpcConfig.clusterSecurityGroupId");++ assertFalse(clusterSecurityGroupId.isBlank());+ assertTrue(clusterSecurityGroupId.startsWith("sg-"));+ }++ @Test+ @Order(3)+ void describeClusterReturnsClusterSecurityGroupId() {+ given().contentType(JSON)+ .when().get("/clusters/" + CLUSTER)+ .then().statusCode(200)+ .body("cluster.resourcesVpcConfig.clusterSecurityGroupId", equalTo(clusterSecurityGroupId));+ }++ @Test+ @Order(4)+ void ec2DescribesClusterSecurityGroupWithTagsAndDescription() {+ given()+ .formParam("Action", "DescribeSecurityGroups")+ .formParam("GroupId.1", clusterSecurityGroupId)+ .when().post("/")+ .then().statusCode(200)+ .body("DescribeSecurityGroupsResponse.securityGroupInfo.item.groupId", equalTo(clusterSecurityGroupId))+ .body("DescribeSecurityGroupsResponse.securityGroupInfo.item.vpcId", equalTo(vpcId))+ .body("DescribeSecurityGroupsResponse.securityGroupInfo.item.groupDescription",+ equalTo("EKS created security group applied to ENI that is attached to EKS Control Plane master nodes, as well as any managed workloads."))+ .body("DescribeSecurityGroupsResponse.securityGroupInfo.item.groupName",+ startsWith("eks-cluster-sg-" + CLUSTER + "-"))+ .body("DescribeSecurityGroupsResponse.securityGroupInfo.item.tagSet.item.find { it.key == 'aws:eks:cluster-name' }.value",+ equalTo(CLUSTER))+ .body("DescribeSecurityGroupsResponse.securityGroupInfo.item.tagSet.item.find { it.key == 'kubernetes.io/cluster/" + CLUSTER + "' }.value",+ equalTo("owned"))+ .body("DescribeSecurityGroupsResponse.securityGroupInfo.item.ipPermissions.item.ipProtocol",+ equalTo("-1"))+ .body("DescribeSecurityGroupsResponse.securityGroupInfo.item.ipPermissions.item.groups.item.groupId",+ equalTo(clusterSecurityGroupId))+ .body("DescribeSecurityGroupsResponse.securityGroupInfo.item.ipPermissionsEgress.toString()",+ containsString(clusterSecurityGroupId));+ }++ @Test+ @Order(5)+ void deleteClusterRemovesSecurityGroup() {+ given().contentType(JSON)+ .when().delete("/clusters/" + CLUSTER)+ .then().statusCode(200);++ given()+ .formParam("Action", "DescribeSecurityGroups")+ .formParam("GroupId.1", clusterSecurityGroupId)+ .when().post("/")+ .then().statusCode(200)+ .body("DescribeSecurityGroupsResponse.securityGroupInfo.item.size()", equalTo(0));++ given()+ .formParam("Action", "DeleteSecurityGroup")+ .formParam("GroupId", clusterSecurityGroupId)+ .when().post("/")+ .then().statusCode(400)+ .body("Response.Errors.Error.Code", equalTo("InvalidGroup.NotFound"));+ }++ @Test+ @Order(6)+ void createClusterWithoutVpcOmitsClusterSecurityGroupId() {+ String noVpcCluster = "no-vpc-it-cluster";+ given().contentType(JSON)+ .body("{\"name\":\"" + noVpcCluster + "\",\"roleArn\":\"arn:aws:iam::000000000000:role/eks-role\"}")+ .when().post("/clusters")+ .then().statusCode(200)+ .body("cluster.name", equalTo(noVpcCluster))+ .body("cluster.resourcesVpcConfig.clusterSecurityGroupId", nullValue());++ given().contentType(JSON)+ .when().get("/clusters/" + noVpcCluster)+ .then().statusCode(200)+ .body("cluster.name", equalTo(noVpcCluster))+ .body("cluster.resourcesVpcConfig.clusterSecurityGroupId", nullValue());++ given().contentType(JSON)+ .when().delete("/clusters/" + noVpcCluster)+ .then().statusCode(200);+ }++ @Test+ @Order(7)+ void backfillClusterSecurityGroupsRunsUnderAccountScopeInQuarkus() {+ String nonDefaultAccount = "111122223333";+ String nonDefaultVpcId = RequestScopes.callAs(nonDefaultAccount, () ->+ ec2Service.createVpc("us-east-1", "10.10.0.0/16", false).getVpcId());++ Cluster nonDefaultCluster = new Cluster();+ nonDefaultCluster.setName("it-nondefault-cluster");+ nonDefaultCluster.setArn("arn:aws:eks:us-east-1:" + nonDefaultAccount + ":cluster/it-nondefault-cluster");+ nonDefaultCluster.setAccountId(nonDefaultAccount);+ nonDefaultCluster.setStatus(ClusterStatus.ACTIVE);+ ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig();+ vpcConfig.setVpcId(nonDefaultVpcId);+ nonDefaultCluster.setResourcesVpcConfig(vpcConfig);++ eksService.putClusterForAccount(nonDefaultAccount, nonDefaultCluster);++ try {+ eksService.backfillClusterSecurityGroups();++ Cluster backfilled = eksService.findAuthenticationCluster(nonDefaultAccount, "it-nondefault-cluster").orElseThrow();+ String backfilledSgId = backfilled.getResourcesVpcConfig().getClusterSecurityGroupId();+ assertNotNull(backfilledSgId);+ assertTrue(backfilledSgId.startsWith("sg-"));++ // Verify the security group exists in EC2 under the non-default account and has ownerId matching it+ SecurityGroup nonDefaultSg = RequestScopes.callAs(nonDefaultAccount, () ->+ ec2Service.describeSecurityGroups("us-east-1", List.of(backfilledSgId), List.of(), Map.of())+ .stream().findFirst().orElse(null));+ assertNotNull(nonDefaultSg, "Security group should exist in EC2 under non-default account");+ assertEquals(nonDefaultAccount, nonDefaultSg.getOwnerId(), "Security group ownerId should match non-default account");++ // Verify it does NOT exist under the default account in EC2+ SecurityGroup defaultAccountSg = RequestScopes.callAs("000000000000", () ->+ ec2Service.describeSecurityGroups("us-east-1", List.of(backfilledSgId), List.of(), Map.of())+ .stream().findFirst().orElse(null));+ assertNull(defaultAccountSg, "Security group should not exist in EC2 under default account");++ // Clean up security group+ RequestScopes.runAs(nonDefaultAccount, () -> ec2Service.deleteSecurityGroup("us-east-1", backfilledSgId));+ } finally {+ RequestScopes.runAs(nonDefaultAccount, () -> ec2Service.deleteVpc("us-east-1", nonDefaultVpcId));+ eksService.deleteClusterForAccount(nonDefaultAccount, "it-nondefault-cluster");+ }+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksServiceTest.javaindex 7932fd8b54..3859201232 100644--- a/src/test/java/io/github/hectorvent/floci/services/eks/EksServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksServiceTest.java@@ -13,6 +13,8 @@ import io.github.hectorvent.floci.services.ec2.Ec2ContainerManager;import io.github.hectorvent.floci.services.ec2.Ec2ImageCatalog;import io.github.hectorvent.floci.services.ec2.Ec2InstanceTypeCatalog;import io.github.hectorvent.floci.services.ec2.Ec2Service;+import io.github.hectorvent.floci.services.ec2.model.SecurityGroup;+import io.github.hectorvent.floci.services.ec2.model.Tag;import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager;import io.github.hectorvent.floci.services.eks.model.ClusterIdentity;import io.github.hectorvent.floci.services.eks.model.ClusterOidcKey;@@ -39,12 +41,14 @@ import java.lang.reflect.Proxy;import java.util.List;import java.util.Map;import java.util.Optional;+import java.util.stream.Collectors;import static org.junit.jupiter.api.Assertions.*;import static org.mockito.ArgumentMatchers.any;import static org.mockito.Mockito.doThrow;import static org.mockito.Mockito.mock;import static org.mockito.Mockito.never;+import static org.mockito.Mockito.spy;import static org.mockito.Mockito.verify;import static org.mockito.Mockito.when;@@ -1067,4 +1071,451 @@ class EksServiceTest {assertEquals(ClusterStatus.FAILED, service.createCluster(request).getStatus());}++ @Test+ void createClusterCreatesClusterSecurityGroupInResolvedVpc() {+ Ec2Service ec2Service = realEc2Service();+ ec2Service.ensureDefaultResources("us-east-1");+ String defaultVpc = Ec2Service.defaultVpcId("us-east-1");+ String subnetId = ec2Service+ .createSubnet("us-east-1", defaultVpc, "172.31.100.0/24", "us-east-1a")+ .getSubnetId();++ StorageFactory storageFactory = fixedStorageFactory(new InMemoryStorage<>());+ RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");+ EksService service = new EksService(storageFactory, testConfig(), regionResolver, null,+ ec2Service, new EksOidcService(storageFactory, new ObjectMapper()), mock(EksAccessEntryService.class));++ ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig();+ vpcConfig.setSubnetIds(List.of(subnetId));++ CreateClusterRequest req = new CreateClusterRequest();+ req.setName("cluster-sg-test");+ req.setRoleArn("arn:aws:iam::000000000000:role/eks-role");+ req.setResourcesVpcConfig(vpcConfig);++ Cluster created = service.createCluster(req);+ assertNotNull(created.getResourcesVpcConfig());+ String sgId = created.getResourcesVpcConfig().getClusterSecurityGroupId();+ assertNotNull(sgId);+ assertTrue(sgId.startsWith("sg-"), "Security group ID should start with sg-");++ Cluster described = service.describeCluster("cluster-sg-test");+ assertEquals(sgId, described.getResourcesVpcConfig().getClusterSecurityGroupId());++ List<SecurityGroup> sgs = ec2Service.describeSecurityGroups("us-east-1", List.of(sgId), List.of(), Map.of());+ assertEquals(1, sgs.size());+ SecurityGroup sg = sgs.getFirst();+ assertEquals(sgId, sg.getGroupId());+ assertTrue(sg.getGroupName().matches("^eks-cluster-sg-cluster-sg-test-[0-9a-f]{8}$"),+ "Group name should match pattern, got: " + sg.getGroupName());+ assertEquals("EKS created security group applied to ENI that is attached to EKS Control Plane master nodes, as well as any managed workloads.",+ sg.getDescription());+ assertEquals(defaultVpc, sg.getVpcId());++ Map<String, String> tagMap = sg.getTags().stream()+ .collect(Collectors.toMap(Tag::getKey, Tag::getValue));+ assertEquals(sg.getGroupName(), tagMap.get("Name"));+ assertEquals("owned", tagMap.get("kubernetes.io/cluster/cluster-sg-test"));+ assertEquals("cluster-sg-test", tagMap.get("aws:eks:cluster-name"));++ assertEquals(1, sg.getIpPermissions().size());+ assertEquals("-1", sg.getIpPermissions().getFirst().getIpProtocol());+ assertEquals(sgId, sg.getIpPermissions().getFirst().getUserIdGroupPairs().getFirst().getGroupId());++ boolean hasSelfEgress = sg.getIpPermissionsEgress().stream()+ .anyMatch(p -> "-1".equals(p.getIpProtocol())+ && p.getUserIdGroupPairs().stream().anyMatch(u -> sgId.equals(u.getGroupId())));+ assertTrue(hasSelfEgress, "Security group should have self-referencing outbound egress rule for EFA");+ }++ @Test+ void createClusterWithoutVpcLeavesClusterSecurityGroupIdNull() {+ StorageFactory storageFactory = fixedStorageFactory(new InMemoryStorage<>());+ RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");+ Ec2Service ec2Service = realEc2Service();+ EksService service = new EksService(storageFactory, testConfig(), regionResolver, null,+ ec2Service, new EksOidcService(storageFactory, new ObjectMapper()), mock(EksAccessEntryService.class));++ CreateClusterRequest req = new CreateClusterRequest();+ req.setName("cluster-no-vpc");+ req.setRoleArn("arn:aws:iam::000000000000:role/eks-role");++ Cluster created = service.createCluster(req);+ assertNotNull(created.getResourcesVpcConfig());+ assertEquals("", created.getResourcesVpcConfig().getVpcId());+ assertNull(created.getResourcesVpcConfig().getClusterSecurityGroupId());+ }++ @Test+ void deleteClusterDeletesClusterSecurityGroup() {+ Ec2Service ec2Service = realEc2Service();+ ec2Service.ensureDefaultResources("us-east-1");+ String defaultVpc = Ec2Service.defaultVpcId("us-east-1");+ String subnetId = ec2Service+ .createSubnet("us-east-1", defaultVpc, "172.31.101.0/24", "us-east-1a")+ .getSubnetId();++ StorageFactory storageFactory = fixedStorageFactory(new InMemoryStorage<>());+ RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");+ EksService service = new EksService(storageFactory, testConfig(), regionResolver, null,+ ec2Service, new EksOidcService(storageFactory, new ObjectMapper()), mock(EksAccessEntryService.class));++ ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig();+ vpcConfig.setSubnetIds(List.of(subnetId));++ CreateClusterRequest req = new CreateClusterRequest();+ req.setName("cluster-to-delete");+ req.setRoleArn("arn:aws:iam::000000000000:role/eks-role");+ req.setResourcesVpcConfig(vpcConfig);++ Cluster created = service.createCluster(req);+ String sgId = created.getResourcesVpcConfig().getClusterSecurityGroupId();+ assertFalse(ec2Service.describeSecurityGroups("us-east-1", List.of(sgId), List.of(), Map.of()).isEmpty());++ service.deleteCluster("cluster-to-delete");+ assertTrue(ec2Service.describeSecurityGroups("us-east-1", List.of(sgId), List.of(), Map.of()).isEmpty());+ }++ @Test+ void deleteClusterSucceedsWhenClusterSecurityGroupAlreadyDeleted() {+ Ec2Service ec2Service = realEc2Service();+ ec2Service.ensureDefaultResources("us-east-1");+ String defaultVpc = Ec2Service.defaultVpcId("us-east-1");+ String subnetId = ec2Service+ .createSubnet("us-east-1", defaultVpc, "172.31.102.0/24", "us-east-1a")+ .getSubnetId();++ StorageFactory storageFactory = fixedStorageFactory(new InMemoryStorage<>());+ RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");+ EksService service = new EksService(storageFactory, testConfig(), regionResolver, null,+ ec2Service, new EksOidcService(storageFactory, new ObjectMapper()), mock(EksAccessEntryService.class));++ ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig();+ vpcConfig.setSubnetIds(List.of(subnetId));++ CreateClusterRequest req = new CreateClusterRequest();+ req.setName("cluster-pre-deleted-sg");+ req.setRoleArn("arn:aws:iam::000000000000:role/eks-role");+ req.setResourcesVpcConfig(vpcConfig);++ Cluster created = service.createCluster(req);+ String sgId = created.getResourcesVpcConfig().getClusterSecurityGroupId();++ // Delete the security group before deleting the cluster+ ec2Service.deleteSecurityGroup("us-east-1", sgId);++ // Deleting the cluster must tolerate InvalidGroup.NotFound+ Cluster deleted = service.deleteCluster("cluster-pre-deleted-sg");+ assertEquals(ClusterStatus.DELETING, deleted.getStatus());+ }++ @Test+ void deleteClusterPropagatesGenuineEc2Failure() {+ Ec2Service mockEc2 = mock(Ec2Service.class);+ doThrow(new AwsException("DependencyViolation", "Rules reference this group", 400))+ .when(mockEc2).deleteSecurityGroup(any(), any());++ StorageBackend<String, Cluster> rawClusters = new InMemoryStorage<>();+ AccountAwareStorageBackend<Cluster> clusterStore =+ new AccountAwareStorageBackend<>(rawClusters, null, "000000000000");++ Cluster cluster = new Cluster();+ cluster.setName("failing-delete-cluster");+ cluster.setArn("arn:aws:eks:us-east-1:000000000000:cluster/failing-delete-cluster");+ ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig();+ vpcConfig.setClusterSecurityGroupId("sg-12345678");+ cluster.setResourcesVpcConfig(vpcConfig);+ clusterStore.putForAccount("000000000000", "failing-delete-cluster", cluster);++ RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");+ StorageFactory storageFactory = fixedStorageFactory(clusterStore);+ EksService service = new EksService(storageFactory, testConfig(), regionResolver, null,+ mockEc2, new EksOidcService(fixedStorageFactory(new InMemoryStorage<String, ClusterOidcKey>()), new ObjectMapper()), mock(EksAccessEntryService.class));++ AwsException ex = assertThrows(AwsException.class, () -> service.deleteCluster("failing-delete-cluster"));+ assertEquals("DependencyViolation", ex.getErrorCode());+ assertEquals(400, ex.getHttpStatus());+ }++ @Test+ void backfillClusterSecurityGroupOnStartup() {+ Ec2Service ec2Service = realEc2Service();+ ec2Service.ensureDefaultResources("us-east-1");+ String defaultVpc = Ec2Service.defaultVpcId("us-east-1");++ StorageBackend<String, Cluster> rawClusters = new InMemoryStorage<>();+ AccountAwareStorageBackend<Cluster> clusterStore =+ new AccountAwareStorageBackend<>(rawClusters, null, "000000000000");++ Cluster legacyCluster = new Cluster();+ legacyCluster.setName("legacy-eks");+ legacyCluster.setArn("arn:aws:eks:us-east-1:000000000000:cluster/legacy-eks");+ legacyCluster.setStatus(ClusterStatus.ACTIVE);+ ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig();+ vpcConfig.setVpcId(defaultVpc);+ vpcConfig.setClusterSecurityGroupId(null);+ legacyCluster.setResourcesVpcConfig(vpcConfig);+ clusterStore.putForAccount("000000000000", "legacy-eks", legacyCluster);++ StorageFactory storageFactory = fixedStorageFactory(clusterStore);+ RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");+ EksService service = new EksService(storageFactory, testConfig(true), regionResolver, null,+ ec2Service, new EksOidcService(fixedStorageFactory(new InMemoryStorage<String, ClusterOidcKey>()), new ObjectMapper()), mock(EksAccessEntryService.class));++ service.init();++ Cluster described = service.describeCluster("legacy-eks");+ String sgId = described.getResourcesVpcConfig().getClusterSecurityGroupId();+ assertNotNull(sgId);+ assertTrue(sgId.startsWith("sg-"));++ List<SecurityGroup> sgs = ec2Service.describeSecurityGroups("us-east-1", List.of(sgId), List.of(), Map.of());+ assertEquals(1, sgs.size());+ SecurityGroup sg = sgs.getFirst();+ assertEquals(defaultVpc, sg.getVpcId());+ assertTrue(sg.getGroupName().startsWith("eks-cluster-sg-legacy-eks-"));+ }++ @Test+ void backfillClusterSecurityGroupLeavesExistingSecurityGroupUnchanged() {+ Ec2Service ec2Service = realEc2Service();+ ec2Service.ensureDefaultResources("us-east-1");+ String defaultVpc = Ec2Service.defaultVpcId("us-east-1");++ StorageBackend<String, Cluster> rawClusters = new InMemoryStorage<>();+ AccountAwareStorageBackend<Cluster> clusterStore =+ new AccountAwareStorageBackend<>(rawClusters, null, "000000000000");++ Cluster existingCluster = new Cluster();+ existingCluster.setName("existing-eks");+ existingCluster.setArn("arn:aws:eks:us-east-1:000000000000:cluster/existing-eks");+ existingCluster.setStatus(ClusterStatus.ACTIVE);+ ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig();+ vpcConfig.setVpcId(defaultVpc);+ vpcConfig.setClusterSecurityGroupId("sg-already-present");+ existingCluster.setResourcesVpcConfig(vpcConfig);+ clusterStore.putForAccount("000000000000", "existing-eks", existingCluster);++ StorageFactory storageFactory = fixedStorageFactory(clusterStore);+ RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");+ EksService service = new EksService(storageFactory, testConfig(true), regionResolver, null,+ ec2Service, new EksOidcService(fixedStorageFactory(new InMemoryStorage<String, ClusterOidcKey>()), new ObjectMapper()), mock(EksAccessEntryService.class));++ service.init();++ Cluster described = service.describeCluster("existing-eks");+ assertEquals("sg-already-present", described.getResourcesVpcConfig().getClusterSecurityGroupId());+ }++ @Test+ void restartRoundTripPersistsClusterSecurityGroupId() {+ Ec2Service ec2Service = realEc2Service();+ ec2Service.ensureDefaultResources("us-east-1");+ String defaultVpc = Ec2Service.defaultVpcId("us-east-1");+ String subnetId = ec2Service+ .createSubnet("us-east-1", defaultVpc, "172.31.103.0/24", "us-east-1a")+ .getSubnetId();++ StorageBackend<String, Cluster> rawClusters = new InMemoryStorage<>();+ AccountAwareStorageBackend<Cluster> clusterStore =+ new AccountAwareStorageBackend<>(rawClusters, null, "000000000000");+ StorageFactory storageFactory = fixedStorageFactory(clusterStore);+ RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");+ StorageFactory oidcStorageFactory = fixedStorageFactory(new InMemoryStorage<String, ClusterOidcKey>());++ EksService firstRun = new EksService(storageFactory, testConfig(), regionResolver, null,+ ec2Service, new EksOidcService(oidcStorageFactory, new ObjectMapper()), mock(EksAccessEntryService.class));++ ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig();+ vpcConfig.setSubnetIds(List.of(subnetId));++ CreateClusterRequest req = new CreateClusterRequest();+ req.setName("round-trip-cluster");+ req.setRoleArn("arn:aws:iam::000000000000:role/eks-role");+ req.setResourcesVpcConfig(vpcConfig);++ Cluster created = firstRun.createCluster(req);+ String sgId = created.getResourcesVpcConfig().getClusterSecurityGroupId();+ assertNotNull(sgId);+ assertTrue(sgId.startsWith("sg-"));++ // Simulate restart by creating a new EksService instance backed by the same storage and ec2Service+ EksService restarted = new EksService(storageFactory, testConfig(true), regionResolver, null,+ ec2Service, new EksOidcService(oidcStorageFactory, new ObjectMapper()), mock(EksAccessEntryService.class));+ restarted.init();++ Cluster described = restarted.describeCluster("round-trip-cluster");+ assertEquals(sgId, described.getResourcesVpcConfig().getClusterSecurityGroupId());++ // And verify it still resolves in EC2+ List<SecurityGroup> sgs = ec2Service.describeSecurityGroups("us-east-1", List.of(sgId), List.of(), Map.of());+ assertEquals(1, sgs.size());+ assertEquals(sgId, sgs.getFirst().getGroupId());+ }++ @Test+ void createClusterWithNonExistentVpcThrowsInvalidParameterException() {+ StorageFactory storageFactory = fixedStorageFactory(new InMemoryStorage<>());+ RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");+ Ec2Service ec2Service = realEc2Service();+ EksService service = new EksService(storageFactory, testConfig(), regionResolver, null,+ ec2Service, new EksOidcService(storageFactory, new ObjectMapper()), mock(EksAccessEntryService.class));++ ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig();+ vpcConfig.setVpcId("vpc-nonexistent");++ CreateClusterRequest req = new CreateClusterRequest();+ req.setName("cluster-invalid-vpc");+ req.setRoleArn("arn:aws:iam::000000000000:role/eks-role");+ req.setResourcesVpcConfig(vpcConfig);++ AwsException ex = assertThrows(AwsException.class, () -> service.createCluster(req));+ assertEquals("InvalidParameterException", ex.getErrorCode());+ assertEquals(400, ex.getHttpStatus());+ assertTrue(ex.getMessage().contains("vpc-nonexistent"));+ }++ @Test+ void createClusterCleansUpSecurityGroupWhenClusterCreationFails() {+ Ec2Service ec2Service = realEc2Service();+ ec2Service.ensureDefaultResources("us-east-1");+ String defaultVpc = Ec2Service.defaultVpcId("us-east-1");+ String subnetId = ec2Service+ .createSubnet("us-east-1", defaultVpc, "172.31.104.0/24", "us-east-1a")+ .getSubnetId();++ StorageBackend<String, Cluster> failingStorage = new InMemoryStorage<>() {+ @Override+ public void put(String key, Cluster value) {+ throw new RuntimeException("Simulated storage failure");+ }+ };+ StorageFactory storageFactory = fixedStorageFactory(failingStorage);+ RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");+ EksService service = new EksService(storageFactory, testConfig(), regionResolver, null,+ ec2Service, new EksOidcService(fixedStorageFactory(new InMemoryStorage<String, ClusterOidcKey>()), new ObjectMapper()), mock(EksAccessEntryService.class));++ ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig();+ vpcConfig.setSubnetIds(List.of(subnetId));++ CreateClusterRequest req = new CreateClusterRequest();+ req.setName("cluster-leak-test");+ req.setRoleArn("arn:aws:iam::000000000000:role/eks-role");+ req.setResourcesVpcConfig(vpcConfig);++ assertThrows(RuntimeException.class, () -> service.createCluster(req));++ List<SecurityGroup> allSgs = ec2Service.describeSecurityGroups("us-east-1", List.of(), List.of(), Map.of());+ boolean leaked = allSgs.stream().anyMatch(sg -> sg.getGroupName().startsWith("eks-cluster-sg-cluster-leak-test-"));+ assertFalse(leaked, "Cluster security group should have been deleted when cluster creation failed");+ }++ @Test+ void backfillClusterSecurityGroupsSkipsMissingVpcWithoutFailingStartup() {+ Ec2Service ec2Service = realEc2Service();+ ec2Service.ensureDefaultResources("us-east-1");+ String defaultVpc = Ec2Service.defaultVpcId("us-east-1");++ StorageBackend<String, Cluster> rawClusters = new InMemoryStorage<>();+ AccountAwareStorageBackend<Cluster> clusterStore =+ new AccountAwareStorageBackend<>(rawClusters, null, "000000000000");++ Cluster brokenCluster = new Cluster();+ brokenCluster.setName("broken-eks");+ brokenCluster.setArn("arn:aws:eks:us-east-1:000000000000:cluster/broken-eks");+ brokenCluster.setStatus(ClusterStatus.ACTIVE);+ ResourcesVpcConfig brokenVpc = new ResourcesVpcConfig();+ brokenVpc.setVpcId("vpc-missing");+ brokenCluster.setResourcesVpcConfig(brokenVpc);+ clusterStore.putForAccount("000000000000", "broken-eks", brokenCluster);++ Cluster validCluster = new Cluster();+ validCluster.setName("valid-eks");+ validCluster.setArn("arn:aws:eks:us-east-1:000000000000:cluster/valid-eks");+ validCluster.setStatus(ClusterStatus.ACTIVE);+ ResourcesVpcConfig validVpc = new ResourcesVpcConfig();+ validVpc.setVpcId(defaultVpc);+ validCluster.setResourcesVpcConfig(validVpc);+ clusterStore.putForAccount("000000000000", "valid-eks", validCluster);++ StorageFactory storageFactory = fixedStorageFactory(clusterStore);+ RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");+ EksService service = new EksService(storageFactory, testConfig(true), regionResolver, null,+ ec2Service, new EksOidcService(fixedStorageFactory(new InMemoryStorage<String, ClusterOidcKey>()), new ObjectMapper()), mock(EksAccessEntryService.class));++ assertDoesNotThrow(service::init);++ Cluster describedBroken = service.describeCluster("broken-eks");+ assertNull(describedBroken.getResourcesVpcConfig().getClusterSecurityGroupId());++ Cluster describedValid = service.describeCluster("valid-eks");+ String validSgId = describedValid.getResourcesVpcConfig().getClusterSecurityGroupId();+ assertNotNull(validSgId);+ assertTrue(validSgId.startsWith("sg-"));+ }++ @Test+ void backfillClusterSecurityGroupsInNonDefaultAccount() {+ String nonDefaultAccount = "123456789012";+ Ec2Service ec2Service = realEc2Service();+ ec2Service.ensureDefaultResources("us-east-1");+ String defaultVpc = Ec2Service.defaultVpcId("us-east-1");++ StorageBackend<String, Cluster> rawClusters = new InMemoryStorage<>();+ AccountAwareStorageBackend<Cluster> clusterStore =+ new AccountAwareStorageBackend<>(rawClusters, null, "000000000000");++ Cluster legacyCluster = new Cluster();+ legacyCluster.setName("non-default-eks");+ legacyCluster.setArn("arn:aws:eks:us-east-1:" + nonDefaultAccount + ":cluster/non-default-eks");+ legacyCluster.setAccountId(nonDefaultAccount);+ legacyCluster.setStatus(ClusterStatus.ACTIVE);+ ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig();+ vpcConfig.setVpcId(defaultVpc);+ legacyCluster.setResourcesVpcConfig(vpcConfig);+ clusterStore.putForAccount(nonDefaultAccount, "non-default-eks", legacyCluster);++ StorageFactory storageFactory = fixedStorageFactory(clusterStore);+ RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");+ EksService service = new EksService(storageFactory, testConfig(true), regionResolver, null,+ ec2Service, new EksOidcService(fixedStorageFactory(new InMemoryStorage<String, ClusterOidcKey>()), new ObjectMapper()), mock(EksAccessEntryService.class));++ assertDoesNotThrow(service::init);++ Cluster described = clusterStore.getForAccount(nonDefaultAccount, "non-default-eks")+ .orElseThrow();+ String sgId = described.getResourcesVpcConfig().getClusterSecurityGroupId();+ assertNotNull(sgId);+ assertTrue(sgId.startsWith("sg-"));+ assertEquals(nonDefaultAccount, described.getAccountId());+ }++ @Test+ void createClusterSecurityGroupCleansUpWhenTaggingFails() {+ Ec2Service spyEc2 = spy(realEc2Service());+ spyEc2.ensureDefaultResources("us-east-1");+ String defaultVpc = Ec2Service.defaultVpcId("us-east-1");++ doThrow(new RuntimeException("Simulated tagging failure"))+ .when(spyEc2).createTags(any(), any(), any());++ StorageFactory storageFactory = fixedStorageFactory(new InMemoryStorage<>());+ RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");+ EksService service = new EksService(storageFactory, testConfig(), regionResolver, null,+ spyEc2, new EksOidcService(storageFactory, new ObjectMapper()), mock(EksAccessEntryService.class));++ ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig();+ vpcConfig.setVpcId(defaultVpc);+ CreateClusterRequest req = new CreateClusterRequest();+ req.setName("cluster-tag-fail");+ req.setRoleArn("arn:aws:iam::000000000000:role/eks-role");+ req.setResourcesVpcConfig(vpcConfig);++ assertThrows(RuntimeException.class, () -> service.createCluster(req));++ List<SecurityGroup> allSgs = spyEc2.describeSecurityGroups("us-east-1", List.of(), List.of(), Map.of());+ boolean leaked = allSgs.stream().anyMatch(sg -> sg.getGroupName().startsWith("eks-cluster-sg-cluster-tag-fail-"));+ assertFalse(leaked, "Cluster security group should have been deleted when tagging failed");+ }}