From 5746133527b03a4e29bf35c3c977dff83dfbb49d Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Fri, 27 Nov 2026 18:48:48 -0400 Subject: [PATCH] refactor(iam): check EKS by its generated routes alone (#9624) IamActionRegistry kept 70 hand route rules for EKS. The generated REST routes, read from botocore, map every EKS route to the same action, except the access entry routes Floci's controller serves with the principal ARN unencoded: it takes the principal as {principalArn: .+} and a cluster access policy ARN after it with its slash sent as it is or encoded, where the model gives each one segment. Those six operations join ARN_SPANNING_ROUTES, as API Gateway v2's and OAM's tag routes are, so their labels span the slashes. A DELETE on a principal whose IAM path holds an access-policies segment matches both DeleteAccessEntry's route and DisassociateAccessPolicy's, which outranks it, while EKS's router takes it as a disassociate only when a cluster access policy ARN follows. A request is now matched, of the routes it matches, to the one whose labels the path gives the values the router gave the path parameters of the method it dispatched to, when there is one: the route of the operation that runs. With that, the rules go, and none of EKS's is left. --- .../floci/aws/services/eks/EksController.java | 2 +- .../aws/services/iam/IamActionRegistry.java | 111 ++++-------------- .../floci/aws/services/iam/IamRestRoutes.java | 55 +++++++-- src/main/resources/aws/rest-routes.json | 12 +- .../services/iam/IamActionRegistryTest.java | 25 +++- ...amRestRouteEnforcementIntegrationTest.java | 8 +- tools/aws/regen_rest_routes.py | 5 +- tools/aws/test_regen_rest_routes.py | 12 ++ 8 files changed, 114 insertions(+), 116 deletions(-) diff --git a/src/main/java/io/floci/aws/services/eks/EksController.java b/src/main/java/io/floci/aws/services/eks/EksController.java index 7a5b06406d..accc04edae 100644 --- a/src/main/java/io/floci/aws/services/eks/EksController.java +++ b/src/main/java/io/floci/aws/services/eks/EksController.java @@ -57,7 +57,7 @@ public class EksController { * 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])" + private 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])[^/]+"; diff --git a/src/main/java/io/floci/aws/services/iam/IamActionRegistry.java b/src/main/java/io/floci/aws/services/iam/IamActionRegistry.java index 7a56e63e3d..3aaf3e800a 100644 --- a/src/main/java/io/floci/aws/services/iam/IamActionRegistry.java +++ b/src/main/java/io/floci/aws/services/iam/IamActionRegistry.java @@ -8,7 +8,6 @@ import io.floci.aws.core.common.RequestResources; import io.floci.aws.core.common.ResteasyRequests; import io.floci.aws.core.common.SignedServiceRoute; import io.floci.aws.core.common.WireProtocol; -import io.floci.aws.services.eks.EksController; import io.floci.aws.services.s3.S3BucketSubresources; import io.floci.aws.services.s3.S3ObjectActions; import jakarta.enterprise.context.ApplicationScoped; @@ -22,6 +21,7 @@ import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Optional; @@ -239,91 +239,6 @@ public class IamActionRegistry { rule("rds-data", "POST", "^/CommitTransaction/?$", "rds-data:CommitTransaction"), rule("rds-data", "POST", "^/RollbackTransaction/?$", "rds-data:RollbackTransaction"), - // ── EKS ──────────────────────────────────────────────────────────────── - // 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/.+$", "eks:UpdateAccessEntry"), - rule("eks", "GET", "^/clusters/[^/]+/access-entries$", "eks:ListAccessEntries"), - rule("eks", "POST", "^/clusters/[^/]+/access-entries$", "eks:CreateAccessEntry"), - rule("eks", "GET", "^/clusters$", "eks:ListClusters"), - rule("eks", "POST", "^/clusters$", "eks:CreateCluster"), - rule("eks", "GET", "^/clusters/[^/]+$", "eks:DescribeCluster"), - rule("eks", "DELETE", "^/clusters/[^/]+$", "eks:DeleteCluster"), - rule("eks", "POST", "^/clusters/[^/]+/update-config$", "eks:UpdateClusterConfig"), - rule("eks", "POST", "^/clusters/[^/]+/encryption-config/associate$", "eks:AssociateEncryptionConfig"), - rule("eks", "GET", "^/clusters/[^/]+/updates$", "eks:ListUpdates"), - rule("eks", "POST", "^/clusters/[^/]+/updates$", "eks:UpdateClusterVersion"), - rule("eks", "GET", "^/clusters/[^/]+/updates/[^/]+$", "eks:DescribeUpdate"), - rule("eks", "POST", "^/clusters/[^/]+/updates/[^/]+/cancel-update$", "eks:CancelUpdate"), - rule("eks", "GET", "^/clusters/[^/]+/node-groups$", "eks:ListNodegroups"), - rule("eks", "POST", "^/clusters/[^/]+/node-groups$", "eks:CreateNodegroup"), - rule("eks", "GET", "^/clusters/[^/]+/node-groups/[^/]+$", "eks:DescribeNodegroup"), - rule("eks", "DELETE", "^/clusters/[^/]+/node-groups/[^/]+$", "eks:DeleteNodegroup"), - rule("eks", "POST", "^/clusters/[^/]+/node-groups/[^/]+/update-config$", "eks:UpdateNodegroupConfig"), - rule("eks", "POST", "^/clusters/[^/]+/node-groups/[^/]+/update-version$", "eks:UpdateNodegroupVersion"), - rule("eks", "GET", "^/clusters/[^/]+/fargate-profiles$", "eks:ListFargateProfiles"), - rule("eks", "POST", "^/clusters/[^/]+/fargate-profiles$", "eks:CreateFargateProfile"), - rule("eks", "GET", "^/clusters/[^/]+/fargate-profiles/[^/]+$", "eks:DescribeFargateProfile"), - rule("eks", "DELETE", "^/clusters/[^/]+/fargate-profiles/[^/]+$", "eks:DeleteFargateProfile"), - rule("eks", "GET", "^/clusters/[^/]+/addons$", "eks:ListAddons"), - rule("eks", "POST", "^/clusters/[^/]+/addons$", "eks:CreateAddon"), - rule("eks", "GET", "^/clusters/[^/]+/addons/[^/]+$", "eks:DescribeAddon"), - rule("eks", "DELETE", "^/clusters/[^/]+/addons/[^/]+$", "eks:DeleteAddon"), - rule("eks", "POST", "^/clusters/[^/]+/addons/[^/]+/update$", "eks:UpdateAddon"), - rule("eks", "GET", "^/addons/supported-versions$", "eks:DescribeAddonVersions"), - rule("eks", "GET", "^/addons/configuration-schemas$", "eks:DescribeAddonConfiguration"), - rule("eks", "GET", "^/clusters/[^/]+/pod-identity-associations$", "eks:ListPodIdentityAssociations"), - rule("eks", "POST", "^/clusters/[^/]+/pod-identity-associations$", "eks:CreatePodIdentityAssociation"), - rule("eks", "GET", "^/clusters/[^/]+/pod-identity-associations/[^/]+$", - "eks:DescribePodIdentityAssociation"), - rule("eks", "POST", "^/clusters/[^/]+/pod-identity-associations/[^/]+$", - "eks:UpdatePodIdentityAssociation"), - rule("eks", "DELETE", "^/clusters/[^/]+/pod-identity-associations/[^/]+$", - "eks:DeletePodIdentityAssociation"), - rule("eks", "GET", "^/clusters/[^/]+/identity-provider-configs$", "eks:ListIdentityProviderConfigs"), - rule("eks", "POST", "^/clusters/[^/]+/identity-provider-configs/associate$", - "eks:AssociateIdentityProviderConfig"), - rule("eks", "POST", "^/clusters/[^/]+/identity-provider-configs/describe$", - "eks:DescribeIdentityProviderConfig"), - rule("eks", "POST", "^/clusters/[^/]+/identity-provider-configs/disassociate$", - "eks:DisassociateIdentityProviderConfig"), - rule("eks", "POST", "^/clusters/[^/]+/insights$", "eks:ListInsights"), - rule("eks", "GET", "^/clusters/[^/]+/insights/[^/]+$", "eks:DescribeInsight"), - rule("eks", "GET", "^/clusters/[^/]+/insights-refresh$", "eks:DescribeInsightsRefresh"), - rule("eks", "POST", "^/clusters/[^/]+/insights-refresh$", "eks:StartInsightsRefresh"), - rule("eks", "GET", "^/clusters/[^/]+/capabilities$", "eks:ListCapabilities"), - rule("eks", "POST", "^/clusters/[^/]+/capabilities$", "eks:CreateCapability"), - rule("eks", "GET", "^/clusters/[^/]+/capabilities/[^/]+$", "eks:DescribeCapability"), - rule("eks", "POST", "^/clusters/[^/]+/capabilities/[^/]+$", "eks:UpdateCapability"), - rule("eks", "DELETE", "^/clusters/[^/]+/capabilities/[^/]+$", "eks:DeleteCapability"), - rule("eks", "GET", "^/clusters/[^/]+/certificate-authorities$", "eks:ListCertificateAuthorities"), - rule("eks", "POST", "^/clusters/[^/]+/certificate-authorities$", "eks:CreateCertificateAuthority"), - rule("eks", "GET", "^/clusters/[^/]+/certificate-authorities/[^/]+$", - "eks:DescribeCertificateAuthority"), - rule("eks", "DELETE", "^/clusters/[^/]+/certificate-authorities/[^/]+$", - "eks:DeleteCertificateAuthority"), - rule("eks", "POST", "^/clusters/[^/]+/certificate-authorities/[^/]+/activate$", - "eks:ActivateCertificateAuthority"), - rule("eks", "GET", "^/access-policies$", "eks:ListAccessPolicies"), - rule("eks", "GET", "^/cluster-versions$", "eks:DescribeClusterVersions"), - rule("eks", "POST", "^/cluster-registrations$", "eks:RegisterCluster"), - rule("eks", "DELETE", "^/cluster-registrations/[^/]+$", "eks:DeregisterCluster"), - rule("eks", "GET", "^/eks-anywhere-subscriptions$", "eks:ListEksAnywhereSubscriptions"), - rule("eks", "POST", "^/eks-anywhere-subscriptions$", "eks:CreateEksAnywhereSubscription"), - rule("eks", "GET", "^/eks-anywhere-subscriptions/[^/]+$", "eks:DescribeEksAnywhereSubscription"), - rule("eks", "POST", "^/eks-anywhere-subscriptions/[^/]+$", "eks:UpdateEksAnywhereSubscription"), - rule("eks", "DELETE", "^/eks-anywhere-subscriptions/[^/]+$", "eks:DeleteEksAnywhereSubscription"), - rule("eks", "GET", "^/tags(/.+)?$", "eks:ListTagsForResource"), - rule("eks", "POST", "^/tags(/.+)?$", "eks:TagResource"), - rule("eks", "DELETE", "^/tags(/.+)?$", "eks:UntagResource"), - // ── Resource Groups ───────────────────────────────────────────────────── rule("resource-groups", "POST", "^/_rg/groups/?$", "resource-groups:CreateGroup"), rule("resource-groups", "POST", "^/_rg/get-group/?$", "resource-groups:GetGroup"), @@ -461,7 +376,7 @@ public class IamActionRegistry { } MultivaluedMap query = ctx.getUriInfo() == null ? null : ctx.getUriInfo().getQueryParameters(); Optional match = restRoutes.match(credentialScope, method, withoutInternalPrefix(ctx, path), - query, movedUnderPrefix(ctx), ctx.getHeaders()); + query, movedUnderPrefix(ctx), ctx.getHeaders(), routedLabels(ctx)); if (match.isPresent()) { String action = IamAuthorizedActions.actionFor(credentialScope, match.get().operation(), method, match.get().model(), query); @@ -511,7 +426,7 @@ public class IamActionRegistry { ContainerRequestContext ctx) { return routes.match(credentialScope, ctx.getMethod().toUpperCase(), withoutInternalPrefix(ctx, routedPath(ctx)), ctx.getUriInfo() == null ? null : ctx.getUriInfo().getQueryParameters(), movedUnderPrefix(ctx), - ctx.getHeaders()); + ctx.getHeaders(), routedLabels(ctx)); } /** @@ -534,7 +449,7 @@ public class IamActionRegistry { String method = ctx.getMethod().toUpperCase(); MultivaluedMap query = ctx.getUriInfo() == null ? null : ctx.getUriInfo().getQueryParameters(); return restRoutes.match(credentialScope, method, withoutInternalPrefix(ctx, routedPath(ctx)), query, - movedUnderPrefix(ctx), ctx.getHeaders()) + movedUnderPrefix(ctx), ctx.getHeaders(), routedLabels(ctx)) .filter(match -> action.equals(IamAuthorizedActions.actionFor(credentialScope, match.operation(), method, match.model(), query))) .map(match -> credentialScope + ":" + match.operation()) @@ -564,7 +479,7 @@ public class IamActionRegistry { String path = routedPath(ctx); return IamRestRoutes.vendored().ambiguous(credentialScope, ctx.getMethod().toUpperCase(), withoutInternalPrefix(ctx, path), ctx.getUriInfo().getQueryParameters(), movedUnderPrefix(ctx), - ctx.getHeaders()); + ctx.getHeaders(), routedLabels(ctx)); } /** The route a request of the operation matches, {@link RouteMembers#NONE} for one it matches none of. */ @@ -576,7 +491,7 @@ public class IamActionRegistry { } return IamRestRoutes.vendored().match(operation.substring(0, separator), ctx.getMethod().toUpperCase(), withoutInternalPrefix(ctx, routedPath(ctx)), ctx.getUriInfo().getQueryParameters(), - movedUnderPrefix(ctx), ctx.getHeaders()) + movedUnderPrefix(ctx), ctx.getHeaders(), routedLabels(ctx)) .filter(match -> match.operation().equals(operation.substring(separator + 1))) .map(match -> new RouteMembers(match.located(), match.model(), IamRestRoutes.vendored().apiVersion(match.model()))) @@ -662,6 +577,20 @@ public class IamActionRegistry { return SignedServiceRoute.internalPrefix(ctx).isPresent(); } + /** + * The values the router gave the path parameters of the method it dispatched the request to, which tell apart the + * vendored routes the request matches ({@link IamRestRoutes#match}); none for a request it has not dispatched. + */ + private static List routedLabels(ContainerRequestContext ctx) { + UriInfo uriInfo = ctx.getUriInfo(); + MultivaluedMap parameters = uriInfo == null ? null : uriInfo.getPathParameters(); + List values = new ArrayList<>(); + if (parameters != null) { + parameters.values().forEach(values::addAll); + } + return values; + } + /** The path without its trailing slashes, the root's own kept. */ static String withoutTrailingSlash(String path) { String trimmed = path; diff --git a/src/main/java/io/floci/aws/services/iam/IamRestRoutes.java b/src/main/java/io/floci/aws/services/iam/IamRestRoutes.java index dbbade3fa7..61cee42709 100644 --- a/src/main/java/io/floci/aws/services/iam/IamRestRoutes.java +++ b/src/main/java/io/floci/aws/services/iam/IamRestRoutes.java @@ -11,6 +11,7 @@ import java.net.URLDecoder; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; @@ -240,14 +241,27 @@ final class IamRestRoutes { */ Optional match(String scope, String method, String path, MultivaluedMap query, boolean movedUnderPrefix, MultivaluedMap headers) { + return match(scope, method, path, query, movedUnderPrefix, headers, List.of()); + } + + /** + * As {@link #match(String, String, String, MultivaluedMap, boolean, MultivaluedMap)}, for a request the router + * dispatched giving the path parameters of the method serving it the values {@code routed}: of the routes the + * request matches, one whose labels the path gives those values is the route of the operation the router chose, + * so it is the match when there is one. EKS takes a principal whose IAM path holds an {@code access-policies} + * segment as an access entry, whose DeleteAccessEntry route the DisassociateAccessPolicy one would outrank. + */ + Optional match(String scope, String method, String path, MultivaluedMap query, + boolean movedUnderPrefix, MultivaluedMap headers, Collection routed) { String requestPath = IamActionRegistry.withoutTrailingSlash(path); - Optional match = best(scope, method, requestPath, query, false, headers); - return match.isPresent() || !movedUnderPrefix ? match : best(scope, method, requestPath, query, true, headers); + Optional match = best(scope, method, requestPath, query, false, headers, routed); + return match.isPresent() || !movedUnderPrefix + ? match : best(scope, method, requestPath, query, true, headers, routed); } private Optional best(String scope, String method, String requestPath, MultivaluedMap query, - boolean spanned, MultivaluedMap headers) { - Route best = ranked(scope, method, requestPath, query, spanned, headers).best(); + boolean spanned, MultivaluedMap headers, Collection routed) { + Route best = ranked(scope, method, requestPath, query, spanned, headers, routed).best(); return best == null ? Optional.empty() : Optional.of(new Match(best.operation(), best.labelValues(requestPath, spanned), best.located(), best.documented(), best.model())); @@ -260,10 +274,19 @@ final class IamRestRoutes { */ boolean ambiguous(String scope, String method, String path, MultivaluedMap query, boolean movedUnderPrefix, MultivaluedMap headers) { + return ambiguous(scope, method, path, query, movedUnderPrefix, headers, List.of()); + } + + /** + * As {@link #ambiguous(String, String, String, MultivaluedMap, boolean, MultivaluedMap)}, of the routes whose labels + * the router gave the values {@code routed}, when the request matches one. + */ + boolean ambiguous(String scope, String method, String path, MultivaluedMap query, + boolean movedUnderPrefix, MultivaluedMap headers, Collection routed) { String requestPath = IamActionRegistry.withoutTrailingSlash(path); - Ranked ranked = ranked(scope, method, requestPath, query, false, headers); + Ranked ranked = ranked(scope, method, requestPath, query, false, headers, routed); return ranked.best() == null && movedUnderPrefix - ? ranked(scope, method, requestPath, query, true, headers).tied() : ranked.tied(); + ? ranked(scope, method, requestPath, query, true, headers, routed).tied() : ranked.tied(); } /** The route a request matches that outranks the others it matches, and whether another operation's ties it. */ @@ -271,15 +294,25 @@ final class IamRestRoutes { } private Ranked ranked(String scope, String method, String requestPath, MultivaluedMap query, - boolean spanned, MultivaluedMap headers) { + boolean spanned, MultivaluedMap headers, Collection routed) { List matching = new ArrayList<>(); - Route best = null; for (Route route : routes.getOrDefault(scope, Map.of()).getOrDefault(method, List.of())) { if (route.matches(requestPath, query, spanned) && route.carries(headers)) { matching.add(route); - if (best == null || route.outranks(best)) { - best = route; - } + } + } + List routedValues = routed.stream().sorted().toList(); + List agreeing = routedValues.isEmpty() ? List.of() : matching.stream() + .filter(route -> route.labelValues(requestPath, spanned).values().stream().sorted().toList() + .equals(routedValues)) + .toList(); + if (!agreeing.isEmpty()) { + matching = agreeing; + } + Route best = null; + for (Route route : matching) { + if (best == null || route.outranks(best)) { + best = route; } } Route winner = best; diff --git a/src/main/resources/aws/rest-routes.json b/src/main/resources/aws/rest-routes.json index 005bc9d37a..31ad38cbc3 100644 --- a/src/main/resources/aws/rest-routes.json +++ b/src/main/resources/aws/rest-routes.json @@ -1061,7 +1061,7 @@ "accessDeniedStatus": 403, "operations": { "ActivateCertificateAuthority": {"method": "POST", "requestUri": "/clusters/{name}/certificate-authorities/{certificateAuthorityId}/activate"}, - "AssociateAccessPolicy": {"method": "POST", "requestUri": "/clusters/{name}/access-entries/{principalArn}/access-policies"}, + "AssociateAccessPolicy": {"method": "POST", "requestUri": "/clusters/{name}/access-entries/{principalArn+}/access-policies"}, "AssociateEncryptionConfig": {"method": "POST", "requestUri": "/clusters/{name}/encryption-config/associate"}, "AssociateIdentityProviderConfig": {"method": "POST", "requestUri": "/clusters/{name}/identity-provider-configs/associate"}, "CancelUpdate": {"method": "POST", "requestUri": "/clusters/{name}/updates/{updateId}/cancel-update"}, @@ -1074,7 +1074,7 @@ "CreateFargateProfile": {"method": "POST", "requestUri": "/clusters/{name}/fargate-profiles"}, "CreateNodegroup": {"method": "POST", "requestUri": "/clusters/{name}/node-groups"}, "CreatePodIdentityAssociation": {"method": "POST", "requestUri": "/clusters/{name}/pod-identity-associations"}, - "DeleteAccessEntry": {"method": "DELETE", "requestUri": "/clusters/{name}/access-entries/{principalArn}"}, + "DeleteAccessEntry": {"method": "DELETE", "requestUri": "/clusters/{name}/access-entries/{principalArn+}"}, "DeleteAddon": {"method": "DELETE", "requestUri": "/clusters/{name}/addons/{addonName}"}, "DeleteCapability": {"method": "DELETE", "requestUri": "/clusters/{name}/capabilities/{capabilityName}"}, "DeleteCertificateAuthority": {"method": "DELETE", "requestUri": "/clusters/{name}/certificate-authorities/{certificateAuthorityId}"}, @@ -1084,7 +1084,7 @@ "DeleteNodegroup": {"method": "DELETE", "requestUri": "/clusters/{name}/node-groups/{nodegroupName}"}, "DeletePodIdentityAssociation": {"method": "DELETE", "requestUri": "/clusters/{name}/pod-identity-associations/{associationId}"}, "DeregisterCluster": {"method": "DELETE", "requestUri": "/cluster-registrations/{name}"}, - "DescribeAccessEntry": {"method": "GET", "requestUri": "/clusters/{name}/access-entries/{principalArn}"}, + "DescribeAccessEntry": {"method": "GET", "requestUri": "/clusters/{name}/access-entries/{principalArn+}"}, "DescribeAddon": {"method": "GET", "requestUri": "/clusters/{name}/addons/{addonName}"}, "DescribeAddonConfiguration": {"method": "GET", "requestUri": "/addons/configuration-schemas"}, "DescribeAddonVersions": {"method": "GET", "requestUri": "/addons/supported-versions"}, @@ -1100,12 +1100,12 @@ "DescribeNodegroup": {"method": "GET", "requestUri": "/clusters/{name}/node-groups/{nodegroupName}"}, "DescribePodIdentityAssociation": {"method": "GET", "requestUri": "/clusters/{name}/pod-identity-associations/{associationId}"}, "DescribeUpdate": {"method": "GET", "requestUri": "/clusters/{name}/updates/{updateId}"}, - "DisassociateAccessPolicy": {"method": "DELETE", "requestUri": "/clusters/{name}/access-entries/{principalArn}/access-policies/{policyArn}"}, + "DisassociateAccessPolicy": {"method": "DELETE", "requestUri": "/clusters/{name}/access-entries/{principalArn+}/access-policies/{policyArn+}"}, "DisassociateIdentityProviderConfig": {"method": "POST", "requestUri": "/clusters/{name}/identity-provider-configs/disassociate"}, "ListAccessEntries": {"method": "GET", "requestUri": "/clusters/{name}/access-entries"}, "ListAccessPolicies": {"method": "GET", "requestUri": "/access-policies"}, "ListAddons": {"method": "GET", "requestUri": "/clusters/{name}/addons"}, - "ListAssociatedAccessPolicies": {"method": "GET", "requestUri": "/clusters/{name}/access-entries/{principalArn}/access-policies"}, + "ListAssociatedAccessPolicies": {"method": "GET", "requestUri": "/clusters/{name}/access-entries/{principalArn+}/access-policies"}, "ListCapabilities": {"method": "GET", "requestUri": "/clusters/{name}/capabilities"}, "ListCertificateAuthorities": {"method": "GET", "requestUri": "/clusters/{name}/certificate-authorities"}, "ListClusters": {"method": "GET", "requestUri": "/clusters"}, @@ -1121,7 +1121,7 @@ "StartInsightsRefresh": {"method": "POST", "requestUri": "/clusters/{name}/insights-refresh"}, "TagResource": {"method": "POST", "requestUri": "/tags/{resourceArn}"}, "UntagResource": {"method": "DELETE", "requestUri": "/tags/{resourceArn}"}, - "UpdateAccessEntry": {"method": "POST", "requestUri": "/clusters/{name}/access-entries/{principalArn}"}, + "UpdateAccessEntry": {"method": "POST", "requestUri": "/clusters/{name}/access-entries/{principalArn+}"}, "UpdateAddon": {"method": "POST", "requestUri": "/clusters/{name}/addons/{addonName}/update"}, "UpdateCapability": {"method": "POST", "requestUri": "/clusters/{name}/capabilities/{capabilityName}"}, "UpdateClusterConfig": {"method": "POST", "requestUri": "/clusters/{name}/update-config"}, diff --git a/src/test/java/io/floci/aws/services/iam/IamActionRegistryTest.java b/src/test/java/io/floci/aws/services/iam/IamActionRegistryTest.java index 9fd55785eb..ac09e09eee 100644 --- a/src/test/java/io/floci/aws/services/iam/IamActionRegistryTest.java +++ b/src/test/java/io/floci/aws/services/iam/IamActionRegistryTest.java @@ -671,10 +671,6 @@ class IamActionRegistryTest { {"POST", entry + "/access-policies", "eks:AssociateAccessPolicy"}, {"DELETE", entry + "/access-policies/arn%3Aaws%3Aeks%3A%3Aaws%3Acluster-access-policy%2FAdmin", "eks:DisassociateAccessPolicy"}, - // A principal whose IAM path holds an access-policies segment is still an entry: only a cluster - // access policy ARN after it makes a DELETE a disassociate. - {"DELETE", "/clusters/c/access-entries/arn:aws:iam::000000000000:role/ci/access-policies/runner", - "eks:DeleteAccessEntry"}, {"GET", "/clusters/c/access-entries/arn:aws:iam::000000000000:role/ci/access-policies/runner", "eks:DescribeAccessEntry"}, {"POST", entry, "eks:UpdateAccessEntry"}, @@ -686,6 +682,27 @@ class IamActionRegistryTest { } } + @Test + void aRequestIsTheRouteWhoseLabelsTheRouterGaveTheirValues() { + // A principal whose IAM path holds an access-policies segment is still an entry: EKS's router takes a DELETE + // as a disassociate only when a cluster access policy ARN follows, so the route of the operation it + // dispatched to is DeleteAccessEntry's, whose labels are the values it gave, though DisassociateAccessPolicy's + // outranks it. + String principal = "arn:aws:iam::000000000000:role/ci/access-policies/runner"; + ContainerRequestContext ctx = mockCtx("DELETE", "/clusters/c/access-entries/" + principal, + new MultivaluedHashMap<>(), MediaType.APPLICATION_JSON_TYPE, "{}"); + assertEquals("eks:DisassociateAccessPolicy", registry.resolveRoute("eks", ctx)); + MultivaluedMap routed = new MultivaluedHashMap<>(); + routed.add("name", "c"); + routed.add("principalArn", principal); + when(ctx.getUriInfo().getPathParameters()).thenReturn(routed); + assertEquals("eks:DeleteAccessEntry", registry.resolveRoute("eks", ctx)); + assertEquals(Map.of("name", "c", "principalArn", principal), IamActionRegistry.pathLabels("eks", ctx)); + // Values no route's labels take leave the outranking route the match. + routed.putSingle("principalArn", "arn:aws:iam::000000000000:role/other"); + assertEquals("eks:DisassociateAccessPolicy", registry.resolveRoute("eks", ctx)); + } + @Test void returnsNullForUnknownRestJsonRoute() { ContainerRequestContext ctx = mockCtx( diff --git a/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java index fafafdef8a..f22d39fadb 100644 --- a/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java @@ -455,7 +455,8 @@ class IamRestRouteEnforcementIntegrationTest { void anEksAccessEntrysPolicyRoutesAreCheckedAsTheirOwnActionsOnTheEntry() { // Associating, listing and disassociating an entry's access policies act on the cluster's entries, the parts // of an entry's ARN the request does not give open, so a grant on another cluster's entries does not cover - // them, and one on this cluster's does. + // them, and one on this cluster's does. So does describing the entry with its principal sent unencoded, which + // EKS takes whole. String user = user("route-eks-entry"); String name = "route-entry-" + suffix(); String base = "/clusters/" + name + "/access-entries"; @@ -474,7 +475,7 @@ class IamRestRouteEnforcementIntegrationTest { String admin = "arn:aws:eks::aws:cluster-access-policy/AmazonEKSClusterAdminPolicy"; String associate = "{\"policyArn\":\"" + admin + "\",\"accessScope\":{\"type\":\"cluster\"}}"; String actions = "[\"eks:AssociateAccessPolicy\",\"eks:ListAssociatedAccessPolicies\"," - + "\"eks:DisassociateAccessPolicy\"]"; + + "\"eks:DisassociateAccessPolicy\",\"eks:DescribeAccessEntry\"]"; policy(user, "{\"Effect\":\"Allow\",\"Action\":" + actions + ",\"Resource\":\"" + other + "\"}"); refused(as(user, "eks").urlEncodingEnabled(false).body(associate).post(policies).then(), @@ -482,12 +483,15 @@ class IamRestRouteEnforcementIntegrationTest { refused(as(user, "eks").urlEncodingEnabled(false).get(policies).then(), "eks:ListAssociatedAccessPolicies"); refused(as(user, "eks").urlEncodingEnabled(false).delete(policies + "/" + encoded(admin)).then(), "eks:DisassociateAccessPolicy"); + refused(as(user, "eks").urlEncodingEnabled(false).get(base + "/" + role).then(), "eks:DescribeAccessEntry"); policy(user, "{\"Effect\":\"Allow\",\"Action\":" + actions + ",\"Resource\":\"" + entries + "\"}"); as(user, "eks").urlEncodingEnabled(false).body(associate).post(policies).then().statusCode(200); as(user, "eks").urlEncodingEnabled(false).get(policies).then().statusCode(200) .body("associatedAccessPolicies[0].policyArn", equalTo(admin)); as(user, "eks").urlEncodingEnabled(false).delete(policies + "/" + encoded(admin)).then().statusCode(200); + as(user, "eks").urlEncodingEnabled(false).get(base + "/" + role).then().statusCode(200) + .body("accessEntry.principalArn", equalTo(role)); // A principal whose IAM path holds access-policies is an entry, so deleting it is DeleteAccessEntry. refused(as(user, "eks").urlEncodingEnabled(false) .delete(base + "/arn:aws:iam::" + ACCOUNT + ":role/ci/access-policies/runner").then(), diff --git a/tools/aws/regen_rest_routes.py b/tools/aws/regen_rest_routes.py index d2b68b26be..fac8a1f679 100644 --- a/tools/aws/regen_rest_routes.py +++ b/tools/aws/regen_rest_routes.py @@ -129,9 +129,12 @@ SHARED_ROUTES: dict[str, frozenset[str]] = { # the slashes the ARN holds: a request may send the ARN unencoded and still reach the operation, so its # route has to take that request too or it would go unchecked. API Gateway v2's tag routes are # `/v2/tags/{resourceArn: .+}` in its controller, and OAM's `/tags/{resourceArn:.+}` in its own and in the -# shared tags controller, which takes every `/tags/{arn}` request. +# shared tags controller, which takes every `/tags/{arn}` request. EKS's access entry routes take the principal +# as `{principalArn: .+}`, and a cluster access policy ARN after it with its `/` sent as it is or encoded. ARN_SPANNING_ROUTES: dict[str, frozenset[str]] = { "apigatewayv2": frozenset({"TagResource", "UntagResource", "GetTags"}), + "eks": frozenset({"DescribeAccessEntry", "DeleteAccessEntry", "UpdateAccessEntry", "AssociateAccessPolicy", + "ListAssociatedAccessPolicies", "DisassociateAccessPolicy"}), "oam": frozenset({"TagResource", "UntagResource", "ListTagsForResource"}), } diff --git a/tools/aws/test_regen_rest_routes.py b/tools/aws/test_regen_rest_routes.py index fa18708139..f831447a13 100644 --- a/tools/aws/test_regen_rest_routes.py +++ b/tools/aws/test_regen_rest_routes.py @@ -285,6 +285,18 @@ def test_an_arn_label_the_controller_lets_span_slashes_is_greedy(): doc = r.build({"oam": model("rest-json", "2022-06-10", TagResource=("PUT", "/tags/{ResourceArn}"), GetSink=("POST", "/GetSink"))}, "test") assert doc["models"]["oam"]["operations"]["TagResource"] == {"method": "PUT", "requestUri": "/tags/{ResourceArn+}"} + # EKS's access entry routes take the principal ARN, and a policy ARN after it, whole; the cluster's name stays + # one segment, and a route of EKS's naming no ARN keeps the model's. + doc = r.build({"eks": model("rest-json", "2017-11-01", + DisassociateAccessPolicy=("DELETE", "/clusters/{name}/access-entries/{principalArn}" + "/access-policies/{policyArn}"), + DescribeAccessEntry=("GET", "/clusters/{name}/access-entries/{principalArn}"), + DescribeCluster=("GET", "/clusters/{name}"))}, "test") + operations = doc["models"]["eks"]["operations"] + assert operations["DisassociateAccessPolicy"]["requestUri"] == \ + "/clusters/{name}/access-entries/{principalArn+}/access-policies/{policyArn+}" + assert operations["DescribeAccessEntry"]["requestUri"] == "/clusters/{name}/access-entries/{principalArn+}" + assert operations["DescribeCluster"] == {"method": "GET", "requestUri": "/clusters/{name}"} PAGE = "https://docs.aws.amazon.com/glue/latest/dg/iceberg-rest-apis.html"