mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
refactor(ses): extract the shared v2 JSON helpers and guard controller registration (#3640)
SesController's 13 request-parsing helpers that more than one URL group uses (body and member shape checks, the Tags array, the v1-to-v2 error remapping) move byte-for-byte into a package-private static SesV2Json so the per-domain controllers that follow can share them without inheriting from or duplicating the monolith; readOptionBody now takes the caller's ObjectMapper. SesV2JsonTest pins each helper's accept/reject split and wording directly, and SesV2ControllerRegistrationTest asserts that every /v2/email resource class in the package is registered on the SES descriptor, since an unregistered controller still serves signed SDK calls and only silently loses the service-enabled gate and the x-amzn-errortype header for unsigned requests. No endpoint moves and no wire behavior changes.
This commit is contained in:
1 parent
efdef71527
commit
190c556940
5 files changed
+535
-218
No files matched your search
+93
@@ -0,0 +1,93 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.ResolvedServiceCatalog;
|
||||
import io.github.hectorvent.floci.core.common.ServiceDescriptor;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Tripwire for the SES v2 controller split: every JAX-RS resource class in this package that
|
||||
* serves {@code /v2/email} must be listed in the SES descriptor's resource classes in
|
||||
* {@code ResolvedServiceCatalog}. {@code ServiceEnabledFilter} and
|
||||
* {@code AwsRestJsonErrorHeaderFilter} resolve the service by resource class first and only fall
|
||||
* back to the SigV4 credential scope, so a controller missing from the catalog still works for
|
||||
* signed SDK calls and silently loses the service-disabled gate and the {@code x-amzn-errortype}
|
||||
* header for unsigned ones. The internal {@code /_aws/ses} controllers are outside the AWS surface
|
||||
* and stay unregistered on purpose.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class SesV2ControllerRegistrationTest {
|
||||
|
||||
private static final String V2_PREFIX = "/v2/email";
|
||||
|
||||
@Inject
|
||||
ResolvedServiceCatalog catalog;
|
||||
|
||||
@Test
|
||||
void everyV2EmailResourceClassIsRegisteredOnTheSesDescriptor() throws Exception {
|
||||
List<Class<?>> v2Controllers = v2EmailResourceClasses();
|
||||
assertTrue(v2Controllers.contains(SesController.class),
|
||||
"scan must at least find SesController; found " + v2Controllers);
|
||||
|
||||
List<String> unregistered = new ArrayList<>();
|
||||
for (Class<?> controller : v2Controllers) {
|
||||
String owner = catalog.byResourceClass(controller)
|
||||
.map(ServiceDescriptor::externalKey).orElse(null);
|
||||
if (!"email".equals(owner)) {
|
||||
unregistered.add(controller.getSimpleName() + " (resolved to " + owner + ")");
|
||||
}
|
||||
}
|
||||
assertEquals(List.of(), unregistered,
|
||||
"add these to the SES descriptor's resource classes in ResolvedServiceCatalog");
|
||||
}
|
||||
|
||||
@Test
|
||||
void scanLeavesTheInternalControllersOutOfTheRule() throws Exception {
|
||||
// /_aws/ses is Floci's own inspection surface, not an AWS route, and is unregistered on
|
||||
// purpose; the prefix filter must not pull it into the registration requirement.
|
||||
List<Class<?>> v2Controllers = v2EmailResourceClasses();
|
||||
assertFalse(v2Controllers.contains(SesInspectionController.class));
|
||||
assertFalse(v2Controllers.contains(SesUnsubscribeController.class));
|
||||
assertTrue(catalog.byResourceClass(SesInspectionController.class).isEmpty());
|
||||
assertTrue(catalog.byResourceClass(SesUnsubscribeController.class).isEmpty());
|
||||
}
|
||||
|
||||
private List<Class<?>> v2EmailResourceClasses() throws Exception {
|
||||
URI codeSource = SesController.class.getProtectionDomain().getCodeSource()
|
||||
.getLocation().toURI();
|
||||
Path classesDir = Path.of(codeSource);
|
||||
String packagePrefix = SesController.class.getPackageName() + ".";
|
||||
List<Class<?>> found = new ArrayList<>();
|
||||
try (Stream<Path> paths = Files.walk(classesDir)) {
|
||||
List<Path> classFiles = paths.filter(p -> p.toString().endsWith(".class")).toList();
|
||||
for (Path p : classFiles) {
|
||||
String name = classesDir.relativize(p).toString()
|
||||
.replace(File.separatorChar, '.')
|
||||
.replaceAll("\\.class$", "");
|
||||
if (!name.startsWith(packagePrefix) || name.contains("$")) {
|
||||
continue;
|
||||
}
|
||||
Class<?> type = Class.forName(name, false, getClass().getClassLoader());
|
||||
// jakarta.ws.rs.Path clashes with the java.nio.file.Path this scan is built on.
|
||||
jakarta.ws.rs.Path route = type.getAnnotation(jakarta.ws.rs.Path.class);
|
||||
if (route != null && route.value().startsWith(V2_PREFIX)) {
|
||||
found.add(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
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.ses.model.Tag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Unit tests for the request-parsing helpers shared by the SES v2 controllers. Each helper's
|
||||
* accept / reject split and its wording were previously pinned only through the controller
|
||||
* integration tests; these pin them directly so the controllers can move without re-proving them.
|
||||
*/
|
||||
class SesV2JsonTest {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
private static JsonNode json(String text) {
|
||||
try {
|
||||
return MAPPER.readTree(text);
|
||||
} catch (Exception e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static AwsException assertAws(String code, int status, Executable call) {
|
||||
AwsException e = assertThrows(AwsException.class, call);
|
||||
assertEquals(code, e.getErrorCode());
|
||||
assertEquals(status, e.getHttpStatus());
|
||||
return e;
|
||||
}
|
||||
|
||||
@Test
|
||||
void remapV1Exception_mapsEachV1FamilyAndPassesOthersThrough() {
|
||||
AwsException bad = SesV2Json.remapV1Exception(
|
||||
new AwsException("InvalidParameterValue", "m", 400));
|
||||
assertEquals("BadRequestException", bad.getErrorCode());
|
||||
assertEquals("m", bad.getMessage());
|
||||
assertEquals(400, bad.getHttpStatus());
|
||||
assertEquals("BadRequestException", SesV2Json.remapV1Exception(
|
||||
new AwsException("MissingRenderingAttribute", "m", 400)).getErrorCode());
|
||||
|
||||
AwsException notFound = SesV2Json.remapV1Exception(
|
||||
new AwsException("ConfigurationSetDoesNotExist", "m", 400));
|
||||
assertEquals("NotFoundException", notFound.getErrorCode());
|
||||
assertEquals(404, notFound.getHttpStatus());
|
||||
|
||||
assertEquals("AlreadyExistsException", SesV2Json.remapV1Exception(new AwsException(
|
||||
"CustomVerificationEmailTemplateAlreadyExists", "m", 400)).getErrorCode());
|
||||
assertEquals("SendingPausedException", SesV2Json.remapV1Exception(new AwsException(
|
||||
"ConfigurationSetSendingPausedException", "m", 400)).getErrorCode());
|
||||
|
||||
AwsException untouched = new AwsException("NotFoundException", "already v2", 404);
|
||||
assertSame(untouched, SesV2Json.remapV1Exception(untouched));
|
||||
}
|
||||
|
||||
@Test
|
||||
void requireJsonObject_rejectsNullAndNonObjects() {
|
||||
assertEquals("Request body must be a JSON object.", assertAws("BadRequestException", 400,
|
||||
() -> SesV2Json.requireJsonObject(null)).getMessage());
|
||||
assertAws("BadRequestException", 400, () -> SesV2Json.requireJsonObject(json("[]")));
|
||||
SesV2Json.requireJsonObject(json("{}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void requireObjectOrAbsent_returnsChild_rejectsScalars() {
|
||||
assertTrue(SesV2Json.requireObjectOrAbsent(json("{}"), "Options").isMissingNode());
|
||||
assertTrue(SesV2Json.requireObjectOrAbsent(json("{\"Options\":null}"), "Options").isNull());
|
||||
JsonNode nested = json("{\"Options\":{\"a\":\"x\"}}");
|
||||
assertEquals("x", SesV2Json.requireObjectOrAbsent(nested, "Options").path("a").asText());
|
||||
assertEquals("Options must be a JSON object.",
|
||||
assertAws("BadRequestException", 400, () -> SesV2Json.requireObjectOrAbsent(
|
||||
json("{\"Options\":1}"), "Options")).getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseTagsArray_absentIsNull_shapeErrorsAreWireErrors() {
|
||||
assertNull(SesV2Json.parseTagsArray(json("{}").path("Tags")));
|
||||
assertNull(SesV2Json.parseTagsArray(json("{\"Tags\":null}").path("Tags")));
|
||||
assertEquals("Tags must be an array.", assertAws("BadRequestException", 400,
|
||||
() -> SesV2Json.parseTagsArray(json("{\"Tags\":{}}").path("Tags"))).getMessage());
|
||||
assertAws("SerializationException", 400,
|
||||
() -> SesV2Json.parseTagsArray(json("[\"k=v\"]")));
|
||||
assertAws("SerializationException", 400,
|
||||
() -> SesV2Json.parseTagsArray(json("[{\"Key\":1,\"Value\":\"v\"}]")));
|
||||
assertAws("SerializationException", 400,
|
||||
() -> SesV2Json.parseTagsArray(json("[{\"Key\":\"k\",\"Value\":[]}]")));
|
||||
|
||||
List<Tag> tags = SesV2Json.parseTagsArray(
|
||||
json("[{\"Key\":\"k\",\"Value\":\"v\"},{\"Key\":\"only\"}]"));
|
||||
assertEquals(2, tags.size());
|
||||
assertEquals("k", tags.get(0).key());
|
||||
assertEquals("v", tags.get(0).value());
|
||||
// A missing member is left to the downstream tag validation rather than rejected here.
|
||||
assertEquals("only", tags.get(1).key());
|
||||
assertNull(tags.get(1).value());
|
||||
}
|
||||
|
||||
@Test
|
||||
void readOptionBody_blankIsEmptyObject_badShapesAre400() {
|
||||
assertTrue(SesV2Json.readOptionBody(MAPPER, null).isObject());
|
||||
assertTrue(SesV2Json.readOptionBody(MAPPER, " ").isObject());
|
||||
assertEquals("REQUIRE", SesV2Json.readOptionBody(MAPPER, "{\"HttpsPolicy\":\"REQUIRE\"}")
|
||||
.path("HttpsPolicy").asText());
|
||||
assertEquals("Request body must be a JSON object.", assertAws("BadRequestException", 400,
|
||||
() -> SesV2Json.readOptionBody(MAPPER, "[1]")).getMessage());
|
||||
assertAws("BadRequestException", 400, () -> SesV2Json.readOptionBody(MAPPER, "{not json"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseOptionString_absentIsNull_nonStringIs400() {
|
||||
JsonNode node = json("{\"TlsPolicy\":\"REQUIRE\",\"Bad\":true}");
|
||||
assertNull(SesV2Json.parseOptionString(node.path("Missing"), "Missing"));
|
||||
assertEquals("REQUIRE", SesV2Json.parseOptionString(node.path("TlsPolicy"), "TlsPolicy"));
|
||||
assertEquals("Bad must be a JSON string.", assertAws("BadRequestException", 400,
|
||||
() -> SesV2Json.parseOptionString(node.path("Bad"), "Bad")).getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void coerceBoolean_matchesAwsJacksonCoercion() {
|
||||
JsonNode node = json(
|
||||
"{\"t\":true,\"f\":false,\"s\":\"no\",\"z\":null,\"n\":0,\"a\":[],\"o\":{}}");
|
||||
assertTrue(SesV2Json.coerceBoolean(node.path("t")));
|
||||
assertFalse(SesV2Json.coerceBoolean(node.path("f")));
|
||||
// A JSON string coerces to true regardless of its text (probe-confirmed).
|
||||
assertTrue(SesV2Json.coerceBoolean(node.path("s")));
|
||||
assertNull(assertAws("SerializationException", 400,
|
||||
() -> SesV2Json.coerceBoolean(node.path("z"))).getMessage());
|
||||
assertEquals("NUMBER_VALUE can not be converted to a Boolean",
|
||||
assertAws("SerializationException", 400,
|
||||
() -> SesV2Json.coerceBoolean(node.path("n"))).getMessage());
|
||||
assertEquals("Start of list found where not expected",
|
||||
assertAws("SerializationException", 400,
|
||||
() -> SesV2Json.coerceBoolean(node.path("a"))).getMessage());
|
||||
assertEquals("Start of structure or map found where not expected.",
|
||||
assertAws("SerializationException", 400,
|
||||
() -> SesV2Json.coerceBoolean(node.path("o"))).getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unexpectedStartError_distinguishesListFromStructure() {
|
||||
assertEquals("Start of list found where not expected",
|
||||
SesV2Json.unexpectedStartError(json("[]")).getMessage());
|
||||
assertEquals("Start of structure or map found where not expected.",
|
||||
SesV2Json.unexpectedStartError(json("{}")).getMessage());
|
||||
assertEquals("Start of structure or map found where not expected.",
|
||||
SesV2Json.unexpectedStartError(json("1")).getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stringMemberOrAbsent_absentAndNullAreNull_wrongTypeIsWireError() {
|
||||
JsonNode node = json("{\"s\":\"x\",\"z\":null,\"n\":1}");
|
||||
assertEquals("x", SesV2Json.stringMemberOrAbsent(node, "s"));
|
||||
assertNull(SesV2Json.stringMemberOrAbsent(node, "z"));
|
||||
assertNull(SesV2Json.stringMemberOrAbsent(node, "missing"));
|
||||
assertAws("SerializationException", 400, () -> SesV2Json.stringMemberOrAbsent(node, "n"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void stringArrayOrAbsent_keepsEmptyListDistinctFromAbsent() {
|
||||
JsonNode node = json("{\"a\":[\"x\",\"y\"],\"e\":[],\"z\":null,\"s\":\"x\",\"m\":[1]}");
|
||||
assertEquals(List.of("x", "y"), SesV2Json.stringArrayOrAbsent(node, "a"));
|
||||
assertEquals(List.of(), SesV2Json.stringArrayOrAbsent(node, "e"));
|
||||
assertNull(SesV2Json.stringArrayOrAbsent(node, "z"));
|
||||
assertNull(SesV2Json.stringArrayOrAbsent(node, "missing"));
|
||||
assertAws("SerializationException", 400, () -> SesV2Json.stringArrayOrAbsent(node, "s"));
|
||||
assertAws("SerializationException", 400, () -> SesV2Json.stringArrayOrAbsent(node, "m"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void intMemberOrAbsent_rejectsNonIntegralAndOutOfRange() {
|
||||
JsonNode node = json("{\"i\":5,\"f\":1.5,\"big\":1099511627776,\"s\":\"5\",\"z\":null}");
|
||||
assertEquals(5, SesV2Json.intMemberOrAbsent(node, "i"));
|
||||
assertNull(SesV2Json.intMemberOrAbsent(node, "z"));
|
||||
assertNull(SesV2Json.intMemberOrAbsent(node, "missing"));
|
||||
assertAws("SerializationException", 400, () -> SesV2Json.intMemberOrAbsent(node, "f"));
|
||||
assertAws("SerializationException", 400, () -> SesV2Json.intMemberOrAbsent(node, "big"));
|
||||
assertAws("SerializationException", 400, () -> SesV2Json.intMemberOrAbsent(node, "s"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void readRequiredStringField_missingNullOrNonStringIsRequiredError() {
|
||||
JsonNode node = json("{\"PoolName\":\"p\",\"Bad\":1,\"Nil\":null}");
|
||||
assertEquals("p", SesV2Json.readRequiredStringField(node, "PoolName"));
|
||||
for (String field : List.of("Missing", "Bad", "Nil")) {
|
||||
assertEquals(field + " is required.", assertAws("BadRequestException", 400,
|
||||
() -> SesV2Json.readRequiredStringField(node, field)).getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user