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:
rajp33 authored and GitHub committed 2026-11-19 19:14:50 -04:00
1 parent b8632351e8
commit 9f959a3f27
13 files changed
+589 -27

No files matched your search

@@ -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());
}
}