mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(lambda): give resource-tag conditions the tags of the Lambda resource a request names (#8569)
The generated request resources give Lambda's function, function version and alias, event source mapping, web function, revision and endpoint, MicroVM image and network connector types aws:ResourceTag/ keys, but LambdaStoredResourceArns did not answer storedTags, so a condition on their tags saw none. It now answers from the service holding each: LambdaService for a function, whose versions and aliases have its tags as Lambda keeps tags on the function alone, and an event source mapping; LambdaWebService for a web function, whose revisions and endpoints inherit its tags; LambdaMicrovmsService for an image or a connector. Each reads the resource under the ARN in the account and Region it names, a pre-multi-account one of the default account in place, through the new AccountAwareStorageBackend.readInAccount, matched on the whole ARN. Tagging a function or a mapping now puts a new tag map in place of the old one, so the lookup always copies one whole set.
This commit is contained in:
1 parent
08479ffffe
commit
b2a019ef4e
17 files changed
+457
-20
No files matched your search
@@ -2181,7 +2181,7 @@ class ResourceArnBuilderTest {
|
||||
Instance<LambdaMicrovmsService> instance = mock(Instance.class);
|
||||
when(instance.isResolvable()).thenReturn(true);
|
||||
when(instance.get()).thenReturn(microvms);
|
||||
ResourceArnBuilder backed = reading(new LambdaStoredResourceArns(null, instance));
|
||||
ResourceArnBuilder backed = reading(new LambdaStoredResourceArns(null, null, instance, null));
|
||||
|
||||
assertEquals(List.of(image), lambda(backed, "GET", "/2025-09-09/microvms/microvm-1", "GetMicrovm"));
|
||||
assertEquals(List.of(image), lambda(backed, "POST", "/2025-09-09/microvms/microvm-1/suspend",
|
||||
@@ -2549,7 +2549,7 @@ class ResourceArnBuilderTest {
|
||||
String callbackId = Base64.getEncoder().encodeToString((execution + "|c1|nonce").getBytes(StandardCharsets.UTF_8));
|
||||
@SuppressWarnings("unchecked")
|
||||
Instance<LambdaMicrovmsService> none = mock(Instance.class);
|
||||
ResourceArnBuilder backed = reading(new LambdaStoredResourceArns(null, none));
|
||||
ResourceArnBuilder backed = reading(new LambdaStoredResourceArns(null, null, none, null));
|
||||
assertEquals(List.of(execution), lambda(backed, "POST",
|
||||
"/2025-12-01/durable-execution-callbacks/" + callbackId.replace("/", "%2F") + "/succeed",
|
||||
"SendDurableExecutionCallbackSuccess"));
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
package io.floci.aws.services.lambda;
|
||||
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.floci.aws.testing.EnforcementFixtures;
|
||||
import io.floci.aws.testing.EnforcementFixtures.UserCredentials;
|
||||
import io.floci.aws.testing.IamEnforcementProfile;
|
||||
import io.floci.aws.testing.PartitionCleanup;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.lambda.LambdaClient;
|
||||
import software.amazon.awssdk.services.lambda.model.LambdaException;
|
||||
import software.amazon.awssdk.services.lambda.model.ProvisionedConcurrencyConfigNotFoundException;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.floci.aws.testing.EnforcementFixtures.auth;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(IamEnforcementProfile.class)
|
||||
class LambdaResourceTagIamIntegrationTest {
|
||||
|
||||
private static final String ACCOUNT = "741681410031";
|
||||
|
||||
@TestHTTPResource("/")
|
||||
URI endpoint;
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@Test
|
||||
void aResourceTagConditionReadsTheTagsOfTheFunctionItsVersionsAndAnEventSourceMapping() throws Exception {
|
||||
Region region = Region.US_EAST_1;
|
||||
String tagged = "tag-iam-" + UUID.randomUUID();
|
||||
String untagged = "tag-iam-" + UUID.randomUUID();
|
||||
try (LambdaClient owner = AwsTestClients.create(LambdaClient.builder(), ACCOUNT, region, endpoint)) {
|
||||
String taggedArn = LambdaFunctionUrlFixtures.createFunction(owner, tagged, ACCOUNT, region, endpoint, cleanup);
|
||||
LambdaFunctionUrlFixtures.createFunction(owner, untagged, ACCOUNT, region, endpoint, cleanup);
|
||||
owner.tagResource(request -> request.resource(taggedArn).tags(Map.of("team", "data")));
|
||||
String taggedMapping = mapping(owner, tagged, Map.of("team", "data"));
|
||||
String untaggedMapping = mapping(owner, tagged, Map.of("team", "web"));
|
||||
String authorization = auth(ACCOUNT, region.id(), "iam");
|
||||
String user = "tag-reader-" + UUID.randomUUID();
|
||||
UserCredentials credentials = EnforcementFixtures.createUser(cleanup, endpoint.getPort(), authorization, user);
|
||||
EnforcementFixtures.putUserPolicy(endpoint.getPort(), authorization, user, "team-data",
|
||||
"""
|
||||
{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [{
|
||||
"Effect": "Allow",
|
||||
"Action": ["lambda:GetFunction", "lambda:GetProvisionedConcurrencyConfig",
|
||||
"lambda:GetEventSourceMapping"],
|
||||
"Resource": "*",
|
||||
"Condition": {"StringEquals": {"aws:ResourceTag/team": "data"}}
|
||||
}]
|
||||
}
|
||||
""");
|
||||
try (LambdaClient reader = AwsTestClients.create(LambdaClient.builder(),
|
||||
credentials.accessKeyId(), credentials.secretAccessKey(), region, endpoint)) {
|
||||
assertEquals(taggedArn, reader.getFunction(request -> request.functionName(tagged))
|
||||
.configuration().functionArn());
|
||||
denied(() -> reader.getFunction(request -> request.functionName(untagged)));
|
||||
|
||||
assertThrows(ProvisionedConcurrencyConfigNotFoundException.class, () ->
|
||||
reader.getProvisionedConcurrencyConfig(request -> request.functionName(tagged).qualifier("1")));
|
||||
denied(() -> reader.getProvisionedConcurrencyConfig(request ->
|
||||
request.functionName(untagged).qualifier("1")));
|
||||
|
||||
assertEquals(taggedMapping, reader.getEventSourceMapping(request -> request.uuid(taggedMapping))
|
||||
.uuid());
|
||||
denied(() -> reader.getEventSourceMapping(request -> request.uuid(untaggedMapping)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** A disabled self-managed Kafka mapping of the function, so nothing polls the endpoint it names. */
|
||||
private String mapping(LambdaClient owner, String function, Map<String, String> tags) {
|
||||
String uuid = owner.createEventSourceMapping(request -> request.functionName(function)
|
||||
.topics("orders").enabled(false).tags(tags)
|
||||
.selfManagedEventSource(source -> source.endpointsWithStrings(
|
||||
Map.of("KAFKA_BOOTSTRAP_SERVERS", List.of("localhost:9092"))))).uuid();
|
||||
cleanup.register(() -> {
|
||||
try (LambdaClient client = AwsTestClients.create(LambdaClient.builder(), ACCOUNT, Region.US_EAST_1,
|
||||
endpoint)) {
|
||||
client.deleteEventSourceMapping(request -> request.uuid(uuid));
|
||||
}
|
||||
});
|
||||
return uuid;
|
||||
}
|
||||
|
||||
private static void denied(Runnable call) {
|
||||
LambdaException denied = assertThrows(LambdaException.class, call::run);
|
||||
assertEquals(403, denied.statusCode());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package io.floci.aws.services.lambda;
|
||||
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.InMemoryStorage;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.lambda.model.EventSourceMapping;
|
||||
import io.floci.aws.services.lambda.model.LambdaFunction;
|
||||
import io.floci.aws.services.lambda.zip.CodeStore;
|
||||
import io.floci.aws.services.lambda.zip.ZipExtractor;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class LambdaStoredTagsTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
private LambdaService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
StorageFactory storageFactory = mock(StorageFactory.class);
|
||||
when(storageFactory.create(anyString(), anyString(), any()))
|
||||
.thenAnswer(inv -> AccountAwareStorageBackend.inMemory("000000000000"));
|
||||
service = new LambdaService(new LambdaFunctionStore(new InMemoryStorage<String, LambdaFunction>()),
|
||||
new WarmPool(), new CodeStore(Path.of("target/test-data/lambda-code")), new ZipExtractor(), null,
|
||||
new RegionResolver(REGION, "000000000000"), storageFactory);
|
||||
}
|
||||
|
||||
private static Map<String, Object> functionRequest(String name, Map<String, String> tags) {
|
||||
return new HashMap<>(Map.of(
|
||||
"FunctionName", name,
|
||||
"Runtime", "nodejs20.x",
|
||||
"Role", "arn:aws:iam::000000000000:role/test-role",
|
||||
"Handler", "index.handler",
|
||||
"Tags", tags));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFunctionsVersionsAndAliasesHaveItsTags() {
|
||||
LambdaFunction fn = service.createFunction(REGION, functionRequest("tagged-fn", Map.of("team", "data")));
|
||||
LambdaFunction version = service.publishVersion(REGION, "tagged-fn", null);
|
||||
service.createAlias(REGION, "tagged-fn", "live", version.getVersion(), null, null);
|
||||
String arn = fn.getFunctionArn();
|
||||
|
||||
assertEquals(Map.of("team", "data"), service.storedTags(arn));
|
||||
assertEquals(Map.of("team", "data"), service.storedTags(arn + ":$LATEST"));
|
||||
assertEquals(Map.of("team", "data"), service.storedTags(arn + ":" + version.getVersion()));
|
||||
assertEquals(Map.of("team", "data"), service.storedTags(arn + ":live"));
|
||||
assertEquals(Map.of(), service.storedTags(arn + ":7"));
|
||||
assertEquals(Map.of(), service.storedTags(arn + ":staging"));
|
||||
assertEquals(Map.of(), service.storedTags(arn + ":1/durable-execution/run/0a1b2c"));
|
||||
assertEquals(Map.of(), service.storedTags(arn.replace("000000000000", "111111111111")));
|
||||
assertEquals(Map.of(), service.storedTags(arn.replace(REGION, "us-west-2")));
|
||||
assertEquals(Map.of(), service.storedTags(arn + "-missing"));
|
||||
|
||||
service.tagResource(arn, Map.of("stage", "prod"));
|
||||
service.untagResource(arn, List.of("team"));
|
||||
|
||||
assertEquals(Map.of("stage", "prod"), service.storedTags(arn + ":live"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anEventSourceMappingHasItsOwnTags() {
|
||||
service.createFunction(REGION, functionRequest("mapped-fn", Map.of("team", "data")));
|
||||
Map<String, Object> request = new HashMap<>(Map.of(
|
||||
"FunctionName", "mapped-fn",
|
||||
"Topics", List.of("orders"),
|
||||
"SelfManagedEventSource", Map.of("Endpoints",
|
||||
Map.of("KAFKA_BOOTSTRAP_SERVERS", List.of("localhost:9092"))),
|
||||
"Tags", Map.of("pipeline", "orders")));
|
||||
EventSourceMapping esm = service.createEventSourceMapping(REGION, request);
|
||||
String arn = LambdaArnUtils.eventSourceMappingArn(REGION, "000000000000", esm.getUuid());
|
||||
|
||||
assertEquals(Map.of("pipeline", "orders"), service.storedTags(arn));
|
||||
|
||||
service.tagResource(arn, Map.of("owner", "ingest"));
|
||||
|
||||
assertEquals(Map.of("pipeline", "orders", "owner", "ingest"), service.storedTags(arn));
|
||||
assertEquals(Map.of(), service.storedTags(arn.replace(esm.getUuid(), "00000000-0000-0000-0000-000000000000")));
|
||||
assertEquals(Map.of(), service.storedTags(arn.replace("000000000000", "111111111111")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void taggingPutsANewTagMapInPlaceOfTheOneAReaderHolds() {
|
||||
LambdaFunction fn = service.createFunction(REGION, functionRequest("cow-fn", Map.of("team", "data")));
|
||||
Map<String, String> held = service.getFunction(REGION, "cow-fn").getTags();
|
||||
|
||||
service.tagResource(fn.getFunctionArn(), Map.of("stage", "prod"));
|
||||
service.untagResource(fn.getFunctionArn(), List.of("team"));
|
||||
|
||||
assertEquals(Map.of("team", "data"), held);
|
||||
assertEquals(Map.of("stage", "prod"), service.getFunction(REGION, "cow-fn").getTags());
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package io.floci.aws.services.lambdamicrovms;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.resource.ExplorerResource;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -21,6 +22,8 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -91,6 +94,32 @@ class LambdaMicrovmsServiceTest {
|
||||
assertEquals(0, service.getSupportedResourceTypes().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void storedTagsReadTheImageOrTheConnectorTheArnNamesInItsAccount() {
|
||||
RegionResolver regionResolver = mock(RegionResolver.class);
|
||||
when(regionResolver.getAccountId()).thenReturn(ACCOUNT_ID);
|
||||
StorageFactory storageFactory = mock(StorageFactory.class);
|
||||
when(storageFactory.create(anyString(), anyString(), any()))
|
||||
.thenAnswer(inv -> AccountAwareStorageBackend.inMemory(ACCOUNT_ID));
|
||||
LambdaMicrovmsService service = new LambdaMicrovmsService(regionResolver, storageFactory);
|
||||
service.initializeStorage();
|
||||
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"));
|
||||
|
||||
assertEquals(Map.of("team", "sandbox"), service.storedTags(image.imageArn));
|
||||
assertEquals(Map.of("team", "net"), service.storedTags(connector.arn));
|
||||
assertEquals(Map.of(), service.storedTags(connector.arn + ":1"));
|
||||
assertEquals(Map.of(), service.storedTags(image.imageArn.replace(ACCOUNT_ID, "111111111111")));
|
||||
assertEquals(Map.of(), service.storedTags(image.imageArn.replace(REGION, "us-west-2")));
|
||||
assertEquals(Map.of(), service.storedTags("arn:aws:lambda:us-east-1:000000000000:microvm-image:missing"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aVersionQualifiedConnectorIsNotATagTarget() {
|
||||
RegionResolver regionResolver = mock(RegionResolver.class);
|
||||
|
||||
@@ -254,6 +254,24 @@ class LambdaWebServiceTest {
|
||||
message(() -> service.tagResource(REGION, "not-an-arn", mapper.createObjectNode())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void storedTagsAreTheWebFunctionsForItAndItsRevisionsAndEndpoints() {
|
||||
LambdaWebService service = service();
|
||||
ObjectNode request = createRequest("app");
|
||||
request.putObject("tags").put("team", "data");
|
||||
WebFunction created = service.createFunction(REGION, request);
|
||||
String revision = created.revisions.keySet().iterator().next();
|
||||
|
||||
assertEquals(Map.of("team", "data"), service.storedTags(ARN));
|
||||
assertEquals(Map.of("team", "data"), service.storedTags(ARN + "/revision/" + revision));
|
||||
assertEquals(Map.of("team", "data"), service.storedTags(ARN + "/endpoint/dev"));
|
||||
assertEquals(Map.of(), service.storedTags(ARN + "/endpoint/prod"));
|
||||
assertEquals(Map.of(), service.storedTags(ARN + "/revision/rev-zzzzz"));
|
||||
assertEquals(Map.of(), service.storedTags(ARN.replace(ACCOUNT_ID, "111111111111")));
|
||||
assertEquals(Map.of(), service.storedTags(ARN.replace(REGION, "us-west-2")));
|
||||
assertEquals(Map.of(), service.storedTags(ARN + "-missing"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void untagValidatesEveryKey() {
|
||||
LambdaWebService service = service();
|
||||
|
||||
Reference in new issue
Block a user