feat(rekognition): add Amazon Rekognition sync image-analysis actions (#2706)

* feat(rekognition): add Amazon Rekognition sync image-analysis actions

Implements DetectLabels, DetectFaces, DetectText, CompareFaces, and
DetectModerationLabels as fixed AWS-shaped stub responses over the
real JSON 1.1 wire protocol (X-Amz-Target: RekognitionService.*).
Image/SourceImage/TargetImage input is required and validated (must
specify Bytes or S3Object) but never decoded, matching the pattern
already used for Textract's Document input.

Content-listing operations (DetectLabels, DetectText) always return
one canned entry, matching TextractService's stub precedent. Face and
moderation operations (DetectFaces, CompareFaces,
DetectModerationLabels) return empty results by default rather than
fabricating biometric attributes or a moderation flag for content
that was never analyzed, matching ComprehendService's PII-detection
precedent.

Face-collection persistence, custom-model training, live video
(StreamProcessor), async video jobs, and Face Liveness sessions are
out of scope for this PR.

Closes #2705

* fix(rekognition): reject wrong-typed Bytes/S3Object as SerializationException

requireImage() only checked field presence via hasNonNull(), so a
wrong-typed Bytes (e.g. a number) or S3Object (e.g. a string) was
silently accepted instead of rejected, same class of bug as the
Comprehend Text/LanguageCode type-coercion fix. Bytes must now be
textual and S3Object must be an object when present, both rejected
as SerializationException (a shape/marshalling mismatch) otherwise.

Content-level validation (well-formed base64 in Bytes, S3Object's
Bucket/Name presence, rejecting both Bytes and S3Object together) is
intentionally not added: S3Object has no required members in AWS's
own model, and nothing in the model declares supplying both fields
invalid — documented as intentional deviations.

Addresses review feedback on #2706.
This commit is contained in:
Ahmed khaled authored and GitHub committed 2026-08-29 14:15:52 -04:00
1 parent 5600d21f32
commit cf6fede453
13 files changed
+543 -4

No files matched your search

@@ -658,6 +658,7 @@ public interface EmulatorConfig {
TransferServiceConfig transfer();
TextractServiceConfig textract();
ComprehendServiceConfig comprehend();
RekognitionServiceConfig rekognition();
PricingServiceConfig pricing();
DuckConfig duck();
TranscribeServiceConfig transcribe();
@@ -1549,6 +1550,11 @@ public interface EmulatorConfig {
boolean enabled();
}
interface RekognitionServiceConfig {
@WithDefault("true")
boolean enabled();
}
interface PricingServiceConfig {
@WithDefault("true")
boolean enabled();
@@ -29,6 +29,7 @@ import io.github.hectorvent.floci.services.cur.CurJsonHandler;
import io.github.hectorvent.floci.services.pricing.PricingJsonHandler;
import io.github.hectorvent.floci.services.textract.TextractJsonHandler;
import io.github.hectorvent.floci.services.comprehend.ComprehendJsonHandler;
import io.github.hectorvent.floci.services.rekognition.RekognitionJsonHandler;
import io.github.hectorvent.floci.services.transcribe.TranscribeJsonHandler;
import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2JsonHandler;
import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsHandler;
@@ -102,6 +103,7 @@ public class AwsJson11Controller {
private final TransferHandler transferHandler;
private final TextractJsonHandler textractJsonHandler;
private final ComprehendJsonHandler comprehendJsonHandler;
private final RekognitionJsonHandler rekognitionJsonHandler;
private final PricingJsonHandler pricingJsonHandler;
private final TranscribeJsonHandler transcribeJsonHandler;
private final CostExplorerJsonHandler costExplorerJsonHandler;
@@ -146,6 +148,7 @@ public class AwsJson11Controller {
TransferHandler transferHandler,
TextractJsonHandler textractJsonHandler,
ComprehendJsonHandler comprehendJsonHandler,
RekognitionJsonHandler rekognitionJsonHandler,
PricingJsonHandler pricingJsonHandler,
TranscribeJsonHandler transcribeJsonHandler,
CostExplorerJsonHandler costExplorerJsonHandler,
@@ -194,6 +197,7 @@ public class AwsJson11Controller {
this.transferHandler = transferHandler;
this.textractJsonHandler = textractJsonHandler;
this.comprehendJsonHandler = comprehendJsonHandler;
this.rekognitionJsonHandler = rekognitionJsonHandler;
this.pricingJsonHandler = pricingJsonHandler;
this.transcribeJsonHandler = transcribeJsonHandler;
this.costExplorerJsonHandler = costExplorerJsonHandler;
@@ -273,6 +277,7 @@ public class AwsJson11Controller {
case "transfer" -> transferHandler.handle(action, request, region);
case "textract" -> textractJsonHandler.handle(action, request, region);
case "comprehend" -> comprehendJsonHandler.handle(action, request, region);
case "rekognition" -> rekognitionJsonHandler.handle(action, request, region);
case "pricing" -> pricingJsonHandler.handle(action, request, region);
case "transcribe" -> transcribeJsonHandler.handle(action, request, region);
case "ce" -> costExplorerJsonHandler.handle(action, request, region);
@@ -450,6 +450,10 @@ public class ResolvedServiceCatalog {
null, null, 5000L, null, ServiceProtocol.JSON,
protocols(ServiceProtocol.JSON),
Set.of("Comprehend_20171127."), Set.of("comprehend"), Set.of(), Set.of()),
descriptor("rekognition", "rekognition", config.services().rekognition().enabled(), true,
null, null, 5000L, null, ServiceProtocol.JSON,
protocols(ServiceProtocol.JSON),
Set.of("RekognitionService."), Set.of("rekognition"), Set.of(), Set.of()),
descriptor("pricing", "pricing", config.services().pricing().enabled(), true,
null, null, 5000L, null, ServiceProtocol.JSON,
protocols(ServiceProtocol.JSON),
@@ -0,0 +1,98 @@
package io.github.hectorvent.floci.services.rekognition;
import com.fasterxml.jackson.databind.JsonNode;
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
import io.github.hectorvent.floci.core.common.AwsException;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import org.jboss.logging.Logger;
import java.util.function.Predicate;
/**
* JSON 1.1 handler for Amazon Rekognition API operations.
* Dispatches X-Amz-Target: RekognitionService.* actions to {@link RekognitionService}.
*
* @see <a href="https://docs.aws.amazon.com/rekognition/latest/APIReference/Welcome.html">Rekognition API Reference</a>
*/
@ApplicationScoped
public class RekognitionJsonHandler {
private static final Logger LOG = Logger.getLogger(RekognitionJsonHandler.class);
private final RekognitionService rekognitionService;
@Inject
public RekognitionJsonHandler(RekognitionService rekognitionService) {
this.rekognitionService = rekognitionService;
}
/**
* Dispatches Rekognition actions received via the AwsJson11Controller.
* Only sync image-analysis actions are implemented; face-collection persistence
* (CreateCollection/IndexFaces/SearchFaces), custom-model training (Projects/
* Datasets/ProjectVersions), live video (StreamProcessor), async video jobs
* (the Start and Get pairs for Celebrity/Content/Face/Label/Person/Segment/Text
* detection), and Face Liveness sessions are out of scope.
*/
public Response handle(String action, JsonNode request, String region) {
LOG.debugv("Rekognition action: {0}", action);
return switch (action) {
case "DetectLabels" -> {
requireImage(request, "Image");
yield rekognitionService.detectLabels();
}
case "DetectFaces" -> {
requireImage(request, "Image");
yield rekognitionService.detectFaces();
}
case "DetectText" -> {
requireImage(request, "Image");
yield rekognitionService.detectText();
}
case "CompareFaces" -> {
requireImage(request, "SourceImage");
requireImage(request, "TargetImage");
yield rekognitionService.compareFaces();
}
case "DetectModerationLabels" -> {
requireImage(request, "Image");
yield rekognitionService.detectModerationLabels();
}
default -> Response.status(400)
.entity(new AwsErrorResponse("UnknownOperationException",
"Unknown operation: RekognitionService." + action))
.build();
};
}
/**
* The Image shape (real content ignored — the response is a fixed stub) is still
* required and must carry Bytes or S3Object, matching AWS's own required-member
* and one-of-Bytes-or-S3Object modeling for Image/SourceImage/TargetImage.
*/
private void requireImage(JsonNode request, String field) {
JsonNode image = request == null ? null : request.get(field);
if (image == null || image.isNull()) {
throw new AwsException("InvalidParameterException", field + " is a required field.", 400);
}
if (!image.isObject()) {
throw new AwsException("SerializationException",
"Unable to unmarshal request; the value for '" + field + "' is not a structure.", 400);
}
requireCorrectTypeIfPresent(image, "Bytes", JsonNode::isTextual, field);
requireCorrectTypeIfPresent(image, "S3Object", JsonNode::isObject, field);
if (!image.hasNonNull("Bytes") && !image.hasNonNull("S3Object")) {
throw new AwsException("InvalidParameterException",
field + " must specify Bytes or S3Object.", 400);
}
}
/**
* Bytes (a blob shape, base64-encoded string on the wire) and S3Object (a structure)
* must each carry their modeled JSON type when present. Content-level correctness
* (valid base64, S3Object.Bucket/Name presence — S3Object has no required members
* in the modeled shape) is not validated: this stub never reads image content, and
* enforcing more than the wire shape declares would invent stricter validation than
* AWS's own model.
*/
private void requireCorrectTypeIfPresent(JsonNode image, String member, Predicate<JsonNode> isCorrectType, String field) {
JsonNode value = image.get(member);
if (value != null && !value.isNull() && !isCorrectType.test(value)) {
throw new AwsException("SerializationException",
"Unable to unmarshal request; the value for '" + field + "." + member + "' has the wrong type.", 400);
}
}
}
@@ -0,0 +1,123 @@
package io.github.hectorvent.floci.services.rekognition;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
/**
* Dummy response builder for Amazon Rekognition. Stateless — every action ignores
* the actual image content and returns a fixed but AWS-shaped response.
* <p>
* Content-listing operations (DetectLabels, DetectText) always return one canned
* entry, matching the TextractService stub precedent. Face/moderation operations
* (DetectFaces, CompareFaces, DetectModerationLabels) return empty results by
* default — an honest "nothing detected" response, rather than fabricating
* biometric attributes or a moderation flag for content that was never analyzed,
* matching the ComprehendService PII-detection precedent.
* <p>
* Real detection logic is a planned follow-up; see the tracking issue for scope.
*
* @see <a href="https://docs.aws.amazon.com/rekognition/latest/APIReference/Welcome.html">Rekognition API Reference</a>
*/
@ApplicationScoped
public class RekognitionService {
static final String MODEL_VERSION = "1.0";
private final ObjectMapper objectMapper;
@Inject
public RekognitionService(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
/**
* DetectLabels — always returns a single stub label.
* Response shape: https://docs.aws.amazon.com/rekognition/latest/APIReference/API_DetectLabels.html
*/
public Response detectLabels() {
ObjectNode root = objectMapper.createObjectNode();
ArrayNode labels = root.putArray("Labels");
ObjectNode label = labels.addObject();
label.put("Name", "Floci");
label.put("Confidence", 99.9);
root.put("LabelModelVersion", MODEL_VERSION);
return Response.ok(root).build();
}
/**
* DetectFaces — always reports no faces found.
* Response shape: https://docs.aws.amazon.com/rekognition/latest/APIReference/API_DetectFaces.html
*/
public Response detectFaces() {
ObjectNode root = objectMapper.createObjectNode();
root.putArray("FaceDetails");
return Response.ok(root).build();
}
/**
* DetectText — always returns one stub LINE and its child WORD, matching
* Textract's PAGE/LINE/WORD block-hierarchy convention.
* Response shape: https://docs.aws.amazon.com/rekognition/latest/APIReference/API_DetectText.html
*/
public Response detectText() {
ObjectNode root = objectMapper.createObjectNode();
ArrayNode detections = root.putArray("TextDetections");
ObjectNode line = detections.addObject();
line.put("DetectedText", "Floci");
line.put("Type", "LINE");
line.put("Id", 0);
line.put("Confidence", 99.9);
line.set("Geometry", buildGeometry(0.1, 0.1, 0.15, 0.05));
ObjectNode word = detections.addObject();
word.put("DetectedText", "Floci");
word.put("Type", "WORD");
word.put("Id", 1);
word.put("ParentId", 0);
word.put("Confidence", 99.9);
word.set("Geometry", buildGeometry(0.1, 0.1, 0.15, 0.05));
root.put("TextModelVersion", MODEL_VERSION);
return Response.ok(root).build();
}
/**
* CompareFaces — always reports no matches (SourceImageFace is optional and
* legitimately omitted when no face is found in the source image).
* Response shape: https://docs.aws.amazon.com/rekognition/latest/APIReference/API_CompareFaces.html
*/
public Response compareFaces() {
ObjectNode root = objectMapper.createObjectNode();
root.putArray("FaceMatches");
root.putArray("UnmatchedFaces");
return Response.ok(root).build();
}
/**
* DetectModerationLabels — always reports no moderation labels found.
* Response shape: https://docs.aws.amazon.com/rekognition/latest/APIReference/API_DetectModerationLabels.html
*/
public Response detectModerationLabels() {
ObjectNode root = objectMapper.createObjectNode();
root.putArray("ModerationLabels");
root.put("ModerationModelVersion", MODEL_VERSION);
return Response.ok(root).build();
}
// Private helpers
/**
* Builds a Geometry object with BoundingBox and a 4-point Polygon — the same
* shape Textract uses (Rekognition's Geometry shape is structurally identical).
* @see <a href="https://docs.aws.amazon.com/rekognition/latest/APIReference/API_Geometry.html">Geometry</a>
*/
private ObjectNode buildGeometry(double left, double top, double width, double height) {
ObjectNode geometry = objectMapper.createObjectNode();
ObjectNode bbox = geometry.putObject("BoundingBox");
bbox.put("Width", width);
bbox.put("Height", height);
bbox.put("Left", left);
bbox.put("Top", top);
ArrayNode polygon = geometry.putArray("Polygon");
addPoint(polygon, left, top);
addPoint(polygon, left + width, top);
addPoint(polygon, left + width, top + height);
addPoint(polygon, left, top + height);
return geometry;
}
private void addPoint(ArrayNode polygon, double x, double y) {
ObjectNode point = polygon.addObject();
point.put("X", x);
point.put("Y", y);
}
}