mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
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:
1 parent
10d3317346
commit
110f58ae34
6 files changed
+249
-1
No files matched your search
@@ -517,6 +517,12 @@ Response caching needs both switches AWS requires: `cacheClusterEnabled` on the
|
||||
|
||||
The five REST VPC Link operations (`CreateVpcLink`, `GetVpcLink`, `GetVpcLinks`, `UpdateVpcLink`, `DeleteVpcLink`) are emulated at `/vpclinks`. `CreateVpcLink` requires a name and at least one target ARN, answers `202`, and provisions the link as `AVAILABLE` immediately rather than transitioning through `PENDING`. Since Floci has no real VPC, a valid link routes straight to the integration URI; what is enforced is that the link exists and is available. `UpdateVpcLink` follows AWS's patch-operation table: only `replace` on `/name` and `/description` is accepted, and any other operation or path returns `BadRequestException` rather than being applied or silently ignored.
|
||||
|
||||
|
||||
REST API VPC links also support `GetTags`, `TagResource` and `UntagResource` with the ARN
|
||||
`arn:{partition}:apigateway:{region}::/vpclinks/{vpcLinkId}`. Creation tags and later updates
|
||||
are shared with `GetVpcLink`; tagging does not replace the link. These are REST API (v1)
|
||||
links, separate from HTTP API (v2) VPC links.
|
||||
|
||||
### Examples
|
||||
|
||||
```bash
|
||||
|
||||
@@ -330,6 +330,12 @@
|
||||
</dependency>
|
||||
|
||||
<!-- Testing -->
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>apigateway</artifactId>
|
||||
<version>${aws.sdk.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>s3vectors</artifactId>
|
||||
|
||||
@@ -130,6 +130,7 @@ public class ApiGatewayService implements ResourceProvider {
|
||||
* also keeps a mapping from being created under a domain that is being deleted at that moment.
|
||||
*/
|
||||
private final Object domainNameLock = new Object();
|
||||
private final Object vpcLinkTagLock = new Object();
|
||||
private final TlsCertificateManager certificateManager;
|
||||
private final EmulatorConfig config;
|
||||
private final RegionResolver regionResolver;
|
||||
@@ -4657,6 +4658,31 @@ public class ApiGatewayService implements ResourceProvider {
|
||||
.orElseThrow(() -> new AwsException("NotFoundException", "Invalid VPC link identifier specified", 404));
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize tag writers and replace the map so readers and persistence can safely iterate
|
||||
* previously returned maps, as with custom-domain tags.
|
||||
*/
|
||||
public void tagVpcLink(String region, String vpcLinkId, Map<String, String> tags) {
|
||||
ReservedTags.rejectApiGatewayReservedTagsOnUpdate(tags);
|
||||
synchronized (vpcLinkTagLock) {
|
||||
VpcLink link = getVpcLink(region, vpcLinkId);
|
||||
Map<String, String> merged = new LinkedHashMap<>(link.getTags());
|
||||
merged.putAll(tags);
|
||||
link.setTags(merged);
|
||||
vpcLinkStore.put(vpcLinkKey(region, vpcLinkId), link);
|
||||
}
|
||||
}
|
||||
|
||||
public void untagVpcLink(String region, String vpcLinkId, List<String> tagKeys) {
|
||||
synchronized (vpcLinkTagLock) {
|
||||
VpcLink link = getVpcLink(region, vpcLinkId);
|
||||
Map<String, String> remaining = new LinkedHashMap<>(link.getTags());
|
||||
tagKeys.forEach(remaining::remove);
|
||||
link.setTags(remaining);
|
||||
vpcLinkStore.put(vpcLinkKey(region, vpcLinkId), link);
|
||||
}
|
||||
}
|
||||
|
||||
public List<VpcLink> getVpcLinks(String region) {
|
||||
return vpcLinkStore.scan(k -> k.startsWith(region + "::"));
|
||||
}
|
||||
|
||||
+13
-1
@@ -17,7 +17,8 @@ import java.util.regex.Pattern;
|
||||
* <p>ARN formats: {@code arn:aws:apigateway:<region>::/restapis/<apiId>} for a REST API,
|
||||
* {@code arn:aws:apigateway:<region>::/domainnames/<domainName>} for a custom domain,
|
||||
* {@code arn:aws:apigateway:<region>::/apikeys/<apiKeyId>} for an API key and
|
||||
* {@code arn:aws:apigateway:<region>::/usageplans/<usagePlanId>} for a usage plan. The
|
||||
* {@code arn:aws:apigateway:<region>::/usageplans/<usagePlanId>} for a usage plan and
|
||||
* {@code arn:aws:apigateway:<region>::/vpclinks/<vpcLinkId>} for a REST VPC link. The
|
||||
* {@code apiId}, domain name, key id or plan id is the canonical identifier the underlying
|
||||
* {@link ApiGatewayService} uses for its tag store.
|
||||
*/
|
||||
@@ -28,6 +29,7 @@ public class ApiGatewayTagHandler implements TagHandler {
|
||||
private static final String DOMAIN_NAMES = "/domainnames/";
|
||||
private static final String REST_APIS = "/restapis/";
|
||||
private static final String USAGE_PLANS = "/usageplans/";
|
||||
private static final String VPC_LINKS = "/vpclinks/";
|
||||
private static final Pattern STAGE_RESOURCE = Pattern.compile("/restapis/[^/]+/stages/([^/]+)");
|
||||
|
||||
private final ApiGatewayService service;
|
||||
@@ -62,6 +64,10 @@ public class ApiGatewayTagHandler implements TagHandler {
|
||||
if (usagePlanId != null) {
|
||||
return service.getUsagePlan(region, usagePlanId).getTags();
|
||||
}
|
||||
String vpcLinkId = topLevelIdFromArn(arn, VPC_LINKS);
|
||||
if (vpcLinkId != null) {
|
||||
return service.getVpcLink(region, vpcLinkId).getTags();
|
||||
}
|
||||
String stageName = stageNameFromArn(arn);
|
||||
return stageName != null
|
||||
? service.getStageTags(region, apiIdFromArn(arn), stageName)
|
||||
@@ -74,6 +80,7 @@ public class ApiGatewayTagHandler implements TagHandler {
|
||||
String domainName = topLevelIdFromArn(arn, DOMAIN_NAMES);
|
||||
String apiKeyId = topLevelIdFromArn(arn, API_KEYS);
|
||||
String usagePlanId = topLevelIdFromArn(arn, USAGE_PLANS);
|
||||
String vpcLinkId = topLevelIdFromArn(arn, VPC_LINKS);
|
||||
String stageName = stageNameFromArn(arn);
|
||||
if (domainName != null) {
|
||||
service.tagDomainName(region, domainName, tags);
|
||||
@@ -81,6 +88,8 @@ public class ApiGatewayTagHandler implements TagHandler {
|
||||
service.tagApiKey(region, apiKeyId, tags);
|
||||
} else if (usagePlanId != null) {
|
||||
service.tagUsagePlan(region, usagePlanId, tags);
|
||||
} else if (vpcLinkId != null) {
|
||||
service.tagVpcLink(region, vpcLinkId, tags);
|
||||
} else if (stageName != null) {
|
||||
service.tagStage(region, apiIdFromArn(arn), stageName, tags);
|
||||
} else {
|
||||
@@ -94,6 +103,7 @@ public class ApiGatewayTagHandler implements TagHandler {
|
||||
String domainName = topLevelIdFromArn(arn, DOMAIN_NAMES);
|
||||
String apiKeyId = topLevelIdFromArn(arn, API_KEYS);
|
||||
String usagePlanId = topLevelIdFromArn(arn, USAGE_PLANS);
|
||||
String vpcLinkId = topLevelIdFromArn(arn, VPC_LINKS);
|
||||
String stageName = stageNameFromArn(arn);
|
||||
if (domainName != null) {
|
||||
service.untagDomainName(region, domainName, tagKeys);
|
||||
@@ -101,6 +111,8 @@ public class ApiGatewayTagHandler implements TagHandler {
|
||||
service.untagApiKey(region, apiKeyId, tagKeys);
|
||||
} else if (usagePlanId != null) {
|
||||
service.untagUsagePlan(region, usagePlanId, tagKeys);
|
||||
} else if (vpcLinkId != null) {
|
||||
service.untagVpcLink(region, vpcLinkId, tagKeys);
|
||||
} else if (stageName != null) {
|
||||
service.untagStage(region, apiIdFromArn(arn), stageName, tagKeys);
|
||||
} else {
|
||||
|
||||
+94
@@ -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")));
|
||||
}
|
||||
}
|
||||
}
|
||||
+104
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user