feat(redshift): add event subscriptions with SNS delivery (#6705)

* feat(redshift): add event subscriptions with SNS delivery

Add Create, Modify, Delete and DescribeEventSubscriptions, subscription tagging, and publication of matching events to the subscription's SNS topic, honouring the topic policy.

* fix(redshift): deliver events from a consistent subscription snapshot and reject nameless subscription ARNs

Delivery copied no state under the lock modifySubscription holds, so a concurrent
update could change the topic between matching and publishing. Match and publish
from a snapshot taken under that lock.

A tag ARN ending in eventsubscription: passed an empty name to
describeSubscriptions, which lists all subscriptions. Reject it up front.
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-10-27 11:34:38 -04:00
1 parent c538fb5520
commit 3d30bbcf7a
15 files changed
+1744 -105

No files matched your search

@@ -6,54 +6,99 @@ import software.amazon.awssdk.services.redshift.RedshiftClient;
import software.amazon.awssdk.services.redshift.model.DescribeEventsResponse;
import software.amazon.awssdk.services.redshift.model.Event;
import software.amazon.awssdk.services.redshift.model.EventCategoriesMap;
import software.amazon.awssdk.services.redshift.model.EventSubscription;
import software.amazon.awssdk.services.redshift.model.Parameter;
import software.amazon.awssdk.services.redshift.model.RedshiftException;
import software.amazon.awssdk.services.redshift.model.Tag;
import software.amazon.awssdk.services.sns.SnsClient;
import java.time.Instant;
import java.util.List;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
@DisplayName("Redshift event history")
@DisplayName("Redshift event history and notification subscriptions")
class RedshiftEventsTest {
@Test
@DisplayName("SDK reads parameter-group events and their category metadata")
void parameterGroupEventsAndCategories() {
assumeFalse(TestFixtures.isRealAws(), "Checks immediate local event recording");
@DisplayName("SDK reads parameter-group events and manages a filtered SNS event subscription")
void parameterGroupEventsAndSubscriptionLifecycle() {
assumeFalse(TestFixtures.isRealAws(), "Checks immediate local event recording and subscription updates");
String name = "compat-rsevent-" + UUID.randomUUID().toString().substring(0, 12);
String sourceType = "cluster-parameter-group";
Instant start = Instant.now().minusSeconds(1);
try (RedshiftClient redshift = TestFixtures.redshiftClient()) {
redshift.createClusterParameterGroup(request -> request.parameterGroupName(name)
.parameterGroupFamily("redshift-1.0").description("SDK event compatibility"));
try (RedshiftClient redshift = TestFixtures.redshiftClient(); SnsClient sns = TestFixtures.snsClient()) {
String topic = sns.createTopic(request -> request.name(name)).topicArn();
try {
redshift.modifyClusterParameterGroup(request -> request.parameterGroupName(name)
.parameters(Parameter.builder().parameterName("statement_timeout")
.parameterValue("5000").build()));
DescribeEventsResponse history = redshift.describeEvents(request -> request
.sourceIdentifier(name).sourceType(sourceType).startTime(start));
assertThat(history.events()).extracting(Event::eventId)
.contains("REDSHIFT-EVENT-1003", "REDSHIFT-EVENT-1005");
assertThat(history.events()).allSatisfy(event -> {
assertThat(event.sourceIdentifier()).isEqualTo(name);
assertThat(event.sourceTypeAsString()).isEqualTo(sourceType);
assertThat(event.date()).isAfterOrEqualTo(start);
assertThat(event.message()).isNotBlank();
});
redshift.createClusterParameterGroup(request -> request.parameterGroupName(name)
.parameterGroupFamily("redshift-1.0").description("SDK event compatibility"));
try {
EventSubscription created = redshift.createEventSubscription(request -> request
.subscriptionName(name).snsTopicArn(topic).sourceType(sourceType).sourceIds(name)
.eventCategories("configuration").severity("INFO").enabled(true)
.tags(Tag.builder().key("suite").value("sdk-java").build())).eventSubscription();
try {
assertThat(created.custSubscriptionId()).isEqualTo(name);
assertThat(created.snsTopicArn()).isEqualTo(topic);
assertThat(created.sourceType()).isEqualTo(sourceType);
assertThat(created.sourceIdsList()).containsExactly(name);
assertThat(created.eventCategoriesList()).containsExactly("configuration");
assertThat(created.subscriptionCreationTime()).isNotNull();
assertThat(created.enabled()).isTrue();
assertThat(created.status()).isEqualTo("active");
assertThat(created.tags()).extracting(Tag::key).containsExactly("suite");
List<EventCategoriesMap> categories = redshift.describeEventCategories(request -> request
.sourceType(sourceType)).eventCategoriesMapList();
assertThat(categories).hasSize(1);
assertThat(categories.get(0).sourceType()).isEqualTo(sourceType);
assertThat(categories.get(0).events()).anySatisfy(info -> {
assertThat(info.eventId()).isEqualTo("REDSHIFT-EVENT-1003");
assertThat(info.eventCategories()).containsExactly("configuration");
assertThat(info.severity()).isEqualTo("INFO");
});
EventSubscription described = redshift.describeEventSubscriptions(request -> request
.subscriptionName(name)).eventSubscriptionsList().get(0);
assertThat(described.sourceIdsList()).containsExactly(name);
assertThat(described.subscriptionCreationTime()).isEqualTo(created.subscriptionCreationTime());
redshift.modifyClusterParameterGroup(request -> request.parameterGroupName(name)
.parameters(Parameter.builder().parameterName("statement_timeout")
.parameterValue("5000").build()));
DescribeEventsResponse history = redshift.describeEvents(request -> request
.sourceIdentifier(name).sourceType(sourceType).startTime(start));
assertThat(history.events()).extracting(Event::eventId)
.contains("REDSHIFT-EVENT-1003", "REDSHIFT-EVENT-1005");
assertThat(history.events()).allSatisfy(event -> {
assertThat(event.sourceIdentifier()).isEqualTo(name);
assertThat(event.sourceTypeAsString()).isEqualTo(sourceType);
assertThat(event.date()).isAfterOrEqualTo(start);
assertThat(event.message()).isNotBlank();
});
List<EventCategoriesMap> categories = redshift.describeEventCategories(request -> request
.sourceType(sourceType)).eventCategoriesMapList();
assertThat(categories).hasSize(1);
assertThat(categories.get(0).sourceType()).isEqualTo(sourceType);
assertThat(categories.get(0).events()).anySatisfy(info -> {
assertThat(info.eventId()).isEqualTo("REDSHIFT-EVENT-1003");
assertThat(info.eventCategories()).containsExactly("configuration");
assertThat(info.severity()).isEqualTo("INFO");
});
EventSubscription disabled = redshift.modifyEventSubscription(request -> request
.subscriptionName(name).enabled(false)).eventSubscription();
assertThat(disabled.enabled()).isFalse();
assertThat(disabled.sourceIdsList()).containsExactly(name);
assertThat(disabled.eventCategoriesList()).containsExactly("configuration");
assertThat(disabled.subscriptionCreationTime()).isEqualTo(created.subscriptionCreationTime());
} finally {
redshift.deleteEventSubscription(request -> request.subscriptionName(name));
}
assertThatThrownBy(() -> redshift.describeEventSubscriptions(request -> request.subscriptionName(name)))
.isInstanceOfSatisfying(RedshiftException.class, error -> {
assertThat(error.awsErrorDetails().errorCode()).isEqualTo("SubscriptionNotFound");
assertThat(error.statusCode()).isEqualTo(404);
});
} finally {
redshift.deleteClusterParameterGroup(request -> request.parameterGroupName(name));
}
} finally {
redshift.deleteClusterParameterGroup(request -> request.parameterGroupName(name));
sns.deleteTopic(request -> request.topicArn(topic));
}
}
}
+1 -1
View File
@@ -76,7 +76,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac
| [Neptune](neptune.md) | `POST /` with `Action=` param + Gremlin TCP proxy | Query + WebSocket | 14 |
| [DocumentDB](docdb.md) | `POST /` with `Action=` param + MongoDB container | Query + MongoDB wire | 13 |
| [DMS](dms.md) | `POST /` + `X-Amz-Target: AmazonDMSv20160101.*` | JSON 1.1 | 20 |
| [Redshift](redshift.md) | `POST /` with `Action=` param + PostgreSQL container | Query + PostgreSQL wire (+ CFN) | 46 |
| [Redshift](redshift.md) | `POST /` with `Action=` param + PostgreSQL container | Query + PostgreSQL wire (+ CFN) | 50 |
| [Redshift Data API](redshift-data.md) | `POST /` + `X-Amz-Target: RedshiftData.*` | JSON 1.1 | 11 |
| [Redshift Serverless](redshift-serverless.md) | `POST /` + `X-Amz-Target: RedshiftServerless.*` | JSON 1.1 | 55 |
| [EMR](emr.md) | `POST /` + `X-Amz-Target: ElasticMapReduce.*` | JSON 1.1 | 40 |
+35 -5
View File
@@ -14,14 +14,14 @@ Snapshot-dump and catalog-seed uploads retry transient Docker I/O failures for u
For running SQL without a PostgreSQL wire connection (the way Lambda and Step Functions do), see the [Redshift Data API](redshift-data.md).
## Events
## Events and notifications
`DescribeEvents` returns the last hour by default, accepts `StartTime`, `EndTime`,
`Duration`, and a paired `SourceType`/`SourceIdentifier` filter, and retains the last
14 days. `MaxRecords` accepts 20 through 100; continue with the returned `Marker`.
These bounds follow the [AWS DescribeEvents API](https://docs.aws.amazon.com/redshift/latest/APIReference/API_DescribeEvents.html).
History is isolated by account and region and uses the configured Redshift storage
mode, including persistent, hybrid and WAL storage.
History and event subscriptions are isolated by account and region and use the
configured Redshift storage mode, including persistent, hybrid and WAL storage.
Floci records cluster creation, deletion, reboot, restore, password changes,
parameter-group association and security-group changes. Snapshot operations record
@@ -33,9 +33,35 @@ type. It does not advertise hardware failures, maintenance or other conditions F
does not simulate. Security-group and scheduled-action source types are accepted as
filters, but Floci does not model resources or generate events for those source types.
Use `CreateEventSubscription`, `ModifyEventSubscription`, `DescribeEventSubscriptions`
and `DeleteEventSubscription` to manage SNS delivery. A subscription can filter source
type, source IDs, categories and severity. Omitted severity receives both INFO and
ERROR; explicit severity selects only that severity. All specified filters must match.
Only standard SNS topics are accepted, as required by
[AWS subscription rules](https://docs.aws.amazon.com/redshift/latest/mgmt/working-with-event-notifications-subscribe.html).
Subscriptions default to enabled; set `Enabled=false` to stop delivery without deleting
the subscription. The limit is 20 subscriptions per account and region.
Matching events are published to SNS with subject `Amazon Redshift Notification`.
The JSON message includes `Event Source`, `Event Time`, `Source ID`, `Event ID`,
`Event Categories`, `Severity` and `Event Message`. AWS does not specify a stable
notification-body schema in the API reference; this message format is Floci's
representation of the event details, not a claim of byte-for-byte AWS parity.
With IAM enforcement on, the topic's policy must allow the `redshift.amazonaws.com`
principal to `sns:Publish` (conditions on `aws:SourceArn`, the subscription's ARN, and
`aws:SourceAccount` apply); otherwise nothing is published and the subscription becomes
`no-permission`. A deleted topic changes subscription status to `topic-not-exist`;
other authorization failures also change it to `no-permission`. Notification delivery failures are logged
and do not roll back the Redshift operation.
For example, parameter-group events work without a database container:
```bash
TOPIC=$(aws --endpoint-url http://localhost:4566 sns create-topic \
--name redshift-events --query TopicArn --output text)
aws --endpoint-url http://localhost:4566 redshift create-event-subscription \
--subscription-name parameter-events --sns-topic-arn "$TOPIC" \
--source-type cluster-parameter-group --event-categories configuration
aws --endpoint-url http://localhost:4566 redshift create-cluster-parameter-group \
--parameter-group-name event-demo --parameter-group-family redshift-1.0 \
--description 'Event example'
@@ -74,8 +100,12 @@ aws --endpoint-url http://localhost:4566 redshift describe-events \
| `CreateSnapshotCopyGrant` | Register a snapshot copy grant, defaulting `KmsKeyId` to the AWS-managed Redshift key. `SnapshotCopyGrantName` must be 1-63 characters, start with a lowercase letter, and contain only lowercase letters, digits and non-consecutive hyphens |
| `DescribeSnapshotCopyGrants` | List snapshot copy grants, optionally filtered by name, paged with `MaxRecords` and `Marker` |
| `DeleteSnapshotCopyGrant` | Remove a snapshot copy grant |
| `DescribeEvents` | - |
| `DescribeEventCategories` | - |
| `DescribeEvents` | Return retained events filtered by source and time, with marker pagination |
| `DescribeEventCategories` | List implemented event IDs, categories and severities by source type |
| `CreateEventSubscription` | Create an enabled or disabled subscription delivering matching events to a standard SNS topic |
| `ModifyEventSubscription` | Update the specified subscription filters, topic or enabled state |
| `DeleteEventSubscription` | Delete an event notification subscription |
| `DescribeEventSubscriptions` | List event subscriptions filtered by name or tags |
| `EnableSnapshotCopy` | Record cross-region snapshot copy on a cluster (`RetentionPeriod` defaults to 7, `ManualSnapshotRetentionPeriod` to -1); `DescribeClusters` then reports `ClusterSnapshotCopyStatus`. A `DestinationRegion` that is not an AWS region is refused with `UnknownSnapshotCopyRegionFault`. No snapshot is copied |
| `DisableSnapshotCopy` | Clear a cluster's snapshot copy configuration |
| `ModifySnapshotCopyRetentionPeriod` | Change the automated (or, with `Manual`, the manual) snapshot copy retention period |
+12
View File
@@ -426,6 +426,18 @@
<version>${aws.sdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sns</artifactId>
<version>${aws.sdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sqs</artifactId>
<version>${aws.sdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>ssm</artifactId>
@@ -1,14 +1,22 @@
package io.github.hectorvent.floci.services.redshift;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.Pagination;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.RequestScopes;
import io.github.hectorvent.floci.core.common.ServiceCallAuthorizer;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.Decision;
import io.github.hectorvent.floci.services.redshift.RedshiftEventCatalog.EventInfo;
import io.github.hectorvent.floci.services.redshift.model.EventSubscription;
import io.github.hectorvent.floci.services.redshift.model.RedshiftEvent;
import io.github.hectorvent.floci.services.sns.SnsService;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
@@ -16,18 +24,26 @@ import org.jboss.logging.Logger;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiPredicate;
import java.util.regex.Pattern;
/**
* Records the events Floci raises for Redshift resources and serves DescribeEvents and
* DescribeEventCategories from that history.
* Records the events Floci raises for Redshift resources, serves DescribeEvents and the event
* subscription operations, and publishes matching events to the subscription's SNS topic.
*/
@ApplicationScoped
public class RedshiftEventService {
@@ -37,28 +53,47 @@ public class RedshiftEventService {
/** DescribeEvents reports the past 14 days at most. */
static final Duration RETENTION = Duration.ofDays(14);
private static final long DEFAULT_WINDOW_MINUTES = 60;
private static final int MAX_SUBSCRIPTIONS = 20;
private static final int MAX_TAGS = 50;
private static final int PAGE_MIN = 20;
private static final int PAGE_MAX = 100;
private static final int PRUNE_EVERY = 200;
private static final String SNS_SUBJECT = "Amazon Redshift Notification";
private static final Pattern SUBSCRIPTION_NAME = Pattern.compile("[A-Za-z](?:-?[A-Za-z0-9])*");
private static final DateTimeFormatter EVENT_TIME = DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss.SSS", Locale.ROOT).withZone(ZoneOffset.UTC);
private final AccountAwareStorageBackend<RedshiftEvent> events;
private final AccountAwareStorageBackend<EventSubscription> subscriptions;
private final SnsService snsService;
private final ServiceCallAuthorizer serviceCallAuthorizer;
private final RegionResolver regionResolver;
private final ObjectMapper objectMapper;
private final Clock clock;
private final AtomicLong sequence = new AtomicLong();
@Inject
public RedshiftEventService(StorageFactory storageFactory, RegionResolver regionResolver) {
this(storageFactory, regionResolver, Clock.systemUTC());
public RedshiftEventService(StorageFactory storageFactory, SnsService snsService,
ServiceCallAuthorizer serviceCallAuthorizer, RegionResolver regionResolver,
ObjectMapper objectMapper) {
this(storageFactory, snsService, serviceCallAuthorizer, regionResolver, objectMapper, Clock.systemUTC());
}
RedshiftEventService(StorageFactory storageFactory, RegionResolver regionResolver, Clock clock) {
RedshiftEventService(StorageFactory storageFactory, SnsService snsService,
ServiceCallAuthorizer serviceCallAuthorizer, RegionResolver regionResolver,
ObjectMapper objectMapper, Clock clock) {
this.events = storageFactory.create("redshift", "redshift-events.json",
new TypeReference<Map<String, RedshiftEvent>>() { });
this.subscriptions = storageFactory.create("redshift", "redshift-event-subscriptions.json",
new TypeReference<Map<String, EventSubscription>>() { });
this.snsService = snsService;
this.serviceCallAuthorizer = serviceCallAuthorizer;
this.regionResolver = regionResolver;
this.objectMapper = objectMapper;
this.clock = clock;
}
// ── Recording ──────────────────────────────────────────────────────────────────────────────────────────────────────────
// ── Recording and delivery ───────────────────────────────────────────────
/** Never fails the operation that raised the event: a lost event is logged, not surfaced. */
public void record(EventInfo info, String sourceIdentifier, String message) {
@@ -83,6 +118,7 @@ public class RedshiftEventService {
if (eventSequence % PRUNE_EVERY == 0) {
prune(now);
}
deliver(event, accountId, region);
} catch (RuntimeException e) {
LOG.warnv(e, "Could not record Redshift event {0} for {1}", info.id(), sourceIdentifier);
}
@@ -97,6 +133,127 @@ public class RedshiftEventService {
}
}
private void deliver(RedshiftEvent event, String accountId, String region) {
for (Delivery delivery : matchingDeliveries(event, accountId, region)) {
publish(delivery, event, accountId, region);
}
}
/**
* Filters and topic are read under the lock {@link #modifySubscription} holds, so an event is never matched
* against one version of a subscription and published to the topic of another.
*/
private synchronized List<Delivery> matchingDeliveries(RedshiftEvent event, String accountId, String region) {
List<Delivery> matching = new ArrayList<>();
for (EventSubscription subscription : subscriptions.scanForAccount(accountId, key -> key.startsWith(region + "/"))) {
EventSubscription snapshot = snapshot(subscription);
if (matches(snapshot, event)) {
matching.add(new Delivery(subscription, snapshot));
}
}
return matching;
}
/** {@code stored} identifies the subscription a failure is recorded against; {@code snapshot} is what matched. */
private record Delivery(EventSubscription stored, EventSubscription snapshot) { }
private static EventSubscription snapshot(EventSubscription subscription) {
EventSubscription copy = new EventSubscription();
copy.setCustomerAwsId(subscription.getCustomerAwsId());
copy.setCustSubscriptionId(subscription.getCustSubscriptionId());
copy.setSnsTopicArn(subscription.getSnsTopicArn());
copy.setStatus(subscription.getStatus());
copy.setSubscriptionCreationTime(subscription.getSubscriptionCreationTime());
copy.setSourceType(subscription.getSourceType());
copy.setSourceIdsList(new ArrayList<>(subscription.getSourceIdsList()));
copy.setEventCategoriesList(new ArrayList<>(subscription.getEventCategoriesList()));
copy.setSeverity(subscription.getSeverity());
copy.setEnabled(subscription.isEnabled());
return copy;
}
private static boolean matches(EventSubscription subscription, RedshiftEvent event) {
if (!subscription.isEnabled() || !"active".equals(subscription.getStatus())) {
return false;
}
if (subscription.getSourceType() != null && !subscription.getSourceType().equals(event.getSourceType())) {
return false;
}
List<String> sourceIds = subscription.getSourceIdsList();
if (!sourceIds.isEmpty() && !sourceIds.contains(event.getSourceIdentifier())) {
return false;
}
List<String> categories = subscription.getEventCategoriesList();
if (!categories.isEmpty() && categories.stream().noneMatch(category ->
event.getEventCategories().stream().anyMatch(category::equalsIgnoreCase))) {
return false;
}
return subscription.getSeverity() == null || subscription.getSeverity().equals(event.getSeverity());
}
private void publish(Delivery delivery, RedshiftEvent event, String accountId, String region) {
EventSubscription subscription = delivery.snapshot();
String topicArn = subscription.getSnsTopicArn();
// A missing topic has no policy to consult, so it would read as a denial; name it for what it is.
if (!snsService.topicExists(topicArn, AwsArnUtils.parse(topicArn).region())) {
updateFailureStatus(delivery.stored(), topicArn, "topic-not-exist", accountId, region);
LOG.warnv("The topic {0} of the subscription does not exist; event {1} was not published",
topicArn, event.getEventId());
return;
}
String subscriptionArn = regionResolver.buildArn("redshift", region, accountId,
"eventsubscription:" + subscription.getCustSubscriptionId());
// The topic's policy decides alone, as for any service publishing on its own behalf.
if (serviceCallAuthorizer.authorize("redshift", "sns:Publish", topicArn, subscriptionArn, accountId)
!= Decision.ALLOW) {
updateFailureStatus(delivery.stored(), topicArn, "no-permission", accountId, region);
LOG.warnv("The policy of {0} does not let Redshift publish event {1}", topicArn, event.getEventId());
return;
}
try {
String topicRegion = AwsArnUtils.parse(topicArn).region();
RequestScopes.runAs(accountId, topicRegion,
() -> snsService.publish(topicArn, null, notification(event), SNS_SUBJECT, topicRegion));
} catch (AwsException e) {
if ("NotFound".equals(e.getErrorCode())) {
updateFailureStatus(delivery.stored(), topicArn, "topic-not-exist", accountId, region);
} else if (isAuthorizationFailure(e)) {
updateFailureStatus(delivery.stored(), topicArn, "no-permission", accountId, region);
}
LOG.warnv(e, "Could not publish Redshift event {0} to {1}", event.getEventId(), topicArn);
} catch (RuntimeException e) {
LOG.warnv(e, "Could not publish Redshift event {0} to {1}", event.getEventId(), topicArn);
}
}
private synchronized void updateFailureStatus(EventSubscription attempted, String topicArn, String status,
String accountId, String region) {
String storageKey = key(region, attempted.getCustSubscriptionId());
subscriptions.getForAccount(accountId, storageKey)
.filter(current -> current == attempted && topicArn.equals(current.getSnsTopicArn()))
.ifPresent(current -> {
current.setStatus(status);
subscriptions.putForAccount(accountId, storageKey, current);
});
}
/** AWS does not document the notification body, so this carries the same facts DescribeEvents reports. */
private String notification(RedshiftEvent event) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("Event Source", event.getSourceType());
body.put("Event Time", EVENT_TIME.format(event.getDate()));
body.put("Source ID", event.getSourceIdentifier());
body.put("Event ID", event.getEventId());
body.put("Event Categories", event.getEventCategories());
body.put("Severity", event.getSeverity());
body.put("Event Message", event.getMessage());
try {
return objectMapper.writeValueAsString(body);
} catch (JsonProcessingException e) {
throw new IllegalStateException("Could not serialize the Redshift event notification", e);
}
}
// ── DescribeEvents / DescribeEventCategories ─────────────────────────────
public PaginatedResult<RedshiftEvent> describeEvents(String sourceIdentifier, String sourceType, Instant startTime,
@@ -171,6 +328,192 @@ public class RedshiftEventService {
}
}
// ── Event subscriptions ──────────────────────────────────────────────────
/** {@code sourceExists} answers whether a source of the given type and identifier exists. */
public synchronized EventSubscription createSubscription(String name, String snsTopicArn, String sourceType,
List<String> sourceIds, List<String> categories,
String severity, Boolean enabled,
Map<String, String> tags,
BiPredicate<String, String> sourceExists) {
validateSubscriptionName(name);
if (subscriptions.get(key(name)).isPresent()) {
throw new AwsException("SubscriptionAlreadyExist", "Subscription " + name + " already exists.", 400);
}
if (subscriptions.scan(this::inRegion).size() >= MAX_SUBSCRIPTIONS) {
throw new AwsException("EventSubscriptionQuotaExceeded",
"The request would exceed the allowed number of event subscriptions for this account.", 400);
}
validateTags(tags);
validateFilters(sourceType, sourceIds, categories, severity, sourceExists);
requireTopic(snsTopicArn);
EventSubscription subscription = new EventSubscription();
subscription.setCustomerAwsId(regionResolver.getAccountId());
subscription.setCustSubscriptionId(name);
subscription.setSnsTopicArn(snsTopicArn);
subscription.setStatus("active");
subscription.setSubscriptionCreationTime(clock.instant().truncatedTo(ChronoUnit.MILLIS));
subscription.setSourceType(blankToNull(sourceType));
subscription.setSourceIdsList(sourceIds == null ? new ArrayList<>() : new ArrayList<>(sourceIds));
subscription.setEventCategoriesList(categories == null ? new ArrayList<>() : new ArrayList<>(categories));
subscription.setSeverity(blankToNull(severity) == null ? null : severity.toUpperCase(Locale.ROOT));
subscription.setEnabled(enabled == null || enabled);
subscription.setTags(tags == null ? new LinkedHashMap<>() : new LinkedHashMap<>(tags));
subscriptions.put(key(name), subscription);
subscriptions.flush();
return subscription;
}
/** Applies only the members the request names. */
public synchronized EventSubscription modifySubscription(String name, String snsTopicArn, String sourceType,
List<String> sourceIds, List<String> categories,
String severity, Boolean enabled,
BiPredicate<String, String> sourceExists) {
EventSubscription subscription = requireSubscription(name);
if (blankToNull(snsTopicArn) != null) {
requireTopic(snsTopicArn);
}
String effectiveType = blankToNull(sourceType) != null ? sourceType : subscription.getSourceType();
boolean changedType = !Objects.equals(effectiveType, subscription.getSourceType());
List<String> effectiveIds = sourceIds != null ? sourceIds
: changedType ? List.of() : subscription.getSourceIdsList();
validateFilters(effectiveType, effectiveIds, categories, severity,
sourceIds != null || changedType ? sourceExists : (type, id) -> true);
if (blankToNull(snsTopicArn) != null) {
subscription.setSnsTopicArn(snsTopicArn);
subscription.setStatus("active");
}
subscription.setSourceType(effectiveType);
subscription.setSourceIdsList(new ArrayList<>(effectiveIds));
if (categories != null) {
subscription.setEventCategoriesList(new ArrayList<>(categories));
}
if (blankToNull(severity) != null) {
subscription.setSeverity(severity.toUpperCase(Locale.ROOT));
}
if (enabled != null) {
subscription.setEnabled(enabled);
}
subscriptions.put(key(name), subscription);
subscriptions.flush();
return subscription;
}
public synchronized void deleteSubscription(String name) {
requireSubscription(name);
subscriptions.delete(key(name));
subscriptions.flush();
}
public PaginatedResult<EventSubscription> describeSubscriptions(String name, List<String> tagKeys,
List<String> tagValues, Integer maxRecords,
String marker) {
validatePageSize(maxRecords);
List<EventSubscription> matching;
if (blankToNull(name) != null) {
matching = List.of(requireSubscription(name));
} else {
boolean filterByTag = tagKeys != null && !tagKeys.isEmpty() || tagValues != null && !tagValues.isEmpty();
matching = subscriptions.scan(this::inRegion).stream()
.filter(subscription -> !filterByTag || hasAnyTag(subscription, tagKeys, tagValues))
.sorted(Comparator.comparing(EventSubscription::getCustSubscriptionId))
.toList();
}
return Pagination.paginate(matching, EventSubscription::getCustSubscriptionId, maxRecords, marker,
PAGE_MAX, PAGE_MAX, "InvalidParameterValue", "Marker");
}
private static boolean hasAnyTag(EventSubscription subscription, List<String> keys, List<String> values) {
return subscription.getTags().entrySet().stream().anyMatch(tag ->
(keys == null || keys.isEmpty() || keys.contains(tag.getKey()))
&& (values == null || values.isEmpty() || values.contains(tag.getValue())));
}
private EventSubscription requireSubscription(String name) {
if (blankToNull(name) == null) {
throw new AwsException("InvalidParameterValue", "SubscriptionName is required", 400);
}
return subscriptions.get(key(name)).orElseThrow(() -> new AwsException("SubscriptionNotFound",
"Event subscription " + name + " not found.", 404));
}
private static void validateSubscriptionName(String name) {
if (name == null || name.isBlank()) {
throw new AwsException("InvalidParameterValue", "SubscriptionName is required", 400);
}
if (name.length() > 255 || !SUBSCRIPTION_NAME.matcher(name).matches()) {
throw new AwsException("InvalidParameterValue", "SubscriptionName must contain from 1 to 255 "
+ "alphanumeric characters or hyphens, start with a letter, and neither end with a hyphen "
+ "nor contain two consecutive hyphens.", 400);
}
}
private void requireTopic(String snsTopicArn) {
if (blankToNull(snsTopicArn) == null) {
throw new AwsException("InvalidParameterValue", "SnsTopicArn is required", 400);
}
if (!AwsArnUtils.isArnFor(snsTopicArn, "sns")) {
throw new AwsException("SNSInvalidTopic", "SnsTopicArn is not a valid Amazon SNS topic ARN.", 400);
}
try {
Map<String, String> attributes = snsService.getTopicAttributes(snsTopicArn,
AwsArnUtils.parse(snsTopicArn).region());
if ("true".equals(attributes.get("FifoTopic"))) {
throw new AwsException("SNSInvalidTopic", "Redshift event subscriptions require a standard SNS topic.", 400);
}
} catch (AwsException e) {
if ("NotFound".equals(e.getErrorCode())) {
throw new AwsException("SNSTopicArnNotFound",
"An Amazon SNS topic with the ARN " + snsTopicArn + " does not exist.", 404);
}
if (isAuthorizationFailure(e)) {
throw new AwsException("SNSNoAuthorization", "Not authorized to access the SNS topic.", 400);
}
throw e;
}
}
private static void validateFilters(String sourceType, List<String> sourceIds, List<String> categories,
String severity, BiPredicate<String, String> sourceExists) {
boolean hasType = blankToNull(sourceType) != null;
if (hasType) {
requireSourceType(sourceType);
}
if (sourceIds != null && !sourceIds.isEmpty()) {
if (!hasType) {
throw new AwsException("InvalidParameterCombination",
"SourceType must be provided when SourceIds are supplied.", 400);
}
for (String sourceId : sourceIds) {
if (!sourceExists.test(sourceType, sourceId)) {
throw new AwsException("SourceNotFound", "Source " + sourceId + " of type " + sourceType
+ " could not be found.", 404);
}
}
}
if (categories != null) {
for (String category : categories) {
if (!RedshiftEventCatalog.CATEGORIES.contains(category.toLowerCase(Locale.ROOT))) {
throw new AwsException("SubscriptionCategoryNotFound",
"Event category " + category + " is not valid.", 404);
}
}
}
if (blankToNull(severity) != null
&& !RedshiftEventCatalog.SEVERITIES.contains(severity.toUpperCase(Locale.ROOT))) {
throw new AwsException("SubscriptionSeverityNotFound",
"Severity " + severity + " must be ERROR or INFO.", 404);
}
}
private static String blankToNull(String value) {
return value == null || value.isBlank() ? null : value;
}
private String key(String name) {
return key(regionResolver.getRegion(), name);
}
private static String key(String region, String name) {
return region + "/" + name;
}
@@ -178,4 +521,47 @@ public class RedshiftEventService {
private boolean inRegion(String storedKey) {
return storedKey.startsWith(regionResolver.getRegion() + "/");
}
private static boolean isAuthorizationFailure(AwsException error) {
return Set.of("AuthorizationError", "AccessDenied", "AccessDeniedException", "Forbidden")
.contains(error.getErrorCode());
}
private static void validateTags(Map<String, String> tags) {
if (tags == null) {
return;
}
if (tags.size() > MAX_TAGS) {
throw new AwsException("TagLimitExceededFault", "You have exceeded the number of tags allowed.", 400);
}
for (Map.Entry<String, String> tag : tags.entrySet()) {
if (tag.getKey() == null || tag.getKey().isBlank() || tag.getKey().length() > 128
|| tag.getKey().toLowerCase(Locale.ROOT).startsWith("aws:")
|| tag.getValue() == null || tag.getValue().length() > 256) {
throw new AwsException("InvalidTagFault", "Invalid event subscription tag.", 400);
}
}
}
public synchronized void addSubscriptionTags(String name, Map<String, String> tags) {
Map<String, String> updated = new LinkedHashMap<>(requireSubscription(name).getTags());
updated.putAll(tags);
replaceSubscriptionTags(name, updated);
}
public synchronized void deleteSubscriptionTags(String name, Collection<String> keys) {
Map<String, String> updated = new LinkedHashMap<>(requireSubscription(name).getTags());
keys.forEach(updated::remove);
replaceSubscriptionTags(name, updated);
}
public synchronized void replaceSubscriptionTags(String name, Map<String, String> tags) {
EventSubscription subscription = requireSubscription(name);
validateTags(tags);
subscription.setTags(new LinkedHashMap<>(tags));
subscriptions.put(key(name), subscription);
subscriptions.flush();
}
}
@@ -13,6 +13,7 @@ import io.github.hectorvent.floci.services.redshift.RedshiftEventCatalog.EventIn
import io.github.hectorvent.floci.services.redshift.model.Cluster;
import io.github.hectorvent.floci.services.redshift.model.ClusterParameterGroup;
import io.github.hectorvent.floci.services.redshift.model.ClusterSubnetGroup;
import io.github.hectorvent.floci.services.redshift.model.EventSubscription;
import io.github.hectorvent.floci.services.redshift.model.Integration;
import io.github.hectorvent.floci.services.redshift.model.Parameter;
import io.github.hectorvent.floci.services.redshift.model.RedshiftEvent;
@@ -688,6 +689,57 @@ public class RedshiftQueryHandler {
.build();
return Response.ok(xml).type(MediaType.APPLICATION_XML).build();
}
case "CreateEventSubscription" -> {
EventSubscription subscription = service.createEventSubscription(
params.getFirst("SubscriptionName"), params.getFirst("SnsTopicArn"),
params.getFirst("SourceType"), memberList(params, "SourceIds"),
memberList(params, "EventCategories"), params.getFirst("Severity"),
booleanParam(params, "Enabled"), parseTags(params));
return eventSubscriptionResponse(action, subscription);
}
case "ModifyEventSubscription" -> {
EventSubscription subscription = service.modifyEventSubscription(
params.getFirst("SubscriptionName"), params.getFirst("SnsTopicArn"),
params.getFirst("SourceType"), optionalEventList(params, "SourceIds"),
optionalEventList(params, "EventCategories"), params.getFirst("Severity"),
booleanParam(params, "Enabled"));
return eventSubscriptionResponse(action, subscription);
}
case "DeleteEventSubscription" -> {
service.deleteEventSubscription(params.getFirst("SubscriptionName"));
String xml = new XmlBuilder()
.start("DeleteEventSubscriptionResponse", AwsNamespaces.REDSHIFT)
.start("ResponseMetadata")
.elem("RequestId", "test-req-id")
.end("ResponseMetadata")
.end("DeleteEventSubscriptionResponse")
.build();
return Response.ok(xml).type(MediaType.APPLICATION_XML).build();
}
case "DescribeEventSubscriptions" -> {
PaginatedResult<EventSubscription> page = service.describeEventSubscriptions(
params.getFirst("SubscriptionName"), memberList(params, "TagKeys"),
memberList(params, "TagValues"), intParam(params, "MaxRecords"), params.getFirst("Marker"));
XmlBuilder xmlBuilder = new XmlBuilder()
.start("DescribeEventSubscriptionsResponse", AwsNamespaces.REDSHIFT)
.start("DescribeEventSubscriptionsResult");
if (page.nextToken() != null) {
xmlBuilder.elem("Marker", page.nextToken());
}
xmlBuilder.start("EventSubscriptionsList");
for (EventSubscription subscription : page.items()) {
xmlBuilder.raw(buildEventSubscriptionXml(subscription));
}
String xml = xmlBuilder
.end("EventSubscriptionsList")
.end("DescribeEventSubscriptionsResult")
.start("ResponseMetadata")
.elem("RequestId", "test-req-id")
.end("ResponseMetadata")
.end("DescribeEventSubscriptionsResponse")
.build();
return Response.ok(xml).type(MediaType.APPLICATION_XML).build();
}
case "EnableSnapshotCopy" -> {
Cluster cluster = service.enableSnapshotCopy(requireParam(params, "ClusterIdentifier"),
requireParam(params, "DestinationRegion"),
@@ -1302,6 +1354,50 @@ public class RedshiftQueryHandler {
return builder.end("Subnets").end("ClusterSubnetGroup").build();
}
private Response eventSubscriptionResponse(String operation, EventSubscription subscription) {
String xml = new XmlBuilder()
.start(operation + "Response", AwsNamespaces.REDSHIFT)
.start(operation + "Result")
.raw(buildEventSubscriptionXml(subscription))
.end(operation + "Result")
.start("ResponseMetadata")
.elem("RequestId", "test-req-id")
.end("ResponseMetadata")
.end(operation + "Response")
.build();
return Response.ok(xml).type(MediaType.APPLICATION_XML).build();
}
private String buildEventSubscriptionXml(EventSubscription subscription) {
XmlBuilder builder = new XmlBuilder()
.start("EventSubscription")
.elem("Severity", subscription.getSeverity())
.elem("CustSubscriptionId", subscription.getCustSubscriptionId());
if (subscription.getSourceType() != null) {
builder.elem("SourceType", subscription.getSourceType());
}
builder.elem("SnsTopicArn", subscription.getSnsTopicArn())
.start("SourceIdsList");
for (String sourceId : subscription.getSourceIdsList()) {
builder.elem("SourceId", sourceId);
}
builder.end("SourceIdsList").start("EventCategoriesList");
for (String category : subscription.getEventCategoriesList()) {
builder.elem("EventCategory", category);
}
builder.end("EventCategoriesList")
.elem("SubscriptionCreationTime", EVENT_TIMESTAMP.format(subscription.getSubscriptionCreationTime()))
.elem("Enabled", String.valueOf(subscription.isEnabled()))
.start("Tags");
for (Map.Entry<String, String> tag : subscription.getTags().entrySet()) {
builder.start("Tag").elem("Key", tag.getKey()).elem("Value", tag.getValue()).end("Tag");
}
return builder.end("Tags")
.elem("Status", subscription.getStatus())
.elem("CustomerAwsId", subscription.getCustomerAwsId())
.end("EventSubscription").build();
}
private String buildEventXml(RedshiftEvent event) {
XmlBuilder builder = new XmlBuilder()
.start("Event")
@@ -1319,6 +1415,22 @@ public class RedshiftQueryHandler {
.end("Event").build();
}
private static Boolean booleanParam(MultivaluedMap<String, String> params, String name) {
String raw = params.getFirst(name);
if (raw == null || raw.isBlank()) {
return null;
}
if (!"true".equalsIgnoreCase(raw) && !"false".equalsIgnoreCase(raw)) {
throw new AwsException("InvalidParameterValue", name + " must be true or false.", 400);
}
return Boolean.valueOf(raw);
}
private static List<String> optionalEventList(MultivaluedMap<String, String> params, String name) {
return params.keySet().stream().anyMatch(key -> key.equals(name) || key.startsWith(name + "."))
? memberList(params, name) : null;
}
/** ISO 8601 as the SDKs send it, with or without seconds, or epoch seconds. */
private static Instant timestampParam(MultivaluedMap<String, String> params, String name) {
String raw = params.getFirst(name);
@@ -1503,6 +1615,8 @@ public class RedshiftQueryHandler {
case "IamRoles", "AddIamRoles", "RemoveIamRoles" -> "IamRoleArn";
case "TagKeys" -> "TagKey";
case "TagValues" -> "TagValue";
case "SourceIds" -> "SourceId";
case "EventCategories" -> "EventCategory";
case "ScheduleDefinitions" -> "ScheduleDefinition";
case "DbGroups" -> "DbGroup";
default -> null;
@@ -22,6 +22,7 @@ import io.github.hectorvent.floci.services.redshift.model.Cluster;
import io.github.hectorvent.floci.services.redshift.model.ClusterParameterGroup;
import io.github.hectorvent.floci.services.redshift.model.ClusterSubnetGroup;
import io.github.hectorvent.floci.services.redshift.model.Endpoint;
import io.github.hectorvent.floci.services.redshift.model.EventSubscription;
import io.github.hectorvent.floci.services.redshift.model.Integration;
import io.github.hectorvent.floci.services.redshift.model.Parameter;
import io.github.hectorvent.floci.services.redshift.model.RedshiftEvent;
@@ -1895,6 +1896,17 @@ public class RedshiftService implements ManagedSecretRotator {
+ "' from snapshot '" + snapshotIdentifier + "' failed at " + eventTime() + ".");
}
/** Whether the named source exists; Floci does not model cluster security groups or scheduled actions. */
private boolean eventSourceExists(String sourceType, String sourceIdentifier) {
return switch (sourceType) {
case RedshiftEventCatalog.CLUSTER -> clusters.get(sourceIdentifier).isPresent();
case RedshiftEventCatalog.PARAMETER_GROUP -> DEFAULT_PARAMETER_GROUP_NAME.equals(sourceIdentifier)
|| parameterGroups.get(sourceIdentifier).isPresent();
case RedshiftEventCatalog.SNAPSHOT -> snapshots.get(sourceIdentifier).isPresent();
default -> false;
};
}
public PaginatedResult<RedshiftEvent> describeEvents(String sourceIdentifier, String sourceType,
Instant startTime, Instant endTime, Integer duration,
Integer maxRecords, String marker) {
@@ -1906,6 +1918,30 @@ public class RedshiftService implements ManagedSecretRotator {
return eventService().describeEventCategories(sourceType);
}
public EventSubscription createEventSubscription(String name, String snsTopicArn, String sourceType,
List<String> sourceIds, List<String> categories,
String severity, Boolean enabled, Map<String, String> tags) {
return eventService().createSubscription(name, snsTopicArn, sourceType, sourceIds, categories, severity,
enabled, tags, this::eventSourceExists);
}
public EventSubscription modifyEventSubscription(String name, String snsTopicArn, String sourceType,
List<String> sourceIds, List<String> categories,
String severity, Boolean enabled) {
return eventService().modifySubscription(name, snsTopicArn, sourceType, sourceIds, categories, severity,
enabled, this::eventSourceExists);
}
public void deleteEventSubscription(String name) {
eventService().deleteSubscription(name);
}
public PaginatedResult<EventSubscription> describeEventSubscriptions(String name, List<String> tagKeys,
List<String> tagValues, Integer maxRecords,
String marker) {
return eventService().describeSubscriptions(name, tagKeys, tagValues, maxRecords, marker);
}
// ── Cluster Subnet Group Operations ──────────────────────────────────────
public ClusterSubnetGroup createClusterSubnetGroup(String name, String description, String vpcId, List<String> subnetIds) {
@@ -2461,6 +2497,11 @@ public class RedshiftService implements ManagedSecretRotator {
}
public synchronized void createTags(String resourceName, Map<String, String> tags) {
String subscriptionName = eventSubscriptionTagName(resourceName);
if (subscriptionName != null) {
eventService().addSubscriptionTags(subscriptionName, tags);
return;
}
TagHandle handle = resolveTagHandle(resourceName);
Map<String, String> updated = new LinkedHashMap<>(handle.tags());
updated.putAll(tags);
@@ -2468,6 +2509,11 @@ public class RedshiftService implements ManagedSecretRotator {
}
public synchronized void deleteTags(String resourceName, Collection<String> tagKeys) {
String subscriptionName = eventSubscriptionTagName(resourceName);
if (subscriptionName != null) {
eventService().deleteSubscriptionTags(subscriptionName, tagKeys);
return;
}
TagHandle handle = resolveTagHandle(resourceName);
Map<String, String> updated = new LinkedHashMap<>(handle.tags());
tagKeys.forEach(updated::remove);
@@ -2517,6 +2563,13 @@ public class RedshiftService implements ManagedSecretRotator {
"snapshotschedule", s.getTags(), tagKeysFilter);
}
}
if (events != null && (resourceType == null || "eventsubscription".equalsIgnoreCase(resourceType))) {
for (EventSubscription subscription : events.describeSubscriptions(null, null, null, null, null).items()) {
addTaggedResources(result, regionResolver.buildArn("redshift", regionResolver.getRegion(),
"eventsubscription:" + subscription.getCustSubscriptionId()),
"eventsubscription", subscription.getTags(), tagKeysFilter);
}
}
return result;
}
@@ -2572,7 +2625,7 @@ public class RedshiftService implements ManagedSecretRotator {
* resolution, there is no bare-name fallback: Redshift tagging is new, so there is no
* existing caller to stay backward compatible with.
*/
private TagHandle resolveTagHandle(String resourceName) {
private AwsArnUtils.Arn requireTagArn(String resourceName) {
if (resourceName == null || resourceName.isBlank()) {
throw new AwsException("InvalidParameterValue", "ResourceName is required.", 400);
}
@@ -2598,6 +2651,17 @@ public class RedshiftService implements ManagedSecretRotator {
|| !regionResolver.getRegion().equals(arn.region())) {
throw new AwsException("ResourceNotFoundFault", "Resource " + resourceName + " not found", 404);
}
return arn;
}
private String eventSubscriptionTagName(String resourceName) {
String resource = requireTagArn(resourceName).resource();
String prefix = "eventsubscription:";
return resource.startsWith(prefix) ? resource.substring(prefix.length()) : null;
}
private TagHandle resolveTagHandle(String resourceName) {
AwsArnUtils.Arn arn = requireTagArn(resourceName);
String resource = arn.resource();
int sep = resource.indexOf(':');
if (sep < 0) {
@@ -2607,6 +2671,14 @@ public class RedshiftService implements ManagedSecretRotator {
String id = resource.substring(sep + 1);
return switch (type) {
case "eventsubscription" -> {
if (id.isBlank()) {
throw new AwsException("InvalidParameterValue", "Invalid resource name: " + resourceName, 400);
}
EventSubscription subscription = eventService().describeSubscriptions(id, null, null, null, null)
.items().getFirst();
yield new TagHandle(subscription.getTags(), updated -> eventService().replaceSubscriptionTags(id, updated));
}
case "cluster" -> {
Cluster cluster = clusters.get(id)
.orElseThrow(() -> new AwsException("ClusterNotFound", "Cluster " + id + " not found", 404));
@@ -0,0 +1,53 @@
package io.github.hectorvent.floci.services.redshift.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.time.Instant;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/** A Redshift event notification subscription, as DescribeEventSubscriptions reports it. */
@RegisterForReflection
public class EventSubscription {
private String customerAwsId;
private String custSubscriptionId;
private String snsTopicArn;
private String status;
private Instant subscriptionCreationTime;
private String sourceType;
private List<String> sourceIdsList = new ArrayList<>();
private List<String> eventCategoriesList = new ArrayList<>();
private String severity;
private boolean enabled;
private Map<String, String> tags = new LinkedHashMap<>();
public String getCustomerAwsId() { return customerAwsId; }
public void setCustomerAwsId(String customerAwsId) { this.customerAwsId = customerAwsId; }
public String getCustSubscriptionId() { return custSubscriptionId; }
public void setCustSubscriptionId(String custSubscriptionId) { this.custSubscriptionId = custSubscriptionId; }
public String getSnsTopicArn() { return snsTopicArn; }
public void setSnsTopicArn(String snsTopicArn) { this.snsTopicArn = snsTopicArn; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
public Instant getSubscriptionCreationTime() { return subscriptionCreationTime; }
public void setSubscriptionCreationTime(Instant subscriptionCreationTime) {
this.subscriptionCreationTime = subscriptionCreationTime;
}
public String getSourceType() { return sourceType; }
public void setSourceType(String sourceType) { this.sourceType = sourceType; }
public List<String> getSourceIdsList() { return sourceIdsList; }
public void setSourceIdsList(List<String> sourceIdsList) { this.sourceIdsList = sourceIdsList; }
public List<String> getEventCategoriesList() { return eventCategoriesList; }
public void setEventCategoriesList(List<String> eventCategoriesList) {
this.eventCategoriesList = eventCategoriesList;
}
public String getSeverity() { return severity; }
public void setSeverity(String severity) { this.severity = severity; }
public boolean isEnabled() { return enabled; }
public void setEnabled(boolean enabled) { this.enabled = enabled; }
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags; }
}
@@ -1,16 +1,25 @@
package io.github.hectorvent.floci.services.redshift;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.ServiceCallAuthorizer;
import io.github.hectorvent.floci.core.common.ServiceConfigAccess;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.Decision;
import io.github.hectorvent.floci.services.redshift.model.EventSubscription;
import io.github.hectorvent.floci.services.redshift.model.RedshiftEvent;
import io.github.hectorvent.floci.services.sns.SnsService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.ArgumentCaptor;
import java.time.Clock;
import java.time.Duration;
@@ -19,26 +28,64 @@ import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiPredicate;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentCaptor.forClass;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
class RedshiftEventServiceTest {
private static final String TOPIC = "arn:aws:sns:us-east-1:000000000000:redshift-events";
private static final BiPredicate<String, String> ALL_SOURCES_EXIST = (type, id) -> true;
private final ObjectMapper objectMapper = new ObjectMapper();
private SnsService sns;
private ServiceCallAuthorizer authorizer;
private MutableClock clock;
private RedshiftEventService service;
private StorageFactory factory;
@Test
void explicitRecordingUsesTheOwnersHistory() {
@ParameterizedTest
@CsvSource({"success,active", "NotFound,topic-not-exist", "AuthorizationError,no-permission"})
void explicitRecordingUsesTheOwnersHistorySubscriptionsAndFailureStatus(String failure, String expectedStatus) {
create("watcher", null, List.of(), List.of(), null);
String owner = "565656565656";
String region = "us-west-2";
String ownerTopic = "arn:aws:sns:us-west-2:565656565656:snapshots";
AccountAwareStorageBackend<EventSubscription> subscriptionStore = factory.create("redshift",
"redshift-event-subscriptions.json", new TypeReference<Map<String, EventSubscription>>() { });
EventSubscription watcher = new EventSubscription();
watcher.setCustSubscriptionId("watcher");
watcher.setCustomerAwsId(owner);
watcher.setSnsTopicArn(ownerTopic);
watcher.setStatus("active");
watcher.setEnabled(true);
subscriptionStore.putForAccount(owner, region + "/watcher", watcher);
if (!"success".equals(failure)) {
doThrow(new AwsException(failure, "delivery failed", 400)).when(sns)
.publish(eq(ownerTopic), any(), anyString(), anyString(), eq(region));
}
service.recordForAccount(owner, region, RedshiftEventCatalog.SNAPSHOT_COMPLETED, "owner-snapshot", "completed");
@@ -46,6 +93,55 @@ class RedshiftEventServiceTest {
new TypeReference<Map<String, RedshiftEvent>>() { });
assertEquals(1, history.scanForAccount(owner, key -> key.startsWith(region + "/")).size());
assertTrue(service.describeEvents(null, null, null, null, null, null, null).items().isEmpty());
assertEquals(expectedStatus, subscriptionStore.getForAccount(owner, region + "/watcher").orElseThrow().getStatus());
assertEquals("active", service.describeSubscriptions("watcher", null, null, null, null).items().getFirst().getStatus());
verify(sns).publish(eq(ownerTopic), any(), anyString(), anyString(), eq(region));
verify(sns, never()).publish(eq(TOPIC), any(), anyString(), anyString(), anyString());
}
@ParameterizedTest
@CsvSource({"NotFound,deleted", "NotFound,recreated", "NotFound,retargeted",
"AuthorizationError,deleted", "AuthorizationError,recreated", "AuthorizationError,retargeted"})
void failedNotificationDoesNotOverwriteConcurrentSubscriptionChange(String errorCode, String change) throws Exception {
create("watcher", null, List.of(), List.of(), null);
String replacementTopic = "arn:aws:sns:us-east-1:000000000000:replacement";
CountDownLatch publishing = new CountDownLatch(1);
CountDownLatch release = new CountDownLatch(1);
doAnswer(invocation -> {
publishing.countDown();
assertTrue(release.await(5, TimeUnit.SECONDS), "SNS publish was not released");
throw new AwsException(errorCode, "Topic delivery failed", 400);
}).when(sns).publish(eq(TOPIC), any(), anyString(), anyString(), anyString());
try (ExecutorService executor = Executors.newSingleThreadExecutor()) {
Future<?> recording = executor.submit(() ->
service.record(RedshiftEventCatalog.CLUSTER_CREATED, "warehouse", "created"));
try {
assertTrue(publishing.await(5, TimeUnit.SECONDS), "SNS publish did not start");
switch (change) {
case "deleted" -> service.deleteSubscription("watcher");
case "recreated" -> {
service.deleteSubscription("watcher");
create("watcher", null, List.of(), List.of(), null);
}
case "retargeted" -> service.modifySubscription("watcher", replacementTopic,
null, null, null, null, null, ALL_SOURCES_EXIST);
default -> throw new IllegalArgumentException(change);
}
} finally {
release.countDown();
}
recording.get(5, TimeUnit.SECONDS);
}
List<EventSubscription> remaining = service.describeSubscriptions(null, null, null, null, null).items();
if ("deleted".equals(change)) {
assertTrue(remaining.isEmpty(), "A late SNS failure must not recreate a deleted subscription");
} else {
assertEquals(1, remaining.size());
assertEquals("active", remaining.getFirst().getStatus());
assertEquals("retargeted".equals(change) ? replacementTopic : TOPIC, remaining.getFirst().getSnsTopicArn());
}
}
@Test
@@ -67,7 +163,7 @@ class RedshiftEventServiceTest {
AtomicReference<String> region = new AtomicReference<>("us-east-1");
RegionResolver resolver = mock(RegionResolver.class);
when(resolver.getRegion()).thenAnswer(invocation -> region.get());
RedshiftEventService alternating = new RedshiftEventService(factory, resolver, clock);
RedshiftEventService alternating = new RedshiftEventService(factory, sns, authorizer, resolver, objectMapper, clock);
for (int index = 0; index < 200; index++) {
region.set(index % 2 == 0 ? "us-east-1" : "us-west-2");
alternating.record(RedshiftEventCatalog.CLUSTER_CREATED, "warehouse", "created");
@@ -87,8 +183,14 @@ class RedshiftEventServiceTest {
when(config.storage().persistentPath()).thenReturn("target/test-data");
when(config.defaultAccountId()).thenReturn("000000000000");
factory = new StorageFactory(config, access);
sns = mock(SnsService.class);
clock = new MutableClock(Instant.parse("2026-10-26T12:00:00Z"));
service = new RedshiftEventService(factory, new RegionResolver("us-east-1", "000000000000"), clock);
when(sns.topicExists(anyString(), anyString())).thenReturn(true);
authorizer = mock(ServiceCallAuthorizer.class);
when(authorizer.authorize(anyString(), anyString(), anyString(), anyString(), anyString()))
.thenReturn(Decision.ALLOW);
service = new RedshiftEventService(factory, sns, authorizer, new RegionResolver("us-east-1", "000000000000"),
objectMapper, clock);
}
@Test
@@ -175,19 +277,331 @@ class RedshiftEventServiceTest {
}
@Test
void eventHistoryIsRegional() {
service.record(RedshiftEventCatalog.CLUSTER_CREATED, "warehouse", "created");
RedshiftEventService west = new RedshiftEventService(factory,
new RegionResolver("us-west-2", "000000000000"), clock);
void createSubscriptionDefaultsToActiveEnabledWithoutSeverityFilter() {
EventSubscription subscription = create("all-events", null, List.of(), List.of(), null);
assertTrue(west.describeEvents(null, null, null, null, null, null, null).items().isEmpty());
assertEquals("active", subscription.getStatus());
assertTrue(subscription.isEnabled());
assertNull(subscription.getSeverity());
assertEquals("000000000000", subscription.getCustomerAwsId());
assertEquals(clock.instant(), subscription.getSubscriptionCreationTime());
}
@Test
void createSubscriptionValidatesItsArguments() {
assertEquals("InvalidParameterValue", code(() -> create("1-starts-with-digit", null, List.of(), List.of(), null)));
assertEquals("InvalidParameterValue", code(() -> create("ends-with-hyphen-", null, List.of(), List.of(), null)));
assertEquals("InvalidParameterValue", code(() -> create("double--hyphen", null, List.of(), List.of(), null)));
assertEquals("InvalidParameterCombination", code(() -> create("ids-only", null, List.of("warehouse"), List.of(), null)));
assertEquals("InvalidParameterValue", code(() -> create("bad-type", "database", List.of(), List.of(), null)));
assertEquals("SubscriptionCategoryNotFound", code(() -> create("bad-category", null, List.of(), List.of("billing"), null)));
assertEquals("SubscriptionSeverityNotFound", code(() -> create("bad-severity", null, List.of(), List.of(), "WARN")));
assertEquals("SNSInvalidTopic", code(() -> service.createSubscription("not-a-topic", "arn:aws:sqs:us-east-1:0:q",
null, List.of(), List.of(), null, null, Map.of(), ALL_SOURCES_EXIST)));
verifyNoInteractions(sns);
}
@Test
void createSubscriptionRejectsMissingTopicsAndSources() {
doThrow(new AwsException("NotFound", "Topic does not exist.", 404)).when(sns).getTopicAttributes(anyString(), anyString());
assertEquals("SNSTopicArnNotFound", code(() -> create("no-topic", null, List.of(), List.of(), null)));
setUp();
assertEquals("SourceNotFound", code(() -> service.createSubscription("no-source", TOPIC, "cluster",
List.of("missing"), List.of(), null, null, Map.of(), (type, id) -> false)));
}
@Test
void createSubscriptionRejectsDuplicatesAndTheTwentyFirst() {
create("first", null, List.of(), List.of(), null);
assertEquals("SubscriptionAlreadyExist", code(() -> create("first", null, List.of(), List.of(), null)));
for (int i = 2; i <= 20; i++) {
create("subscription-" + i, null, List.of(), List.of(), null);
}
assertEquals("EventSubscriptionQuotaExceeded", code(() -> create("one-too-many", null, List.of(), List.of(), null)));
}
@Test
void modifySubscriptionChangesOnlyTheNamedMembers() {
create("watcher", "cluster", List.of("warehouse"), List.of("management"), "ERROR");
EventSubscription modified = service.modifySubscription("watcher", null, null, null, null, null, false,
ALL_SOURCES_EXIST);
assertFalse(modified.isEnabled());
assertEquals("cluster", modified.getSourceType());
assertEquals(List.of("warehouse"), modified.getSourceIdsList());
assertEquals(List.of("management"), modified.getEventCategoriesList());
assertEquals("ERROR", modified.getSeverity());
assertEquals("SubscriptionNotFound", code(() -> service.modifySubscription("missing", null, null, null, null,
null, null, ALL_SOURCES_EXIST)));
}
@Test
void deleteSubscriptionRemovesIt() {
create("watcher", null, List.of(), List.of(), null);
service.deleteSubscription("watcher");
assertEquals("SubscriptionNotFound", code(() -> service.deleteSubscription("watcher")));
assertTrue(service.describeSubscriptions(null, null, null, null, null).items().isEmpty());
}
@Test
void describeSubscriptionsFiltersByNameAndTagsAndPages() {
service.createSubscription("tagged", TOPIC, null, List.of(), List.of(), null, null,
Map.of("team", "data"), ALL_SOURCES_EXIST);
create("untagged", null, List.of(), List.of(), null);
assertEquals(List.of("tagged"), names(service.describeSubscriptions("tagged", null, null, null, null)));
assertEquals(List.of("tagged"), names(service.describeSubscriptions(null, List.of("team"), null, null, null)));
assertEquals(List.of("tagged"), names(service.describeSubscriptions(null, null, List.of("data"), null, null)));
assertEquals(List.of("tagged", "untagged"), names(service.describeSubscriptions(null, null, null, null, null)));
assertEquals("SubscriptionNotFound", code(() -> service.describeSubscriptions("missing", null, null, null, null)));
for (int i = 0; i < 18; i++) {
create(String.format("page-%02d", i), null, List.of(), List.of(), null);
}
PaginatedResult<EventSubscription> first = service.describeSubscriptions(null, null, null, 20, null);
assertEquals(20, first.items().size());
assertNull(first.nextToken());
}
@Test
void matchingEventsArePublishedToTheTopicWithTheirDetails() throws Exception {
create("everything", null, List.of(), List.of(), null);
service.record(RedshiftEventCatalog.CLUSTER_CREATED, "warehouse", "Amazon Redshift cluster 'warehouse' created.");
ArgumentCaptor<String> message = forClass(String.class);
verify(sns).publish(eq(TOPIC), eq(null), message.capture(), eq("Amazon Redshift Notification"), eq("us-east-1"));
JsonNode body = objectMapper.readTree(message.getValue());
assertEquals("cluster", body.get("Event Source").asText());
assertEquals("warehouse", body.get("Source ID").asText());
assertEquals("REDSHIFT-EVENT-2000", body.get("Event ID").asText());
assertEquals("INFO", body.get("Severity").asText());
assertEquals("management", body.get("Event Categories").get(0).asText());
assertEquals("2026-10-26 12:00:00.000", body.get("Event Time").asText());
assertEquals("Amazon Redshift cluster 'warehouse' created.", body.get("Event Message").asText());
}
@Test
void subscriptionFiltersDecideWhichEventsArePublished() {
create("clusters-only", "cluster", List.of(), List.of(), null);
create("one-cluster", "cluster", List.of("analytics"), List.of(), null);
create("security-only", null, List.of(), List.of("security"), null);
create("errors-only", null, List.of(), List.of(), "ERROR");
service.modifySubscription("security-only", null, null, null, null, null, null, ALL_SOURCES_EXIST);
service.record(RedshiftEventCatalog.CLUSTER_CREATED, "warehouse", "created");
// clusters-only matches; one-cluster names another cluster; security-only wants another
// category; errors-only wants another severity.
verify(sns, times(1)).publish(anyString(), any(), anyString(), anyString(), anyString());
service.record(RedshiftEventCatalog.SNAPSHOT_FAILED, "nightly", "failed");
// The ERROR snapshot event reaches errors-only and nothing else.
verify(sns, times(2)).publish(anyString(), any(), anyString(), anyString(), anyString());
}
@Test
void anEventIsPublishedToTheTopicItWasMatchedAgainstWhenTheSubscriptionChangesMidDelivery() throws Exception {
create("watcher", "cluster", List.of(), List.of(), null);
String replacementTopic = "arn:aws:sns:us-east-1:000000000000:replacement";
CountDownLatch delivering = new CountDownLatch(1);
CountDownLatch release = new CountDownLatch(1);
when(sns.topicExists(anyString(), anyString())).thenAnswer(invocation -> {
delivering.countDown();
assertTrue(release.await(5, TimeUnit.SECONDS), "Delivery was not released");
return true;
});
try (ExecutorService executor = Executors.newSingleThreadExecutor()) {
Future<?> recording = executor.submit(() ->
service.record(RedshiftEventCatalog.CLUSTER_CREATED, "warehouse", "created"));
try {
assertTrue(delivering.await(5, TimeUnit.SECONDS), "Delivery did not start");
service.modifySubscription("watcher", replacementTopic, "cluster-snapshot", null, null, null, null,
ALL_SOURCES_EXIST);
} finally {
release.countDown();
}
recording.get(5, TimeUnit.SECONDS);
}
verify(sns).publish(eq(TOPIC), any(), anyString(), anyString(), anyString());
verify(sns, never()).publish(eq(replacementTopic), any(), anyString(), anyString(), anyString());
}
@Test
void theTopicPolicyIsCheckedForRedshiftBeforeEachPublish() {
create("watcher", null, List.of(), List.of(), null);
service.record(RedshiftEventCatalog.CLUSTER_CREATED, "warehouse", "created");
verify(authorizer).authorize("redshift", "sns:Publish", TOPIC,
"arn:aws:redshift:us-east-1:000000000000:eventsubscription:watcher", "000000000000");
verify(sns).publish(eq(TOPIC), any(), anyString(), anyString(), anyString());
}
@Test
void aMissingTopicIsReportedAsMissingRatherThanDenied() {
create("watcher", null, List.of(), List.of(), null);
when(sns.topicExists(anyString(), anyString())).thenReturn(false);
service.record(RedshiftEventCatalog.CLUSTER_CREATED, "warehouse", "created");
assertEquals("topic-not-exist",
service.describeSubscriptions("watcher", null, null, null, null).items().getFirst().getStatus());
verify(sns, never()).publish(anyString(), any(), anyString(), anyString(), anyString());
verify(authorizer, never()).authorize(anyString(), anyString(), anyString(), anyString(), anyString());
}
@Test
void aTopicPolicyThatDeniesRedshiftBlocksDeliveryAndMarksTheSubscription() {
create("watcher", null, List.of(), List.of(), null);
when(authorizer.authorize(anyString(), anyString(), anyString(), anyString(), anyString()))
.thenReturn(Decision.DENY);
service.record(RedshiftEventCatalog.CLUSTER_CREATED, "warehouse", "created");
verify(sns, never()).publish(anyString(), any(), anyString(), anyString(), anyString());
assertEquals("no-permission",
service.describeSubscriptions("watcher", null, null, null, null).items().getFirst().getStatus());
assertEquals(1, service.describeEvents(null, null, null, null, null, null, null).items().size());
service.record(RedshiftEventCatalog.CLUSTER_DELETED, "warehouse", "deleted");
verify(authorizer, times(1)).authorize(anyString(), anyString(), anyString(), anyString(), anyString());
}
@Test
void disabledSubscriptionsPublishNothing() {
service.createSubscription("quiet", TOPIC, null, List.of(), List.of(), null, false, Map.of(), ALL_SOURCES_EXIST);
service.record(RedshiftEventCatalog.CLUSTER_CREATED, "warehouse", "created");
verify(sns, never()).publish(anyString(), any(), anyString(), anyString(), anyString());
}
@Test
void explicitInfoSeverityDoesNotReceiveErrors() {
create("info-only", null, List.of(), List.of(), "INFO");
service.record(RedshiftEventCatalog.SNAPSHOT_FAILED, "nightly", "failed");
verify(sns, never()).publish(anyString(), any(), anyString(), anyString(), anyString());
}
@Test
void omittedSeverityReceivesBothSeverities() {
create("all-severities", null, List.of(), List.of(), null);
service.record(RedshiftEventCatalog.CLUSTER_CREATED, "warehouse", "created");
service.record(RedshiftEventCatalog.SNAPSHOT_FAILED, "nightly", "failed");
verify(sns, times(2)).publish(anyString(), any(), anyString(), anyString(), anyString());
}
@Test
void fifoTopicsAreRejectedBeforeCreatingSubscription() {
when(sns.getTopicAttributes(TOPIC, "us-east-1")).thenReturn(Map.of("FifoTopic", "true"));
assertEquals("SNSInvalidTopic", code(() -> create("fifo", null, List.of(), List.of(), null)));
assertTrue(service.describeSubscriptions(null, null, null, null, null).items().isEmpty());
}
@Test
void permissionFailuresUseRedshiftErrorsAndStatus() {
doThrow(new AwsException("AuthorizationError", "denied", 403))
.when(sns).getTopicAttributes(TOPIC, "us-east-1");
assertEquals("SNSNoAuthorization", code(() -> create("denied", null, List.of(), List.of(), null)));
}
@Test
void lostPublishPermissionMarksSubscriptionWithoutFailingOperation() {
create("watcher", null, List.of(), List.of(), null);
doThrow(new AwsException("AuthorizationError", "denied", 403))
.when(sns).publish(anyString(), any(), anyString(), anyString(), anyString());
service.record(RedshiftEventCatalog.CLUSTER_CREATED, "warehouse", "created");
assertEquals("no-permission", service.describeSubscriptions("watcher", null, null, null, null)
.items().getFirst().getStatus());
assertEquals(1, service.describeEvents(null, null, null, null, null, null, null).items().size());
}
@Test
void repeatingSourceTypePreservesSourceIds() {
create("watcher", "cluster", List.of("warehouse"), List.of(), null);
EventSubscription modified = service.modifySubscription("watcher", null, "cluster", null, null, null,
false, ALL_SOURCES_EXIST);
assertEquals(List.of("warehouse"), modified.getSourceIdsList());
}
@Test
void disablingSubscriptionDoesNotRequireItsDeletedSource() {
create("watcher", "cluster", List.of("warehouse"), List.of(), null);
EventSubscription modified = service.modifySubscription("watcher", null, null, null, null, null,
false, (type, id) -> false);
assertFalse(modified.isEnabled());
}
@Test
void tagKeyAndValueMustMatchTheSameTag() {
service.createSubscription("wrong-value", TOPIC, null, List.of(), List.of(), null, null,
Map.of("team", "other"), ALL_SOURCES_EXIST);
service.createSubscription("wrong-key", TOPIC, null, List.of(), List.of(), null, null,
Map.of("owner", "data"), ALL_SOURCES_EXIST);
service.createSubscription("matching", TOPIC, null, List.of(), List.of(), null, null,
Map.of("team", "data"), ALL_SOURCES_EXIST);
assertEquals(List.of("matching"), names(service.describeSubscriptions(null,
List.of("team"), List.of("data"), null, null)));
}
@Test
void eventHistoryAndSubscriptionNamesAreRegional() {
create("watcher", null, List.of(), List.of(), null);
service.record(RedshiftEventCatalog.CLUSTER_CREATED, "warehouse", "created");
RedshiftEventService west = new RedshiftEventService(factory, sns, authorizer,
new RegionResolver("us-west-2", "000000000000"), objectMapper, clock);
assertTrue(west.describeEvents(null, null, null, null, null, null, null).items().isEmpty());
assertTrue(west.describeSubscriptions(null, null, null, null, null).items().isEmpty());
west.createSubscription("watcher", TOPIC, null, List.of(), List.of(), null, false, Map.of(),
ALL_SOURCES_EXIST);
assertTrue(service.describeSubscriptions("watcher", null, null, null, null).items().getFirst().isEnabled());
}
@Test
void aDeletedTopicMarksTheSubscriptionAndNeverFailsTheOperation() {
create("watcher", null, List.of(), List.of(), null);
doThrow(new AwsException("NotFound", "Topic does not exist.", 404))
.when(sns).publish(anyString(), any(), anyString(), anyString(), anyString());
service.record(RedshiftEventCatalog.CLUSTER_CREATED, "warehouse", "created");
assertEquals("topic-not-exist",
service.describeSubscriptions("watcher", null, null, null, null).items().getFirst().getStatus());
assertEquals(1, service.describeEvents(null, null, null, null, null, null, null).items().size());
service.record(RedshiftEventCatalog.CLUSTER_DELETED, "warehouse", "deleted");
verify(sns, times(1)).publish(anyString(), any(), anyString(), anyString(), anyString());
}
private EventSubscription create(String name, String sourceType, List<String> sourceIds, List<String> categories,
String severity) {
return service.createSubscription(name, TOPIC, sourceType, sourceIds, categories, severity, null, Map.of(),
ALL_SOURCES_EXIST);
}
private static String code(Runnable action) {
return assertThrows(AwsException.class, action::run).getErrorCode();
}
private static List<String> ids(PaginatedResult<RedshiftEvent> page) {
return page.items().stream().map(RedshiftEvent::getEventId).toList();
}
private static List<String> names(PaginatedResult<EventSubscription> page) {
return page.items().stream().map(EventSubscription::getCustSubscriptionId).toList();
}
private static final class MutableClock extends Clock {
private Instant now;
@@ -1,10 +1,15 @@
package io.github.hectorvent.floci.services.redshift;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.ServiceCallAuthorizer;
import io.github.hectorvent.floci.core.common.ServiceConfigAccess;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.Decision;
import io.github.hectorvent.floci.services.redshift.model.EventSubscription;
import io.github.hectorvent.floci.services.redshift.model.RedshiftEvent;
import io.github.hectorvent.floci.services.sns.SnsService;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
@@ -14,11 +19,20 @@ import java.time.Clock;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
class RedshiftEventStorageTest {
@@ -27,6 +41,7 @@ class RedshiftEventStorageTest {
private static final String OTHER_ACCOUNT = "222222222222";
private static final String EAST = "us-east-1";
private static final String WEST = "us-west-2";
private static final String SUBSCRIPTION = "persistent-watcher";
private static final Instant NOW = Instant.parse("2026-10-26T12:00:00Z");
@TempDir
@@ -34,14 +49,19 @@ class RedshiftEventStorageTest {
@ParameterizedTest
@ValueSource(strings = {"persistent", "hybrid", "wal"})
void historySurvivesRestart(String mode) {
void historyAndSubscriptionsSurviveRestartWithoutReplayingNotifications(String mode) {
SnsService sns = sns();
String topic = topic(ACCOUNT, EAST);
String originalKey;
try (Fixture first = fixture(mode, ACCOUNT)) {
try (Fixture first = fixture(mode, ACCOUNT, sns)) {
createSubscription(first.east(), topic);
first.east().record(RedshiftEventCatalog.CLUSTER_CREATED, "warehouse", "created before restart");
originalKey = history(first.east()).getFirst().getKey();
verify(sns).publish(eq(topic), eq(null), anyString(), anyString(), eq(EAST));
}
try (Fixture restarted = fixture(mode, ACCOUNT)) {
clearInvocations(sns);
try (Fixture restarted = fixture(mode, ACCOUNT, sns)) {
restarted.factory().loadAll();
restarted.factory().loadAll();
List<RedshiftEvent> history = history(restarted.east());
@@ -51,49 +71,86 @@ class RedshiftEventStorageTest {
assertEquals("REDSHIFT-EVENT-2000", history.getFirst().getEventId());
assertEquals("created before restart", history.getFirst().getMessage());
EventSubscription restored = restarted.east().describeSubscriptions(
SUBSCRIPTION, null, null, null, null).items().getFirst();
assertEquals(ACCOUNT, restored.getCustomerAwsId());
assertEquals(topic, restored.getSnsTopicArn());
assertEquals(NOW, restored.getSubscriptionCreationTime());
assertEquals(Map.of("team", "analytics"), restored.getTags());
assertTrue(restored.isEnabled());
assertEquals("active", restored.getStatus());
verifyNoInteractions(sns);
restarted.east().record(RedshiftEventCatalog.CLUSTER_REBOOTED, "warehouse", "rebooted after restart");
assertEquals(2, history(restarted.east()).size());
verify(sns).publish(eq(topic), eq(null), anyString(), anyString(), eq(EAST));
}
try (Fixture third = fixture(mode, ACCOUNT)) {
clearInvocations(sns);
try (Fixture third = fixture(mode, ACCOUNT, sns)) {
assertEquals(2, history(third.east()).size());
assertEquals(2, history(third.east()).stream().map(RedshiftEvent::getKey).distinct().count());
assertEquals(List.of("REDSHIFT-EVENT-2000", "REDSHIFT-EVENT-3000"), history(third.east()).stream()
.map(RedshiftEvent::getEventId).sorted().toList());
verifyNoInteractions(sns);
}
}
@ParameterizedTest
@ValueSource(strings = {"persistent", "hybrid", "wal"})
void persistedHistoryStaysWithinItsAccountAndRegion(String mode) {
try (Fixture owner = fixture(mode, ACCOUNT)) {
void persistedHistoryAndSubscriptionDeliveryStayWithinTheirAccountAndRegion(String mode) {
SnsService sns = sns();
String eastTopic = topic(ACCOUNT, EAST);
String westTopic = topic(ACCOUNT, WEST);
String otherTopic = topic(OTHER_ACCOUNT, EAST);
try (Fixture owner = fixture(mode, ACCOUNT, sns)) {
createSubscription(owner.east(), eastTopic);
createSubscription(owner.west(), westTopic);
owner.east().record(RedshiftEventCatalog.CLUSTER_CREATED, "east-cluster", "east owner");
owner.west().record(RedshiftEventCatalog.CLUSTER_CREATED, "west-cluster", "west owner");
assertEquals(List.of("east-cluster"), sourceIds(owner.east()));
assertEquals(List.of("west-cluster"), sourceIds(owner.west()));
}
try (Fixture other = fixture(mode, OTHER_ACCOUNT)) {
try (Fixture other = fixture(mode, OTHER_ACCOUNT, sns)) {
assertTrue(history(other.east()).isEmpty());
assertTrue(history(other.west()).isEmpty());
assertTrue(other.east().describeSubscriptions(null, null, null, null, null).items().isEmpty());
createSubscription(other.east(), otherTopic);
other.east().record(RedshiftEventCatalog.CLUSTER_CREATED, "other-cluster", "other owner");
}
try (Fixture restored = fixture(mode, ACCOUNT)) {
clearInvocations(sns);
try (Fixture restored = fixture(mode, ACCOUNT, sns)) {
assertEquals(List.of("east-cluster"), sourceIds(restored.east()));
assertEquals(List.of("west-cluster"), sourceIds(restored.west()));
assertEquals(eastTopic, restored.east().describeSubscriptions(SUBSCRIPTION, null, null, null, null)
.items().getFirst().getSnsTopicArn());
assertEquals(westTopic, restored.west().describeSubscriptions(SUBSCRIPTION, null, null, null, null)
.items().getFirst().getSnsTopicArn());
restored.east().record(RedshiftEventCatalog.CLUSTER_REBOOTED, "east-cluster", "owner reboot");
assertEquals(2, history(restored.east()).size());
verify(sns, times(1)).publish(anyString(), any(), anyString(), anyString(), anyString());
verify(sns).publish(eq(eastTopic), eq(null), anyString(), anyString(), eq(EAST));
assertEquals(1, history(restored.west()).size());
}
try (Fixture otherRestored = fixture(mode, OTHER_ACCOUNT)) {
try (Fixture otherRestored = fixture(mode, OTHER_ACCOUNT, sns)) {
assertEquals(List.of("other-cluster"), sourceIds(otherRestored.east()));
assertTrue(history(otherRestored.west()).isEmpty());
EventSubscription subscription = otherRestored.east().describeSubscriptions(
SUBSCRIPTION, null, null, null, null).items().getFirst();
assertEquals(OTHER_ACCOUNT, subscription.getCustomerAwsId());
assertEquals(otherTopic, subscription.getSnsTopicArn());
otherRestored.east().modifySubscription(SUBSCRIPTION, null, null, null, null,
null, false, (type, id) -> true);
}
try (Fixture otherDisabled = fixture(mode, OTHER_ACCOUNT, sns)) {
assertFalse(otherDisabled.east().describeSubscriptions(SUBSCRIPTION, null, null, null, null)
.items().getFirst().isEnabled());
}
}
private Fixture fixture(String mode, String account) {
private Fixture fixture(String mode, String account, SnsService sns) {
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
when(config.defaultAccountId()).thenReturn(account);
when(config.storage().persistentPath()).thenReturn(directory.toString());
@@ -102,12 +159,32 @@ class RedshiftEventStorageTest {
when(access.storageMode("redshift")).thenReturn(mode);
when(access.storageFlushInterval("redshift")).thenReturn(60_000L);
StorageFactory factory = new StorageFactory(config, access);
ObjectMapper mapper = new ObjectMapper();
Clock clock = Clock.fixed(NOW, ZoneOffset.UTC);
RedshiftEventService east = new RedshiftEventService(factory, new RegionResolver(EAST, account), clock);
RedshiftEventService west = new RedshiftEventService(factory, new RegionResolver(WEST, account), clock);
RedshiftEventService east = new RedshiftEventService(factory, sns, allowAll(), new RegionResolver(EAST, account), mapper, clock);
RedshiftEventService west = new RedshiftEventService(factory, sns, allowAll(), new RegionResolver(WEST, account), mapper, clock);
return new Fixture(factory, east, west);
}
private static ServiceCallAuthorizer allowAll() {
ServiceCallAuthorizer authorizer = mock(ServiceCallAuthorizer.class);
when(authorizer.authorize(anyString(), anyString(), anyString(), anyString(), anyString()))
.thenReturn(Decision.ALLOW);
return authorizer;
}
private static SnsService sns() {
SnsService sns = mock(SnsService.class);
when(sns.topicExists(anyString(), anyString())).thenReturn(true);
when(sns.getTopicAttributes(anyString(), anyString())).thenReturn(Map.of());
return sns;
}
private static void createSubscription(RedshiftEventService service, String topic) {
service.createSubscription(SUBSCRIPTION, topic, null, List.of(), List.of(), null, true,
Map.of("team", "analytics"), (type, id) -> true);
}
private static List<RedshiftEvent> history(RedshiftEventService service) {
return service.describeEvents(null, null, null, null, null, null, null).items();
}
@@ -116,6 +193,10 @@ class RedshiftEventStorageTest {
return history(service).stream().map(RedshiftEvent::getSourceIdentifier).toList();
}
private static String topic(String account, String region) {
return "arn:aws:sns:" + region + ":" + account + ":redshift-events";
}
private record Fixture(StorageFactory factory, RedshiftEventService east,
RedshiftEventService west) implements AutoCloseable {
@Override
@@ -10,16 +10,28 @@ import software.amazon.awssdk.services.redshift.RedshiftClient;
import software.amazon.awssdk.services.redshift.model.DescribeEventsResponse;
import software.amazon.awssdk.services.redshift.model.Event;
import software.amazon.awssdk.services.redshift.model.EventCategoriesMap;
import software.amazon.awssdk.services.redshift.model.EventSubscription;
import software.amazon.awssdk.services.redshift.model.Parameter;
import software.amazon.awssdk.services.redshift.model.RedshiftException;
import software.amazon.awssdk.services.redshift.model.Tag;
import software.amazon.awssdk.services.redshift.model.TaggedResource;
import software.amazon.awssdk.services.sns.SnsClient;
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.net.URI;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -32,6 +44,59 @@ class RedshiftEventsIntegrationTest {
@TestHTTPResource
URI endpoint;
@Test
void sdkSubscriptionLifecyclePreservesNamedListsTagsAndTimestamps() {
String name = name();
try (RedshiftClient redshift = redshift(Region.US_EAST_1); SnsClient sns = sns(Region.US_EAST_1)) {
String topic = sns.createTopic(request -> request.name(name)).topicArn();
createGroup(redshift, name);
try {
EventSubscription created = redshift.createEventSubscription(request -> request
.subscriptionName(name).snsTopicArn(topic).sourceType(SOURCE_TYPE)
.sourceIds(name).eventCategories("configuration").severity("INFO")
.tags(Tag.builder().key("team").value("analytics").build())).eventSubscription();
try {
assertEquals(name, created.custSubscriptionId());
assertEquals(ACCOUNT, created.customerAwsId());
assertEquals(SOURCE_TYPE, created.sourceType());
assertEquals(List.of(name), created.sourceIdsList());
assertEquals(List.of("configuration"), created.eventCategoriesList());
assertEquals("INFO", created.severity());
assertEquals("active", created.status());
assertTrue(created.enabled());
assertNotNull(created.subscriptionCreationTime());
assertEquals("team", created.tags().getFirst().key());
assertEquals("analytics", created.tags().getFirst().value());
List<EventSubscription> described = redshift.describeEventSubscriptions(request -> request
.subscriptionName(name)).eventSubscriptionsList();
assertEquals(1, described.size());
assertEquals(created.subscriptionCreationTime(), described.getFirst().subscriptionCreationTime());
assertEquals(List.of(name), redshift.describeEventSubscriptions(request -> request
.tagKeys("team").tagValues("analytics")).eventSubscriptionsList().stream()
.map(EventSubscription::custSubscriptionId).filter(name::equals).toList());
EventSubscription disabled = redshift.modifyEventSubscription(request -> request
.subscriptionName(name).enabled(false)).eventSubscription();
assertFalse(disabled.enabled());
assertEquals(created.subscriptionCreationTime(), disabled.subscriptionCreationTime());
assertEquals(List.of(name), disabled.sourceIdsList());
assertEquals(List.of("configuration"), disabled.eventCategoriesList());
assertEquals("INFO", disabled.severity());
} finally {
redshift.deleteEventSubscription(request -> request.subscriptionName(name));
}
RedshiftException missing = assertThrows(RedshiftException.class,
() -> redshift.describeEventSubscriptions(request -> request.subscriptionName(name)));
assertEquals("SubscriptionNotFound", missing.awsErrorDetails().errorCode());
assertEquals(404, missing.statusCode());
} finally {
redshift.deleteClusterParameterGroup(request -> request.parameterGroupName(name));
sns.deleteTopic(request -> request.topicArn(topic));
}
}
}
@Test
void sdkDescribesRealParameterGroupEventsAndCategoryMetadata() {
String name = name();
@@ -71,57 +136,196 @@ class RedshiftEventsIntegrationTest {
}
@Test
void sdkReceivesAwsErrorsForAnUnpairedSourceAndOutOfRangePages() {
void sdkSubscriptionDeliversMatchingEventsThroughSnsToSqsAndStopsWhenDisabled() {
String name = name();
try (RedshiftClient redshift = redshift(Region.US_EAST_1)) {
RedshiftException unpaired = assertThrows(RedshiftException.class, () -> redshift.describeEvents(request ->
request.sourceIdentifier(name)));
assertEquals("InvalidParameterCombination", unpaired.awsErrorDetails().errorCode());
RedshiftException pageSize = assertThrows(RedshiftException.class, () -> redshift.describeEvents(request ->
request.maxRecords(19)));
assertEquals("InvalidParameterValue", pageSize.awsErrorDetails().errorCode());
}
}
@Test
void historyIsIsolatedByRegion() {
String name = name();
try (RedshiftClient east = redshift(Region.US_EAST_1); RedshiftClient west = redshift(Region.US_WEST_2)) {
createGroup(east, name);
try (RedshiftClient redshift = redshift(Region.US_EAST_1); SnsClient sns = sns(Region.US_EAST_1);
SqsClient sqs = sqs()) {
String topic = sns.createTopic(request -> request.name(name)).topicArn();
String queue = sqs.createQueue(request -> request.queueName(name)).queueUrl();
String queueArn = sqs.getQueueAttributes(request -> request.queueUrl(queue)
.attributeNames(QueueAttributeName.QUEUE_ARN)).attributes().get(QueueAttributeName.QUEUE_ARN);
String snsSubscription = sns.subscribe(request -> request.topicArn(topic).protocol("sqs")
.endpoint(queueArn).attributes(Map.of("RawMessageDelivery", "true"))).subscriptionArn();
redshift.createEventSubscription(request -> request.subscriptionName(name).snsTopicArn(topic)
.sourceType(SOURCE_TYPE).eventCategories("configuration").severity("INFO"));
try {
assertEquals(1, east.describeEvents(request -> request.sourceIdentifier(name).sourceType(SOURCE_TYPE))
.events().size());
assertTrue(west.describeEvents(request -> request.sourceIdentifier(name).sourceType(SOURCE_TYPE))
.events().isEmpty());
createGroup(redshift, name);
try {
AtomicReference<Message> received = new AtomicReference<>();
await().atMost(Duration.ofSeconds(10)).until(() -> {
List<Message> messages = sqs.receiveMessage(request -> request.queueUrl(queue)
.maxNumberOfMessages(1).waitTimeSeconds(0)).messages();
if (messages.isEmpty()) {
return false;
}
received.set(messages.getFirst());
return true;
});
assertTrue(received.get().body().contains("REDSHIFT-EVENT-1003"));
assertTrue(received.get().body().contains(name));
sqs.deleteMessage(request -> request.queueUrl(queue).receiptHandle(received.get().receiptHandle()));
redshift.modifyEventSubscription(request -> request.subscriptionName(name).enabled(false));
redshift.modifyClusterParameterGroup(request -> request.parameterGroupName(name)
.parameters(Parameter.builder().parameterName("statement_timeout").parameterValue("5001").build()));
assertTrue(sqs.receiveMessage(request -> request.queueUrl(queue).waitTimeSeconds(1)).messages().isEmpty());
} finally {
redshift.deleteClusterParameterGroup(request -> request.parameterGroupName(name));
}
} finally {
east.deleteClusterParameterGroup(request -> request.parameterGroupName(name));
redshift.deleteEventSubscription(request -> request.subscriptionName(name));
sns.unsubscribe(request -> request.subscriptionArn(snsSubscription));
sns.deleteTopic(request -> request.topicArn(topic));
sqs.deleteQueue(request -> request.queueUrl(queue));
}
}
}
@Test
void historyIsIsolatedByAccount() {
void sdkReceivesAwsErrorsForInvalidFiltersAndFifoTopics() {
String name = name();
String otherAccount = "414141414141";
try (RedshiftClient owner = redshift(Region.US_EAST_1, ACCOUNT);
RedshiftClient other = redshift(Region.US_EAST_1, otherAccount)) {
createGroup(owner, name);
try (RedshiftClient redshift = redshift(Region.US_EAST_1); SnsClient sns = sns(Region.US_EAST_1)) {
String topic = sns.createTopic(request -> request.name(name)).topicArn();
String fifo = sns.createTopic(request -> request.name(name + ".fifo")
.attributes(Map.of("FifoTopic", "true"))).topicArn();
try {
assertEquals(1, owner.describeEvents(request -> request.sourceIdentifier(name).sourceType(SOURCE_TYPE))
.events().size());
assertTrue(other.describeEvents(request -> request.sourceIdentifier(name).sourceType(SOURCE_TYPE))
.events().isEmpty());
createGroup(other, name);
RedshiftException unpaired = assertThrows(RedshiftException.class, () -> redshift.describeEvents(request ->
request.sourceIdentifier(name)));
assertEquals("InvalidParameterCombination", unpaired.awsErrorDetails().errorCode());
RedshiftException severity = assertThrows(RedshiftException.class, () -> redshift.createEventSubscription(
request -> request.subscriptionName(name).snsTopicArn(topic).severity("WARN")));
assertEquals("SubscriptionSeverityNotFound", severity.awsErrorDetails().errorCode());
RedshiftException fifoError = assertThrows(RedshiftException.class, () -> redshift.createEventSubscription(
request -> request.subscriptionName(name).snsTopicArn(fifo)));
assertEquals("SNSInvalidTopic", fifoError.awsErrorDetails().errorCode());
} finally {
sns.deleteTopic(request -> request.topicArn(topic));
sns.deleteTopic(request -> request.topicArn(fifo));
}
}
}
@Test
void subscriptionNamesAndHistoryAreIsolatedByRegion() {
String name = name();
try (RedshiftClient east = redshift(Region.US_EAST_1); RedshiftClient west = redshift(Region.US_WEST_2);
SnsClient eastSns = sns(Region.US_EAST_1); SnsClient westSns = sns(Region.US_WEST_2)) {
String eastTopic = eastSns.createTopic(request -> request.name(name)).topicArn();
String westTopic = westSns.createTopic(request -> request.name(name)).topicArn();
east.createEventSubscription(request -> request.subscriptionName(name).snsTopicArn(eastTopic));
try {
west.createEventSubscription(request -> request.subscriptionName(name).snsTopicArn(westTopic));
try {
assertEquals(1, other.describeEvents(request -> request.sourceIdentifier(name).sourceType(SOURCE_TYPE))
.events().size());
assertEquals(1, owner.describeEvents(request -> request.sourceIdentifier(name).sourceType(SOURCE_TYPE))
.events().size());
assertEquals(eastTopic, east.describeEventSubscriptions(request -> request.subscriptionName(name))
.eventSubscriptionsList().getFirst().snsTopicArn());
assertEquals(westTopic, west.describeEventSubscriptions(request -> request.subscriptionName(name))
.eventSubscriptionsList().getFirst().snsTopicArn());
createGroup(east, name);
try {
assertTrue(west.describeEvents(request -> request.sourceIdentifier(name).sourceType(SOURCE_TYPE))
.events().isEmpty());
} finally {
east.deleteClusterParameterGroup(request -> request.parameterGroupName(name));
}
} finally {
other.deleteClusterParameterGroup(request -> request.parameterGroupName(name));
west.deleteEventSubscription(request -> request.subscriptionName(name));
}
} finally {
owner.deleteClusterParameterGroup(request -> request.parameterGroupName(name));
east.deleteEventSubscription(request -> request.subscriptionName(name));
eastSns.deleteTopic(request -> request.topicArn(eastTopic));
westSns.deleteTopic(request -> request.topicArn(westTopic));
}
}
}
@Test
void sdkTagsSubscriptionArnAndSubscriptionDescriptionsReflectChanges() {
String name = name();
String arn = "arn:aws:redshift:us-east-1:" + ACCOUNT + ":eventsubscription:" + name;
Tag original = Tag.builder().key("team").value("analytics").build();
Tag updated = Tag.builder().key("team").value("platform").build();
Tag environment = Tag.builder().key("environment").value("test").build();
try (RedshiftClient redshift = redshift(Region.US_EAST_1); SnsClient sns = sns(Region.US_EAST_1)) {
String topic = sns.createTopic(request -> request.name(name)).topicArn();
try {
redshift.createEventSubscription(request -> request.subscriptionName(name).snsTopicArn(topic).tags(original));
try {
List<TaggedResource> initial = redshift.describeTags(request -> request.resourceName(arn)).taggedResources();
assertEquals(1, initial.size());
assertEquals(arn, initial.getFirst().resourceName());
assertEquals("eventsubscription", initial.getFirst().resourceType());
assertEquals(original, initial.getFirst().tag());
redshift.createTags(request -> request.resourceName(arn).tags(updated, environment));
List<Tag> described = redshift.describeTags(request -> request.resourceName(arn)).taggedResources()
.stream().map(TaggedResource::tag).toList();
assertEquals(2, described.size());
assertTrue(described.containsAll(List.of(updated, environment)));
List<Tag> subscriptionTags = redshift.describeEventSubscriptions(request -> request.subscriptionName(name))
.eventSubscriptionsList().getFirst().tags();
assertEquals(2, subscriptionTags.size());
assertTrue(subscriptionTags.containsAll(List.of(updated, environment)));
redshift.deleteTags(request -> request.resourceName(arn).tagKeys("team"));
assertEquals(List.of(environment), redshift.describeTags(request -> request.resourceName(arn))
.taggedResources().stream().map(TaggedResource::tag).toList());
assertEquals(List.of(environment), redshift.describeEventSubscriptions(request -> request.subscriptionName(name))
.eventSubscriptionsList().getFirst().tags());
} finally {
redshift.deleteEventSubscription(request -> request.subscriptionName(name));
}
} finally {
sns.deleteTopic(request -> request.topicArn(topic));
}
}
}
@Test
void subscriptionNamesAndHistoryAreIsolatedByAccount() {
String name = name();
String otherAccount = "414141414141";
try (RedshiftClient owner = redshift(Region.US_EAST_1); RedshiftClient other = redshift(Region.US_EAST_1, otherAccount);
SnsClient ownerSns = sns(Region.US_EAST_1); SnsClient otherSns = sns(Region.US_EAST_1, otherAccount)) {
String ownerTopic = ownerSns.createTopic(request -> request.name(name)).topicArn();
String otherTopic = otherSns.createTopic(request -> request.name(name)).topicArn();
try {
owner.createEventSubscription(request -> request.subscriptionName(name).snsTopicArn(ownerTopic));
try {
assertTrue(other.describeEventSubscriptions().eventSubscriptionsList().stream()
.noneMatch(subscription -> name.equals(subscription.custSubscriptionId())));
other.createEventSubscription(request -> request.subscriptionName(name).snsTopicArn(otherTopic));
try {
assertEquals(ACCOUNT, owner.describeEventSubscriptions(request -> request.subscriptionName(name))
.eventSubscriptionsList().getFirst().customerAwsId());
assertEquals(otherAccount, other.describeEventSubscriptions(request -> request.subscriptionName(name))
.eventSubscriptionsList().getFirst().customerAwsId());
createGroup(owner, name);
try {
assertEquals(1, owner.describeEvents(request -> request.sourceIdentifier(name).sourceType(SOURCE_TYPE))
.events().size());
assertTrue(other.describeEvents(request -> request.sourceIdentifier(name).sourceType(SOURCE_TYPE))
.events().isEmpty());
createGroup(other, name);
try {
assertEquals(1, other.describeEvents(request -> request.sourceIdentifier(name).sourceType(SOURCE_TYPE))
.events().size());
assertEquals(1, owner.describeEvents(request -> request.sourceIdentifier(name).sourceType(SOURCE_TYPE))
.events().size());
} finally {
other.deleteClusterParameterGroup(request -> request.parameterGroupName(name));
}
} finally {
owner.deleteClusterParameterGroup(request -> request.parameterGroupName(name));
}
} finally {
other.deleteEventSubscription(request -> request.subscriptionName(name));
}
} finally {
owner.deleteEventSubscription(request -> request.subscriptionName(name));
}
} finally {
ownerSns.deleteTopic(request -> request.topicArn(ownerTopic));
otherSns.deleteTopic(request -> request.topicArn(otherTopic));
}
}
}
@@ -132,8 +336,29 @@ class RedshiftEventsIntegrationTest {
private RedshiftClient redshift(Region region, String account) {
return RedshiftClient.builder().endpointOverride(endpoint).region(region)
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(account, "test")))
.build();
.credentialsProvider(credentials(account)).build();
}
private SnsClient sns(Region region) {
return sns(region, ACCOUNT);
}
private SnsClient sns(Region region, String account) {
return SnsClient.builder().endpointOverride(endpoint).region(region)
.credentialsProvider(credentials(account)).build();
}
private SqsClient sqs() {
return SqsClient.builder().endpointOverride(endpoint).region(Region.US_EAST_1)
.credentialsProvider(credentials()).build();
}
private static StaticCredentialsProvider credentials() {
return credentials(ACCOUNT);
}
private static StaticCredentialsProvider credentials(String account) {
return StaticCredentialsProvider.create(AwsBasicCredentials.create(account, "test"));
}
private static String name() {
@@ -3,12 +3,14 @@ package io.github.hectorvent.floci.services.redshift;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsNamespaces;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.services.redshift.model.Cluster;
import io.github.hectorvent.floci.services.redshift.model.ClusterParameterGroup;
import io.github.hectorvent.floci.services.redshift.model.ClusterSubnetGroup;
import io.github.hectorvent.floci.services.redshift.model.Endpoint;
import io.github.hectorvent.floci.services.redshift.model.EventSubscription;
import io.github.hectorvent.floci.services.redshift.model.Parameter;
import io.github.hectorvent.floci.services.redshift.model.Snapshot;
import io.github.hectorvent.floci.services.redshift.model.SnapshotCopyGrant;
@@ -33,6 +35,18 @@ import static org.mockito.Mockito.*;
class RedshiftQueryHandlerTest {
@Test
void eventSubscriptionResponsesUseTheRedshiftNamespace() {
EventSubscription subscription = new EventSubscription();
subscription.setCustSubscriptionId("watcher");
subscription.setSubscriptionCreationTime(Instant.parse("2026-10-26T12:00:00Z"));
when(service.createEventSubscription(any(), any(), any(), any(), any(), any(), any(), any()))
.thenReturn(subscription);
Response response = handler.handle("CreateEventSubscription", new MultivaluedHashMap<>());
assertTrue(response.getEntity().toString().contains(
"<CreateEventSubscriptionResponse xmlns=\"" + AwsNamespaces.REDSHIFT + "\">"));
}
@Test
void nonFiniteEventTimestampsReturnAnAwsValidationError() {
for (String invalid : List.of("NaN", "Infinity", "-Infinity", "1e300")) {
@@ -2435,6 +2435,14 @@ class RedshiftServiceTest {
service.createTags("arn:aws:redshift:us-east-1:111111111111:reservednode:foo", Map.of("env", "test")));
}
@Test
void describeTagsRejectsEventSubscriptionArnWithoutName() {
AwsException e = assertThrows(AwsException.class, () ->
service.describeTags("arn:aws:redshift:us-east-1:111111111111:eventsubscription:", null, null));
assertEquals("InvalidParameterValue", e.getErrorCode());
}
@Test
void describeTagsForSpecificResource() {
Cluster cluster = new Cluster();
@@ -0,0 +1,185 @@
package io.github.hectorvent.floci.services.redshift;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.redshift.container.RedshiftContainerHandle;
import io.github.hectorvent.floci.services.redshift.container.RedshiftContainerManager;
import io.github.hectorvent.floci.services.redshift.model.Cluster;
import io.github.hectorvent.floci.services.redshift.proxy.RedshiftProxyManager;
import io.quarkus.test.InjectMock;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.redshift.RedshiftClient;
import software.amazon.awssdk.services.redshift.model.Event;
import software.amazon.awssdk.services.sns.SnsClient;
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.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@QuarkusTest
class RedshiftSnapshotEventsIntegrationTest {
private static final String OWNER = "565656565656";
private static final String DEFAULT_ACCOUNT = "000000000000";
@InjectMock
RedshiftContainerManager manager;
@InjectMock
RedshiftProxyManager proxy;
@Inject
RedshiftSnapshotScheduler scheduler;
@Inject
StorageFactory factory;
@TestHTTPResource
URI endpoint;
@ParameterizedTest
@ValueSource(booleans = {false, true})
void backgroundSnapshotHistoryAndNotificationsUseTheResourceAccountAndRegion(boolean snapshotFails) throws Exception {
String name = "sdk-snapshot-events-" + UUID.randomUUID().toString().substring(0, 8);
String clusterId = name + "-cluster";
String scheduleId = name + "-schedule";
String terminalEvent = snapshotFails ? "REDSHIFT-EVENT-3503" : "REDSHIFT-EVENT-3004";
when(manager.start(anyString(), anyString(), anyString(), anyString(), any(), any()))
.thenReturn(new RedshiftContainerHandle("mock", clusterId, "localhost", 5432));
doAnswer(invocation -> {
if (snapshotFails) {
throw new IOException("snapshot failed for " + clusterId);
}
Files.writeString(invocation.getArgument(3, Path.class), "backup contents");
return null;
}).when(manager).takeSnapshot(anyString(), anyString(), anyString(), any(Path.class));
try (Scope ownerWest = scope(OWNER, Region.US_WEST_2);
Scope fallback = scope(DEFAULT_ACCOUNT, Region.US_EAST_1);
Scope ownerEast = scope(OWNER, Region.US_EAST_1);
Watcher westWatcher = watcher(ownerWest, name);
Watcher fallbackWatcher = watcher(fallback, name);
Watcher eastWatcher = watcher(ownerEast, name)) {
ownerWest.redshift().createSnapshotSchedule(request -> request.scheduleIdentifier(scheduleId)
.scheduleDefinitions("rate(1 hour)"));
try {
ownerWest.redshift().createCluster(request -> request.clusterIdentifier(clusterId).nodeType("dc2.large")
.masterUsername("admin").masterUserPassword("Secret123").automatedSnapshotRetentionPeriod(1));
try {
ownerWest.redshift().modifyClusterSnapshotSchedule(request -> request.clusterIdentifier(clusterId)
.scheduleIdentifier(scheduleId));
AccountAwareStorageBackend<Cluster> clusters = factory.create("redshift", "redshift-clusters.json",
new TypeReference<Map<String, Cluster>>() { });
Instant due = clusters.getForAccount(OWNER, clusterId).orElseThrow()
.getAutomatedSnapshotAnchor().plusSeconds(3600);
CompletableFuture.runAsync(() -> scheduler.tick(due)).get(20, TimeUnit.SECONDS);
List<Event> history = snapshotEvents(ownerWest.redshift(), clusterId);
assertEquals(List.of("REDSHIFT-EVENT-2009", terminalEvent), history.stream().map(Event::eventId).toList());
assertTrue(snapshotEvents(fallback.redshift(), clusterId).isEmpty(),
"Background events must not be stored under the default account");
assertTrue(snapshotEvents(ownerEast.redshift(), clusterId).isEmpty(),
"Background events must not use the default region within the resource account");
List<Message> delivered = receiveTwo(ownerWest.sqs(), westWatcher.queueUrl());
assertEquals(2, delivered.size());
assertTrue(delivered.stream().allMatch(message -> message.body().contains(clusterId)));
assertTrue(delivered.stream().anyMatch(message -> message.body().contains("REDSHIFT-EVENT-2009")));
assertTrue(delivered.stream().anyMatch(message -> message.body().contains(terminalEvent)));
assertTrue(fallback.sqs().receiveMessage(request -> request.queueUrl(fallbackWatcher.queueUrl())
.waitTimeSeconds(0)).messages().isEmpty(), "Default-account subscription received a foreign event");
assertTrue(ownerEast.sqs().receiveMessage(request -> request.queueUrl(eastWatcher.queueUrl())
.waitTimeSeconds(0)).messages().isEmpty(), "Another region's subscription received the snapshot event");
verify(manager).takeSnapshot(eq(OWNER), eq(clusterId), eq("admin"), any(Path.class));
} finally {
ownerWest.redshift().deleteCluster(request -> request.clusterIdentifier(clusterId).skipFinalClusterSnapshot(true));
}
} finally {
ownerWest.redshift().deleteSnapshotSchedule(request -> request.scheduleIdentifier(scheduleId));
}
}
}
private Scope scope(String account, Region region) {
StaticCredentialsProvider credentials = StaticCredentialsProvider.create(AwsBasicCredentials.create(account, "test"));
RedshiftClient redshift = RedshiftClient.builder().endpointOverride(endpoint).region(region)
.credentialsProvider(credentials).build();
SnsClient sns = SnsClient.builder().endpointOverride(endpoint).region(region).credentialsProvider(credentials).build();
SqsClient sqs = SqsClient.builder().endpointOverride(endpoint).region(region).credentialsProvider(credentials).build();
return new Scope(redshift, sns, sqs);
}
private static Watcher watcher(Scope scope, String name) {
String topic = scope.sns().createTopic(request -> request.name(name)).topicArn();
String queue = scope.sqs().createQueue(request -> request.queueName(name)).queueUrl();
String queueArn = scope.sqs().getQueueAttributes(request -> request.queueUrl(queue)
.attributeNames(QueueAttributeName.QUEUE_ARN)).attributes().get(QueueAttributeName.QUEUE_ARN);
String snsSubscription = scope.sns().subscribe(request -> request.topicArn(topic).protocol("sqs")
.endpoint(queueArn).attributes(Map.of("RawMessageDelivery", "true"))).subscriptionArn();
scope.redshift().createEventSubscription(request -> request.subscriptionName(name).snsTopicArn(topic)
.sourceType("cluster-snapshot"));
return new Watcher(scope, name, topic, queue, snsSubscription);
}
private static List<Event> snapshotEvents(RedshiftClient client, String clusterId) {
return client.describeEvents().events().stream()
.filter(event -> "cluster-snapshot".equals(event.sourceTypeAsString()) && event.message().contains(clusterId))
.toList();
}
private static List<Message> receiveTwo(SqsClient sqs, String queue) {
List<Message> received = new ArrayList<>();
await().atMost(Duration.ofSeconds(10)).until(() -> {
received.addAll(sqs.receiveMessage(request -> request.queueUrl(queue).maxNumberOfMessages(10)
.waitTimeSeconds(0)).messages());
return received.size() >= 2;
});
return received;
}
private record Scope(RedshiftClient redshift, SnsClient sns, SqsClient sqs) implements AutoCloseable {
@Override
public void close() {
redshift.close();
sns.close();
sqs.close();
}
}
private record Watcher(Scope scope, String name, String topicArn,
String queueUrl, String snsSubscription) implements AutoCloseable {
@Override
public void close() {
scope.redshift().deleteEventSubscription(request -> request.subscriptionName(name));
scope.sns().unsubscribe(request -> request.subscriptionArn(snsSubscription));
scope.sns().deleteTopic(request -> request.topicArn(topicArn));
scope.sqs().deleteQueue(request -> request.queueUrl(queueUrl));
}
}
}
+1 -1
View File
@@ -322,7 +322,7 @@ services:
# SubnetIds / VpcSecurityGroupIds / IamRoles / AddIamRoles / RemoveIamRoles / TagKeys / TagValues /
# DbGroups / ScheduleDefinitions are labels of an inner member-name switch (memberKeyRegex), not
# actions, same shape as the rds exclusions above.
exclude_actions: [SubnetIds, VpcSecurityGroupIds, IamRoles, AddIamRoles, RemoveIamRoles, TagKeys, TagValues, DbGroups, ScheduleDefinitions]
exclude_actions: [SubnetIds, VpcSecurityGroupIds, IamRoles, AddIamRoles, RemoveIamRoles, TagKeys, TagValues, DbGroups, ScheduleDefinitions, SourceIds, EventCategories]
- service: redshiftdata
doc: docs/services/redshift-data.md
sources: