mirror of
https://github.com/floci-io/floci.git
synced 2026-11-29 00:21:51 -05:00
feat(eks): associate access policies with access entries (#6427)
AssociateAccessPolicy, ListAssociatedAccessPolicies and DisassociateAccessPolicy had no route, so their requests fell through to S3's path-style catch-all and came back as an S3 XML error. Terraform's aws_eks_access_policy_association could not be created. The three operations follow access entries: they are management metadata, stored and returned but not enforced in Kubernetes. Each association is kept on its access entry, so deleting the entry or the cluster removes it, and entries persisted before this change load with no policies. ListAccessEntries now honours associatedPolicyArn. Validation follows the EKS user guide: only STANDARD entries take access policies, the scope is cluster (no namespaces) or namespace (one or more names, wildcards kept, a repeated name stored once), and associating a policy again replaces its scope and keeps its associatedAt. policyArn must be an EKS cluster access policy ARN in the cluster's partition; the name is not checked against AWS's list. Listing pages from 1 to 100, with tokens bound to the entry they came from. Disassociating a policy the entry does not hold succeeds, since EKS documents no error for it; a missing entry is still ResourceNotFoundException, and an ARN that associate would refuse is InvalidParameterException. ListAccessEntries now pages through the shared Pagination helper too, so a token issued before this change is refused. Under IAM enforcement the list and disassociate routes were checked as DescribeAccessEntry and DeleteAccessEntry, and all three against a principal ending in /access-policies, so a grant scoped to the entry never matched. They are now checked as their own actions, against the entry's ARN. A DELETE is a disassociate only when a cluster access policy ARN follows access-policies, in the route, the IAM rule and the resource, so an entry whose IAM path holds an access-policies segment can still be deleted. The router matches the encoded path, so a principal sent URL-encoded, as the SDKs send it, is never split. Co-authored-by: Robin Baeckman <27041454+RobinBaeckman@users.noreply.github.com>
This commit is contained in:
1 parent
bc75edb4c4
commit
7453011c43
15 files changed
+719
-79
No files matched your search
@@ -3,11 +3,15 @@ package io.github.hectorvent.floci.services.eks;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.Base64Url;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
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.eks.model.AccessEntry;
|
||||
import io.github.hectorvent.floci.services.eks.model.AccessScope;
|
||||
import io.github.hectorvent.floci.services.eks.model.AssociateAccessPolicyRequest;
|
||||
import io.github.hectorvent.floci.services.eks.model.AssociatedAccessPolicy;
|
||||
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.CreateAccessEntryRequest;
|
||||
@@ -18,19 +22,22 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.Clock;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
/** Access-entry management metadata; Kubernetes authorization is handled separately. */
|
||||
/** Access-entry and access-policy management metadata; Kubernetes authorization is handled separately. */
|
||||
@ApplicationScoped
|
||||
public class EksAccessEntryService {
|
||||
private final StorageBackend<String, StoredEntry> entries;
|
||||
private final IamService iam;
|
||||
private final Clock clock;
|
||||
|
||||
@Inject
|
||||
public EksAccessEntryService(StorageFactory factory, IamService iam) {
|
||||
@@ -38,16 +45,104 @@ public class EksAccessEntryService {
|
||||
}
|
||||
|
||||
EksAccessEntryService(StorageBackend<String, StoredEntry> entries, IamService iam) {
|
||||
this(entries, iam, Clock.systemUTC());
|
||||
}
|
||||
|
||||
EksAccessEntryService(StorageBackend<String, StoredEntry> entries, IamService iam, Clock clock) {
|
||||
this.entries = entries;
|
||||
this.iam = iam;
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
@RegisterForReflection
|
||||
public record StoredEntry(AccessEntry entry, String principalId, String clientRequestToken) {}
|
||||
public record StoredEntry(AccessEntry entry, String principalId, String clientRequestToken,
|
||||
List<AssociatedAccessPolicy> accessPolicies) {
|
||||
public StoredEntry {
|
||||
accessPolicies = accessPolicies == null ? List.of() : List.copyOf(accessPolicies);
|
||||
}
|
||||
}
|
||||
|
||||
@RegisterForReflection
|
||||
public record Page(List<String> accessEntries, String nextToken) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record PolicyPage(List<AssociatedAccessPolicy> associatedAccessPolicies, String nextToken) {}
|
||||
|
||||
public synchronized AssociatedAccessPolicy associate(Cluster cluster, String principal,
|
||||
AssociateAccessPolicyRequest request) {
|
||||
StoredEntry stored = storedEntry(cluster, principal);
|
||||
if (request == null || request.policyArn() == null) {
|
||||
throw invalid("policyArn is required");
|
||||
}
|
||||
requireAccessPolicyArn(cluster, request.policyArn());
|
||||
AccessScope scope = accessScope(request.accessScope());
|
||||
if (!"STANDARD".equals(stored.entry().type())) {
|
||||
throw new AwsException("InvalidRequestException",
|
||||
"Access policies can only be associated with STANDARD access entries", 400);
|
||||
}
|
||||
double now = clock.millis() / 1000.0;
|
||||
double associatedAt = stored.accessPolicies().stream()
|
||||
.filter(existing -> existing.policyArn().equals(request.policyArn()))
|
||||
.mapToDouble(AssociatedAccessPolicy::associatedAt).findFirst().orElse(now);
|
||||
AssociatedAccessPolicy association = new AssociatedAccessPolicy(request.policyArn(), scope, associatedAt, now);
|
||||
List<AssociatedAccessPolicy> policies = new ArrayList<>(stored.accessPolicies());
|
||||
policies.removeIf(existing -> existing.policyArn().equals(request.policyArn()));
|
||||
policies.add(association);
|
||||
policies.sort(Comparator.comparing(AssociatedAccessPolicy::policyArn));
|
||||
entries.put(prefix(cluster) + principal, new StoredEntry(stored.entry(), stored.principalId(),
|
||||
stored.clientRequestToken(), policies));
|
||||
return association;
|
||||
}
|
||||
|
||||
public synchronized PolicyPage listAccessPolicies(Cluster cluster, String principal, Integer maxResults,
|
||||
String nextToken) {
|
||||
List<AssociatedAccessPolicy> policies = storedEntry(cluster, principal).accessPolicies();
|
||||
// The newline ends the principal, so a token for role/a:b is not read as one for role/a.
|
||||
PaginatedResult<AssociatedAccessPolicy> page = Pagination.paginate(policies, AssociatedAccessPolicy::policyArn,
|
||||
pageSize(maxResults), nextToken, "policies:" + prefix(cluster) + principal + "\n",
|
||||
token -> invalid("Invalid nextToken"));
|
||||
return new PolicyPage(page.items(), page.nextToken());
|
||||
}
|
||||
|
||||
public synchronized void disassociate(Cluster cluster, String principal, String policyArn) {
|
||||
StoredEntry stored = storedEntry(cluster, principal);
|
||||
requireAccessPolicyArn(cluster, policyArn);
|
||||
List<AssociatedAccessPolicy> policies = stored.accessPolicies().stream()
|
||||
.filter(existing -> !existing.policyArn().equals(policyArn)).toList();
|
||||
if (policies.size() == stored.accessPolicies().size()) {
|
||||
return; // EKS documents no error for a policy the entry does not hold
|
||||
}
|
||||
entries.put(prefix(cluster) + principal, new StoredEntry(stored.entry(), stored.principalId(),
|
||||
stored.clientRequestToken(), policies));
|
||||
}
|
||||
|
||||
private StoredEntry storedEntry(Cluster cluster, String principal) {
|
||||
requireApiAccess(cluster);
|
||||
return entries.get(prefix(cluster) + principal)
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Access entry not found", 404));
|
||||
}
|
||||
|
||||
private static AccessScope accessScope(AccessScope scope) {
|
||||
if (scope == null || scope.type() == null) {
|
||||
throw invalid("accessScope.type is required");
|
||||
}
|
||||
List<String> namespaces = scope.namespaces() == null ? List.of() : scope.namespaces();
|
||||
if ("cluster".equals(scope.type())) {
|
||||
if (!namespaces.isEmpty()) {
|
||||
throw invalid("namespaces can only be specified for a namespace access scope");
|
||||
}
|
||||
return new AccessScope("cluster", List.of());
|
||||
}
|
||||
if (!"namespace".equals(scope.type())) {
|
||||
throw invalid("accessScope.type must be cluster or namespace");
|
||||
}
|
||||
if (namespaces.isEmpty() || namespaces.stream().anyMatch(namespace -> namespace == null || namespace.isBlank())) {
|
||||
throw invalid("A namespace access scope requires nonempty namespace names");
|
||||
}
|
||||
// Terraform models namespaces as a set, so a repeated name must not come back twice.
|
||||
return new AccessScope("namespace", List.copyOf(new LinkedHashSet<>(namespaces)));
|
||||
}
|
||||
|
||||
public synchronized AccessEntry create(Cluster cluster, CreateAccessEntryRequest request) {
|
||||
requireApiAccess(cluster);
|
||||
if (request == null || request.principalArn() == null) {
|
||||
@@ -125,13 +220,13 @@ public class EksAccessEntryService {
|
||||
if (entries.get(key).isPresent()) {
|
||||
throw new AwsException("ResourceInUseException", "Access entry already exists", 409);
|
||||
}
|
||||
double now = Instant.now().toEpochMilli() / 1000.0;
|
||||
double now = clock.millis() / 1000.0;
|
||||
String entryArn = new AwsArnUtils.Arn(clusterArn.partition(), "eks", clusterArn.region(), clusterArn.accountId(),
|
||||
"access-entry/" + cluster.getName() + "/" + (role ? "role" : "user") + "/" + arn.accountId() + "/" + name
|
||||
+ "/" + UUID.randomUUID()).toString();
|
||||
AccessEntry entry = new AccessEntry(entryArn, cluster.getName(), principal,
|
||||
type, username, List.copyOf(groups), Map.copyOf(tags), now, now);
|
||||
entries.put(key, new StoredEntry(entry, principalId, token));
|
||||
entries.put(key, new StoredEntry(entry, principalId, token, List.of()));
|
||||
return entry;
|
||||
}
|
||||
|
||||
@@ -145,9 +240,7 @@ public class EksAccessEntryService {
|
||||
}
|
||||
|
||||
public synchronized AccessEntry describe(Cluster cluster, String principal) {
|
||||
requireApiAccess(cluster);
|
||||
return entries.get(prefix(cluster) + principal).map(StoredEntry::entry)
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Access entry not found", 404));
|
||||
return storedEntry(cluster, principal).entry();
|
||||
}
|
||||
|
||||
public synchronized void delete(Cluster cluster, String principal) {
|
||||
@@ -156,34 +249,19 @@ public class EksAccessEntryService {
|
||||
}
|
||||
|
||||
public synchronized Page list(Cluster cluster, Integer maxResults, String nextToken) {
|
||||
return list(cluster, maxResults, nextToken, null);
|
||||
}
|
||||
|
||||
public synchronized Page list(Cluster cluster, Integer maxResults, String nextToken, String associatedPolicyArn) {
|
||||
requireApiAccess(cluster);
|
||||
int limit = maxResults == null ? 100 : maxResults;
|
||||
if (limit < 1 || limit > 100) {
|
||||
throw invalid("maxResults must be between 1 and 100");
|
||||
}
|
||||
String prefix = prefix(cluster);
|
||||
String after = "";
|
||||
if (nextToken != null) {
|
||||
try {
|
||||
String decoded = Base64Url.decode(nextToken);
|
||||
if (!decoded.startsWith(prefix + "\n")) {
|
||||
throw invalid("nextToken does not belong to this cluster");
|
||||
}
|
||||
after = decoded.substring(prefix.length() + 1);
|
||||
if (after.isBlank()) {
|
||||
throw invalid("Invalid nextToken");
|
||||
}
|
||||
} catch (IllegalArgumentException exception) {
|
||||
throw invalid("Invalid nextToken");
|
||||
}
|
||||
}
|
||||
String cursor = after;
|
||||
List<String> remaining = entries.scan(key -> key.startsWith(prefix)).stream()
|
||||
.map(stored -> stored.entry().principalArn()).filter(principal -> principal.compareTo(cursor) > 0)
|
||||
.sorted(Comparator.naturalOrder()).toList();
|
||||
List<String> page = remaining.stream().limit(limit).toList();
|
||||
String token = remaining.size() > limit ? Base64Url.encode(prefix + "\n" + page.getLast()) : null;
|
||||
return new Page(page, token);
|
||||
List<String> principals = entries.scan(key -> key.startsWith(prefix)).stream()
|
||||
.filter(stored -> associatedPolicyArn == null || stored.accessPolicies().stream()
|
||||
.anyMatch(policy -> policy.policyArn().equals(associatedPolicyArn)))
|
||||
.map(stored -> stored.entry().principalArn()).toList();
|
||||
PaginatedResult<String> page = Pagination.paginate(principals, principal -> principal, pageSize(maxResults),
|
||||
nextToken, "entries:" + prefix, token -> invalid("Invalid nextToken"));
|
||||
return new Page(page.items(), page.nextToken());
|
||||
}
|
||||
|
||||
public synchronized void deleteClusterEntries(Cluster cluster) {
|
||||
@@ -207,6 +285,31 @@ public class EksAccessEntryService {
|
||||
}
|
||||
}
|
||||
|
||||
private static int pageSize(Integer maxResults) {
|
||||
int limit = maxResults == null ? 100 : maxResults;
|
||||
if (limit < 1 || limit > 100) {
|
||||
throw invalid("maxResults must be between 1 and 100");
|
||||
}
|
||||
return limit;
|
||||
}
|
||||
|
||||
private static void requireAccessPolicyArn(Cluster cluster, String policyArn) {
|
||||
AwsArnUtils.Arn arn;
|
||||
try {
|
||||
arn = AwsArnUtils.parse(policyArn);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw invalid("policyArn must identify an EKS cluster access policy");
|
||||
}
|
||||
boolean accessPolicy = arn.partition().equals(AwsArnUtils.parse(cluster.getArn()).partition())
|
||||
&& arn.service().equals("eks")
|
||||
&& arn.region().isEmpty()
|
||||
&& arn.accountId().equals("aws") // partition-literal: the AWS-managed account, "aws" in every partition
|
||||
&& arn.resource().matches("cluster-access-policy/[A-Za-z0-9]+");
|
||||
if (!accessPolicy) {
|
||||
throw invalid("policyArn must identify an EKS cluster access policy");
|
||||
}
|
||||
}
|
||||
|
||||
private static AwsException invalid(String message) {
|
||||
return new AwsException("InvalidParameterException", message, 400);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package io.github.hectorvent.floci.services.eks;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.services.eks.model.Addon;
|
||||
import io.github.hectorvent.floci.services.eks.model.AssociateAccessPolicyRequest;
|
||||
import io.github.hectorvent.floci.services.eks.model.AssociatedAccessPolicy;
|
||||
import io.github.hectorvent.floci.services.eks.model.Cluster;
|
||||
import io.github.hectorvent.floci.services.eks.model.CreateAccessEntryRequest;
|
||||
import io.github.hectorvent.floci.services.eks.model.CreateAddonRequest;
|
||||
@@ -27,6 +29,7 @@ import jakarta.ws.rs.QueryParam;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -42,6 +45,15 @@ import java.util.Map;
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public class EksController {
|
||||
|
||||
/**
|
||||
* A cluster access policy ARN as a path carries it, its {@code :} and {@code /} encoded or not. Only such an
|
||||
* ARN after {@code access-policies} makes a DELETE a disassociate, so an entry whose IAM path holds an
|
||||
* {@code access-policies} segment can still be deleted.
|
||||
*/
|
||||
public static final String ACCESS_POLICY_ARN = "arn(?::|%3[aA])[a-z-]+(?::|%3[aA])eks(?::|%3[aA])(?::|%3[aA])"
|
||||
+ "aws(?::|%3[aA])cluster-access-policy(?:/|%2[fF])[^/]+";
|
||||
|
||||
|
||||
private final EksService eksService;
|
||||
private final EksAccessEntryService accessEntries;
|
||||
private final EksPodIdentityAssociationService podIdentityAssociations;
|
||||
@@ -183,9 +195,10 @@ public class EksController {
|
||||
@Path("/clusters/{name}/access-entries")
|
||||
public Response listAccessEntries(@PathParam("name") String name,
|
||||
@QueryParam("maxResults") String maxResults,
|
||||
@QueryParam("nextToken") String nextToken) {
|
||||
@QueryParam("nextToken") String nextToken,
|
||||
@QueryParam("associatedPolicyArn") String associatedPolicyArn) {
|
||||
EksAccessEntryService.Page page = accessEntries.list(eksService.describeCluster(name),
|
||||
Pagination.parseMaxResults(maxResults, "InvalidParameterException"), nextToken);
|
||||
Pagination.parseMaxResults(maxResults, "InvalidParameterException"), nextToken, associatedPolicyArn);
|
||||
return Response.ok(page.nextToken() == null ? Map.of("accessEntries", page.accessEntries())
|
||||
: Map.of("accessEntries", page.accessEntries(), "nextToken", page.nextToken())).build();
|
||||
}
|
||||
@@ -203,6 +216,42 @@ public class EksController {
|
||||
return Response.ok(Map.of()).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/clusters/{name}/access-entries/{principalArn: .+}/access-policies")
|
||||
public Response associateAccessPolicy(@PathParam("name") String name, @PathParam("principalArn") String principalArn,
|
||||
AssociateAccessPolicyRequest request) {
|
||||
AssociatedAccessPolicy policy = accessEntries.associate(eksService.describeCluster(name), principalArn, request);
|
||||
return Response.ok(Map.of("clusterName", name, "principalArn", principalArn,
|
||||
"associatedAccessPolicy", policy)).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/clusters/{name}/access-entries/{principalArn: .+}/access-policies")
|
||||
public Response listAssociatedAccessPolicies(@PathParam("name") String name,
|
||||
@PathParam("principalArn") String principalArn,
|
||||
@QueryParam("maxResults") String maxResults,
|
||||
@QueryParam("nextToken") String nextToken) {
|
||||
EksAccessEntryService.PolicyPage page = accessEntries.listAccessPolicies(eksService.describeCluster(name),
|
||||
principalArn, Pagination.parseMaxResults(maxResults, "InvalidParameterException"), nextToken);
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("clusterName", name);
|
||||
body.put("principalArn", principalArn);
|
||||
body.put("associatedAccessPolicies", page.associatedAccessPolicies());
|
||||
if (page.nextToken() != null) {
|
||||
body.put("nextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(body).build();
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/clusters/{name}/access-entries/{principalArn: .+}/access-policies/{policyArn: " + ACCESS_POLICY_ARN + "}")
|
||||
public Response disassociateAccessPolicy(@PathParam("name") String name,
|
||||
@PathParam("principalArn") String principalArn,
|
||||
@PathParam("policyArn") String policyArn) {
|
||||
accessEntries.disassociate(eksService.describeCluster(name), principalArn, policyArn);
|
||||
return Response.ok(Map.of()).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/clusters/{name}/addons")
|
||||
public Response createAddon(@PathParam("name") String name, CreateAddonRequest request) {
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package io.github.hectorvent.floci.services.eks.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public record AccessScope(String type, List<String> namespaces) {}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package io.github.hectorvent.floci.services.eks.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public record AssociateAccessPolicyRequest(String policyArn, AccessScope accessScope) {}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.eks.model;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
public record AssociatedAccessPolicy(String policyArn, AccessScope accessScope, double associatedAt,
|
||||
double modifiedAt) {}
|
||||
@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.core.common.AwsQueryServiceResolver;
|
||||
import io.github.hectorvent.floci.core.common.ProtocolClaim;
|
||||
import io.github.hectorvent.floci.core.common.WireProtocol;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbPartiQLParser;
|
||||
import io.github.hectorvent.floci.services.eks.EksController;
|
||||
import io.github.hectorvent.floci.services.s3.S3ObjectActions;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
@@ -288,12 +289,15 @@ public class IamActionRegistry {
|
||||
rule("pipes", "DELETE", "^/tags(/.+)?$", "pipes:UntagResource"),
|
||||
|
||||
// ── EKS ────────────────────────────────────────────────────────────────
|
||||
// Floci takes an access entry's principal ARN whole, slashes included, so a GET or DELETE under an
|
||||
// entry is DescribeAccessEntry or DeleteAccessEntry: the entry's access policy routes, which Floci
|
||||
// does not serve, run as those and are checked as those.
|
||||
// Floci takes an access entry's principal ARN whole, slashes included, so any other GET or DELETE under
|
||||
// an entry is DescribeAccessEntry or DeleteAccessEntry. The entry's access policy routes come first, as
|
||||
// they win in the router; a DELETE is a disassociate only when a cluster access policy ARN follows.
|
||||
rule("eks", "GET", "^/clusters/[^/]+/access-entries/.+/access-policies$", "eks:ListAssociatedAccessPolicies"),
|
||||
rule("eks", "POST", "^/clusters/[^/]+/access-entries/.+/access-policies$", "eks:AssociateAccessPolicy"),
|
||||
rule("eks", "DELETE", "^/clusters/[^/]+/access-entries/.+/access-policies/" + EksController.ACCESS_POLICY_ARN + "$",
|
||||
"eks:DisassociateAccessPolicy"),
|
||||
rule("eks", "GET", "^/clusters/[^/]+/access-entries/.+$", "eks:DescribeAccessEntry"),
|
||||
rule("eks", "DELETE", "^/clusters/[^/]+/access-entries/.+$", "eks:DeleteAccessEntry"),
|
||||
rule("eks", "POST", "^/clusters/[^/]+/access-entries/.+/access-policies$", "eks:AssociateAccessPolicy"),
|
||||
rule("eks", "POST", "^/clusters/[^/]+/access-entries/.+$", "eks:UpdateAccessEntry"),
|
||||
rule("eks", "GET", "^/clusters/[^/]+/access-entries$", "eks:ListAccessEntries"),
|
||||
rule("eks", "POST", "^/clusters/[^/]+/access-entries$", "eks:CreateAccessEntry"),
|
||||
|
||||
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.core.common.ServicePrincipals;
|
||||
import io.github.hectorvent.floci.core.common.SigV4CredentialScope;
|
||||
import io.github.hectorvent.floci.core.common.StoredResourceArns;
|
||||
import io.github.hectorvent.floci.services.cloudwatch.logs.LogGroupTagTarget;
|
||||
import io.github.hectorvent.floci.services.eks.EksController;
|
||||
import io.github.hectorvent.floci.services.eks.EksService;
|
||||
import io.github.hectorvent.floci.services.kms.KmsService;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaArnUtils;
|
||||
@@ -37,6 +38,7 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Constructs the target resource ARN for a request so the policy evaluator
|
||||
@@ -48,6 +50,8 @@ import java.util.regex.Matcher;
|
||||
@ApplicationScoped
|
||||
public class ResourceArnBuilder {
|
||||
|
||||
private static final Pattern ACCESS_POLICY_ARN = Pattern.compile(EksController.ACCESS_POLICY_ARN);
|
||||
|
||||
/** The only scheme IAM accepts on an OIDC provider URL, and the part its ARN drops. */
|
||||
private static final String OIDC_URL_SCHEME = "https://";
|
||||
|
||||
@@ -1164,7 +1168,7 @@ public class ResourceArnBuilder {
|
||||
return "*";
|
||||
}
|
||||
return switch (segments[0]) {
|
||||
case "clusters" -> eksClusterRouteArn(name, segments, region, accountId);
|
||||
case "clusters" -> eksClusterRouteArn(name, segments, ctx.getMethod(), region, accountId);
|
||||
case "cluster-registrations" -> eksClusterArn(name, region, accountId);
|
||||
case "eks-anywhere-subscriptions" ->
|
||||
AwsArnUtils.Arn.of("eks", region, accountId, "eks-anywhere-subscription/" + name).toString();
|
||||
@@ -1172,8 +1176,9 @@ public class ResourceArnBuilder {
|
||||
};
|
||||
}
|
||||
|
||||
/** The resource of a route under the cluster {@code cluster}, its path split into {@code segments}. */
|
||||
private String eksClusterRouteArn(String cluster, String[] segments, String region, String accountId) {
|
||||
/** The resource of a {@code method} route under the cluster {@code cluster}, its path split into {@code segments}. */
|
||||
private String eksClusterRouteArn(String cluster, String[] segments, String method, String region,
|
||||
String accountId) {
|
||||
if (segments.length < 4 || segments[3].isEmpty()) {
|
||||
return eksClusterArn(cluster, region, accountId);
|
||||
}
|
||||
@@ -1182,7 +1187,7 @@ public class ResourceArnBuilder {
|
||||
case "node-groups", "fargate-profiles", "addons", "pod-identity-associations" ->
|
||||
eksHeldArn(cluster, collection, pathSegment(segments[3]));
|
||||
case "access-entries" -> eksHeldArn(cluster, collection,
|
||||
pathSegment(String.join("/", List.of(segments).subList(3, segments.length))));
|
||||
pathSegment(String.join("/", List.of(segments).subList(3, accessEntryEnd(segments, method)))));
|
||||
case "updates" -> {
|
||||
// The updates Floci keeps are its add-ons', and EKS returns one whether or not the request names
|
||||
// its add-on, so the update is checked against the add-on it was made to.
|
||||
@@ -1198,6 +1203,28 @@ public class ResourceArnBuilder {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Where an access entry's principal ends in {@code segments}: before {@code access-policies} on the routes
|
||||
* that act on the entry's policies, as the router reads them (a DELETE only when a cluster access policy
|
||||
* ARN follows), and at the end on every other route.
|
||||
*/
|
||||
private static int accessEntryEnd(String[] segments, String method) {
|
||||
int last = segments.length - 1;
|
||||
if (("GET".equalsIgnoreCase(method) || "POST".equalsIgnoreCase(method))
|
||||
&& last > 3 && "access-policies".equals(segments[last])) {
|
||||
return last;
|
||||
}
|
||||
if ("DELETE".equalsIgnoreCase(method)) {
|
||||
for (int i = last - 1; i > 3; i--) {
|
||||
if ("access-policies".equals(segments[i]) && ACCESS_POLICY_ARN.matcher(
|
||||
String.join("/", List.of(segments).subList(i + 1, segments.length))).matches()) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return segments.length;
|
||||
}
|
||||
|
||||
/** The ARN EKS holds the cluster {@code name} under, or else the one a create mints in {@code region}. */
|
||||
private String eksClusterArn(String name, String region, String accountId) {
|
||||
Optional<String> held = eksService == null ? Optional.empty()
|
||||
|
||||
Reference in new issue
Block a user