mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(eks): authenticate EKS Auto Mode nodes through their EC2 access entry (#9399)
Worker authentication accepts the node role's EC2 entry and names the node by its session name, the instance ID. The entry is also made after an authentication-mode change while compute is on, and never for a cluster being deleted. CreateCluster makes it together with the cluster record, and DeleteCluster removes it before the record, both under the same lock, so a failed or concurrent create or delete leaves none behind. A client-token retry makes the entry only for a request that changes Auto Mode or the authentication mode, and a failed create keeps its own error when the entry cleanup fails too.
This commit is contained in:
1 parent
8216c2b382
commit
b85b0a8966
7 files changed
+257
-19
No files matched your search
@@ -296,7 +296,8 @@ public class EksAccessEntryService {
|
||||
Optional<StoredEntry> stored = entries instanceof AccountAwareStorageBackend<StoredEntry> aware
|
||||
? aware.getForAccount(accountId, key) : entries.get(key);
|
||||
return stored.filter(value -> roleId != null && roleId.equals(value.principalId()))
|
||||
.map(StoredEntry::entry).filter(entry -> "EC2_LINUX".equals(entry.type()));
|
||||
.map(StoredEntry::entry)
|
||||
.filter(entry -> "EC2_LINUX".equals(entry.type()) || AUTO_MODE_NODE_TYPE.equals(entry.type()));
|
||||
}
|
||||
|
||||
public synchronized AccessEntry describe(Cluster cluster, String principal) {
|
||||
|
||||
@@ -1023,6 +1023,7 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
cluster.setPlatformVersion("eks.1");
|
||||
cluster.setCertificateAuthority(new CertificateAuthority(""));
|
||||
|
||||
boolean madeNodeEntry = false;
|
||||
try {
|
||||
String issuer = oidcService.newIssuerUrl(region);
|
||||
cluster.setIdentity(new ClusterIdentity(new OidcIdentity(issuer)));
|
||||
@@ -1044,9 +1045,24 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
}
|
||||
}
|
||||
|
||||
ensureAutoModeNodeEntry(cluster);
|
||||
storage.put(clusterKey(region, name), cluster);
|
||||
// The entry and the cluster appear together, under the lock a delete removes entries under, so no
|
||||
// reader sees an Auto Mode cluster without its node's entry.
|
||||
synchronized (clustersBeingDeleted) {
|
||||
madeNodeEntry = true;
|
||||
makeAutoModeNodeEntry(cluster);
|
||||
storage.put(clusterKey(region, name), cluster);
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
if (madeNodeEntry) {
|
||||
// A create that failed once it began making the node's entry leaves none behind; the cluster is new.
|
||||
// A cleanup that fails too is logged, so the security group still goes and the create's error stays.
|
||||
try {
|
||||
accessEntries.deleteClusterEntries(cluster);
|
||||
} catch (RuntimeException cleanupEx) {
|
||||
LOG.warnv("Failed to clean up the access entries of cluster {0} after its creation failed: {1}",
|
||||
name, cleanupEx.getMessage());
|
||||
}
|
||||
}
|
||||
if (clusterSg != null) {
|
||||
try {
|
||||
ec2Service.deleteSecurityGroup(region, clusterSg.getGroupId());
|
||||
@@ -1199,6 +1215,12 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
Optional<EksAddonService.StoredUpdate> previous = addons.findConfigUpdateByToken(cluster, token);
|
||||
if (previous.isPresent()) {
|
||||
if (updateParametersMatch(previous.get().request(), request)) {
|
||||
// A retry of a request that makes the node's entry finishes it, should the first attempt not have;
|
||||
// a retry of any other update leaves the entries as they are.
|
||||
if (request.changesAutoMode() || (request.accessConfig() != null
|
||||
&& request.accessConfig().authenticationMode() != null)) {
|
||||
ensureAutoModeNodeEntry(cluster);
|
||||
}
|
||||
return List.of(previous.get().update());
|
||||
}
|
||||
throw new AwsException("InvalidParameterException", EksAddonService.TOKEN_REUSED, 400);
|
||||
@@ -1317,7 +1339,8 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
&& !config.services().eks().mock() && clusterManager != null && cluster.getContainerId() != null) {
|
||||
replaceNodeForServiceLb(requireClusterInRequestRegion(name));
|
||||
}
|
||||
if (nextAutoMode != null && nextAutoMode.computeOn()) {
|
||||
if ((nextAutoMode != null && nextAutoMode.computeOn()) || requestedMode != null) {
|
||||
// Turning compute on, or moving off CONFIG_MAP while it is on, gives the node role its entry.
|
||||
ensureAutoModeNodeEntry(requireClusterInRequestRegion(name));
|
||||
}
|
||||
LOG.infov("Updated the configuration of EKS cluster {0}: {1}", cluster.getName(),
|
||||
@@ -1374,6 +1397,20 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
* cluster's EKS Auto Mode compute capability is on (measured on AWS for CreateCluster).
|
||||
*/
|
||||
private void ensureAutoModeNodeEntry(Cluster cluster) {
|
||||
ComputeConfig compute = cluster.getComputeConfig();
|
||||
if (accessEntries == null || compute == null || !Boolean.TRUE.equals(compute.enabled())) {
|
||||
return;
|
||||
}
|
||||
// A cluster being deleted, or gone, gets no entry: its delete removes entries under this lock.
|
||||
synchronized (clustersBeingDeleted) {
|
||||
if (!isClusterDeleting(cluster.getAccountId(), cluster)
|
||||
&& findClusterForAccount(cluster.getAccountId(), clusterKey(cluster)).isPresent()) {
|
||||
makeAutoModeNodeEntry(cluster);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void makeAutoModeNodeEntry(Cluster cluster) {
|
||||
ComputeConfig compute = cluster.getComputeConfig();
|
||||
if (accessEntries != null && compute != null && Boolean.TRUE.equals(compute.enabled())) {
|
||||
accessEntries.ensureAutoModeNodeEntry(cluster, compute.nodeRoleArn());
|
||||
@@ -1756,7 +1793,6 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
deleteClusterSecurityGroup(cluster);
|
||||
deleteNodeGroupAutoScalingGroups(cluster.getAccountId(), cluster);
|
||||
deleteAutoModeLoadBalancers(cluster);
|
||||
accessEntries.deleteClusterEntries(cluster);
|
||||
if (podIdentityAssociations != null) {
|
||||
podIdentityAssociations.deleteClusterAssociations(cluster);
|
||||
}
|
||||
@@ -1767,6 +1803,9 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
deletedFargateArns.keys().stream().filter(key -> key.startsWith(deletedPrefix))
|
||||
.toList().forEach(deletedFargateArns::delete);
|
||||
synchronized (clustersBeingDeleted) {
|
||||
// Under the lock ensureAutoModeNodeEntry takes, so an entry it makes meanwhile goes too, and before
|
||||
// the record, so a delete whose entry cleanup fails can be retried.
|
||||
accessEntries.deleteClusterEntries(cluster);
|
||||
storage.delete(clusterKey(cluster));
|
||||
clearAppliedClusterUserData(cluster, name);
|
||||
oidcService.deleteKey(clusterKey(cluster));
|
||||
|
||||
@@ -5,6 +5,7 @@ import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.services.ec2.Ec2Service;
|
||||
import io.floci.aws.services.ec2.model.Instance;
|
||||
import io.floci.aws.services.eks.model.AccessEntry;
|
||||
import io.floci.aws.services.eks.model.Cluster;
|
||||
import io.floci.aws.services.iam.IamService;
|
||||
import io.floci.aws.services.iam.IamService.EksSessionIdentity;
|
||||
@@ -70,22 +71,30 @@ class EksWorkerAuthentication {
|
||||
if (role.isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
Optional<AccessEntry> entry;
|
||||
try {
|
||||
if (entries.workerEntry(cluster.get(), account, identity.roleArn(), identity.roleId()).isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
entry = entries.workerEntry(cluster.get(), account, identity.roleArn(), identity.roleId());
|
||||
} catch (AwsException rejected) {
|
||||
return Optional.empty();
|
||||
}
|
||||
if (entry.isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
Optional<Instance> instance = ec2.findInstanceForAccount(account, region, identity.instanceId())
|
||||
.filter(value -> value.getState() != null && "running".equals(value.getState().getName()))
|
||||
.filter(value -> region.equals(value.getRegion()))
|
||||
.filter(value -> profileMatches(value, account, roleName));
|
||||
if (instance.isEmpty() || instance.get().getPrivateDnsName() == null
|
||||
|| instance.get().getPrivateDnsName().isBlank()) {
|
||||
if (instance.isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(Map.of("username", "system:node:" + instance.get().getPrivateDnsName(),
|
||||
// An EC2_LINUX entry names the node by its private DNS name, an EKS Auto Mode node's EC2 entry by
|
||||
// {{SessionName}}, which an instance profile's session is named for: the instance ID.
|
||||
String node = EksAccessEntryService.AUTO_MODE_NODE_TYPE.equals(entry.get().type())
|
||||
? identity.instanceId() : instance.get().getPrivateDnsName();
|
||||
if (node == null || node.isBlank()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(Map.of("username", "system:node:" + node,
|
||||
"uid", account + ":" + identity.roleId() + ":" + identity.instanceId(),
|
||||
"groups", List.of("system:bootstrappers", "system:nodes")));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user