mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(eventbridge): report a delivered event's time to the second, from the entry's Time when given (#8020)
This commit is contained in:
1 parent
e37f45f714
commit
3eb01eb8e9
4 files changed
+121
-2
No files matched your search
@@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.core.common.AwsErrorResponse;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.AwsJson11Controller;
|
||||
import io.floci.aws.core.common.AwsJsonTimestamp;
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.services.eventbridge.model.ApiDestination;
|
||||
@@ -413,6 +414,10 @@ public class EventBridgeHandler {
|
||||
if (!entryNode.path("Resources").isMissingNode()) {
|
||||
entry.put("Resources", entryNode.path("Resources"));
|
||||
}
|
||||
Instant time = AwsJsonTimestamp.epochSeconds(entryNode, "Time", "SerializationException");
|
||||
if (time != null) {
|
||||
entry.put("Time", time);
|
||||
}
|
||||
entries.add(entry);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +51,7 @@ import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
@@ -1490,7 +1491,8 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
node.put("source", source);
|
||||
node.put("detail-type", detailType);
|
||||
node.put("account", envelopeAccount);
|
||||
node.put("time", Instant.now().toString());
|
||||
Instant time = entry.get("Time") instanceof Instant entryTime ? entryTime : Instant.now();
|
||||
node.put("time", time.truncatedTo(ChronoUnit.SECONDS).toString());
|
||||
node.put("region", envelopeRegion);
|
||||
node.putArray("resources").addAll(resources);
|
||||
node.set("detail", objectMapper.readTree(detail));
|
||||
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
package io.floci.aws.services.eventbridge;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.floci.aws.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.eventbridge.EventBridgeClient;
|
||||
import software.amazon.awssdk.services.eventbridge.model.PutEventsRequestEntry;
|
||||
import software.amazon.awssdk.services.sqs.SqsClient;
|
||||
import software.amazon.awssdk.services.sqs.model.Message;
|
||||
import software.amazon.awssdk.services.sqs.model.QueueAttributeName;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class EventBridgeEventTimeIntegrationTest {
|
||||
|
||||
private static final String ACCOUNT = "725018800073";
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
private EventBridgeClient events;
|
||||
private SqsClient sqs;
|
||||
private String name;
|
||||
private String source;
|
||||
private String queueUrl;
|
||||
|
||||
@BeforeEach
|
||||
void routeASourceToAQueue() {
|
||||
events = AwsTestClients.create(EventBridgeClient.builder(), ACCOUNT, Region.US_EAST_1);
|
||||
sqs = AwsTestClients.create(SqsClient.builder(), ACCOUNT, Region.US_EAST_1);
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
name = "event-time-" + UUID.randomUUID();
|
||||
source = "repro." + name;
|
||||
queueUrl = sqs.createQueue(request -> request.queueName(name)).queueUrl();
|
||||
String queueArn = sqs.getQueueAttributes(request -> request.queueUrl(queueUrl)
|
||||
.attributeNames(QueueAttributeName.QUEUE_ARN)).attributes().get(QueueAttributeName.QUEUE_ARN);
|
||||
events.putRule(request -> request.name(name).eventPattern("{\"source\":[\"" + source + "\"]}"));
|
||||
events.putTargets(request -> request.rule(name).targets(target -> target.id("q").arn(queueArn)));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void deleteTheRouteAndCloseTheClients() {
|
||||
try {
|
||||
events.removeTargets(request -> request.rule(name).ids("q"));
|
||||
events.deleteRule(request -> request.name(name));
|
||||
sqs.deleteQueue(request -> request.queueUrl(queueUrl));
|
||||
} finally {
|
||||
try {
|
||||
sqs.close();
|
||||
} finally {
|
||||
events.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void deliveredEventWithoutAnEntryTimeIsStampedToTheSecond() throws Exception {
|
||||
String time = deliveredTime(PutEventsRequestEntry.builder()
|
||||
.source(source).detailType("no-time").detail("{}").build());
|
||||
|
||||
assertTrue(time.matches("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z"), time);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deliveredEventReportsTheEntryTimeToTheSecond() throws Exception {
|
||||
String time = deliveredTime(PutEventsRequestEntry.builder()
|
||||
.source(source).detailType("with-time").detail("{}")
|
||||
.time(Instant.parse("2026-01-02T03:04:05.678Z")).build());
|
||||
|
||||
assertEquals("2026-01-02T03:04:05Z", time);
|
||||
}
|
||||
|
||||
@Test
|
||||
void entryTimeOutsideTheTimestampRangeIsRefusedAsASerializationError() {
|
||||
putEventsWithTime("1e30");
|
||||
}
|
||||
|
||||
@Test
|
||||
void entryTimeThatIsNotANumberIsRefusedAsASerializationError() {
|
||||
putEventsWithTime("\"yesterday\"");
|
||||
}
|
||||
|
||||
private void putEventsWithTime(String time) {
|
||||
given()
|
||||
.contentType("application/x-amz-json-1.1")
|
||||
.header("X-Amz-Target", "AWSEvents.PutEvents")
|
||||
.body("{\"Entries\":[{\"Source\":\"" + source
|
||||
+ "\",\"DetailType\":\"bad-time\",\"Detail\":\"{}\",\"Time\":" + time + "}]}")
|
||||
.when().post("/")
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("SerializationException"));
|
||||
}
|
||||
|
||||
private String deliveredTime(PutEventsRequestEntry entry) throws Exception {
|
||||
events.putEvents(request -> request.entries(entry));
|
||||
List<Message> messages = sqs.receiveMessage(request -> request.queueUrl(queueUrl)
|
||||
.waitTimeSeconds(5)).messages();
|
||||
assertEquals(1, messages.size());
|
||||
return MAPPER.readTree(messages.get(0).body()).path("time").asText();
|
||||
}
|
||||
}
|
||||
@@ -140,7 +140,7 @@ class EventBridgeIntegrationTest {
|
||||
.then().statusCode(200);
|
||||
|
||||
String expectedMessage = "\\{\"version\":\"0\",\"id\":\"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\",\"source\":\"com.mycompany.myapp\"," +
|
||||
"\"detail-type\":\"myDetailType\",\"account\":\"000000000000\",\"time\":\"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]+Z\"," +
|
||||
"\"detail-type\":\"myDetailType\",\"account\":\"000000000000\",\"time\":\"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z\"," +
|
||||
"\"region\":\"us-east-1\",\"resources\":\\[\"resource1\",\"resource2\"],\"detail\":\\{\"key1\":\"value1\",\"key2\":\"value2\"},\"event-bus-name\":\"default\"}";
|
||||
|
||||
given()
|
||||
|
||||
Reference in new issue
Block a user