Light, fluffy, and always free - The AWS Local Emulator alternative
feat(apigateway): persist API key tags and add GetApiKey route (#1677)
Yahia2288da7f234f
diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayController.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayController.javaindex 51338f208e..077d010d8e 100644--- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayController.java+++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayController.java@@ -613,15 +613,35 @@ public class ApiGatewayController {@GET@Path("/apikeys")- public Response getApiKeys(@Context HttpHeaders headers) {+ public Response getApiKeys(@Context HttpHeaders headers,+ @QueryParam("includeValues") boolean includeValues) {String region = regionResolver.resolveRegion(headers);List<ApiKey> keys = service.getApiKeys(region);ObjectNode root = objectMapper.createObjectNode();ArrayNode items = root.putArray("item");- keys.forEach(k -> items.add(toApiKeyNode(k)));+ keys.forEach(k -> {+ ObjectNode node = toApiKeyNode(k);+ if (!includeValues) {+ node.remove("value");+ }+ items.add(node);+ });return Response.ok(root.toString()).type(MediaType.APPLICATION_JSON).build();}+ @GET+ @Path("/apikeys/{apiKeyId}")+ public Response getApiKey(@Context HttpHeaders headers,+ @PathParam("apiKeyId") String apiKeyId,+ @QueryParam("includeValue") boolean includeValue) {+ String region = regionResolver.resolveRegion(headers);+ ObjectNode node = toApiKeyNode(service.getApiKey(region, apiKeyId));+ if (!includeValue) {+ node.remove("value");+ }+ return Response.ok(node.toString()).type(MediaType.APPLICATION_JSON).build();+ }+@POST@Path("/usageplans")public Response createUsagePlan(@Context HttpHeaders headers, String body) {@@ -1642,6 +1662,10 @@ public class ApiGatewayController {node.put("name", k.getName());node.put("value", k.getValue());node.put("enabled", k.isEnabled());+ if (k.getTags() != null && !k.getTags().isEmpty()) {+ ObjectNode tags = node.putObject("tags");+ k.getTags().forEach(tags::put);+ }return node;}diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.javaindex a8c3f3a3e9..441fbba266 100644--- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java+++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java@@ -661,6 +661,12 @@ public class ApiGatewayService {apiKey.setCreatedDate(System.currentTimeMillis() / 1000L);apiKey.setLastUpdatedDate(apiKey.getCreatedDate());+ Map<String, String> tags = new HashMap<>();+ if (request.get("tags") instanceof Map<?, ?> rawTags) {+ rawTags.forEach((key, value) -> tags.put(String.valueOf(key), String.valueOf(value)));+ }+ apiKey.setTags(tags);+apiKeyStore.put(apiKeyGlobalKey(region, apiKey.getId()), apiKey);LOG.infov("Created API Key {0}", apiKey.getId());return apiKey;@@ -668,7 +674,7 @@ public class ApiGatewayService {public ApiKey getApiKey(String region, String apiKeyId) {return apiKeyStore.get(apiKeyGlobalKey(region, apiKeyId))- .orElseThrow(() -> new AwsException("NotFoundException", "API Key not found", 404));+ .orElseThrow(() -> new AwsException("NotFoundException", "Invalid API Key identifier specified", 404));}public List<ApiKey> getApiKeys(String region) {diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/model/ApiKey.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/model/ApiKey.javaindex bb5dd41abd..f97e9fa60c 100644--- a/src/main/java/io/github/hectorvent/floci/services/apigateway/model/ApiKey.java+++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/model/ApiKey.java@@ -3,6 +3,9 @@ package io.github.hectorvent.floci.services.apigateway.model;import com.fasterxml.jackson.annotation.JsonIgnoreProperties;import io.quarkus.runtime.annotations.RegisterForReflection;+import java.util.HashMap;+import java.util.Map;+@RegisterForReflection@JsonIgnoreProperties(ignoreUnknown = true)public class ApiKey {@@ -12,6 +15,7 @@ public class ApiKey {private boolean enabled;private long createdDate;private long lastUpdatedDate;+ private Map<String, String> tags = new HashMap<>();public ApiKey() {}@@ -32,4 +36,7 @@ public class ApiKey {public long getLastUpdatedDate() { return lastUpdatedDate; }public void setLastUpdatedDate(long lastUpdatedDate) { this.lastUpdatedDate = lastUpdatedDate; }++ public Map<String, String> getTags() { return tags; }+ public void setTags(Map<String, String> tags) { this.tags = tags != null ? tags : new HashMap<>(); }}diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayApiKeyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayApiKeyIntegrationTest.javanew file mode 100644index 0000000000..bee6e2752d--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayApiKeyIntegrationTest.java@@ -0,0 +1,93 @@+package io.github.hectorvent.floci.services.apigateway;++import io.quarkus.test.junit.QuarkusTest;+import io.restassured.http.ContentType;+import org.junit.jupiter.api.MethodOrderer;+import org.junit.jupiter.api.Order;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.TestMethodOrder;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.*;++/**+ * Integration tests for API Gateway API key tags and the GetApiKey route.+ *+ * <p>Validates that {@code CreateApiKey} persists {@code tags} and returns them+ * from {@code CreateApiKey}, {@code GetApiKey} and {@code GetApiKeys}, and that+ * {@code GET /apikeys/{apiKeyId}} is served (returning the key, omitting the+ * value unless {@code includeValue=true}, and 404 when the key is unknown).+ */+@QuarkusTest+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)+class ApiGatewayApiKeyIntegrationTest {++ private static String apiKeyId = "uninitialized";++ @Test @Order(1)+ void createApiKeyPersistsTags() {+ String body = """+ {"name":"k","enabled":true,"tags":{"Team":"platform","Project":"demo"}}+ """;+ apiKeyId = given()+ .contentType(ContentType.JSON)+ .body(body)+ .when().post("/apikeys")+ .then()+ .statusCode(201)+ .body("id", notNullValue())+ .body("name", equalTo("k"))+ .body("enabled", equalTo(true))+ .body("tags.Team", equalTo("platform"))+ .body("tags.Project", equalTo("demo"))+ .extract().path("id");+ }++ @Test @Order(2)+ void getApiKeyReturnsTagsAndOmitsValueByDefault() {+ given()+ .when().get("/apikeys/" + apiKeyId)+ .then()+ .statusCode(200)+ .body("id", equalTo(apiKeyId))+ .body("tags.Team", equalTo("platform"))+ .body("tags.Project", equalTo("demo"))+ .body("value", nullValue());+ }++ @Test @Order(3)+ void getApiKeyIncludesValueWhenRequested() {+ given()+ .when().get("/apikeys/" + apiKeyId + "?includeValue=true")+ .then()+ .statusCode(200)+ .body("value", notNullValue());+ }++ @Test @Order(4)+ void listApiKeysIncludesTagsAndOmitsValueByDefault() {+ given()+ .when().get("/apikeys")+ .then()+ .statusCode(200)+ .body("item.find { it.id == '" + apiKeyId + "' }.tags.Team", equalTo("platform"))+ .body("item.find { it.id == '" + apiKeyId + "' }.value", nullValue());++ given()+ .when().get("/apikeys?includeValues=true")+ .then()+ .statusCode(200)+ .body("item.find { it.id == '" + apiKeyId + "' }.value", notNullValue());+ }++ @Test @Order(5)+ void getApiKeyNotFound() {+ given()+ .when().get("/apikeys/doesnotexist")+ .then()+ .statusCode(404)+ .contentType(ContentType.JSON)+ .body("__type", equalTo("NotFoundException"))+ .body("message", equalTo("Invalid API Key identifier specified"));+ }+}