mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(iam): check EKS's REST routes and roles, and routes with a trailing slash (#6323)
Under IAM enforcement a REST request whose route no rule maps is let through unchecked. No EKS
route had a rule, so any signed caller could create, read, change or delete clusters, node groups,
Fargate profiles, add-ons, access entries and Pod Identity associations with no EKS permission, and
without iam:PassRole on the roles they named. A Pod Identity association hands its service
account's pods the credentials of its role, so a caller could have pods run as any role in the
account.
Every operation of the eks model now has a rule and is checked as the action of its own name, as
the Service Authorization Reference gives it, including the routes Floci does not serve yet, and
IamRestRouteCoverageTest checks the whole model. Floci takes an access entry's principal as the
rest of the path, so a GET or DELETE under an entry reads or deletes an entry: the entry's access
policy routes, which Floci does not serve, run as DescribeAccessEntry and DeleteAccessEntry and are
checked as those.
Each is checked against the resource the reference gives it, in the caller's account, where EKS
looks everything up:
- a cluster against the ARN EKS holds it under, or cluster/<name> in the request's Region for one
it holds none of, and CreateCluster against the cluster its body names, as the EKS user guide's
example policy for creating a cluster grants it;
- a node group, Fargate profile, add-on, access entry or Pod Identity association against the ARN
EKS holds it under, which ends in an id the request does not carry, read through
EksService.findResourceArn with the lookups its handlers make; listing and creating them, and the
other routes under a cluster, against the cluster;
- DescribeUpdate against the add-on the update was made to, since the updates Floci keeps are its
add-ons' and EKS returns one whether or not the request names its add-on, and otherwise the
cluster;
- a tagging request against the caller's cluster named after the ARN's last /, as EKS tags it.
The roles each operation passes need iam:PassRole, with iam:PassedToService the value the Service
Reference gives: the cluster role, node role, Fargate pod execution role and an add-on's service
account and Pod Identity roles go to eks.amazonaws.com, a Pod Identity association's roleArn to
pods.eks.amazonaws.com ("The IAM principal that is creating the association must have
iam:PassRole."), and an EC2_LINUX or EC2_WINDOWS access entry's principal to ec2.amazonaws.com.
The router also matches a path with a trailing slash as the path without it, while the rules
matched the slash: POST /clusters/, GET /schedules/ and GET /2015-03-31/functions/<name>/ reached
their operation unchecked. A route is now matched without its trailing slash, as the router
matches it. An S3 path is matched as sent, as before: an S3 key may end in a slash, as a folder
marker's does, and S3's rules already take a bucket's one trailing slash.
docs/services/iam.md says which EKS routes are mapped, what each is checked against and the roles
they pass.
This commit is contained in:
1 parent
16c159e5b2
commit
ec5597eb5c
14 files changed
+814
-18
No files matched your search
@@ -53,6 +53,17 @@ import java.util.regex.Pattern;
|
||||
* you must grant your user permission to pass the role to Amazon ECS"), task roles to
|
||||
* {@code ecs-tasks.amazonaws.com}.
|
||||
*
|
||||
* <p>An EKS request passes the cluster's {@code roleArn}, a node group's {@code nodeRole}, a Fargate
|
||||
* profile's {@code podExecutionRoleArn}, and an add-on's service account role and the role of each Pod
|
||||
* Identity association it makes, to {@code eks.amazonaws.com}. A Pod Identity association passes its
|
||||
* {@code roleArn} to {@code pods.eks.amazonaws.com}: the EKS user guide has "The IAM principal that is
|
||||
* creating the association must have iam:PassRole." Its target role is not passed but assumed with the
|
||||
* association's role: "Pod Identity then automatically performs two role assumptions in sequence: first
|
||||
* assuming the EKS Pod Identity role, then using those credentials to assume the Target IAM Role." An
|
||||
* access entry passes its principal to {@code ec2.amazonaws.com} for the node types alone: "If you create
|
||||
* an access entry with type EC2_LINUX or EC2_WINDOWS, the IAM principal creating the access entry must
|
||||
* have the iam:PassRole permission."
|
||||
*
|
||||
* <p>A CloudFormation stack's service role is checked where a request names it, and not on later
|
||||
* operations that reuse it: CloudFormation "uses this role even if the users don't have permission
|
||||
* to pass it". A self-managed StackSet that names no administration role passes the default one:
|
||||
@@ -89,6 +100,8 @@ final class PassedRoles {
|
||||
private static final String EVENTS = ServicePrincipals.of("events");
|
||||
private static final String SCHEDULER = ServicePrincipals.of("scheduler");
|
||||
private static final String PIPES = ServicePrincipals.of("pipes");
|
||||
private static final String EKS = ServicePrincipals.of("eks");
|
||||
private static final String EKS_PODS = ServicePrincipals.of("pods.eks");
|
||||
private static final String CLOUDFORMATION = ServicePrincipals.of("cloudformation");
|
||||
private static final String AUTOSCALING = ServicePrincipals.of("autoscaling");
|
||||
private static final String APPLICATION_AUTOSCALING = ServicePrincipals.of("application-autoscaling");
|
||||
@@ -169,6 +182,13 @@ final class PassedRoles {
|
||||
case "events:PutTargets" -> fromJson(ctx, EVENTS, "Targets", "[]", "RoleArn");
|
||||
case "scheduler:CreateSchedule", "scheduler:UpdateSchedule" -> fromJson(ctx, SCHEDULER, "Target", "RoleArn");
|
||||
case "pipes:CreatePipe", "pipes:UpdatePipe" -> fromJson(ctx, PIPES, "RoleArn");
|
||||
case "eks:CreateCluster" -> fromJson(ctx, EKS, "roleArn");
|
||||
case "eks:CreateNodegroup" -> fromJson(ctx, EKS, "nodeRole");
|
||||
case "eks:CreateFargateProfile" -> fromJson(ctx, EKS, "podExecutionRoleArn");
|
||||
case "eks:CreateAddon", "eks:UpdateAddon" -> eksAddonRoles(jsonBody(ctx));
|
||||
case "eks:CreatePodIdentityAssociation", "eks:UpdatePodIdentityAssociation" ->
|
||||
fromJson(ctx, EKS_PODS, "roleArn");
|
||||
case "eks:CreateAccessEntry" -> eksNodeAccessEntry(jsonBody(ctx));
|
||||
case "lambda:CreateMicrovmImage", "lambda:UpdateMicrovmImage" -> fromJson(ctx, LAMBDA, "buildRoleArn");
|
||||
case "lambda:RunMicrovm" -> fromJson(ctx, LAMBDA, "executionRoleArn");
|
||||
case "lambda:CreateNetworkConnector", "lambda:UpdateNetworkConnector" -> fromJson(ctx, LAMBDA, "OperatorRole");
|
||||
@@ -256,6 +276,20 @@ final class PassedRoles {
|
||||
return passed(arns, service);
|
||||
}
|
||||
|
||||
/** An EKS add-on's service account role and the role of each Pod Identity association it makes. */
|
||||
private static List<PassedRole> eksAddonRoles(JsonNode body) {
|
||||
List<String> roles = new ArrayList<>(values(body, "serviceAccountRoleArn"));
|
||||
roles.addAll(values(body, "podIdentityAssociations", "[]", "roleArn"));
|
||||
return passed(roles, EKS);
|
||||
}
|
||||
|
||||
/** An EKS access entry's principal, when its type is one whose nodes run as that role. */
|
||||
private static List<PassedRole> eksNodeAccessEntry(JsonNode body) {
|
||||
List<String> type = values(body, "type");
|
||||
return type.contains("EC2_LINUX") || type.contains("EC2_WINDOWS")
|
||||
? passed(values(body, "principalArn"), EC2) : List.of();
|
||||
}
|
||||
|
||||
/**
|
||||
* The administration role a StackSet operation passes: the one it names, or else, unless the
|
||||
* StackSet uses service-managed permissions, the default one. An update goes by the StackSet's
|
||||
|
||||
@@ -343,13 +343,7 @@ public class EksAddonService {
|
||||
}
|
||||
|
||||
public Update describeUpdate(Cluster cluster, String updateId, String addonName) {
|
||||
requireActiveCluster(cluster);
|
||||
if (updateId == null || updateId.isBlank()) {
|
||||
throw new AwsException("InvalidParameterException", "updateId is required", 400);
|
||||
}
|
||||
StoredUpdate stored = updatesStorage.get(prefix(cluster) + updateId)
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"No update: " + updateId + " found for cluster: " + cluster.getName(), 404));
|
||||
StoredUpdate stored = storedUpdate(cluster, updateId);
|
||||
if (addonName != null && !addonName.isBlank()
|
||||
&& stored.addonName() != null && !stored.addonName().equalsIgnoreCase(addonName.trim())) {
|
||||
throw new AwsException("ResourceNotFoundException",
|
||||
@@ -358,6 +352,24 @@ public class EksAddonService {
|
||||
return stored.update();
|
||||
}
|
||||
|
||||
/**
|
||||
* The name of the add-on the update {@code updateId} was made to, looked up as {@link #describeUpdate} looks
|
||||
* it up, which returns the update whether or not the request names its add-on.
|
||||
*/
|
||||
public String updateAddonName(Cluster cluster, String updateId) {
|
||||
return storedUpdate(cluster, updateId).addonName();
|
||||
}
|
||||
|
||||
private StoredUpdate storedUpdate(Cluster cluster, String updateId) {
|
||||
requireActiveCluster(cluster);
|
||||
if (updateId == null || updateId.isBlank()) {
|
||||
throw new AwsException("InvalidParameterException", "updateId is required", 400);
|
||||
}
|
||||
return updatesStorage.get(prefix(cluster) + updateId)
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"No update: " + updateId + " found for cluster: " + cluster.getName(), 404));
|
||||
}
|
||||
|
||||
public AddonVersionsPage describeAddonVersions(String addonName, String kubernetesVersion,
|
||||
Integer maxResults, String nextToken,
|
||||
List<String> publishers, List<String> types, List<String> owners) {
|
||||
|
||||
@@ -742,6 +742,37 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
? aware.getForAccount(accountId, name) : storage.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* The ARN of what an EKS route names in the caller's account, looked up as its handler looks it up: the
|
||||
* 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.
|
||||
*/
|
||||
public Optional<String> findResourceArn(String clusterName, String collection, String name) {
|
||||
Optional<Cluster> cluster = storage.get(clusterName);
|
||||
if (cluster.isEmpty() || collection == null) {
|
||||
return cluster.map(Cluster::getArn);
|
||||
}
|
||||
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 "access-entries" -> accessEntries.describe(cluster.get(), name).accessEntryArn();
|
||||
case "pod-identity-associations" -> podIdentityAssociations.describe(cluster.get(), name).associationArn();
|
||||
case "updates" -> {
|
||||
String addon = addons.updateAddonName(cluster.get(), name);
|
||||
yield addon == null ? null : addons.describe(cluster.get(), addon).addonArn();
|
||||
}
|
||||
default -> null;
|
||||
});
|
||||
} catch (AwsException expected) {
|
||||
// The handler answers this lookup with the same error, so the route names no resource to act on.
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public Cluster describeCluster(String name) {
|
||||
Cluster cluster = storage.get(name)
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
|
||||
@@ -287,6 +287,88 @@ public class IamActionRegistry {
|
||||
rule("pipes", "POST", "^/tags(/.+)?$", "pipes:TagResource"),
|
||||
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.
|
||||
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"),
|
||||
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"),
|
||||
|
||||
// ── SES v2 ─────────────────────────────────────────────────────────────
|
||||
// SES v1 is a Query API and resolves from its Action like any other; v2 is REST-JSON under
|
||||
// /v2/email, so every operation needs its own rule. One rule per operation in the SES v2
|
||||
@@ -494,7 +576,9 @@ public class IamActionRegistry {
|
||||
*/
|
||||
public String resolveRoute(String credentialScope, ContainerRequestContext ctx) {
|
||||
String method = ctx.getMethod().toUpperCase();
|
||||
String path = rawPath(ctx.getUriInfo());
|
||||
// An S3 key may end in a slash, PUT /bucket// writes the key "/", and S3's rules already take a
|
||||
// bucket's one trailing slash, so an S3 path is matched as sent.
|
||||
String path = "s3".equals(credentialScope) ? rawPath(ctx.getUriInfo()) : routedPath(ctx.getUriInfo());
|
||||
|
||||
// S3 sub-resource override: the URL path alone doesn't distinguish
|
||||
// s3:GetObjectAcl / s3:PutObjectAcl from s3:GetObject / s3:PutObject
|
||||
@@ -531,6 +615,19 @@ public class IamActionRegistry {
|
||||
return path.startsWith("/") ? path : "/" + path;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link #rawPath} without a trailing slash: the router matches {@code /clusters/} as {@code /clusters},
|
||||
* so a rule ending at the route's last segment has to match it too, or the request would pass as an
|
||||
* unknown action.
|
||||
*/
|
||||
static String routedPath(UriInfo uriInfo) {
|
||||
String path = rawPath(uriInfo);
|
||||
while (path.length() > 1 && path.endsWith("/")) {
|
||||
path = path.substring(0, path.length() - 1);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Query action from GET query parameters or the POST form body, preserving the
|
||||
* entity stream. Both use the controller's Action-first, Operation-second rule; a POST URL
|
||||
|
||||
@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.core.common.AwsRegions;
|
||||
import io.github.hectorvent.floci.core.common.ServicePrincipals;
|
||||
import io.github.hectorvent.floci.core.common.SigV4CredentialScope;
|
||||
import io.github.hectorvent.floci.services.cloudwatch.logs.LogGroupTagTarget;
|
||||
import io.github.hectorvent.floci.services.eks.EksService;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaArnUtils;
|
||||
import io.github.hectorvent.floci.services.lambda.durable.DurableTokens;
|
||||
import io.github.hectorvent.floci.services.lambdamicrovms.LambdaMicrovmsService;
|
||||
@@ -67,15 +68,25 @@ public class ResourceArnBuilder {
|
||||
private final Instance<SecretsManagerService> secretsManagerService;
|
||||
/** Looked up lazily for the same reason, to read the image a MicroVM runs. */
|
||||
private final Instance<LambdaMicrovmsService> microvmsService;
|
||||
/** Looked up lazily for the same reason, to read the ARN an EKS cluster or what it holds is stored under. */
|
||||
private final Instance<EksService> eksService;
|
||||
|
||||
@Inject
|
||||
public ResourceArnBuilder(ObjectMapper objectMapper, Instance<IamService> iamService,
|
||||
Instance<SecretsManagerService> secretsManagerService,
|
||||
Instance<LambdaMicrovmsService> microvmsService) {
|
||||
Instance<LambdaMicrovmsService> microvmsService,
|
||||
Instance<EksService> eksService) {
|
||||
this.objectMapper = objectMapper;
|
||||
this.iamService = iamService;
|
||||
this.secretsManagerService = secretsManagerService;
|
||||
this.microvmsService = microvmsService;
|
||||
this.eksService = eksService;
|
||||
}
|
||||
|
||||
public ResourceArnBuilder(ObjectMapper objectMapper, Instance<IamService> iamService,
|
||||
Instance<SecretsManagerService> secretsManagerService,
|
||||
Instance<LambdaMicrovmsService> microvmsService) {
|
||||
this(objectMapper, iamService, secretsManagerService, microvmsService, null);
|
||||
}
|
||||
|
||||
public ResourceArnBuilder(ObjectMapper objectMapper, Instance<IamService> iamService,
|
||||
@@ -131,6 +142,7 @@ public class ResourceArnBuilder {
|
||||
case "scheduler" -> List.of(buildSchedulerArn(path, ctx, region, accountId, partition));
|
||||
case "pipes" -> List.of(buildPipesArn(path, ctx, region, accountId, partition));
|
||||
case "dsql" -> List.of(buildDsqlArn(path, ctx, region, accountId, partition));
|
||||
case "eks" -> List.of(buildEksArn(path, ctx, region, accountId));
|
||||
default -> List.of("*");
|
||||
};
|
||||
}
|
||||
@@ -1018,6 +1030,88 @@ public class ResourceArnBuilder {
|
||||
return taggedResource(ctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* The resource of an EKS route, as the Service Reference gives it, in the caller's account, where EKS
|
||||
* looks everything up. A cluster is the ARN EKS holds it under, or for one it holds none of, the ARN a
|
||||
* create mints in the request's Region; {@code CreateCluster} is checked against the cluster its body
|
||||
* names, as the EKS user guide's example policy for creating a cluster grants it. A node group, Fargate
|
||||
* profile, add-on, access entry or Pod Identity association is the ARN EKS holds it under, which ends
|
||||
* in an id the request does not carry, or {@code *} when EKS holds none to act on; listing or creating
|
||||
* them, and every other route under a cluster, act on the cluster. The path is split as the router
|
||||
* splits it, before decoding, so an encoded {@code /} stays inside a name, and an access entry's
|
||||
* principal is the rest of the path, as EKS takes it. {@code DescribeUpdate} acts on the add-on its
|
||||
* {@code addonName} names, since the updates EKS keeps are its add-ons', and otherwise on the cluster. A
|
||||
* tagging route acts on the caller's cluster named by what follows the ARN's last {@code /}, as EKS tags
|
||||
* it, whatever the ARN names before that. Deregistering a cluster acts on that cluster, and an EKS
|
||||
* Anywhere subscription route on the subscription its id names. Listing clusters, registering one, the
|
||||
* account-wide reads, and an identity provider config or capability, which end in an id Floci does not
|
||||
* keep, name no resource, so they are {@code *}.
|
||||
*/
|
||||
private String buildEksArn(String path, ContainerRequestContext ctx, String region, String accountId) {
|
||||
if (path.equals("/tags") || path.startsWith("/tags/")) {
|
||||
String tagged = taggedArn(ctx);
|
||||
int slash = tagged == null ? -1 : tagged.lastIndexOf('/');
|
||||
return slash < 0 || slash == tagged.length() - 1 ? "*"
|
||||
: eksClusterArn(tagged.substring(slash + 1), region, accountId);
|
||||
}
|
||||
String[] segments = IamActionRegistry.routedPath(ctx.getUriInfo()).substring(1).split("/", -1);
|
||||
if (segments.length == 1) {
|
||||
JsonNode body = "clusters".equals(segments[0]) && "POST".equalsIgnoreCase(ctx.getMethod())
|
||||
? readJsonBody(ctx) : null;
|
||||
String cluster = body == null ? null : body.path("name").asText(null);
|
||||
return cluster == null || cluster.isBlank() ? "*" : eksClusterArn(cluster, region, accountId);
|
||||
}
|
||||
String name = pathSegment(segments[1]);
|
||||
if (name.isEmpty()) {
|
||||
return "*";
|
||||
}
|
||||
return switch (segments[0]) {
|
||||
case "clusters" -> eksClusterRouteArn(name, segments, region, accountId);
|
||||
case "cluster-registrations" -> eksClusterArn(name, region, accountId);
|
||||
case "eks-anywhere-subscriptions" ->
|
||||
AwsArnUtils.Arn.of("eks", region, accountId, "eks-anywhere-subscription/" + name).toString();
|
||||
default -> "*";
|
||||
};
|
||||
}
|
||||
|
||||
/** 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) {
|
||||
if (segments.length < 4 || segments[3].isEmpty()) {
|
||||
return eksClusterArn(cluster, region, accountId);
|
||||
}
|
||||
String collection = segments[2];
|
||||
return switch (collection) {
|
||||
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))));
|
||||
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.
|
||||
Optional<String> addon = segments.length == 4 && eksService != null
|
||||
? eksService.get().findResourceArn(cluster, collection, pathSegment(segments[3]))
|
||||
: Optional.empty();
|
||||
yield addon.orElseGet(() -> eksClusterArn(cluster, region, accountId));
|
||||
}
|
||||
case "identity-provider-configs" ->
|
||||
"associate".equals(segments[3]) ? eksClusterArn(cluster, region, accountId) : "*";
|
||||
case "capabilities" -> "*";
|
||||
default -> eksClusterArn(cluster, region, accountId);
|
||||
};
|
||||
}
|
||||
|
||||
/** 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()
|
||||
: eksService.get().findResourceArn(name, null, null);
|
||||
return held.orElseGet(() -> AwsArnUtils.Arn.of("eks", region, accountId, "cluster/" + name).toString());
|
||||
}
|
||||
|
||||
/** The ARN EKS holds {@code name} of {@code collection} in the cluster under, or {@code *} when it holds none. */
|
||||
private String eksHeldArn(String cluster, String collection, String name) {
|
||||
return eksService == null ? "*" : eksService.get().findResourceArn(cluster, collection, name).orElse("*");
|
||||
}
|
||||
|
||||
/** The ARN a shared tagging route names, or {@code *} for any other route. */
|
||||
private String taggedResource(ContainerRequestContext ctx) {
|
||||
String arn = taggedArn(ctx);
|
||||
|
||||
Reference in new issue
Block a user