Light, fluffy, and always free - The AWS Local Emulator alternative
fix(apigateway): patch a usage plan's throttle, quota and stage throttles (#6418)
Mario Toffia74cab790f238
diff --git a/docs/services/api-gateway.md b/docs/services/api-gateway.mdindex 3192d9c847..69ef92a5aa 100644--- a/docs/services/api-gateway.md+++ b/docs/services/api-gateway.md@@ -580,9 +580,19 @@ curl http://localhost:4566/restapis/$API_ID/dev/_user_request_/users`CreateUsagePlan`, `GetUsagePlan`, `GetUsagePlans` and `UpdateUsagePlan` return a plan's `apiStages`,as `[]` for a plan with none, with each stage's per-method `throttle`, and the plan's `throttle` and-`quota`. `CreateUsagePlan` validates them 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 listing a-stage twice is a `ConflictException`.+`quota`. `UpdateUsagePlan` supports every patch path AWS documents for them except `/productCode`:+`/apiStages` add and remove, `/apiStages/<apiId>:<stage>/throttle` and its+`/<resourcePath>/<httpMethod>[/burstLimit|/rateLimit]` paths, `/throttle/burstLimit`,+`/throttle/rateLimit`, `/throttle`, `/quota/limit`, `/quota/offset`, `/quota/period` and `/quota`.+A whole `/apiStages/<apiId>:<stage>/throttle` value is a JSON map of method key to `burstLimit` and+`rateLimit`, read as API Gateway reads it: a fractional burst limit is truncated, numeric text is+taken, and a burst limit outside the int range, non-numeric text, another JSON type or an unknown+member is `Invalid API Stage throttle specified for usageplan`.+Usage plan product codes are not supported, so an `add`, `replace` or `remove` of `/productCode` is a+`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`.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/ApiGatewayController.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayController.javaindex 3e2c4b5e0f..d1be061e18 100644--- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayController.java+++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayController.java@@ -913,11 +913,35 @@ public class ApiGatewayController {@Path("/usageplans/{usagePlanId}")public Response updateUsagePlan(@Context HttpHeaders headers, @PathParam("usagePlanId") String usagePlanId, String body) {String region = regionResolver.resolveRegion(headers);+ checkUpdateUsagePlanInput(body);List<Map<String, String>> patchOperations = parsePatchOperations(body);UsagePlan plan = service.updateUsagePlan(region, usagePlanId, patchOperations);return Response.ok(toUsagePlanNode(plan, true).toString()).type(MediaType.APPLICATION_JSON).build();}+ /**+ * UpdateUsagePlan's input model, measured on API Gateway: a member of the wrong JSON type is a+ * SerializationException, then every op outside the model's enum is listed in one+ * ValidationException. A body that is not a JSON object is left to {@link #parsePatchOperations}.+ */+ private void checkUpdateUsagePlanInput(String body) {+ JsonNode node;+ try {+ node = objectMapper.readTree(body);+ } catch (JsonProcessingException | IllegalArgumentException expected) {+ // parsePatchOperations answers a body that is not JSON, as it does for every PATCH operation.+ return;+ }+ if (node == null || !node.isObject()) {+ return;+ }+ ApiGatewayRequestShapes.check("UpdateUsagePlan", node);+ List<String> violations = UsagePlanLimits.opViolations(node.path("patchOperations"));+ if (!violations.isEmpty()) {+ throw SmithyValidationErrors.validationException(violations);+ }+ }+@DELETE@Path("/usageplans/{usagePlanId}")public Response deleteUsagePlan(@Context HttpHeaders headers, @PathParam("usagePlanId") String usagePlanId) {diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.javaindex 75083d81d4..b48312a231 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@@ -132,6 +132,11 @@ public class ApiGatewayService implements ResourceProvider {*/private final Object domainNameLock = new Object();private final Object vpcLinkTagLock = new Object();+ /**+ * Guards a usage plan patch. A patch writes back the stages, throttle and quota it read, so two at+ * once would restore one another's stale values.+ */+ private final Object usagePlanLock = new Object();private final TlsCertificateManager certificateManager;private final EmulatorConfig config;private final RegionResolver regionResolver;@@ -1541,62 +1546,56 @@ public class ApiGatewayService implements ResourceProvider {}public UsagePlan updateUsagePlan(String region, String usagePlanId, List<Map<String, String>> patchOperations) {- UsagePlan plan = getUsagePlan(region, usagePlanId);- // The store hands back live objects, so every op is validated against pending values first and- // only applied once the whole patch is known to be good.- String newName = plan.getName();- String newDescription = plan.getDescription();- List<UsagePlan.ApiStage> newApiStages = new ArrayList<>(plan.getApiStages());- if (patchOperations != null) {- for (Map<String, String> op : patchOperations) {- if (op == null) {- throw new AwsException("BadRequestException", "Invalid patch operation", 400);- }- String opType = op.get("op");- String path = op.get("path");- String value = op.get("value");- if (path == null || value == null) {- throw new AwsException("BadRequestException", "Missing path or value", 400);- }- if ("/apiStages".equals(path)) {- // AWS models stage membership as add/remove of an "apiId:stage" pair.- if (!"add".equals(opType) && !"remove".equals(opType)) {- throw new AwsException("BadRequestException", "Invalid operation", 400);+ synchronized (usagePlanLock) {+ UsagePlan plan = getUsagePlan(region, usagePlanId);+ // The store hands back live objects, so every op is validated against pending values first and+ // the plan only changes once the whole patch is known to be good.+ String newName = plan.getName();+ String newDescription = plan.getDescription();+ UsagePlanLimits.Patch limits = new UsagePlanLimits.Patch(plan);+ if (patchOperations != null) {+ for (Map<String, String> op : patchOperations) {+ if (op == null) {+ throw new AwsException("BadRequestException", "Invalid patch operation", 400);+ }+ String opType = op.get("op");+ String path = op.get("path");+ String value = op.get("value");+ UsagePlanLimits.requirePatchOp(opType);+ if (path == null) {+ throw new AwsException("BadRequestException", "Missing path or value", 400);}- String[] parts = value.split(":", 2);- if (parts.length != 2 || parts[0].isEmpty() || parts[1].isEmpty()) {+ // A remove of /throttle, /quota or a stage throttle carries no value, so only the name+ // and description ops below insist on one.+ if (limits.apply(opType, path, value)) {+ continue;+ }+ // API Gateway allows /productCode on add, remove and replace; Floci has no product codes to set.+ if ("/productCode".equals(path)) {+ throw new AwsException("BadRequestException", "Usage plan product codes are not supported", 400);+ }+ // API Gateway takes /name and /description through replace only.+ if (!"replace".equals(opType) || (!"/name".equals(path) && !"/description".equals(path))) {+ throw UsagePlanLimits.invalidPatchPath(opType, path);+ }+ if (value == null) {throw new AwsException("BadRequestException",- "apiStages value must be in the form apiId:stage", 400);+ "/name".equals(path) ? "Usage Plan name must be specified" : "Missing path or value", 400);}- // Matched by API and stage alone: a stage's method throttle is not part of its identity.- int index = UsagePlanLimits.indexOf(newApiStages, parts[0], parts[1]);- if ("add".equals(opType)) {- if (index < 0) {- newApiStages.add(new UsagePlan.ApiStage(parts[0], parts[1]));- }- } else if (index >= 0) {- newApiStages.remove(index);+ if ("/name".equals(path)) {+ newName = value;+ } else {+ newDescription = value;}- continue;- }- if (!"add".equals(opType) && !"replace".equals(opType)) {- throw new AwsException("BadRequestException", "Invalid operation", 400);- }- if ("/name".equals(path)) {- newName = value;- } else if ("/description".equals(path)) {- newDescription = value;- } else {- throw new AwsException("BadRequestException", "Unsupported path: " + path, 400);}}+ UsagePlan.Limits newLimits = limits.result();+ plan.setName(newName);+ plan.setDescription(newDescription);+ plan.applyLimits(newLimits);+ usagePlanStore.put(usagePlanKey(region, usagePlanId), plan);+ return plan;}- plan.setName(newName);- plan.setDescription(newDescription);- // A new list, not an in-place refill, so a concurrent read copies either the old stages or the new ones.- plan.setApiStages(newApiStages);- usagePlanStore.put(usagePlanKey(region, usagePlanId), plan);- return plan;}public List<UsagePlan> getUsagePlans(String region) {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.javaindex 031ec8848f..b612d425cc 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@@ -1,7 +1,11 @@package io.github.hectorvent.floci.services.apigateway;+import com.fasterxml.jackson.core.JsonProcessingException;+import com.fasterxml.jackson.databind.JsonNode;+import com.fasterxml.jackson.databind.ObjectMapper;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.common.SmithyValidationErrors;+import io.github.hectorvent.floci.services.apigateway.model.UsagePlan;import io.github.hectorvent.floci.services.apigateway.model.UsagePlan.ApiStage;import io.github.hectorvent.floci.services.apigateway.model.UsagePlan.Limits;import io.github.hectorvent.floci.services.apigateway.model.UsagePlan.Quota;@@ -16,8 +20,9 @@ import java.util.Map;import java.util.Objects;/**- * A usage plan's API stages, throttle and quota: read from a {@code CreateUsagePlan} request, with- * the errors API Gateway answers. Every message was measured against API Gateway in eu-west-1.+ * A usage plan's API stages, throttle and quota: read from a {@code CreateUsagePlan} request and+ * patched by {@code UpdateUsagePlan}, with the errors API Gateway answers. Every message was+ * measured against API Gateway in eu-west-1.*/public final class UsagePlanLimits {@@ -32,6 +37,29 @@ public final class UsagePlanLimits {"Can not define fine grain throttling if usage plan throttle is not defined.";private static final String QUOTA_OFFSET_NOT_NUMERIC ="Usage Plan quota offset must be numeric and less than the number of time units in the period";+ private static final ObjectMapper JSON = new ObjectMapper();+ private static final String STAGE_THROTTLE_INVALID = "Invalid API Stage throttle specified for usageplan";+ private static final String PLAN_THROTTLE_UNDER_FINE_GRAIN =+ "Can not remove usage plan throttling if fine grain throttling is defined for one or more api-stages.";+ private static final String STAGES_PREFIX = "/apiStages/";+ private static final String STAGE_THROTTLE = "/throttle";+ /** The input model's op enum, in the order its violation lists it. */+ private static final List<String> OP_ENUM = List.of("add", "remove", "move", "test", "replace", "copy");+ /** The paths UpdateUsagePlan allows per op, verbatim from API Gateway's error ("apidId" is AWS's own spelling). */+ private static final Map<String, String> PATCH_PATHS = Map.of(+ "add", "[/apiStages, /productCode, /quota/period, /quota/limit, /quota/offset, /throttle/rateLimit, "+ + "/throttle/burstLimit, "+ + "/apiStages/<apidId:stageName>/throttle/<resourcePath>/<httpMethod>/rateLimit, "+ + "/apiStages/<apidId:stageName>/throttle/<resourcePath>/<httpMethod>/burstLimit, "+ + "/apiStages/<apidId:stageName>/throttle]",+ "replace", "[/name, /description, /productCode, /quota/period, /quota/limit, /quota/offset, "+ + "/throttle/rateLimit, /throttle/burstLimit, "+ + "/apiStages/<apidId:stageName>/throttle/<resourcePath>/<httpMethod>/rateLimit, "+ + "/apiStages/<apidId:stageName>/throttle/<resourcePath>/<httpMethod>/burstLimit, "+ + "/apiStages/<apidId:stageName>/throttle]",+ "remove", "[/apiStages, /quota, /throttle, /productCode, "+ + "/apiStages/<apidId:stageName>/throttle/<resourcePath>/<httpMethod>, "+ + "/apiStages/<apidId:stageName>/throttle]");private UsagePlanLimits() {}@@ -46,7 +74,8 @@ public final class UsagePlanLimits {Object period = quotaSettings == null ? null : quotaSettings.get("period");String periodViolation = periodViolation(request);if (periodViolation != null) {- // CreateUsagePlan's input model refuses an unknown period before any of API Gateway's own checks.+ // CreateUsagePlan's input model refuses an unknown period before any of API Gateway's own checks;+ // UpdateUsagePlan has no such model and answers with quota()'s message instead.throw SmithyValidationErrors.validationException(periodViolation);}Throttle throttle = request.get("throttle") instanceof Map<?, ?> settings ? throttle(settings) : null;@@ -112,15 +141,19 @@ public final class UsagePlanLimits {for (Map.Entry<String, Map<?, ?>> entry : settingsByKey.entrySet()) {String[] resourcePathAndMethod = methodKey(entry.getKey());if (resourcePathAndMethod[1].isEmpty()) {- // No space after the comma: API Gateway's own formatting.- throw new AwsException(BAD_REQUEST, "Invalid method {resourcePath: " + resourcePathAndMethod[0]- + ",method: " + resourcePathAndMethod[1] + "} specified", 400);+ throw new AwsException(BAD_REQUEST, "Invalid method " + methodRef(resourcePathAndMethod) + " specified",+ 400);}methods.put(entry.getKey(), throttle(entry.getValue()));}return methods;}+ /** {@code {resourcePath: <path>,method: <method>}}, with no space after the comma: API Gateway's own formatting. */+ private static String methodRef(String[] resourcePathAndMethod) {+ return "{resourcePath: " + resourcePathAndMethod[0] + ",method: " + resourcePathAndMethod[1] + "}";+ }+/*** {@code [resourcePath, method]} of a method throttle key, split as API Gateway splits it: at the* last slash, unless that slash leads or ends the key, which leaves the method empty. Floci does@@ -168,16 +201,34 @@ public final class UsagePlanLimits {}static int burstLimit(Object value) {- Integer burst = value == null ? null : intValue(value);- if (burst == null || burst < 0) {+ int burst = numericBurstLimit(value);+ if (burst < 0) {throw new AwsException(BAD_REQUEST, THROTTLE_INVALID, 400);}return burst;}static double rateLimit(Object value) {+ double rate = numericRateLimit(value);+ if (rate < 0) {+ throw new AwsException(BAD_REQUEST, THROTTLE_INVALID, 400);+ }+ return rate;+ }++ /** A whole number, negative or not; the sign is the caller's to check. */+ static int numericBurstLimit(Object value) {+ Integer burst = value == null ? null : intValue(value);+ if (burst == null) {+ throw new AwsException(BAD_REQUEST, THROTTLE_INVALID, 400);+ }+ return burst;+ }++ /** A finite number, negative or not; the sign is the caller's to check. */+ static double numericRateLimit(Object value) {Double rate = value == null ? null : doubleValue(value);- if (rate == null || rate < 0) {+ if (rate == null) {throw new AwsException(BAD_REQUEST, THROTTLE_INVALID, 400);}return rate;@@ -201,6 +252,368 @@ public final class UsagePlanLimits {"API Stage is already defined for Usage Plan: " + apiId + ":" + stage, 409);}+ /**+ * The stage, throttle and quota part of one {@code UpdateUsagePlan} request. Ops apply in order to+ * pending values and {@link #result()} validates what they add up to, the way API Gateway accepts a+ * quota built from separate limit and period ops. Nothing touches the plan itself.+ */+ public static final class Patch {++ private final String usagePlanId;+ private final List<ApiStage> stages;+ private Throttle throttle;+ private boolean hasQuota;+ private Object quotaLimit;+ private Object quotaOffset;+ private Object quotaPeriod;++ public Patch(UsagePlan plan) {+ usagePlanId = plan.getId();+ stages = new ArrayList<>(plan.getApiStages());+ throttle = plan.getThrottle();+ Quota quota = plan.getQuota();+ hasQuota = quota != null;+ if (hasQuota) {+ quotaLimit = quota.limit();+ quotaOffset = quota.offset();+ quotaPeriod = quota.period();+ }+ }++ /** Applies one op; false when {@code path} is not a stage, throttle or quota path. */+ public boolean apply(String op, String path, String value) {+ requirePatchOp(op);+ if ("/apiStages".equals(path)) {+ applyStage(op, value);+ } else if (path.startsWith(STAGES_PREFIX)) {+ applyStageThrottle(op, path, value);+ } else if ("/throttle".equals(path) || path.startsWith("/throttle/")) {+ applyThrottle(op, path, value);+ } else if ("/quota".equals(path) || path.startsWith("/quota/")) {+ applyQuota(op, path, value);+ } else {+ return false;+ }+ return true;+ }++ public Limits result() {+ Quota quota = hasQuota ? quota(quotaLimit, quotaOffset, quotaPeriod) : null;+ // A plan throttle field may go negative between ops, as on API Gateway; only the outcome must not.+ if (throttle != null && (throttle.burstLimit() < 0 || throttle.rateLimit() < 0)) {+ throw new AwsException(BAD_REQUEST, THROTTLE_INVALID, 400);+ }+ if (throttle == null && hasMethodThrottles(stages)) {+ throw new AwsException(BAD_REQUEST, FINE_GRAIN_WITHOUT_PLAN_THROTTLE, 400);+ }+ return new Limits(stages, throttle, quota);+ }++ private void applyStage(String op, String value) {+ if (!"add".equals(op) && !"remove".equals(op)) {+ throw invalidPatchPath(op, "/apiStages");+ }+ String[] ref = stageRef(value);+ if ("add".equals(op)) {+ if (indexOf(stages, ref[0], ref[1]) >= 0) {+ throw alreadyDefined(ref[0], ref[1]);+ }+ stages.add(new ApiStage(ref[0], ref[1]));+ } else {+ stages.remove(stageIndex(ref));+ }+ }++ private void applyStageThrottle(String op, String path, String value) {+ String rest = path.substring(STAGES_PREFIX.length());+ int slash = rest.indexOf('/');+ String suffix = slash < 0 ? "" : rest.substring(slash);+ if (!STAGE_THROTTLE.equals(suffix) && !suffix.startsWith(STAGE_THROTTLE + "/")) {+ throw invalidPatchPath(op, path);+ }+ // A method throttle comes off whole: remove takes no single rateLimit or burstLimit of it.+ String last = path.substring(path.lastIndexOf('/') + 1);+ if ("remove".equals(op) && suffix.startsWith(STAGE_THROTTLE + "/")+ && ("rateLimit".equals(last) || "burstLimit".equals(last))) {+ throw invalidPatchPath(op, path);+ }+ int index = stageIndex(stageRef(rest.substring(0, slash)));+ ApiStage stage = stages.get(index);+ Map<String, Throttle> methods = stage.throttle() == null ? null : new LinkedHashMap<>(stage.throttle());+ if (STAGE_THROTTLE.equals(suffix)) {+ methods = switch (op == null ? "" : op) {+ case "add" -> {+ if (methods != null) {+ throw new AwsException("ConflictException",+ "API Stage throttle is already defined for usageplan " + usagePlanId, 409);+ }+ yield methodThrottles(value);+ }+ case "replace" -> {+ if (methods == null) {+ throw new AwsException("NotFoundException",+ "API Stage throttle not found for usageplan " + usagePlanId, 404);+ }+ yield methodThrottles(value);+ }+ case "remove" -> null;+ default -> throw invalidPatchPath(op, path);+ };+ } else {+ methods = patchMethodThrottle(methods, op, path, suffix.substring(STAGE_THROTTLE.length() + 1), value);+ }+ stages.set(index, new ApiStage(stage.apiId(), stage.stage(), methods));+ }++ /**+ * {@code target} is {@code <resourcePath>/<httpMethod>[/<limit>]}; the key is the raw text, so+ * {@code //pets/GET/rateLimit} after {@code /throttle} names {@code /pets/GET} and {@code ~1} is+ * not decoded, as on API Gateway.+ */+ private Map<String, Throttle> patchMethodThrottle(Map<String, Throttle> methods, String op, String path,+ String target, String value) {+ Map<String, Throttle> patched = methods == null ? new LinkedHashMap<>() : methods;+ int slash = target.lastIndexOf('/');+ if ("remove".equals(op)) {+ // As for add and replace, a path that names no method is invalid, before any key is looked up.+ if (methodKey(target)[1].isEmpty()) {+ throw invalidPatchPath(op, path);+ }+ if (patched.remove(target) == null) {+ throw new AwsException("NotFoundException", "Method throttle does not exist for {resourcePath: "+ + (slash < 0 ? target : target.substring(0, slash)) + ", method: "+ + (slash < 0 ? "" : target.substring(slash + 1)) + "} for usageplan " + usagePlanId, 404);+ }+ return patched;+ }+ requireAddOrReplace(op, path);+ String field = slash < 0 ? "" : target.substring(slash + 1);+ if (!"burstLimit".equals(field) && !"rateLimit".equals(field)) {+ throw invalidPatchPath(op, path);+ }+ String key = target.substring(0, slash);+ if (methodKey(key)[1].isEmpty()) {+ throw invalidPatchPath(op, path);+ }+ Throttle current = patched.getOrDefault(key, new Throttle(0, 0.0));+ patched.put(key, "burstLimit".equals(field)+ ? new Throttle(burstLimit(value), current.rateLimit())+ : new Throttle(current.burstLimit(), rateLimit(value)));+ return patched;+ }++ private void applyThrottle(String op, String path, String value) {+ switch (path) {+ case "/throttle" -> {+ if (!"remove".equals(op)) {+ throw invalidPatchPath(op, path);+ }+ // Checked against the stages as they stand at this op, not at the end of the patch.+ if (hasMethodThrottles(stages)) {+ throw new AwsException(BAD_REQUEST, PLAN_THROTTLE_UNDER_FINE_GRAIN, 400);+ }+ throttle = null;+ }+ case "/throttle/burstLimit", "/throttle/rateLimit" -> {+ requireAddOrReplace(op, path);+ Throttle current = throttle == null ? new Throttle(0, 0.0) : throttle;+ throttle = path.endsWith("burstLimit")+ ? new Throttle(numericBurstLimit(value), current.rateLimit())+ : new Throttle(current.burstLimit(), numericRateLimit(value));+ }+ default -> throw invalidPatchPath(op, path);+ }+ }++ private void applyQuota(String op, String path, String value) {+ if ("/quota".equals(path)) {+ if (!"remove".equals(op)) {+ throw invalidPatchPath(op, path);+ }+ hasQuota = false;+ quotaLimit = null;+ quotaOffset = null;+ quotaPeriod = null;+ return;+ }+ requireAddOrReplace(op, path);+ switch (path) {+ case "/quota/limit" -> quotaLimit = value;+ case "/quota/offset" -> {+ // Without a value API Gateway refuses the op rather than resetting the offset to 0.+ if (value == null) {+ throw new AwsException(BAD_REQUEST, QUOTA_OFFSET_NOT_NUMERIC, 400);+ }+ quotaOffset = value;+ }+ case "/quota/period" -> quotaPeriod = value;+ default -> throw invalidPatchPath(op, path);+ }+ hasQuota = true;+ }++ /** The index of the plan's stage named by {@code ref}, or API Gateway's 404 when the plan has none. */+ private int stageIndex(String[] ref) {+ int index = indexOf(stages, ref[0], ref[1]);+ if (index < 0) {+ throw new AwsException("NotFoundException", "Invalid API Stage {api: " + ref[0] + ", stage: "+ + ref[1] + "} specified for usageplan " + usagePlanId, 404);+ }+ return index;+ }+ }++ /**+ * A {@code /apiStages/<apiId>:<stage>/throttle} value: a JSON map of method key to limits, read as+ * API Gateway reads it, which is not how a create body's numbers are read. A burst limit takes a+ * JSON number in int range, truncated, or whole-number text; a rate limit takes a finite number or+ * numeric text. Anything else, an unknown member included, refuses the whole value. Every value is+ * read before any key is checked, then each key's method before its limits and their sign.+ */+ private static Map<String, Throttle> methodThrottles(String json) {+ JsonNode root;+ try {+ root = json == null ? null : JSON.readTree(json);+ } catch (JsonProcessingException e) {+ throw stageThrottleInvalid();+ }+ if (root == null || !root.isObject()) {+ throw stageThrottleInvalid();+ }+ Map<String, Throttle> read = new LinkedHashMap<>();+ for (Map.Entry<String, JsonNode> entry : root.properties()) {+ JsonNode settings = entry.getValue();+ if (!settings.isNull() && !settings.isObject()) {+ throw stageThrottleInvalid();+ }+ read.put(entry.getKey(), settings.isNull() ? null : stageThrottle(settings));+ }+ Map<String, Throttle> methods = new LinkedHashMap<>();+ for (Map.Entry<String, Throttle> entry : read.entrySet()) {+ String[] resourcePathAndMethod = methodKey(entry.getKey());+ if (resourcePathAndMethod[1].isEmpty()) {+ throw new AwsException(BAD_REQUEST, "Invalid method " + methodRef(resourcePathAndMethod) + " specified",+ 400);+ }+ Throttle throttle = entry.getValue();+ if (throttle == null) {+ throw new AwsException(BAD_REQUEST,+ "Invalid throttle setting specified for method " + methodRef(resourcePathAndMethod), 400);+ }+ if (throttle.burstLimit() < 0 || throttle.rateLimit() < 0) {+ throw new AwsException(BAD_REQUEST, THROTTLE_INVALID, 400);+ }+ methods.put(entry.getKey(), throttle);+ }+ return methods;+ }++ /** One method's limits in a stage throttle value, sign unchecked; a limit left out or null reads 0. */+ private static Throttle stageThrottle(JsonNode settings) {+ for (Map.Entry<String, JsonNode> member : settings.properties()) {+ if (!"burstLimit".equals(member.getKey()) && !"rateLimit".equals(member.getKey())) {+ throw stageThrottleInvalid();+ }+ }+ JsonNode burst = settings.path("burstLimit");+ JsonNode rate = settings.path("rateLimit");+ return new Throttle(burst.isMissingNode() || burst.isNull() ? 0 : stageBurstLimit(burst),+ rate.isMissingNode() || rate.isNull() ? 0.0 : stageRateLimit(rate));+ }++ private static int stageBurstLimit(JsonNode value) {+ if (value.isNumber() && value.canConvertToInt()) {+ return value.intValue();+ }+ if (value.isTextual()) {+ try {+ return Integer.parseInt(value.textValue());+ } catch (NumberFormatException e) {+ throw stageThrottleInvalid();+ }+ }+ throw stageThrottleInvalid();+ }++ private static double stageRateLimit(JsonNode value) {+ double rate;+ if (value.isNumber()) {+ rate = value.doubleValue();+ } else if (value.isTextual()) {+ try {+ rate = Double.parseDouble(value.textValue());+ } catch (NumberFormatException e) {+ throw stageThrottleInvalid();+ }+ } else {+ throw stageThrottleInvalid();+ }+ // API Gateway answers a rate beyond the double range, such as 1e400, with a 500 InternalFailure, a+ // fault rather than a contract; Floci refuses it as an invalid stage throttle instead.+ if (!Double.isFinite(rate)) {+ throw stageThrottleInvalid();+ }+ return rate;+ }++ private static AwsException stageThrottleInvalid() {+ return new AwsException(BAD_REQUEST, STAGE_THROTTLE_INVALID, 400);+ }++ /** {@code apiId:stage} split in two, or API Gateway's 400 for anything else. */+ private static String[] stageRef(String value) {+ String[] parts = value == null ? new String[0] : value.split(":", 2);+ if (parts.length != 2 || parts[0].isEmpty() || parts[1].isEmpty()) {+ throw new AwsException(BAD_REQUEST, "Invalid API Stage specified", 400);+ }+ return parts;+ }++ private static void requireAddOrReplace(String op, String path) {+ if (!"add".equals(op) && !"replace".equals(op)) {+ throw invalidPatchPath(op, path);+ }+ }++ /**+ * UpdateUsagePlan's input-model violations, one per op outside the model's enum, each at its+ * 1-based index, as API Gateway lists them before its own op check. A missing or null op is not+ * one; {@link #requirePatchOp} refuses it.+ */+ public static List<String> opViolations(JsonNode patchOperations) {+ List<String> violations = new ArrayList<>();+ for (int i = 0; i < patchOperations.size(); i++) {+ JsonNode op = patchOperations.get(i).path("op");+ if (op.isTextual() && !OP_ENUM.contains(op.textValue())) {+ violations.add(SmithyValidationErrors.notInEnum(op.textValue(),+ "updateUsagePlanInput.patchOperations." + (i + 1) + ".member.op", OP_ENUM));+ }+ }+ return violations;+ }++ /** API Gateway refuses any op but add, remove and replace, a missing one included, before it reads the path. */+ public static void requirePatchOp(String op) {+ if (op == null || !PATCH_PATHS.containsKey(op)) {+ throw invalidPatchOp();+ }+ }++ private static AwsException invalidPatchOp() {+ return new AwsException(BAD_REQUEST, "Invalid patch operation specified. Must be 'add'|'remove'|'replace'", 400);+ }++ /** API Gateway's 400 for a path {@code op} does not allow, listing the paths it does. */+ public static AwsException invalidPatchPath(String op, String path) {+ String allowed = op == null ? null : PATCH_PATHS.get(op);+ if (allowed == null) {+ return invalidPatchOp();+ }+ // Two spaces after "path", as API Gateway writes it.+ return new AwsException(BAD_REQUEST,+ "Invalid patch path '" + path + "' specified for op '" + op + "'. Must be one of: " + allowed, 400);+ }+/*** {@code value} as an int, or null when it is not one. A number is read from its decimal digits,* rounded toward negative infinity and clamped to the int range, as API Gateway reads a JSONdiff --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.javaindex b4629ec98d..b3d134bd9e 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@@ -42,12 +42,16 @@ public class UsagePlan {public Quota getQuota() { return quota; }public void setQuota(Quota quota) { this.quota = quota; }- /** The plan's stages, throttle and quota as one value, for comparing and replacing them together. */- public Limits limits() {+ /**+ * The plan's stages, throttle and quota as one value, for comparing and replacing them together.+ * Synchronised with {@link #applyLimits} so a reader never sees parts of two revisions.+ */+ public synchronized Limits limits() {return new Limits(List.copyOf(apiStages), throttle, quota);}- public void applyLimits(Limits limits) {+ /** Synchronised with {@link #limits} because it writes three fields that are only valid together. */+ public synchronized void applyLimits(Limits limits) {this.apiStages = new ArrayList<>(limits.apiStages());this.throttle = limits.throttle();this.quota = limits.quota();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.javaindex fecfcbf2d3..421425cc42 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@@ -117,12 +117,12 @@ public class ApiGatewayUsagePlanCfnProvisioner implements CfnResourceProvisionerList<UsagePlan.ApiStage> desired = apiStages(props, ctx);List<UsagePlan.ApiStage> current = List.copyOf(existing.getApiStages());for (UsagePlan.ApiStage stage : current) {- if (!desired.contains(stage)) {+ if (!listsStage(desired, stage)) {patches.add(patch("remove", "/apiStages", stage.apiId() + KEY_ID_SEPARATOR + stage.stage()));}}for (UsagePlan.ApiStage stage : desired) {- if (!current.contains(stage)) {+ if (!listsStage(current, stage)) {patches.add(patch("add", "/apiStages", stage.apiId() + KEY_ID_SEPARATOR + stage.stage()));}}@@ -139,6 +139,15 @@ public class ApiGatewayUsagePlanCfnProvisioner implements CfnResourceProvisionerrecordPlan(r, plan);}+ /**+ * 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.+ */+ 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()));+ }+/** 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<>();diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayUsagePlanCrudIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayUsagePlanCrudIntegrationTest.javaindex 8743c0c984..777d76820c 100644--- a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayUsagePlanCrudIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayUsagePlanCrudIntegrationTest.java@@ -119,46 +119,71 @@ class ApiGatewayUsagePlanCrudIntegrationTest {}@Test- void aStageWithAMethodThrottleIsMatchedByApiAndStage() {+ void patchesReachTheWireWithApiGatewaysErrors() {String usagePlanId = given().contentType(ContentType.JSON)- .body("{\"name\":\"stage-remove\",\"throttle\":{\"burstLimit\":20,\"rateLimit\":10},"+ .body("{\"name\":\"patched\",\"throttle\":{\"burstLimit\":20,\"rateLimit\":10},"+ + "\"quota\":{\"limit\":1000,\"period\":\"MONTH\"},"+ "\"apiStages\":[{\"apiId\":\"a1\",\"stage\":\"v1\","- + "\"throttle\":{\"/pets/GET\":{\"burstLimit\":10,\"rateLimit\":5}}},"- + "{\"apiId\":\"a1\",\"stage\":\"v2\"}]}")+ + "\"throttle\":{\"/pets/GET\":{\"burstLimit\":10,\"rateLimit\":5}}}]}").post("/usageplans").then().statusCode(201)+ .body("throttle.burstLimit", equalTo(20))+ .body("quota.limit", equalTo(1000))+ .body("quota.offset", equalTo(0)).body("apiStages[0].throttle['/pets/GET'].burstLimit", equalTo(10)).extract().path("id");given().contentType(ContentType.JSON)- .body("{\"patchOperations\":[{\"op\":\"add\",\"path\":\"/apiStages\",\"value\":\"a1:v1\"}]}")+ .body("{\"patchOperations\":["+ + "{\"op\":\"add\",\"path\":\"/apiStages\",\"value\":\"a1:v2\"},"+ + "{\"op\":\"replace\",\"path\":\"/apiStages/a1:v1/throttle//pets/GET/burstLimit\",\"value\":\"8\"},"+ + "{\"op\":\"remove\",\"path\":\"/quota\"}]}").patch("/usageplans/{usagePlanId}", usagePlanId).then().statusCode(200).body("apiStages", hasSize(2))- .body("apiStages[0].throttle['/pets/GET'].burstLimit", equalTo(10));+ .body("apiStages[0].throttle['/pets/GET'].burstLimit", equalTo(8))+ .body("apiStages[1].throttle", nullValue())+ .body("quota", nullValue());given().contentType(ContentType.JSON)- .body("{\"patchOperations\":[{\"op\":\"remove\",\"path\":\"/apiStages\",\"value\":\"a1:v1\"}]}")+ .body("{\"patchOperations\":[{\"op\":\"add\",\"path\":\"/apiStages\",\"value\":\"a1:v2\"}]}")+ .patch("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(409)+ .body("__type", equalTo("ConflictException"))+ .body("message", equalTo("API Stage is already defined for Usage Plan: a1:v2"));++ given().contentType(ContentType.JSON)+ .body("{\"patchOperations\":[{\"op\":\"remove\",\"path\":\"/apiStages\",\"value\":\"a1:v9\"}]}").patch("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(404)+ .body("__type", equalTo("NotFoundException"))+ .body("message", equalTo("Invalid API Stage {api: a1, stage: v9} specified for usageplan "+ + usagePlanId));++ // A refused patch leaves the plan as it was.+ given().get("/usageplans/{usagePlanId}", usagePlanId).then().statusCode(200)- .body("apiStages", hasSize(1))- .body("apiStages[0].stage", equalTo("v2"));+ .body("apiStages", hasSize(2));++ given().contentType(ContentType.JSON)+ .body("{\"patchOperations\":[{\"op\":\"remove\",\"path\":\"/apiStages\",\"value\":\"a1:v1\"}]}")+ .patch("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(200);given().get("/usageplans/{usagePlanId}", usagePlanId).then().statusCode(200).body("apiStages", hasSize(1))- .body("apiStages[0].stage", equalTo("v2"))- .body("throttle.burstLimit", equalTo(20));- }+ .body("apiStages[0].stage", equalTo("v2"));- @Test- void anUnknownQuotaPeriodOnCreateIsAValidationException() {given().contentType(ContentType.JSON).body("{\"name\":\"bad-period\",\"quota\":{\"limit\":1,\"period\":\"day\"}}").post("/usageplans")@@ -167,6 +192,49 @@ class ApiGatewayUsagePlanCrudIntegrationTest {.body("__type", equalTo("ValidationException"));}+ @Test+ void nameAndDescriptionOnlyTakeReplace() {+ String usagePlanId = given()+ .contentType(ContentType.JSON)+ .body("{\"name\":\"replace-only\",\"description\":\"kept\"}")+ .post("/usageplans")+ .then()+ .statusCode(201)+ .extract()+ .path("id");++ // "apidId" is AWS's own spelling in these lists.+ given().contentType(ContentType.JSON)+ .body("{\"patchOperations\":[{\"op\":\"add\",\"path\":\"/name\",\"value\":\"renamed\"}]}")+ .patch("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(400)+ .body("__type", equalTo("BadRequestException"))+ .body("message", equalTo("Invalid patch path '/name' specified for op 'add'. Must be one of: "+ + "[/apiStages, /productCode, /quota/period, /quota/limit, /quota/offset, "+ + "/throttle/rateLimit, /throttle/burstLimit, "+ + "/apiStages/<apidId:stageName>/throttle/<resourcePath>/<httpMethod>/rateLimit, "+ + "/apiStages/<apidId:stageName>/throttle/<resourcePath>/<httpMethod>/burstLimit, "+ + "/apiStages/<apidId:stageName>/throttle]"));++ given().contentType(ContentType.JSON)+ .body("{\"patchOperations\":[{\"op\":\"remove\",\"path\":\"/description\"}]}")+ .patch("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(400)+ .body("__type", equalTo("BadRequestException"))+ .body("message", equalTo("Invalid patch path '/description' specified for op 'remove'. "+ + "Must be one of: [/apiStages, /quota, /throttle, /productCode, "+ + "/apiStages/<apidId:stageName>/throttle/<resourcePath>/<httpMethod>, "+ + "/apiStages/<apidId:stageName>/throttle]"));++ given().get("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(200)+ .body("name", equalTo("replace-only"))+ .body("description", equalTo("kept"));+ }+// Measured on API Gateway: GetUsagePlan and UpdateUsagePlan carry "tags": {}, CreateUsagePlan and// the GetUsagePlans items leave an empty map out.@Test@@ -208,6 +276,107 @@ class ApiGatewayUsagePlanCrudIntegrationTest {.body("message", equalTo("Invalid Usage Plan ID specified"));}+ @Test+ void anOpOtherThanAddRemoveOrReplaceIsRefusedBeforeItsPath() {+ String usagePlanId = given()+ .contentType(ContentType.JSON)+ .body("{\"name\":\"ops-only\"}")+ .post("/usageplans")+ .then()+ .statusCode(201)+ .extract()+ .path("id");++ for (String operation : new String[] {+ "{\"op\":\"copy\",\"path\":\"/name\",\"value\":\"x\"}",+ "{\"op\":\"move\",\"path\":\"/name\",\"value\":\"x\"}",+ "{\"op\":\"test\",\"path\":\"/name\",\"value\":\"x\"}",+ "{\"path\":\"/name\",\"value\":\"x\"}",+ "{\"op\":\"move\"}"}) {+ given().contentType(ContentType.JSON)+ .body("{\"patchOperations\":[" + operation + "]}")+ .patch("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(400)+ .body("__type", equalTo("BadRequestException"))+ .body("message", equalTo("Invalid patch operation specified. Must be 'add'|'remove'|'replace'"));+ }++ given().get("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(200)+ .body("name", equalTo("ops-only"));+ }++ @Test+ void aProductCodeOpIsRefusedAsNotSupported() {+ String usagePlanId = given()+ .contentType(ContentType.JSON)+ .body("{\"name\":\"product-code\"}")+ .post("/usageplans")+ .then()+ .statusCode(201)+ .extract()+ .path("id");++ for (String operation : new String[] {+ "{\"op\":\"add\",\"path\":\"/productCode\",\"value\":\"code\"}",+ "{\"op\":\"replace\",\"path\":\"/productCode\",\"value\":\"code\"}",+ "{\"op\":\"remove\",\"path\":\"/productCode\"}"}) {+ given().contentType(ContentType.JSON)+ .body("{\"patchOperations\":[" + operation + "]}")+ .patch("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(400)+ .body("__type", equalTo("BadRequestException"))+ .body("message", equalTo("Usage plan product codes are not supported"));+ }++ // An op other than add, remove or replace is still refused before its path is read.+ given().contentType(ContentType.JSON)+ .body("{\"patchOperations\":[{\"op\":\"copy\",\"path\":\"/productCode\",\"value\":\"code\"}]}")+ .patch("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(400)+ .body("__type", equalTo("BadRequestException"))+ .body("message", equalTo("Invalid patch operation specified. Must be 'add'|'remove'|'replace'"));+ }++ @Test+ void aValuelessOffsetOrNameIsRefusedAndLeavesThePlanAsItWas() {+ String usagePlanId = given()+ .contentType(ContentType.JSON)+ .body("{\"name\":\"valueless\",\"quota\":{\"limit\":1000,\"offset\":3,\"period\":\"MONTH\"}}")+ .post("/usageplans")+ .then()+ .statusCode(201)+ .extract()+ .path("id");++ given().contentType(ContentType.JSON)+ .body("{\"patchOperations\":[{\"op\":\"replace\",\"path\":\"/quota/offset\"}]}")+ .patch("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(400)+ .body("__type", equalTo("BadRequestException"))+ .body("message", equalTo("Usage Plan quota offset must be numeric and less than the number of time "+ + "units in the period"));++ given().contentType(ContentType.JSON)+ .body("{\"patchOperations\":[{\"op\":\"replace\",\"path\":\"/name\"}]}")+ .patch("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(400)+ .body("__type", equalTo("BadRequestException"))+ .body("message", equalTo("Usage Plan name must be specified"));++ given().get("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(200)+ .body("name", equalTo("valueless"))+ .body("quota.offset", equalTo(3));+ }+@Testvoid aStageWithoutAnApiIdIsRefusedAndNoPlanIsStored() {given().contentType(ContentType.JSON)@@ -375,6 +544,175 @@ class ApiGatewayUsagePlanCrudIntegrationTest {+ "constraint: Member must satisfy enum value set: [MONTH, WEEK, DAY]");}+ // Measured on API Gateway: a fractional burst limit is truncated, numeric text is taken, and a burst+ // limit outside the int range refuses the whole value.+ @Test+ void aStageThrottleValueIsReadAsApiGatewayReadsIt() {+ String usagePlanId = given()+ .contentType(ContentType.JSON)+ .body("{\"name\":\"stage-throttle-value\",\"throttle\":{\"burstLimit\":20,\"rateLimit\":10},"+ + "\"apiStages\":[{\"apiId\":\"a1\",\"stage\":\"v1\","+ + "\"throttle\":{\"/pets/GET\":{\"burstLimit\":10,\"rateLimit\":5}}}]}")+ .post("/usageplans")+ .then()+ .statusCode(201)+ .extract()+ .path("id");++ given().contentType(ContentType.JSON)+ .body("{\"patchOperations\":[{\"op\":\"replace\",\"path\":\"/apiStages/a1:v1/throttle\","+ + "\"value\":\"{\\\"/pets/GET\\\":{\\\"burstLimit\\\":1.5,\\\"rateLimit\\\":\\\"5.5\\\"}}\"}]}")+ .patch("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(200)+ .body("apiStages[0].throttle['/pets/GET'].burstLimit", equalTo(1))+ .body("apiStages[0].throttle['/pets/GET'].rateLimit", equalTo(5.5f));++ given().contentType(ContentType.JSON)+ .body("{\"patchOperations\":[{\"op\":\"replace\",\"path\":\"/apiStages/a1:v1/throttle\","+ + "\"value\":\"{\\\"/pets/GET\\\":{\\\"burstLimit\\\":3000000000}}\"}]}")+ .patch("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(400)+ .body("__type", equalTo("BadRequestException"))+ .body("message", equalTo("Invalid API Stage throttle specified for usageplan"));++ given().get("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(200)+ .body("apiStages[0].throttle['/pets/GET'].burstLimit", equalTo(1));+ }++ // Measured on API Gateway: an op outside the model's enum is a ValidationException and leaves the plan as it was.+ @Test+ void anOpOutsideTheEnumIsAValidationExceptionAndLeavesThePlanAsItWas() {+ String usagePlanId = createNamedPlan("op-enum");+ for (String op : new String[] {"frobnicate", "ADD", ""}) {+ given().contentType(ContentType.JSON)+ .body("{\"patchOperations\":[{\"op\":\"" + op + "\",\"path\":\"/name\",\"value\":\"changed\"}]}")+ .patch("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(400)+ .body("__type", equalTo("ValidationException"))+ .body("message", equalTo("1 validation error detected: Value '" + op + "' at "+ + "'updateUsagePlanInput.patchOperations.1.member.op' failed to satisfy constraint: "+ + "Member must satisfy enum value set: [add, remove, move, test, replace, copy]"));+ assertPlanNamed(usagePlanId, "op-enum");+ }+ }++ // Measured on API Gateway: an op that is a number is refused by the deserializer.+ @Test+ void aNumericOpIsASerializationExceptionAndLeavesThePlanAsItWas() {+ String usagePlanId = createNamedPlan("op-number");+ given().contentType(ContentType.JSON)+ .body("{\"patchOperations\":[{\"op\":5,\"path\":\"/name\",\"value\":\"changed\"}]}")+ .patch("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(400)+ .body("__type", equalTo("SerializationException"))+ .body("message", equalTo("class com.amazon.coral.value.json.numbers.TruncatingBigNumber "+ + "can not be converted to an String"));+ assertPlanNamed(usagePlanId, "op-number");+ }++ // copy is in the model's enum, so it passes the input model and the usage plan refuses it.+ @Test+ void anOpInTheEnumThatAUsagePlanDoesNotTakeKeepsItsBusinessMessage() {+ String usagePlanId = createNamedPlan("op-copy");+ given().contentType(ContentType.JSON)+ .body("{\"patchOperations\":[{\"op\":\"copy\",\"path\":\"/name\",\"value\":\"changed\"}]}")+ .patch("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(400)+ .body("__type", equalTo("BadRequestException"))+ .body("message", equalTo("Invalid patch operation specified. Must be 'add'|'remove'|'replace'"));+ assertPlanNamed(usagePlanId, "op-copy");+ }++ // Measured on API Gateway: a refused op rolls back the whole request, the ops before it included.+ @Test+ void aRefusedPatchRollsBackEveryOpInTheRequest() {+ String usagePlanId = given()+ .contentType(ContentType.JSON)+ .body("{\"name\":\"compat10\",\"throttle\":{\"burstLimit\":20,\"rateLimit\":10},"+ + "\"quota\":{\"limit\":1000,\"offset\":0,\"period\":\"MONTH\"}}")+ .post("/usageplans")+ .then()+ .statusCode(201)+ .extract()+ .path("id");++ given().contentType(ContentType.JSON)+ .body("{\"patchOperations\":["+ + "{\"op\":\"replace\",\"path\":\"/name\",\"value\":\"compat10-renamed\"},"+ + "{\"op\":\"replace\",\"path\":\"/throttle/burstLimit\",\"value\":\"30\"},"+ + "{\"op\":\"replace\",\"path\":\"/quota/period\",\"value\":\"DAY\"},"+ + "{\"op\":\"replace\",\"path\":\"/quota/offset\",\"value\":\"3\"}]}")+ .patch("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(400)+ .body("__type", equalTo("BadRequestException"))+ .body("message", equalTo("Usage Plan quota offset must be zero in the DAY period"));++ given().get("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(200)+ .body("name", equalTo("compat10"))+ .body("throttle.burstLimit", equalTo(20))+ .body("quota.limit", equalTo(1000))+ .body("quota.offset", equalTo(0))+ .body("quota.period", equalTo("MONTH"));+ }++ // Measured on API Gateway: removing a method throttle path that names no method is an invalid path.+ @Test+ void removingAMethodThrottlePathThatNamesNoMethodIsAnInvalidPathAndLeavesTheStage() {+ String usagePlanId = given()+ .contentType(ContentType.JSON)+ .body("{\"name\":\"remove-no-method\",\"throttle\":{\"burstLimit\":20,\"rateLimit\":10},"+ + "\"apiStages\":[{\"apiId\":\"a1\",\"stage\":\"v1\","+ + "\"throttle\":{\"/pets/GET\":{\"burstLimit\":10,\"rateLimit\":5}}}]}")+ .post("/usageplans")+ .then()+ .statusCode(201)+ .extract()+ .path("id");++ given().contentType(ContentType.JSON)+ .body("{\"patchOperations\":[{\"op\":\"remove\",\"path\":\"/apiStages/a1:v1/throttle/nope\"}]}")+ .patch("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(400)+ .body("__type", equalTo("BadRequestException"))+ .body("message", equalTo("Invalid patch path '/apiStages/a1:v1/throttle/nope' specified for op "+ + "'remove'. Must be one of: [/apiStages, /quota, /throttle, /productCode, "+ + "/apiStages/<apidId:stageName>/throttle/<resourcePath>/<httpMethod>, "+ + "/apiStages/<apidId:stageName>/throttle]"));++ given().get("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(200)+ .body("apiStages[0].throttle['/pets/GET'].burstLimit", equalTo(10));+ }++ private static String createNamedPlan(String name) {+ return given().contentType(ContentType.JSON)+ .body("{\"name\":\"" + name + "\"}")+ .post("/usageplans")+ .then()+ .statusCode(201)+ .extract()+ .path("id");+ }++ private static void assertPlanNamed(String usagePlanId, String name) {+ given().get("/usageplans/{usagePlanId}", usagePlanId)+ .then()+ .statusCode(200)+ .body("name", equalTo(name));+ }+private static int planCount() {return given().get("/usageplans").then().statusCode(200).extract().path("item.size()");}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.javanew file mode 100644index 0000000000..1532021262--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayUsagePlanServiceTest.java@@ -0,0 +1,128 @@+package io.github.hectorvent.floci.services.apigateway;++import io.github.hectorvent.floci.config.EmulatorConfig;+import io.github.hectorvent.floci.config.TlsCertificateManager;+import io.github.hectorvent.floci.core.common.RegionResolver;+import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;+import io.github.hectorvent.floci.core.storage.StorageFactory;+import io.github.hectorvent.floci.services.apigateway.model.UsagePlan;+import org.junit.jupiter.api.Test;++import java.util.ArrayList;+import java.util.HashMap;+import java.util.HashSet;+import java.util.List;+import java.util.Map;+import java.util.Set;+import java.util.concurrent.CountDownLatch;+import java.util.concurrent.ExecutorService;+import java.util.concurrent.Executors;+import java.util.concurrent.Future;+import java.util.concurrent.TimeUnit;+import java.util.concurrent.atomic.AtomicLong;++import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertTrue;+import static org.mockito.ArgumentMatchers.any;+import static org.mockito.ArgumentMatchers.anyString;+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.when;++class ApiGatewayUsagePlanServiceTest {++ private static final String REGION = "us-east-1";+ private static final String ACCOUNT = "000000000000";+ private static final int REPLACEMENTS = 200_000;++ private static ApiGatewayService service() {+ Map<String, AccountAwareStorageBackend<?>> stores = new HashMap<>();+ StorageFactory storageFactory = mock(StorageFactory.class);+ when(storageFactory.create(anyString(), anyString(), any())).thenAnswer(invocation ->+ stores.computeIfAbsent(invocation.getArgument(1, String.class),+ ignored -> AccountAwareStorageBackend.inMemory(ACCOUNT)));+ return new ApiGatewayService(storageFactory, mock(EmulatorConfig.class, RETURNS_DEEP_STUBS),+ mock(TlsCertificateManager.class), new RegionResolver(REGION, ACCOUNT));+ }++ // Every patch writes back the stages it read, so without serialising them concurrent adds drop each other.+ @Test+ void concurrentStagePatchesKeepEveryStage() throws Exception {+ ApiGatewayService service = service();+ String planId = service.createUsagePlan(REGION, Map.of("name", "concurrent")).getId();+ Set<String> expected = new HashSet<>();+ for (int i = 0; i < 128; i++) {+ expected.add("api:s" + i);+ }+ CountDownLatch start = new CountDownLatch(1);+ try (ExecutorService executor = Executors.newFixedThreadPool(8)) {+ List<Future<?>> writers = new ArrayList<>();+ for (int worker = 0; worker < 8; worker++) {+ int offset = worker * 16;+ writers.add(executor.submit(() -> {+ assertTrue(start.await(5, TimeUnit.SECONDS));+ for (int i = offset; i < offset + 16; i++) {+ service.updateUsagePlan(REGION, planId,+ List.of(Map.of("op", "add", "path", "/apiStages", "value", "api:s" + i)));+ }+ return null;+ }));+ }+ start.countDown();+ for (Future<?> writer : writers) {+ writer.get(10, TimeUnit.SECONDS);+ }+ }++ Set<String> stages = new HashSet<>();+ for (UsagePlan.ApiStage stage : service.getUsagePlan(REGION, planId).getApiStages()) {+ stages.add(stage.apiId() + ":" + stage.stage());+ }+ assertEquals(expected, stages);+ }++ // applyLimits writes stages, throttle and quota one after another, so an unserialised read can mix two revisions.+ @Test+ void limitsReadDuringAReplaceAreOneRevision() throws Exception {+ UsagePlan.Limits a = new UsagePlan.Limits(+ List.of(new UsagePlan.ApiStage("api", "v1", Map.of("/pets/GET", new UsagePlan.Throttle(10, 5.0)))),+ new UsagePlan.Throttle(20, 10.0), new UsagePlan.Quota(1000, 0, "MONTH"));+ UsagePlan.Limits b = new UsagePlan.Limits(List.of(new UsagePlan.ApiStage("api", "v2")), null, null);+ UsagePlan plan = new UsagePlan();+ plan.applyLimits(a);+ CountDownLatch start = new CountDownLatch(1);+ CountDownLatch written = new CountDownLatch(1);+ AtomicLong mixed = new AtomicLong();+ try (ExecutorService executor = Executors.newFixedThreadPool(2)) {+ Future<?> writer = executor.submit(() -> {+ assertTrue(start.await(5, TimeUnit.SECONDS));+ try {+ for (int i = 0; i < REPLACEMENTS; i++) {+ plan.applyLimits(i % 2 == 0 ? b : a);+ }+ } finally {+ written.countDown();+ }+ return null;+ });+ Future<List<UsagePlan.Limits>> reader = executor.submit(() -> {+ List<UsagePlan.Limits> samples = new ArrayList<>();+ assertTrue(start.await(5, TimeUnit.SECONDS));+ while (written.getCount() > 0) {+ UsagePlan.Limits seen = plan.limits();+ if (!seen.equals(a) && !seen.equals(b)) {+ mixed.incrementAndGet();+ if (samples.size() < 3) {+ samples.add(seen);+ }+ }+ }+ return samples;+ });+ start.countDown();+ writer.get(60, TimeUnit.SECONDS);+ List<UsagePlan.Limits> samples = reader.get(60, TimeUnit.SECONDS);+ assertEquals(0L, mixed.get(), "limits mixing two revisions, for example " + samples);+ }+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/UsagePlanLimitsTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/UsagePlanLimitsTest.javaindex 8b2c94373c..e4e6f9f8a0 100644--- a/src/test/java/io/github/hectorvent/floci/services/apigateway/UsagePlanLimitsTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/UsagePlanLimitsTest.java@@ -1,5 +1,6 @@package io.github.hectorvent.floci.services.apigateway;+import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.services.apigateway.model.UsagePlan;@@ -12,6 +13,7 @@ import org.junit.jupiter.api.function.Executable;import org.junit.jupiter.params.ParameterizedTest;import org.junit.jupiter.params.provider.Arguments;import org.junit.jupiter.params.provider.MethodSource;+import org.junit.jupiter.params.provider.ValueSource;import java.math.BigDecimal;import java.time.Duration;@@ -25,9 +27,32 @@ import static org.junit.jupiter.api.Assertions.assertFalse;import static org.junit.jupiter.api.Assertions.assertNull;import static org.junit.jupiter.api.Assertions.assertThrows;import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;+import static org.junit.jupiter.api.Assertions.assertTrue;class UsagePlanLimitsTest {+ // "apidId" is AWS's own spelling in these lists.+ private static final String ADD_PATHS = "[/apiStages, /productCode, /quota/period, /quota/limit, /quota/offset, "+ + "/throttle/rateLimit, /throttle/burstLimit, "+ + "/apiStages/<apidId:stageName>/throttle/<resourcePath>/<httpMethod>/rateLimit, "+ + "/apiStages/<apidId:stageName>/throttle/<resourcePath>/<httpMethod>/burstLimit, "+ + "/apiStages/<apidId:stageName>/throttle]";+ private static final String REPLACE_PATHS = "[/name, /description, /productCode, /quota/period, /quota/limit, "+ + "/quota/offset, /throttle/rateLimit, /throttle/burstLimit, "+ + "/apiStages/<apidId:stageName>/throttle/<resourcePath>/<httpMethod>/rateLimit, "+ + "/apiStages/<apidId:stageName>/throttle/<resourcePath>/<httpMethod>/burstLimit, "+ + "/apiStages/<apidId:stageName>/throttle]";+ private static final String REMOVE_PATHS = "[/apiStages, /quota, /throttle, /productCode, "+ + "/apiStages/<apidId:stageName>/throttle/<resourcePath>/<httpMethod>, "+ + "/apiStages/<apidId:stageName>/throttle]";++ private static final String INVALID_OP = "Invalid patch operation specified. Must be 'add'|'remove'|'replace'";++ /** API Gateway's message for a path the op does not allow; note the two spaces after "path". */+ private static String invalidPath(String op, String path, String allowed) {+ return "Invalid patch path '" + path + "' specified for op '" + op + "'. Must be one of: " + allowed;+ }+private static Map<String, Object> request(Object... keyValues) {Map<String, Object> request = new HashMap<>();for (int i = 0; i < keyValues.length; i += 2) {@@ -235,6 +260,8 @@ class UsagePlanLimitsTest {UsagePlanLimits.fromRequest(stageThrottle("//GET")).apiStages().get(0).throttle());assertEquals(Map.of("/pets/GET/extra", new Throttle(5, 2.0)),UsagePlanLimits.fromRequest(stageThrottle("/pets/GET/extra")).apiStages().get(0).throttle());+ assertEquals(new Throttle(0, 1.0), patch(fixture(), "replace", "/apiStages/a:v1/throttle///GET/rateLimit", "1")+ .apiStages().get(0).throttle().get("//GET"));}@Test@@ -250,4 +277,353 @@ class UsagePlanLimitsTest {void createRefusesWhatApiGatewayRefuses(Map<String, Object> request, String code, int status, String message) {assertRefused(() -> UsagePlanLimits.fromRequest(request), code, status, message);}++ /** id plan1; stage a:v1 with /pets/GET at 10/5.0; plan throttle 20/10.0; quota 1000/3/MONTH. */+ private static UsagePlan fixture() {+ UsagePlan plan = new UsagePlan();+ plan.setId("plan1");+ plan.applyLimits(new Limits(List.of(new ApiStage("a", "v1", Map.of("/pets/GET", new Throttle(10, 5.0)))),+ new Throttle(20, 10.0), new Quota(1000, 3, "MONTH")));+ return plan;+ }++ /** Applies op/path/value triples (value may be null) the way updateUsagePlan does. */+ private static Limits patch(UsagePlan plan, String... opPathValue) {+ UsagePlanLimits.Patch patch = new UsagePlanLimits.Patch(plan);+ for (int i = 0; i < opPathValue.length; i += 3) {+ assertTrue(patch.apply(opPathValue[i], opPathValue[i + 1], opPathValue[i + 2]), opPathValue[i + 1]);+ }+ return patch.result();+ }++ @Test+ void addAndRemoveStagesInOneRequest() {+ Limits limits = patch(fixture(), "add", "/apiStages", "a:v2", "add", "/apiStages", "a:v3",+ "remove", "/apiStages", "a:v3");+ assertEquals(List.of(new ApiStage("a", "v1", Map.of("/pets/GET", new Throttle(10, 5.0))),+ new ApiStage("a", "v2")), limits.apiStages());+ }++ @Test+ void throttleFieldsCreateAPlanThrottleWhenThereIsNone() {+ UsagePlan plan = new UsagePlan();+ plan.setId("p");+ assertEquals(new Throttle(30, 0.0), patch(plan, "replace", "/throttle/burstLimit", "30").throttle());+ assertEquals(new Throttle(30, 15.0), patch(plan, "replace", "/throttle/burstLimit", "30",+ "add", "/throttle/rateLimit", "15").throttle());+ }++ @Test+ void aQuotaIsBuiltFromSeparateOpsAndRemovedWithoutAValue() {+ UsagePlan plan = new UsagePlan();+ plan.setId("p");+ assertEquals(new Quota(10, 0, "DAY"), patch(plan, "replace", "/quota/limit", "10",+ "replace", "/quota/period", "DAY").quota());+ assertNull(patch(fixture(), "replace", "/quota/limit", "5", "remove", "/quota", null).quota());+ assertNull(patch(plan, "remove", "/quota", null).quota());+ assertEquals(new Quota(2000, 1, "WEEK"), patch(fixture(), "replace", "/quota/limit", "2000",+ "replace", "/quota/period", "WEEK", "replace", "/quota/offset", "1").quota());+ }++ @Test+ void methodThrottleOpsUseTheRawResourcePathAsKey() {+ Limits limits = patch(fixture(),+ "replace", "/apiStages/a:v1/throttle//pets/GET/burstLimit", "8",+ "add", "/apiStages/a:v1/throttle//pets/POST/rateLimit", "3");+ assertEquals(Map.of("/pets/GET", new Throttle(8, 5.0), "/pets/POST", new Throttle(0, 3.0)),+ limits.apiStages().get(0).throttle());+ }++ @Test+ void removingTheLastMethodLeavesAnEmptyStageThrottle() {+ Limits limits = patch(fixture(), "remove", "/apiStages/a:v1/throttle//pets/GET", null);+ assertEquals(Map.of(), limits.apiStages().get(0).throttle());+ // An empty stage throttle is not fine-grain throttling, so the plan throttle can go.+ assertNull(patch(fixture(), "remove", "/apiStages/a:v1/throttle//pets/GET", null,+ "remove", "/throttle", null).throttle());+ }++ @Test+ void aWholeStageThrottleIsReplacedAndRemoved() {+ assertEquals(Map.of("/a/GET", new Throttle(1, 2.0)), patch(fixture(), "replace", "/apiStages/a:v1/throttle",+ "{\"/a/GET\":{\"burstLimit\":1,\"rateLimit\":2}}").apiStages().get(0).throttle());+ assertNull(patch(fixture(), "remove", "/apiStages/a:v1/throttle", null,+ "remove", "/apiStages/a:v1/throttle", null).apiStages().get(0).throttle());+ }++ @Test+ void aWholeStageThrottleIsReplacedOnlyWhereOneExists() {+ assertRefused(() -> patch(fixture(), "add", "/apiStages", "a:v2",+ "replace", "/apiStages/a:v2/throttle", "{\"/pets/POST\":{\"burstLimit\":3,\"rateLimit\":2}}"),+ "NotFoundException", 404, "API Stage throttle not found for usageplan plan1");+ assertEquals(Map.of("/pets/POST", new Throttle(3, 2.0)), patch(fixture(), "add", "/apiStages", "a:v2",+ "add", "/apiStages/a:v2/throttle", "{\"/pets/POST\":{\"burstLimit\":3,\"rateLimit\":2}}")+ .apiStages().get(1).throttle());+ }++ @Test+ void aPlanThrottleMayPassThroughANegativeValueWithinOnePatch() {+ assertEquals(new Throttle(20, 5.0), patch(fixture(), "replace", "/throttle/rateLimit", "-1",+ "replace", "/throttle/rateLimit", "5").throttle());+ assertEquals(new Throttle(5, 10.0), patch(fixture(), "replace", "/throttle/burstLimit", "-1",+ "replace", "/throttle/burstLimit", "5").throttle());+ }++ @Test+ void aPlanThrottleIsNumericAtEachOpAndNonNegativeAtTheEnd() {+ String throttle = "Usage Plan throttle rate limit and burst limit must be numeric and greater than zero";+ assertRefused(() -> patch(fixture(), "replace", "/throttle/rateLimit", "5",+ "replace", "/throttle/rateLimit", "-1"), "BadRequestException", 400, throttle);+ assertRefused(() -> patch(fixture(), "replace", "/throttle/rateLimit", "-1"), "BadRequestException", 400,+ throttle);+ UsagePlan plan = new UsagePlan();+ plan.setId("p");+ plan.applyLimits(new Limits(List.of(), new Throttle(20, 10.0), null));+ assertRefused(() -> patch(plan, "replace", "/throttle/burstLimit", "abc", "remove", "/throttle", null),+ "BadRequestException", 400, throttle);+ // A method throttle limit is checked in full at every op.+ assertRefused(() -> patch(fixture(), "replace", "/apiStages/a:v1/throttle//pets/GET/rateLimit", "-1",+ "replace", "/apiStages/a:v1/throttle//pets/GET/rateLimit", "5"), "BadRequestException", 400, throttle);+ }++ @Test+ void removingThePlanThrottleIsCheckedAgainstTheStagesAtThatOp() {+ assertRefused(() -> patch(fixture(), "remove", "/throttle", null, "replace", "/throttle/burstLimit", "5"),+ "BadRequestException", 400,+ "Can not remove usage plan throttling if fine grain throttling is defined for one or more api-stages.");+ Limits limits = patch(fixture(), "remove", "/apiStages", "a:v1", "remove", "/throttle", null);+ assertNull(limits.throttle());+ assertEquals(List.of(), limits.apiStages());+ }++ @Test+ void aWholeStageThrottleAddRefusesAKeyWithoutAMethod() {+ assertRefused(() -> patch(fixture(), "add", "/apiStages", "a:v2", "add", "/apiStages/a:v2/throttle",+ "{\"\":{\"burstLimit\":1,\"rateLimit\":1}}"),+ "BadRequestException", 400, "Invalid method {resourcePath: ,method: } specified");+ }++ @Test+ void anEmptyWholeStageThrottleIsKeptAsAnEmptyMap() {+ assertEquals(Map.of(), patch(fixture(), "replace", "/apiStages/a:v1/throttle", "{}")+ .apiStages().get(0).throttle());+ assertEquals(Map.of(), patch(fixture(), "add", "/apiStages", "a:v2", "add", "/apiStages/a:v2/throttle", "{}")+ .apiStages().get(1).throttle());+ }++ @Test+ void aValuelessStageThrottleAddIsRefused() {+ assertRefused(() -> patch(fixture(), "add", "/apiStages", "a:v2", "add", "/apiStages/a:v2/throttle", null),+ "BadRequestException", 400, "Invalid API Stage throttle specified for usageplan");+ }++ @Test+ void nameAndDescriptionAreNotLimitPaths() {+ assertFalse(new UsagePlanLimits.Patch(fixture()).apply("replace", "/name", "x"));+ assertFalse(new UsagePlanLimits.Patch(fixture()).apply("replace", "/description", "x"));+ }++ static Stream<Arguments> refusedPatches() {+ String throttle = "Usage Plan throttle rate limit and burst limit must be numeric and greater than zero";+ return Stream.of(+ Arguments.of("add", "/apiStages", "a:v1", "ConflictException", 409,+ "API Stage is already defined for Usage Plan: a:v1"),+ Arguments.of("remove", "/apiStages", "a:v9", "NotFoundException", 404,+ "Invalid API Stage {api: a, stage: v9} specified for usageplan plan1"),+ Arguments.of("add", "/apiStages", "bad", "BadRequestException", 400, "Invalid API Stage specified"),+ Arguments.of("replace", "/apiStages", "a:v2", "BadRequestException", 400,+ invalidPath("replace", "/apiStages", REPLACE_PATHS)),+ Arguments.of("remove", "/throttle", null, "BadRequestException", 400,+ "Can not remove usage plan throttling if fine grain throttling is defined for one or more "+ + "api-stages."),+ Arguments.of("replace", "/throttle/burstLimit", "abc", "BadRequestException", 400, throttle),+ Arguments.of("replace", "/throttle/burstLimit", "-1", "BadRequestException", 400, throttle),+ Arguments.of("replace", "/throttle/burstLimit", "1.5", "BadRequestException", 400, throttle),+ Arguments.of("replace", "/quota/period", "YEAR", "BadRequestException", 400,+ "Invalid Usage Plan quota period specified. Must be one of [DAY, WEEK, MONTH]"),+ Arguments.of("replace", "/quota/period", "DAY", "BadRequestException", 400,+ "Usage Plan quota offset must be zero in the DAY period"),+ Arguments.of("replace", "/quota/offset", "x", "BadRequestException", 400,+ "Usage Plan quota offset must be numeric and less than the number of time units in the period"),+ Arguments.of("replace", "/quota/limit", "-5", "BadRequestException", 400,+ "Usage Plan quota limit must be a non-negative numeric"),+ Arguments.of("replace", "/quota/offset", null, "BadRequestException", 400,+ "Usage Plan quota offset must be numeric and less than the number of time units in the period"),+ Arguments.of("add", "/apiStages", null, "BadRequestException", 400, "Invalid API Stage specified"),+ Arguments.of("replace", "/apiStages/a:v1/throttle", null, "BadRequestException", 400,+ "Invalid API Stage throttle specified for usageplan"),+ // API Gateway answers this with a 500 InternalFailure, a fault rather than a contract, so it stays a 400.+ Arguments.of("replace", "/throttle/rateLimit", null, "BadRequestException", 400, throttle),+ Arguments.of("replace", "/quota", "x", "BadRequestException", 400,+ invalidPath("replace", "/quota", REPLACE_PATHS)),+ Arguments.of("replace", "/throttle", "x", "BadRequestException", 400,+ invalidPath("replace", "/throttle", REPLACE_PATHS)),+ Arguments.of("remove", "/throttle/rateLimit", null, "BadRequestException", 400,+ invalidPath("remove", "/throttle/rateLimit", REMOVE_PATHS)),+ Arguments.of("add", "/quota", "x", "BadRequestException", 400, invalidPath("add", "/quota", ADD_PATHS)),+ // Any op but add, remove and replace is refused before the path is looked at.+ Arguments.of("move", "/quota/limit", "1", "BadRequestException", 400, INVALID_OP),+ Arguments.of("copy", "/apiStages", "a:v2", "BadRequestException", 400, INVALID_OP),+ Arguments.of("test", "/apiStages/a:v1/throttle", "{}", "BadRequestException", 400, INVALID_OP),+ Arguments.of(null, "/throttle", null, "BadRequestException", 400, INVALID_OP),+ Arguments.of("move", "/nope", "x", "BadRequestException", 400, INVALID_OP),+ Arguments.of("replace", "/apiStages/a:v2/throttle//pets/GET/rateLimit", "1", "NotFoundException", 404,+ "Invalid API Stage {api: a, stage: v2} specified for usageplan plan1"),+ Arguments.of("add", "/apiStages/a:v1/throttle", "{\"/a/GET\":{\"burstLimit\":4}}", "ConflictException",+ 409, "API Stage throttle is already defined for usageplan plan1"),+ Arguments.of("replace", "/apiStages/a:v1/throttle", "not-json", "BadRequestException", 400,+ "Invalid API Stage throttle specified for usageplan"),+ Arguments.of("replace", "/apiStages/a:v1/throttle", "{\"/a/GET\":5}", "BadRequestException", 400,+ "Invalid API Stage throttle specified for usageplan"),+ Arguments.of("remove", "/apiStages/a:v1/throttle//pets/GET/rateLimit", null, "BadRequestException", 400,+ invalidPath("remove", "/apiStages/a:v1/throttle//pets/GET/rateLimit", REMOVE_PATHS)),+ Arguments.of("remove", "/apiStages/a:v1/throttle//pets/GET/burstLimit", null, "BadRequestException",+ 400, invalidPath("remove", "/apiStages/a:v1/throttle//pets/GET/burstLimit", REMOVE_PATHS)),+ Arguments.of("remove", "/apiStages/a:v1/throttle//pets/POST", null, "NotFoundException", 404,+ "Method throttle does not exist for {resourcePath: /pets, method: POST} for usageplan plan1"),+ Arguments.of("replace", "/apiStages/a:v1/throttle//pets/GET/rateLimit", "abc", "BadRequestException",+ 400, throttle),+ Arguments.of("replace", "/apiStages/a:v1/limits", "1", "BadRequestException", 400,+ invalidPath("replace", "/apiStages/a:v1/limits", REPLACE_PATHS)),+ Arguments.of("replace", "/apiStages/a:v1/throttle", "{\"/GET\":{\"burstLimit\":1,\"rateLimit\":1}}",+ "BadRequestException", 400, "Invalid method {resourcePath: /GET,method: } specified"),+ Arguments.of("replace", "/apiStages/a:v1/throttle//rateLimit", "1", "BadRequestException", 400,+ invalidPath("replace", "/apiStages/a:v1/throttle//rateLimit", REPLACE_PATHS)),+ Arguments.of("replace", "/apiStages/a:v1/throttle/rateLimit", "1", "BadRequestException", 400,+ invalidPath("replace", "/apiStages/a:v1/throttle/rateLimit", REPLACE_PATHS)),+ Arguments.of("add", "/apiStages/a:v1/throttle//GET/burstLimit", "1", "BadRequestException", 400,+ invalidPath("add", "/apiStages/a:v1/throttle//GET/burstLimit", ADD_PATHS)),+ Arguments.of("replace", "/apiStages/a:v1/throttle//pets//rateLimit", "1", "BadRequestException", 400,+ invalidPath("replace", "/apiStages/a:v1/throttle//pets//rateLimit", REPLACE_PATHS)));+ }++ @ParameterizedTest+ @MethodSource("refusedPatches")+ void patchRefusesWhatApiGatewayRefuses(String op, String path, String value, String code, int status,+ String message) {+ assertRefused(() -> patch(fixture(), op, path, value), code, status, message);+ }++ static Stream<Arguments> storedStageThrottleValues() {+ return Stream.of(+ Arguments.of("{\"burstLimit\":1.5,\"rateLimit\":2}", new Throttle(1, 2.0)),+ Arguments.of("{\"burstLimit\":1e1}", new Throttle(10, 0.0)),+ Arguments.of("{\"burstLimit\":2147483647}", new Throttle(Integer.MAX_VALUE, 0.0)),+ Arguments.of("{\"burstLimit\":\"7\"}", new Throttle(7, 0.0)),+ Arguments.of("{\"rateLimit\":\"5.5\"}", new Throttle(0, 5.5)),+ Arguments.of("{\"burstLimit\":1}", new Throttle(1, 0.0)),+ Arguments.of("{}", new Throttle(0, 0.0)),+ Arguments.of("{\"burstLimit\":null,\"rateLimit\":2}", new Throttle(0, 2.0)));+ }++ // Measured on API Gateway with a replace of /apiStages/<apiId>:<stage>/throttle.+ @ParameterizedTest+ @MethodSource("storedStageThrottleValues")+ void aStageThrottleValueIsReadAsApiGatewayReadsIt(String settings, Throttle stored) {+ assertEquals(Map.of("/pets/GET", stored), patch(fixture(), "replace", "/apiStages/a:v1/throttle",+ "{\"/pets/GET\":" + settings + "}").apiStages().get(0).throttle());+ }++ static Stream<Arguments> refusedStageThrottleValues() {+ String stageThrottle = "Invalid API Stage throttle specified for usageplan";+ String throttle = "Usage Plan throttle rate limit and burst limit must be numeric and greater than zero";+ return Stream.of(+ Arguments.of("{\"/pets/GET\":{\"burstLimit\":3000000000}}", stageThrottle),+ Arguments.of("{\"/pets/GET\":{\"burstLimit\":-3000000000}}", stageThrottle),+ Arguments.of("{\"/pets/GET\":{\"burstLimit\":\"7.5\"}}", stageThrottle),+ Arguments.of("{\"/pets/GET\":{\"rateLimit\":\"abc\"}}", stageThrottle),+ Arguments.of("{\"/pets/GET\":{\"burstLimit\":true}}", stageThrottle),+ Arguments.of("{\"/pets/GET\":{\"rateLimit\":true}}", stageThrottle),+ Arguments.of("{\"/pets/GET\":{\"burstLimit\":[]}}", stageThrottle),+ Arguments.of("{\"/pets/GET\":{\"burstLimit\":1,\"rateLimit\":2,\"foo\":3}}", stageThrottle),+ Arguments.of("{\"/pets/GET\":5}", stageThrottle),+ // API Gateway answers this with a 500 InternalFailure, a fault rather than a contract.+ Arguments.of("{\"/pets/GET\":{\"rateLimit\":1e400}}", stageThrottle),+ Arguments.of("{\"/pets/GET\":null}",+ "Invalid throttle setting specified for method {resourcePath: /pets,method: GET}"),+ Arguments.of("{\"/pets/GET\":{\"burstLimit\":-1}}", throttle),+ Arguments.of("{\"/pets/GET\":{\"rateLimit\":-2}}", throttle),+ Arguments.of("{\"/GET\":{\"burstLimit\":1}}",+ "Invalid method {resourcePath: /GET,method: } specified"));+ }++ @ParameterizedTest+ @MethodSource("refusedStageThrottleValues")+ void aStageThrottleValueApiGatewayCannotReadIsRefused(String value, String message) {+ assertRefused(() -> patch(fixture(), "replace", "/apiStages/a:v1/throttle", value), "BadRequestException",+ 400, message);+ }++ // Measured on API Gateway: unlike a create body, a whole-stage throttle value is read through a double, so+ // a burst limit just under a whole number rounds up and a negative fraction truncates to 0.+ @Test+ void aStageThrottleValueReadsItsNumbersThroughADouble() {+ assertEquals(Map.of("/pets/GET", new Throttle(2, 2.0)),+ replacedStageThrottle("{\"/pets/GET\":{\"burstLimit\":1.99999999999999999,\"rateLimit\":2}}"));+ assertEquals(Map.of("/pets/GET", new Throttle(Integer.MAX_VALUE, 0.0)),+ replacedStageThrottle("{\"/pets/GET\":{\"burstLimit\":2147483647.0000001}}"));+ assertEquals(Map.of("/pets/GET", new Throttle(0, 0.0)),+ replacedStageThrottle("{\"/pets/GET\":{\"burstLimit\":-0.5}}"));+ assertRefused(() -> replacedStageThrottle("{\"/pets/GET\":{\"burstLimit\":2147483647.9}}"),+ "BadRequestException", 400, "Invalid API Stage throttle specified for usageplan");+ }++ /** The method throttles a replace of stage a:v1's whole throttle with {@code value} leaves. */+ private static Map<String, Throttle> replacedStageThrottle(String value) {+ return patch(fixture(), "replace", "/apiStages/a:v1/throttle", value).apiStages().get(0).throttle();+ }++ // A single limit is read as before: outside the int range it is an invalid throttle, not a stage throttle.+ @Test+ void aSingleBurstLimitOutsideTheIntRangeIsAnInvalidThrottle() {+ assertRefused(() -> patch(fixture(), "replace", "/apiStages/a:v1/throttle//pets/GET/burstLimit",+ "3000000000"), "BadRequestException", 400,+ "Usage Plan throttle rate limit and burst limit must be numeric and greater than zero");+ }++ // Measured on API Gateway: UpdateUsagePlan's input model lists every op outside its enum, by 1-based index.+ @Test+ void anOpOutsideTheModelsEnumIsAnInputModelViolation() throws Exception {+ ObjectMapper mapper = new ObjectMapper();+ JsonNode operations = mapper.readTree("[{\"op\":\"frobnicate\"},{\"op\":\"replace\"},"+ + "{\"op\":\"ADD\"},{\"path\":\"/name\"},{\"op\":\"\"},{\"op\":\"copy\"},{\"op\":null},null]");+ assertEquals(List.of(opViolation("frobnicate", 1), opViolation("ADD", 3), opViolation("", 5)),+ UsagePlanLimits.opViolations(operations));+ assertEquals(List.of(), UsagePlanLimits.opViolations(mapper.readTree("[]")));+ assertEquals(List.of(), UsagePlanLimits.opViolations(mapper.readTree("{}").path("patchOperations")));+ }++ private static String opViolation(String op, int index) {+ return "Value '" + op + "' at 'updateUsagePlanInput.patchOperations." + index + ".member.op' failed to satisfy "+ + "constraint: Member must satisfy enum value set: [add, remove, move, test, replace, copy]";+ }++ // Measured on API Gateway: a remove whose method throttle path names no method is an invalid path, as for+ // add and replace, before the key is looked up.+ @ParameterizedTest+ @ValueSource(strings = {"/nope", "//pets/", "/GET", "//GET", "/"})+ void removingAMethodThrottlePathThatNamesNoMethodIsAnInvalidPath(String suffix) {+ String path = "/apiStages/a:v1/throttle" + suffix;+ assertRefused(() -> patch(fixture(), "remove", path, null), "BadRequestException", 400,+ invalidPath("remove", path, REMOVE_PATHS));+ }++ @Test+ void removingAMethodThrottleThePlanLacksIsStillNotFound() {+ assertRefused(() -> patch(fixture(), "remove", "/apiStages/a:v1/throttle//pets/POST", null),+ "NotFoundException", 404,+ "Method throttle does not exist for {resourcePath: /pets, method: POST} for usageplan plan1");+ }++ @Test+ void aMethodThrottleNeedsAPlanThrottle() {+ UsagePlan plan = new UsagePlan();+ plan.setId("p");+ plan.applyLimits(new Limits(List.of(new ApiStage("a", "v1")), null, null));+ assertRefused(() -> patch(plan, "replace", "/apiStages/a:v1/throttle//pets/GET/rateLimit", "1"),+ "BadRequestException", 400, "Can not define fine grain throttling if usage plan throttle is not defined.");+ assertRefused(() -> patch(plan, "add", "/apiStages/a:v1/throttle",+ "{\"/pets/GET\":{\"burstLimit\":3,\"rateLimit\":2}}"),+ "BadRequestException", 400, "Can not define fine grain throttling if usage plan throttle is not defined.");+ }}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.javaindex 724fc87189..bdd2ab5874 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@@ -173,6 +173,25 @@ class ApiGatewayUsagePlanCfnProvisionerTest {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());+ }+@Testvoid anUnchangedUsagePlanUpdateTouchesNothing() {UsagePlan existing = plan("plan-1", "gold", "same", Map.of("stack", "v1"), new UsagePlan.ApiStage("api1", "dev"));