fix(apigateway): tag REST API VPC links (#6165)

* fix(apigateway): tag REST API VPC links

* fix(apigateway): synchronize VPC link tag updates
This commit is contained in:
jwoa95 authored and GitHub committed 2026-10-21 15:55:24 -04:00
1 parent 10d3317346
commit 110f58ae34
6 files changed
+249 -1

No files matched your search

@@ -0,0 +1,94 @@
package io.github.hectorvent.floci.services.apigateway;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.apigateway.ApiGatewayClient;
import software.amazon.awssdk.services.apigateway.model.BadRequestException;
import software.amazon.awssdk.services.apigateway.model.CreateVpcLinkResponse;
import software.amazon.awssdk.services.apigateway.model.NotFoundException;
import java.net.URI;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@QuarkusTest
class ApiGatewayVpcLinkTaggingIntegrationTest {
@TestHTTPResource("/")
URI endpoint;
private ApiGatewayClient client() {
return ApiGatewayClient.builder().endpointOverride(endpoint).region(Region.US_EAST_1)
.httpClientBuilder(UrlConnectionHttpClient.builder())
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")))
.build();
}
@Test
void vpcLinkTagsRoundTripThroughTheSdkAndVpcLinkRead() {
try (ApiGatewayClient client = client()) {
CreateVpcLinkResponse link = client.createVpcLink(r -> r.name("tagged-link")
.targetArns("arn:aws:elasticloadbalancing:us-east-1:000000000000:loadbalancer/net/test/123")
.tags(Map.of("owner", "network", "keep", "yes")));
String arn = "arn:aws:apigateway:us-east-1::/vpclinks/" + link.id();
try {
assertEquals(link.tags(), client.getTags(r -> r.resourceArn(arn)).tags());
client.tagResource(r -> r.resourceArn(arn).tags(Map.of("owner", "platform", "env", "test")));
Map<String, String> updated = Map.of("owner", "platform", "keep", "yes", "env", "test");
assertEquals(updated, client.getTags(r -> r.resourceArn(arn)).tags());
assertEquals(updated, client.getVpcLink(r -> r.vpcLinkId(link.id())).tags());
client.untagResource(r -> r.resourceArn(arn).tagKeys("env", "absent"));
assertEquals(Map.of("owner", "platform", "keep", "yes"),
client.getTags(r -> r.resourceArn(arn)).tags());
} finally {
client.deleteVpcLink(r -> r.vpcLinkId(link.id()));
}
assertThrows(NotFoundException.class, () -> client.getTags(r -> r.resourceArn(arn)));
assertThrows(NotFoundException.class,
() -> client.tagResource(r -> r.resourceArn(arn).tags(Map.of("env", "test"))));
assertThrows(NotFoundException.class, () -> client.untagResource(r -> r.resourceArn(arn).tagKeys("env")));
}
}
@Test
void reservedTagUpdatesAreRejectedWithoutChangingExistingTags() {
try (ApiGatewayClient client = client()) {
Map<String, String> original = Map.of("owner", "network");
CreateVpcLinkResponse link = client.createVpcLink(r -> r.name("reserved-tag-link")
.targetArns("arn:aws:elasticloadbalancing:us-east-1:000000000000:loadbalancer/net/test/123")
.tags(original));
String arn = "arn:aws:apigateway:us-east-1::/vpclinks/" + link.id();
try {
for (String key : List.of("floci:custom-id", "floci:reserved", "_custom_id_")) {
BadRequestException error = assertThrows(BadRequestException.class,
() -> client.tagResource(r -> r.resourceArn(arn)
.tags(Map.of("owner", "changed", key, "reserved"))));
assertEquals(400, error.statusCode());
assertEquals(original, client.getTags(r -> r.resourceArn(arn)).tags());
assertEquals(original, client.getVpcLink(r -> r.vpcLinkId(link.id())).tags());
}
} finally {
client.deleteVpcLink(r -> r.vpcLinkId(link.id()));
}
}
}
@Test
void nestedVpcLinkArnIsRejected() {
try (ApiGatewayClient client = client()) {
String arn = "arn:aws:apigateway:us-east-1::/vpclinks/missing/nested";
assertThrows(BadRequestException.class, () -> client.getTags(r -> r.resourceArn(arn)));
assertThrows(BadRequestException.class,
() -> client.tagResource(r -> r.resourceArn(arn).tags(Map.of("env", "test"))));
assertThrows(BadRequestException.class, () -> client.untagResource(r -> r.resourceArn(arn).tagKeys("env")));
}
}
}
@@ -0,0 +1,104 @@
package io.github.hectorvent.floci.services.apigateway;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.config.TlsCertificateManager;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.apigateway.model.VpcLink;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class ApiGatewayVpcLinkTaggingServiceTest {
private static final String REGION = "us-east-1";
private static final String ACCOUNT = "000000000000";
private ApiGatewayService service;
private VpcLink link;
@BeforeEach
void setUp() {
Map<String, AccountAwareStorageBackend<?>> stores = new HashMap<>();
StorageFactory storageFactory = mock(StorageFactory.class);
when(storageFactory.create(anyString(), anyString(), any())).thenAnswer(invocation ->
stores.computeIfAbsent(invocation.getArgument(1, String.class),
ignored -> AccountAwareStorageBackend.inMemory(ACCOUNT)));
service = new ApiGatewayService(storageFactory,
mock(EmulatorConfig.class, RETURNS_DEEP_STUBS), mock(TlsCertificateManager.class),
new RegionResolver(REGION, ACCOUNT));
link = service.createVpcLink(REGION, Map.of("name", "tagged-link",
"targetArns", List.of("target"), "tags", Map.of("owner", "network", "keep", "yes")));
}
@Test
void tagWriteLeavesPreviouslyReturnedMapUnchanged() {
Map<String, String> snapshot = service.getVpcLink(REGION, link.getId()).getTags();
service.tagVpcLink(REGION, link.getId(), Map.of("owner", "platform", "env", "test"));
assertEquals(Map.of("owner", "network", "keep", "yes"), snapshot);
assertEquals(Map.of("owner", "platform", "keep", "yes", "env", "test"),
service.getVpcLink(REGION, link.getId()).getTags());
}
@Test
void untagWriteLeavesPreviouslyReturnedMapUnchanged() {
Map<String, String> snapshot = service.getVpcLink(REGION, link.getId()).getTags();
service.untagVpcLink(REGION, link.getId(), List.of("owner", "absent"));
assertEquals(Map.of("owner", "network", "keep", "yes"), snapshot);
assertEquals(Map.of("keep", "yes"), service.getVpcLink(REGION, link.getId()).getTags());
}
@Test
void concurrentTagAndUntagCallsRetainEveryIndependentChange() throws Exception {
Map<String, String> initial = new HashMap<>();
Map<String, String> expected = new HashMap<>(Map.of("owner", "network", "keep", "yes"));
for (int i = 0; i < 128; i++) {
initial.put("remove-" + i, "old");
expected.put("add-" + i, "new");
}
service.tagVpcLink(REGION, link.getId(), initial);
CountDownLatch start = new CountDownLatch(1);
try (ExecutorService executor = Executors.newFixedThreadPool(8)) {
List<Future<?>> writers = new ArrayList<>();
for (int worker = 0; worker < 8; worker++) {
int offset = worker * 16;
writers.add(executor.submit(() -> {
assertTrue(start.await(5, TimeUnit.SECONDS));
for (int i = offset; i < offset + 16; i++) {
service.tagVpcLink(REGION, link.getId(), Map.of("add-" + i, "new"));
service.untagVpcLink(REGION, link.getId(), List.of("remove-" + i));
}
return null;
}));
}
start.countDown();
for (Future<?> writer : writers) {
writer.get(10, TimeUnit.SECONDS);
}
}
assertEquals(expected, service.getVpcLink(REGION, link.getId()).getTags());
}
}