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

@@ -770,7 +770,9 @@ public class ApiGatewayController {
String region = regionResolver.resolveRegion(headers);
List<Map<String, String>> patchOperations = parsePatchOperations(body);
ApiKey key = service.updateApiKey(region, apiKeyId, patchOperations);
return Response.ok(toApiKeyNode(key).toString()).type(MediaType.APPLICATION_JSON).build();
ObjectNode node = toApiKeyNode(key);
node.remove("value");
return Response.ok(node.toString()).type(MediaType.APPLICATION_JSON).build();
}
@DELETE
@@ -2297,11 +2299,20 @@ public class ApiGatewayController {
node.put("name", k.getName());
node.put("value", k.getValue());
node.put("enabled", k.isEnabled());
node.put("createdDate", k.getCreatedDate());
node.put("lastUpdatedDate", k.getLastUpdatedDate());
if (k.getCustomerId() != null) {
node.put("customerId", k.getCustomerId());
}
if (k.getDescription() != null) {
node.put("description", k.getDescription());
}
if (k.getTags() != null && !k.getTags().isEmpty()) {
ObjectNode tags = node.putObject("tags");
ArrayNode stageKeys = node.putArray("stageKeys");
if (k.getStageKeys() != null) {
k.getStageKeys().forEach(stageKeys::add);
}
ObjectNode tags = node.putObject("tags");
if (k.getTags() != null) {
k.getTags().forEach(tags::put);
}
return node;
@@ -1003,22 +1003,29 @@ public class ApiGatewayService {
apiKey.setCreatedDate(System.currentTimeMillis() / 1000L);
apiKey.setLastUpdatedDate(apiKey.getCreatedDate());
apiKey.setDescription((String) request.get("description"));
apiKey.setCustomerId((String) request.get("customerId"));
boolean generateDistinctId = Boolean.TRUE.equals(request.get("generateDistinctId"));
String suppliedValue = (String) request.get("value");
String keyValue = (suppliedValue != null && !suppliedValue.isBlank())
? suppliedValue
: UUID.randomUUID().toString().replace("-", "");
boolean generateDistinctId = !Boolean.FALSE.equals(request.get("generateDistinctId"));
apiKey.setId(generateDistinctId ? shortId(10) : keyValue);
apiKey.setValue(keyValue);
if (!generateDistinctId) {
String sharedValue = (suppliedValue != null && !suppliedValue.isBlank())
? suppliedValue
: UUID.randomUUID().toString().replace("-", "");
apiKey.setId(sharedValue);
apiKey.setValue(sharedValue);
} else {
apiKey.setId(shortId(10));
apiKey.setValue((suppliedValue != null && !suppliedValue.isBlank())
? suppliedValue
: UUID.randomUUID().toString().replace("-", ""));
List<String> stageKeys = new ArrayList<>();
if (request.get("stageKeys") instanceof List<?> rawStageKeys) {
for (Object rawStageKey : rawStageKeys) {
if (rawStageKey instanceof Map<?, ?> stageKey) {
Object restApiId = stageKey.get("restApiId");
Object stageName = stageKey.get("stageName");
if (restApiId != null && stageName != null) {
stageKeys.add(restApiId + "/" + stageName);
}
}
}
}
apiKey.setStageKeys(stageKeys);
Map<String, String> tags = new HashMap<>();
if (request.get("tags") instanceof Map<?, ?> rawTags) {
@@ -1146,6 +1153,7 @@ public class ApiGatewayService {
case "/name" -> key.setName(op.get("value"));
case "/description" -> key.setDescription(op.get("value"));
case "/enabled" -> key.setEnabled(Boolean.parseBoolean(op.get("value")));
case "/customerId" -> key.setCustomerId(op.get("value"));
}
}
}
@@ -3,7 +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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RegisterForReflection
@@ -12,10 +14,12 @@ public class ApiKey {
private String id;
private String name;
private String value;
private String customerId;
private boolean enabled;
private long createdDate;
private long lastUpdatedDate;
private String description;
private List<String> stageKeys = new ArrayList<>();
private Map<String, String> tags = new HashMap<>();
public ApiKey() {}
@@ -29,6 +33,9 @@ public class ApiKey {
public String getValue() { return value; }
public void setValue(String value) { this.value = value; }
public String getCustomerId() { return customerId; }
public void setCustomerId(String customerId) { this.customerId = customerId; }
public boolean isEnabled() { return enabled; }
public void setEnabled(boolean enabled) { this.enabled = enabled; }
@@ -41,6 +48,9 @@ public class ApiKey {
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
public List<String> getStageKeys() { return stageKeys; }
public void setStageKeys(List<String> stageKeys) { this.stageKeys = stageKeys != null ? stageKeys : new ArrayList<>(); }
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags != null ? tags : new HashMap<>(); }
}
@@ -23,8 +23,7 @@ import java.util.Set;
* <p>Description, Enabled and Tags update in place. Name, Value and GenerateDistinctId are
* createOnly in the registry schema: AWS replaces the key for a change to any of them, and this
* provisioner has no replacement path, so such a change is reported rather than applied to a key
* whose value callers already hold. CustomerId and the deprecated StageKeys have no counterpart in
* {@link ApiKey} and are accepted without effect.
* whose value callers already hold. The deprecated StageKeys property is accepted without effect.
*/
@ApplicationScoped
public class ApiGatewayApiKeyCfnProvisioner implements CfnResourceProvisioner {
@@ -69,6 +68,10 @@ public class ApiGatewayApiKeyCfnProvisioner implements CfnResourceProvisioner {
Map<String, Object> request = new HashMap<>();
request.put("name", name);
request.put("description", ctx.resolveOptional(props, "Description"));
String customerId = ctx.resolveOptional(props, "CustomerId");
if (customerId != null && !customerId.isBlank()) {
request.put("customerId", customerId);
}
Boolean enabled = resolveBoolean(props, "Enabled", ctx);
if (enabled != null) {
request.put("enabled", enabled);
@@ -97,15 +100,19 @@ public class ApiGatewayApiKeyCfnProvisioner implements CfnResourceProvisioner {
if (value != null && !value.isBlank()) {
rejectIfChanged("Value", existing.getValue(), value);
}
// createApiKey gives a key with GenerateDistinctId=false its value as its id, so whether the
// two differ records which mode the key was created in.
boolean requestedDistinct = Boolean.TRUE.equals(resolveBoolean(props, "GenerateDistinctId", ctx));
boolean existingDistinct = !Objects.equals(existing.getId(), existing.getValue());
if (requestedDistinct != existingDistinct) {
throw replacementNotSupported("GenerateDistinctId");
Boolean requestedDistinct = resolveBoolean(props, "GenerateDistinctId", ctx);
if (requestedDistinct != null) {
boolean existingDistinct = !Objects.equals(existing.getId(), existing.getValue());
if (requestedDistinct != existingDistinct) {
throw replacementNotSupported("GenerateDistinctId");
}
}
List<Map<String, String>> patches = new ArrayList<>();
String customerId = ctx.resolveOptional(props, "CustomerId");
if (!Objects.equals(blankToNull(customerId), blankToNull(existing.getCustomerId()))) {
patches.add(patch("/customerId", customerId));
}
String description = ctx.resolveOptional(props, "Description");
if (!Objects.equals(blankToNull(description), blankToNull(existing.getDescription()))) {
patches.add(patch("/description", description));