Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(cloudformation): provision a usage plan's throttle, quota and stage throttles (#6419)

Mario Toffiae09c0b648f96
diff --git a/docs/services/api-gateway.md b/docs/services/api-gateway.md
index 69ef92a5aa..44bfc94846 100644
--- a/docs/services/api-gateway.md
+++ b/docs/services/api-gateway.md
@@ -592,7 +592,8 @@ Usage plan product codes are not supported, so an `add`, `replace` or `remove` o
`BadRequestException`.
Values are validated with API Gateway's messages: a quota needs a `DAY`, `WEEK` or `MONTH` period and
an offset inside it, a per-method throttle needs a plan throttle, and adding a stage the plan already
-lists is a `ConflictException`.
+lists is a `ConflictException`. `AWS::ApiGateway::UsagePlan` maps its `ApiStages`, with each stage's
+`Throttle`, and its `Throttle` and `Quota` the same way, and an update replaces them.
Floci does not check that a stage's API, stage or method exists, does not filter `GetUsagePlans` by
`keyId`, and does not enforce throttles or quotas.
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.java
index 49498948d6..b82c6d6488 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
@@ -80,6 +80,7 @@ import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
+import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Pattern;
@@ -133,9 +134,10 @@ public class ApiGatewayService implements ResourceProvider {
private final Object domainNameLock = new Object();
private final Object vpcLinkTagLock = new Object();
/**
- * Guards a usage plan patch and delete. A patch writes back the stages, throttle and quota it
- * read, so two at once would restore one another's stale values, and a delete between its read
- * and its write would be undone.
+ * Guards a usage plan patch, its delete, the CloudFormation limits replacement, the rollback
+ * restore and the {@code readUsagePlan} read. A patch writes back what it read, so two at once
+ * would restore one another's stale values, and a delete between its read and its write would
+ * be undone.
*/
private final Object usagePlanLock = new Object();
private final TlsCertificateManager certificateManager;
@@ -1546,6 +1548,13 @@ public class ApiGatewayService implements ResourceProvider {
.orElseThrow(() -> new AwsException("NotFoundException", "Invalid Usage Plan ID specified", 404));
}
+ /** Reads the stored plan under the lock patches hold, so the reader sees one revision of it. */
+ public <T> T readUsagePlan(String region, String usagePlanId, Function<UsagePlan, T> reader) {
+ synchronized (usagePlanLock) {
+ return reader.apply(getUsagePlan(region, usagePlanId));
+ }
+ }
+
public UsagePlan updateUsagePlan(String region, String usagePlanId, List<Map<String, String>> patchOperations) {
synchronized (usagePlanLock) {
UsagePlan plan = getUsagePlan(region, usagePlanId);
@@ -1626,6 +1635,37 @@ public class ApiGatewayService implements ResourceProvider {
return plan;
}
+ /**
+ * Replaces a usage plan's stages, throttle and quota wholesale. CloudFormation drives them to the
+ * template's desired state on update, as it does tags, so a stage the template dropped has to go.
+ */
+ public UsagePlan replaceUsagePlanLimits(String region, String usagePlanId, UsagePlan.Limits limits) {
+ synchronized (usagePlanLock) {
+ UsagePlan plan = getUsagePlan(region, usagePlanId);
+ plan.applyLimits(limits);
+ usagePlanStore.put(usagePlanKey(region, usagePlanId), plan);
+ return plan;
+ }
+ }
+
+ /**
+ * Sets a usage plan's name, description, stages, throttle, quota and tags to exactly the given
+ * values, a null description included, which no patch can set. It is for putting back the plan
+ * as it was before a failed CloudFormation update changed it.
+ */
+ public UsagePlan restoreUsagePlan(String region, String usagePlanId, String name, String description,
+ UsagePlan.Limits limits, Map<String, String> tags) {
+ synchronized (usagePlanLock) {
+ UsagePlan plan = getUsagePlan(region, usagePlanId);
+ plan.setName(name);
+ plan.setDescription(description);
+ plan.applyLimits(limits);
+ plan.setTags(new HashMap<>(tags));
+ usagePlanStore.put(usagePlanKey(region, usagePlanId), plan);
+ return plan;
+ }
+ }
+
public void tagUsagePlan(String region, String usagePlanId, Map<String, String> tags) {
ReservedTags.rejectApiGatewayReservedTagsOnUpdate(tags);
Map<String, String> merged = new HashMap<>(getUsagePlan(region, usagePlanId).getTags());
diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/UsagePlanLimits.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/UsagePlanLimits.java
index b612d425cc..1987a3a2da 100644
--- a/src/main/java/io/github/hectorvent/floci/services/apigateway/UsagePlanLimits.java
+++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/UsagePlanLimits.java
@@ -66,8 +66,9 @@ public final class UsagePlanLimits {
/**
* The stages, throttle and quota of a {@code CreateUsagePlan} request whose JSON types
- * {@link ApiGatewayRequestShapes} has checked. A JSON number is read as API Gateway reads it, and
- * text, which only CloudFormation's template values send, is parsed as written.
+ * {@link ApiGatewayRequestShapes} has checked, or of the same shape as CloudFormation's provisioner
+ * builds it. A JSON number is read as API Gateway reads it, and text, which only CloudFormation's
+ * template values send, is parsed as written.
*/
public static Limits fromRequest(Map<String, Object> request) {
Map<?, ?> quotaSettings = request.get("quota") instanceof Map<?, ?> settings ? settings : null;
diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/model/UsagePlan.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/model/UsagePlan.java
index b3d134bd9e..2b3735785a 100644
--- a/src/main/java/io/github/hectorvent/floci/services/apigateway/model/UsagePlan.java
+++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/model/UsagePlan.java
@@ -72,7 +72,7 @@ public class UsagePlan {
@RegisterForReflection
public record Quota(int limit, int offset, String period) {}
- /** A plan's stages, throttle and quota as one value. */
+ /** A plan's stages, throttle and quota, which CloudFormation replaces as one. */
@RegisterForReflection
public record Limits(List<ApiStage> apiStages, Throttle throttle, Quota quota) {}
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayApiKeyCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayApiKeyCfnProvisioner.java
index 08915a563b..b93ee9e16c 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayApiKeyCfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayApiKeyCfnProvisioner.java
@@ -1,6 +1,5 @@
package io.github.hectorvent.floci.services.cloudformation.provisioners;
-import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
@@ -67,22 +66,10 @@ public class ApiGatewayApiKeyCfnProvisioner implements CfnResourceProvisioner {
}
create(r, props, ctx);
if (ctx.isUpdate()) {
- recordCreatedByUpdate(r, ctx);
+ CfnRollback.recordCreatedByUpdate(r, ctx, CfnRollback.API_KEY_UPDATE_SNAPSHOT_ATTR);
}
}
- /**
- * Records that this update created the key because the one at the prior physical id was gone,
- * so {@link #rollbackUpdate} can delete it and name the prior id again.
- */
- private static void recordCreatedByUpdate(StackResource r, ProvisionContext ctx) {
- ObjectNode snapshot = MAPPER.createObjectNode();
- snapshot.put("region", ctx.region());
- snapshot.put("createdId", r.getPhysicalId());
- snapshot.put("priorId", ctx.priorPhysicalId());
- r.getAttributes().put(CfnRollback.API_KEY_UPDATE_SNAPSHOT_ATTR, snapshot.toString());
- }
-
private void create(StackResource r, JsonNode props, ProvisionContext ctx) {
String name = ctx.resolveOptional(props, "Name");
if (name == null || name.isBlank()) {
@@ -143,29 +130,15 @@ public class ApiGatewayApiKeyCfnProvisioner implements CfnResourceProvisioner {
// Tags go first: the service refuses a tag change that adds a reserved tag key, and refusing
// it before the patch leaves the key exactly as the stack last committed it.
- ApiKey key = existing;
- try {
+ CfnRollback.guarded(r, ctx, false, () -> { }, () -> rollbackUpdate(r), () -> {
if (tagsChanged) {
- key = apiGatewayService.replaceApiKeyTags(ctx.region(), existing.getId(), desiredTags);
+ apiGatewayService.replaceApiKeyTags(ctx.region(), existing.getId(), desiredTags);
}
if (!patches.isEmpty()) {
- key = apiGatewayService.updateApiKey(ctx.region(), existing.getId(), patches);
- }
- } catch (RuntimeException failure) {
- // The stack skips rollbackUpdate for a resource whose own update failed, so undo it here.
- try {
- rollbackUpdate(r);
- } catch (RuntimeException restoreFailure) {
- r.getAttributes().put(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR,
- "Could not roll back the update of API key " + existing.getId() + ": "
- + restoreFailure.getMessage());
- if (restoreFailure != failure) {
- failure.addSuppressed(restoreFailure);
- }
+ apiGatewayService.updateApiKey(ctx.region(), existing.getId(), patches);
}
- throw failure;
- }
- record(r, key);
+ });
+ record(r, existing);
}
/**
@@ -249,14 +222,13 @@ public class ApiGatewayApiKeyCfnProvisioner implements CfnResourceProvisioner {
}
/**
- * Undoes the update from the snapshot on the resource, and is also called by {@code update} when
- * one of its own mutating calls fails. When the update created the key because
- * the one at the prior physical id was gone, the created key is deleted and the resource names
- * the prior id again. Otherwise the customer id, description, enabled flag and tags are put
- * back, writing only the values where the live key differs from the snapshot, so an update that
- * failed before it changed anything writes nothing. With no snapshot on the resource the update
- * changed nothing, since every mutating call is preceded by the snapshot or followed by the
- * creation record, so there is nothing to undo.
+ * Undoes the update from the snapshot on the resource, and is also the in-place undo
+ * {@code update} hands to {@link CfnRollback#guarded}. A key the update created is removed by
+ * {@link CfnRollback#undoCreatedByUpdate}. Otherwise the customer id, description, enabled flag
+ * and tags are put back, writing only the values where the live key differs from the snapshot,
+ * so an update that failed before it changed anything writes nothing. With no snapshot on the
+ * resource the update changed nothing, since every mutating call is preceded by the snapshot or
+ * followed by the creation record, so there is nothing to undo.
* A key deleted since the update fails the rollback with NotFoundException, and the stack
* reports the rollback as failed.
*/
@@ -264,29 +236,15 @@ public class ApiGatewayApiKeyCfnProvisioner implements CfnResourceProvisioner {
public boolean rollbackUpdate(StackResource resource) {
// The snapshot is spent only once the restore succeeded: a restore that throws leaves it in
// place for the next attempt instead of reporting a rollback that never happened.
- String raw = resource.getAttributes().get(CfnRollback.API_KEY_UPDATE_SNAPSHOT_ATTR);
- if (raw == null) {
+ JsonNode snapshot = CfnRollback.updateSnapshot(resource, CfnRollback.API_KEY_UPDATE_SNAPSHOT_ATTR);
+ if (snapshot == null) {
return true;
}
- JsonNode snapshot;
- try {
- snapshot = MAPPER.readTree(raw);
- } catch (JsonProcessingException e) {
- throw new IllegalStateException("Could not read the API key update snapshot for "
- + resource.getLogicalId(), e);
- }
- String region = snapshot.path("region").asText();
- String createdId = snapshotText(snapshot, "createdId");
- if (createdId != null) {
- // Deleted before the resource names the prior id again, so a failed delete leaves the
- // resource naming the created key and deleting the stack still removes it.
- delete(TYPE, createdId, region);
- String priorId = snapshot.path("priorId").asText();
- resource.setPhysicalId(priorId);
- resource.getAttributes().put("APIKeyId", priorId);
- resource.getAttributes().remove(CfnRollback.API_KEY_UPDATE_SNAPSHOT_ATTR);
+ if (CfnRollback.undoCreatedByUpdate(resource, snapshot, CfnRollback.API_KEY_UPDATE_SNAPSHOT_ATTR,
+ "APIKeyId", (createdId, region) -> delete(TYPE, createdId, region))) {
return true;
}
+ String region = snapshot.path("region").asText();
String id = snapshot.path("id").asText();
ApiKey live = apiGatewayService.getApiKey(region, id);
Map<String, String> tags = new HashMap<>();
@@ -294,18 +252,12 @@ public class ApiGatewayApiKeyCfnProvisioner implements CfnResourceProvisioner {
if (!tags.equals(live.getTags())) {
apiGatewayService.restoreApiKeyTags(region, id, tags);
}
- List<Map<String, String>> patches = patchesTo(live, snapshotText(snapshot, "customerId"),
- snapshotText(snapshot, "description"), snapshot.path("enabled").asBoolean());
+ List<Map<String, String>> patches = patchesTo(live, CfnRollback.snapshotText(snapshot, "customerId"),
+ CfnRollback.snapshotText(snapshot, "description"), snapshot.path("enabled").asBoolean());
if (!patches.isEmpty()) {
apiGatewayService.updateApiKey(region, id, patches);
}
resource.getAttributes().remove(CfnRollback.API_KEY_UPDATE_SNAPSHOT_ATTR);
return true;
}
-
- /** A snapshotted string, or null when the key carried none. */
- private static String snapshotText(JsonNode snapshot, String field) {
- JsonNode value = snapshot.get(field);
- return value == null || value.isNull() ? null : value.asText();
- }
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayUsagePlanCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayUsagePlanCfnProvisioner.java
index 421425cc42..d6c4900da4 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayUsagePlanCfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayUsagePlanCfnProvisioner.java
@@ -1,9 +1,13 @@
package io.github.hectorvent.floci.services.cloudformation.provisioners;
import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.JsonNodeType;
+import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.ReservedTags;
import io.github.hectorvent.floci.services.apigateway.ApiGatewayService;
+import io.github.hectorvent.floci.services.apigateway.UsagePlanLimits;
import io.github.hectorvent.floci.services.apigateway.model.UsagePlan;
import io.github.hectorvent.floci.services.apigateway.model.UsagePlanKey;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
@@ -13,6 +17,7 @@ import org.jboss.logging.Logger;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -23,9 +28,10 @@ import java.util.Set;
* {@code AWS::ApiGateway::UsagePlanKey}, backed by {@link ApiGatewayService}.
*
* <p>A usage plan's {@code Ref} and {@code Fn::GetAtt Id} are the plan id. Every plan property is
- * mutable, so an update patches the plan in place, adding and removing API stages as the template
- * moves them. Throttle and Quota, on the plan and per stage, have no counterpart in
- * {@link UsagePlan} and are accepted without effect.
+ * mutable, so an update patches the name and description in place and replaces the API stages,
+ * throttle and quota together when any of them changes. The values the plan had are kept on the
+ * resource so a failed stack update can put them all back. Throttle and quota are stored and
+ * reported, not enforced.
*
* <p>A usage plan key's {@code Ref} and {@code Fn::GetAtt Id} are {@code <keyId>:<usagePlanId>},
* as on AWS. All three of its properties are createOnly, so a change to any of them is refused as a
@@ -39,6 +45,7 @@ public class ApiGatewayUsagePlanCfnProvisioner implements CfnResourceProvisioner
private static final String USAGE_PLAN = "AWS::ApiGateway::UsagePlan";
private static final String USAGE_PLAN_KEY = "AWS::ApiGateway::UsagePlanKey";
private static final String NOT_FOUND = "NotFoundException";
+ private static final ObjectMapper MAPPER = new ObjectMapper();
private static final String KEY_ID_SEPARATOR = ":";
/** The registry schema's only allowed KeyType. */
private static final String API_KEY_TYPE = "API_KEY";
@@ -70,6 +77,8 @@ public class ApiGatewayUsagePlanCfnProvisioner implements CfnResourceProvisioner
// ──────────────────────────── UsagePlan ────────────────────────────
private void provisionUsagePlan(StackResource r, JsonNode props, ProvisionContext ctx) {
+ // A snapshot describes the update in flight; one an earlier update left behind is stale.
+ r.getAttributes().remove(CfnRollback.USAGE_PLAN_UPDATE_SNAPSHOT_ATTR);
if (ctx.isUpdate()) {
// An update re-invokes provision with the prior physical id. Creating unconditionally
// would mint a second plan on every update and orphan the first, which then outlives
@@ -84,23 +93,20 @@ public class ApiGatewayUsagePlanCfnProvisioner implements CfnResourceProvisioner
if (name == null || name.isBlank()) {
name = ctx.generatePhysicalName(r.getLogicalId(), GENERATED_NAME_MAX, false);
}
- Map<String, Object> request = new HashMap<>();
+ Map<String, Object> request = limitsRequest(props, ctx);
request.put("name", name);
request.put("description", ctx.resolveOptional(props, "Description"));
- List<Map<String, Object>> apiStages = new ArrayList<>();
- for (UsagePlan.ApiStage stage : apiStages(props, ctx)) {
- Map<String, Object> entry = new HashMap<>();
- entry.put("apiId", stage.apiId());
- entry.put("stage", stage.stage());
- apiStages.add(entry);
- }
- request.put("apiStages", apiStages);
request.put("tags", ctx.resolveTags(props, "Tags"));
recordPlan(r, apiGatewayService.createUsagePlan(ctx.region(), request));
+ if (ctx.isUpdate()) {
+ CfnRollback.recordCreatedByUpdate(r, ctx, CfnRollback.USAGE_PLAN_UPDATE_SNAPSHOT_ATTR);
+ }
}
private void updateUsagePlan(StackResource r, UsagePlan existing, JsonNode props, ProvisionContext ctx) {
+ // Validated before anything changes, so a template the service would refuse leaves the plan as it was.
+ UsagePlan.Limits desiredLimits = UsagePlanLimits.fromRequest(limitsRequest(props, ctx));
List<Map<String, String>> patches = new ArrayList<>();
// A template that omits UsagePlanName keeps whatever name the plan was created with.
String name = ctx.resolveOptional(props, "UsagePlanName");
@@ -112,70 +118,140 @@ public class ApiGatewayUsagePlanCfnProvisioner implements CfnResourceProvisioner
// The service rejects a null patch value, so a dropped Description clears to "".
patches.add(patch("replace", "/description", description == null ? "" : description));
}
- // ApiStages is a set the template owns outright: stages it no longer lists come off the
- // plan, stages it adds go on. The service models that as add/remove of "apiId:stage".
- List<UsagePlan.ApiStage> desired = apiStages(props, ctx);
- List<UsagePlan.ApiStage> current = List.copyOf(existing.getApiStages());
- for (UsagePlan.ApiStage stage : current) {
- if (!listsStage(desired, stage)) {
- patches.add(patch("remove", "/apiStages", stage.apiId() + KEY_ID_SEPARATOR + stage.stage()));
- }
- }
- for (UsagePlan.ApiStage stage : desired) {
- if (!listsStage(current, stage)) {
- patches.add(patch("add", "/apiStages", stage.apiId() + KEY_ID_SEPARATOR + stage.stage()));
- }
- }
- UsagePlan plan = patches.isEmpty()
- ? existing
- : apiGatewayService.updateUsagePlan(ctx.region(), existing.getId(), patches);
-
+ // ApiStages, Throttle and Quota belong to the template outright, so what it no longer lists comes
+ // off the plan. They are replaced together, and only when one of them changed.
+ boolean limitsChanged = !desiredLimits.equals(existing.limits());
// The stored tags never include the reserved id-override keys (createUsagePlan strips them),
// so the comparison ignores them too; the service strips them again on the way in.
Map<String, String> desiredTags = ctx.resolveTags(props, "Tags");
- if (!ReservedTags.stripApiGatewayReservedTags(desiredTags).equals(plan.getTags())) {
- plan = apiGatewayService.replaceUsagePlanTags(ctx.region(), plan.getId(), desiredTags);
+ boolean tagsChanged = !ReservedTags.stripApiGatewayReservedTags(desiredTags).equals(existing.getTags());
+ if (!patches.isEmpty() || limitsChanged || tagsChanged) {
+ r.getAttributes().put(CfnRollback.USAGE_PLAN_UPDATE_SNAPSHOT_ATTR, apiGatewayService.readUsagePlan(
+ ctx.region(), existing.getId(), plan -> snapshot(plan, ctx.region())));
}
- recordPlan(r, plan);
+
+ CfnRollback.guarded(r, ctx, false, () -> { }, () -> rollbackUpdate(r), () -> {
+ if (!patches.isEmpty()) {
+ apiGatewayService.updateUsagePlan(ctx.region(), existing.getId(), patches);
+ }
+ if (limitsChanged) {
+ apiGatewayService.replaceUsagePlanLimits(ctx.region(), existing.getId(), desiredLimits);
+ }
+ if (tagsChanged) {
+ apiGatewayService.replaceUsagePlanTags(ctx.region(), existing.getId(), desiredTags);
+ }
+ });
+ recordPlan(r, existing);
}
/**
- * Whether {@code stages} lists {@code stage}'s API and stage. A stage's method throttle is not part of
- * its identity, so a stage throttled through UpdateUsagePlan still matches the template's.
+ * The name, description, stages with their method throttles, throttle, quota and tags the plan
+ * has before an in-place update changes them, so {@link #rollbackUpdate} can put them back.
+ * Written as text before the first mutating call, because the service updates the stored plan in
+ * place, and built inside {@link ApiGatewayService#readUsagePlan} so a concurrent patch cannot
+ * land halfway through it.
*/
- private static boolean listsStage(List<UsagePlan.ApiStage> stages, UsagePlan.ApiStage stage) {
- return stages.stream().anyMatch(listed -> Objects.equals(listed.apiId(), stage.apiId())
- && Objects.equals(listed.stage(), stage.stage()));
+ private static String snapshot(UsagePlan plan, String region) {
+ ObjectNode snapshot = MAPPER.createObjectNode();
+ snapshot.put("region", region);
+ snapshot.put("id", plan.getId());
+ snapshot.put("name", plan.getName());
+ snapshot.put("description", plan.getDescription());
+ snapshot.set("limits", MAPPER.valueToTree(plan.limits()));
+ ObjectNode tags = snapshot.putObject("tags");
+ plan.getTags().forEach(tags::put);
+ return snapshot.toString();
}
- /** The template's ApiStages as (apiId, stage) pairs, in template order. */
- private static List<UsagePlan.ApiStage> apiStages(JsonNode props, ProvisionContext ctx) {
- List<UsagePlan.ApiStage> stages = new ArrayList<>();
- if (props == null || !props.has("ApiStages")) {
- return stages;
+ /**
+ * The template's ApiStages, Throttle and Quota in CreateUsagePlan's request shape, which
+ * {@link UsagePlanLimits#fromRequest} validates for create and update alike.
+ */
+ private static Map<String, Object> limitsRequest(JsonNode props, ProvisionContext ctx) {
+ Map<String, Object> request = new HashMap<>();
+ request.put("apiStages", apiStages(props, ctx));
+ JsonNode throttle = resolved(props, "Throttle", ctx);
+ if (throttle != null) {
+ CfnPropertyTypes.requireType("Throttle", throttle, JsonNodeType.OBJECT);
+ request.put("throttle", throttle(throttle, ctx));
}
- JsonNode resolved = ctx.engine().resolveNode(props.get("ApiStages"));
- if (resolved == null || resolved.isNull()) {
- return stages;
+ JsonNode quota = resolved(props, "Quota", ctx);
+ if (quota != null) {
+ CfnPropertyTypes.requireType("Quota", quota, JsonNodeType.OBJECT);
+ Map<String, Object> settings = new HashMap<>();
+ putResolved(settings, "limit", quota.path("Limit"), ctx);
+ putResolved(settings, "offset", quota.path("Offset"), ctx);
+ putResolved(settings, "period", quota.path("Period"), ctx);
+ request.put("quota", settings);
}
- if (!resolved.isArray()) {
- throw new AwsException("ValidationError", "ApiStages of a usage plan must be a list.", 400);
+ return request;
+ }
+
+ /** The template's ApiStages in template order, each with its per-method Throttle. */
+ private static List<Map<String, Object>> apiStages(JsonNode props, ProvisionContext ctx) {
+ List<Map<String, Object>> stages = new ArrayList<>();
+ JsonNode resolved = resolved(props, "ApiStages", ctx);
+ if (resolved == null) {
+ return stages;
}
- for (JsonNode entry : resolved) {
+ CfnPropertyTypes.requireType("ApiStages", resolved, JsonNodeType.ARRAY);
+ for (int i = 0; i < resolved.size(); i++) {
+ JsonNode entry = resolved.get(i);
+ // Template validation names a list entry by its index.
+ CfnPropertyTypes.requireType(String.valueOf(i), entry, JsonNodeType.OBJECT);
String apiId = blankToNull(ctx.engine().resolve(entry.path("ApiId")));
String stage = blankToNull(ctx.engine().resolve(entry.path("Stage")));
if (apiId == null || stage == null) {
throw new AwsException("ValidationError",
"Every ApiStages entry of a usage plan needs both ApiId and Stage.", 400);
}
- UsagePlan.ApiStage apiStage = new UsagePlan.ApiStage(apiId, stage);
- if (!stages.contains(apiStage)) {
- stages.add(apiStage);
+ Map<String, Object> apiStage = new HashMap<>();
+ apiStage.put("apiId", apiId);
+ apiStage.put("stage", stage);
+ JsonNode methods = entry.path("Throttle");
+ if (!methods.isMissingNode() && !methods.isNull()) {
+ CfnPropertyTypes.requireType("Throttle", methods, JsonNodeType.OBJECT);
+ Map<String, Object> throttles = new LinkedHashMap<>();
+ for (Map.Entry<String, JsonNode> method : methods.properties()) {
+ CfnPropertyTypes.requireType(method.getKey(), method.getValue(), JsonNodeType.OBJECT);
+ throttles.put(method.getKey(), throttle(method.getValue(), ctx));
+ }
+ apiStage.put("throttle", throttles);
}
+ stages.add(apiStage);
}
return stages;
}
+ private static Map<String, Object> throttle(JsonNode settings, ProvisionContext ctx) {
+ Map<String, Object> throttle = new HashMap<>();
+ putResolved(throttle, "burstLimit", settings.path("BurstLimit"), ctx);
+ putResolved(throttle, "rateLimit", settings.path("RateLimit"), ctx);
+ return throttle;
+ }
+
+ /**
+ * {@code props[name]} with its intrinsics resolved, or null when the template leaves it out,
+ * including through {@code AWS::NoValue}.
+ */
+ private static JsonNode resolved(JsonNode props, String name, ProvisionContext ctx) {
+ if (props == null || !props.has(name)) {
+ return null;
+ }
+ JsonNode node = ctx.engine().resolveNodeOmittingNoValue(props.get(name));
+ return node == null || node.isNull() || node.isMissingNode() ? null : node;
+ }
+
+ /**
+ * A member the template gives is passed on as it resolves, blank included, so the service refuses an
+ * invalid one instead of reading it as absent; only a missing or null member is left out.
+ */
+ private static void putResolved(Map<String, Object> target, String key, JsonNode value, ProvisionContext ctx) {
+ if (!value.isMissingNode() && !value.isNull()) {
+ target.put(key, ctx.engine().resolve(value));
+ }
+ }
+
private UsagePlan findUsagePlan(String region, String usagePlanId) {
try {
return apiGatewayService.getUsagePlan(region, usagePlanId);
@@ -285,6 +361,67 @@ public class ApiGatewayUsagePlanCfnProvisioner implements CfnResourceProvisioner
return patch;
}
+ /**
+ * An update that failed after its snapshot may already have changed the plan, and its own undo
+ * may have failed too, so the engine keeps this attempt for {@link #rollbackUpdate} instead of
+ * putting the previous resource back over it and losing the snapshot. A failure before the
+ * snapshot changed nothing and is left to the engine.
+ */
+ @Override
+ public boolean retainsFailedUpdateState(StackResource resource) {
+ return resource.getAttributes().containsKey(CfnRollback.USAGE_PLAN_UPDATE_SNAPSHOT_ATTR);
+ }
+
+ /**
+ * The engine calls this for every resource once a stack update commits, so the snapshot is
+ * dropped here: the update committed and nothing will roll it back, and a snapshot left behind
+ * would make a later failure keep its attempt and restore stale values.
+ */
+ @Override
+ public UpdateCleanupResult completeUpdate(StackResource resource) {
+ resource.getAttributes().remove(CfnRollback.USAGE_PLAN_UPDATE_SNAPSHOT_ATTR);
+ return UpdateCleanupResult.notApplicable();
+ }
+
+ /**
+ * Undoes a usage plan update from the snapshot on the resource, and is also the in-place undo
+ * {@code updateUsagePlan} hands to {@link CfnRollback#guarded}. A plan the update created is
+ * removed by {@link CfnRollback#undoCreatedByUpdate}. Otherwise the name, description, stages,
+ * throttle, quota and tags are put back with one restore, which runs under the service's patch
+ * lock and is written whether or not the plan already matches the snapshot: a comparison read
+ * outside that lock could mix two revisions of the plan. With no snapshot on the resource the
+ * update changed nothing, since every mutating call is preceded by the snapshot or followed by
+ * the creation record, so there is nothing to undo. A plan deleted since the update fails the
+ * rollback with NotFoundException, and the stack reports the rollback as failed. A usage plan
+ * key keeps the default, which answers false.
+ */
+ @Override
+ public boolean rollbackUpdate(StackResource resource) {
+ if (!USAGE_PLAN.equals(resource.getResourceType())) {
+ return false;
+ }
+ // The snapshot is spent only once the restore succeeded: a restore that throws leaves it in
+ // place for the next attempt instead of reporting a rollback that never happened.
+ JsonNode snapshot = CfnRollback.updateSnapshot(resource, CfnRollback.USAGE_PLAN_UPDATE_SNAPSHOT_ATTR);
+ if (snapshot == null) {
+ return true;
+ }
+ if (CfnRollback.undoCreatedByUpdate(resource, snapshot, CfnRollback.USAGE_PLAN_UPDATE_SNAPSHOT_ATTR, "Id",
+ (createdId, region) -> delete(USAGE_PLAN, createdId, region))) {
+ return true;
+ }
+ String region = snapshot.path("region").asText();
+ String id = snapshot.path("id").asText();
+ String name = CfnRollback.snapshotText(snapshot, "name");
+ String description = CfnRollback.snapshotText(snapshot, "description");
+ UsagePlan.Limits limits = MAPPER.convertValue(snapshot.path("limits"), UsagePlan.Limits.class);
+ Map<String, String> tags = new HashMap<>();
+ snapshot.path("tags").properties().forEach(tag -> tags.put(tag.getKey(), tag.getValue().asText()));
+ apiGatewayService.restoreUsagePlan(region, id, name, description, limits, tags);
+ resource.getAttributes().remove(CfnRollback.USAGE_PLAN_UPDATE_SNAPSHOT_ATTR);
+ return true;
+ }
+
/** Without this the plan, or the key's membership in it, outlives the stack. */
@Override
public void delete(String resourceType, String physicalId, String region) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java
index 3ab5cc142b..00044c8082 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java
@@ -1,8 +1,14 @@
package io.github.hectorvent.floci.services.cloudformation.provisioners;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
import org.jboss.logging.Logger;
+import java.util.function.BiConsumer;
+
/**
* Rollback bookkeeping shared by every {@link CfnResourceProvisioner} and read by
* {@code CloudFormationService}. It lives here so the ownership marker and the cleanup logging
@@ -11,6 +17,7 @@ import org.jboss.logging.Logger;
public final class CfnRollback {
private static final Logger LOG = Logger.getLogger(CfnRollback.class);
+ private static final ObjectMapper MAPPER = new ObjectMapper();
/**
* Marks a resource whose backing entity this stack created. {@code CloudFormationService} reads
@@ -165,6 +172,15 @@ public final class CfnRollback {
*/
public static final String API_KEY_UPDATE_SNAPSHOT_ATTR = "__FlociApiKeyUpdateSnapshot";
+ /**
+ * Holds the name, description, stages with their method throttles, throttle, quota and tags a
+ * usage plan carried before an in-place update changed them, or the plan an update created because
+ * the one the stack held was gone, so a failed stack update can put the plan back or delete the
+ * created one. Written by {@code ApiGatewayUsagePlanCfnProvisioner} before its first in-place
+ * mutating call or right after that creation, and spent by its {@code rollbackUpdate}.
+ */
+ public static final String USAGE_PLAN_UPDATE_SNAPSHOT_ATTR = "__FlociUsagePlanUpdateSnapshot";
+
/**
* Holds the role, object prefixes, tagging action and tags a GuardDuty Malware Protection plan
* carried before an in-place update changed them, or the plan an update created because the one
@@ -334,7 +350,8 @@ public final class CfnRollback {
* provisioner whose snapshot a ContinueUpdateRollback still needs after an undo that failed
* carries it in {@link CfnResourceProvisioner#mergeFailedUpdateResourceTracking}.
*
- * <p>A compensation that fails is reported, never hidden. A created entity carries
+ * <p>A compensation that fails is reported, never hidden, and is attached to the failure as
+ * suppressed unless it rethrows that same exception. A created entity carries
* {@link #ROLLBACK_OWNED_ATTR} while the steps run, so the engine's create rollback deletes it
* (and reports that failing) if the undo here could not, and during an update the entity is
* also listed in the cleanup record, which the engine carries onto the restored resource and
@@ -357,7 +374,9 @@ public final class CfnRollback {
undoCreate.run();
r.getAttributes().remove(ROLLBACK_OWNED_ATTR);
} catch (RuntimeException undo) {
- e.addSuppressed(undo);
+ if (undo != e) {
+ e.addSuppressed(undo);
+ }
String reason = "Could not remove " + r.getPhysicalId() + ", created for " + r.getLogicalId()
+ " by a failed update: " + undo.getMessage();
LOG.warn(reason);
@@ -372,7 +391,9 @@ public final class CfnRollback {
try {
undoInPlace.run();
} catch (RuntimeException undo) {
- e.addSuppressed(undo);
+ if (undo != e) {
+ e.addSuppressed(undo);
+ }
String reason = "Could not restore " + r.getPhysicalId() + " for " + r.getLogicalId()
+ " after a failed update: " + undo.getMessage();
LOG.warn(reason);
@@ -382,4 +403,58 @@ public final class CfnRollback {
throw e;
}
}
+
+ /**
+ * Records that an update created the entity because the one at the prior physical id was gone,
+ * so {@link #undoCreatedByUpdate} can delete it and name the prior id again.
+ */
+ public static void recordCreatedByUpdate(StackResource r, ProvisionContext ctx, String snapshotAttr) {
+ ObjectNode snapshot = MAPPER.createObjectNode();
+ snapshot.put("region", ctx.region());
+ snapshot.put("createdId", r.getPhysicalId());
+ snapshot.put("priorId", ctx.priorPhysicalId());
+ r.getAttributes().put(snapshotAttr, snapshot.toString());
+ }
+
+ /** The update snapshot held in {@code snapshotAttr}, or null when the resource carries none. */
+ public static JsonNode updateSnapshot(StackResource resource, String snapshotAttr) {
+ String raw = resource.getAttributes().get(snapshotAttr);
+ if (raw == null) {
+ return null;
+ }
+ try {
+ return MAPPER.readTree(raw);
+ } catch (JsonProcessingException e) {
+ throw new IllegalStateException("Could not read the update snapshot " + snapshotAttr + " for "
+ + resource.getLogicalId(), e);
+ }
+ }
+
+ /** A snapshotted string, or null when the field is absent or JSON null. */
+ public static String snapshotText(JsonNode snapshot, String field) {
+ JsonNode value = snapshot.get(field);
+ return value == null || value.isNull() ? null : value.asText();
+ }
+
+ /**
+ * Undoes an update {@link #recordCreatedByUpdate} recorded: deletes the created entity with
+ * {@code deleteInRegion}, which takes its id and region, names the prior id again as the physical
+ * id and in {@code idAttribute}, and spends the snapshot. Returns false, changing nothing, when
+ * the update did not create the entity.
+ */
+ public static boolean undoCreatedByUpdate(StackResource resource, JsonNode snapshot, String snapshotAttr,
+ String idAttribute, BiConsumer<String, String> deleteInRegion) {
+ String createdId = snapshotText(snapshot, "createdId");
+ if (createdId == null) {
+ return false;
+ }
+ // Deleted before the resource names the prior id again, so a failed delete leaves the
+ // resource naming the created entity and deleting the stack still removes it.
+ deleteInRegion.accept(createdId, snapshot.path("region").asText());
+ String priorId = snapshot.path("priorId").asText();
+ resource.setPhysicalId(priorId);
+ resource.getAttributes().put(idAttribute, priorId);
+ resource.getAttributes().remove(snapshotAttr);
+ return true;
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayUsagePlanServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayUsagePlanServiceTest.java
index 65ca1589c8..23ff0903a6 100644
--- a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayUsagePlanServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayUsagePlanServiceTest.java
@@ -21,8 +21,12 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicReference;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
@@ -165,4 +169,62 @@ class ApiGatewayUsagePlanServiceTest {
}
assertEquals(0, resurrected, "plans a patch wrote back after they were deleted");
}
+
+ // A failed CloudFormation update is undone with every value it had, a description it lacked included.
+ @Test
+ void restoreSetsEveryValueOfThePlanANullDescriptionIncluded() {
+ ApiGatewayService service = service();
+ String planId = service.createUsagePlan(REGION, Map.of("name", "restored", "tags", Map.of("stack", "v1")))
+ .getId();
+ service.updateUsagePlan(REGION, planId, List.of(
+ Map.of("op", "replace", "path", "/name", "value", "renamed"),
+ Map.of("op", "replace", "path", "/description", "value", "added")));
+ UsagePlan.Limits limits = new UsagePlan.Limits(
+ List.of(new UsagePlan.ApiStage("api", "v1", Map.of("/pets/GET", new UsagePlan.Throttle(3, 1.0)))),
+ new UsagePlan.Throttle(10, 5.0), new UsagePlan.Quota(100, 0, "DAY"));
+
+ service.restoreUsagePlan(REGION, planId, "restored", null, limits, Map.of("stack", "v1"));
+
+ UsagePlan plan = service.getUsagePlan(REGION, planId);
+ assertEquals("restored", plan.getName());
+ assertNull(plan.getDescription());
+ assertEquals(limits, plan.limits());
+ assertEquals(Map.of("stack", "v1"), plan.getTags());
+ }
+
+ // A CloudFormation update snapshots the plan through this read, so a patch landing halfway through it
+ // would leave a snapshot mixing two revisions. The patch has to wait until the reader returns: the
+ // reader holds on until the patch's thread is blocked or the patch is done, so a slow start cannot
+ // pass for a wait.
+ @Test
+ void aPatchWaitsForAReadOfThePlanToFinish() throws Exception {
+ ApiGatewayService service = service();
+ String planId = service.createUsagePlan(REGION, Map.of("name", "before")).getId();
+ AtomicReference<Thread> worker = new AtomicReference<>();
+ CountDownLatch started = new CountDownLatch(1);
+ try (ExecutorService executor = Executors.newSingleThreadExecutor()) {
+ Future<UsagePlan> patch = service.readUsagePlan(REGION, planId, plan -> {
+ Future<UsagePlan> pending = executor.submit(() -> {
+ worker.set(Thread.currentThread());
+ started.countDown();
+ return service.updateUsagePlan(REGION, planId,
+ List.of(Map.of("op", "replace", "path", "/name", "value", "after")));
+ });
+ assertTrue(assertDoesNotThrow(() -> started.await(5, TimeUnit.SECONDS)),
+ "the patch never started");
+ long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
+ while (worker.get().getState() != Thread.State.BLOCKED && !pending.isDone()
+ && System.nanoTime() < deadline) {
+ Thread.onSpinWait();
+ }
+ assertFalse(pending.isDone(), "the patch finished while the plan was being read");
+ assertEquals(Thread.State.BLOCKED, worker.get().getState());
+ assertEquals("before", plan.getName());
+ return pending;
+ });
+
+ assertEquals("after", patch.get(5, TimeUnit.SECONDS).getName());
+ assertEquals("after", service.getUsagePlan(REGION, planId).getName());
+ }
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayUsagePlanCfnIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayUsagePlanCfnIntegrationTest.java
index 75a9b2c2eb..878b1338d8 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayUsagePlanCfnIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayUsagePlanCfnIntegrationTest.java
@@ -1,24 +1,22 @@
package io.github.hectorvent.floci.services.cloudformation;
import io.github.hectorvent.floci.core.common.XmlParser;
-import io.github.hectorvent.floci.services.apigateway.ApiGatewayService;
-import io.github.hectorvent.floci.services.apigateway.model.UsagePlan;
import io.github.hectorvent.floci.testing.CfnStackWaits;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;
-import jakarta.inject.Inject;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
-import java.util.List;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
@@ -26,13 +24,15 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
* {@code AWS::ApiGateway::UsagePlanKey} attaching an API key to it, the way a template guards an
* API behind a key. The API and the key are created outside the stack, since the stack only owns
* the plan and the association. Asserts that {@code Ref} and {@code Fn::GetAtt Id} resolve to the
- * plan id and to {@code <keyId>:<usagePlanId>}, that an update renames the plan and moves its stage
- * in place, and that deleting the stack removes the plan and the association but not the key.
+ * plan id and to {@code <keyId>:<usagePlanId>}, that the plan reports its stage with the stage's
+ * method throttle, its throttle and its quota, that an update renames the plan, moves its stage and
+ * changes the stage throttle in place and drops a quota the template omits through
+ * {@code AWS::NoValue}, and that deleting the stack removes the plan and
+ * the association but not the key.
*/
@QuarkusTest
class ApiGatewayUsagePlanCfnIntegrationTest {
- private static final String REGION = "us-east-1";
private static final String CFN_AUTH =
"AWS4-HMAC-SHA256 Credential=test/20260905/us-east-1/cloudformation/aws4_request";
private static final String STACK = "apigw-usageplan-cfn-it";
@@ -45,7 +45,13 @@ class ApiGatewayUsagePlanCfnIntegrationTest {
"KeyId": {"Type": "String"},
"PlanName": {"Type": "String"},
"Description": {"Type": "String"},
- "TagValue": {"Type": "String"}
+ "TagValue": {"Type": "String"},
+ "QuotaLimit": {"Type": "Number"},
+ "BurstLimit": {"Type": "Number"},
+ "WithQuota": {"Type": "String"}
+ },
+ "Conditions": {
+ "HasQuota": {"Fn::Equals": [{"Ref": "WithQuota"}, "true"]}
},
"Resources": {
"Plan": {
@@ -53,9 +59,11 @@ class ApiGatewayUsagePlanCfnIntegrationTest {
"Properties": {
"UsagePlanName": {"Ref": "PlanName"},
"Description": {"Ref": "Description"},
- "ApiStages": [{"ApiId": {"Ref": "ApiId"}, "Stage": {"Ref": "StageName"}}],
+ "ApiStages": [{"ApiId": {"Ref": "ApiId"}, "Stage": {"Ref": "StageName"},
+ "Throttle": {"/items/GET": {"BurstLimit": {"Ref": "BurstLimit"}, "RateLimit": 2}}}],
"Throttle": {"BurstLimit": 10, "RateLimit": 5},
- "Quota": {"Limit": 1000, "Period": "MONTH"},
+ "Quota": {"Fn::If": ["HasQuota", {"Limit": {"Ref": "QuotaLimit"}, "Period": "MONTH"},
+ {"Ref": "AWS::NoValue"}]},
"Tags": [{"Key": "stack", "Value": {"Ref": "TagValue"}}]
}
},
@@ -77,9 +85,6 @@ class ApiGatewayUsagePlanCfnIntegrationTest {
}
""";
- @Inject
- ApiGatewayService apiGatewayService;
-
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
@@ -94,7 +99,7 @@ class ApiGatewayUsagePlanCfnIntegrationTest {
String keyId = given().contentType(ContentType.JSON).body("{\"name\":\"" + STACK + "-key\"}")
.when().post("/apikeys").then().statusCode(201).extract().path("id");
try {
- cloudFormation("CreateStack", parameters(apiId, "dev", keyId, "gold", "first", "v1"));
+ cloudFormation("CreateStack", parameters(apiId, "dev", keyId, "gold", "first", "v1", "1000", "4", "true"));
String created = describeStacks("CREATE_COMPLETE");
String planId = outputValue(created, "PlanRef");
@@ -108,11 +113,21 @@ class ApiGatewayUsagePlanCfnIntegrationTest {
.body("name", equalTo("gold"))
.body("description", equalTo("first"))
.body("tags.stack", equalTo("v1"));
- assertEquals(List.of(new UsagePlan.ApiStage(apiId, "dev")),
- apiGatewayService.getUsagePlan(REGION, planId).getApiStages());
+ getUsagePlan(planId)
+ .statusCode(200)
+ .body("apiStages", hasSize(1))
+ .body("apiStages[0].apiId", equalTo(apiId))
+ .body("apiStages[0].stage", equalTo("dev"))
+ .body("apiStages[0].throttle['/items/GET'].burstLimit", equalTo(4))
+ .body("apiStages[0].throttle['/items/GET'].rateLimit", equalTo(2.0f))
+ .body("throttle.burstLimit", equalTo(10))
+ .body("throttle.rateLimit", equalTo(5.0f))
+ .body("quota.limit", equalTo(1000))
+ .body("quota.offset", equalTo(0))
+ .body("quota.period", equalTo("MONTH"));
getUsagePlanKey(planId, keyId).statusCode(200).body("type", equalTo("API_KEY"));
- cloudFormation("UpdateStack", parameters(apiId, "prod", keyId, "platinum", "second", "v2"));
+ cloudFormation("UpdateStack", parameters(apiId, "prod", keyId, "platinum", "second", "v2", "2000", "6", "false"));
String updated = describeStacks("UPDATE_COMPLETE");
// The plan is renamed and moved to the other stage in place; the association survives.
@@ -123,8 +138,17 @@ class ApiGatewayUsagePlanCfnIntegrationTest {
.body("name", equalTo("platinum"))
.body("description", equalTo("second"))
.body("tags.stack", equalTo("v2"));
- assertEquals(List.of(new UsagePlan.ApiStage(apiId, "prod")),
- apiGatewayService.getUsagePlan(REGION, planId).getApiStages());
+ getUsagePlan(planId)
+ .statusCode(200)
+ .body("apiStages", hasSize(1))
+ .body("apiStages[0].apiId", equalTo(apiId))
+ .body("apiStages[0].stage", equalTo("prod"))
+ .body("apiStages[0].throttle['/items/GET'].burstLimit", equalTo(6))
+ .body("apiStages[0].throttle['/items/GET'].rateLimit", equalTo(2.0f))
+ .body("throttle.burstLimit", equalTo(10))
+ .body("throttle.rateLimit", equalTo(5.0f))
+ // AWS::NoValue leaves the quota out of the template, so the update takes it off the plan.
+ .body("quota", nullValue());
getUsagePlanKey(planId, keyId).statusCode(200);
cloudFormation("DeleteStack", Map.of());
@@ -141,9 +165,11 @@ class ApiGatewayUsagePlanCfnIntegrationTest {
}
private static Map<String, String> parameters(String apiId, String stage, String keyId, String planName,
- String description, String tagValue) {
+ String description, String tagValue, String quotaLimit,
+ String burstLimit, String withQuota) {
return Map.of("ApiId", apiId, "StageName", stage, "KeyId", keyId, "PlanName", planName,
- "Description", description, "TagValue", tagValue);
+ "Description", description, "TagValue", tagValue, "QuotaLimit", quotaLimit,
+ "BurstLimit", burstLimit, "WithQuota", withQuota);
}
private static void cloudFormation(String action, Map<String, String> parameters) {
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayUsagePlanCfnRollbackIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayUsagePlanCfnRollbackIntegrationTest.java
new file mode 100644
index 0000000000..828f136cd1
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayUsagePlanCfnRollbackIntegrationTest.java
@@ -0,0 +1,228 @@
+package io.github.hectorvent.floci.services.cloudformation;
+
+import io.github.hectorvent.floci.core.common.XmlParser;
+import io.github.hectorvent.floci.services.apigateway.ApiGatewayService;
+import io.github.hectorvent.floci.testing.CfnStackWaits;
+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.mockito.InjectSpy;
+import io.restassured.response.ValidatableResponse;
+import io.restassured.specification.RequestSpecification;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.hasKey;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.not;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyMap;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+
+/**
+ * Stack updates of an {@code AWS::ApiGateway::UsagePlan} that a later resource fails, so the update
+ * rolls back. Asserts that the plan's name, description, stages with their method throttles,
+ * throttle, quota and tags come back as they were, that a plan the update created in place of one
+ * deleted out of band is deleted again, and that a plan whose failed update could not be undone at
+ * once is put back by ContinueUpdateRollback.
+ */
+@QuarkusTest
+class ApiGatewayUsagePlanCfnRollbackIntegrationTest {
+
+ private static final String CFN_AUTH =
+ "AWS4-HMAC-SHA256 Credential=test/20260905/us-east-1/cloudformation/aws4_request";
+
+ /** One plan with the given properties, optionally followed by more resources. */
+ private static final String PLAN_TEMPLATE = """
+ {
+ "Resources": {
+ "Plan": {
+ "Type": "AWS::ApiGateway::UsagePlan",
+ "Properties": {%s}
+ }%s
+ },
+ "Outputs": {"PlanRef": {"Value": {"Ref": "Plan"}}}
+ }
+ """;
+
+ /** A resource that fails after the one it depends on, so the update rolls back. */
+ private static final String FAILING_RESOURCE = """
+ ,
+ "BadSecret": {
+ "Type": "AWS::SecretsManager::Secret",
+ "DependsOn": "%s",
+ "Properties": {
+ "Name": "%s",
+ "SecretString": "explicit",
+ "GenerateSecretString": {"PasswordLength": 32}
+ }
+ }""";
+
+ @InjectSpy
+ ApiGatewayService apiGatewayService;
+
+ @BeforeAll
+ static void configureRestAssured() {
+ RestAssuredJsonUtils.configureAwsContentTypes();
+ }
+
+ @Test
+ void aFailedUpdatePutsThePlansQuotaThrottleAndNameBack() {
+ String stack = "apigw-usageplan-cfn-rollback-it";
+ String original = """
+ "UsagePlanName": "apigw-usageplan-rollback-original",
+ "ApiStages": [{"ApiId": "rollbackapi", "Stage": "v1",
+ "Throttle": {"/items/GET": {"BurstLimit": 3, "RateLimit": 1}}}],
+ "Throttle": {"BurstLimit": 10, "RateLimit": 5},
+ "Quota": {"Limit": 1000, "Period": "MONTH"},
+ "Tags": [{"Key": "stack", "Value": "v1"}]""";
+ cloudFormation(stack, "CreateStack", PLAN_TEMPLATE.formatted(original, ""));
+ String planId = outputValue(describeStacks(stack, "CREATE_COMPLETE"), "PlanRef");
+
+ // Every value changes, the description is added, before the secret fails the update.
+ String changed = """
+ "UsagePlanName": "apigw-usageplan-rollback-changed",
+ "Description": "changed",
+ "ApiStages": [{"ApiId": "rollbackapi", "Stage": "v2"}],
+ "Throttle": {"BurstLimit": 20, "RateLimit": 8},
+ "Quota": {"Limit": 50, "Offset": 1, "Period": "WEEK"},
+ "Tags": [{"Key": "stack", "Value": "v2"}]""";
+ cloudFormation(stack, "UpdateStack",
+ PLAN_TEMPLATE.formatted(changed, FAILING_RESOURCE.formatted("Plan", stack)));
+ assertEquals(planId, outputValue(describeStacks(stack, "UPDATE_ROLLBACK_COMPLETE"), "PlanRef"));
+
+ getUsagePlan(planId)
+ .statusCode(200)
+ .body("name", equalTo("apigw-usageplan-rollback-original"))
+ .body("$", not(hasKey("description")))
+ .body("throttle.burstLimit", equalTo(10))
+ .body("throttle.rateLimit", equalTo(5.0f))
+ .body("quota.limit", equalTo(1000))
+ .body("quota.offset", equalTo(0))
+ .body("quota.period", equalTo("MONTH"))
+ .body("apiStages", hasSize(1))
+ .body("apiStages[0].stage", equalTo("v1"))
+ .body("apiStages[0].throttle['/items/GET'].burstLimit", equalTo(3))
+ .body("tags.stack", equalTo("v1"));
+
+ cloudFormation(stack, "DeleteStack", null);
+ CfnStackWaits.awaitStackDeleted(stack);
+ getUsagePlan(planId).statusCode(404);
+ }
+
+ @Test
+ void aFailedUpdateDeletesThePlanItCreatedInPlaceOfOneDeletedOutOfBand() {
+ String stack = "apigw-usageplan-cfn-recreate-it";
+ String name = "apigw-usageplan-cfn-recreate-it-plan";
+ String properties = "\"UsagePlanName\": \"" + name + "\"";
+ cloudFormation(stack, "CreateStack", PLAN_TEMPLATE.formatted(properties, ""));
+ String planId = outputValue(describeStacks(stack, "CREATE_COMPLETE"), "PlanRef");
+ given().when().delete("/usageplans/" + planId).then().statusCode(202);
+
+ // The description makes the update re-provision the plan, which creates one in place of the
+ // deleted one before the secret fails the update.
+ cloudFormation(stack, "UpdateStack", PLAN_TEMPLATE.formatted(properties + ", \"Description\": \"changed\"",
+ FAILING_RESOURCE.formatted("Plan", stack)));
+ describeStacks(stack, "UPDATE_ROLLBACK_COMPLETE");
+ given().when().get("/usageplans").then()
+ .statusCode(200)
+ .body("item.name", not(hasItem(name)));
+
+ cloudFormation(stack, "DeleteStack", null);
+ CfnStackWaits.awaitStackDeleted(stack);
+ }
+
+ // The plan's own write fails after changing it, and the provisioner's undo fails once too, so the stack
+ // stops in UPDATE_ROLLBACK_FAILED with the plan holding the failed update's values. The retry must put
+ // them back rather than report a rollback that restored nothing.
+ @Test
+ void continueUpdateRollbackPutsBackAPlanWhoseFailedUpdateCouldNotBeUndoneAtOnce() {
+ String stack = "apigw-usageplan-cfn-retry-it";
+ String original = """
+ "UsagePlanName": "apigw-usageplan-retry-original",
+ "Throttle": {"BurstLimit": 10, "RateLimit": 5},
+ "Quota": {"Limit": 1000, "Period": "MONTH"}""";
+ cloudFormation(stack, "CreateStack", PLAN_TEMPLATE.formatted(original, ""));
+ String planId = outputValue(describeStacks(stack, "CREATE_COMPLETE"), "PlanRef");
+
+ Mockito.doAnswer(invocation -> {
+ invocation.callRealMethod();
+ throw new IllegalStateException("simulated write failure");
+ }).doCallRealMethod().when(apiGatewayService).replaceUsagePlanLimits(anyString(), eq(planId), any());
+ Mockito.doThrow(new IllegalStateException("simulated restore failure")).doCallRealMethod()
+ .when(apiGatewayService).restoreUsagePlan(anyString(), eq(planId), any(), any(), any(), anyMap());
+
+ String changed = """
+ "UsagePlanName": "apigw-usageplan-retry-changed",
+ "Throttle": {"BurstLimit": 20, "RateLimit": 8},
+ "Quota": {"Limit": 50, "Period": "WEEK"}""";
+ cloudFormation(stack, "UpdateStack", PLAN_TEMPLATE.formatted(changed, ""));
+ describeStacks(stack, "UPDATE_ROLLBACK_FAILED");
+ getUsagePlan(planId)
+ .statusCode(200)
+ .body("name", equalTo("apigw-usageplan-retry-changed"))
+ .body("quota.limit", equalTo(50));
+
+ cloudFormation(stack, "ContinueUpdateRollback", null);
+ assertEquals(planId, outputValue(describeStacks(stack, "UPDATE_ROLLBACK_COMPLETE"), "PlanRef"));
+ getUsagePlan(planId)
+ .statusCode(200)
+ .body("name", equalTo("apigw-usageplan-retry-original"))
+ .body("throttle.burstLimit", equalTo(10))
+ .body("throttle.rateLimit", equalTo(5.0f))
+ .body("quota.limit", equalTo(1000))
+ .body("quota.period", equalTo("MONTH"));
+
+ // An update that only changes an output commits afterwards, and leaves the plan as it is.
+ cloudFormation(stack, "UpdateStack", PLAN_TEMPLATE.formatted(original, "")
+ .replace("\"PlanRef\": {\"Value\": {\"Ref\": \"Plan\"}}",
+ "\"PlanRef\": {\"Value\": {\"Ref\": \"Plan\"}}, \"Extra\": {\"Value\": \"x\"}"));
+ describeStacks(stack, "UPDATE_COMPLETE");
+ getUsagePlan(planId)
+ .statusCode(200)
+ .body("name", equalTo("apigw-usageplan-retry-original"))
+ .body("quota.limit", equalTo(1000));
+
+ cloudFormation(stack, "DeleteStack", null);
+ CfnStackWaits.awaitStackDeleted(stack);
+ }
+
+ private static void cloudFormation(String stack, String action, String templateBody) {
+ RequestSpecification request = given()
+ .contentType("application/x-www-form-urlencoded")
+ .header("Authorization", CFN_AUTH)
+ .formParam("Action", action)
+ .formParam("StackName", stack);
+ if (templateBody != null) {
+ request.formParam("TemplateBody", templateBody);
+ }
+ request.when().post("/").then().statusCode(200);
+ }
+
+ private static String describeStacks(String stack, String expectedStatus) {
+ CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(stack);
+ assertEquals(expectedStatus, state.status(), state.reason());
+ return given()
+ .contentType("application/x-www-form-urlencoded")
+ .header("Authorization", CFN_AUTH)
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", stack)
+ .when().post("/").then().statusCode(200)
+ .body(containsString("<StackStatus>" + expectedStatus + "</StackStatus>"))
+ .extract().asString();
+ }
+
+ private static String outputValue(String xml, String key) {
+ return XmlParser.extractPairs(xml, "Outputs", "OutputKey", "OutputValue").get(key);
+ }
+
+ private static ValidatableResponse getUsagePlan(String planId) {
+ return given().when().get("/usageplans/" + planId).then();
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayUsagePlanCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayUsagePlanCfnProvisionerTest.java
index bdd2ab5874..9aa0c8692b 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayUsagePlanCfnProvisionerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayUsagePlanCfnProvisionerTest.java
@@ -10,25 +10,35 @@ import io.github.hectorvent.floci.services.apigateway.model.UsagePlan;
import io.github.hectorvent.floci.services.apigateway.model.UsagePlanKey;
import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
+import org.mockito.InOrder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.function.Function;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.clearInvocations;
+import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
/** The API Gateway usage plan CFN provisioner in isolation, against a mocked {@link ApiGatewayService}. */
@@ -39,11 +49,24 @@ class ApiGatewayUsagePlanCfnProvisionerTest {
private static final String PLAN_KEY = "AWS::ApiGateway::UsagePlanKey";
private static final AwsException NOT_FOUND =
new AwsException("NotFoundException", "Usage Plan not found", 404);
+ /** Stage api1:dev throttling /pets/GET at 10/5.0, throttle 20/10.0, quota 1000/0/MONTH. */
+ private static final UsagePlan.Limits ORIGINAL_LIMITS = new UsagePlan.Limits(
+ List.of(new UsagePlan.ApiStage("api1", "dev", Map.of("/pets/GET", new UsagePlan.Throttle(10, 5.0)))),
+ new UsagePlan.Throttle(20, 10.0), new UsagePlan.Quota(1000, 0, "MONTH"));
private final ApiGatewayService apiGateway = mock(ApiGatewayService.class);
private final ApiGatewayUsagePlanCfnProvisioner provisioner = new ApiGatewayUsagePlanCfnProvisioner(apiGateway);
private final ObjectMapper mapper = new ObjectMapper();
+ /** The locked read hands its reader the plan {@code getUsagePlan} is stubbed with. */
+ @BeforeEach
+ void readTheStubbedPlan() {
+ when(apiGateway.readUsagePlan(any(), any(), any())).thenAnswer(inv -> {
+ Function<UsagePlan, Object> reader = inv.getArgument(2);
+ return reader.apply(apiGateway.getUsagePlan(inv.getArgument(0), inv.getArgument(1)));
+ });
+ }
+
private ProvisionContext ctx() {
return ctx(null);
}
@@ -55,6 +78,7 @@ class ApiGatewayUsagePlanCfnProvisionerTest {
return node == null || node.isMissingNode() || node.isNull() ? null : node.asText();
});
when(engine.resolveNode(any())).thenAnswer(inv -> inv.getArgument(0));
+ when(engine.resolveNodeOmittingNoValue(any())).thenAnswer(inv -> inv.getArgument(0));
return new ProvisionContext(engine, REGION, "000000000000", "my-stack", priorPhysicalId);
}
@@ -150,11 +174,80 @@ class ApiGatewayUsagePlanCfnProvisionerTest {
}
@Test
- void usagePlanUpdatePatchesNameDescriptionAndStagesInPlace() {
+ void usagePlanCreateSendsThrottleQuotaAndStageThrottles() {
+ when(apiGateway.createUsagePlan(eq(REGION), anyMap())).thenReturn(plan("plan-1", "gold", null, Map.of()));
+ ObjectNode props = planProps("gold", null, null);
+ props.putObject("Throttle").put("BurstLimit", 10).put("RateLimit", 5);
+ props.putObject("Quota").put("Limit", 1000).put("Offset", 2).put("Period", "WEEK");
+ ObjectNode stage = ((ArrayNode) props.get("ApiStages")).addObject().put("ApiId", "api1").put("Stage", "dev");
+ stage.putObject("Throttle").putObject("/pets/GET").put("BurstLimit", 3).put("RateLimit", 1.5);
+
+ provisioner.provision(resource(PLAN, "Plan", null), props, ctx());
+
+ @SuppressWarnings("unchecked")
+ ArgumentCaptor<Map<String, Object>> request = ArgumentCaptor.forClass(Map.class);
+ verify(apiGateway).createUsagePlan(eq(REGION), request.capture());
+ assertEquals(Map.of("burstLimit", "10", "rateLimit", "5"), request.getValue().get("throttle"));
+ assertEquals(Map.of("limit", "1000", "offset", "2", "period", "WEEK"), request.getValue().get("quota"));
+ assertEquals(List.of(Map.of("apiId", "api1", "stage", "dev",
+ "throttle", Map.of("/pets/GET", Map.of("burstLimit", "3", "rateLimit", "1.5")))),
+ request.getValue().get("apiStages"));
+ }
+
+ @Test
+ void aQuotaTheServiceWouldRefuseLeavesThePlanUntouched() {
+ UsagePlan existing = plan("plan-1", "gold", null, Map.of());
+ when(apiGateway.getUsagePlan(REGION, "plan-1")).thenReturn(existing);
+ ObjectNode props = planProps("platinum", null, null);
+ props.putObject("Quota").put("Limit", 10);
+
+ AwsException e = assertThrows(AwsException.class,
+ () -> provisioner.provision(resource(PLAN, "Plan", "plan-1"), props, ctx("plan-1")));
+
+ assertEquals("BadRequestException", e.getErrorCode());
+ verify(apiGateway, never()).updateUsagePlan(any(), any(), anyList());
+ verify(apiGateway, never()).replaceUsagePlanLimits(any(), any(), any());
+ }
+
+ @Test
+ void aBlankQuotaOffsetIsRefusedRatherThanReadAsZero() {
+ UsagePlan existing = plan("plan-1", "gold", null, Map.of());
+ existing.setQuota(new UsagePlan.Quota(10, 3, "WEEK"));
+ when(apiGateway.getUsagePlan(REGION, "plan-1")).thenReturn(existing);
+ ObjectNode props = planProps("gold", null, null);
+ props.putObject("Quota").put("Limit", 10).put("Offset", "").put("Period", "WEEK");
+
+ AwsException e = assertThrows(AwsException.class,
+ () -> provisioner.provision(resource(PLAN, "Plan", "plan-1"), props, ctx("plan-1")));
+
+ assertEquals("Usage Plan quota offset must be numeric and less than the number of time units in the period",
+ e.getMessage());
+ verify(apiGateway, never()).replaceUsagePlanLimits(any(), any(), any());
+ }
+
+ @Test
+ void aBurstLimitThatIsAListIsRefusedRatherThanReadAsZero() {
+ UsagePlan existing = plan("plan-1", "gold", null, Map.of());
+ existing.setThrottle(new UsagePlan.Throttle(20, 10.0));
+ when(apiGateway.getUsagePlan(REGION, "plan-1")).thenReturn(existing);
+ ObjectNode props = planProps("gold", null, null);
+ props.putObject("Throttle").put("RateLimit", 5).putArray("BurstLimit");
+
+ AwsException e = assertThrows(AwsException.class,
+ () -> provisioner.provision(resource(PLAN, "Plan", "plan-1"), props, ctx("plan-1")));
+
+ assertEquals("Usage Plan throttle rate limit and burst limit must be numeric and greater than zero",
+ e.getMessage());
+ verify(apiGateway, never()).replaceUsagePlanLimits(any(), any(), any());
+ }
+
+ @Test
+ void usagePlanUpdatePatchesNameAndDescriptionAndReplacesTheStages() {
UsagePlan existing = plan("plan-1", "gold", "old", Map.of(),
new UsagePlan.ApiStage("api1", "dev"), new UsagePlan.ApiStage("api1", "test"));
when(apiGateway.getUsagePlan(REGION, "plan-1")).thenReturn(existing);
when(apiGateway.updateUsagePlan(eq(REGION), eq("plan-1"), anyList())).thenReturn(existing);
+ when(apiGateway.replaceUsagePlanLimits(eq(REGION), eq("plan-1"), any())).thenReturn(existing);
StackResource r = resource(PLAN, "Plan", "plan-1");
provisioner.provision(r, planProps("platinum", "new", null, "api1:dev", "api1:prod"), ctx("plan-1"));
@@ -166,32 +259,13 @@ class ApiGatewayUsagePlanCfnProvisionerTest {
List<Map<String, String>> ops = patches.getValue();
assertTrue(ops.contains(Map.of("op", "replace", "path", "/name", "value", "platinum")), ops.toString());
assertTrue(ops.contains(Map.of("op", "replace", "path", "/description", "value", "new")), ops.toString());
- assertTrue(ops.contains(Map.of("op", "remove", "path", "/apiStages", "value", "api1:test")), ops.toString());
- assertTrue(ops.contains(Map.of("op", "add", "path", "/apiStages", "value", "api1:prod")), ops.toString());
- assertEquals(4, ops.size(), ops.toString());
+ assertEquals(2, ops.size(), ops.toString());
+ verify(apiGateway).replaceUsagePlanLimits(REGION, "plan-1", new UsagePlan.Limits(
+ List.of(new UsagePlan.ApiStage("api1", "dev"), new UsagePlan.ApiStage("api1", "prod")), null, null));
assertEquals("plan-1", r.getPhysicalId());
assertEquals("plan-1", r.getAttributes().get("Id"));
}
- // A stage's method throttle is not part of its identity, so a stage the template still lists stays on the
- // plan with its throttle rather than being removed and added again.
- @Test
- void aThrottledStageTheTemplateStillListsIsLeftInPlace() {
- UsagePlan existing = plan("plan-1", "gold", "old", Map.of(), new UsagePlan.ApiStage("api1", "dev",
- Map.of("/pets/GET", new UsagePlan.Throttle(10, 5.0))));
- when(apiGateway.getUsagePlan(REGION, "plan-1")).thenReturn(existing);
- when(apiGateway.updateUsagePlan(eq(REGION), eq("plan-1"), anyList())).thenReturn(existing);
-
- provisioner.provision(resource(PLAN, "Plan", "plan-1"), planProps("gold", "new", null, "api1:dev"),
- ctx("plan-1"));
-
- @SuppressWarnings("unchecked")
- ArgumentCaptor<List<Map<String, String>>> patches = ArgumentCaptor.forClass(List.class);
- verify(apiGateway).updateUsagePlan(eq(REGION), eq("plan-1"), patches.capture());
- assertEquals(List.of(Map.of("op", "replace", "path", "/description", "value", "new")),
- patches.getValue());
- }
-
@Test
void anUnchangedUsagePlanUpdateTouchesNothing() {
UsagePlan existing = plan("plan-1", "gold", "same", Map.of("stack", "v1"), new UsagePlan.ApiStage("api1", "dev"));
@@ -202,6 +276,7 @@ class ApiGatewayUsagePlanCfnProvisionerTest {
verify(apiGateway, never()).createUsagePlan(any(), anyMap());
verify(apiGateway, never()).updateUsagePlan(any(), any(), anyList());
verify(apiGateway, never()).replaceUsagePlanTags(any(), any(), anyMap());
+ verify(apiGateway, never()).replaceUsagePlanLimits(any(), any(), any());
}
@Test
@@ -343,6 +418,75 @@ class ApiGatewayUsagePlanCfnProvisionerTest {
verify(apiGateway, never()).createUsagePlanKey(any(), any(), anyMap());
}
+ private void assertCreateRefused(ObjectNode props, String message) {
+ AwsException e = assertThrows(AwsException.class,
+ () -> provisioner.provision(resource(PLAN, "Plan", null), props, ctx()));
+
+ assertEquals("ValidationError", e.getErrorCode());
+ assertEquals(message, e.getMessage());
+ verify(apiGateway, never()).createUsagePlan(any(), anyMap());
+ }
+
+ @Test
+ void aPlanThrottleThatIsNotAnObjectIsRefused() {
+ ObjectNode props = planProps("gold", null, null);
+ props.putArray("Throttle").addObject().put("BurstLimit", 10);
+
+ assertCreateRefused(props, "Property [Throttle] expected type: JSONObject, found: JSONArray");
+ }
+
+ @Test
+ void aQuotaThatIsNotAnObjectIsRefused() {
+ ObjectNode props = planProps("gold", null, null);
+ props.put("Quota", "1000 a month");
+
+ assertCreateRefused(props, "Property [Quota] expected type: JSONObject, found: String");
+ }
+
+ @Test
+ void anApiStagesEntryThatIsNotAnObjectIsRefusedByItsIndex() {
+ ObjectNode props = planProps("gold", null, null, "api1:dev");
+ ((ArrayNode) props.get("ApiStages")).add("api1:prod");
+
+ assertCreateRefused(props, "Property [1] expected type: JSONObject, found: String");
+ }
+
+ @Test
+ void aStageThrottleThatIsNotAnObjectIsRefused() {
+ ObjectNode props = planProps("gold", null, null);
+ props.putObject("Throttle").put("BurstLimit", 10).put("RateLimit", 5);
+ ((ArrayNode) props.get("ApiStages")).addObject().put("ApiId", "api1").put("Stage", "dev")
+ .putArray("Throttle").addObject().put("BurstLimit", 3);
+
+ assertCreateRefused(props, "Property [Throttle] expected type: JSONObject, found: JSONArray");
+ }
+
+ @Test
+ void aMethodThrottleSettingThatIsNotAnObjectIsRefused() {
+ ObjectNode props = planProps("gold", null, null);
+ props.putObject("Throttle").put("BurstLimit", 10).put("RateLimit", 5);
+ ((ArrayNode) props.get("ApiStages")).addObject().put("ApiId", "api1").put("Stage", "dev")
+ .putObject("Throttle").put("/pets/GET", 3);
+
+ assertCreateRefused(props, "Property [/pets/GET] expected type: JSONObject, found: String");
+ }
+
+ @Test
+ void anUpdateWithAThrottleThatIsNotAnObjectLeavesThePlanUntouched() {
+ UsagePlan existing = plan("plan-1", "gold", null, Map.of());
+ when(apiGateway.getUsagePlan(REGION, "plan-1")).thenReturn(existing);
+ ObjectNode props = planProps("platinum", null, null);
+ props.put("Throttle", "fast");
+
+ AwsException e = assertThrows(AwsException.class,
+ () -> provisioner.provision(resource(PLAN, "Plan", "plan-1"), props, ctx("plan-1")));
+
+ assertEquals("ValidationError", e.getErrorCode());
+ assertEquals("Property [Throttle] expected type: JSONObject, found: String", e.getMessage());
+ verify(apiGateway, never()).updateUsagePlan(any(), any(), anyList());
+ verify(apiGateway, never()).replaceUsagePlanLimits(any(), any(), any());
+ }
+
@Test
void apiStagesThatIsNotAListIsRejected() {
ObjectNode props = mapper.createObjectNode().put("UsagePlanName", "gold").put("ApiStages", "api1:dev");
@@ -363,4 +507,304 @@ class ApiGatewayUsagePlanCfnProvisionerTest {
.when(apiGateway).deleteUsagePlanKey(REGION, "plan-1", "gone");
assertDoesNotThrow(() -> provisioner.delete(PLAN_KEY, "gone:plan-1", REGION));
}
+
+ // ──────────────────────────── Update rollback ────────────────────────────
+
+ /** plan-1 "gold" with no description, tag stack=v1 and {@link #ORIGINAL_LIMITS}. */
+ private static UsagePlan originalPlan() {
+ UsagePlan plan = plan("plan-1", "gold", null, Map.of("stack", "v1"));
+ plan.applyLimits(ORIGINAL_LIMITS);
+ return plan;
+ }
+
+ /** A template that changes every value of {@link #originalPlan}. */
+ private ObjectNode changedPlanProps() {
+ ObjectNode props = planProps("platinum", "new", "v2", "api1:prod");
+ props.putObject("Throttle").put("BurstLimit", "30").put("RateLimit", "15");
+ props.putObject("Quota").put("Limit", "50").put("Offset", "1").put("Period", "WEEK");
+ return props;
+ }
+
+ /**
+ * Stubs the plan's lookups and writes the way the in-memory store behaves: every call sees the one
+ * stored plan object, and a write changes that object in place.
+ */
+ private void storeHolds(UsagePlan stored) {
+ when(apiGateway.getUsagePlan(REGION, stored.getId())).thenReturn(stored);
+ when(apiGateway.updateUsagePlan(eq(REGION), eq(stored.getId()), anyList())).thenAnswer(inv -> {
+ List<Map<String, String>> ops = inv.getArgument(2);
+ for (Map<String, String> op : ops) {
+ switch (op.get("path")) {
+ case "/name" -> stored.setName(op.get("value"));
+ case "/description" -> stored.setDescription(op.get("value"));
+ default -> throw new IllegalArgumentException(op.toString());
+ }
+ }
+ return stored;
+ });
+ when(apiGateway.replaceUsagePlanLimits(eq(REGION), eq(stored.getId()), any())).thenAnswer(inv -> {
+ stored.applyLimits(inv.getArgument(2));
+ return stored;
+ });
+ when(apiGateway.replaceUsagePlanTags(eq(REGION), eq(stored.getId()), anyMap())).thenAnswer(inv -> {
+ Map<String, String> tags = inv.getArgument(2);
+ stored.setTags(new HashMap<>(tags));
+ return stored;
+ });
+ when(apiGateway.restoreUsagePlan(eq(REGION), eq(stored.getId()), any(), any(), any(), anyMap()))
+ .thenAnswer(inv -> {
+ stored.setName(inv.getArgument(2));
+ stored.setDescription(inv.getArgument(3));
+ stored.applyLimits(inv.getArgument(4));
+ Map<String, String> tags = inv.getArgument(5);
+ stored.setTags(new HashMap<>(tags));
+ return stored;
+ });
+ }
+
+ private static void assertOriginal(UsagePlan plan) {
+ assertEquals("gold", plan.getName());
+ assertNull(plan.getDescription());
+ assertEquals(ORIGINAL_LIMITS, plan.limits());
+ assertEquals(Map.of("stack", "v1"), plan.getTags());
+ }
+
+ @Test
+ void anUpdateSnapshotsThePlanBeforeItChangesIt() throws Exception {
+ storeHolds(originalPlan());
+ StackResource r = resource(PLAN, "Plan", "plan-1");
+
+ provisioner.provision(r, changedPlanProps(), ctx("plan-1"));
+
+ JsonNode snapshot = mapper.readTree(r.getAttributes().get(CfnRollback.USAGE_PLAN_UPDATE_SNAPSHOT_ATTR));
+ assertEquals(REGION, snapshot.path("region").asText());
+ assertEquals("plan-1", snapshot.path("id").asText());
+ assertEquals("gold", snapshot.path("name").asText());
+ assertTrue(snapshot.path("description").isNull(), snapshot.toString());
+ assertEquals(ORIGINAL_LIMITS, mapper.convertValue(snapshot.path("limits"), UsagePlan.Limits.class));
+ assertEquals(Map.of("stack", "v1"), mapper.convertValue(snapshot.path("tags"), Map.class));
+ }
+
+ @Test
+ void aFailedStackUpdatePutsThePlanBackAsItWas() {
+ UsagePlan stored = originalPlan();
+ storeHolds(stored);
+ StackResource r = resource(PLAN, "Plan", "plan-1");
+ provisioner.provision(r, changedPlanProps(), ctx("plan-1"));
+ assertEquals("platinum", stored.getName());
+ clearInvocations(apiGateway);
+
+ assertTrue(provisioner.rollbackUpdate(r));
+
+ verify(apiGateway).restoreUsagePlan(REGION, "plan-1", "gold", null, ORIGINAL_LIMITS, Map.of("stack", "v1"));
+ assertOriginal(stored);
+ assertFalse(r.getAttributes().containsKey(CfnRollback.USAGE_PLAN_UPDATE_SNAPSHOT_ATTR));
+ }
+
+ // A patch cannot set a description back to none; the service's replace of /description writes "".
+ @Test
+ void aRollbackRemovesADescriptionThePlanDidNotHave() {
+ UsagePlan stored = originalPlan();
+ storeHolds(stored);
+ StackResource r = resource(PLAN, "Plan", "plan-1");
+ ObjectNode describedOnly = planProps("gold", "new", "v1");
+ describedOnly.set("ApiStages", mapper.valueToTree(List.of(Map.of("ApiId", "api1", "Stage", "dev",
+ "Throttle", Map.of("/pets/GET", Map.of("BurstLimit", "10", "RateLimit", "5.0"))))));
+ describedOnly.putObject("Throttle").put("BurstLimit", "20").put("RateLimit", "10.0");
+ describedOnly.putObject("Quota").put("Limit", "1000").put("Period", "MONTH");
+ provisioner.provision(r, describedOnly, ctx("plan-1"));
+ assertEquals("new", stored.getDescription());
+ clearInvocations(apiGateway);
+
+ assertTrue(provisioner.rollbackUpdate(r));
+
+ assertNull(stored.getDescription());
+ verify(apiGateway, never()).updateUsagePlan(any(), any(), anyList());
+ }
+
+ @Test
+ void aRollbackWithoutASnapshotTouchesNothing() {
+ assertTrue(provisioner.rollbackUpdate(resource(PLAN, "Plan", "plan-1")));
+ verifyNoInteractions(apiGateway);
+ }
+
+ @Test
+ void aSnapshotFromAnEarlierUpdateIsReplacedSoAnUnchangedUpdateLeavesNothingToUndo() {
+ UsagePlan stored = originalPlan();
+ storeHolds(stored);
+ StackResource r = resource(PLAN, "Plan", "plan-1");
+ // An earlier update that changed the plan and committed, then one that changes nothing.
+ provisioner.provision(r, changedPlanProps(), ctx("plan-1"));
+ provisioner.provision(r, changedPlanProps(), ctx("plan-1"));
+ clearInvocations(apiGateway);
+
+ assertTrue(provisioner.rollbackUpdate(r));
+
+ verifyNoInteractions(apiGateway);
+ assertEquals("platinum", stored.getName());
+ }
+
+ // The provisioner does not compare the plan with its snapshot first: a read outside the service's
+ // patch lock could mix two revisions, so it always restores, which is idempotent and runs under it.
+ @Test
+ void aRollbackRestoresThePlanEvenWhenItAlreadyMatchesItsSnapshot() {
+ UsagePlan stored = originalPlan();
+ storeHolds(stored);
+ StackResource r = resource(PLAN, "Plan", "plan-1");
+ provisioner.provision(r, changedPlanProps(), ctx("plan-1"));
+ UsagePlan original = originalPlan();
+ stored.setName(original.getName());
+ stored.setDescription(original.getDescription());
+ stored.applyLimits(original.limits());
+ stored.setTags(original.getTags());
+ clearInvocations(apiGateway);
+
+ assertTrue(provisioner.rollbackUpdate(r));
+
+ verify(apiGateway).restoreUsagePlan(REGION, "plan-1", "gold", null, ORIGINAL_LIMITS, Map.of("stack", "v1"));
+ assertOriginal(stored);
+ assertFalse(r.getAttributes().containsKey(CfnRollback.USAGE_PLAN_UPDATE_SNAPSHOT_ATTR));
+ }
+
+ @Test
+ void aFailedStackUpdateDeletesThePlanItCreatedInPlaceOfOneDeletedOutOfBand() {
+ when(apiGateway.getUsagePlan(REGION, "gone")).thenThrow(NOT_FOUND);
+ when(apiGateway.createUsagePlan(eq(REGION), anyMap())).thenReturn(plan("plan-2", "gold", null, Map.of()));
+ StackResource r = resource(PLAN, "Plan", "gone");
+ r.getAttributes().put("Id", "gone");
+ provisioner.provision(r, planProps("gold", null, null), ctx("gone"));
+ assertEquals("plan-2", r.getPhysicalId());
+
+ assertTrue(provisioner.rollbackUpdate(r));
+
+ verify(apiGateway).deleteUsagePlan(REGION, "plan-2");
+ assertEquals("gone", r.getPhysicalId());
+ assertEquals("gone", r.getAttributes().get("Id"));
+ assertFalse(r.getAttributes().containsKey(CfnRollback.USAGE_PLAN_UPDATE_SNAPSHOT_ATTR));
+ }
+
+ @Test
+ void aFailedWriteDuringTheUpdatePutsTheWholePlanBack() {
+ UsagePlan stored = originalPlan();
+ storeHolds(stored);
+ RuntimeException failure = new IllegalStateException("store write failed");
+ // The limits change the stored plan and then fail to save, as a failing store write does.
+ doAnswer(inv -> {
+ stored.applyLimits(inv.getArgument(2));
+ throw failure;
+ }).when(apiGateway).replaceUsagePlanLimits(eq(REGION), eq("plan-1"), any());
+ StackResource r = resource(PLAN, "Plan", "plan-1");
+
+ RuntimeException thrown = assertThrows(RuntimeException.class,
+ () -> provisioner.provision(r, changedPlanProps(), ctx("plan-1")));
+
+ assertSame(failure, thrown);
+ verify(apiGateway).restoreUsagePlan(REGION, "plan-1", "gold", null, ORIGINAL_LIMITS, Map.of("stack", "v1"));
+ assertOriginal(stored);
+ assertFalse(r.getAttributes().containsKey(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR));
+ }
+
+ @Test
+ void aFailedUndoOfAFailedUpdateMarksTheRollbackFailed() {
+ storeHolds(originalPlan());
+ RuntimeException failure = new IllegalStateException("store write failed");
+ doThrow(failure).when(apiGateway).replaceUsagePlanTags(eq(REGION), eq("plan-1"), anyMap());
+ RuntimeException restoreFailure = new IllegalStateException("store write failed again");
+ doThrow(restoreFailure).when(apiGateway).restoreUsagePlan(eq(REGION), eq("plan-1"), any(), any(), any(),
+ anyMap());
+ StackResource r = resource(PLAN, "Plan", "plan-1");
+
+ RuntimeException thrown = assertThrows(RuntimeException.class,
+ () -> provisioner.provision(r, changedPlanProps(), ctx("plan-1")));
+
+ assertSame(failure, thrown);
+ assertTrue(List.of(thrown.getSuppressed()).contains(restoreFailure));
+ String reason = r.getAttributes().get(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR);
+ assertTrue(reason != null && reason.contains("plan-1") && reason.contains("store write failed again"),
+ String.valueOf(reason));
+ }
+
+ @Test
+ void aRestoreRethrowingTheUpdateFailureIsNotSuppressedInItself() {
+ storeHolds(originalPlan());
+ RuntimeException failure = new IllegalStateException("store write failed");
+ // The update and its restore both fail to save the plan with the same exception object.
+ doThrow(failure).when(apiGateway).replaceUsagePlanTags(eq(REGION), eq("plan-1"), anyMap());
+ doThrow(failure).when(apiGateway).restoreUsagePlan(eq(REGION), eq("plan-1"), any(), any(), any(), anyMap());
+ StackResource r = resource(PLAN, "Plan", "plan-1");
+
+ RuntimeException thrown = assertThrows(RuntimeException.class,
+ () -> provisioner.provision(r, changedPlanProps(), ctx("plan-1")));
+
+ assertSame(failure, thrown);
+ assertEquals(0, thrown.getSuppressed().length);
+ assertTrue(r.getAttributes().containsKey(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR));
+ }
+
+ // The snapshot is the plan read under the service's patch lock before the first write, not the
+ // provisioner's earlier lookup: here a patch renamed the plan between the two, and the rollback
+ // puts back the revision the locked read saw.
+ @Test
+ void theSnapshotIsTheRevisionReadUnderThePatchLockBeforeTheFirstWrite() {
+ UsagePlan stored = originalPlan();
+ storeHolds(stored);
+ UsagePlan patched = originalPlan();
+ patched.setName("renamed-by-a-patch");
+ doAnswer(inv -> {
+ Function<UsagePlan, Object> reader = inv.getArgument(2);
+ return reader.apply(patched);
+ }).when(apiGateway).readUsagePlan(eq(REGION), eq("plan-1"), any());
+ StackResource r = resource(PLAN, "Plan", "plan-1");
+
+ provisioner.provision(r, changedPlanProps(), ctx("plan-1"));
+
+ InOrder order = inOrder(apiGateway);
+ order.verify(apiGateway).readUsagePlan(eq(REGION), eq("plan-1"), any());
+ order.verify(apiGateway).updateUsagePlan(eq(REGION), eq("plan-1"), anyList());
+ assertTrue(provisioner.rollbackUpdate(r));
+ assertEquals("renamed-by-a-patch", stored.getName());
+ }
+
+ // An attempt that failed after its snapshot may have changed the plan, so the engine keeps it for
+ // rollbackUpdate; one without a snapshot changed nothing and the engine puts the previous resource back.
+ @Test
+ void aFailedUpdateIsKeptForItsRollbackOnlyWhileItHasASnapshot() {
+ storeHolds(originalPlan());
+ StackResource r = resource(PLAN, "Plan", "plan-1");
+ assertFalse(provisioner.retainsFailedUpdateState(r));
+
+ provisioner.provision(r, changedPlanProps(), ctx("plan-1"));
+ assertTrue(provisioner.retainsFailedUpdateState(r));
+
+ assertTrue(provisioner.rollbackUpdate(r));
+ assertFalse(provisioner.retainsFailedUpdateState(r));
+ }
+
+ @Test
+ void aCommittedUpdateDropsItsSnapshot() {
+ storeHolds(originalPlan());
+ StackResource r = resource(PLAN, "Plan", "plan-1");
+ provisioner.provision(r, changedPlanProps(), ctx("plan-1"));
+
+ UpdateCleanupResult result = provisioner.completeUpdate(r);
+
+ assertFalse(result.applicable());
+ assertFalse(r.getAttributes().containsKey(CfnRollback.USAGE_PLAN_UPDATE_SNAPSHOT_ATTR));
+ assertFalse(provisioner.retainsFailedUpdateState(r));
+ }
+
+ @Test
+ void aRollbackOfAPlanDeletedSinceTheUpdateFails() {
+ storeHolds(originalPlan());
+ StackResource r = resource(PLAN, "Plan", "plan-1");
+ provisioner.provision(r, changedPlanProps(), ctx("plan-1"));
+ doThrow(NOT_FOUND).when(apiGateway).getUsagePlan(REGION, "plan-1");
+ doThrow(NOT_FOUND).when(apiGateway).restoreUsagePlan(eq(REGION), eq("plan-1"), any(), any(), any(),
+ anyMap());
+
+ AwsException e = assertThrows(AwsException.class, () -> provisioner.rollbackUpdate(r));
+
+ assertEquals("NotFoundException", e.getErrorCode());
+ assertTrue(r.getAttributes().containsKey(CfnRollback.USAGE_PLAN_UPDATE_SNAPSHOT_ATTR));
+ }
}