mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(iam): check the Lambda MicroVM and network connector routes (#6122)
Under IAM enforcement no Lambda MicroVM or network connector route was mapped to an IAM action, so every call went unchecked, and the roles they take with it. - Every MicroVM route (2025-09-09) and network connector route (2026-04-04) is mapped, and IamRestRouteCoverageTest now checks every operation of both models. An image or connector request is checked against microvm-image:<name> or network-connector:<id> in the request's account and Region, an identifier given as an ARN taken for its name, as the service looks it up. RunMicrovm is checked against the image its imageIdentifier names, an action on a running MicroVM against the image it runs, and a managed image's versions against that image, which aws owns. Creating an image or a connector and the list operations name no resource, so they are checked against *. - Lambda's own tag routes, /2017-03-31/tags/<arn>, which MicroVM images share with functions, are mapped and checked against the resource they tag: the caller's own function, image or connector of the ARN's Region and name, whatever account the ARN names, as Lambda looks it up, a target without the arn: prefix read as the MicroVM tag handler reads it, and an event source mapping's ARN as written. - iam:PassRole, to lambda.amazonaws.com as the Service Reference gives: CreateMicrovmImage and UpdateMicrovmImage (buildRoleArn), RunMicrovm (executionRoleArn), and CreateNetworkConnector and UpdateNetworkConnector (OperatorRole). This finishes the Lambda half of the follow-up on #5938.
This commit is contained in:
1 parent
4eea01741f
commit
29384867cf
12 files changed
+474
-22
No files matched your search
@@ -148,6 +148,9 @@ 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 "lambda:CreateMicrovmImage", "lambda:UpdateMicrovmImage" -> fromJson(ctx, LAMBDA, "buildRoleArn");
|
||||
case "lambda:RunMicrovm" -> fromJson(ctx, LAMBDA, "executionRoleArn");
|
||||
case "lambda:CreateNetworkConnector", "lambda:UpdateNetworkConnector" -> fromJson(ctx, LAMBDA, "OperatorRole");
|
||||
case "cloudformation:CreateStack", "cloudformation:UpdateStack", "cloudformation:DeleteStack",
|
||||
"cloudformation:CreateChangeSet", "cloudformation:ContinueUpdateRollback" ->
|
||||
fromForm(ctx, CLOUDFORMATION, "RoleARN");
|
||||
|
||||
@@ -116,6 +116,40 @@ public class IamActionRegistry {
|
||||
// Invoke and GetLayerVersion (iam-authorized-actions.json).
|
||||
rule("lambda", "POST", ".*/functions/[^/]+/response-streaming-invocations$", "lambda:InvokeFunction"),
|
||||
rule("lambda", "GET", "^/2018-10-31/layers$", "find", "LayerVersion", "lambda:GetLayerVersion"),
|
||||
// Lambda MicroVMs (2025-09-09) and their tags, and network connectors (2026-04-04).
|
||||
rule("lambda", "POST", "^/2025-09-09/microvms/[^/]+/auth-token$", "lambda:CreateMicrovmAuthToken"),
|
||||
rule("lambda", "POST", "^/2025-09-09/microvms/[^/]+/shell-auth-token$", "lambda:CreateMicrovmShellAuthToken"),
|
||||
rule("lambda", "POST", "^/2025-09-09/microvms/[^/]+/resume$", "lambda:ResumeMicrovm"),
|
||||
rule("lambda", "POST", "^/2025-09-09/microvms/[^/]+/suspend$", "lambda:SuspendMicrovm"),
|
||||
rule("lambda", "GET", "^/2025-09-09/microvms/[^/]+$", "lambda:GetMicrovm"),
|
||||
rule("lambda", "DELETE", "^/2025-09-09/microvms/[^/]+$", "lambda:TerminateMicrovm"),
|
||||
rule("lambda", "GET", "^/2025-09-09/microvms$", "lambda:ListMicrovms"),
|
||||
rule("lambda", "POST", "^/2025-09-09/microvms$", "lambda:RunMicrovm"),
|
||||
rule("lambda", "GET", "^/2025-09-09/microvm-images/[^/]+/versions/[^/]+/builds/[^/]+$",
|
||||
"lambda:GetMicrovmImageBuild"),
|
||||
rule("lambda", "GET", "^/2025-09-09/microvm-images/[^/]+/versions/[^/]+/builds$",
|
||||
"lambda:ListMicrovmImageBuilds"),
|
||||
rule("lambda", "GET", "^/2025-09-09/microvm-images/[^/]+/versions/[^/]+$", "lambda:GetMicrovmImageVersion"),
|
||||
rule("lambda", "PATCH", "^/2025-09-09/microvm-images/[^/]+/versions/[^/]+$", "lambda:UpdateMicrovmImageVersion"),
|
||||
rule("lambda", "DELETE", "^/2025-09-09/microvm-images/[^/]+/versions/[^/]+$", "lambda:DeleteMicrovmImageVersion"),
|
||||
rule("lambda", "GET", "^/2025-09-09/microvm-images/[^/]+/versions$", "lambda:ListMicrovmImageVersions"),
|
||||
rule("lambda", "GET", "^/2025-09-09/microvm-images/[^/]+$", "lambda:GetMicrovmImage"),
|
||||
rule("lambda", "PUT", "^/2025-09-09/microvm-images/[^/]+$", "lambda:UpdateMicrovmImage"),
|
||||
rule("lambda", "DELETE", "^/2025-09-09/microvm-images/[^/]+$", "lambda:DeleteMicrovmImage"),
|
||||
rule("lambda", "GET", "^/2025-09-09/microvm-images$", "lambda:ListMicrovmImages"),
|
||||
rule("lambda", "POST", "^/2025-09-09/microvm-images$", "lambda:CreateMicrovmImage"),
|
||||
rule("lambda", "GET", "^/2025-09-09/managed-microvm-images/[^/]+/versions$",
|
||||
"lambda:ListManagedMicrovmImageVersions"),
|
||||
rule("lambda", "GET", "^/2025-09-09/managed-microvm-images$", "lambda:ListManagedMicrovmImages"),
|
||||
rule("lambda", "GET", "^/2017-03-31/tags/.+", "lambda:ListTags"),
|
||||
rule("lambda", "POST", "^/2017-03-31/tags/.+", "lambda:TagResource"),
|
||||
rule("lambda", "DELETE", "^/2017-03-31/tags/.+", "lambda:UntagResource"),
|
||||
rule("lambda", "GET", "^/2026-04-04/network-connectors/[^/]+/versions$", "lambda:ListNetworkConnectorVersions"),
|
||||
rule("lambda", "GET", "^/2026-04-04/network-connectors/[^/]+$", "lambda:GetNetworkConnector"),
|
||||
rule("lambda", "PUT", "^/2026-04-04/network-connectors/[^/]+$", "lambda:UpdateNetworkConnector"),
|
||||
rule("lambda", "DELETE", "^/2026-04-04/network-connectors/[^/]+$", "lambda:DeleteNetworkConnector"),
|
||||
rule("lambda", "GET", "^/2026-04-04/network-connectors$", "lambda:ListNetworkConnectors"),
|
||||
rule("lambda", "POST", "^/2026-04-04/network-connectors$", "lambda:CreateNetworkConnector"),
|
||||
|
||||
// ── DynamoDB (JSON 1.1, action from X-Amz-Target handled separately) ──
|
||||
// Handled via Query-style action extraction in the filter
|
||||
|
||||
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.core.common.SigV4CredentialScope;
|
||||
import io.github.hectorvent.floci.services.cloudwatch.logs.LogGroupTagTarget;
|
||||
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;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
@@ -65,13 +66,22 @@ public class ResourceArnBuilder {
|
||||
private final Instance<IamService> iamService;
|
||||
/** Looked up lazily for the same reason, to resolve a secret named by its name or a partial ARN. */
|
||||
private final Instance<SecretsManagerService> secretsManagerService;
|
||||
/** Looked up lazily for the same reason, to read the image a MicroVM runs. */
|
||||
private final Instance<LambdaMicrovmsService> microvmsService;
|
||||
|
||||
@Inject
|
||||
public ResourceArnBuilder(ObjectMapper objectMapper, Instance<IamService> iamService,
|
||||
Instance<SecretsManagerService> secretsManagerService) {
|
||||
Instance<SecretsManagerService> secretsManagerService,
|
||||
Instance<LambdaMicrovmsService> microvmsService) {
|
||||
this.objectMapper = objectMapper;
|
||||
this.iamService = iamService;
|
||||
this.secretsManagerService = secretsManagerService;
|
||||
this.microvmsService = microvmsService;
|
||||
}
|
||||
|
||||
public ResourceArnBuilder(ObjectMapper objectMapper, Instance<IamService> iamService,
|
||||
Instance<SecretsManagerService> secretsManagerService) {
|
||||
this(objectMapper, iamService, secretsManagerService, null);
|
||||
}
|
||||
|
||||
ResourceArnBuilder(ObjectMapper objectMapper, Instance<IamService> iamService) {
|
||||
@@ -679,6 +689,14 @@ public class ResourceArnBuilder {
|
||||
if (webFunctionArn != null) {
|
||||
return webFunctionArn;
|
||||
}
|
||||
String microvmArn = microvmArn(path, ctx, region, accountId);
|
||||
if (microvmArn != null) {
|
||||
return microvmArn;
|
||||
}
|
||||
// TagResource, ListTags and UntagResource name the tagged function, image or other resource by its ARN.
|
||||
if (path.startsWith("/2017-03-31/tags/")) {
|
||||
return lambdaTaggedArn(path.substring("/2017-03-31/tags/".length()), accountId);
|
||||
}
|
||||
String executionArn = durableExecutionArn(path);
|
||||
if (executionArn == null) {
|
||||
executionArn = durableCallbackExecutionArn(path);
|
||||
@@ -715,6 +733,103 @@ public class ResourceArnBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The resource of a Lambda MicroVM or network connector route, as the Service Reference gives it:
|
||||
* {@code microvm-image:<name>} for an action on an image, its versions and builds, and
|
||||
* {@code network-connector:<id>} for one on a connector. An identifier given as an ARN is taken for
|
||||
* its trailing name, in the request's account and Region, since that is the resource the service
|
||||
* looks up and acts on, whatever account or Region the ARN names. {@code RunMicrovm} is checked
|
||||
* against the image its {@code imageIdentifier} names, read the same way, an action on a running
|
||||
* MicroVM against the image that MicroVM runs, and a managed image's versions against the managed
|
||||
* image. Creating an image or a connector, listing them and listing MicroVMs name no resource, so
|
||||
* they are {@code *}, as is a MicroVM the account does not hold. Null for every other Lambda route.
|
||||
*/
|
||||
private String microvmArn(String path, ContainerRequestContext ctx, String region, String accountId) {
|
||||
if (path.startsWith("/2025-09-09/")) {
|
||||
String image = segmentAfterPrefix(path, "/2025-09-09/microvm-images/");
|
||||
if (image != null) {
|
||||
return microvmImageArn(region, accountId, image);
|
||||
}
|
||||
String managed = segmentAfterPrefix(path, "/2025-09-09/managed-microvm-images/");
|
||||
if (managed != null) {
|
||||
return LambdaMicrovmsService.namesManagedImage(managed)
|
||||
? LambdaMicrovmsService.managedImageArn(region) : "*";
|
||||
}
|
||||
String microvm = segmentAfterPrefix(path, "/2025-09-09/microvms/");
|
||||
if (microvm != null) {
|
||||
return runningMicrovmImageArn(region, microvm);
|
||||
}
|
||||
if (path.equals("/2025-09-09/microvms") && "POST".equalsIgnoreCase(ctx.getMethod())) {
|
||||
JsonNode body = readJsonBody(ctx);
|
||||
String identifier = body == null ? null : body.path("imageIdentifier").asText(null);
|
||||
return identifier == null || identifier.isBlank() ? "*" : microvmImageArn(region, accountId, identifier);
|
||||
}
|
||||
return "*";
|
||||
}
|
||||
if (path.startsWith("/2026-04-04/")) {
|
||||
String connector = segmentAfterPrefix(path, "/2026-04-04/network-connectors/");
|
||||
return connector == null ? "*"
|
||||
: AwsArnUtils.Arn.of("lambda", region, accountId,
|
||||
"network-connector:" + trailingName(connector)).toString();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String microvmImageArn(String region, String accountId, String identifier) {
|
||||
return AwsArnUtils.Arn.of("lambda", region, accountId, "microvm-image:" + trailingName(identifier)).toString();
|
||||
}
|
||||
|
||||
/** The image the caller's MicroVM runs, or {@code *} when the account holds no such MicroVM to act on. */
|
||||
private String runningMicrovmImageArn(String region, String microvmId) {
|
||||
if (microvmsService == null) {
|
||||
return "*";
|
||||
}
|
||||
return microvmsService.get().findMicrovm(region, microvmId)
|
||||
.map(vm -> vm.imageArn)
|
||||
.filter(arn -> arn != null && !arn.isBlank())
|
||||
.orElse("*");
|
||||
}
|
||||
|
||||
/** An identifier's name: an ARN's part after its last {@code :}, as the MicroVM service unwraps it. */
|
||||
private static String trailingName(String identifier) {
|
||||
return identifier.startsWith("arn:") ? identifier.substring(identifier.lastIndexOf(':') + 1) : identifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* The resource a Lambda tag route acts on, from the ARN it names. Tagging looks a function up by the
|
||||
* ARN's Region and name, and a MicroVM image or network connector by the ARN's Region and trailing
|
||||
* name, in the caller's own account whatever account the ARN names, so that resource is the one
|
||||
* checked. The MicroVM tag handler takes any target holding {@code :microvm-image:} or
|
||||
* {@code :network-connector:}, an {@code arn:} prefix or not, so the target is read as it reads it.
|
||||
* Any other ARN, an event source mapping's included, has to name its resource exactly or is
|
||||
* refused, so it is checked as written.
|
||||
*/
|
||||
private static String lambdaTaggedArn(String tagged, String accountId) {
|
||||
if (LambdaMicrovmsService.ownsArn(tagged)) {
|
||||
// Read as LambdaTagController and LambdaMicrovmsService read it: the Region is the fourth field,
|
||||
// the name what follows the last colon.
|
||||
String[] fields = tagged.split(":");
|
||||
String region = fields.length > 3 ? fields[3] : "";
|
||||
String type = tagged.contains(":microvm-image:") ? "microvm-image:"
|
||||
: tagged.contains(":network-connector:") ? "network-connector:" : null;
|
||||
if (type != null && !region.isBlank()) {
|
||||
return AwsArnUtils.Arn.of("lambda", region, accountId,
|
||||
type + tagged.substring(tagged.lastIndexOf(':') + 1)).toString();
|
||||
}
|
||||
}
|
||||
if (!tagged.startsWith("arn:")) {
|
||||
return "*";
|
||||
}
|
||||
try {
|
||||
LambdaArnUtils.ResolvedFunctionRef ref = LambdaArnUtils.resolve(tagged);
|
||||
return ref.qualifier() != null ? tagged
|
||||
: AwsArnUtils.Arn.of("lambda", ref.region(), accountId, "function:" + ref.name()).toString();
|
||||
} catch (AwsException e) {
|
||||
// Not a function ARN Lambda takes, which it tags only when the ARN names the resource exactly.
|
||||
return tagged;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The resource of an EventBridge Scheduler route: {@code schedule/<group>/<name>}, the group read where
|
||||
* Scheduler reads it, the body's {@code GroupName} on a create or an update and {@code groupName}
|
||||
|
||||
+22
-8
@@ -17,6 +17,7 @@ import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Consumer;
|
||||
@@ -46,6 +47,8 @@ public class LambdaMicrovmsService {
|
||||
|
||||
private static final Pattern NAME_PATTERN = Pattern.compile("[a-zA-Z0-9-_]{1,64}");
|
||||
private static final int MAX_CONNECTOR_SUBNETS = 16;
|
||||
/** The one AWS-managed base image the catalog serves. */
|
||||
private static final String MANAGED_IMAGE = "al2023-1";
|
||||
|
||||
private final RegionResolver regionResolver;
|
||||
private final StorageFactory storageFactory;
|
||||
@@ -293,16 +296,26 @@ public class LambdaMicrovmsService {
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> listManagedImageVersions(String region, String identifier) {
|
||||
if (!identifier.contains("al2023-1")) {
|
||||
if (!namesManagedImage(identifier)) {
|
||||
throw new AwsException("ResourceNotFoundException",
|
||||
"Managed MicroVM image not found: " + identifier, 404);
|
||||
}
|
||||
return List.of(managedImage(region));
|
||||
}
|
||||
|
||||
/** True when {@code identifier} names the managed image, as {@link #listManagedImageVersions} reads it. */
|
||||
public static boolean namesManagedImage(String identifier) {
|
||||
return identifier.contains(MANAGED_IMAGE);
|
||||
}
|
||||
|
||||
/** The managed image's ARN in {@code region}, owned by {@code aws} rather than the caller's account. */
|
||||
public static String managedImageArn(String region) {
|
||||
return AwsArnUtils.Arn.of("lambda", region, "aws", "microvm-image:" + MANAGED_IMAGE).toString();
|
||||
}
|
||||
|
||||
private Map<String, Object> managedImage(String region) {
|
||||
Map<String, Object> entry = new LinkedHashMap<>();
|
||||
entry.put("imageArn", AwsArnUtils.Arn.of("lambda", region, "aws", "microvm-image:al2023-1").toString());
|
||||
entry.put("imageArn", managedImageArn(region));
|
||||
entry.put("createdAt", 1781833144.754d);
|
||||
entry.put("updatedAt", 1784165932.388d);
|
||||
return entry;
|
||||
@@ -351,12 +364,13 @@ public class LambdaMicrovmsService {
|
||||
}
|
||||
|
||||
public Microvm getMicrovm(String region, String microvmId) {
|
||||
Microvm vm = microvms.get(key(region, unwrap(microvmId)));
|
||||
if (vm == null) {
|
||||
throw new AwsException("ResourceNotFoundException",
|
||||
"MicroVM not found: " + microvmId, 404);
|
||||
}
|
||||
return vm;
|
||||
return findMicrovm(region, microvmId).orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"MicroVM not found: " + microvmId, 404));
|
||||
}
|
||||
|
||||
/** The MicroVM of the caller's account that {@code microvmId} names in {@code region}, looked up as every route does. */
|
||||
public Optional<Microvm> findMicrovm(String region, String microvmId) {
|
||||
return Optional.ofNullable(microvms.get(key(region, unwrap(microvmId))));
|
||||
}
|
||||
|
||||
public List<Microvm> listMicrovms(String region) {
|
||||
|
||||
Reference in new issue
Block a user