mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(apigateway): return stored method, policy, and stage settings (#3857)
* fix(apigateway): return stored method, policy, and stage settings Store and return method request parameters, the REST API policy, and stage access log settings, tracing, and tags. These writes were accepted but never saved, so Terraform planned the same changes on every run. Support the patch paths Terraform uses for these fields. Return the policy escaped the same way AWS does. Tag stages by their own ARN instead of tagging the REST API. Add REST regression coverage. * test(apigateway): add Terraform compat test for REST read-back Apply a REST API gateway module with Terraform, then check that it converges. Check that the method, policy, and stage settings read back after apply. Check that a second apply succeeds and the plan after it has no changes. Check that changing a request parameter or a stage tag applies and re-plans clean. Use the deployment trigger hash from the bug report, which used to fail with "Provider produced inconsistent final plan".
This commit is contained in:
1 parent
e32266537e
commit
004d562f34
9 files changed
+701
-3
No files matched your search
+31
@@ -10,6 +10,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.fasterxml.jackson.core.io.JsonStringEncoder;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
@@ -2145,9 +2146,21 @@ public class ApiGatewayController {
|
||||
node.put("apiKeySource", "HEADER");
|
||||
node.put("disableExecuteApiEndpoint", false);
|
||||
|
||||
// AWS returns the policy JSON-escaped inside the string ({\"Version\":...}), and the
|
||||
// Terraform provider unquotes it on read, so a raw document here would fail to parse.
|
||||
if (api.getPolicy() != null) {
|
||||
node.put("policy", escapePolicy(api.getPolicy()));
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
// Full JSON string escaping, not just quotes: a pretty-printed policy carries newlines, and
|
||||
// those must be escaped too or the provider's quote-and-unquote step fails to parse it.
|
||||
private static String escapePolicy(String policy) {
|
||||
return new String(JsonStringEncoder.getInstance().quoteAsString(policy));
|
||||
}
|
||||
|
||||
private ObjectNode toResourceNode(ApiGatewayResource r) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("id", r.getId());
|
||||
@@ -2164,6 +2177,10 @@ public class ApiGatewayController {
|
||||
node.put("apiKeyRequired", m.isApiKeyRequired());
|
||||
if (m.getAuthorizerId() != null) node.put("authorizerId", m.getAuthorizerId());
|
||||
if (m.getRequestValidatorId() != null) node.put("requestValidatorId", m.getRequestValidatorId());
|
||||
if (m.getRequestParameters() != null && !m.getRequestParameters().isEmpty()) {
|
||||
ObjectNode params = node.putObject("requestParameters");
|
||||
m.getRequestParameters().forEach(params::put);
|
||||
}
|
||||
if (m.getRequestModels() != null && !m.getRequestModels().isEmpty()) {
|
||||
ObjectNode models = objectMapper.createObjectNode();
|
||||
m.getRequestModels().forEach(models::put);
|
||||
@@ -2252,6 +2269,20 @@ public class ApiGatewayController {
|
||||
node.put("cacheClusterEnabled", s.isCacheClusterEnabled());
|
||||
node.put("cacheClusterStatus", s.getCacheClusterStatus());
|
||||
if (s.getCacheClusterSize() != null) node.put("cacheClusterSize", s.getCacheClusterSize());
|
||||
node.put("tracingEnabled", s.isTracingEnabled());
|
||||
if (s.getAccessLogSettings() != null) {
|
||||
ObjectNode logs = node.putObject("accessLogSettings");
|
||||
if (s.getAccessLogSettings().destinationArn() != null) {
|
||||
logs.put("destinationArn", s.getAccessLogSettings().destinationArn());
|
||||
}
|
||||
if (s.getAccessLogSettings().format() != null) {
|
||||
logs.put("format", s.getAccessLogSettings().format());
|
||||
}
|
||||
}
|
||||
if (!s.getTags().isEmpty()) {
|
||||
ObjectNode tags = node.putObject("tags");
|
||||
s.getTags().forEach(tags::put);
|
||||
}
|
||||
if (!s.getVariables().isEmpty()) {
|
||||
ObjectNode vars = node.putObject("variables");
|
||||
s.getVariables().forEach(vars::put);
|
||||
|
||||
+77
-1
@@ -232,6 +232,9 @@ public class ApiGatewayService {
|
||||
api.setBinaryMediaTypes(binaryTypes.stream()
|
||||
.filter(String.class::isInstance).map(String.class::cast).toList());
|
||||
}
|
||||
if (request.get("policy") instanceof String policy && !policy.isEmpty()) {
|
||||
api.setPolicy(policy);
|
||||
}
|
||||
|
||||
EndpointConfiguration endpointConfiguration = new EndpointConfiguration();
|
||||
if (request.get(EPC_KEY) instanceof Map<?, ?> epMap) {
|
||||
@@ -775,6 +778,18 @@ public class ApiGatewayService {
|
||||
stage.setCacheClusterSize((String) request.getOrDefault("cacheClusterSize", "0.5"));
|
||||
}
|
||||
|
||||
stage.setTracingEnabled(Boolean.TRUE.equals(request.get("tracingEnabled")));
|
||||
if (request.get("tags") instanceof Map<?, ?> tags) {
|
||||
tags.forEach((k, v) -> {
|
||||
if (k instanceof String key && v instanceof String val) stage.getTags().put(key, val);
|
||||
});
|
||||
}
|
||||
if (request.get("accessLogSettings") instanceof Map<?, ?> logs) {
|
||||
stage.setAccessLogSettings(new Stage.AccessLogSettings(
|
||||
logs.get("destinationArn") instanceof String arn ? arn : null,
|
||||
logs.get("format") instanceof String format ? format : null));
|
||||
}
|
||||
|
||||
stageStore.put(stageKey(region, apiId, stageName), stage);
|
||||
LOG.infov("Created stage {0} for API {1}", stageName, apiId);
|
||||
return stage;
|
||||
@@ -803,12 +818,24 @@ public class ApiGatewayService {
|
||||
String value = op.get("value");
|
||||
LOG.infov("Patch operation: op={0}, path={1}, value={2}", opType, path, value);
|
||||
|
||||
if ("remove" .equals(opType) && "/accessLogSettings" .equals(path)) {
|
||||
stage.setAccessLogSettings(null);
|
||||
continue;
|
||||
}
|
||||
if (!"replace" .equals(opType) && !"add" .equals(opType)) continue;
|
||||
|
||||
if ("/description" .equals(path)) {
|
||||
stage.setDescription(value);
|
||||
} else if ("/deploymentId" .equals(path)) {
|
||||
stage.setDeploymentId(value);
|
||||
} else if ("/tracingEnabled" .equals(path)) {
|
||||
stage.setTracingEnabled(Boolean.parseBoolean(value));
|
||||
} else if ("/accessLogSettings/destinationArn" .equals(path)) {
|
||||
Stage.AccessLogSettings logs = stage.getAccessLogSettings();
|
||||
stage.setAccessLogSettings(new Stage.AccessLogSettings(value, logs != null ? logs.format() : null));
|
||||
} else if ("/accessLogSettings/format" .equals(path)) {
|
||||
Stage.AccessLogSettings logs = stage.getAccessLogSettings();
|
||||
stage.setAccessLogSettings(new Stage.AccessLogSettings(logs != null ? logs.destinationArn() : null, value));
|
||||
} else if (path.startsWith("/variables/")) {
|
||||
String varKey = path.substring("/variables/" .length());
|
||||
LOG.infov("Setting stage variable {0} = {1}", varKey, value);
|
||||
@@ -2264,6 +2291,9 @@ public class ApiGatewayService {
|
||||
String value = op.get("value");
|
||||
if ("/name" .equals(path)) api.setName(value);
|
||||
else if ("/description" .equals(path)) api.setDescription(value);
|
||||
// aws_api_gateway_rest_api_policy writes the policy this way, and clears it by
|
||||
// replacing it with an empty string.
|
||||
else if ("/policy" .equals(path)) api.setPolicy(value == null || value.isEmpty() ? null : value);
|
||||
}
|
||||
}
|
||||
apiStore.put(apiKey(region, apiId), api);
|
||||
@@ -2418,14 +2448,44 @@ public class ApiGatewayService {
|
||||
throw new AwsException("BadRequestException", "Invalid boolean value '" + value + "' for apiKeyRequired. Must be 'true' or 'false'", 400);
|
||||
}
|
||||
}
|
||||
// Checked here, before anything is applied: the method is the live stored object,
|
||||
// so a failure halfway through the apply loop would leave earlier ops in place.
|
||||
if (path.startsWith("/requestParameters/") && ("add".equals(opType) || "replace".equals(opType))) {
|
||||
validateRequestParameterNames(
|
||||
Map.of(unescapeJsonPointer(path.substring("/requestParameters/".length())), Boolean.TRUE));
|
||||
}
|
||||
}
|
||||
for (Map<String, String> op : patchOperations) {
|
||||
if (!"replace".equals(op.get("op"))) continue;
|
||||
String opType = op.get("op");
|
||||
String path = op.getOrDefault("path", "");
|
||||
String value = op.get("value");
|
||||
// Terraform patches request parameters and models one entry at a time with
|
||||
// add/replace/remove, so those maps are handled before the replace-only members.
|
||||
if (path.startsWith("/requestParameters/")) {
|
||||
String name = unescapeJsonPointer(path.substring("/requestParameters/".length()));
|
||||
if ("remove".equals(opType)) {
|
||||
method.getRequestParameters().remove(name);
|
||||
} else if ("add".equals(opType) || "replace".equals(opType)) {
|
||||
method.getRequestParameters().put(name, Boolean.parseBoolean(value));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (path.startsWith("/requestModels/")) {
|
||||
String contentType = unescapeJsonPointer(path.substring("/requestModels/".length()));
|
||||
if ("remove".equals(opType)) {
|
||||
method.getRequestModels().remove(contentType);
|
||||
} else if (("add".equals(opType) || "replace".equals(opType)) && value != null) {
|
||||
method.getRequestModels().put(contentType, value);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (!"replace".equals(opType)) continue;
|
||||
if ("/authorizationType".equals(path)) method.setAuthorizationType(value);
|
||||
else if ("/authorizerId".equals(path)) method.setAuthorizerId(value);
|
||||
else if ("/apiKeyRequired".equals(path)) method.setApiKeyRequired(Boolean.parseBoolean(value));
|
||||
else if ("/requestValidatorId".equals(path)) {
|
||||
method.setRequestValidatorId(value == null || value.isEmpty() ? null : value);
|
||||
}
|
||||
}
|
||||
}
|
||||
resourceStore.put(resourceKey(region, apiId, resourceId), getResource(region, apiId, resourceId));
|
||||
@@ -2499,6 +2559,22 @@ public class ApiGatewayService {
|
||||
apiStore.put(apiKey(region, apiId), api);
|
||||
}
|
||||
|
||||
public Map<String, String> getStageTags(String region, String apiId, String stageName) {
|
||||
return getStage(region, apiId, stageName).getTags();
|
||||
}
|
||||
|
||||
public void tagStage(String region, String apiId, String stageName, Map<String, String> tags) {
|
||||
Stage stage = getStage(region, apiId, stageName);
|
||||
stage.getTags().putAll(tags);
|
||||
stageStore.put(stageKey(region, apiId, stageName), stage);
|
||||
}
|
||||
|
||||
public void untagStage(String region, String apiId, String stageName, List<String> tagKeys) {
|
||||
Stage stage = getStage(region, apiId, stageName);
|
||||
tagKeys.forEach(stage.getTags()::remove);
|
||||
stageStore.put(stageKey(region, apiId, stageName), stage);
|
||||
}
|
||||
|
||||
public Map<String, String> getDomainNameTags(String region, String domainName) {
|
||||
synchronized (domainNameLock) {
|
||||
Map<String, String> tags = getDomainName(region, domainName).getTags();
|
||||
|
||||
+26
-2
@@ -20,6 +20,7 @@ import java.util.Map;
|
||||
public class ApiGatewayTagHandler implements TagHandler {
|
||||
|
||||
private static final String DOMAIN_NAMES = "/domainnames/";
|
||||
private static final String STAGES = "/stages/";
|
||||
|
||||
private final ApiGatewayService service;
|
||||
|
||||
@@ -41,16 +42,23 @@ public class ApiGatewayTagHandler implements TagHandler {
|
||||
@Override
|
||||
public Map<String, String> listTags(String region, String arn) {
|
||||
String domainName = domainNameFromArn(arn);
|
||||
return domainName != null
|
||||
? service.getDomainNameTags(region, domainName)
|
||||
if (domainName != null) {
|
||||
return service.getDomainNameTags(region, domainName);
|
||||
}
|
||||
String stageName = stageNameFromArn(arn);
|
||||
return stageName != null
|
||||
? service.getStageTags(region, apiIdFromArn(arn), stageName)
|
||||
: service.getTags(region, apiIdFromArn(arn));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tagResource(String region, String arn, Map<String, String> tags) {
|
||||
String domainName = domainNameFromArn(arn);
|
||||
String stageName = stageNameFromArn(arn);
|
||||
if (domainName != null) {
|
||||
service.tagDomainName(region, domainName, tags);
|
||||
} else if (stageName != null) {
|
||||
service.tagStage(region, apiIdFromArn(arn), stageName, tags);
|
||||
} else {
|
||||
service.tagResource(region, apiIdFromArn(arn), tags);
|
||||
}
|
||||
@@ -59,8 +67,11 @@ public class ApiGatewayTagHandler implements TagHandler {
|
||||
@Override
|
||||
public void untagResource(String region, String arn, List<String> tagKeys) {
|
||||
String domainName = domainNameFromArn(arn);
|
||||
String stageName = stageNameFromArn(arn);
|
||||
if (domainName != null) {
|
||||
service.untagDomainName(region, domainName, tagKeys);
|
||||
} else if (stageName != null) {
|
||||
service.untagStage(region, apiIdFromArn(arn), stageName, tagKeys);
|
||||
} else {
|
||||
service.untagResource(region, apiIdFromArn(arn), tagKeys);
|
||||
}
|
||||
@@ -74,6 +85,19 @@ public class ApiGatewayTagHandler implements TagHandler {
|
||||
return parts[1].split("/")[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* The stage a {@code /restapis/<apiId>/stages/<stageName>} ARN names, or null for any other ARN.
|
||||
* Stages carry their own tags; without this they would land on the REST API.
|
||||
*/
|
||||
private static String stageNameFromArn(String arn) {
|
||||
int at = arn.indexOf(STAGES);
|
||||
if (at < 0 || !arn.contains("/restapis/")) {
|
||||
return null;
|
||||
}
|
||||
String stageName = arn.substring(at + STAGES.length());
|
||||
return stageName.isEmpty() || stageName.contains("/") ? null : stageName;
|
||||
}
|
||||
|
||||
/**
|
||||
* The domain a {@code /domainnames/<name>} ARN names, or null for any other ARN. A base path
|
||||
* mapping ARN continues past the domain and is not taggable, so it falls through to the REST API
|
||||
|
||||
@@ -22,6 +22,16 @@ public class RestApi {
|
||||
private EndpointConfiguration endpointConfiguration;
|
||||
/** Content types treated as binary when applying integration contentHandling. */
|
||||
private List<String> binaryMediaTypes = new ArrayList<>();
|
||||
/** Resource policy document, stored exactly as the caller sent it. */
|
||||
private String policy;
|
||||
|
||||
public String getPolicy() {
|
||||
return policy;
|
||||
}
|
||||
|
||||
public void setPolicy(String policy) {
|
||||
this.policy = policy;
|
||||
}
|
||||
|
||||
public List<String> getBinaryMediaTypes() {
|
||||
return binaryMediaTypes;
|
||||
|
||||
@@ -20,6 +20,36 @@ public class Stage {
|
||||
private boolean cacheClusterEnabled;
|
||||
private String cacheClusterSize;
|
||||
private String cacheClusterStatus = "NOT_AVAILABLE";
|
||||
private AccessLogSettings accessLogSettings;
|
||||
private boolean tracingEnabled;
|
||||
private Map<String, String> tags = new HashMap<>();
|
||||
|
||||
@RegisterForReflection
|
||||
public record AccessLogSettings(String destinationArn, String format) {}
|
||||
|
||||
public AccessLogSettings getAccessLogSettings() {
|
||||
return accessLogSettings;
|
||||
}
|
||||
|
||||
public void setAccessLogSettings(AccessLogSettings accessLogSettings) {
|
||||
this.accessLogSettings = accessLogSettings;
|
||||
}
|
||||
|
||||
public boolean isTracingEnabled() {
|
||||
return tracingEnabled;
|
||||
}
|
||||
|
||||
public void setTracingEnabled(boolean tracingEnabled) {
|
||||
this.tracingEnabled = tracingEnabled;
|
||||
}
|
||||
|
||||
public Map<String, String> getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
public void setTags(Map<String, String> tags) {
|
||||
this.tags = tags != null ? tags : new HashMap<>();
|
||||
}
|
||||
|
||||
public boolean isCacheClusterEnabled() {
|
||||
return cacheClusterEnabled;
|
||||
|
||||
Reference in new issue
Block a user