mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(cognito): implement log delivery configuration (#2867)
* feat(cognito): implement log delivery configuration SetLogDeliveryConfiguration and GetLogDeliveryConfiguration were unsupported, so a config declaring `aws_cognito_log_delivery_configuration` could not be applied against Floci. The configuration is stored on the user pool and validated the way the live API validates it: - LogLevel accepts ERROR or INFO, EventSource accepts userNotification or userAuthEvents, both reported with AWS's 1-based member path - every entry must name a CloudWatch, Firehose or S3 destination - Set replaces the list rather than merging, so an empty list clears it - Get always returns LogConfigurations, as [] when nothing is configured Floci stores the configuration and delivers nothing: the destination is neither written to nor required to exist, and the pricing-tier gate AWS applies to userAuthEvents is not reproduced. Closes #2855 * fix(cognito): reject a null LogConfigurations instead of clearing the configuration SetLogDeliveryConfiguration treated an omitted, null or wrongly typed LogConfigurations as an empty replacement, so a malformed request returned 200 and erased the pool's stored configuration. AWS rejects all three. An omitted or null member fails validation, and a value of the wrong JSON type fails deserialization: omitted / null -> InvalidParameterException: 1 validation error detected: Value null at 'logConfigurations' failed to satisfy constraint: Member must not be null wrong type -> SerializationException: Expected list or null Also drops two em-dashes from the docs, per AGENTS.md. * fix(cognito): translate malformed LogConfigurations elements the way AWS does A scalar inside the array reached Jackson's convertValue directly, so an IllegalArgumentException escaped as a 500 InternalFailure instead of a client error, and a null element would have failed on the validation loop. AWS answers both without a 500: [1, 2] / ["a string"] -> SerializationException: Expected null [null] -> 200, the null element is dropped Elements are now checked before conversion: nulls are skipped and non-objects raise SerializationException. * fix(cognito): bound LogConfigurations and reject a repeated event source SetLogDeliveryConfiguration validated each element but never checked the list itself, so a request with three or more well-formed configurations, or two naming the same event source, was accepted and stored where AWS rejects it. Measured against Cognito in ap-southeast-1: 3 well-formed configs InvalidParameterException, length constraint on 'logConfigurations', max 2 2 duplicate sources InvalidParameterException, "appear more then once" 3 configs, one bad enum 2 validation errors, the length one reported first unknown pool, bad enum the validation error, not ResourceNotFoundException 0 configs 200 OK The shape checks now run before the pool lookup, matching that last row, and collect every violation into one message instead of throwing on the first. The duplicate and missing-destination complaints share one message with the missing-destination clause first, and each event source is named once however many configurations carry it, as measured. "more then once" is the service's own spelling, kept so the message matches. The length message embeds the request model's toString, which the emulator now reproduces for all three destination shapes and for an absent destination.
This commit is contained in:
1 parent
7d693de663
commit
b8f1e22c74
6 files changed
+816
No files matched your search
+450
@@ -0,0 +1,450 @@
|
||||
package io.github.hectorvent.floci.services.cognito;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoAction;
|
||||
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoJson;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Covers SetLogDeliveryConfiguration and GetLogDeliveryConfiguration.
|
||||
*
|
||||
* <p>The enum sets, the missing-destination rejection and the 1-based validation path were
|
||||
* measured against the live Cognito API.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class CognitoLogDeliveryConfigurationIntegrationTest {
|
||||
|
||||
private static String poolId;
|
||||
|
||||
private static final String LOG_GROUP_ARN =
|
||||
"arn:aws:logs:us-east-1:000000000000:log-group:cognito-test-logs";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createPool() throws Exception {
|
||||
JsonNode response = cognitoJson("CreateUserPool", """
|
||||
{
|
||||
"PoolName": "LogDeliveryTestPool"
|
||||
}
|
||||
""");
|
||||
poolId = response.path("UserPool").path("Id").asText();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void getReturnsAnEmptyListBeforeAnythingIsConfigured() throws Exception {
|
||||
JsonNode config = cognitoJson("GetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "%s"
|
||||
}
|
||||
""".formatted(poolId)).path("LogDeliveryConfiguration");
|
||||
|
||||
assertEquals(poolId, config.path("UserPoolId").asText());
|
||||
assertTrue(config.path("LogConfigurations").isArray());
|
||||
assertEquals(0, config.path("LogConfigurations").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void setStoresAndEchoesTheConfiguration() throws Exception {
|
||||
JsonNode config = cognitoJson("SetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"LogConfigurations": [
|
||||
{
|
||||
"LogLevel": "ERROR",
|
||||
"EventSource": "userNotification",
|
||||
"CloudWatchLogsConfiguration": {"LogGroupArn": "%s"}
|
||||
}
|
||||
]
|
||||
}
|
||||
""".formatted(poolId, LOG_GROUP_ARN)).path("LogDeliveryConfiguration");
|
||||
|
||||
assertEquals(1, config.path("LogConfigurations").size());
|
||||
JsonNode entry = config.path("LogConfigurations").get(0);
|
||||
assertEquals("ERROR", entry.path("LogLevel").asText());
|
||||
assertEquals("userNotification", entry.path("EventSource").asText());
|
||||
assertEquals(LOG_GROUP_ARN,
|
||||
entry.path("CloudWatchLogsConfiguration").path("LogGroupArn").asText());
|
||||
|
||||
JsonNode readBack = cognitoJson("GetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "%s"
|
||||
}
|
||||
""".formatted(poolId)).path("LogDeliveryConfiguration");
|
||||
assertEquals(config.path("LogConfigurations"), readBack.path("LogConfigurations"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void setRejectsAnUnknownLogLevel() {
|
||||
cognitoAction("SetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"LogConfigurations": [
|
||||
{
|
||||
"LogLevel": "TRACE",
|
||||
"EventSource": "userNotification",
|
||||
"CloudWatchLogsConfiguration": {"LogGroupArn": "%s"}
|
||||
}
|
||||
]
|
||||
}
|
||||
""".formatted(poolId, LOG_GROUP_ARN))
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterException"))
|
||||
.body("message", equalTo(
|
||||
"1 validation error detected: Value 'TRACE' at "
|
||||
+ "'logConfigurations.1.member.logLevel' failed to satisfy constraint: "
|
||||
+ "Member must satisfy enum value set: [ERROR, INFO]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void setRejectsAnUnknownEventSource() {
|
||||
cognitoAction("SetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"LogConfigurations": [
|
||||
{
|
||||
"LogLevel": "ERROR",
|
||||
"EventSource": "notAThing",
|
||||
"CloudWatchLogsConfiguration": {"LogGroupArn": "%s"}
|
||||
}
|
||||
]
|
||||
}
|
||||
""".formatted(poolId, LOG_GROUP_ARN))
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterException"))
|
||||
.body("message", equalTo(
|
||||
"1 validation error detected: Value 'notAThing' at "
|
||||
+ "'logConfigurations.1.member.eventSource' failed to satisfy constraint: "
|
||||
+ "Member must satisfy enum value set: [userAuthEvents, userNotification]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void setRejectsAConfigurationWithNoDestination() {
|
||||
cognitoAction("SetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"LogConfigurations": [
|
||||
{
|
||||
"LogLevel": "ERROR",
|
||||
"EventSource": "userNotification"
|
||||
}
|
||||
]
|
||||
}
|
||||
""".formatted(poolId))
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterException"))
|
||||
.body("message", equalTo(
|
||||
"Request validation Failed. Following event sources in request have no "
|
||||
+ "destination: [userNotification]."));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void setReplacesRatherThanMerges() throws Exception {
|
||||
JsonNode config = cognitoJson("SetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"LogConfigurations": [
|
||||
{
|
||||
"LogLevel": "INFO",
|
||||
"EventSource": "userAuthEvents",
|
||||
"CloudWatchLogsConfiguration": {"LogGroupArn": "%s"}
|
||||
}
|
||||
]
|
||||
}
|
||||
""".formatted(poolId, LOG_GROUP_ARN)).path("LogDeliveryConfiguration");
|
||||
|
||||
assertEquals(1, config.path("LogConfigurations").size(),
|
||||
"Set replaces the whole list; the userNotification entry must be gone");
|
||||
assertEquals("userAuthEvents", config.path("LogConfigurations").get(0).path("EventSource").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void anEmptyListClearsTheConfiguration() throws Exception {
|
||||
assertEquals(0, cognitoJson("SetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"LogConfigurations": []
|
||||
}
|
||||
""".formatted(poolId)).path("LogDeliveryConfiguration").path("LogConfigurations").size());
|
||||
|
||||
assertEquals(0, cognitoJson("GetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "%s"
|
||||
}
|
||||
""".formatted(poolId)).path("LogDeliveryConfiguration").path("LogConfigurations").size());
|
||||
}
|
||||
|
||||
/**
|
||||
* A malformed request must not be read as an intentional clear: AWS rejects a null
|
||||
* LogConfigurations outright, and reports a wrong JSON type as a serialization failure.
|
||||
*/
|
||||
@Test
|
||||
@Order(9)
|
||||
void malformedRequestsAreRejectedRatherThanClearingTheConfiguration() throws Exception {
|
||||
cognitoJson("SetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"LogConfigurations": [
|
||||
{
|
||||
"LogLevel": "ERROR",
|
||||
"EventSource": "userNotification",
|
||||
"CloudWatchLogsConfiguration": {"LogGroupArn": "%s"}
|
||||
}
|
||||
]
|
||||
}
|
||||
""".formatted(poolId, LOG_GROUP_ARN));
|
||||
|
||||
cognitoAction("SetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "%s"
|
||||
}
|
||||
""".formatted(poolId))
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterException"))
|
||||
.body("message", equalTo(
|
||||
"1 validation error detected: Value null at 'logConfigurations' failed to "
|
||||
+ "satisfy constraint: Member must not be null"));
|
||||
|
||||
cognitoAction("SetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"LogConfigurations": "nope"
|
||||
}
|
||||
""".formatted(poolId))
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("SerializationException"));
|
||||
|
||||
assertEquals(1, cognitoJson("GetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "%s"
|
||||
}
|
||||
""".formatted(poolId)).path("LogDeliveryConfiguration").path("LogConfigurations").size(),
|
||||
"a rejected request must leave the stored configuration untouched");
|
||||
}
|
||||
|
||||
/**
|
||||
* A scalar inside the array is a deserialization failure for AWS, not a 500, and a null
|
||||
* element is dropped rather than rejected.
|
||||
*/
|
||||
@Test
|
||||
@Order(10)
|
||||
void malformedArrayElementsMatchAwsHandling() throws Exception {
|
||||
cognitoAction("SetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"LogConfigurations": [1, 2]
|
||||
}
|
||||
""".formatted(poolId))
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("SerializationException"));
|
||||
|
||||
cognitoAction("SetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"LogConfigurations": ["a string"]
|
||||
}
|
||||
""".formatted(poolId))
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("SerializationException"));
|
||||
|
||||
assertEquals(0, cognitoJson("SetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"LogConfigurations": [null]
|
||||
}
|
||||
""".formatted(poolId)).path("LogDeliveryConfiguration").path("LogConfigurations").size(),
|
||||
"AWS drops a null element rather than rejecting it");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(11)
|
||||
void unknownPoolIsRejected() {
|
||||
cognitoAction("GetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "us-east-1_nosuchpool"
|
||||
}
|
||||
""")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ResourceNotFoundException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(12)
|
||||
void setRejectsMoreThanTwoConfigurations() {
|
||||
cognitoAction("SetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"LogConfigurations": [
|
||||
{"LogLevel": "ERROR", "EventSource": "userNotification",
|
||||
"CloudWatchLogsConfiguration": {"LogGroupArn": "%s"}},
|
||||
{"LogLevel": "INFO", "EventSource": "userAuthEvents",
|
||||
"CloudWatchLogsConfiguration": {"LogGroupArn": "%s"}},
|
||||
{"LogLevel": "ERROR", "EventSource": "userNotification",
|
||||
"CloudWatchLogsConfiguration": {"LogGroupArn": "%s"}}
|
||||
]
|
||||
}
|
||||
""".formatted(poolId, LOG_GROUP_ARN, LOG_GROUP_ARN, LOG_GROUP_ARN))
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterException"))
|
||||
.body("message", equalTo(
|
||||
"1 validation error detected: Value '["
|
||||
+ cloudWatchConfig("ERROR", "userNotification") + ", "
|
||||
+ cloudWatchConfig("INFO", "userAuthEvents") + ", "
|
||||
+ cloudWatchConfig("ERROR", "userNotification")
|
||||
+ "]' at 'logConfigurations' failed to satisfy constraint: "
|
||||
+ "Member must have length less than or equal to 2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(13)
|
||||
void theLengthViolationIsReportedAlongsideElementViolations() {
|
||||
cognitoAction("SetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"LogConfigurations": [
|
||||
{"LogLevel": "ERROR", "EventSource": "userNotification",
|
||||
"CloudWatchLogsConfiguration": {"LogGroupArn": "%s"}},
|
||||
{"LogLevel": "BOGUS", "EventSource": "userAuthEvents",
|
||||
"CloudWatchLogsConfiguration": {"LogGroupArn": "%s"}},
|
||||
{"LogLevel": "ERROR", "EventSource": "userNotification",
|
||||
"CloudWatchLogsConfiguration": {"LogGroupArn": "%s"}}
|
||||
]
|
||||
}
|
||||
""".formatted(poolId, LOG_GROUP_ARN, LOG_GROUP_ARN, LOG_GROUP_ARN))
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterException"))
|
||||
.body("message", equalTo(
|
||||
"2 validation errors detected: Value '["
|
||||
+ cloudWatchConfig("ERROR", "userNotification") + ", "
|
||||
+ cloudWatchConfig("BOGUS", "userAuthEvents") + ", "
|
||||
+ cloudWatchConfig("ERROR", "userNotification")
|
||||
+ "]' at 'logConfigurations' failed to satisfy constraint: "
|
||||
+ "Member must have length less than or equal to 2; "
|
||||
+ "Value 'BOGUS' at 'logConfigurations.2.member.logLevel' failed to satisfy "
|
||||
+ "constraint: Member must satisfy enum value set: [ERROR, INFO]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(14)
|
||||
void shapeViolationsAreReportedBeforeTheMissingPool() {
|
||||
cognitoAction("SetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "us-east-1_nosuchpool",
|
||||
"LogConfigurations": [
|
||||
{"LogLevel": "BOGUS", "EventSource": "userNotification",
|
||||
"CloudWatchLogsConfiguration": {"LogGroupArn": "%s"}}
|
||||
]
|
||||
}
|
||||
""".formatted(LOG_GROUP_ARN))
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterException"))
|
||||
.body("message", equalTo(
|
||||
"1 validation error detected: Value 'BOGUS' at "
|
||||
+ "'logConfigurations.1.member.logLevel' failed to satisfy constraint: "
|
||||
+ "Member must satisfy enum value set: [ERROR, INFO]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(15)
|
||||
void setRejectsARepeatedEventSource() {
|
||||
cognitoAction("SetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"LogConfigurations": [
|
||||
{"LogLevel": "ERROR", "EventSource": "userNotification",
|
||||
"CloudWatchLogsConfiguration": {"LogGroupArn": "%s"}},
|
||||
{"LogLevel": "INFO", "EventSource": "userNotification",
|
||||
"CloudWatchLogsConfiguration": {"LogGroupArn": "%s"}}
|
||||
]
|
||||
}
|
||||
""".formatted(poolId, LOG_GROUP_ARN, LOG_GROUP_ARN))
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterException"))
|
||||
.body("message", equalTo(
|
||||
"Request validation Failed. Following event sources appear more then once "
|
||||
+ "in a request: [userNotification]."));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(16)
|
||||
void aRepeatedEventSourceWithNoDestinationReportsBothClauses() {
|
||||
cognitoAction("SetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"LogConfigurations": [
|
||||
{"LogLevel": "ERROR", "EventSource": "userNotification"},
|
||||
{"LogLevel": "ERROR", "EventSource": "userNotification"}
|
||||
]
|
||||
}
|
||||
""".formatted(poolId))
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterException"))
|
||||
.body("message", equalTo(
|
||||
"Request validation Failed. Following event sources in request have no "
|
||||
+ "destination: [userNotification]. Following event sources appear "
|
||||
+ "more then once in a request: [userNotification]."));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(17)
|
||||
void aRejectedOversizedRequestLeavesTheConfigurationAlone() throws Exception {
|
||||
assertEquals(0, cognitoJson("GetLogDeliveryConfiguration", """
|
||||
{
|
||||
"UserPoolId": "%s"
|
||||
}
|
||||
""".formatted(poolId)).path("LogDeliveryConfiguration").path("LogConfigurations").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(18)
|
||||
void deletePool() {
|
||||
cognitoAction("DeleteUserPool", """
|
||||
{
|
||||
"UserPoolId": "%s"
|
||||
}
|
||||
""".formatted(poolId))
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
private static String cloudWatchConfig(String logLevel, String eventSource) {
|
||||
return "LogConfigurationType(logLevel=" + logLevel + ", eventSource=" + eventSource
|
||||
+ ", cloudWatchLogsConfiguration=CloudWatchLogsConfigurationType(logGroupArn="
|
||||
+ LOG_GROUP_ARN + "), s3Configuration=null, firehoseConfiguration=null)";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user