feat(redshift-serverless): validate requests against the AWS model constraints (#5641)

* feat(redshift-serverless): validate requests against the AWS model constraints

Generate the input constraints (required members, length, pattern, enum,
number range, list size) from botocore's Redshift Serverless model with
tools/aws/regen_redshift_serverless_shapes.py and vendor them under
src/main/resources/aws, like the DynamoDB request shapes. A new
RedshiftServerlessRequestShapes check runs before dispatch and answers a
ValidationException. Pagination members are left to the services, which keep
reporting InvalidPaginationException.

Move the request parsing and error helpers that the JSON handler, the usage
limit service and the service each carried a copy of into ServerlessSupport,
so the resource families that follow share one implementation.

* fix(redshift-serverless): address review findings on request validation

- Match a model pattern against the whole value, so an unanchored pattern such
  as the account id no longer accepts a value that merely contains a match.
- Count string lengths in Unicode code points, so a tag of 200 supplementary
  characters is not rejected for exceeding 256 UTF-16 units.
- Validate only the actions the handler implements, so a modeled but
  unsupported action keeps answering UnknownOperationException.
- Read every string member through ServerlessSupport.text and reject a tag key
  or value that is not a string, so a numeric nextToken or tag member is a
  ValidationException instead of being treated as absent or coerced.
- Regenerate the vendored shapes from botocore 1.43.113, the version
  tools/aws/requirements.txt pins.
- Cover the rejected-request case with a state check, and add the pattern, code
  point, page token and tag regressions.

* refactor(redshift-serverless): use VendoredResources, ServerlessSupport.notFound and update docs
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-10-15 09:06:46 -04:00
1 parent 340e727ede
commit 77384dc3de
12 files changed
+2040 -277

No files matched your search

@@ -381,12 +381,59 @@ class RedshiftServerlessJsonHandlerTest {
@Test
void anUnknownActionIsReportedAsUnknownOperation() {
Response response = handler.handle("NoSuchOperation", mapper.createObjectNode(), REGION);
assertEquals(400, response.getStatus());
assertEquals("UnknownOperationException", errorType(response));
}
@Test
void aModeledActionWithoutAnImplementationIsStillAnUnknownOperation() {
Response response = handler.handle("CreateScheduledAction", mapper.createObjectNode(), REGION);
assertEquals(400, response.getStatus());
assertEquals("UnknownOperationException", errorType(response));
}
@Test
void aRequestBreakingTheModelConstraintsIsRejectedWithoutChangingState() {
Response badEnum = handler.handle("CreateNamespace",
parse("{\"namespaceName\":\"state-ns\",\"logExports\":[\"nope\"]}"), REGION);
Response missingNamespace = handler.handle("CreateWorkgroup", parse("{\"workgroupName\":\"state-wg\"}"), REGION);
Response missingName = handler.handle("GetWorkgroup", parse("{}"), REGION);
assertEquals("ValidationException", errorType(badEnum));
assertEquals("ValidationException", errorType(missingNamespace));
assertEquals("ValidationException", errorType(missingName));
assertEquals("ResourceNotFoundException", errorType(
handler.handle("GetNamespace", parse("{\"namespaceName\":\"state-ns\"}"), REGION)));
assertEquals(0, body(handler.handle("ListWorkgroups", parse("{}"), REGION)).get("workgroups").size());
}
@Test
void aNonStringPageTokenIsRejectedInsteadOfRestartingTheListing() {
for (String action : new String[] {"ListNamespaces", "ListWorkgroups", "ListSnapshots", "ListUsageLimits"}) {
Response response = handler.handle(action, parse("{\"nextToken\":12345678}"), REGION);
assertEquals("ValidationException", errorType(response), action);
}
}
@Test
void aTagWithANonStringKeyOrValueIsRejectedInsteadOfCoerced() {
String arn = body(handler.handle("CreateNamespace", parse("{\"namespaceName\":\"tag-ns\"}"), REGION))
.get("namespace").get("namespaceArn").textValue();
for (String tag : new String[] {"{\"key\":123,\"value\":\"v\"}", "{\"key\":\"k\",\"value\":false}"}) {
Response response = handler.handle("TagResource",
parse("{\"resourceArn\":\"" + arn + "\",\"tags\":[" + tag + "]}"), REGION);
assertEquals("ValidationException", errorType(response), tag);
}
assertEquals(0, body(handler.handle("ListTagsForResource", parse("{\"resourceArn\":\"" + arn + "\"}"), REGION))
.get("tags").size());
}
@Test
void aPresentCollectionMemberOfTheWrongTypeIsRejectedPerMember() {
create("wrong-type-ns");
@@ -0,0 +1,104 @@
package io.github.hectorvent.floci.services.redshiftserverless;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsException;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class RedshiftServerlessRequestShapesTest {
private static final ObjectMapper MAPPER = new ObjectMapper();
private static JsonNode parse(String json) {
try {
return MAPPER.readTree(json);
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
private static AwsException rejected(String action, String json) {
AwsException e = assertThrows(AwsException.class,
() -> RedshiftServerlessRequestShapes.check(action, parse(json)));
assertEquals("ValidationException", e.getErrorCode());
assertEquals(400, e.getHttpStatus());
return e;
}
@Test
void aMissingRequiredMemberIsRejected() {
assertTrue(rejected("GetTrack", "{}").getMessage().contains("trackName is required"));
assertTrue(rejected("GetTrack", "{\"trackName\":null}").getMessage().contains("trackName is required"));
}
@Test
void aRequiredMemberInsideAListElementIsRejected() {
AwsException e = rejected("TagResource", "{\"resourceArn\":\"arn:aws:redshift-serverless:us-east-1:"
+ "000000000000:namespace/x\",\"tags\":[{\"key\":\"k\"}]}");
assertTrue(e.getMessage().contains("tags[0].value is required"), e.getMessage());
}
@Test
void stringPatternLengthAndEnumFollowTheModel() {
assertTrue(rejected("GetTrack", "{\"trackName\":\"has space\"}").getMessage().contains("pattern"));
assertTrue(rejected("GetTrack", "{\"trackName\":\"\"}").getMessage().contains("at least 1"));
assertTrue(rejected("GetTrack", "{\"trackName\":\"" + "a".repeat(257) + "\"}")
.getMessage().contains("at most 256"));
assertTrue(rejected("CreateNamespace", "{\"namespaceName\":\"ns-ok\",\"logExports\":[\"nope\"]}")
.getMessage().contains("logExports[0] must be one of"));
assertDoesNotThrow(() -> RedshiftServerlessRequestShapes.check("GetTrack", parse("{\"trackName\":\"current\"}")));
}
@Test
void numberRangeFollowsTheModel() {
AwsException e = rejected("CreateWorkgroup", "{\"workgroupName\":\"wg-ok\",\"namespaceName\":\"ns-ok\","
+ "\"pricePerformanceTarget\":{\"status\":\"ENABLED\",\"level\":101}}");
assertTrue(e.getMessage().contains("pricePerformanceTarget.level"), e.getMessage());
assertTrue(e.getMessage().contains("at most 100"), e.getMessage());
}
@Test
void paginationMembersAreLeftToTheServices() {
assertDoesNotThrow(() -> RedshiftServerlessRequestShapes.check("ListNamespaces", parse("{\"maxResults\":0}")));
assertDoesNotThrow(() -> RedshiftServerlessRequestShapes.check("ListNamespaces", parse("{\"nextToken\":\"x\"}")));
}
@Test
void aPatternMustMatchTheWholeValue() {
assertDoesNotThrow(() -> RedshiftServerlessRequestShapes.check("ListWorkgroups",
parse("{\"ownerAccount\":\"123456789012\"}")));
assertTrue(rejected("ListWorkgroups", "{\"ownerAccount\":\"x123456789012y\"}")
.getMessage().contains("ownerAccount"));
}
@Test
void aLengthCountsCodePointsNotUtf16Units() {
String arn = "arn:aws:redshift-serverless:us-east-1:000000000000:namespace/x";
String emoji200 = "\uD83D\uDE00".repeat(200);
String emoji257 = "\uD83D\uDE00".repeat(257);
assertDoesNotThrow(() -> RedshiftServerlessRequestShapes.check("TagResource", parse(
"{\"resourceArn\":\"" + arn + "\",\"tags\":[{\"key\":\"k\",\"value\":\"" + emoji200 + "\"}]}")));
assertTrue(rejected("TagResource", "{\"resourceArn\":\"" + arn + "\",\"tags\":[{\"key\":\"k\",\"value\":\""
+ emoji257 + "\"}]}").getMessage().contains("at most 256"));
}
@Test
void aMemberOfTheWrongTypeIsLeftForTheHandlerToReport() {
assertDoesNotThrow(() -> RedshiftServerlessRequestShapes.check("GetTrack", parse("{\"trackName\":7}")));
}
@Test
void unknownActionsAndUnknownMembersAreIgnored() {
assertDoesNotThrow(() -> RedshiftServerlessRequestShapes.check("NoSuchAction", parse("{}")));
assertDoesNotThrow(() -> RedshiftServerlessRequestShapes.check("ListNamespaces", parse("{\"extra\":1}")));
assertDoesNotThrow(() -> RedshiftServerlessRequestShapes.check("ListNamespaces", null));
}
}