fix(eks): populate clusterSecurityGroupId on cluster responses (#3924)

* fix(eks): populate clusterSecurityGroupId on cluster responses

Real AWS EKS automatically provisions a cluster security group and
returns its identifier in resourcesVpcConfig.clusterSecurityGroupId
on CreateCluster and DescribeCluster responses.

- Create a security group in Ec2Service with the pattern
  eks-cluster-sg-<cluster-name>-<suffix> using 8 hex characters.
- Set standard AWS description and tags (Name, kubernetes.io/cluster/<cluster-name>,
  and aws:eks:cluster-name).
- Set clusterSecurityGroupId to empty string when cluster has no VPC.
- Delete the cluster security group on DeleteCluster, tolerating
  InvalidGroup.NotFound for idempotency.
- Backfill cluster security groups for pre-existing clusters on startup.

* fix(eks): address cluster security group review feedback

* fix(eks): add self-referencing egress rule and cleanup on SG setup failure
This commit is contained in:
Simone Primarosa authored and GitHub committed 2026-09-20 08:52:40 -04:00
1 parent 9379fa1179
commit 51467dbc23
5 files changed
+873 -19

No files matched your search

@@ -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;
@ApplicationScoped
public 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 {
@PostConstruct
public 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);
}
@PreDestroy
public 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);
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);
}
} catch (Exception e) {
LOG.errorv("Failed to start k3s container for cluster {0}: {1}", name, e.getMessage());
cluster.setStatus(ClusterStatus.FAILED);
}
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());
}
}
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);
@@ -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")