mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 14:10:49 -05:00
fix(eks): preserve scoped authorization for deletion waiters (#8580)
Deleting an EKS add-on or Fargate profile now keeps only its ARN for IAM authorization, keyed by the cluster's incarnation, so a deletion waiter with a resource-scoped describe grant receives ResourceNotFoundException instead of AccessDeniedException, while an unrelated grant or an explicit deny still gets 403. Describe and list still report the resource as gone, recreation clears the kept ARN and cluster deletion purges it. Resource tags are not kept. Closes #8494
This commit is contained in:
1 parent
0d6aa45d1f
commit
6eb66cd6bf
6 files changed
+147
-6
No files matched your search
@@ -25,6 +25,15 @@ boolean or the text `true` or `false`, such as `"endpointPublicAccess": 1`, is r
|
||||
no value. A structure, list or map of the wrong kind is refused the same way. EKS's own wording
|
||||
for this error has not been measured; Floci answers as the REST JSON services it was measured on do.
|
||||
|
||||
## Deletion waiters and IAM
|
||||
|
||||
After deleting an add-on or Fargate profile, a principal with resource-scoped describe
|
||||
permission receives `ResourceNotFoundException` from its deletion waiter. Floci retains
|
||||
only the deleted ARN for authorization, so unrelated resource grants and explicit denies
|
||||
still return `AccessDeniedException`. This metadata follows the configured EKS storage mode;
|
||||
it is cleared on resource recreation or cluster deletion and is not returned by describe
|
||||
or list operations. Resource tags are not retained for post-deletion policy conditions.
|
||||
|
||||
## Supported Operations
|
||||
|
||||
| Operation | Description |
|
||||
|
||||
@@ -49,6 +49,7 @@ public class EksAddonService {
|
||||
|
||||
private final StorageBackend<String, StoredAddon> storage;
|
||||
private final StorageBackend<String, StoredUpdate> updatesStorage;
|
||||
private final StorageBackend<String, String> deletedArns;
|
||||
private final EksAddonCatalog catalog;
|
||||
private final IamService iam;
|
||||
private final EksPodIdentityAssociationService podIdentityAssociations;
|
||||
@@ -60,16 +61,20 @@ public class EksAddonService {
|
||||
new TypeReference<Map<String, StoredAddon>>() {}),
|
||||
storageFactory.create("eks", "eks-updates.json",
|
||||
new TypeReference<Map<String, StoredUpdate>>() {}),
|
||||
storageFactory.create("eks", "eks-addon-deleted-arns.json",
|
||||
new TypeReference<Map<String, String>>() {}),
|
||||
catalog, iam, podIdentityAssociations);
|
||||
}
|
||||
|
||||
EksAddonService(StorageBackend<String, StoredAddon> storage,
|
||||
StorageBackend<String, StoredUpdate> updatesStorage,
|
||||
StorageBackend<String, String> deletedArns,
|
||||
EksAddonCatalog catalog,
|
||||
IamService iam,
|
||||
EksPodIdentityAssociationService podIdentityAssociations) {
|
||||
this.storage = storage;
|
||||
this.updatesStorage = updatesStorage;
|
||||
this.deletedArns = deletedArns;
|
||||
this.catalog = catalog;
|
||||
this.iam = iam;
|
||||
this.podIdentityAssociations = podIdentityAssociations;
|
||||
@@ -159,6 +164,7 @@ public class EksAddonService {
|
||||
);
|
||||
|
||||
storage.put(storageKey, new StoredAddon(addon, request.clientRequestToken()));
|
||||
deletedArns.delete(storageKey);
|
||||
LOG.infov("Created EKS addon {0} on cluster {1} with version {2}", addonName, cluster.getName(), version);
|
||||
return addon;
|
||||
}
|
||||
@@ -304,6 +310,7 @@ public class EksAddonService {
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"No addon: " + addonName + " found for cluster: " + cluster.getName(), 404));
|
||||
|
||||
deletedArns.put(storageKey, stored.addon().addonArn());
|
||||
storage.delete(storageKey);
|
||||
if (preserve) {
|
||||
LOG.infov("Deleted EKS addon {0} on cluster {1} (preserve=true)", addonName, cluster.getName());
|
||||
@@ -335,11 +342,23 @@ public class EksAddonService {
|
||||
);
|
||||
}
|
||||
|
||||
/** Retain only the deleted ARN so scoped IAM policies still authorize deletion waiters. */
|
||||
public synchronized Optional<String> authorizationArn(Cluster cluster, String addonName) {
|
||||
requireActiveCluster(cluster);
|
||||
if (addonName == null || addonName.isBlank()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
String key = prefix(cluster) + addonName.trim().toLowerCase(Locale.ROOT);
|
||||
return storage.get(key).map(stored -> stored.addon().addonArn())
|
||||
.or(() -> deletedArns.get(key));
|
||||
}
|
||||
|
||||
public synchronized void deleteClusterAddons(Cluster cluster) {
|
||||
if (cluster != null) {
|
||||
String p = prefix(cluster);
|
||||
storage.keys().stream().filter(k -> k.startsWith(p)).toList().forEach(storage::delete);
|
||||
updatesStorage.keys().stream().filter(k -> k.startsWith(p)).toList().forEach(updatesStorage::delete);
|
||||
deletedArns.keys().stream().filter(k -> k.startsWith(p)).toList().forEach(deletedArns::delete);
|
||||
LOG.infov("Purged all addons and updates for cluster {0}", cluster.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,6 +106,7 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
private final StorageBackend<String, Cluster> storage;
|
||||
private final StorageBackend<String, Nodegroup> nodeGroupStorage;
|
||||
private final StorageBackend<String, FargateProfile> fargateProfileStorage;
|
||||
private final StorageBackend<String, String> deletedFargateArns;
|
||||
private final EmulatorConfig config;
|
||||
private final RegionResolver regionResolver;
|
||||
private final EksClusterManager clusterManager;
|
||||
@@ -140,6 +141,8 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
this.fargateProfileStorage = storageFactory.create("eks", "eks-fargate-profiles.json",
|
||||
new TypeReference<Map<String, FargateProfile>>() {
|
||||
});
|
||||
this.deletedFargateArns = storageFactory.create("eks", "eks-deleted-fargate-arns.json",
|
||||
new TypeReference<Map<String, String>>() {});
|
||||
this.config = config;
|
||||
this.regionResolver = regionResolver;
|
||||
this.clusterManager = clusterManager;
|
||||
@@ -956,7 +959,7 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
* cluster {@code clusterName} when {@code collection} is null, or else the node group, Fargate profile,
|
||||
* add-on, access entry or Pod Identity association {@code name} in that cluster, by the route's
|
||||
* collection segment, or for the update {@code name} the add-on it was made to. Empty when there is none,
|
||||
* or the handler would refuse the lookup.
|
||||
* or the handler would refuse the lookup. Deleted add-ons and Fargate profiles retain their ARN for scoped IAM deletion waiters.
|
||||
*/
|
||||
public Optional<String> findResourceArn(String clusterName, String collection, String name) {
|
||||
Optional<Cluster> cluster = storage.get(requestClusterKey(clusterName));
|
||||
@@ -966,8 +969,8 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
try {
|
||||
return Optional.ofNullable(switch (collection) {
|
||||
case "node-groups" -> describeNodeGroup(clusterName, name).getNodegroupArn();
|
||||
case "fargate-profiles" -> describeFargateProfile(clusterName, name).getFargateProfileArn();
|
||||
case "addons" -> addons.describe(cluster.get(), name).addonArn();
|
||||
case "fargate-profiles" -> fargateAuthorizationArn(cluster.get(), name).orElse(null);
|
||||
case "addons" -> addons.authorizationArn(cluster.get(), name).orElse(null);
|
||||
case "access-entries" -> accessEntries.describe(cluster.get(), name).accessEntryArn();
|
||||
case "pod-identity-associations" -> podIdentityAssociations.describe(cluster.get(), name).associationArn();
|
||||
case "updates" -> {
|
||||
@@ -1149,6 +1152,9 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
if (addons != null) {
|
||||
addons.deleteClusterAddons(cluster);
|
||||
}
|
||||
String deletedPrefix = deletedFargatePrefix(cluster);
|
||||
deletedFargateArns.keys().stream().filter(key -> key.startsWith(deletedPrefix))
|
||||
.toList().forEach(deletedFargateArns::delete);
|
||||
synchronized (clustersBeingDeleted) {
|
||||
storage.delete(clusterKey(cluster));
|
||||
clearAppliedClusterUserData(cluster, name);
|
||||
@@ -1455,7 +1461,7 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
}
|
||||
}
|
||||
|
||||
public FargateProfile createFargateProfile(String clusterName, CreateFargateProfileRequest request) {
|
||||
public synchronized FargateProfile createFargateProfile(String clusterName, CreateFargateProfileRequest request) {
|
||||
Cluster cluster = describeCluster(clusterName);
|
||||
|
||||
String fargateProfileName = request.getFargateProfileName();
|
||||
@@ -1492,6 +1498,7 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
profile.setTags(request.getTags() != null ? new HashMap<>(request.getTags()) : new HashMap<>());
|
||||
|
||||
fargateProfileStorage.put(storageKey, profile);
|
||||
deletedFargateArns.delete(deletedFargatePrefix(cluster) + fargateProfileName);
|
||||
return profile;
|
||||
}
|
||||
|
||||
@@ -1510,8 +1517,20 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public FargateProfile deleteFargateProfile(String clusterName, String fargateProfileName) {
|
||||
private static String deletedFargatePrefix(Cluster cluster) {
|
||||
return cluster.getArn() + "/" + Objects.toString(cluster.getCreatedAt()) + "/";
|
||||
}
|
||||
|
||||
private synchronized Optional<String> fargateAuthorizationArn(Cluster cluster, String name) {
|
||||
return fargateProfileStorage.get(fargateProfileKey(clusterKey(cluster), name))
|
||||
.map(FargateProfile::getFargateProfileArn)
|
||||
.or(() -> deletedFargateArns.get(deletedFargatePrefix(cluster) + name));
|
||||
}
|
||||
|
||||
public synchronized FargateProfile deleteFargateProfile(String clusterName, String fargateProfileName) {
|
||||
FargateProfile profile = describeFargateProfile(clusterName, fargateProfileName);
|
||||
deletedFargateArns.put(deletedFargatePrefix(describeCluster(clusterName)) + fargateProfileName,
|
||||
profile.getFargateProfileArn());
|
||||
profile.setStatus(FargateProfileStatus.DELETING);
|
||||
fargateProfileStorage.delete(fargateProfileKey(requestClusterKey(clusterName), fargateProfileName));
|
||||
return profile;
|
||||
|
||||
@@ -315,6 +315,28 @@ class EksAddonServiceTest {
|
||||
assertTrue(fixture.service.list(fixture.cluster, null, null).addons().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletedAuthorizationArnIsHiddenFromReadsReplacedOnRecreationAndPurgedWithCluster() {
|
||||
Fixture fixture = fixture();
|
||||
fixture.cluster.setVersion("1.29");
|
||||
CreateAddonRequest request = new CreateAddonRequest("vpc-cni", null, null, null, null, null, null, null);
|
||||
String original = fixture.service.create(fixture.cluster, request).addonArn();
|
||||
fixture.service.delete(fixture.cluster, "vpc-cni", false);
|
||||
assertEquals(Optional.of(original), fixture.service.authorizationArn(fixture.cluster, "vpc-cni"));
|
||||
assertTrue(fixture.service.list(fixture.cluster, null, null).addons().isEmpty());
|
||||
assertThrows(AwsException.class, () -> fixture.service.describe(fixture.cluster, "vpc-cni"));
|
||||
String replacement = fixture.service.create(fixture.cluster, request).addonArn();
|
||||
assertNotEquals(original, replacement);
|
||||
assertEquals(Optional.of(replacement), fixture.service.authorizationArn(fixture.cluster, "vpc-cni"));
|
||||
fixture.service.delete(fixture.cluster, "vpc-cni", false);
|
||||
Instant previousCreation = fixture.cluster.getCreatedAt();
|
||||
fixture.cluster.setCreatedAt(previousCreation.plusSeconds(10));
|
||||
assertTrue(fixture.service.authorizationArn(fixture.cluster, "vpc-cni").isEmpty());
|
||||
fixture.cluster.setCreatedAt(previousCreation);
|
||||
fixture.service.deleteClusterAddons(fixture.cluster);
|
||||
assertTrue(fixture.service.authorizationArn(fixture.cluster, "vpc-cni").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void inactiveClusterRejectsOperations() {
|
||||
Fixture fixture = fixture();
|
||||
@@ -732,7 +754,7 @@ class EksAddonServiceTest {
|
||||
InMemoryStorage<String, EksPodIdentityAssociationService.StoredAssociation> assocStorage = new InMemoryStorage<>();
|
||||
EksPodIdentityAssociationService podIdentityAssociations = new EksPodIdentityAssociationService(
|
||||
assocStorage, iam, new RegionResolver("us-east-1", "000000000000"));
|
||||
EksAddonService service = new EksAddonService(storage, updatesStorage, catalog, iam, podIdentityAssociations);
|
||||
EksAddonService service = new EksAddonService(storage, updatesStorage, new InMemoryStorage<>(), catalog, iam, podIdentityAssociations);
|
||||
|
||||
return new Fixture(cluster, iam, role, storage, updatesStorage, catalog, podIdentityAssociations, service);
|
||||
}
|
||||
|
||||
@@ -1578,6 +1578,17 @@ class EksServiceTest {
|
||||
assertEquals(FargateProfileStatus.DELETING, deleted.getStatus());
|
||||
assertThrows(AwsException.class, () -> eksService.describeFargateProfile("my-eks-cluster", "profile-a"));
|
||||
assertEquals(List.of("profile-b"), eksService.listFargateProfiles("my-eks-cluster"));
|
||||
assertEquals(Optional.of(deleted.getFargateProfileArn()),
|
||||
eksService.findResourceArn("my-eks-cluster", "fargate-profiles", "profile-a"));
|
||||
FargateProfile replacement = eksService.createFargateProfile("my-eks-cluster", fargateProfileRequest("profile-a"));
|
||||
assertNotEquals(deleted.getFargateProfileArn(), replacement.getFargateProfileArn());
|
||||
assertEquals(Optional.of(replacement.getFargateProfileArn()),
|
||||
eksService.findResourceArn("my-eks-cluster", "fargate-profiles", "profile-a"));
|
||||
eksService.deleteFargateProfile("my-eks-cluster", "profile-a");
|
||||
eksService.deleteFargateProfile("my-eks-cluster", "profile-b");
|
||||
eksService.deleteCluster("my-eks-cluster");
|
||||
createTestCluster("my-eks-cluster");
|
||||
assertTrue(eksService.findResourceArn("my-eks-cluster", "fargate-profiles", "profile-a").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.floci.aws.services.iam;
|
||||
|
||||
import io.floci.aws.core.common.XmlParser;
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.floci.aws.testing.EnforcementFixtures;
|
||||
import io.floci.aws.testing.IamEnforcementProfile;
|
||||
import io.floci.aws.testing.OrganizationsRequests;
|
||||
@@ -15,6 +16,10 @@ import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.eks.EksClient;
|
||||
import software.amazon.awssdk.services.eks.model.EksException;
|
||||
import software.amazon.awssdk.services.eks.model.ResourceNotFoundException;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URLEncoder;
|
||||
@@ -31,6 +36,8 @@ import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* REST requests whose route no rule mapped to an IAM action were let through under enforcement
|
||||
@@ -241,6 +248,60 @@ class IamRestRouteEnforcementIntegrationTest {
|
||||
as(user, "eks").get(path).then().statusCode(200).body("nodegroup.nodegroupArn", equalTo(nodegroup));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletedEksAddonRemainsAuthorizedForItsScopedDescribeWaiter() {
|
||||
String name = "addon-waiter-" + suffix();
|
||||
eksCluster(name);
|
||||
String base = "/clusters/" + name + "/addons";
|
||||
String arn = eks().body("{\"addonName\":\"vpc-cni\"}").post(base).then().statusCode(200)
|
||||
.extract().path("addon.addonArn");
|
||||
String user = "addon-waiter-user-" + suffix();
|
||||
EnforcementFixtures.UserCredentials credentials = EnforcementFixtures.createUser(
|
||||
cleanup, baseUri.getPort(), auth(ADMIN, "iam"), user);
|
||||
policy(user, allow("eks:DescribeAddon", arn) + "," + allow("eks:DeleteAddon", arn));
|
||||
try (EksClient client = AwsTestClients.create(EksClient.builder(), credentials.accessKeyId(),
|
||||
credentials.secretAccessKey(), Region.US_EAST_1, baseUri)) {
|
||||
assertEquals(arn, client.describeAddon(r -> r.clusterName(name).addonName("vpc-cni")).addon().addonArn());
|
||||
client.deleteAddon(r -> r.clusterName(name).addonName("vpc-cni"));
|
||||
assertThrows(ResourceNotFoundException.class,
|
||||
() -> client.describeAddon(r -> r.clusterName(name).addonName("vpc-cni")));
|
||||
policy(user, allow("eks:DescribeAddon", arn + "-foreign"));
|
||||
assertEquals(403, assertThrows(EksException.class,
|
||||
() -> client.describeAddon(r -> r.clusterName(name).addonName("vpc-cni"))).statusCode());
|
||||
policy(user, allow("eks:DescribeAddon", "*") + "," + deny("eks:DescribeAddon", arn));
|
||||
assertEquals(403, assertThrows(EksException.class,
|
||||
() -> client.describeAddon(r -> r.clusterName(name).addonName("vpc-cni"))).statusCode());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletedEksFargateProfileRemainsAuthorizedForItsScopedDescribeWaiter() {
|
||||
String name = "fargate-waiter-" + suffix();
|
||||
eksCluster(name);
|
||||
String path = "/clusters/" + name + "/fargate-profiles";
|
||||
String arn = eks().body("{\"fargateProfileName\":\"fp\",\"podExecutionRoleArn\":\"arn:aws:iam::"
|
||||
+ ACCOUNT + ":role/route-fargate\"}").post(path).then().statusCode(200)
|
||||
.extract().path("fargateProfile.fargateProfileArn");
|
||||
String user = "fargate-waiter-user-" + suffix();
|
||||
EnforcementFixtures.UserCredentials credentials = EnforcementFixtures.createUser(
|
||||
cleanup, baseUri.getPort(), auth(ADMIN, "iam"), user);
|
||||
policy(user, allow("eks:DescribeFargateProfile", arn) + "," + allow("eks:DeleteFargateProfile", arn));
|
||||
try (EksClient client = AwsTestClients.create(EksClient.builder(), credentials.accessKeyId(),
|
||||
credentials.secretAccessKey(), Region.US_EAST_1, baseUri)) {
|
||||
assertEquals(arn, client.describeFargateProfile(r -> r.clusterName(name).fargateProfileName("fp"))
|
||||
.fargateProfile().fargateProfileArn());
|
||||
client.deleteFargateProfile(r -> r.clusterName(name).fargateProfileName("fp"));
|
||||
assertThrows(ResourceNotFoundException.class,
|
||||
() -> client.describeFargateProfile(r -> r.clusterName(name).fargateProfileName("fp")));
|
||||
policy(user, allow("eks:DescribeFargateProfile", arn + "-foreign"));
|
||||
assertEquals(403, assertThrows(EksException.class,
|
||||
() -> client.describeFargateProfile(r -> r.clusterName(name).fargateProfileName("fp"))).statusCode());
|
||||
policy(user, allow("eks:DescribeFargateProfile", "*") + "," + deny("eks:DescribeFargateProfile", arn));
|
||||
assertEquals(403, assertThrows(EksException.class,
|
||||
() -> client.describeFargateProfile(r -> r.clusterName(name).fargateProfileName("fp"))).statusCode());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void eachResourceAnEksClusterHoldsIsCheckedAgainstTheArnEksHoldsItUnder() {
|
||||
// Each is looked up as EKS looks it up, so a Deny on its own ARN applies where the action is allowed
|
||||
|
||||
Reference in new issue
Block a user