mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(iot): support SetV2LoggingOptions and GetV2LoggingOptions (#8945)
* feat(iot): support SetV2LoggingOptions and GetV2LoggingOptions * fix(iot): keep omitted V2 logging options and require role and level on first set * fix(iot): validate SetV2LoggingOptions log level and member types * fix(iot): answer SetV2LoggingOptions with an empty body * test(iot): cover V2 logging setup, updates, errors and account scoping in the SDK suite * test(iot): sign V2 logging requests with the shared EnforcementFixtures.auth helper * test(iot): cover V2 logging persistence and document eventConfigurations and verbose limits * docs(iot): count the V2 logging routes in the service matrix * fix(iot): reject a SetV2LoggingOptions body that is not a JSON object * fix(iot): reject a blank roleArn in SetV2LoggingOptions * fix(iot): serialize the SetV2LoggingOptions read-merge-write * test(iot): share one IoT client builder in the SDK fixtures * fix(iot): stop refusing a blank roleArn in SetV2LoggingOptions * fix(iot): build the V2 log level refusal with SmithyValidationErrors * refactor(iot): share IoT request body readers between logging and fleet indexing
This commit is contained in:
1 parent
b8632351e8
commit
9f959a3f27
13 files changed
+589
-27
No files matched your search
@@ -1731,10 +1731,18 @@ public final class TestFixtures {
|
||||
}
|
||||
|
||||
public static IotClient iotClient() {
|
||||
return iotClient(CREDENTIALS);
|
||||
}
|
||||
|
||||
public static IotClient iotClient(String accountId) {
|
||||
return iotClient(StaticCredentialsProvider.create(AwsBasicCredentials.create(accountId, "test")));
|
||||
}
|
||||
|
||||
private static IotClient iotClient(StaticCredentialsProvider credentials) {
|
||||
return IotClient.builder()
|
||||
.endpointOverride(ENDPOINT)
|
||||
.region(REGION)
|
||||
.credentialsProvider(CREDENTIALS)
|
||||
.credentialsProvider(credentials)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
@@ -63,6 +63,8 @@ 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.GetV2LoggingOptionsRequest;
|
||||
import software.amazon.awssdk.services.iot.model.GetV2LoggingOptionsResponse;
|
||||
import software.amazon.awssdk.services.iot.model.IndexNotReadyException;
|
||||
import software.amazon.awssdk.services.iot.model.IndexStatus;
|
||||
import software.amazon.awssdk.services.iot.model.InvalidRequestException;
|
||||
@@ -85,7 +87,10 @@ 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.LogEventConfiguration;
|
||||
import software.amazon.awssdk.services.iot.model.LogLevel;
|
||||
import software.amazon.awssdk.services.iot.model.NamedShadowIndexingMode;
|
||||
import software.amazon.awssdk.services.iot.model.NotConfiguredException;
|
||||
import software.amazon.awssdk.services.iot.model.RemoveThingFromThingGroupRequest;
|
||||
import software.amazon.awssdk.services.iot.model.ResourceAlreadyExistsException;
|
||||
import software.amazon.awssdk.services.iot.model.ResourceNotFoundException;
|
||||
@@ -93,6 +98,7 @@ import software.amazon.awssdk.services.iot.model.SearchIndexRequest;
|
||||
import software.amazon.awssdk.services.iot.model.SearchIndexResponse;
|
||||
import software.amazon.awssdk.services.iot.model.ServerCertificateStatus;
|
||||
import software.amazon.awssdk.services.iot.model.ServiceType;
|
||||
import software.amazon.awssdk.services.iot.model.SetV2LoggingOptionsRequest;
|
||||
import software.amazon.awssdk.services.iot.model.SqsAction;
|
||||
import software.amazon.awssdk.services.iot.model.Tag;
|
||||
import software.amazon.awssdk.services.iot.model.TagResourceRequest;
|
||||
@@ -155,6 +161,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
@@ -288,6 +295,50 @@ class IotTest {
|
||||
.isInstanceOf(ResourceNotFoundException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2LoggingOptions() {
|
||||
String account = String.valueOf(ThreadLocalRandom.current().nextLong(100_000_000_000L, 1_000_000_000_000L));
|
||||
String roleArn = TestFixtures.globalArn("iam", account, "role/iot-logging");
|
||||
try (IotClient accountIot = TestFixtures.iotClient(account)) {
|
||||
assertThatThrownBy(() -> accountIot.getV2LoggingOptions(GetV2LoggingOptionsRequest.builder().build()))
|
||||
.isInstanceOf(NotConfiguredException.class);
|
||||
assertThatThrownBy(() -> accountIot.setV2LoggingOptions(SetV2LoggingOptionsRequest.builder()
|
||||
.defaultLogLevel(LogLevel.ERROR).build()))
|
||||
.isInstanceOf(InvalidRequestException.class);
|
||||
|
||||
accountIot.setV2LoggingOptions(SetV2LoggingOptionsRequest.builder()
|
||||
.roleArn(roleArn)
|
||||
.defaultLogLevel(LogLevel.WARN)
|
||||
.eventConfigurations(LogEventConfiguration.builder()
|
||||
.eventType("Connect").logLevel(LogLevel.DEBUG).build())
|
||||
.build());
|
||||
accountIot.setV2LoggingOptions(SetV2LoggingOptionsRequest.builder().disableAllLogs(true).build());
|
||||
assertThatThrownBy(() -> accountIot.setV2LoggingOptions(SetV2LoggingOptionsRequest.builder()
|
||||
.defaultLogLevel("TRACE").build()))
|
||||
.isInstanceOf(InvalidRequestException.class);
|
||||
|
||||
GetV2LoggingOptionsResponse options =
|
||||
accountIot.getV2LoggingOptions(GetV2LoggingOptionsRequest.builder().verbose(true).build());
|
||||
assertThat(options.roleArn()).isEqualTo(roleArn);
|
||||
assertThat(options.defaultLogLevel()).isEqualTo(LogLevel.WARN);
|
||||
assertThat(options.disableAllLogs()).isTrue();
|
||||
assertThat(options.hasEventConfigurations()).isFalse();
|
||||
|
||||
for (LogLevel level : List.of(LogLevel.DEBUG, LogLevel.INFO, LogLevel.ERROR, LogLevel.WARN,
|
||||
LogLevel.DISABLED)) {
|
||||
accountIot.setV2LoggingOptions(SetV2LoggingOptionsRequest.builder().defaultLogLevel(level).build());
|
||||
assertThat(accountIot.getV2LoggingOptions(GetV2LoggingOptionsRequest.builder().build())
|
||||
.defaultLogLevel()).isEqualTo(level);
|
||||
}
|
||||
}
|
||||
|
||||
String otherAccount = String.valueOf(Long.parseLong(account) + 1);
|
||||
try (IotClient otherIot = TestFixtures.iotClient(otherAccount)) {
|
||||
assertThatThrownBy(() -> otherIot.getV2LoggingOptions(GetV2LoggingOptionsRequest.builder().build()))
|
||||
.isInstanceOf(NotConfiguredException.class);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void fleetIndexingConfiguration() {
|
||||
GetIndexingConfigurationResponse prior =
|
||||
|
||||
@@ -154,7 +154,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 | 83 |
|
||||
| [IoT Core](iot.md) | `/things/...`, `/endpoint`, rules/policies REST paths | REST JSON | 85 |
|
||||
| [IoT Data](iot.md#iot-data) | `/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.
|
||||
|
||||
@@ -174,6 +174,20 @@ Current limitations:
|
||||
- `connectivity.sessionExpiry` and the socket information of `filter.connectivity.includeSocketInformation` are not reported.
|
||||
- `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.
|
||||
|
||||
## Logging Options
|
||||
|
||||
Status: control plane only.
|
||||
|
||||
`SetV2LoggingOptions` and `GetV2LoggingOptions` (`/v2LoggingOptions`) are served on the REST-JSON path the AWS SDKs use:
|
||||
|
||||
- There is one configuration per account and region. `GetV2LoggingOptions` before the first `SetV2LoggingOptions` fails with `NotConfiguredException` (404).
|
||||
- The first `SetV2LoggingOptions` must carry `roleArn` and `defaultLogLevel`, or it fails with `InvalidRequestException`; `disableAllLogs` defaults to `false`.
|
||||
- A later `SetV2LoggingOptions` changes only the options it carries and keeps the others.
|
||||
- A `defaultLogLevel` other than `DEBUG`, `INFO`, `ERROR`, `WARN` or `DISABLED` fails with `InvalidRequestException`, and a member of the wrong JSON type with `SerializationException`; neither changes the stored options.
|
||||
- `eventConfigurations` (the per-event-type `eventType`, `logLevel` and `logDestination` overrides) is not modeled: `SetV2LoggingOptions` accepts and drops it, and `GetV2LoggingOptions` reports none.
|
||||
- The `verbose` query parameter of `GetV2LoggingOptions` is accepted and ignored: Floci has no catalog of event types to list.
|
||||
- The options are stored only: Floci writes no IoT logs to CloudWatch Logs.
|
||||
|
||||
## IoT Data API {#iot-data}
|
||||
|
||||
The IoT Data plane answers on the same endpoint as the control plane. The notes under
|
||||
|
||||
@@ -55,6 +55,7 @@ import io.floci.aws.services.iot.IotController;
|
||||
import io.floci.aws.services.iot.IotDataController;
|
||||
import io.floci.aws.services.iot.IotDomainConfigurationController;
|
||||
import io.floci.aws.services.iot.IotFleetIndexingController;
|
||||
import io.floci.aws.services.iot.IotLoggingController;
|
||||
import io.floci.aws.services.lakeformation.LakeFormationController;
|
||||
import io.floci.aws.services.lambda.LambdaController;
|
||||
import io.floci.aws.services.lambda.durable.DurableExecutionController;
|
||||
@@ -755,7 +756,8 @@ 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, IotFleetIndexingController.class)),
|
||||
IotAuthorizerController.class, IotFleetIndexingController.class,
|
||||
IotLoggingController.class)),
|
||||
descriptor("iotdata", "iotdata", config.services().iotdata().enabled(), true,
|
||||
"iot", config.storage().mode(), 5000L, null, ServiceProtocol.REST_JSON,
|
||||
protocols(ServiceProtocol.REST_JSON),
|
||||
|
||||
@@ -26,7 +26,6 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
@@ -36,6 +35,10 @@ import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static io.floci.aws.services.iot.IotRequestBody.requireObject;
|
||||
import static io.floci.aws.services.iot.IotRequestBody.serializationError;
|
||||
import static io.floci.aws.services.iot.IotRequestBody.text;
|
||||
|
||||
/**
|
||||
* AWS IoT fleet indexing: UpdateIndexingConfiguration, GetIndexingConfiguration, DescribeIndex and
|
||||
* a bounded SearchIndex. One configuration per account and region; a region never updated is OFF.
|
||||
@@ -117,9 +120,7 @@ public class IotFleetIndexingService {
|
||||
}
|
||||
|
||||
public void updateIndexingConfiguration(JsonNode request, String region) {
|
||||
if (!request.isObject()) {
|
||||
throw serializationError("request body", JsonNodeType.OBJECT);
|
||||
}
|
||||
requireObject(request);
|
||||
String groupPrefix = "thingGroupIndexingConfiguration";
|
||||
JsonNode thingNode = object(request.path("thingIndexingConfiguration"), "thingIndexingConfiguration");
|
||||
JsonNode groupNode = object(request.path(groupPrefix), groupPrefix);
|
||||
@@ -172,9 +173,7 @@ public class IotFleetIndexingService {
|
||||
* whatever was deleted before it. It also carries a checksum, so an edited token is invalid.
|
||||
*/
|
||||
public IotService.Page<ObjectNode> searchIndex(JsonNode request, String accountId, String region) {
|
||||
if (!request.isObject()) {
|
||||
throw serializationError("request body", JsonNodeType.OBJECT);
|
||||
}
|
||||
requireObject(request);
|
||||
String queryString = text(request.path("queryString"), "queryString");
|
||||
JsonNode maxResults = request.path("maxResults");
|
||||
if (present(maxResults) && !maxResults.isNumber()) {
|
||||
@@ -525,27 +524,10 @@ public class IotFleetIndexingService {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package io.floci.aws.services.iot;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.services.iot.model.IotV2LoggingOptions;
|
||||
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.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;
|
||||
|
||||
@Path("/v2LoggingOptions")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public class IotLoggingController {
|
||||
|
||||
private final IotLoggingService loggingService;
|
||||
private final RegionResolver regionResolver;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@Inject
|
||||
public IotLoggingController(IotLoggingService loggingService, RegionResolver regionResolver,
|
||||
ObjectMapper objectMapper) {
|
||||
this.loggingService = loggingService;
|
||||
this.regionResolver = regionResolver;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
@POST
|
||||
public Response setV2LoggingOptions(@Context HttpHeaders headers, String body) {
|
||||
loggingService.setV2LoggingOptions(IotRequestBody.read(objectMapper, body),
|
||||
regionResolver.resolveRegion(headers));
|
||||
return Response.ok().build();
|
||||
}
|
||||
|
||||
@GET
|
||||
public Response getV2LoggingOptions(@Context HttpHeaders headers) {
|
||||
IotV2LoggingOptions options = loggingService.getV2LoggingOptions(regionResolver.resolveRegion(headers));
|
||||
return Response.ok(objectMapper.createObjectNode()
|
||||
.put("roleArn", options.roleArn())
|
||||
.put("defaultLogLevel", options.defaultLogLevel())
|
||||
.put("disableAllLogs", options.disableAllLogs())).build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package io.floci.aws.services.iot;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeType;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.SmithyValidationErrors;
|
||||
import io.floci.aws.core.storage.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.iot.model.IotV2LoggingOptions;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static io.floci.aws.services.iot.IotRequestBody.requireObject;
|
||||
import static io.floci.aws.services.iot.IotRequestBody.serializationError;
|
||||
import static io.floci.aws.services.iot.IotRequestBody.text;
|
||||
|
||||
@ApplicationScoped
|
||||
public class IotLoggingService {
|
||||
|
||||
private static final List<String> LOG_LEVELS = List.of("DEBUG", "INFO", "ERROR", "WARN", "DISABLED");
|
||||
|
||||
private final StorageBackend<String, IotV2LoggingOptions> store;
|
||||
private final Object lock = new Object();
|
||||
|
||||
@Inject
|
||||
public IotLoggingService(StorageFactory storageFactory) {
|
||||
this(storageFactory.create("iot", "iot-v2-logging-options.json",
|
||||
new TypeReference<Map<String, IotV2LoggingOptions>>() {}));
|
||||
}
|
||||
|
||||
IotLoggingService(StorageBackend<String, IotV2LoggingOptions> store) {
|
||||
this.store = store;
|
||||
}
|
||||
|
||||
public void setV2LoggingOptions(JsonNode request, String region) {
|
||||
requireObject(request);
|
||||
String roleArn = text(request.path("roleArn"), "roleArn");
|
||||
String defaultLogLevel = text(request.path("defaultLogLevel"), "defaultLogLevel");
|
||||
Boolean disableAllLogs = bool(request.path("disableAllLogs"), "disableAllLogs");
|
||||
if (defaultLogLevel != null && !LOG_LEVELS.contains(defaultLogLevel)) {
|
||||
throw SmithyValidationErrors.exception("InvalidRequestException",
|
||||
SmithyValidationErrors.notInEnum(defaultLogLevel, "defaultLogLevel", LOG_LEVELS));
|
||||
}
|
||||
synchronized (lock) {
|
||||
Optional<IotV2LoggingOptions> current = store.get(region);
|
||||
if (current.isEmpty() && (roleArn == null || defaultLogLevel == null)) {
|
||||
throw new AwsException("InvalidRequestException",
|
||||
"roleArn and defaultLogLevel are required for the initial setup.", 400);
|
||||
}
|
||||
IotV2LoggingOptions prior = current.orElse(new IotV2LoggingOptions(null, null, false));
|
||||
store.put(region, new IotV2LoggingOptions(roleArn != null ? roleArn : prior.roleArn(),
|
||||
defaultLogLevel != null ? defaultLogLevel : prior.defaultLogLevel(),
|
||||
disableAllLogs != null ? disableAllLogs : prior.disableAllLogs()));
|
||||
}
|
||||
}
|
||||
|
||||
public IotV2LoggingOptions getV2LoggingOptions(String region) {
|
||||
return store.get(region).orElseThrow(() ->
|
||||
new AwsException("NotConfiguredException", "V2 logging options are not configured.", 404));
|
||||
}
|
||||
|
||||
private static Boolean bool(JsonNode node, String member) {
|
||||
if (node.isMissingNode() || node.isNull()) {
|
||||
return null;
|
||||
}
|
||||
if (!node.isBoolean()) {
|
||||
throw serializationError(member, JsonNodeType.BOOLEAN);
|
||||
}
|
||||
return node.booleanValue();
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,11 @@ 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.node.JsonNodeType;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Reads an AWS IoT REST-JSON request body. AWS rejects a malformed body, and content after the
|
||||
* JSON value, with {@code SerializationException}.
|
||||
@@ -23,4 +26,27 @@ final class IotRequestBody {
|
||||
throw new AwsException("SerializationException", e.getOriginalMessage(), 400);
|
||||
}
|
||||
}
|
||||
|
||||
static void requireObject(JsonNode request) {
|
||||
if (!request.isObject()) {
|
||||
throw serializationError("request body", JsonNodeType.OBJECT);
|
||||
}
|
||||
}
|
||||
|
||||
/** A string member, null when it is absent or null. */
|
||||
static String text(JsonNode node, String path) {
|
||||
if (node.isMissingNode() || node.isNull()) {
|
||||
return null;
|
||||
}
|
||||
if (!node.isTextual()) {
|
||||
throw serializationError(path, JsonNodeType.STRING);
|
||||
}
|
||||
return node.textValue();
|
||||
}
|
||||
|
||||
/** A member of the wrong JSON type fails before any validation, as AWS fails to deserialize it. */
|
||||
static AwsException serializationError(String path, JsonNodeType expected) {
|
||||
return new AwsException("SerializationException",
|
||||
path + " must be a JSON " + expected.name().toLowerCase(Locale.ROOT), 400);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package io.floci.aws.services.iot.model;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
public record IotV2LoggingOptions(String roleArn, String defaultLogLevel, boolean disableAllLogs) {
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
package io.floci.aws.services.iot;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.floci.aws.testing.EnforcementFixtures.auth;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.aMapWithSize;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.emptyString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
@QuarkusTest
|
||||
class IotLoggingIntegrationTest {
|
||||
|
||||
@Test
|
||||
void getBeforeAnySetIsNotConfigured() {
|
||||
given()
|
||||
.header("Authorization", auth("111100000101", "iot"))
|
||||
.when()
|
||||
.get("/v2LoggingOptions")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.header("x-amzn-ErrorType", equalTo("NotConfiguredException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void setThenGetRoundTripsTheOptions() {
|
||||
String auth = auth("111100000102", "iot");
|
||||
String roleArn = "arn:aws:iam::111100000102:role/iot-logging";
|
||||
given()
|
||||
.header("Authorization", auth)
|
||||
.contentType("application/json")
|
||||
.body("""
|
||||
{"roleArn": "%s", "defaultLogLevel": "INFO", "disableAllLogs": true}
|
||||
""".formatted(roleArn))
|
||||
.when()
|
||||
.post("/v2LoggingOptions")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(emptyString());
|
||||
|
||||
given()
|
||||
.header("Authorization", auth)
|
||||
.when()
|
||||
.get("/v2LoggingOptions")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("$", aMapWithSize(3))
|
||||
.body("roleArn", equalTo(roleArn))
|
||||
.body("defaultLogLevel", equalTo("INFO"))
|
||||
.body("disableAllLogs", equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void initialSetupWithoutARoleIsRejected() {
|
||||
String auth = auth("111100000103", "iot");
|
||||
given()
|
||||
.header("Authorization", auth)
|
||||
.contentType("application/json")
|
||||
.body("""
|
||||
{"defaultLogLevel": "ERROR"}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2LoggingOptions")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.header("x-amzn-ErrorType", equalTo("InvalidRequestException"));
|
||||
|
||||
given()
|
||||
.header("Authorization", auth)
|
||||
.when()
|
||||
.get("/v2LoggingOptions")
|
||||
.then()
|
||||
.statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnknownLogLevelIsRejected() {
|
||||
given()
|
||||
.header("Authorization", auth("111100000107", "iot"))
|
||||
.contentType("application/json")
|
||||
.body("""
|
||||
{"roleArn": "arn:aws:iam::111100000107:role/iot-logging", "defaultLogLevel": "TRACE"}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2LoggingOptions")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.header("x-amzn-ErrorType", equalTo("InvalidRequestException"))
|
||||
.body("message", containsString("defaultLogLevel"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMemberOfTheWrongTypeIsASerializationError() {
|
||||
given()
|
||||
.header("Authorization", auth("111100000108", "iot"))
|
||||
.contentType("application/json")
|
||||
.body("""
|
||||
{"roleArn": "arn:aws:iam::111100000108:role/iot-logging", "defaultLogLevel": "INFO",
|
||||
"disableAllLogs": "true"}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2LoggingOptions")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.header("x-amzn-ErrorType", equalTo("SerializationException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void laterSetsKeepTheOptionsTheyOmit() {
|
||||
String auth = auth("111100000104", "iot");
|
||||
String roleArn = "arn:aws:iam::111100000104:role/iot-logging";
|
||||
given()
|
||||
.header("Authorization", auth)
|
||||
.contentType("application/json")
|
||||
.body("""
|
||||
{"roleArn": "%s", "defaultLogLevel": "DEBUG"}
|
||||
""".formatted(roleArn))
|
||||
.when()
|
||||
.post("/v2LoggingOptions")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("Authorization", auth)
|
||||
.contentType("application/json")
|
||||
.body("""
|
||||
{"disableAllLogs": true}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2LoggingOptions")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("Authorization", auth)
|
||||
.when()
|
||||
.get("/v2LoggingOptions")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("roleArn", equalTo(roleArn))
|
||||
.body("defaultLogLevel", equalTo("DEBUG"))
|
||||
.body("disableAllLogs", equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void eachAccountAndRegionKeepsItsOwnOptions() {
|
||||
given()
|
||||
.header("Authorization", auth("111100000105", "iot"))
|
||||
.contentType("application/json")
|
||||
.body("""
|
||||
{"roleArn": "arn:aws:iam::111100000105:role/iot-logging", "defaultLogLevel": "INFO"}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2LoggingOptions")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("Authorization", auth("111100000106", "iot"))
|
||||
.when()
|
||||
.get("/v2LoggingOptions")
|
||||
.then()
|
||||
.statusCode(404);
|
||||
|
||||
given()
|
||||
.header("Authorization", auth("111100000105", "eu-west-1", "iot"))
|
||||
.when()
|
||||
.get("/v2LoggingOptions")
|
||||
.then()
|
||||
.statusCode(404);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
package io.floci.aws.services.iot;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.storage.InMemoryStorage;
|
||||
import io.floci.aws.core.storage.PersistentStorage;
|
||||
import io.floci.aws.services.iot.model.IotV2LoggingOptions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class IotLoggingServiceTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ROLE_ARN = "arn:aws:iam::123456789012:role/iot-logging";
|
||||
private static final String OTHER_ROLE_ARN = "arn:aws:iam::123456789012:role/iot-logging-2";
|
||||
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private final IotLoggingService service = new IotLoggingService(new InMemoryStorage<>());
|
||||
|
||||
private void set(String body, String region) throws Exception {
|
||||
service.setV2LoggingOptions(objectMapper.readTree(body), region);
|
||||
}
|
||||
|
||||
private String failure(String body) {
|
||||
return assertThrows(AwsException.class, () -> set(body, REGION)).getErrorCode();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getBeforeAnySetIsNotConfigured() {
|
||||
AwsException failure = assertThrows(AwsException.class, () -> service.getV2LoggingOptions(REGION));
|
||||
assertEquals("NotConfiguredException", failure.getErrorCode());
|
||||
assertEquals(404, failure.getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void setThenGetReturnsTheOptions() throws Exception {
|
||||
set("""
|
||||
{"roleArn": "%s", "defaultLogLevel": "INFO", "disableAllLogs": true}
|
||||
""".formatted(ROLE_ARN), REGION);
|
||||
|
||||
assertEquals(new IotV2LoggingOptions(ROLE_ARN, "INFO", true), service.getV2LoggingOptions(REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void initialSetupDefaultsDisableAllLogsToFalse() throws Exception {
|
||||
set("""
|
||||
{"roleArn": "%s", "defaultLogLevel": "WARN"}
|
||||
""".formatted(ROLE_ARN), REGION);
|
||||
|
||||
assertEquals(new IotV2LoggingOptions(ROLE_ARN, "WARN", false), service.getV2LoggingOptions(REGION));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"{\"defaultLogLevel\": \"ERROR\"}",
|
||||
"{\"roleArn\": \"arn:aws:iam::123456789012:role/iot-logging\"}",
|
||||
"{\"disableAllLogs\": true}",
|
||||
"{}"
|
||||
})
|
||||
void initialSetupRequiresRoleArnAndDefaultLogLevel(String body) {
|
||||
assertEquals("InvalidRequestException", failure(body));
|
||||
assertEquals("NotConfiguredException",
|
||||
assertThrows(AwsException.class, () -> service.getV2LoggingOptions(REGION)).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void laterSetsKeepTheOptionsTheyOmit() throws Exception {
|
||||
set("""
|
||||
{"roleArn": "%s", "defaultLogLevel": "INFO"}
|
||||
""".formatted(ROLE_ARN), REGION);
|
||||
|
||||
set("""
|
||||
{"disableAllLogs": true}
|
||||
""", REGION);
|
||||
assertEquals(new IotV2LoggingOptions(ROLE_ARN, "INFO", true), service.getV2LoggingOptions(REGION));
|
||||
|
||||
set("""
|
||||
{"defaultLogLevel": "ERROR"}
|
||||
""", REGION);
|
||||
assertEquals(new IotV2LoggingOptions(ROLE_ARN, "ERROR", true), service.getV2LoggingOptions(REGION));
|
||||
|
||||
set("""
|
||||
{"roleArn": "%s", "disableAllLogs": false}
|
||||
""".formatted(OTHER_ROLE_ARN), REGION);
|
||||
assertEquals(new IotV2LoggingOptions(OTHER_ROLE_ARN, "ERROR", false), service.getV2LoggingOptions(REGION));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"DEBUG", "INFO", "ERROR", "WARN", "DISABLED"})
|
||||
void acceptsEveryLogLevel(String level) throws Exception {
|
||||
set("""
|
||||
{"roleArn": "%s", "defaultLogLevel": "%s"}
|
||||
""".formatted(ROLE_ARN, level), REGION);
|
||||
|
||||
assertEquals(level, service.getV2LoggingOptions(REGION).defaultLogLevel());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"TRACE", "info", "", " "})
|
||||
void rejectsAnUnknownLogLevelAndKeepsTheStoredOptions(String level) throws Exception {
|
||||
set("""
|
||||
{"roleArn": "%s", "defaultLogLevel": "INFO"}
|
||||
""".formatted(ROLE_ARN), REGION);
|
||||
|
||||
assertEquals("InvalidRequestException", failure("""
|
||||
{"defaultLogLevel": "%s", "disableAllLogs": true}
|
||||
""".formatted(level)));
|
||||
assertEquals(new IotV2LoggingOptions(ROLE_ARN, "INFO", false), service.getV2LoggingOptions(REGION));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"{\"roleArn\": 42}",
|
||||
"{\"defaultLogLevel\": []}",
|
||||
"{\"disableAllLogs\": \"true\"}"
|
||||
})
|
||||
void rejectsAMemberOfTheWrongTypeAndKeepsTheStoredOptions(String body) throws Exception {
|
||||
set("""
|
||||
{"roleArn": "%s", "defaultLogLevel": "INFO"}
|
||||
""".formatted(ROLE_ARN), REGION);
|
||||
|
||||
assertEquals("SerializationException", failure(body));
|
||||
assertEquals(new IotV2LoggingOptions(ROLE_ARN, "INFO", false), service.getV2LoggingOptions(REGION));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"[]", "[{\"roleArn\": \"x\", \"defaultLogLevel\": \"INFO\"}]", "null", "42", "\"INFO\""})
|
||||
void rejectsABodyThatIsNotAJsonObject(String body) {
|
||||
assertEquals("SerializationException", failure(body));
|
||||
assertEquals("NotConfiguredException",
|
||||
assertThrows(AwsException.class, () -> service.getV2LoggingOptions(REGION)).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void eachRegionKeepsItsOwnOptions() throws Exception {
|
||||
set("""
|
||||
{"roleArn": "%s", "defaultLogLevel": "WARN"}
|
||||
""".formatted(ROLE_ARN), REGION);
|
||||
|
||||
assertEquals("NotConfiguredException",
|
||||
assertThrows(AwsException.class, () -> service.getV2LoggingOptions("eu-west-1")).getErrorCode());
|
||||
assertEquals("InvalidRequestException", assertThrows(AwsException.class, () -> set("""
|
||||
{"defaultLogLevel": "ERROR"}
|
||||
""", "eu-west-1")).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void optionsSurviveAReloadOfPersistentStorage(@TempDir Path tempDir) throws Exception {
|
||||
Path file = tempDir.resolve("iot-v2-logging-options.json");
|
||||
TypeReference<Map<String, IotV2LoggingOptions>> type = new TypeReference<>() {};
|
||||
new IotLoggingService(new PersistentStorage<>(file, type)).setV2LoggingOptions(objectMapper.readTree("""
|
||||
{"roleArn": "%s", "defaultLogLevel": "DEBUG", "disableAllLogs": true}
|
||||
""".formatted(ROLE_ARN)), REGION);
|
||||
|
||||
PersistentStorage<String, IotV2LoggingOptions> reloaded = new PersistentStorage<>(file, type);
|
||||
reloaded.load();
|
||||
IotLoggingService reader = new IotLoggingService(reloaded);
|
||||
|
||||
assertEquals(new IotV2LoggingOptions(ROLE_ARN, "DEBUG", true), reader.getV2LoggingOptions(REGION));
|
||||
assertEquals("NotConfiguredException",
|
||||
assertThrows(AwsException.class, () -> reader.getV2LoggingOptions("eu-west-1")).getErrorCode());
|
||||
}
|
||||
}
|
||||
@@ -266,6 +266,7 @@ action_count_sources:
|
||||
- { path: services/iot/IotAuthorizerController.java, mode: rest }
|
||||
- { path: services/iot/IotDomainConfigurationController.java, mode: rest }
|
||||
- { path: services/iot/IotFleetIndexingController.java, mode: rest }
|
||||
- { path: services/iot/IotLoggingController.java, mode: rest }
|
||||
- { path: services/iot/IotTagHandler.java, mode: tag_handler }
|
||||
rename_actions:
|
||||
RegisterCertificateWithoutCa: RegisterCertificateWithoutCA
|
||||
|
||||
Reference in new issue
Block a user