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:
Priamo Germosen authored and GitHub committed 2026-10-21 18:37:24 -05:00
1 parent 4eea01741f
commit 29384867cf
12 files changed
+474 -22

No files matched your search

@@ -176,7 +176,7 @@ class PassedRolesTest {
}
@Test
void schedulerAndPipesPassTheRoleTheyName() {
void schedulerPipesAndLambdaMicrovmsPassTheRoleTheyName() {
String target = "{\"Target\":{\"RoleArn\":\"" + ROLES + "schedule\"}}";
assertEquals(List.of(passed("schedule", "scheduler.amazonaws.com")),
PassedRoles.of("scheduler:CreateSchedule", json(target), LOOKUPS));
@@ -185,6 +185,18 @@ class PassedRolesTest {
String pipe = "{\"RoleArn\":\"" + ROLES + "pipe\"}";
assertEquals(List.of(passed("pipe", "pipes.amazonaws.com")), PassedRoles.of("pipes:CreatePipe", json(pipe), LOOKUPS));
assertEquals(List.of(passed("pipe", "pipes.amazonaws.com")), PassedRoles.of("pipes:UpdatePipe", json(pipe), LOOKUPS));
String build = "{\"buildRoleArn\":\"" + ROLES + "build\"}";
assertEquals(List.of(passed("build", "lambda.amazonaws.com")),
PassedRoles.of("lambda:CreateMicrovmImage", json(build), LOOKUPS));
assertEquals(List.of(passed("build", "lambda.amazonaws.com")),
PassedRoles.of("lambda:UpdateMicrovmImage", json(build), LOOKUPS));
assertEquals(List.of(passed("vm", "lambda.amazonaws.com")), PassedRoles.of("lambda:RunMicrovm",
json("{\"executionRoleArn\":\"" + ROLES + "vm\"}"), LOOKUPS));
String operator = "{\"OperatorRole\":\"" + ROLES + "operator\"}";
assertEquals(List.of(passed("operator", "lambda.amazonaws.com")),
PassedRoles.of("lambda:CreateNetworkConnector", json(operator), LOOKUPS));
assertEquals(List.of(passed("operator", "lambda.amazonaws.com")),
PassedRoles.of("lambda:UpdateNetworkConnector", json(operator), LOOKUPS));
}
@Test
@@ -47,6 +47,7 @@ class IamPassRoleEnforcementIntegrationTest {
private static final String ECS_TASKS = "ecs-tasks.amazonaws.com";
private static final String ECS = "ecs.amazonaws.com";
private static final String QUEUE_ARN = "arn:aws:sqs:us-east-1:" + ACCOUNT + ":passrole-queue";
private static final String LAMBDA = "lambda.amazonaws.com";
private static final String SCHEDULER = "scheduler.amazonaws.com";
private static final String PIPES = "pipes.amazonaws.com";
private static final String AUTOSCALING = "autoscaling.amazonaws.com";
@@ -447,6 +448,42 @@ class IamPassRoleEnforcementIntegrationTest {
user.rest("pipes").body(update).put(path).then().statusCode(200);
}
@Test
void aMicrovmImageAndANetworkConnectorNeedPassRoleOnTheRolesTheyName() {
String build = role("microvm-build");
String operator = role("connector-operator");
User user = user("passrole-microvm");
String image = "passrole-image-" + suffix();
String createImage = """
{"name":"%s","baseImageArn":"arn:aws:lambda:us-east-1:aws:microvm-image:al2023-1",
"buildRoleArn":"%s","codeArtifact":{"uri":"s3://bucket/code.zip"}}""".formatted(image, build);
user.policy(allow("lambda:CreateMicrovmImage", "*"));
denied(user.rest("lambda").body(createImage).post("/2025-09-09/microvm-images").then());
user.policy(allow("lambda:CreateMicrovmImage", "*") + "," + passRole(build, LAMBDA));
user.rest("lambda").body(createImage).post("/2025-09-09/microvm-images").then().statusCode(201);
cleanup.register(() -> admin("lambda").delete("/2025-09-09/microvm-images/" + image));
// Reading it is checked against the image's ARN.
String imageArn = "arn:aws:lambda:us-east-1:" + ACCOUNT + ":microvm-image:" + image;
user.policy(allow("lambda:GetMicrovmImage", imageArn + "-other"));
refused(user.rest("lambda").get("/2025-09-09/microvm-images/" + image).then(), "lambda:GetMicrovmImage");
user.policy(allow("lambda:GetMicrovmImage", imageArn));
user.rest("lambda").get("/2025-09-09/microvm-images/" + image).then().statusCode(200);
String createConnector = """
{"Name":"passrole-connector-%1$s","ClientToken":"passrole-token-%1$s","OperatorRole":"%2$s",
"Configuration":{"VpcEgressConfiguration":{"AssociatedComputeResourceTypes":["MicroVm"],
"NetworkProtocol":"IPv4","SubnetIds":["subnet-0000000000000it01"],
"SecurityGroupIds":["sg-0000000000000it01"]}}}"""
.formatted(suffix(), operator);
user.policy(allow("lambda:CreateNetworkConnector", "*"));
denied(user.rest("lambda").body(createConnector).post("/2026-04-04/network-connectors").then());
user.policy(allow("lambda:CreateNetworkConnector", "*") + "," + passRole(operator, LAMBDA));
String connector = user.rest("lambda").body(createConnector).post("/2026-04-04/network-connectors").then()
.statusCode(202).extract().path("Id");
cleanup.register(() -> admin("lambda").delete("/2026-04-04/network-connectors/" + connector));
}
@Test
void aStackNeedsPassRoleOnAServiceRoleTheRequestNames() {
String role = role("cloudformation");
@@ -52,7 +52,7 @@ class IamRestRouteCoverageTest {
"DeleteBucketMetadataConfiguration", "s3:DeleteBucket");
/** Models every operation of which is checked: the services whose routes the registry maps in full. */
private static final Set<String> FULLY_ROUTED_MODELS = Set.of("scheduler", "pipes");
private static final Set<String> FULLY_ROUTED_MODELS = Set.of("scheduler", "pipes", "lambda-microvms", "lambda-core");
private final IamActionRegistry registry = new IamActionRegistry();
@@ -126,6 +126,92 @@ class IamRestRouteEnforcementIntegrationTest {
return "{\"resourceArn\":\"" + arn + "\",\"tags\":[{\"key\":\"team\",\"value\":\"a\"}]}";
}
@Test
void lambdaTaggingIsCheckedAgainstTheTaggedFunction() {
String user = user("route-lambda-tags");
String function = "arn:aws:lambda:us-east-1:" + ACCOUNT + ":function:route-fn-" + suffix();
String path = "/2017-03-31/tags/" + function;
String body = "{\"Tags\":{\"team\":\"a\"}}";
policy(user, allow("lambda:TagResource", function + "-other"));
refused(as(user, "lambda").body(body).post(path).then(), "lambda:TagResource");
// A grant on the function itself covers it; the request reaches Lambda, which has no such function.
policy(user, allow("lambda:TagResource", function));
as(user, "lambda").body(body).post(path).then().statusCode(not(403));
}
@Test
void lambdaTaggingIsCheckedAgainstTheCallersOwnResourceWhateverAccountTheArnNames() {
// Lambda tags the caller's own function or MicroVM image of the ARN's name, so a grant on another
// account's ARN of that name does not cover it, and a grant on the caller's own does.
String user = user("route-lambda-tags-account");
String name = "route-tagged-" + suffix();
String body = "{\"Tags\":{\"team\":\"a\"}}";
for (String type : new String[] {"function:", "microvm-image:"}) {
String own = "arn:aws:lambda:us-east-1:" + ACCOUNT + ":" + type + name;
String foreign = "arn:aws:lambda:us-east-1:111122223333:" + type + name;
policy(user, allow("lambda:TagResource", foreign));
refused(as(user, "lambda").body(body).post("/2017-03-31/tags/" + foreign).then(), "lambda:TagResource");
policy(user, allow("lambda:TagResource", own));
as(user, "lambda").body(body).post("/2017-03-31/tags/" + foreign).then().statusCode(not(403));
}
}
@Test
void aMicrovmIsCheckedAgainstTheImageItRuns() {
// RunMicrovm and the actions on a running MicroVM act on its image, so a Deny on the image applies to
// them even where the action is allowed on every resource, and a grant on the image alone covers them.
String user = user("route-microvm-image");
String image = microvmImage("route-image-" + suffix());
String run = "{\"imageIdentifier\":\"" + image.substring(image.lastIndexOf(':') + 1) + "\"}";
policy(user, allow("lambda:RunMicrovm", "*") + "," + deny("lambda:RunMicrovm", image));
refused(as(user, "lambda").body(run).post("/2025-09-09/microvms").then(), "lambda:RunMicrovm");
policy(user, allow("lambda:RunMicrovm", image));
String microvm = as(user, "lambda").body(run).post("/2025-09-09/microvms").then().statusCode(200)
.extract().path("microvmId");
cleanup.register(() -> admin().delete("/2025-09-09/microvms/" + microvm));
policy(user, allow("lambda:GetMicrovm", "*") + "," + deny("lambda:GetMicrovm", image));
refused(as(user, "lambda").get("/2025-09-09/microvms/" + microvm).then(), "lambda:GetMicrovm");
policy(user, allow("lambda:GetMicrovm", image));
as(user, "lambda").get("/2025-09-09/microvms/" + microvm).then().statusCode(200);
}
@Test
void aMicrovmImageTaggedThroughATargetWithoutTheArnPrefixIsCheckedAsTheImage() {
// Lambda tags the image any target holding :microvm-image: names, read by its fourth field and
// its last, so a Deny on the image applies however the target is spelled.
String user = user("route-image-tags");
String image = microvmImage("route-tagged-image-" + suffix());
String target = "/2017-03-31/tags/x:y:z:us-east-1:microvm-image:" + image.substring(image.lastIndexOf(':') + 1);
String body = "{\"Tags\":{\"team\":\"a\"}}";
policy(user, allow("lambda:TagResource", "*") + "," + deny("lambda:TagResource", image));
refused(as(user, "lambda").body(body).post(target).then(), "lambda:TagResource");
policy(user, allow("lambda:TagResource", "*"));
as(user, "lambda").body(body).post(target).then().statusCode(204);
}
/** Creates a MicroVM image as the account's admin, removed afterwards, and returns its ARN. */
private String microvmImage(String name) {
admin().body("""
{"name":"%s","baseImageArn":"arn:aws:lambda:us-east-1:aws:microvm-image:al2023-1",
"buildRoleArn":"arn:aws:iam::%s:role/route-build","codeArtifact":{"uri":"s3://bucket/code.zip"}}"""
.formatted(name, ACCOUNT)).post("/2025-09-09/microvm-images").then().statusCode(201);
cleanup.register(() -> admin().delete("/2025-09-09/microvm-images/" + name));
return "arn:aws:lambda:us-east-1:" + ACCOUNT + ":microvm-image:" + name;
}
private static RequestSpecification admin() {
return given().header("Authorization", auth(ADMIN, "lambda")).contentType("application/json");
}
private static String deny(String action, String resource) {
return "{\"Effect\":\"Deny\",\"Action\":\"" + action + "\",\"Resource\":\"" + resource + "\"}";
}
private static void refused(ValidatableResponse response, String action) {
response.statusCode(403).body(containsString(action)).body(not(containsString("iam:PassRole")));
}
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.services.iam.model.IamRole;
import io.github.hectorvent.floci.services.iam.model.IamUser;
import io.github.hectorvent.floci.services.iam.model.InstanceProfile;
import io.github.hectorvent.floci.services.iam.model.ServerCertificate;
import io.github.hectorvent.floci.services.lambdamicrovms.LambdaMicrovmsService;
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
import jakarta.enterprise.inject.Instance;
import jakarta.ws.rs.container.ContainerRequestContext;
@@ -110,6 +111,14 @@ class ResourceArnBuilderTest {
return new ResourceArnBuilder(new ObjectMapper(), instance);
}
/** A builder reading {@code microvms}, for an action on a running MicroVM. */
private static ResourceArnBuilder backedBy(LambdaMicrovmsService microvms) {
@SuppressWarnings("unchecked")
Instance<LambdaMicrovmsService> instance = mock(Instance.class);
when(instance.get()).thenReturn(microvms);
return new ResourceArnBuilder(new ObjectMapper(), null, null, instance);
}
/** A builder reading {@code secrets}, for a secret named by its name. */
private static ResourceArnBuilder backedBy(SecretsManagerService secrets) {
@SuppressWarnings("unchecked")
@@ -1207,6 +1216,66 @@ class ResourceArnBuilderTest {
assertEquals("*", builder.build("lambda", ctx, "us-east-1", "000000000000"));
}
@Test
void lambdaMicrovmAndNetworkConnectorRoutesResolveTheImageOrConnectorTheyName() {
String image = "arn:aws:lambda:us-east-1:000000000000:microvm-image:img";
when(uriInfo.getPath()).thenReturn("/2025-09-09/microvm-images/img");
assertEquals(image, builder.build("lambda", ctx, "us-east-1", "000000000000"));
when(uriInfo.getPath()).thenReturn("/2025-09-09/microvm-images/img/versions/1/builds/b-1");
assertEquals(image, builder.build("lambda", ctx, "us-east-1", "000000000000"));
// An image named by its ARN is looked up by its name in the request's account and Region, so that
// is the resource checked, whatever account or Region the ARN names.
when(uriInfo.getPath()).thenReturn(
"/2025-09-09/microvm-images/arn:aws:lambda:eu-west-1:111122223333:microvm-image:shared/versions");
assertEquals("arn:aws:lambda:us-east-1:000000000000:microvm-image:shared",
builder.build("lambda", ctx, "us-east-1", "000000000000"));
when(uriInfo.getPath()).thenReturn("/2026-04-04/network-connectors/nc-1/versions");
assertEquals("arn:aws:lambda:us-east-1:000000000000:network-connector:nc-1",
builder.build("lambda", ctx, "us-east-1", "000000000000"));
// RunMicrovm runs the image its imageIdentifier names, read as the service reads it.
when(ctx.getMethod()).thenReturn("POST");
when(uriInfo.getPath()).thenReturn("/2025-09-09/microvms");
setJsonBody("{\"imageIdentifier\":\"img\"}");
assertEquals(image, builder.build("lambda", ctx, "us-east-1", "000000000000"));
setJsonBody("{\"imageIdentifier\":\"arn:aws:lambda:eu-west-1:111122223333:microvm-image:img\"}");
assertEquals(image, builder.build("lambda", ctx, "us-east-1", "000000000000"));
setJsonBody("{}");
assertEquals("*", builder.build("lambda", ctx, "us-east-1", "000000000000"));
// A managed image's versions are checked against the managed image, which aws owns.
when(ctx.getMethod()).thenReturn("GET");
when(uriInfo.getPath()).thenReturn("/2025-09-09/managed-microvm-images/al2023-1/versions");
assertEquals("arn:aws:lambda:us-east-1:aws:microvm-image:al2023-1",
builder.build("lambda", ctx, "us-east-1", "000000000000"));
// Creating or listing either, listing MicroVMs, a managed image there is none of, and a MicroVM the
// account does not hold name no resource here.
for (String path : new String[] {"/2025-09-09/microvm-images", "/2025-09-09/managed-microvm-images",
"/2025-09-09/managed-microvm-images/other/versions", "/2025-09-09/microvms",
"/2025-09-09/microvms/vm-1", "/2026-04-04/network-connectors"}) {
when(uriInfo.getPath()).thenReturn(path);
assertEquals("*", builder.build("lambda", ctx, "us-east-1", "000000000000"), path);
}
}
@Test
void anActionOnARunningMicrovmIsCheckedAgainstTheImageItRuns() {
String image = "arn:aws:lambda:us-east-1:000000000000:microvm-image:img";
LambdaMicrovmsService.Microvm vm = new LambdaMicrovmsService.Microvm();
vm.microvmId = "microvm-1";
vm.imageArn = image;
LambdaMicrovmsService microvms = mock(LambdaMicrovmsService.class);
when(microvms.findMicrovm(any(), any())).thenReturn(Optional.empty());
when(microvms.findMicrovm("us-east-1", "microvm-1")).thenReturn(Optional.of(vm));
ResourceArnBuilder backed = backedBy(microvms);
for (String path : new String[] {"/2025-09-09/microvms/microvm-1",
"/2025-09-09/microvms/microvm-1/suspend", "/2025-09-09/microvms/microvm-1/auth-token"}) {
when(uriInfo.getPath()).thenReturn(path);
assertEquals(image, backed.build("lambda", ctx, "us-east-1", "000000000000"), path);
}
when(uriInfo.getPath()).thenReturn("/2025-09-09/microvms/microvm-2");
assertEquals("*", backed.build("lambda", ctx, "us-east-1", "000000000000"));
}
@Test
void schedulerRoutesResolveTheScheduleInItsGroupTheGroupOrTheTaggedArn() {
when(uriInfo.getPath()).thenReturn("/schedules/nightly");
@@ -1295,7 +1364,35 @@ class ResourceArnBuilderTest {
}
@Test
void aStreamingInvokeResolvesItsQualifiedFunctionAsAnInvokeDoes() {
void lambdaTagRoutesAndAStreamingInvokeResolveTheResourceTheyName() {
// Tagging acts on the caller's own function, image or connector of the ARN's Region and name,
// whatever account the ARN names, so that is the resource checked.
when(uriInfo.getPath()).thenReturn("/2017-03-31/tags/arn:aws:lambda:eu-west-1:111122223333:function:shared");
assertEquals("arn:aws:lambda:eu-west-1:000000000000:function:shared",
builder.build("lambda", ctx, "us-east-1", "000000000000"));
when(uriInfo.getPath()).thenReturn(
"/2017-03-31/tags/arn:aws:lambda:eu-west-1:111122223333:microvm-image:img");
assertEquals("arn:aws:lambda:eu-west-1:000000000000:microvm-image:img",
builder.build("lambda", ctx, "us-east-1", "000000000000"));
when(uriInfo.getPath()).thenReturn(
"/2017-03-31/tags/arn:aws:lambda:eu-west-1:111122223333:network-connector:nc-1");
assertEquals("arn:aws:lambda:eu-west-1:000000000000:network-connector:nc-1",
builder.build("lambda", ctx, "us-east-1", "000000000000"));
// A mapping's ARN has to name it exactly, and Lambda refuses a qualified function, a running MicroVM
// and an image in no Region, so these stay as written.
String mapping = "arn:aws:lambda:us-east-1:111122223333:event-source-mapping:"
+ "14e0db71-5d35-4eb5-b481-8945cf9d10c2";
for (String arn : new String[] {mapping, "arn:aws:lambda:us-east-1:111122223333:function:shared:prod",
"arn:aws:lambda:us-east-1:111122223333:microvm:vm-1", "arn:aws:lambda::111122223333:microvm-image:img"}) {
when(uriInfo.getPath()).thenReturn("/2017-03-31/tags/" + arn);
assertEquals(arn, builder.build("lambda", ctx, "us-east-1", "000000000000"), arn);
}
// The MicroVM tag handler takes a target without the arn: prefix too, and reads it the same way.
when(uriInfo.getPath()).thenReturn("/2017-03-31/tags/x:y:z:eu-west-1:microvm-image:img");
assertEquals("arn:aws:lambda:eu-west-1:000000000000:microvm-image:img",
builder.build("lambda", ctx, "us-east-1", "000000000000"));
when(uriInfo.getPath()).thenReturn("/2017-03-31/tags/shared");
assertEquals("*", builder.build("lambda", ctx, "us-east-1", "000000000000"));
// A streaming invoke is an invoke: its qualifier and the account its function ARN names count.
when(uriInfo.getPath()).thenReturn("/2021-11-15/functions/my-function/response-streaming-invocations");
queryParams.putSingle("Qualifier", "prod");