fix(apigateway): complete API Gateway API key responses (#3817)

Return timestamps, stage keys, tags, and customer ID across create, get, list,
and update operations to prevent Terraform provider crashes and state drift.
Generate opaque IDs by default while preserving legacy generateDistinctId=false behavior.
Keep API key values hidden unless explicitly requested.
Add REST, SDK, and CloudFormation regression coverage.
This commit is contained in:
Attila Kis authored and GitHub committed 2026-09-17 16:01:43 -04:00
1 parent 30b491a546
commit ad20699a6b
9 files changed
+244 -71

No files matched your search

@@ -27,7 +27,7 @@ class ApiGatewayApiKeyIntegrationTest {
@Test @Order(1)
void createApiKeyPersistsTags() {
String body = """
{"name":"k","enabled":true,"tags":{"Team":"platform","Project":"demo"}}
{"name":"k","enabled":true,"customerId":"marketplace-customer-1","tags":{"Team":"platform","Project":"demo"}}
""";
apiKeyId = given()
.contentType(ContentType.JSON)
@@ -37,7 +37,11 @@ class ApiGatewayApiKeyIntegrationTest {
.statusCode(201)
.body("id", notNullValue())
.body("name", equalTo("k"))
.body("customerId", equalTo("marketplace-customer-1"))
.body("enabled", equalTo(true))
.body("createdDate", greaterThan(0))
.body("lastUpdatedDate", greaterThan(0))
.body("stageKeys", empty())
.body("tags.Team", equalTo("platform"))
.body("tags.Project", equalTo("demo"))
.extract().path("id");
@@ -50,6 +54,10 @@ class ApiGatewayApiKeyIntegrationTest {
.then()
.statusCode(200)
.body("id", equalTo(apiKeyId))
.body("customerId", equalTo("marketplace-customer-1"))
.body("createdDate", greaterThan(0))
.body("lastUpdatedDate", greaterThan(0))
.body("stageKeys", empty())
.body("tags.Team", equalTo("platform"))
.body("tags.Project", equalTo("demo"))
.body("value", nullValue());
@@ -70,6 +78,10 @@ class ApiGatewayApiKeyIntegrationTest {
.when().get("/apikeys")
.then()
.statusCode(200)
.body("item.find { it.id == '" + apiKeyId + "' }.createdDate", greaterThan(0))
.body("item.find { it.id == '" + apiKeyId + "' }.lastUpdatedDate", greaterThan(0))
.body("item.find { it.id == '" + apiKeyId + "' }.stageKeys", empty())
.body("item.find { it.id == '" + apiKeyId + "' }.customerId", equalTo("marketplace-customer-1"))
.body("item.find { it.id == '" + apiKeyId + "' }.tags.Team", equalTo("platform"))
.body("item.find { it.id == '" + apiKeyId + "' }.value", nullValue());
@@ -103,10 +115,35 @@ class ApiGatewayApiKeyIntegrationTest {
.then()
.statusCode(200)
.body("id", equalTo(apiKeyId))
.body("name", equalTo("updated-name"));
.body("name", equalTo("updated-name"))
.body("customerId", equalTo("marketplace-customer-1"))
.body("createdDate", greaterThan(0))
.body("lastUpdatedDate", greaterThan(0))
.body("stageKeys", empty())
.body("value", nullValue());
}
@Test @Order(7)
void updateApiKeyCustomerId() {
String body = """
{"patchOperations":[{"op":"replace","path":"/customerId","value":"marketplace-customer-2"}]}
""";
given()
.contentType(ContentType.JSON)
.body(body)
.when().patch("/apikeys/" + apiKeyId)
.then()
.statusCode(200)
.body("customerId", equalTo("marketplace-customer-2"));
given()
.when().get("/apikeys/" + apiKeyId)
.then()
.statusCode(200)
.body("customerId", equalTo("marketplace-customer-2"));
}
@Test @Order(8)
void updateApiKeyDescription() {
String body = """
{"patchOperations":[{"op":"replace","path":"/description","value":"a test key"}]}
@@ -120,7 +157,7 @@ class ApiGatewayApiKeyIntegrationTest {
.body("description", equalTo("a test key"));
}
@Test @Order(8)
@Test @Order(9)
void updateApiKeyEnabled() {
String body = """
{"patchOperations":[{"op":"replace","path":"/enabled","value":"false"}]}
@@ -134,7 +171,7 @@ class ApiGatewayApiKeyIntegrationTest {
.body("enabled", equalTo(false));
}
@Test @Order(9)
@Test @Order(10)
void updateApiKeyNotFound() {
String body = """
{"patchOperations":[{"op":"replace","path":"/name","value":"x"}]}
@@ -150,7 +187,7 @@ class ApiGatewayApiKeyIntegrationTest {
.body("message", equalTo("Invalid API Key identifier specified"));
}
@Test @Order(10)
@Test @Order(11)
void deleteApiKey() {
given()
.when().delete("/apikeys/" + apiKeyId)
@@ -158,7 +195,7 @@ class ApiGatewayApiKeyIntegrationTest {
.statusCode(202);
}
@Test @Order(11)
@Test @Order(12)
void deleteApiKeyAlreadyGone() {
given()
.when().delete("/apikeys/" + apiKeyId)
@@ -169,51 +206,57 @@ class ApiGatewayApiKeyIntegrationTest {
.body("message", equalTo("Invalid API Key identifier specified"));
}
@Test @Order(12)
@Test @Order(13)
void createApiKey_generateDistinctIdFalse_idEqualsValue() {
String body = """
{"name":"shared-id-key","enabled":true,"generateDistinctId":false}
{"name":"distinct-id-key-false","enabled":true,"generateDistinctId":false,"value":"11112222333344445555666677778888"}
""";
String sharedId = given()
String keyId = given()
.contentType(ContentType.JSON)
.body(body)
.when().post("/apikeys")
.then()
.statusCode(201)
.body("id", notNullValue())
.body("tags", anEmptyMap())
.body("stageKeys", empty())
.extract().path("id");
given()
.when().get("/apikeys/" + sharedId + "?includeValue=true")
.when().get("/apikeys/" + keyId + "?includeValue=true")
.then()
.statusCode(200)
.body("id", equalTo(sharedId))
.body("value", equalTo(sharedId));
}
@Test @Order(13)
void createApiKey_generateDistinctIdAbsent_idEqualsValue() {
String body = """
{"name":"default-id-key","enabled":true}
""";
String sharedId = given()
.contentType(ContentType.JSON)
.body(body)
.when().post("/apikeys")
.then()
.statusCode(201)
.body("id", notNullValue())
.extract().path("id");
given()
.when().get("/apikeys/" + sharedId + "?includeValue=true")
.then()
.statusCode(200)
.body("id", equalTo(sharedId))
.body("value", equalTo(sharedId));
.body("id", equalTo(keyId))
.body("value", equalTo("11112222333344445555666677778888"))
.body("value", equalTo(keyId))
.body("tags", anEmptyMap())
.body("stageKeys", empty());
}
@Test @Order(14)
void createApiKey_generateDistinctIdAbsent_idDiffersFromValue() {
String body = """
{"name":"default-id-key","enabled":true,"value":"99992222333344445555666677778888"}
""";
String keyId = given()
.contentType(ContentType.JSON)
.body(body)
.when().post("/apikeys")
.then()
.statusCode(201)
.body("id", notNullValue())
.extract().path("id");
given()
.when().get("/apikeys/" + keyId + "?includeValue=true")
.then()
.statusCode(200)
.body("id", equalTo(keyId))
.body("value", equalTo("99992222333344445555666677778888"))
.body("value", not(equalTo(keyId)));
}
@Test @Order(15)
void createApiKey_generateDistinctIdTrue_idDistinctFromValue() {
String body = """
{"name":"distinct-id-key","enabled":true,"generateDistinctId":true}
@@ -234,7 +277,7 @@ class ApiGatewayApiKeyIntegrationTest {
.body("id", equalTo(keyId))
.body("value", not(equalTo(keyId)));
}
@Test @Order(15)
@Test @Order(16)
void getDeletedApiKeyNotFound() {
given()
.when().get("/apikeys/" + apiKeyId)
@@ -91,7 +91,7 @@ class ApiGatewayApiKeyCfnIntegrationTest {
.body("description", equalTo("first"))
.body("enabled", equalTo(false))
.body("tags.stack", equalTo("v1"))
.body("value", equalTo(namedId));
.body("value", not(equalTo(namedId)));
// A key with no properties gets a CloudFormation-style generated name and AWS's defaults.
getApiKey(unnamedId)
@@ -87,6 +87,7 @@ class ApiGatewayApiKeyCfnProvisionerTest {
.thenReturn(key("abc123", "my-key", "0123456789abcdef0123", false, "a key", Map.of("team", "core")));
ObjectNode props = tags("team", "core")
.put("Name", "my-key")
.put("CustomerId", "marketplace-customer-1")
.put("Description", "a key")
.put("Enabled", "false")
.put("GenerateDistinctId", "true")
@@ -101,6 +102,7 @@ class ApiGatewayApiKeyCfnProvisionerTest {
ArgumentCaptor<Map<String, Object>> request = ArgumentCaptor.forClass(Map.class);
verify(apiGateway).createApiKey(eq(REGION), request.capture());
assertEquals("my-key", request.getValue().get("name"));
assertEquals("marketplace-customer-1", request.getValue().get("customerId"));
assertEquals("a key", request.getValue().get("description"));
assertEquals(Boolean.FALSE, request.getValue().get("enabled"));
assertEquals(Boolean.TRUE, request.getValue().get("generateDistinctId"));
@@ -126,9 +128,14 @@ class ApiGatewayApiKeyCfnProvisionerTest {
@Test
void updatePatchesDescriptionAndEnabledInPlace() {
ApiKey existing = key("abc123", "my-key", "abc123", false, "old", Map.of());
existing.setCustomerId("marketplace-customer-1");
when(apiGateway.findApiKey(REGION, "abc123")).thenReturn(Optional.of(existing));
when(apiGateway.updateApiKey(eq(REGION), eq("abc123"), anyList())).thenReturn(existing);
ObjectNode props = mapper.createObjectNode().put("Name", "my-key").put("Description", "new").put("Enabled", "true");
ObjectNode props = mapper.createObjectNode()
.put("Name", "my-key")
.put("CustomerId", "marketplace-customer-2")
.put("Description", "new")
.put("Enabled", "true");
StackResource r = resource("abc123");
provisioner.provision(r, props, ctx("abc123"));
@@ -141,6 +148,8 @@ class ApiGatewayApiKeyCfnProvisionerTest {
"/description".equals(op.get("path")) && "new".equals(op.get("value"))));
assertTrue(patches.getValue().stream().anyMatch(op ->
"/enabled".equals(op.get("path")) && "true".equals(op.get("value"))));
assertTrue(patches.getValue().stream().anyMatch(op ->
"/customerId".equals(op.get("path")) && "marketplace-customer-2".equals(op.get("value"))));
assertEquals("abc123", r.getPhysicalId());
assertEquals("abc123", r.getAttributes().get("APIKeyId"));
}