fix(lambda): include Lambda MicroVM resources (#7352)

This commit is contained in:
KrishmalX authored and GitHub committed 2026-11-05 04:57:31 -04:00
1 parent a15dfb621e
commit 50aef92124
8 files changed
+221 -45

No files matched your search

+5
View File
@@ -115,6 +115,11 @@ map as empty.
| SSM | `ssm:parameter` |
| Step Functions | `states:statemachine` |
Lambda MicroVM images (`lambda:microvm-image`) and network connectors
(`lambda:network-connector`) are discoverable through `Search`, `ListResources` and the Resource
Groups Tagging API, but `ListSupportedResourceTypes` does not list them, because AWS does not
advertise them.
API Gateway ARNs carry no account; its resources report the calling account as their owner.
Route 53 is not discoverable yet: `Route53Service` resolves no account id, and a hosted zone's ARN
has neither a Region nor an account. It is waiting on a field, not on new query support.
+1 -1
View File
@@ -90,7 +90,7 @@ for SQS or `ValidationException` for CloudWatch Logs.
The resource type in `service:resourceType` matches the ARN's type, which is
the ARN resource part with one leading `/` dropped, cut at the first `/` or
`:`, or the type the owning service declares for the resource. So
`lambda:function`, `logs:log-group`, `ec2:instance`, `apigateway:apikeys` and
`lambda:function`, `lambda:microvm-image`, `lambda:network-connector`, `logs:log-group`, `ec2:instance`, `apigateway:apikeys` and
`apigateway:restapis/stages` all match. SQS queue ARNs have no type segment, so
`sqs:queue` matches through the type SQS declares for its queues. As in AWS,
`apigateway:restapis` lists REST APIs together with their stages, and a filter
@@ -2,9 +2,8 @@ package io.github.hectorvent.floci.services.lambda;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.lambdamicrovms.LambdaMicrovmsService;
import io.github.hectorvent.floci.core.common.TaggingApiOnly;
import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
@@ -26,47 +25,30 @@ import java.util.Map;
* TagResource: POST /2017-03-31/tags/{ARN}
* ListTags: GET /2017-03-31/tags/{ARN}
* UntagResource: DELETE /2017-03-31/tags/{ARN}?tagKeys=...
*
* MicroVM-family ARNs share this route with classic Lambda resources; {@link LambdaTagHandler}
* dispatches between them.
*/
@Path("/2017-03-31")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class LambdaTagController {
private final LambdaService lambdaService;
private final LambdaMicrovmsService microvmsService;
private final LambdaTagHandler tagHandler;
private final ObjectMapper objectMapper;
@Inject
public LambdaTagController(LambdaService lambdaService,
LambdaMicrovmsService microvmsService,
ObjectMapper objectMapper) {
this.lambdaService = lambdaService;
this.microvmsService = microvmsService;
public LambdaTagController(@TaggingApiOnly LambdaTagHandler tagHandler, ObjectMapper objectMapper) {
this.tagHandler = tagHandler;
this.objectMapper = objectMapper;
}
/**
* MicroVM-family ARNs (microvm-image, microvm, network-connector) share
* the /2017-03-31/tags route with classic Lambda resources; dispatch on
* the ARN's resource type. The region comes from the ARN itself.
*/
private static String arnRegion(String arn) {
return AwsArnUtils.parse(arn).region();
}
@GET
@Path("/tags/{arn}")
public Response listTags(@PathParam("arn") String arn) {
if (LambdaMicrovmsService.ownsArn(arn)) {
ObjectNode mvRoot = objectMapper.createObjectNode();
ObjectNode mvTags = mvRoot.putObject("Tags");
microvmsService.listTags(arnRegion(arn), arn).forEach(mvTags::put);
return Response.ok(mvRoot).build();
}
Map<String, String> tags = lambdaService.listTags(arn);
ObjectNode root = objectMapper.createObjectNode();
ObjectNode tagsNode = root.putObject("Tags");
tags.forEach(tagsNode::put);
tagHandler.listTags(null, arn).forEach(tagsNode::put);
return Response.ok(root).build();
}
@@ -80,11 +62,7 @@ public class LambdaTagController {
if (tags == null) {
throw new AwsException("InvalidParameterValueException", "Tags is required", 400);
}
if (LambdaMicrovmsService.ownsArn(arn)) {
microvmsService.tagResource(arnRegion(arn), arn, tags);
} else {
lambdaService.tagResource(arn, tags);
}
tagHandler.tagResource(null, arn, tags);
return Response.noContent().build();
} catch (AwsException e) {
throw e;
@@ -97,11 +75,7 @@ public class LambdaTagController {
@Path("/tags/{arn}")
public Response untagResource(@PathParam("arn") String arn,
@QueryParam("tagKeys") List<String> tagKeys) {
if (LambdaMicrovmsService.ownsArn(arn)) {
microvmsService.untagResource(arnRegion(arn), arn, tagKeys);
} else {
lambdaService.untagResource(arn, tagKeys);
}
tagHandler.untagResource(null, arn, tagKeys);
return Response.noContent().build();
}
}
@@ -1,7 +1,9 @@
package io.github.hectorvent.floci.services.lambda;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.TagHandler;
import io.github.hectorvent.floci.core.common.TaggingApiOnly;
import io.github.hectorvent.floci.services.lambdamicrovms.LambdaMicrovmsService;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@@ -9,7 +11,12 @@ import java.util.List;
import java.util.Map;
/**
* Lets Resource Groups Tagging API writes reach a function's own tags.
* Lets Resource Groups Tagging API writes reach a function's own tags, and those of a MicroVM
* image or network connector.
*
* <p>Also the one place that routes MicroVM-family ARNs apart from classic Lambda resources:
* {@link LambdaTagController} calls it too. The {@code region} argument is unused, because a
* MicroVM ARN carries its own.
*
* <p>A separate {@link TaggingApiOnly} bean because Lambda tags on its own
* {@code /2017-03-31/tags/{arn}} path: a default-qualified handler would also serve the
@@ -20,10 +27,12 @@ import java.util.Map;
public class LambdaTagHandler implements TagHandler {
private final LambdaService lambdaService;
private final LambdaMicrovmsService microvmsService;
@Inject
public LambdaTagHandler(LambdaService lambdaService) {
public LambdaTagHandler(LambdaService lambdaService, LambdaMicrovmsService microvmsService) {
this.lambdaService = lambdaService;
this.microvmsService = microvmsService;
}
@Override
@@ -33,16 +42,31 @@ public class LambdaTagHandler implements TagHandler {
@Override
public Map<String, String> listTags(String region, String arn) {
if (LambdaMicrovmsService.ownsArn(arn)) {
return microvmsService.listTags(arnRegion(arn), arn);
}
return lambdaService.listTags(arn);
}
@Override
public void tagResource(String region, String arn, Map<String, String> tags) {
if (LambdaMicrovmsService.ownsArn(arn)) {
microvmsService.tagResource(arnRegion(arn), arn, tags);
return;
}
lambdaService.tagResource(arn, tags);
}
@Override
public void untagResource(String region, String arn, List<String> tagKeys) {
if (LambdaMicrovmsService.ownsArn(arn)) {
microvmsService.untagResource(arnRegion(arn), arn, tagKeys);
return;
}
lambdaService.untagResource(arn, tagKeys);
}
private static String arnRegion(String arn) {
return AwsArnUtils.parse(arn).region();
}
}
@@ -394,7 +394,7 @@ public class LambdaMicrovmsController {
node.putNull("tags");
} else if (shape == ImageShape.GET) {
ObjectNode tags = node.putObject("tags");
image.tags.forEach(tags::put);
service.snapshotTags(image.tags).forEach(tags::put);
}
return node;
}
@@ -4,6 +4,9 @@ import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.resource.ExplorerResource;
import io.github.hectorvent.floci.core.resource.ResourceProvider;
import io.github.hectorvent.floci.core.resource.SupportedResourceType;
import io.github.hectorvent.floci.core.storage.StorageBackedMap;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.lambda.LambdaArnUtils;
@@ -46,8 +49,10 @@ import java.util.stream.Stream;
* observes the resting state.</p>
*/
@ApplicationScoped
public class LambdaMicrovmsService {
public class LambdaMicrovmsService implements ResourceProvider {
private static final String IMAGE_TYPE = "lambda:microvm-image";
private static final String CONNECTOR_TYPE = "lambda:network-connector";
private static final Pattern NAME_PATTERN = Pattern.compile("[a-zA-Z0-9-_]{1,64}");
private static final int MAX_CONNECTOR_SUBNETS = 16;
private static final Pattern CONNECTOR_VERSION = Pattern.compile("[1-9][0-9]{0,17}");
@@ -60,6 +65,8 @@ public class LambdaMicrovmsService {
private final RegionResolver regionResolver;
private final StorageFactory storageFactory;
/** Guards every read and write of an image's or connector's tag map. */
private final Object tagLock = new Object();
/**
* Keyed {@code region/id}. The account is not part of the key here: the backend prefixes every
@@ -710,6 +717,44 @@ public class LambdaMicrovmsService {
// ------------------------------------------------------------------ tags
@Override
public List<ExplorerResource> getResources() {
List<ExplorerResource> resources = new ArrayList<>();
for (MicrovmImage image : images.values()) {
addResource(resources, image.imageArn, IMAGE_TYPE, image.updatedAt, image.tags);
}
for (NetworkConnector connector : connectors.values()) {
addResource(resources, connector.arn, CONNECTOR_TYPE, connector.lastModified, connector.tags);
}
return resources;
}
private void addResource(List<ExplorerResource> resources, String arn, String type,
Instant lastModified, Map<String, String> tags) {
if (arn == null) {
return;
}
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
resources.add(new ExplorerResource(arn, type, "lambda", parsed.region(), parsed.accountId(),
lastModified != null ? lastModified : Instant.now(), snapshotTags(tags)));
}
/** A copy taken under the tag lock, so a concurrent tag change cannot break the iteration. */
Map<String, String> snapshotTags(Map<String, String> tags) {
synchronized (tagLock) {
return new LinkedHashMap<>(tags);
}
}
/**
* Empty on purpose: AWS ListSupportedResourceTypes advertises neither type, so the images and
* connectors stay discoverable through {@link #getResources()} without widening that list.
*/
@Override
public Set<SupportedResourceType> getSupportedResourceTypes() {
return Set.of();
}
/** True when the ARN names a MicroVM-family resource this service owns. */
public static boolean ownsArn(String arn) {
String resource = AwsArnUtils.resourceIfArnFor(arn, "lambda").orElse("");
@@ -745,7 +790,7 @@ public class LambdaMicrovmsService {
}
public Map<String, String> listTags(String region, String arn) {
return taggable(region, arn);
return snapshotTags(taggable(region, arn));
}
public void tagResource(String region, String arn, Map<String, String> tags) {
@@ -768,13 +813,17 @@ public class LambdaMicrovmsService {
String resource = tagTargetResource(arn);
if (resource.startsWith(IMAGE_RESOURCE)) {
MicrovmImage image = getImage(region, resource.substring(IMAGE_RESOURCE.length()));
mutation.accept(image.tags);
synchronized (tagLock) {
mutation.accept(image.tags);
}
persist(images, region, image.name, image);
return;
}
if (resource.startsWith(CONNECTOR_RESOURCE)) {
NetworkConnector connector = getConnector(region, resource.substring(CONNECTOR_RESOURCE.length()));
mutation.accept(connector.tags);
synchronized (tagLock) {
mutation.accept(connector.tags);
}
persist(connectors, region, connector.id, connector);
return;
}
@@ -2,10 +2,12 @@ package io.github.hectorvent.floci.services.lambdamicrovms;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.resource.ExplorerResource;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@@ -63,6 +65,32 @@ class LambdaMicrovmsServiceTest {
assertEquals(updates + 1, service.listConnectorVersions(REGION, created.id).size());
}
@Test
void listsTaggedImagesAndConnectorsAsResources() {
RegionResolver regionResolver = mock(RegionResolver.class);
when(regionResolver.getAccountId()).thenReturn(ACCOUNT_ID);
LambdaMicrovmsService service = new LambdaMicrovmsService(regionResolver, mock(StorageFactory.class));
LambdaMicrovmsService.MicrovmImage image = service.createImage(REGION, ACCOUNT_ID, "tagged-image",
"arn:aws:lambda:us-east-1:aws:microvm-image:al2023-1", "arn:aws:iam::000000000000:role/build",
"s3://artifacts/app.zip", null);
LambdaMicrovmsService.NetworkConnector connector = service.createConnector(REGION, ACCOUNT_ID, "egress",
List.of("subnet-00000000000000001"), List.of("sg-00000000000000001"),
"arn:aws:iam::000000000000:role/operator", "token", List.of("MicroVm"), "IPv4");
service.tagResource(REGION, image.imageArn, Map.of("team", "sandbox"));
service.tagResource(REGION, connector.arn, Map.of("team", "net"));
Map<String, ExplorerResource> byType = new HashMap<>();
service.getResources().forEach(resource -> byType.put(resource.resourceType(), resource));
assertEquals(image.imageArn, byType.get("lambda:microvm-image").arn());
assertEquals(Map.of("team", "sandbox"), byType.get("lambda:microvm-image").tags());
assertEquals(REGION, byType.get("lambda:microvm-image").region());
assertEquals(ACCOUNT_ID, byType.get("lambda:microvm-image").owningAccountId());
assertEquals(connector.arn, byType.get("lambda:network-connector").arn());
assertEquals(Map.of("team", "net"), byType.get("lambda:network-connector").tags());
assertEquals(0, service.getSupportedResourceTypes().size());
}
@Test
void aVersionQualifiedConnectorIsNotATagTarget() {
RegionResolver regionResolver = mock(RegionResolver.class);
@@ -940,6 +940,98 @@ class ResourceGroupsTaggingDiscoveryIntegrationTest {
.body("ResourceTagMappingList", empty());
}
@Test
void microvmImageIsDiscoveredAndTaggingApiWritesReachIt() {
String marker = unique();
String name = "discovery-image-" + marker;
String arn = createMicrovmImage(name);
tagAddedThroughTaggingApi(arn, marker);
getResources(markerFilter("lambda:microvm-image", "added", marker))
.body("ResourceTagMappingList.ResourceARN", contains(arn));
addedTagIsListedOnce(arn, marker);
lambdaTags(arn)
.body("Tags.added", equalTo(marker));
untagAddedThroughTaggingApi(arn);
lambdaTags(arn)
.body("Tags", not(hasKey("added")));
given().when().delete("/2025-09-09/microvm-images/" + name).then().statusCode(200);
getResources(arnFilter(arn))
.body("ResourceTagMappingList", empty());
}
@Test
void networkConnectorIsDiscoveredAndTaggingApiWritesReachIt() {
String marker = unique();
String id = createNetworkConnector("discovery-connector-" + marker);
String arn = given()
.when()
.get("/2026-04-04/network-connectors/" + id)
.then()
.statusCode(200)
.extract().path("Arn");
tagAddedThroughTaggingApi(arn, marker);
getResources(markerFilter("lambda:network-connector", "added", marker))
.body("ResourceTagMappingList.ResourceARN", contains(arn));
addedTagIsListedOnce(arn, marker);
lambdaTags(arn)
.body("Tags.added", equalTo(marker));
untagAddedThroughTaggingApi(arn);
lambdaTags(arn)
.body("Tags", not(hasKey("added")));
given().when().delete("/2026-04-04/network-connectors/" + id).then().statusCode(202);
getResources(arnFilter(arn))
.body("ResourceTagMappingList", empty());
}
private static String createMicrovmImage(String name) {
return given()
.contentType("application/json")
.body("""
{
"name": "%s",
"baseImageArn": "arn:aws:lambda:us-east-1:aws:microvm-image:al2023-1",
"buildRoleArn": "arn:aws:iam::000000000000:role/microvm-build",
"codeArtifact": { "uri": "s3://bucket/code.zip" }
}
""".formatted(name))
.when()
.post("/2025-09-09/microvm-images")
.then()
.statusCode(201)
.extract().path("imageArn");
}
private static String createNetworkConnector(String name) {
return given()
.contentType("application/json")
.body("""
{
"Name": "%s",
"ClientToken": "%s",
"OperatorRole": "arn:aws:iam::000000000000:role/microvm-connector-operator",
"Configuration": { "VpcEgressConfiguration": {
"AssociatedComputeResourceTypes": ["MicroVm"],
"NetworkProtocol": "IPv4",
"SubnetIds": ["subnet-0000000000000it01"],
"SecurityGroupIds": ["sg-0000000000000it01"]
} }
}
""".formatted(name, UUID.randomUUID()))
.when()
.post("/2026-04-04/network-connectors")
.then()
.statusCode(202)
.extract().path("Id");
}
private static String createApiKey(String marker) {
return given()
.contentType("application/json")
@@ -1128,9 +1220,13 @@ class ResourceGroupsTaggingDiscoveryIntegrationTest {
}
private static String markerFilter(String resourceType, String marker) {
return markerFilter(resourceType, "fd", marker);
}
private static String markerFilter(String resourceType, String key, String marker) {
return """
{"ResourceTypeFilters": ["%s"], "TagFilters": [%s]}
""".formatted(resourceType, tagFilter("fd", marker));
""".formatted(resourceType, tagFilter(key, marker));
}
private static String markerAndAddedFilter(String resourceType, String marker) {