diff --git a/docs/services/rds.md b/docs/services/rds.md index 1e6a724761..35e92cc4e4 100644 --- a/docs/services/rds.md +++ b/docs/services/rds.md @@ -33,6 +33,8 @@ RDS Data API (`rds-data`) is documented separately because it uses REST JSON rou | `DescribeEventSubscriptions` | List subscriptions, or the one the request names | | `ModifyEventSubscription` | Update the members the request names | | `DeleteEventSubscription` | Delete a subscription | +| `AddSourceIdentifierToSubscription` | Add a source id to a subscription | +| `RemoveSourceIdentifierFromSubscription` | Remove a source id from a subscription | | `CreateDBSubnetGroup` | Create a DB subnet group; tags given here are readable through `ListTagsForResource` | | `DescribeDBSubnetGroups` | List DB subnet groups | | `ModifyDBSubnetGroup` | Update DB subnet group description and subnet list | @@ -145,6 +147,10 @@ The four subscription actions manage the resource itself. **Nothing is published A subscription is stored and reported back so a client can manage it. No RDS event reaches SNS through it, so an SNS subscriber sees nothing. +`SourceIds` is set at create and changed only through `AddSourceIdentifierToSubscription` and +`RemoveSourceIdentifierFromSubscription`, since `ModifyEventSubscription` carries no `SourceIds` +member. Adding an id twice is a no-op, because the model declares no fault that fits a duplicate. + `SnsTopicArn` is required and is not resolved against the SNS service. `SourceType` is checked against the model's valid values, and a request naming `SourceIds` must also name the `SourceType` they belong to, as the model requires. An omitted `Enabled` activates the subscription. diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java index 2807553467..742c0bd297 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java @@ -95,6 +95,10 @@ public class RdsQueryHandler { case "DescribeEventSubscriptions" -> handleDescribeEventSubscriptions(params, region); case "ModifyEventSubscription" -> handleModifyEventSubscription(params, region); case "DeleteEventSubscription" -> handleDeleteEventSubscription(params, region); + case "AddSourceIdentifierToSubscription" -> + handleAddSourceIdentifierToSubscription(params, region); + case "RemoveSourceIdentifierFromSubscription" -> + handleRemoveSourceIdentifierFromSubscription(params, region); case "CreateDBSubnetGroup" -> handleCreateDbSubnetGroup(params, region); case "DescribeDBSubnetGroups" -> handleDescribeDbSubnetGroups(params, region); case "ModifyDBSubnetGroup" -> handleModifyDbSubnetGroup(params, region); @@ -308,6 +312,22 @@ public class RdsQueryHandler { new XmlBuilder().raw(eventSubscriptionXml(subscription)).build())).build(); } + private Response handleAddSourceIdentifierToSubscription( + MultivaluedMap params, String region) { + EventSubscription subscription = service.addSourceIdentifierToSubscription(region, + params.getFirst("SubscriptionName"), params.getFirst("SourceIdentifier")); + return Response.ok(AwsQueryResponse.envelope("AddSourceIdentifierToSubscription", + AwsNamespaces.RDS, new XmlBuilder().raw(eventSubscriptionXml(subscription)).build())).build(); + } + + private Response handleRemoveSourceIdentifierFromSubscription( + MultivaluedMap params, String region) { + EventSubscription subscription = service.removeSourceIdentifierFromSubscription(region, + params.getFirst("SubscriptionName"), params.getFirst("SourceIdentifier")); + return Response.ok(AwsQueryResponse.envelope("RemoveSourceIdentifierFromSubscription", + AwsNamespaces.RDS, new XmlBuilder().raw(eventSubscriptionXml(subscription)).build())).build(); + } + private Response handleDeleteEventSubscription(MultivaluedMap params, String region) { EventSubscription subscription = service.deleteEventSubscription(region, params.getFirst("SubscriptionName")); diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java index 65dbbfe465..f148c0a997 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java @@ -8588,6 +8588,59 @@ public class RdsService implements Resettable, ResourceProvider { return subscription; } + /** + * AddSourceIdentifierToSubscription. The list is otherwise write-once, because + * ModifyEventSubscription carries no SourceIds member. + * + *

Adding an id the subscription already carries is a no-op rather than an error. The model + * declares only SourceNotFoundFault and SubscriptionNotFoundFault for this operation, so there + * is no fault to raise for a duplicate. + */ + public synchronized EventSubscription addSourceIdentifierToSubscription( + String region, String subscriptionName, String sourceIdentifier) { + requireSourceIdentifierRequest(subscriptionName, sourceIdentifier); + EventSubscription subscription = requireEventSubscription(region, subscriptionName); + List ids = new ArrayList<>(subscription.getSourceIdsList()); + if (!ids.contains(sourceIdentifier)) { + ids.add(sourceIdentifier); + subscription.setSourceIdsList(ids); + eventSubscriptions.put(eventSubscriptionKey(region, subscriptionName), subscription); + } + return subscription; + } + + /** + * RemoveSourceIdentifierFromSubscription. An id the subscription does not carry is + * SourceNotFound, which is the fault the model declares and the only one that fits. + */ + public synchronized EventSubscription removeSourceIdentifierFromSubscription( + String region, String subscriptionName, String sourceIdentifier) { + requireSourceIdentifierRequest(subscriptionName, sourceIdentifier); + EventSubscription subscription = requireEventSubscription(region, subscriptionName); + List ids = new ArrayList<>(subscription.getSourceIdsList()); + if (!ids.remove(sourceIdentifier)) { + throw new AwsException("SourceNotFound", + "Source " + sourceIdentifier + " not found in subscription " + subscriptionName + ".", 404); + } + subscription.setSourceIdsList(ids); + eventSubscriptions.put(eventSubscriptionKey(region, subscriptionName), subscription); + return subscription; + } + + /** + * Both members are required by the model, so both fail the same way. Letting a missing + * SubscriptionName fall through to the lookup would answer SubscriptionNotFound, which tells + * the caller the subscription does not exist when the request simply did not name one. + */ + private static void requireSourceIdentifierRequest(String subscriptionName, String sourceIdentifier) { + if (subscriptionName == null || subscriptionName.isBlank()) { + throw new AwsException("InvalidParameterValue", "SubscriptionName is required.", 400); + } + if (sourceIdentifier == null || sourceIdentifier.isBlank()) { + throw new AwsException("InvalidParameterValue", "SourceIdentifier is required.", 400); + } + } + public synchronized EventSubscription deleteEventSubscription(String region, String subscriptionName) { EventSubscription subscription = requireEventSubscription(region, subscriptionName); eventSubscriptions.delete(eventSubscriptionKey(region, subscriptionName)); diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsSourceIdentifierIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsSourceIdentifierIntegrationTest.java new file mode 100644 index 0000000000..1e220e41e4 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsSourceIdentifierIntegrationTest.java @@ -0,0 +1,124 @@ +package io.github.hectorvent.floci.services.rds; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.ValidatableResponse; +import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** AddSourceIdentifierToSubscription and RemoveSourceIdentifierFromSubscription. */ +@QuarkusTest +class RdsSourceIdentifierIntegrationTest { + + private static final String TOPIC = "arn:aws:sns:us-east-1:000000000000:rds-events"; + + private static ValidatableResponse rds(String action, String... formParams) { + RequestSpecification request = given() + .header("Authorization", "AWS4-HMAC-SHA256 Credential=test/20260615/us-east-1/rds/aws4_request, " + + "SignedHeaders=content-type;host, Signature=test") + .contentType("application/x-www-form-urlencoded") + .formParam("Action", action) + .formParam("Version", "2014-10-31"); + for (int i = 0; i < formParams.length; i += 2) { + request = request.formParam(formParams[i], formParams[i + 1]); + } + return request.when().post("/").then(); + } + + private static String subscription(String prefix) { + String name = prefix + "-" + Long.toString(System.nanoTime(), 36); + rds("CreateEventSubscription", "SubscriptionName", name, "SnsTopicArn", TOPIC, + "SourceType", "db-instance", "SourceIds.member.1", "db-one").statusCode(200); + return name; + } + + @Test + void anIdentifierCanBeAddedAndRemoved() { + String name = subscription("srcid"); + + rds("AddSourceIdentifierToSubscription", "SubscriptionName", name, + "SourceIdentifier", "db-two") + .statusCode(200) + .body(containsString("db-one")) + .body(containsString("db-two")); + + rds("DescribeEventSubscriptions", "SubscriptionName", name) + .statusCode(200) + .body(containsString("db-two")); + + rds("RemoveSourceIdentifierFromSubscription", "SubscriptionName", name, + "SourceIdentifier", "db-two") + .statusCode(200) + .body(not(containsString("db-two"))) + .body(containsString("db-one")); + + rds("DescribeEventSubscriptions", "SubscriptionName", name) + .statusCode(200) + .body(not(containsString("db-two"))); + } + + /** + * The model declares only SourceNotFoundFault and SubscriptionNotFoundFault here, so there is + * no fault that fits a duplicate add. + */ + @Test + void addingAnIdentifierTwiceIsANoOp() { + String name = subscription("dup"); + + rds("AddSourceIdentifierToSubscription", "SubscriptionName", name, + "SourceIdentifier", "db-one").statusCode(200); + + String body = rds("DescribeEventSubscriptions", "SubscriptionName", name) + .statusCode(200).extract().asString(); + int first = body.indexOf("db-one"); + int second = body.indexOf("db-one", first + 1); + assertEquals(-1, second, + "db-one appears once, not twice: " + body); + } + + @Test + void removingAnIdentifierTheSubscriptionDoesNotCarryIsSourceNotFound() { + String name = subscription("absent"); + + rds("RemoveSourceIdentifierFromSubscription", "SubscriptionName", name, + "SourceIdentifier", "db-nine") + .statusCode(404) + .body(containsString("SourceNotFound")); + } + + @Test + void anUnknownSubscriptionIsSubscriptionNotFound() { + rds("AddSourceIdentifierToSubscription", "SubscriptionName", "no-such-subscription", + "SourceIdentifier", "db-one") + .statusCode(404) + .body(containsString("SubscriptionNotFound")); + + rds("RemoveSourceIdentifierFromSubscription", "SubscriptionName", "no-such-subscription", + "SourceIdentifier", "db-one") + .statusCode(404) + .body(containsString("SubscriptionNotFound")); + } + + /** + * Both members are required by the model, so both answer InvalidParameterValue. A missing name + * used to reach the lookup and come back as SubscriptionNotFound, which says the subscription + * does not exist when the request simply did not name one. + */ + @Test + void aMissingRequiredMemberIsRefusedTheSameWayOnBothOperations() { + String name = subscription("nosrc"); + for (String action : new String[]{"AddSourceIdentifierToSubscription", + "RemoveSourceIdentifierFromSubscription"}) { + rds(action, "SubscriptionName", name) + .statusCode(400) + .body(containsString("InvalidParameterValue")); + rds(action, "SourceIdentifier", "db-one") + .statusCode(400) + .body(containsString("InvalidParameterValue")); + } + } +}