feat(iot): indexing configuration (#4896)

AWS IoT fleet indexing had no routes in Floci, so a stack or script
calling UpdateIndexingConfiguration or GetIndexingConfiguration failed
with "Unknown operation: POST /indexing/config", and there was no
stored configuration for a later SearchIndex to consult.

A new IotFleetIndexingService and IotFleetIndexingController, wired
into the iot descriptor beside the domain configuration controller,
serve UpdateIndexingConfiguration and GetIndexingConfiguration on
/indexing/config and DescribeIndex on /indices/{indexName}. There is
one configuration per account and region, kept through StorageFactory;
a region never updated reports the OFF shape AWS returns.

The behaviour follows what real AWS returned when measured: an update
that carries only one of the thing and thing group configurations
keeps the other, turning thing indexing OFF clears the whole thing
configuration, and managedFields are derived from the modes rather
than stored. Client sent managedFields are only checked against the
fields AWS manages and their types. Validation uses AWS's messages for
a missing configuration, null and out of set enum values (the modes,
field types, filter socket information and geolocation order),
secondary modes without thing indexing, named shadow indexing without
shadow names, unknown managed fields, and a thing custom field named
after a managed field. A geolocation sent without an order is stored
with LatLon, the default AWS returns. DescribeIndex returns the
schema the modes select and is ACTIVE at once, a disabled index is
ResourceNotFoundException and an unknown name InvalidRequestException.

Thing group managedFields are checked the same way, also while group
indexing is OFF, and neither group managedFields nor customFields are
stored. A member of the wrong JSON type is a SerializationException,
as on AWS. The fleet indexing, domain configuration and authorizer
controllers now share one strict body reader, so a malformed body or
content after it is a SerializationException on all three, which is
what AWS returns there.

ListIndices, SearchIndex and the statistics APIs are not modeled.
This commit is contained in:
Mario Toffia authored and GitHub committed 2026-10-09 16:52:30 -04:00
1 parent 9f224d9de6
commit f413a667d0
14 files changed
+1644 -38

No files matched your search

@@ -40,6 +40,7 @@ import io.github.hectorvent.floci.services.iot.IotAuthorizerController;
import io.github.hectorvent.floci.services.iot.IotController;
import io.github.hectorvent.floci.services.iot.IotDataController;
import io.github.hectorvent.floci.services.iot.IotDomainConfigurationController;
import io.github.hectorvent.floci.services.iot.IotFleetIndexingController;
import io.github.hectorvent.floci.services.lambda.LambdaController;
import io.github.hectorvent.floci.services.lambda.durable.DurableExecutionController;
import io.github.hectorvent.floci.services.lambdamicrovms.LambdaMicrovmsController;
@@ -650,7 +651,7 @@ public class ResolvedServiceCatalog {
// signs under its own name while IotController serves its /things/*/jobs routes
Set.of(), Set.of("iot", "execute-api", "iot-jobs-data"), Set.of(),
Set.of(IotController.class, IotDomainConfigurationController.class,
IotAuthorizerController.class)),
IotAuthorizerController.class, IotFleetIndexingController.class)),
descriptor("iotdata", "iotdata", config.services().iotdata().enabled(), true,
"iot", config.storage().mode(), 5000L, null, ServiceProtocol.REST_JSON,
protocols(ServiceProtocol.REST_JSON),
@@ -1,13 +1,8 @@
package io.github.hectorvent.floci.services.iot;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.services.iot.model.IotAuthorizer;
import jakarta.inject.Inject;
@@ -40,7 +35,6 @@ public class IotAuthorizerController {
private final IotCustomAuthorizer customAuthorizer;
private final RegionResolver regionResolver;
private final ObjectMapper objectMapper;
private final ObjectReader strictReader;
@Inject
public IotAuthorizerController(IotAuthorizerService authorizerService, IotCustomAuthorizer customAuthorizer,
@@ -49,7 +43,6 @@ public class IotAuthorizerController {
this.customAuthorizer = customAuthorizer;
this.regionResolver = regionResolver;
this.objectMapper = objectMapper;
this.strictReader = objectMapper.reader().with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
}
@POST
@@ -57,7 +50,8 @@ public class IotAuthorizerController {
public Response createAuthorizer(@Context HttpHeaders headers, @PathParam("authorizerName") String authorizerName,
String body) {
return Response.ok(nameAndArn(authorizerService.createAuthorizer(
authorizerName, readJson(body), regionResolver.resolveRegion(headers)))).build();
authorizerName, IotRequestBody.read(objectMapper, body), regionResolver.resolveRegion(headers))))
.build();
}
@GET
@@ -71,7 +65,8 @@ public class IotAuthorizerController {
public Response updateAuthorizer(@Context HttpHeaders headers, @PathParam("authorizerName") String authorizerName,
String body) {
return Response.ok(nameAndArn(authorizerService.updateAuthorizer(
authorizerName, readJson(body), regionResolver.resolveRegion(headers)))).build();
authorizerName, IotRequestBody.read(objectMapper, body), regionResolver.resolveRegion(headers))))
.build();
}
@DELETE
@@ -86,8 +81,8 @@ public class IotAuthorizerController {
@Path("/authorizer/{authorizerName}/test")
public Response testInvokeAuthorizer(@Context HttpHeaders headers, @PathParam("authorizerName") String authorizerName,
String body) {
return Response.ok(customAuthorizer.testInvoke(authorizerName, readJson(body), regionResolver.resolveRegion(headers)))
.build();
return Response.ok(customAuthorizer.testInvoke(authorizerName, IotRequestBody.read(objectMapper, body),
regionResolver.resolveRegion(headers))).build();
}
@GET
@@ -110,7 +105,8 @@ public class IotAuthorizerController {
@Path("/default-authorizer")
public Response setDefaultAuthorizer(@Context HttpHeaders headers, String body) {
return Response.ok(nameAndArn(authorizerService.setDefaultAuthorizer(
readJson(body).path("authorizerName").asText(null), regionResolver.resolveRegion(headers)))).build();
IotRequestBody.read(objectMapper, body).path("authorizerName").asText(null),
regionResolver.resolveRegion(headers)))).build();
}
@GET
@@ -150,12 +146,4 @@ public class IotAuthorizerController {
description.set("tokenSigningPublicKeys", objectMapper.valueToTree(authorizer.getTokenSigningPublicKeys()));
return Response.ok(response).build();
}
private JsonNode readJson(String body) {
try {
return strictReader.readTree(body == null || body.isBlank() ? "{}" : body);
} catch (JsonProcessingException e) {
throw new AwsException("InvalidRequestException", e.getMessage(), 400);
}
}
}
@@ -1,11 +1,8 @@
package io.github.hectorvent.floci.services.iot;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.services.iot.model.IotDomainConfiguration;
import io.github.hectorvent.floci.services.iot.model.IotDomainConfiguration.AuthorizerConfig;
@@ -55,7 +52,8 @@ public class IotDomainConfigurationController {
@PathParam("domainConfigurationName") String domainConfigurationName,
String body) {
IotDomainConfiguration created = domainConfigurationService.createDomainConfiguration(
domainConfigurationName, readJson(body), regionResolver.resolveRegion(headers));
domainConfigurationName, IotRequestBody.read(objectMapper, body),
regionResolver.resolveRegion(headers));
return Response.ok(nameAndArn(created)).build();
}
@@ -74,7 +72,8 @@ public class IotDomainConfigurationController {
@PathParam("domainConfigurationName") String domainConfigurationName,
String body) {
IotDomainConfiguration updated = domainConfigurationService.updateDomainConfiguration(
domainConfigurationName, readJson(body), regionResolver.resolveRegion(headers));
domainConfigurationName, IotRequestBody.read(objectMapper, body),
regionResolver.resolveRegion(headers));
return Response.ok(nameAndArn(updated)).build();
}
@@ -166,12 +165,4 @@ public class IotDomainConfigurationController {
node.put(field, value);
}
}
private JsonNode readJson(String body) {
try {
return objectMapper.readTree(body == null || body.isBlank() ? "{}" : body);
} catch (JsonProcessingException e) {
throw new AwsException("InvalidRequestException", e.getMessage(), 400);
}
}
}
@@ -0,0 +1,96 @@
package io.github.hectorvent.floci.services.iot;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.services.iot.model.IotIndexingConfiguration;
import io.github.hectorvent.floci.services.iot.model.IotIndexingConfiguration.Field;
import io.github.hectorvent.floci.services.iot.model.IotIndexingConfiguration.ThingIndexing;
import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.util.List;
/**
* REST-JSON routes for AWS IoT fleet indexing: UpdateIndexingConfiguration,
* GetIndexingConfiguration and DescribeIndex, on the paths and shapes the AWS SDKs use.
*/
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class IotFleetIndexingController {
private final IotFleetIndexingService fleetIndexingService;
private final RegionResolver regionResolver;
private final ObjectMapper objectMapper;
@Inject
public IotFleetIndexingController(IotFleetIndexingService fleetIndexingService, RegionResolver regionResolver,
ObjectMapper objectMapper) {
this.fleetIndexingService = fleetIndexingService;
this.regionResolver = regionResolver;
this.objectMapper = objectMapper;
}
@POST
@Path("/indexing/config")
public Response updateIndexingConfiguration(@Context HttpHeaders headers, String body) {
fleetIndexingService.updateIndexingConfiguration(IotRequestBody.read(objectMapper, body),
regionResolver.resolveRegion(headers));
return Response.ok(objectMapper.createObjectNode()).build();
}
@GET
@Path("/indexing/config")
public Response getIndexingConfiguration(@Context HttpHeaders headers) {
IotIndexingConfiguration configuration =
fleetIndexingService.getIndexingConfiguration(regionResolver.resolveRegion(headers));
ObjectNode response = objectMapper.createObjectNode();
ThingIndexing thing = configuration.thing();
ObjectNode thingNode = response.putObject("thingIndexingConfiguration");
thingNode.put("thingIndexingMode", thing.thingIndexingMode());
thingNode.put("thingConnectivityIndexingMode", thing.thingConnectivityIndexingMode());
thingNode.put("deviceDefenderIndexingMode", thing.deviceDefenderIndexingMode());
thingNode.put("namedShadowIndexingMode", thing.namedShadowIndexingMode());
List<Field> managedFields = IotFleetIndexingService.thingManagedFields(thing);
ObjectNode filter = thingNode.putObject("filter");
// Managed fields exist exactly while thing indexing is on; AWS then reports the full filter too.
if (!managedFields.isEmpty()) {
thingNode.set("managedFields", objectMapper.valueToTree(managedFields));
if (!thing.customFields().isEmpty()) {
thingNode.set("customFields", objectMapper.valueToTree(thing.customFields()));
}
filter.set("namedShadowNames", objectMapper.valueToTree(thing.namedShadowNames()));
filter.set("geoLocations", objectMapper.valueToTree(thing.geoLocations()));
filter.putObject("connectivity")
.set("includeSocketInformation", objectMapper.valueToTree(thing.includeSocketInformation()));
}
ObjectNode groupNode = response.putObject("thingGroupIndexingConfiguration");
groupNode.put("thingGroupIndexingMode", configuration.thingGroupIndexingMode());
if ("ON".equals(configuration.thingGroupIndexingMode())) {
groupNode.set("managedFields", objectMapper.valueToTree(IotFleetIndexingService.THING_GROUP_MANAGED_FIELDS));
}
return Response.ok(response).build();
}
@GET
@Path("/indices/{indexName}")
public Response describeIndex(@Context HttpHeaders headers, @PathParam("indexName") String indexName) {
String schema = fleetIndexingService.describeIndex(indexName, regionResolver.resolveRegion(headers));
ObjectNode response = objectMapper.createObjectNode();
response.put("indexName", indexName);
// ponytail: always ACTIVE, Floci indexes synchronously so there is no BUILDING or REBUILDING window.
response.put("indexStatus", "ACTIVE");
response.put("schema", schema);
return Response.ok(response).build();
}
}
@@ -0,0 +1,362 @@
package io.github.hectorvent.floci.services.iot;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.databind.node.MissingNode;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.iot.model.IotIndexingConfiguration;
import io.github.hectorvent.floci.services.iot.model.IotIndexingConfiguration.Field;
import io.github.hectorvent.floci.services.iot.model.IotIndexingConfiguration.GeoLocation;
import io.github.hectorvent.floci.services.iot.model.IotIndexingConfiguration.ThingIndexing;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* AWS IoT fleet indexing configuration: UpdateIndexingConfiguration, GetIndexingConfiguration and
* DescribeIndex. One configuration per account and region; a region never updated is OFF.
*/
@ApplicationScoped
public class IotFleetIndexingService {
static final String THINGS_INDEX = "AWS_Things";
static final String THING_GROUPS_INDEX = "AWS_ThingGroups";
// Enum value sets in the order AWS prints them in its validation messages.
private static final List<String> THING_INDEXING_MODES = List.of("REGISTRY_AND_SHADOW", "OFF", "REGISTRY");
private static final List<String> CONNECTIVITY_MODES = List.of("OFF", "STATUS");
private static final List<String> DEVICE_DEFENDER_MODES = List.of("VIOLATIONS", "OFF");
private static final List<String> ON_OFF = List.of("OFF", "ON");
private static final List<String> FIELD_TYPES = List.of("Boolean", "Number", "String");
private static final List<String> SOCKET_INFORMATION_APIS = List.of("GET_THING_CONNECTIVITY_DATA");
private static final List<String> GEO_LOCATION_ORDERS = List.of("LatLon", "LonLat");
private static final List<Field> REGISTRY_FIELDS = List.of(
new Field("thingName", "String"),
new Field("thingId", "String"),
new Field("registry.version", "Number"),
new Field("registry.thingTypeName", "String"),
new Field("registry.thingGroupNames", "String"));
private static final List<Field> SHADOW_FIELDS = List.of(
new Field("shadow.version", "Number"),
new Field("shadow.hasDelta", "Boolean"));
private static final List<Field> NAMED_SHADOW_FIELDS = List.of(
new Field("shadow.name.*.hasDelta", "Boolean"),
new Field("shadow.name.*.version", "Number"));
private static final List<Field> CONNECTIVITY_FIELDS = List.of(
new Field("connectivity.connected", "Boolean"),
new Field("connectivity.timestamp", "Number"),
new Field("connectivity.disconnectReason", "String"),
new Field("connectivity.clientId", "String"),
new Field("connectivity.cleanSession", "Boolean"),
new Field("connectivity.keepAliveDuration", "Number"),
new Field("connectivity.sessionExpiry", "Number"),
new Field("connectivity.version", "Number"));
private static final List<Field> DEVICE_DEFENDER_FIELDS = List.of(
new Field("deviceDefender.version", "Number"),
new Field("deviceDefender.violationCount", "Number"),
new Field("deviceDefender.*.*.inViolation", "Boolean"),
new Field("deviceDefender.*.*.lastViolationTime", "Number"),
new Field("deviceDefender.*.*.metricName", "String"));
private static final Set<Field> KNOWN_THING_FIELDS = Stream.of(REGISTRY_FIELDS, SHADOW_FIELDS,
NAMED_SHADOW_FIELDS, CONNECTIVITY_FIELDS, DEVICE_DEFENDER_FIELDS)
.flatMap(List::stream)
.collect(Collectors.toUnmodifiableSet());
static final List<Field> THING_GROUP_MANAGED_FIELDS = List.of(
new Field("parentGroupNames", "String"),
new Field("description", "String"),
new Field("version", "Number"),
new Field("thingGroupName", "String"),
new Field("thingGroupId", "String"));
private final StorageBackend<String, IotIndexingConfiguration> store;
/** Guards the read-modify-write of an update that changes only one of the two configurations. */
private final Object lock = new Object();
@Inject
public IotFleetIndexingService(StorageFactory storageFactory) {
this(storageFactory.create("iot", "iot-indexing-configuration.json",
new TypeReference<Map<String, IotIndexingConfiguration>>() {}));
}
IotFleetIndexingService(StorageBackend<String, IotIndexingConfiguration> store) {
this.store = store;
}
public void updateIndexingConfiguration(JsonNode request, String region) {
if (!request.isObject()) {
throw serializationError("request body", JsonNodeType.OBJECT);
}
String groupPrefix = "thingGroupIndexingConfiguration";
JsonNode thingNode = object(request.path("thingIndexingConfiguration"), "thingIndexingConfiguration");
JsonNode groupNode = object(request.path(groupPrefix), groupPrefix);
if (!present(thingNode) && !present(groupNode)) {
throw invalid("At least one configuration to update "
+ "(thingIndexingConfiguration / thingGroupIndexingConfiguration) is required");
}
List<String> errors = new ArrayList<>();
ThingIndexing thing = present(thingNode) ? parseThing(thingNode, errors) : null;
List<Field> managedFields = fields(thingNode.path("managedFields"),
"thingIndexingConfiguration.managedFields", errors);
String groupMode = present(groupNode) ? enumValue(groupNode, groupPrefix,
"thingGroupIndexingMode", ON_OFF, true, errors) : null;
List<Field> groupManagedFields = fields(groupNode.path("managedFields"), groupPrefix + ".managedFields",
errors);
fields(groupNode.path("customFields"), groupPrefix + ".customFields", errors);
if (!errors.isEmpty()) {
throw invalid(errors.size() + (errors.size() == 1 ? " validation error" : " validation errors")
+ " detected: " + String.join("; ", errors));
}
if (thing != null) {
thing = validateThing(thing, managedFields);
}
// Checked in either mode and never stored: AWS derives the group's managed fields from its mode.
requireKnownManagedFields(groupManagedFields, THING_GROUP_MANAGED_FIELDS, groupPrefix);
synchronized (lock) {
IotIndexingConfiguration current = getIndexingConfiguration(region);
store.put(key(region), new IotIndexingConfiguration(thing == null ? current.thing() : thing,
groupMode == null ? current.thingGroupIndexingMode() : groupMode));
}
}
/** The stored configuration, or the OFF one AWS reports before the first update. */
public IotIndexingConfiguration getIndexingConfiguration(String region) {
return store.get(key(region)).orElse(IotIndexingConfiguration.OFF);
}
/** The schema of an enabled index. */
public String describeIndex(String indexName, String region) {
IotIndexingConfiguration configuration = getIndexingConfiguration(region);
String schema = switch (indexName) {
case THINGS_INDEX -> thingsSchema(configuration.thing());
case THING_GROUPS_INDEX -> "OFF".equals(configuration.thingGroupIndexingMode()) ? null : "REGISTRY";
default -> throw invalid("Unrecognized indexName " + indexName);
};
if (schema == null) {
throw new AwsException("ResourceNotFoundException", "Index " + indexName + " does not exist", 404);
}
return schema;
}
/** The managed fields AWS derives from the thing indexing modes; none while indexing is OFF. */
public static List<Field> thingManagedFields(ThingIndexing thing) {
if ("OFF".equals(thing.thingIndexingMode())) {
return List.of();
}
List<Field> fields = new ArrayList<>(REGISTRY_FIELDS);
if ("REGISTRY_AND_SHADOW".equals(thing.thingIndexingMode())) {
fields.addAll(SHADOW_FIELDS);
}
if ("ON".equals(thing.namedShadowIndexingMode())) {
fields.addAll(NAMED_SHADOW_FIELDS);
}
if ("STATUS".equals(thing.thingConnectivityIndexingMode())) {
fields.addAll(CONNECTIVITY_FIELDS);
}
if ("VIOLATIONS".equals(thing.deviceDefenderIndexingMode())) {
fields.addAll(DEVICE_DEFENDER_FIELDS);
}
return fields;
}
private static String thingsSchema(ThingIndexing thing) {
if ("OFF".equals(thing.thingIndexingMode())) {
return null;
}
if ("VIOLATIONS".equals(thing.deviceDefenderIndexingMode()) || "ON".equals(thing.namedShadowIndexingMode())) {
return "MULTI_INDEXING_MODE";
}
return "STATUS".equals(thing.thingConnectivityIndexingMode())
? thing.thingIndexingMode() + "_AND_CONNECTIVITY_STATUS"
: thing.thingIndexingMode();
}
private static ThingIndexing parseThing(JsonNode node, List<String> errors) {
String prefix = "thingIndexingConfiguration";
String mode = enumValue(node, prefix, "thingIndexingMode", THING_INDEXING_MODES, true, errors);
String connectivity = enumValue(node, prefix, "thingConnectivityIndexingMode", CONNECTIVITY_MODES, false, errors);
String deviceDefender = enumValue(node, prefix, "deviceDefenderIndexingMode", DEVICE_DEFENDER_MODES, false, errors);
String namedShadow = enumValue(node, prefix, "namedShadowIndexingMode", ON_OFF, false, errors);
List<Field> customFields = fields(node.path("customFields"), prefix + ".customFields", errors);
JsonNode filter = object(node.path("filter"), prefix + ".filter");
JsonNode connectivityFilter = object(filter.path("connectivity"), prefix + ".filter.connectivity");
List<String> socketInformation = texts(connectivityFilter.path("includeSocketInformation"),
prefix + ".filter.connectivity.includeSocketInformation");
// AWS checks this list as a whole and prints the whole list as the offending value.
if (!SOCKET_INFORMATION_APIS.containsAll(socketInformation)) {
errors.add("Value '" + socketInformation + "' at '" + prefix
+ ".filter.connectivity.includeSocketInformation' failed to satisfy constraint: "
+ "Member must satisfy constraint: [Member must satisfy enum value set: "
+ SOCKET_INFORMATION_APIS + "]");
}
List<GeoLocation> geoLocations = new ArrayList<>();
String geoLocationsPath = prefix + ".filter.geoLocations";
for (JsonNode location : list(filter.path("geoLocations"), geoLocationsPath, JsonNodeType.OBJECT)) {
String member = geoLocationsPath + "." + (geoLocations.size() + 1) + ".member";
String name = text(location.path("name"), member + ".name");
String order = text(location.path("order"), member + ".order");
if (order != null && !GEO_LOCATION_ORDERS.contains(order)) {
errors.add(enumError(order, member + ".order", GEO_LOCATION_ORDERS));
}
geoLocations.add(new GeoLocation(name, order == null ? "LatLon" : order));
}
return new ThingIndexing(mode, connectivity, deviceDefender, namedShadow,
texts(filter.path("namedShadowNames"), prefix + ".filter.namedShadowNames"),
geoLocations, socketInformation, customFields);
}
private static ThingIndexing validateThing(ThingIndexing thing, List<Field> managedFields) {
if ("OFF".equals(thing.thingIndexingMode())) {
requireOff(thing.thingConnectivityIndexingMode(), "ThingConnectivityIndexingMode");
requireOff(thing.namedShadowIndexingMode(), "NamedShadowIndexingMode");
requireOff(thing.deviceDefenderIndexingMode(), "DeviceDefenderIndexingMode");
}
if ("ON".equals(thing.namedShadowIndexingMode()) && thing.namedShadowNames().isEmpty()) {
throw invalid("NamedShadowNames Filter must not be empty for enabling NamedShadowIndexingMode");
}
for (Field field : thing.customFields()) {
if (field.name() == null) {
// AWS's message verbatim, the missing space included.
throw invalid("Custom field name cannot be nullFieldName: null, FieldType: " + field.type());
}
if (field.type() == null) {
throw invalid("Invalid field type: null. Expecting one of [String, Number, Boolean, Geopoint]");
}
if (KNOWN_THING_FIELDS.stream().anyMatch(known -> known.name().equals(field.name()))) {
throw invalid("Defined field is a reserved field for MULTI_INDEXING_MODE. Field: " + field.name());
}
}
requireKnownManagedFields(managedFields, KNOWN_THING_FIELDS, "thingIndexingConfiguration");
// AWS keeps nothing of a thing configuration that turns indexing OFF.
return "OFF".equals(thing.thingIndexingMode()) ? ThingIndexing.OFF : thing;
}
private static void requireKnownManagedFields(List<Field> fields, Collection<Field> known, String prefix) {
List<Field> unknown = fields.stream().filter(field -> !known.contains(field)).toList();
if (!unknown.isEmpty()) {
throw invalid("Only managed fields with expected types are allowed in " + prefix
+ ".managedFields. Invalid field(s): " + unknown.stream()
.map(field -> "name:" + field.name() + ", type:" + field.type())
.collect(Collectors.joining(", ", "[", "]")));
}
}
private static void requireOff(String mode, String member) {
if (!"OFF".equals(mode)) {
throw invalid("ThingIndexingMode must be turned ON for enabling " + member);
}
}
private static List<Field> fields(JsonNode node, String path, List<String> errors) {
List<Field> fields = new ArrayList<>();
for (JsonNode field : list(node, path, JsonNodeType.OBJECT)) {
String member = path + "." + (fields.size() + 1) + ".member";
String name = text(field.path("name"), member + ".name");
String type = text(field.path("type"), member + ".type");
if (type != null && !FIELD_TYPES.contains(type)) {
errors.add(enumError(type, member + ".type", FIELD_TYPES));
}
fields.add(new Field(name, type));
}
return fields;
}
/** A mode, OFF when an optional one is absent; a violation is collected rather than thrown. */
private static String enumValue(JsonNode node, String prefix, String member, List<String> allowed,
boolean required, List<String> errors) {
String value = text(node.path(member), prefix + "." + member);
if (value == null) {
if (required) {
errors.add("Value null at '" + prefix + "." + member
+ "' failed to satisfy constraint: Member must not be null");
}
return "OFF";
}
if (!allowed.contains(value)) {
errors.add(enumError(value, prefix + "." + member, allowed));
}
return value;
}
private static String enumError(String value, String path, List<String> allowed) {
return "Value '" + value + "' at '" + path
+ "' failed to satisfy constraint: Member must satisfy enum value set: " + allowed;
}
/** An object member, read as missing when it is absent or null. */
private static JsonNode object(JsonNode node, String path) {
if (!present(node)) {
return MissingNode.getInstance();
}
if (!node.isObject()) {
throw serializationError(path, JsonNodeType.OBJECT);
}
return node;
}
/** The elements of a list member, none when it is absent or null; every element must be of the given type. */
private static List<JsonNode> list(JsonNode node, String path, JsonNodeType elementType) {
if (!present(node)) {
return List.of();
}
if (!node.isArray()) {
throw serializationError(path, JsonNodeType.ARRAY);
}
List<JsonNode> elements = new ArrayList<>();
for (JsonNode element : node) {
if (element.getNodeType() != elementType) {
throw serializationError(path + "." + (elements.size() + 1), elementType);
}
elements.add(element);
}
return elements;
}
private static List<String> texts(JsonNode node, String path) {
List<String> values = new ArrayList<>();
for (JsonNode value : list(node, path, JsonNodeType.STRING)) {
values.add(value.asText());
}
return values;
}
/** A string member, null when it is absent or null. */
private static String text(JsonNode node, String path) {
if (!present(node)) {
return null;
}
if (!node.isTextual()) {
throw serializationError(path, JsonNodeType.STRING);
}
return node.asText();
}
private static boolean present(JsonNode node) {
return !node.isMissingNode() && !node.isNull();
}
/** A member of the wrong JSON type fails before any validation, as AWS fails to deserialize it. */
private static AwsException serializationError(String path, JsonNodeType expected) {
return new AwsException("SerializationException",
path + " must be a JSON " + expected.name().toLowerCase(Locale.ROOT), 400);
}
private static AwsException invalid(String message) {
return new AwsException("InvalidRequestException", message, 400);
}
private static String key(String region) {
return "indexing:" + region;
}
}
@@ -0,0 +1,26 @@
package io.github.hectorvent.floci.services.iot;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsException;
/**
* Reads an AWS IoT REST-JSON request body. AWS rejects a malformed body, and content after the
* JSON value, with {@code SerializationException}.
*/
final class IotRequestBody {
private IotRequestBody() {
}
static JsonNode read(ObjectMapper objectMapper, String body) {
try {
return objectMapper.reader().with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
.readTree(body == null || body.isBlank() ? "{}" : body);
} catch (JsonProcessingException e) {
throw new AwsException("SerializationException", e.getOriginalMessage(), 400);
}
}
}
@@ -0,0 +1,33 @@
package io.github.hectorvent.floci.services.iot.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.util.List;
/**
* The fleet indexing configuration of an account in one region, as UpdateIndexingConfiguration
* stores it. Managed fields are not stored: AWS derives them from the modes.
*/
@RegisterForReflection
public record IotIndexingConfiguration(ThingIndexing thing, String thingGroupIndexingMode) {
public static final IotIndexingConfiguration OFF = new IotIndexingConfiguration(ThingIndexing.OFF, "OFF");
@RegisterForReflection
public record ThingIndexing(String thingIndexingMode, String thingConnectivityIndexingMode,
String deviceDefenderIndexingMode, String namedShadowIndexingMode,
List<String> namedShadowNames, List<GeoLocation> geoLocations,
List<String> includeSocketInformation, List<Field> customFields) {
public static final ThingIndexing OFF =
new ThingIndexing("OFF", "OFF", "OFF", "OFF", List.of(), List.of(), List.of(), List.of());
}
@RegisterForReflection
public record Field(String name, String type) {
}
@RegisterForReflection
public record GeoLocation(String name, String order) {
}
}