From f413a667d033767520ae3eeb52a7488b1bcb3e22 Mon Sep 17 00:00:00 2001 From: Mario Toffia Date: Fri, 9 Oct 2026 22:52:30 +0200 Subject: [PATCH] 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. --- .../src/test/java/com/floci/test/IotTest.java | 107 +++ docs/services/index.md | 2 +- docs/services/iot.md | 20 +- .../core/common/ResolvedServiceCatalog.java | 3 +- .../services/iot/IotAuthorizerController.java | 28 +- .../iot/IotDomainConfigurationController.java | 17 +- .../iot/IotFleetIndexingController.java | 96 +++ .../services/iot/IotFleetIndexingService.java | 362 ++++++++++ .../floci/services/iot/IotRequestBody.java | 26 + .../iot/model/IotIndexingConfiguration.java | 33 + .../IotCustomAuthorizerIntegrationTest.java | 4 +- ...IotDomainConfigurationIntegrationTest.java | 20 + .../iot/IotFleetIndexingIntegrationTest.java | 286 ++++++++ .../iot/IotFleetIndexingServiceTest.java | 678 ++++++++++++++++++ 14 files changed, 1644 insertions(+), 38 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/services/iot/IotFleetIndexingController.java create mode 100644 src/main/java/io/github/hectorvent/floci/services/iot/IotFleetIndexingService.java create mode 100644 src/main/java/io/github/hectorvent/floci/services/iot/IotRequestBody.java create mode 100644 src/main/java/io/github/hectorvent/floci/services/iot/model/IotIndexingConfiguration.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/iot/IotFleetIndexingIntegrationTest.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/iot/IotFleetIndexingServiceTest.java diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/IotTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/IotTest.java index bfbc639301..2ede66e98f 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/IotTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/IotTest.java @@ -41,6 +41,8 @@ import software.amazon.awssdk.services.iot.model.DescribeDomainConfigurationRequ import software.amazon.awssdk.services.iot.model.DescribeDomainConfigurationResponse; import software.amazon.awssdk.services.iot.model.DescribeEndpointRequest; import software.amazon.awssdk.services.iot.model.DescribeEndpointResponse; +import software.amazon.awssdk.services.iot.model.DescribeIndexRequest; +import software.amazon.awssdk.services.iot.model.DescribeIndexResponse; import software.amazon.awssdk.services.iot.model.DescribeJobRequest; import software.amazon.awssdk.services.iot.model.DescribeThingRequest; import software.amazon.awssdk.services.iot.model.DescribeThingResponse; @@ -48,15 +50,20 @@ import software.amazon.awssdk.services.iot.model.DescribeThingTypeRequest; import software.amazon.awssdk.services.iot.model.DescribeThingTypeResponse; import software.amazon.awssdk.services.iot.model.DetachPolicyRequest; import software.amazon.awssdk.services.iot.model.DetachThingPrincipalRequest; +import software.amazon.awssdk.services.iot.model.DeviceDefenderIndexingMode; import software.amazon.awssdk.services.iot.model.DisableTopicRuleRequest; import software.amazon.awssdk.services.iot.model.DomainConfigurationStatus; import software.amazon.awssdk.services.iot.model.DomainConfigurationSummary; import software.amazon.awssdk.services.iot.model.DomainType; import software.amazon.awssdk.services.iot.model.EnableTopicRuleRequest; +import software.amazon.awssdk.services.iot.model.Field; +import software.amazon.awssdk.services.iot.model.GetIndexingConfigurationRequest; +import software.amazon.awssdk.services.iot.model.GetIndexingConfigurationResponse; import software.amazon.awssdk.services.iot.model.GetPolicyRequest; import software.amazon.awssdk.services.iot.model.GetPolicyResponse; import software.amazon.awssdk.services.iot.model.GetTopicRuleRequest; import software.amazon.awssdk.services.iot.model.GetTopicRuleResponse; +import software.amazon.awssdk.services.iot.model.IndexStatus; import software.amazon.awssdk.services.iot.model.InvalidRequestException; import software.amazon.awssdk.services.iot.model.ListCertificatesRequest; import software.amazon.awssdk.services.iot.model.ListCertificatesResponse; @@ -77,6 +84,7 @@ import software.amazon.awssdk.services.iot.model.ListThingsRequest; import software.amazon.awssdk.services.iot.model.ListThingsResponse; import software.amazon.awssdk.services.iot.model.ListTopicRulesRequest; import software.amazon.awssdk.services.iot.model.ListTopicRulesResponse; +import software.amazon.awssdk.services.iot.model.NamedShadowIndexingMode; import software.amazon.awssdk.services.iot.model.RemoveThingFromThingGroupRequest; import software.amazon.awssdk.services.iot.model.ResourceAlreadyExistsException; import software.amazon.awssdk.services.iot.model.ResourceNotFoundException; @@ -85,13 +93,19 @@ import software.amazon.awssdk.services.iot.model.ServiceType; import software.amazon.awssdk.services.iot.model.SqsAction; import software.amazon.awssdk.services.iot.model.Tag; import software.amazon.awssdk.services.iot.model.TagResourceRequest; +import software.amazon.awssdk.services.iot.model.ThingConnectivityIndexingMode; +import software.amazon.awssdk.services.iot.model.ThingGroupIndexingConfiguration; +import software.amazon.awssdk.services.iot.model.ThingGroupIndexingMode; import software.amazon.awssdk.services.iot.model.ThingGroupProperties; +import software.amazon.awssdk.services.iot.model.ThingIndexingConfiguration; +import software.amazon.awssdk.services.iot.model.ThingIndexingMode; import software.amazon.awssdk.services.iot.model.ThingTypeProperties; import software.amazon.awssdk.services.iot.model.TopicRulePayload; import software.amazon.awssdk.services.iot.model.UntagResourceRequest; import software.amazon.awssdk.services.iot.model.UpdateCertificateRequest; import software.amazon.awssdk.services.iot.model.UpdateDomainConfigurationRequest; import software.amazon.awssdk.services.iot.model.UpdateDomainConfigurationResponse; +import software.amazon.awssdk.services.iot.model.UpdateIndexingConfigurationRequest; import software.amazon.awssdk.services.iot.model.UpdateThingRequest; import software.amazon.awssdk.services.iot.model.UpdateThingTypeRequest; import software.amazon.awssdk.services.iot.model.VersionConflictException; @@ -261,6 +275,99 @@ class IotTest { .isInstanceOf(ResourceNotFoundException.class); } + @Test + void fleetIndexingConfiguration() { + GetIndexingConfigurationResponse prior = + iot.getIndexingConfiguration(GetIndexingConfigurationRequest.builder().build()); + try { + iot.updateIndexingConfiguration(UpdateIndexingConfigurationRequest.builder() + .thingIndexingConfiguration(ThingIndexingConfiguration.builder() + .thingIndexingMode(ThingIndexingMode.REGISTRY) + .thingConnectivityIndexingMode(ThingConnectivityIndexingMode.STATUS) + .build()) + .thingGroupIndexingConfiguration(ThingGroupIndexingConfiguration.builder() + .thingGroupIndexingMode(ThingGroupIndexingMode.ON) + .build()) + .build()); + + GetIndexingConfigurationResponse enabled = + iot.getIndexingConfiguration(GetIndexingConfigurationRequest.builder().build()); + ThingIndexingConfiguration thing = enabled.thingIndexingConfiguration(); + assertThat(thing.thingIndexingMode()).isEqualTo(ThingIndexingMode.REGISTRY); + assertThat(thing.thingConnectivityIndexingMode()).isEqualTo(ThingConnectivityIndexingMode.STATUS); + assertThat(thing.deviceDefenderIndexingMode()).isEqualTo(DeviceDefenderIndexingMode.OFF); + assertThat(thing.namedShadowIndexingMode()).isEqualTo(NamedShadowIndexingMode.OFF); + assertThat(thing.managedFields()) + .extracting(field -> field.name() + ":" + field.typeAsString()) + .containsExactlyInAnyOrder( + "thingName:String", "thingId:String", "registry.version:Number", + "registry.thingTypeName:String", "registry.thingGroupNames:String", + "connectivity.connected:Boolean", "connectivity.timestamp:Number", + "connectivity.disconnectReason:String", "connectivity.clientId:String", + "connectivity.cleanSession:Boolean", "connectivity.keepAliveDuration:Number", + "connectivity.sessionExpiry:Number", "connectivity.version:Number"); + assertThat(thing.filter().namedShadowNames()).isEmpty(); + assertThat(enabled.thingGroupIndexingConfiguration().thingGroupIndexingMode()) + .isEqualTo(ThingGroupIndexingMode.ON); + assertThat(enabled.thingGroupIndexingConfiguration().managedFields()) + .extracting(Field::name) + .containsExactlyInAnyOrder("parentGroupNames", "description", "version", "thingGroupName", + "thingGroupId"); + + DescribeIndexResponse things = iot.describeIndex(DescribeIndexRequest.builder() + .indexName("AWS_Things") + .build()); + assertThat(things.schema()).isEqualTo("REGISTRY_AND_CONNECTIVITY_STATUS"); + assertIndexStatus(things.indexStatus()); + DescribeIndexResponse groups = iot.describeIndex(DescribeIndexRequest.builder() + .indexName("AWS_ThingGroups") + .build()); + assertThat(groups.schema()).isEqualTo("REGISTRY"); + assertIndexStatus(groups.indexStatus()); + + assertThatThrownBy(() -> iot.updateIndexingConfiguration(UpdateIndexingConfigurationRequest.builder() + .thingIndexingConfiguration(ThingIndexingConfiguration.builder() + .thingIndexingMode(ThingIndexingMode.REGISTRY_AND_SHADOW) + .namedShadowIndexingMode(NamedShadowIndexingMode.ON) + .build()) + .build())) + .isInstanceOf(InvalidRequestException.class) + .hasMessageContaining("NamedShadowNames Filter must not be empty for enabling NamedShadowIndexingMode"); + + iot.updateIndexingConfiguration(UpdateIndexingConfigurationRequest.builder() + .thingIndexingConfiguration(ThingIndexingConfiguration.builder() + .thingIndexingMode(ThingIndexingMode.OFF) + .build()) + .build()); + assertThatThrownBy(() -> iot.describeIndex(DescribeIndexRequest.builder().indexName("AWS_Things").build())) + .isInstanceOf(ResourceNotFoundException.class) + .hasMessageContaining("Index AWS_Things does not exist"); + GetIndexingConfigurationResponse thingOff = + iot.getIndexingConfiguration(GetIndexingConfigurationRequest.builder().build()); + assertThat(thingOff.thingIndexingConfiguration().thingIndexingMode()).isEqualTo(ThingIndexingMode.OFF); + assertThat(thingOff.thingIndexingConfiguration().thingConnectivityIndexingMode()) + .isEqualTo(ThingConnectivityIndexingMode.OFF); + assertThat(thingOff.thingIndexingConfiguration().managedFields()).isEmpty(); + assertThat(thingOff.thingGroupIndexingConfiguration().thingGroupIndexingMode()) + .isEqualTo(ThingGroupIndexingMode.ON); + } finally { + // Indexing is account wide on AWS: put back whatever the account had before this test. + iot.updateIndexingConfiguration(UpdateIndexingConfigurationRequest.builder() + .thingIndexingConfiguration(prior.thingIndexingConfiguration()) + .thingGroupIndexingConfiguration(prior.thingGroupIndexingConfiguration()) + .build()); + } + } + + private static void assertIndexStatus(IndexStatus status) { + if (TestFixtures.isRealAws()) { + // AWS builds the index in the background after a mode change; Floci has it ready at once. + assertThat(status).isIn(IndexStatus.ACTIVE, IndexStatus.BUILDING, IndexStatus.REBUILDING); + } else { + assertThat(status).isEqualTo(IndexStatus.ACTIVE); + } + } + @Test void thingRegistryCrud() { String thingName = "java-iot-thing"; diff --git a/docs/services/index.md b/docs/services/index.md index f575b1e3bf..ee17d17718 100644 --- a/docs/services/index.md +++ b/docs/services/index.md @@ -142,7 +142,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac | [BCM Data Exports](bcm-data-exports.md) | `POST /` + `X-Amz-Target: AWSBillingAndCostManagementDataExports.*` | JSON 1.1 | 7 | | [Transfer Family](transfer.md) | `POST /` + `X-Amz-Target: TransferService.*` | JSON 1.1 | 17 | | [DataSync](datasync.md) | `POST /` + `X-Amz-Target: FmrsService.*` | JSON 1.1 | 48 | -| [IoT Core](iot.md) | `/things/...`, `/endpoint`, rules/policies REST paths | REST JSON | 71 | +| [IoT Core](iot.md) | `/things/...`, `/endpoint`, rules/policies REST paths | REST JSON | 74 | | [IoT Data](iot.md) | `/things/{thingName}/shadow`, MQTT topics | REST JSON | 11 | **Lambda, ElastiCache, RDS, MSK, MWAA, ECS, EKS, and OpenSearch** spin up real Docker containers and support IAM authentication and SigV4 request signing, the same auth flow as production AWS. **RDS Data API** executes SQL against the local RDS containers through AWS-compatible REST JSON routes. diff --git a/docs/services/iot.md b/docs/services/iot.md index 2b1fe5c255..a156363fee 100644 --- a/docs/services/iot.md +++ b/docs/services/iot.md @@ -45,7 +45,7 @@ Current MVP 2 limitations: - `SendDirectMessage` publishes to the requested MQTT topic through the embedded broker. Unlike AWS IoT Core, it does not yet bypass subscription matching to deliver to a client that is not subscribed to that topic. - `GetConnection` and `ListSubscriptions` report live in-memory broker state only; offline persistent session subscription reporting is not modeled yet. - Jobs reserved MQTT topics remain follow-up scope; Jobs Data HTTP APIs are implemented first. -- Dynamic thing groups, fleet indexing, job rollouts, cancellations, documents from S3, and advanced job scheduling are not yet modeled. +- Dynamic thing groups, job rollouts, cancellations, documents from S3, and advanced job scheduling are not yet modeled. Fleet indexing has its configuration only, see [Fleet Indexing](#fleet-indexing). ## Domain Configurations @@ -102,6 +102,24 @@ Current limitations: - On a connection, the authorizer is looked up in Floci's default account and region. - The MQTT password reaches the function as the broker decodes it, as UTF-8 text, so password bytes that are not valid UTF-8 arrive altered. The broker library, Vert.x MQTT, exposes the password only as a string. +## Fleet Indexing + +Status: configuration only. + +`UpdateIndexingConfiguration` and `GetIndexingConfiguration` (`/indexing/config`) and `DescribeIndex` (`/indices/{indexName}`) are served on the REST-JSON paths the AWS SDKs use, with the AWS shapes, error codes and validation messages: + +- There is one configuration per account and region. Before the first update both indexes are `OFF`. +- An update may carry the thing configuration, the thing group configuration, or both; the one it leaves out keeps its value. Turning `thingIndexingMode` to `OFF` clears the whole thing configuration, as on AWS. +- `managedFields` in `GetIndexingConfiguration` are derived from the modes, as AWS derives them. Managed fields sent in an update are only checked: every entry must be a field AWS manages, with its type, or the update fails with `InvalidRequestException`. A custom shadow path such as `shadow.name..reported.` is rejected this way on AWS too. The thing group's managed fields are checked the same way, also while `thingGroupIndexingMode` is `OFF`. +- The thing configuration's `customFields` and `filter` (named shadow names, geolocations, socket information) are stored and returned as sent, except that a geolocation without an `order` gets `LatLon`. A thing custom field without a name or a type, or named after a managed field, fails with `InvalidRequestException`. +- A member of the wrong JSON type, or content after the JSON body, fails with `SerializationException`. +- `DescribeIndex` reports `AWS_Things` and `AWS_ThingGroups` with the schema the modes select. The index is `ACTIVE` as soon as it is enabled: Floci has no `BUILDING` or `REBUILDING` window. A disabled index is `ResourceNotFoundException`, any other index name `InvalidRequestException`. + +Current limitations: + +- `SearchIndex`, `ListIndices` and the statistics and aggregation APIs (`GetStatistics`, `GetCardinality`, `GetPercentiles`, `GetBucketsAggregation`) are not modeled yet. +- `managedFields` and `customFields` sent in the thing group configuration are checked (the custom fields for their types only) but not stored: `GetIndexingConfiguration` reports the managed fields the mode selects and no custom fields. + ## MQTT Broker Status: complete. diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java index f39ef69a99..d6965e6def 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java @@ -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), diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotAuthorizerController.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotAuthorizerController.java index d519745bed..290f0895b5 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iot/IotAuthorizerController.java +++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotAuthorizerController.java @@ -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); - } - } } diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotDomainConfigurationController.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotDomainConfigurationController.java index 1eac96815f..1043a46e87 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iot/IotDomainConfigurationController.java +++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotDomainConfigurationController.java @@ -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); - } - } } diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotFleetIndexingController.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotFleetIndexingController.java new file mode 100644 index 0000000000..6a1aedf14e --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotFleetIndexingController.java @@ -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 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(); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotFleetIndexingService.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotFleetIndexingService.java new file mode 100644 index 0000000000..ff4850ab67 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotFleetIndexingService.java @@ -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 THING_INDEXING_MODES = List.of("REGISTRY_AND_SHADOW", "OFF", "REGISTRY"); + private static final List CONNECTIVITY_MODES = List.of("OFF", "STATUS"); + private static final List DEVICE_DEFENDER_MODES = List.of("VIOLATIONS", "OFF"); + private static final List ON_OFF = List.of("OFF", "ON"); + private static final List FIELD_TYPES = List.of("Boolean", "Number", "String"); + private static final List SOCKET_INFORMATION_APIS = List.of("GET_THING_CONNECTIVITY_DATA"); + private static final List GEO_LOCATION_ORDERS = List.of("LatLon", "LonLat"); + + private static final List 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 SHADOW_FIELDS = List.of( + new Field("shadow.version", "Number"), + new Field("shadow.hasDelta", "Boolean")); + private static final List NAMED_SHADOW_FIELDS = List.of( + new Field("shadow.name.*.hasDelta", "Boolean"), + new Field("shadow.name.*.version", "Number")); + private static final List 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 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 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 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 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>() {})); + } + + IotFleetIndexingService(StorageBackend 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 errors = new ArrayList<>(); + ThingIndexing thing = present(thingNode) ? parseThing(thingNode, errors) : null; + List managedFields = fields(thingNode.path("managedFields"), + "thingIndexingConfiguration.managedFields", errors); + String groupMode = present(groupNode) ? enumValue(groupNode, groupPrefix, + "thingGroupIndexingMode", ON_OFF, true, errors) : null; + List 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 thingManagedFields(ThingIndexing thing) { + if ("OFF".equals(thing.thingIndexingMode())) { + return List.of(); + } + List 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 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 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 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 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 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 fields, Collection known, String prefix) { + List 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 fields(JsonNode node, String path, List errors) { + List 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 allowed, + boolean required, List 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 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 list(JsonNode node, String path, JsonNodeType elementType) { + if (!present(node)) { + return List.of(); + } + if (!node.isArray()) { + throw serializationError(path, JsonNodeType.ARRAY); + } + List 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 texts(JsonNode node, String path) { + List 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; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotRequestBody.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotRequestBody.java new file mode 100644 index 0000000000..268496c3f5 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotRequestBody.java @@ -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); + } + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/model/IotIndexingConfiguration.java b/src/main/java/io/github/hectorvent/floci/services/iot/model/IotIndexingConfiguration.java new file mode 100644 index 0000000000..58e51fe98d --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/iot/model/IotIndexingConfiguration.java @@ -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 namedShadowNames, List geoLocations, + List includeSocketInformation, List 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) { + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotCustomAuthorizerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotCustomAuthorizerIntegrationTest.java index 3cc89bc079..e0683550e2 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iot/IotCustomAuthorizerIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotCustomAuthorizerIntegrationTest.java @@ -127,12 +127,12 @@ class IotCustomAuthorizerIntegrationTest { } @Test - void aTestInvokeBodyWithTrailingContentIsAnInvalidRequest() { + void aTestInvokeBodyWithTrailingContentIsASerializationError() { given().contentType("application/json").body("{\"mqttContext\": {}} trailing") .when().post("/authorizer/" + authorizer + "/test") .then() .statusCode(400) - .header("X-Amzn-ErrorType", "InvalidRequestException"); + .header("X-Amzn-ErrorType", "SerializationException"); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotDomainConfigurationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotDomainConfigurationIntegrationTest.java index ac71a64ff1..5bbe842246 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iot/IotDomainConfigurationIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotDomainConfigurationIntegrationTest.java @@ -318,6 +318,26 @@ class IotDomainConfigurationIntegrationTest { .body("__type", equalTo("InvalidRequestException")); } + @Test + void createWithTrailingContentAfterTheBodyIsASerializationError() { + given() + .contentType("application/json") + .body(createBody("trailing.it.example.com", "DATA") + " trailing") + .when() + .post("/domainConfigurations/it-domain-trailing") + .then() + .statusCode(400) + .header("x-amzn-ErrorType", equalTo("SerializationException")) + .body("__type", equalTo("SerializationException")); + + given() + .when() + .get("/domainConfigurations/it-domain-trailing") + .then() + .statusCode(404) + .body("__type", equalTo("ResourceNotFoundException")); + } + @Test void describeEndpointAcceptsTheCredentialProviderEndpointType() { given() diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotFleetIndexingIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotFleetIndexingIntegrationTest.java new file mode 100644 index 0000000000..f75a03282c --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotFleetIndexingIntegrationTest.java @@ -0,0 +1,286 @@ +package io.github.hectorvent.floci.services.iot; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.aMapWithSize; +import static org.hamcrest.Matchers.anEmptyMap; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItems; +import static org.hamcrest.Matchers.hasKey; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.startsWith; + +/** + * Drives fleet indexing the way the AWS SDKs do: UpdateIndexingConfiguration and + * GetIndexingConfiguration on {@code /indexing/config}, DescribeIndex on {@code /indices/{indexName}}. + * Every test signs as its own account, so the configurations they store never meet. + */ +@QuarkusTest +class IotFleetIndexingIntegrationTest { + + private static String auth(String account, String region) { + return "AWS4-HMAC-SHA256 Credential=" + account + "/20260215/" + region + + "/iot/aws4_request, SignedHeaders=host, Signature=abc"; + } + + private static void update(String auth, String body) { + given() + .header("Authorization", auth) + .contentType("application/json") + .body(body) + .when() + .post("/indexing/config") + .then() + .statusCode(200) + .body(equalTo("{}")); + } + + @Test + void getBeforeAnyUpdateReturnsTheOffShape() { + given() + .header("Authorization", auth("111100000001", "us-east-1")) + .when() + .get("/indexing/config") + .then() + .statusCode(200) + .body("thingIndexingConfiguration", aMapWithSize(5)) + .body("thingIndexingConfiguration.thingIndexingMode", equalTo("OFF")) + .body("thingIndexingConfiguration.thingConnectivityIndexingMode", equalTo("OFF")) + .body("thingIndexingConfiguration.deviceDefenderIndexingMode", equalTo("OFF")) + .body("thingIndexingConfiguration.namedShadowIndexingMode", equalTo("OFF")) + .body("thingIndexingConfiguration.filter", anEmptyMap()) + .body("thingGroupIndexingConfiguration", aMapWithSize(1)) + .body("thingGroupIndexingConfiguration.thingGroupIndexingMode", equalTo("OFF")); + } + + @Test + void updateThenGetRoundTripsTheWireShape() { + String auth = auth("111100000002", "us-east-1"); + update(auth, """ + { + "thingIndexingConfiguration": { + "thingIndexingMode": "REGISTRY_AND_SHADOW", + "thingConnectivityIndexingMode": "STATUS", + "namedShadowIndexingMode": "ON", + "customFields": [{"name": "attributes.site", "type": "String"}], + "filter": { + "namedShadowNames": ["config"], + "geoLocations": [{"name": "shadow.reported.location", "order": "LatLon"}] + } + }, + "thingGroupIndexingConfiguration": {"thingGroupIndexingMode": "ON"} + } + """); + + given() + .header("Authorization", auth) + .when() + .get("/indexing/config") + .then() + .statusCode(200) + .body("thingIndexingConfiguration.thingIndexingMode", equalTo("REGISTRY_AND_SHADOW")) + .body("thingIndexingConfiguration.thingConnectivityIndexingMode", equalTo("STATUS")) + .body("thingIndexingConfiguration.deviceDefenderIndexingMode", equalTo("OFF")) + .body("thingIndexingConfiguration.namedShadowIndexingMode", equalTo("ON")) + .body("thingIndexingConfiguration.managedFields", hasSize(17)) + .body("thingIndexingConfiguration.managedFields.name", hasItems("thingName", "registry.thingGroupNames", + "shadow.hasDelta", "shadow.name.*.version", "connectivity.connected", "connectivity.sessionExpiry")) + .body("thingIndexingConfiguration.managedFields.find { it.name == 'connectivity.connected' }.type", + equalTo("Boolean")) + .body("thingIndexingConfiguration.customFields", hasSize(1)) + .body("thingIndexingConfiguration.customFields[0].name", equalTo("attributes.site")) + .body("thingIndexingConfiguration.customFields[0].type", equalTo("String")) + .body("thingIndexingConfiguration.filter.namedShadowNames", contains("config")) + .body("thingIndexingConfiguration.filter.geoLocations", hasSize(1)) + .body("thingIndexingConfiguration.filter.geoLocations[0].name", equalTo("shadow.reported.location")) + .body("thingIndexingConfiguration.filter.geoLocations[0].order", equalTo("LatLon")) + .body("thingIndexingConfiguration.filter.connectivity.includeSocketInformation", empty()) + .body("thingGroupIndexingConfiguration.thingGroupIndexingMode", equalTo("ON")) + .body("thingGroupIndexingConfiguration.managedFields.name", containsInAnyOrder("parentGroupNames", + "description", "version", "thingGroupName", "thingGroupId")); + } + + @Test + void describeIndexReportsAnActiveSchemaUntilIndexingIsTurnedOff() { + String auth = auth("111100000003", "us-east-1"); + update(auth, """ + { + "thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY", "thingConnectivityIndexingMode": "STATUS"}, + "thingGroupIndexingConfiguration": {"thingGroupIndexingMode": "ON"} + } + """); + + given() + .header("Authorization", auth) + .when() + .get("/indices/AWS_Things") + .then() + .statusCode(200) + .body("indexName", equalTo("AWS_Things")) + .body("indexStatus", equalTo("ACTIVE")) + .body("schema", equalTo("REGISTRY_AND_CONNECTIVITY_STATUS")); + given() + .header("Authorization", auth) + .when() + .get("/indices/AWS_ThingGroups") + .then() + .statusCode(200) + .body("indexName", equalTo("AWS_ThingGroups")) + .body("indexStatus", equalTo("ACTIVE")) + .body("schema", equalTo("REGISTRY")); + + update(auth, """ + {"thingIndexingConfiguration": {"thingIndexingMode": "OFF"}} + """); + + given() + .header("Authorization", auth) + .when() + .get("/indices/AWS_Things") + .then() + .statusCode(404) + .body("__type", equalTo("ResourceNotFoundException")) + .body("message", equalTo("Index AWS_Things does not exist")); + given() + .header("Authorization", auth) + .when() + .get("/indices/AWS_ThingGroups") + .then() + .statusCode(200) + .body("schema", equalTo("REGISTRY")); + given() + .header("Authorization", auth) + .when() + .get("/indexing/config") + .then() + .statusCode(200) + .body("thingIndexingConfiguration.thingIndexingMode", equalTo("OFF")) + .body("thingIndexingConfiguration.thingConnectivityIndexingMode", equalTo("OFF")) + .body("thingIndexingConfiguration", not(hasKey("managedFields"))) + .body("thingIndexingConfiguration.filter", anEmptyMap()) + .body("thingGroupIndexingConfiguration.thingGroupIndexingMode", equalTo("ON")); + given() + .header("Authorization", auth) + .when() + .get("/indices/Nope") + .then() + .statusCode(400) + .body("__type", equalTo("InvalidRequestException")) + .body("message", equalTo("Unrecognized indexName Nope")); + } + + @Test + void configurationIsIsolatedPerAccountAndRegion() { + String home = auth("111100000004", "us-east-1"); + update(home, """ + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY"}} + """); + + given() + .header("Authorization", auth("111100000005", "us-east-1")) + .when() + .get("/indexing/config") + .then() + .statusCode(200) + .body("thingIndexingConfiguration.thingIndexingMode", equalTo("OFF")); + given() + .header("Authorization", auth("111100000005", "us-east-1")) + .when() + .get("/indices/AWS_Things") + .then() + .statusCode(404) + .body("__type", equalTo("ResourceNotFoundException")); + given() + .header("Authorization", auth("111100000004", "eu-west-1")) + .when() + .get("/indexing/config") + .then() + .statusCode(200) + .body("thingIndexingConfiguration.thingIndexingMode", equalTo("OFF")); + given() + .header("Authorization", home) + .when() + .get("/indexing/config") + .then() + .statusCode(200) + .body("thingIndexingConfiguration.thingIndexingMode", equalTo("REGISTRY")); + } + + @Test + void invalidUpdatesAreRejectedAndStoreNothing() { + String auth = auth("111100000006", "us-east-1"); + given() + .header("Authorization", auth) + .contentType("application/json") + .body("{\"thingIndexingConfiguration\": ") + .when() + .post("/indexing/config") + .then() + .statusCode(400) + .body("__type", equalTo("SerializationException")); + given() + .header("Authorization", auth) + .contentType("application/json") + .body(""" + {"thingIndexingConfiguration": {}} + """) + .when() + .post("/indexing/config") + .then() + .statusCode(400) + .body("__type", equalTo("InvalidRequestException")) + .body("message", equalTo("1 validation error detected: Value null at " + + "'thingIndexingConfiguration.thingIndexingMode' failed to satisfy constraint: " + + "Member must not be null")); + given() + .header("Authorization", auth) + .contentType("application/json") + .body(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY", + "managedFields": [{"name": "shadow.name.building.reported.type", "type": "String"}]}} + """) + .when() + .post("/indexing/config") + .then() + .statusCode(400) + .body("__type", equalTo("InvalidRequestException")) + .body("message", startsWith("Only managed fields with expected types are allowed")); + + given() + .header("Authorization", auth) + .when() + .get("/indexing/config") + .then() + .statusCode(200) + .body("thingIndexingConfiguration.thingIndexingMode", equalTo("OFF")); + } + + @Test + void anUpdateBodyWithTrailingContentIsASerializationError() { + String auth = auth("111100000007", "us-east-1"); + given() + .header("Authorization", auth) + .contentType("application/json") + .body("{\"thingIndexingConfiguration\": {\"thingIndexingMode\": \"REGISTRY\"}} trailing") + .when() + .post("/indexing/config") + .then() + .statusCode(400) + .header("x-amzn-ErrorType", equalTo("SerializationException")) + .body("__type", equalTo("SerializationException")); + + given() + .header("Authorization", auth) + .when() + .get("/indexing/config") + .then() + .statusCode(200) + .body("thingIndexingConfiguration.thingIndexingMode", equalTo("OFF")); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotFleetIndexingServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotFleetIndexingServiceTest.java new file mode 100644 index 0000000000..34699f60f3 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotFleetIndexingServiceTest.java @@ -0,0 +1,678 @@ +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 io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +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 org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Stream; + +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 IotFleetIndexingServiceTest { + + private static final String REGION = "us-east-1"; + + private static final List REGISTRY = 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 SHADOW = List.of( + new Field("shadow.version", "Number"), + new Field("shadow.hasDelta", "Boolean")); + private static final List NAMED_SHADOW = List.of( + new Field("shadow.name.*.hasDelta", "Boolean"), + new Field("shadow.name.*.version", "Number")); + private static final List CONNECTIVITY = 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 DEVICE_DEFENDER = 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 List THING_GROUP = List.of( + new Field("parentGroupNames", "String"), + new Field("description", "String"), + new Field("version", "Number"), + new Field("thingGroupName", "String"), + new Field("thingGroupId", "String")); + + private final ObjectMapper mapper = new ObjectMapper(); + private final IotFleetIndexingService service = new IotFleetIndexingService(new InMemoryStorage<>()); + + private JsonNode json(String text) { + try { + return mapper.readTree(text); + } catch (JsonProcessingException e) { + throw new IllegalArgumentException(e); + } + } + + private void update(String body) { + service.updateIndexingConfiguration(json(body), REGION); + } + + private ThingIndexing thing() { + return service.getIndexingConfiguration(REGION).thing(); + } + + private Set managedFieldsAfter(String body) { + update(body); + return new HashSet<>(IotFleetIndexingService.thingManagedFields(thing())); + } + + @SafeVarargs + private static Set union(List... groups) { + Set fields = new HashSet<>(); + Stream.of(groups).forEach(fields::addAll); + return fields; + } + + private void assertRejected(String body, String message) { + AwsException failure = assertThrows(AwsException.class, () -> update(body)); + assertEquals("InvalidRequestException", failure.getErrorCode()); + assertEquals(400, failure.getHttpStatus()); + assertEquals(message, failure.getMessage()); + } + + private void assertSerializationError(String body) { + AwsException failure = assertThrows(AwsException.class, () -> update(body), body); + assertEquals("SerializationException", failure.getErrorCode(), body); + assertEquals(400, failure.getHttpStatus(), body); + } + + private static String enumError(String value, String path, String allowed) { + return "Value '" + value + "' at '" + path + + "' failed to satisfy constraint: Member must satisfy enum value set: " + allowed; + } + + private void assertDescribeFails(String indexName, String code, int status, String message) { + AwsException failure = assertThrows(AwsException.class, () -> service.describeIndex(indexName, REGION)); + assertEquals(code, failure.getErrorCode()); + assertEquals(status, failure.getHttpStatus()); + assertEquals(message, failure.getMessage()); + } + + @Test + void unsetConfigurationIsOff() { + IotIndexingConfiguration configuration = service.getIndexingConfiguration(REGION); + + assertEquals(IotIndexingConfiguration.OFF, configuration); + assertEquals(List.of(), IotFleetIndexingService.thingManagedFields(configuration.thing())); + } + + @Test + void registryModeManagesRegistryFieldsAndDefaultsTheRest() { + update(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY"}} + """); + + assertEquals(new ThingIndexing("REGISTRY", "OFF", "OFF", "OFF", List.of(), List.of(), List.of(), List.of()), + thing()); + assertEquals(new HashSet<>(REGISTRY), new HashSet<>(IotFleetIndexingService.thingManagedFields(thing()))); + assertEquals("OFF", service.getIndexingConfiguration(REGION).thingGroupIndexingMode()); + } + + @Test + void eachModeAddsItsManagedFields() { + assertEquals(union(REGISTRY, SHADOW), managedFieldsAfter(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY_AND_SHADOW"}} + """)); + assertEquals(union(REGISTRY, CONNECTIVITY), managedFieldsAfter(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY", "thingConnectivityIndexingMode": "STATUS"}} + """)); + assertEquals(union(REGISTRY, NAMED_SHADOW), managedFieldsAfter(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY", "namedShadowIndexingMode": "ON", + "filter": {"namedShadowNames": ["config"]}}} + """)); + assertEquals(union(REGISTRY, DEVICE_DEFENDER), managedFieldsAfter(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY", "deviceDefenderIndexingMode": "VIOLATIONS"}} + """)); + assertEquals(union(REGISTRY, SHADOW, NAMED_SHADOW, CONNECTIVITY, DEVICE_DEFENDER), managedFieldsAfter(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY_AND_SHADOW", + "thingConnectivityIndexingMode": "STATUS", "deviceDefenderIndexingMode": "VIOLATIONS", + "namedShadowIndexingMode": "ON", "filter": {"namedShadowNames": ["config"]}}} + """)); + } + + @Test + void thingGroupIndexingManagesGroupFields() { + update(""" + {"thingGroupIndexingConfiguration": {"thingGroupIndexingMode": "ON"}} + """); + + assertEquals("ON", service.getIndexingConfiguration(REGION).thingGroupIndexingMode()); + assertEquals(new HashSet<>(THING_GROUP), new HashSet<>(IotFleetIndexingService.THING_GROUP_MANAGED_FIELDS)); + } + + @Test + void clientSentFilterAndCustomFieldsAreKept() { + update(""" + {"thingIndexingConfiguration": { + "thingIndexingMode": "REGISTRY_AND_SHADOW", + "namedShadowIndexingMode": "ON", + "customFields": [{"name": "attributes.site", "type": "String"}], + "filter": { + "namedShadowNames": ["config", "state"], + "geoLocations": [{"name": "shadow.reported.location", "order": "LatLon"}], + "connectivity": {"includeSocketInformation": ["GET_THING_CONNECTIVITY_DATA"]} + }}} + """); + + assertEquals(new ThingIndexing("REGISTRY_AND_SHADOW", "OFF", "OFF", "ON", List.of("config", "state"), + List.of(new GeoLocation("shadow.reported.location", "LatLon")), List.of("GET_THING_CONNECTIVITY_DATA"), + List.of(new Field("attributes.site", "String"))), thing()); + } + + @Test + void aGeoLocationWithoutAnOrderIsStoredAsLatLon() { + update(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY_AND_SHADOW", + "filter": {"geoLocations": [{"name": "shadow.name.building.reported.location"}]}}} + """); + + assertEquals(List.of(new GeoLocation("shadow.name.building.reported.location", "LatLon")), + thing().geoLocations()); + } + + @Test + void turningThingIndexingOffResetsTheThingConfiguration() { + update(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY_AND_SHADOW", + "thingConnectivityIndexingMode": "STATUS", "namedShadowIndexingMode": "ON", + "customFields": [{"name": "attributes.site", "type": "String"}], + "filter": {"namedShadowNames": ["config"]}}} + """); + + update(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "OFF", + "customFields": [{"name": "attributes.site", "type": "String"}], + "filter": {"namedShadowNames": ["config"]}}} + """); + + assertEquals(ThingIndexing.OFF, thing()); + assertEquals(List.of(), IotFleetIndexingService.thingManagedFields(thing())); + } + + @Test + void updatingOneConfigurationLeavesTheOtherUnchanged() { + update(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY"}, + "thingGroupIndexingConfiguration": {"thingGroupIndexingMode": "ON"}} + """); + + update(""" + {"thingGroupIndexingConfiguration": {"thingGroupIndexingMode": "OFF"}} + """); + assertEquals("REGISTRY", thing().thingIndexingMode()); + + update(""" + {"thingGroupIndexingConfiguration": {"thingGroupIndexingMode": "ON"}} + """); + update(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY_AND_SHADOW"}} + """); + assertEquals("REGISTRY_AND_SHADOW", thing().thingIndexingMode()); + assertEquals("ON", service.getIndexingConfiguration(REGION).thingGroupIndexingMode()); + } + + @Test + void configurationIsKeptPerRegion() { + update(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY"}} + """); + + assertEquals(IotIndexingConfiguration.OFF, service.getIndexingConfiguration("eu-west-1")); + assertEquals("REGISTRY", thing().thingIndexingMode()); + } + + @Test + void updateWithoutAnyConfigurationIsRejected() { + String message = "At least one configuration to update " + + "(thingIndexingConfiguration / thingGroupIndexingConfiguration) is required"; + assertRejected("{}", message); + assertRejected(""" + {"thingIndexingConfiguration": null} + """, message); + } + + @Test + void missingModesAreReportedAsNullMembers() { + assertRejected(""" + {"thingIndexingConfiguration": {}} + """, "1 validation error detected: Value null at 'thingIndexingConfiguration.thingIndexingMode' " + + "failed to satisfy constraint: Member must not be null"); + assertRejected(""" + {"thingGroupIndexingConfiguration": {}} + """, "1 validation error detected: Value null at 'thingGroupIndexingConfiguration.thingGroupIndexingMode' " + + "failed to satisfy constraint: Member must not be null"); + } + + @Test + void enumViolationsAreReportedWithAwsValueSets() { + Map violations = Map.of( + """ + {"thingIndexingConfiguration": {"thingIndexingMode": "BOGUS"}} + """, enumError("BOGUS", "thingIndexingConfiguration.thingIndexingMode", + "[REGISTRY_AND_SHADOW, OFF, REGISTRY]"), + """ + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY", "thingConnectivityIndexingMode": "BOGUS"}} + """, enumError("BOGUS", "thingIndexingConfiguration.thingConnectivityIndexingMode", "[OFF, STATUS]"), + """ + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY", "deviceDefenderIndexingMode": "BOGUS"}} + """, enumError("BOGUS", "thingIndexingConfiguration.deviceDefenderIndexingMode", "[VIOLATIONS, OFF]"), + """ + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY", "namedShadowIndexingMode": "BOGUS"}} + """, enumError("BOGUS", "thingIndexingConfiguration.namedShadowIndexingMode", "[OFF, ON]"), + """ + {"thingGroupIndexingConfiguration": {"thingGroupIndexingMode": "BOGUS"}} + """, enumError("BOGUS", "thingGroupIndexingConfiguration.thingGroupIndexingMode", "[OFF, ON]"), + """ + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY", + "customFields": [{"name": "attributes.site", "type": "Bogus"}]}} + """, enumError("Bogus", "thingIndexingConfiguration.customFields.1.member.type", + "[Boolean, Number, String]")); + + violations.forEach((body, error) -> assertRejected(body, "1 validation error detected: " + error)); + } + + @Test + void severalViolationsAreReportedTogether() { + AwsException failure = assertThrows(AwsException.class, () -> update(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "BOGUS", "thingConnectivityIndexingMode": "BOGUS"}} + """)); + + assertEquals("InvalidRequestException", failure.getErrorCode()); + assertTrue(failure.getMessage().startsWith("2 validation errors detected: "), failure.getMessage()); + assertTrue(failure.getMessage().contains(enumError("BOGUS", "thingIndexingConfiguration.thingIndexingMode", + "[REGISTRY_AND_SHADOW, OFF, REGISTRY]")), failure.getMessage()); + assertTrue(failure.getMessage().contains("; "), failure.getMessage()); + assertTrue(failure.getMessage().contains(enumError("BOGUS", + "thingIndexingConfiguration.thingConnectivityIndexingMode", "[OFF, STATUS]")), failure.getMessage()); + } + + @Test + void filterEnumsAreValidatedEvenWithThingIndexingOff() { + assertRejected(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "OFF", "filter": { + "geoLocations": [{"name": "shadow.reported.loc", "order": "BOGUS"}], + "connectivity": {"includeSocketInformation": ["GetConnection"]}}}} + """, "2 validation errors detected: Value '[GetConnection]' at " + + "'thingIndexingConfiguration.filter.connectivity.includeSocketInformation' failed to satisfy " + + "constraint: Member must satisfy constraint: [Member must satisfy enum value set: " + + "[GET_THING_CONNECTIVITY_DATA]]; Value 'BOGUS' at " + + "'thingIndexingConfiguration.filter.geoLocations.1.member.order' failed to satisfy constraint: " + + "Member must satisfy enum value set: [LatLon, LonLat]"); + } + + @Test + void secondaryModesNeedThingIndexingOn() { + assertRejected(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "OFF", "thingConnectivityIndexingMode": "STATUS"}} + """, "ThingIndexingMode must be turned ON for enabling ThingConnectivityIndexingMode"); + assertRejected(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "OFF", "namedShadowIndexingMode": "ON", + "filter": {"namedShadowNames": ["config"]}}} + """, "ThingIndexingMode must be turned ON for enabling NamedShadowIndexingMode"); + assertRejected(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "OFF", "deviceDefenderIndexingMode": "VIOLATIONS"}} + """, "ThingIndexingMode must be turned ON for enabling DeviceDefenderIndexingMode"); + } + + @Test + void namedShadowIndexingNeedsShadowNames() { + String message = "NamedShadowNames Filter must not be empty for enabling NamedShadowIndexingMode"; + assertRejected(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY_AND_SHADOW", "namedShadowIndexingMode": "ON"}} + """, message); + assertRejected(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY_AND_SHADOW", "namedShadowIndexingMode": "ON", + "filter": {"namedShadowNames": []}}} + """, message); + } + + @Test + void managedFieldsMustBeKnownWithTheirExpectedType() { + String prefix = "Only managed fields with expected types are allowed in " + + "thingIndexingConfiguration.managedFields. Invalid field(s): "; + assertRejected(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY", + "managedFields": [{"name": "thingName", "type": "Number"}]}} + """, prefix + "[name:thingName, type:Number]"); + assertRejected(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY", + "managedFields": [{"name": "thingId", "type": "String"}, {"name": "a", "type": "String"}, + {"name": "b", "type": "String"}]}} + """, prefix + "[name:a, type:String, name:b, type:String]"); + // The configuration TEOS deploys: AWS rejects custom shadow paths sent as managed fields. + assertRejected(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY_AND_SHADOW", "namedShadowIndexingMode": "ON", + "filter": {"namedShadowNames": ["building"]}, + "managedFields": [{"name": "shadow.name.building.reported.type", "type": "String"}, + {"name": "shadow.name.building.reported.tz", "type": "String"}]}} + """, prefix + "[name:shadow.name.building.reported.type, type:String, " + + "name:shadow.name.building.reported.tz, type:String]"); + } + + @Test + void knownManagedFieldsAreAcceptedInAnyModeAndNotStored() { + update(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY", + "managedFields": [{"name": "shadow.version", "type": "Number"}, + {"name": "connectivity.connected", "type": "Boolean"}]}} + """); + + assertEquals(new HashSet<>(REGISTRY), new HashSet<>(IotFleetIndexingService.thingManagedFields(thing()))); + } + + @Test + void rejectedUpdateChangesNothing() { + update(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY"}, + "thingGroupIndexingConfiguration": {"thingGroupIndexingMode": "ON"}} + """); + + assertThrows(AwsException.class, () -> update(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY_AND_SHADOW", "namedShadowIndexingMode": "ON"}, + "thingGroupIndexingConfiguration": {"thingGroupIndexingMode": "OFF"}} + """)); + + assertEquals("REGISTRY", thing().thingIndexingMode()); + assertEquals("ON", service.getIndexingConfiguration(REGION).thingGroupIndexingMode()); + } + + // Single quotes stand in for double quotes to keep the bodies on one line. + @ParameterizedTest + @ValueSource(strings = { + "[]", + "'x'", + "{'thingIndexingConfiguration': 'x'}", + "{'thingGroupIndexingConfiguration': []}", + "{'thingIndexingConfiguration': {'thingIndexingMode': 1}}", + "{'thingGroupIndexingConfiguration': {'thingGroupIndexingMode': 1}}", + "{'thingIndexingConfiguration': {'thingIndexingMode': 'BOGUS', 'customFields': {}}}", + "{'thingIndexingConfiguration': {'thingIndexingMode': 'BOGUS'}, " + + "'thingGroupIndexingConfiguration': {'thingGroupIndexingMode': 'ON', 'customFields': [1]}}" + }) + void aBodyOrConfigurationOfTheWrongJsonKindIsASerializationError(String body) { + assertSerializationError(body.replace('\'', '"')); + } + + @ParameterizedTest + @ValueSource(strings = { + "'thingConnectivityIndexingMode': true", + "'deviceDefenderIndexingMode': {}", + "'namedShadowIndexingMode': []", + "'customFields': {}", + "'customFields': [1]", + "'customFields': [null]", + "'customFields': [{'name': 1, 'type': 'String'}]", + "'customFields': [{'name': 'attributes.x', 'type': 1}]", + "'managedFields': 'x'", + "'managedFields': ['thingName']", + "'filter': 'bad'", + "'filter': {'namedShadowNames': 'config'}", + "'filter': {'namedShadowNames': [1]}", + "'filter': {'namedShadowNames': [null]}", + "'filter': {'geoLocations': {}}", + "'filter': {'geoLocations': [1]}", + "'filter': {'geoLocations': [{'name': 1}]}", + "'filter': {'geoLocations': [{'name': 'shadow.reported.location', 'order': 1}]}", + "'filter': {'connectivity': 1}", + "'filter': {'connectivity': {'includeSocketInformation': 'GET_THING_CONNECTIVITY_DATA'}}", + "'filter': {'connectivity': {'includeSocketInformation': [1]}}", + "'filter': {'connectivity': {'includeSocketInformation': [null]}}" + }) + void aThingMemberOfTheWrongJsonKindIsASerializationError(String member) { + assertSerializationError(("{'thingIndexingConfiguration': {'thingIndexingMode': 'REGISTRY', " + member + "}}") + .replace('\'', '"')); + } + + @ParameterizedTest + @ValueSource(strings = { + "'managedFields': 'x'", + "'managedFields': [null]", + "'managedFields': [{'name': 'version', 'type': 2}]", + "'customFields': {}", + "'customFields': [true]" + }) + void aThingGroupMemberOfTheWrongJsonKindIsASerializationError(String member) { + assertSerializationError(("{'thingGroupIndexingConfiguration': {'thingGroupIndexingMode': 'OFF', " + member + "}}") + .replace('\'', '"')); + } + + @Test + void absentAndNullMembersAreNotSent() { + update(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY", "thingConnectivityIndexingMode": null, + "customFields": null, "managedFields": null, + "filter": {"namedShadowNames": null, "geoLocations": null, "connectivity": {"includeSocketInformation": null}}}, + "thingGroupIndexingConfiguration": {"thingGroupIndexingMode": "ON", "managedFields": null, "customFields": null}} + """); + assertEquals(new ThingIndexing("REGISTRY", "OFF", "OFF", "OFF", List.of(), List.of(), List.of(), List.of()), + thing()); + + update(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY_AND_SHADOW", "filter": null}, + "thingGroupIndexingConfiguration": null} + """); + update(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY_AND_SHADOW", "filter": {"connectivity": null}}} + """); + assertEquals(new IotIndexingConfiguration(new ThingIndexing("REGISTRY_AND_SHADOW", "OFF", "OFF", "OFF", + List.of(), List.of(), List.of(), List.of()), "ON"), service.getIndexingConfiguration(REGION)); + } + + @Test + void thingCustomFieldsNeedANameAndThenAType() { + String noName = "Custom field name cannot be nullFieldName: null, FieldType: "; + String noType = "Invalid field type: null. Expecting one of [String, Number, Boolean, Geopoint]"; + assertRejected(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY", "customFields": [{}]}} + """, noName + "null"); + assertRejected(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY", "customFields": [{"type": "String"}]}} + """, noName + "String"); + assertRejected(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY", "customFields": [{"name": "attributes.x"}]}} + """, noType); + assertRejected(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY", + "customFields": [{"name": "attributes.a"}, {"type": "Number"}]}} + """, noType); + assertRejected(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY", + "customFields": [{"name": "attributes.a", "type": "String"}, {"type": "Number"}]}} + """, noName + "Number"); + assertRejected(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY", "customFields": [{}], + "managedFields": [{"name": "bogus", "type": "String"}]}} + """, noName + "null"); + } + + @Test + void customFieldTypeViolationsAreReportedBeforeAMissingName() { + assertRejected(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY", + "customFields": [{}, {"name": "attributes.x", "type": "Bogus"}]}} + """, "1 validation error detected: " + enumError("Bogus", + "thingIndexingConfiguration.customFields.2.member.type", "[Boolean, Number, String]")); + } + + @ParameterizedTest + @CsvSource({ + "thingName, String", + "registry.version, Number", + "connectivity.connected, Boolean" + }) + void aThingCustomFieldNamedAfterAManagedFieldIsRejected(String name, String type) { + assertRejected(("{'thingIndexingConfiguration': {'thingIndexingMode': 'REGISTRY_AND_SHADOW', " + + "'thingConnectivityIndexingMode': 'STATUS', " + + "'customFields': [{'name': '" + name + "', 'type': '" + type + "'}]}}").replace('\'', '"'), + "Defined field is a reserved field for MULTI_INDEXING_MODE. Field: " + name); + } + + @Test + void theFirstReservedCustomFieldNameIsReportedWhateverItsType() { + assertRejected(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY_AND_SHADOW", + "thingConnectivityIndexingMode": "STATUS", + "customFields": [{"name": "shadow.name.building.reported.x", "type": "String"}, + {"name": "thingName", "type": "Number"}, + {"name": "registry.version", "type": "String"}]}} + """, "Defined field is a reserved field for MULTI_INDEXING_MODE. Field: thingName"); + } + + @Test + void thingGroupManagedFieldsMustBeKnownWithTheirExpectedTypeInEitherMode() { + String prefix = "Only managed fields with expected types are allowed in " + + "thingGroupIndexingConfiguration.managedFields. Invalid field(s): "; + assertRejected(""" + {"thingGroupIndexingConfiguration": {"thingGroupIndexingMode": "ON", + "managedFields": [{"name": "bogus", "type": "String"}]}} + """, prefix + "[name:bogus, type:String]"); + assertRejected(""" + {"thingGroupIndexingConfiguration": {"thingGroupIndexingMode": "OFF", + "managedFields": [{"name": "bogus", "type": "String"}]}} + """, prefix + "[name:bogus, type:String]"); + assertRejected(""" + {"thingGroupIndexingConfiguration": {"thingGroupIndexingMode": "ON", + "managedFields": [{"name": "version", "type": "Number"}, {"name": "description", "type": "Number"}]}} + """, prefix + "[name:description, type:Number]"); + } + + @Test + void thingGroupFieldTypesAreValidated() { + assertRejected(""" + {"thingGroupIndexingConfiguration": {"thingGroupIndexingMode": "ON", + "managedFields": [{"name": "description", "type": "Bogus"}]}} + """, "1 validation error detected: " + enumError("Bogus", + "thingGroupIndexingConfiguration.managedFields.1.member.type", "[Boolean, Number, String]")); + assertRejected(""" + {"thingGroupIndexingConfiguration": {"thingGroupIndexingMode": "OFF", + "customFields": [{"name": "attributes.x", "type": "Bogus"}]}} + """, "1 validation error detected: " + enumError("Bogus", + "thingGroupIndexingConfiguration.customFields.1.member.type", "[Boolean, Number, String]")); + } + + @Test + void thingGroupFieldsAreAcceptedButNotStored() { + update(""" + {"thingGroupIndexingConfiguration": {"thingGroupIndexingMode": "ON", + "managedFields": [{"name": "parentGroupNames", "type": "String"}, {"name": "description", "type": "String"}, + {"name": "version", "type": "Number"}, {"name": "thingGroupName", "type": "String"}, + {"name": "thingGroupId", "type": "String"}], + "customFields": [{"name": "attributes.x", "type": "String"}, {}]}} + """); + + assertEquals(new IotIndexingConfiguration(ThingIndexing.OFF, "ON"), service.getIndexingConfiguration(REGION)); + } + + @Test + void rejectedThingGroupFieldsChangeNothing() { + update(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY"}, + "thingGroupIndexingConfiguration": {"thingGroupIndexingMode": "ON"}} + """); + IotIndexingConfiguration stored = service.getIndexingConfiguration(REGION); + + assertRejected(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "OFF"}, + "thingGroupIndexingConfiguration": {"thingGroupIndexingMode": "OFF", + "managedFields": [{"name": "bogus", "type": "String"}]}} + """, "Only managed fields with expected types are allowed in " + + "thingGroupIndexingConfiguration.managedFields. Invalid field(s): [name:bogus, type:String]"); + assertSerializationError(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "OFF"}, + "thingGroupIndexingConfiguration": {"thingGroupIndexingMode": "OFF", "customFields": {}}} + """); + + assertEquals(stored, service.getIndexingConfiguration(REGION)); + } + + @Test + void describeIndexSchemaFollowsTheModes() { + Map schemas = Map.of( + """ + {"thingIndexingMode": "REGISTRY"} + """, "REGISTRY", + """ + {"thingIndexingMode": "REGISTRY_AND_SHADOW"} + """, "REGISTRY_AND_SHADOW", + """ + {"thingIndexingMode": "REGISTRY", "thingConnectivityIndexingMode": "STATUS"} + """, "REGISTRY_AND_CONNECTIVITY_STATUS", + """ + {"thingIndexingMode": "REGISTRY_AND_SHADOW", "thingConnectivityIndexingMode": "STATUS"} + """, "REGISTRY_AND_SHADOW_AND_CONNECTIVITY_STATUS", + """ + {"thingIndexingMode": "REGISTRY", "deviceDefenderIndexingMode": "VIOLATIONS"} + """, "MULTI_INDEXING_MODE", + """ + {"thingIndexingMode": "REGISTRY_AND_SHADOW", "thingConnectivityIndexingMode": "STATUS", + "namedShadowIndexingMode": "ON", "filter": {"namedShadowNames": ["config"]}} + """, "MULTI_INDEXING_MODE"); + + schemas.forEach((thingConfiguration, schema) -> { + update("{\"thingIndexingConfiguration\": " + thingConfiguration + "}"); + assertEquals(schema, service.describeIndex("AWS_Things", REGION), thingConfiguration); + }); + + update(""" + {"thingGroupIndexingConfiguration": {"thingGroupIndexingMode": "ON"}} + """); + assertEquals("REGISTRY", service.describeIndex("AWS_ThingGroups", REGION)); + } + + @Test + void describeIndexOfADisabledIndexIsNotFound() { + assertDescribeFails("AWS_Things", "ResourceNotFoundException", 404, "Index AWS_Things does not exist"); + assertDescribeFails("AWS_ThingGroups", "ResourceNotFoundException", 404, + "Index AWS_ThingGroups does not exist"); + + update(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY"}, + "thingGroupIndexingConfiguration": {"thingGroupIndexingMode": "ON"}} + """); + update(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "OFF"}} + """); + assertDescribeFails("AWS_Things", "ResourceNotFoundException", 404, "Index AWS_Things does not exist"); + assertEquals("REGISTRY", service.describeIndex("AWS_ThingGroups", REGION)); + } + + @Test + void describeIndexOfAnUnknownNameIsInvalid() { + assertDescribeFails("Nope", "InvalidRequestException", 400, "Unrecognized indexName Nope"); + + update(""" + {"thingIndexingConfiguration": {"thingIndexingMode": "REGISTRY"}} + """); + assertDescribeFails("AWS_Thing", "InvalidRequestException", 400, "Unrecognized indexName AWS_Thing"); + } +}