mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
feat(sns): add topic permissions, the SMS sandbox and SMS opt-out (#5570)
SNS's Query routing set listed ten names that neither SNS handler dispatched, so a request for any of them was answered UnsupportedOperation. All ten are real SNS operations, and both the Query and JSON protocols now serve them. AddPermission and RemovePermission edit the topic's Policy attribute the way AWS does, going by LocalStack's recordings of the same calls against a live account: the label becomes the statement's Sid, each account its root principal, and a single account or action is written as a string. A duplicate label, an action outside SNS and a missing topic get AWS's errors. A label the policy does not hold leaves it unchanged, since no source shows what AWS answers there. Floci stores topic policies but does not evaluate them under enforcement, so the grant only shows in GetTopicAttributes. The default topic policy now matches what AWS returns for a new topic: Version 2008-10-17 with an AWS:SourceOwner condition, where Floci has had 2012-10-17 and AWS:SourceAccount since the first commit. AddPermission builds on it. GetSMSSandboxAccountStatus reports the account outside the sandbox, because Floci delivers SMS to any number. The sandbox numbers still work per account and region, without the limits AWS lifts outside the sandbox. CreateSMSSandboxPhoneNumber texts a six-digit code through the same path as Publish, so a test reads it from /_aws/sns, and VerifySMSSandboxPhoneNumber checks it. No number is ever opted out. AWS opts a number out when its owner replies STOP, and nothing reaches Floci that way, so CheckIfPhoneNumberIsOptedOut answers false, ListPhoneNumbersOptedOut is empty, and OptInPhoneNumber checks only the E.164 format, with AWS's error for a malformed number. AddPermission takes only twelve-digit account IDs, so a malformed one cannot end up as a principal in the stored policy. Each of these edits reads a record, changes it and writes it back. AddPermission, RemovePermission and SetTopicAttributes, which writes the same Policy attribute, are synchronized, as are the sandbox's create, verify and delete, so overlapping requests cannot lose a grant or bring back a deleted number. A number whose verification text cannot be recorded is put back as it was. The tests park the first of two edits inside that window and check the second waits. testing.ThreadStates holds the wait they use, which several other test classes still carry their own copies of. With SNS dispatching AddPermission and RemovePermission, the last two rows of the Query routing gap file, SQS's two names reaching SNS without a Version, are gone, leaving 171. The SNS row in docs/services/index.md now counts 39 operations, up from a stale 17. Refs #5549
This commit is contained in:
1 parent
a88ef42b4b
commit
b7a1bde2dc
12 files changed
+1589
-27
No files matched your search
@@ -1,34 +1,59 @@
|
||||
package com.floci.test;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.*;
|
||||
import software.amazon.awssdk.services.sns.SnsClient;
|
||||
import software.amazon.awssdk.services.sns.model.AddPermissionRequest;
|
||||
import software.amazon.awssdk.services.sns.model.CheckIfPhoneNumberIsOptedOutRequest;
|
||||
import software.amazon.awssdk.services.sns.model.CreateSmsSandboxPhoneNumberRequest;
|
||||
import software.amazon.awssdk.services.sns.model.CreateTopicRequest;
|
||||
import software.amazon.awssdk.services.sns.model.CreateTopicResponse;
|
||||
import software.amazon.awssdk.services.sns.model.DeleteSmsSandboxPhoneNumberRequest;
|
||||
import software.amazon.awssdk.services.sns.model.DeleteTopicRequest;
|
||||
import software.amazon.awssdk.services.sns.model.GetSmsAttributesRequest;
|
||||
import software.amazon.awssdk.services.sns.model.GetSmsAttributesResponse;
|
||||
import software.amazon.awssdk.services.sns.model.GetSmsSandboxAccountStatusRequest;
|
||||
import software.amazon.awssdk.services.sns.model.GetTopicAttributesRequest;
|
||||
import software.amazon.awssdk.services.sns.model.GetTopicAttributesResponse;
|
||||
import software.amazon.awssdk.services.sns.model.InvalidParameterException;
|
||||
import software.amazon.awssdk.services.sns.model.LanguageCodeString;
|
||||
import software.amazon.awssdk.services.sns.model.ListPhoneNumbersOptedOutRequest;
|
||||
import software.amazon.awssdk.services.sns.model.ListSmsSandboxPhoneNumbersRequest;
|
||||
import software.amazon.awssdk.services.sns.model.ListSubscriptionsByTopicRequest;
|
||||
import software.amazon.awssdk.services.sns.model.ListSubscriptionsByTopicResponse;
|
||||
import software.amazon.awssdk.services.sns.model.ListTopicsResponse;
|
||||
import software.amazon.awssdk.services.sns.model.MessageAttributeValue;
|
||||
import software.amazon.awssdk.services.sns.model.OptInPhoneNumberRequest;
|
||||
import software.amazon.awssdk.services.sns.model.PublishBatchRequest;
|
||||
import software.amazon.awssdk.services.sns.model.PublishBatchRequestEntry;
|
||||
import software.amazon.awssdk.services.sns.model.PublishBatchResponse;
|
||||
import software.amazon.awssdk.services.sns.model.PublishRequest;
|
||||
import software.amazon.awssdk.services.sns.model.PublishResponse;
|
||||
import software.amazon.awssdk.services.sns.model.RemovePermissionRequest;
|
||||
import software.amazon.awssdk.services.sns.model.SMSSandboxPhoneNumber;
|
||||
import software.amazon.awssdk.services.sns.model.SMSSandboxPhoneNumberVerificationStatus;
|
||||
import software.amazon.awssdk.services.sns.model.SetSmsAttributesRequest;
|
||||
import software.amazon.awssdk.services.sns.model.SetSubscriptionAttributesRequest;
|
||||
import software.amazon.awssdk.services.sns.model.SubscribeRequest;
|
||||
import software.amazon.awssdk.services.sns.model.SubscribeResponse;
|
||||
import software.amazon.awssdk.services.sns.model.UnsubscribeRequest;
|
||||
import software.amazon.awssdk.services.sns.model.VerifySmsSandboxPhoneNumberRequest;
|
||||
import software.amazon.awssdk.services.sqs.SqsClient;
|
||||
import software.amazon.awssdk.services.sqs.model.*;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URLEncoder;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeFalse;
|
||||
|
||||
@DisplayName("SNS Simple Notification Service")
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
@@ -601,4 +626,106 @@ class SnsTest {
|
||||
assertThat(sns.getSMSAttributes().attributes())
|
||||
.doesNotContainKeys("DefaultSMSType", "MonthlySpendLimit");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(18)
|
||||
void topicPermissionsAreAddedToAndRemovedFromThePolicy() throws Exception {
|
||||
String permissionTopic = sns.createTopic(CreateTopicRequest.builder()
|
||||
.name(TestFixtures.uniqueName("sns-permission")).build()).topicArn();
|
||||
try {
|
||||
String account = permissionTopic.split(":")[4];
|
||||
sns.addPermission(AddPermissionRequest.builder()
|
||||
.topicArn(permissionTopic).label("compat-grant")
|
||||
.awsAccountIds(account).actionNames("Publish").build());
|
||||
|
||||
JsonNode granted = statementWithSid(permissionTopic, "compat-grant");
|
||||
assertThat(granted).isNotNull();
|
||||
assertThat(granted.path("Principal").path("AWS").asText())
|
||||
.isEqualTo(TestFixtures.globalArn("iam", account, "root"));
|
||||
assertThat(granted.path("Action").asText()).isEqualTo("SNS:Publish");
|
||||
assertThat(granted.path("Resource").asText()).isEqualTo(permissionTopic);
|
||||
|
||||
assertThatThrownBy(() -> sns.addPermission(AddPermissionRequest.builder()
|
||||
.topicArn(permissionTopic).label("compat-grant")
|
||||
.awsAccountIds(account).actionNames("Subscribe").build()))
|
||||
.isInstanceOf(InvalidParameterException.class);
|
||||
|
||||
sns.removePermission(RemovePermissionRequest.builder()
|
||||
.topicArn(permissionTopic).label("compat-grant").build());
|
||||
assertThat(statementWithSid(permissionTopic, "compat-grant")).isNull();
|
||||
} finally {
|
||||
sns.deleteTopic(DeleteTopicRequest.builder().topicArn(permissionTopic).build());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(19)
|
||||
void smsSandboxNumbersAreVerifiedWithTheTextedCode() throws Exception {
|
||||
assumeFalse(TestFixtures.isRealAws(), "Reads the code from Floci's SMS inspection endpoint");
|
||||
assertThat(sns.getSMSSandboxAccountStatus(GetSmsSandboxAccountStatusRequest.builder().build())
|
||||
.isInSandbox()).isFalse();
|
||||
|
||||
String phoneNumber = String.format("+1555%07d", ThreadLocalRandom.current().nextInt(10_000_000));
|
||||
sns.createSMSSandboxPhoneNumber(CreateSmsSandboxPhoneNumberRequest.builder()
|
||||
.phoneNumber(phoneNumber).languageCode(LanguageCodeString.EN_US).build());
|
||||
try {
|
||||
assertThat(sandboxStatus(phoneNumber)).isEqualTo(SMSSandboxPhoneNumberVerificationStatus.PENDING);
|
||||
|
||||
sns.verifySMSSandboxPhoneNumber(VerifySmsSandboxPhoneNumberRequest.builder()
|
||||
.phoneNumber(phoneNumber).oneTimePassword(textedCode(phoneNumber)).build());
|
||||
|
||||
assertThat(sandboxStatus(phoneNumber)).isEqualTo(SMSSandboxPhoneNumberVerificationStatus.VERIFIED);
|
||||
} finally {
|
||||
sns.deleteSMSSandboxPhoneNumber(DeleteSmsSandboxPhoneNumberRequest.builder()
|
||||
.phoneNumber(phoneNumber).build());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(20)
|
||||
void noNumberIsOptedOutAndOptingInChecksTheFormat() {
|
||||
assumeFalse(TestFixtures.isRealAws(), "A real account's opt-out list is whatever its recipients sent");
|
||||
assertThat(sns.checkIfPhoneNumberIsOptedOut(CheckIfPhoneNumberIsOptedOutRequest.builder()
|
||||
.phoneNumber("+4411111111").build()).isOptedOut()).isFalse();
|
||||
assertThat(sns.listPhoneNumbersOptedOut(ListPhoneNumbersOptedOutRequest.builder().build())
|
||||
.phoneNumbers()).isEmpty();
|
||||
|
||||
sns.optInPhoneNumber(OptInPhoneNumberRequest.builder().phoneNumber("+4411111111").build());
|
||||
assertThatThrownBy(() -> sns.optInPhoneNumber(OptInPhoneNumberRequest.builder()
|
||||
.phoneNumber("invalid").build()))
|
||||
.isInstanceOf(InvalidParameterException.class);
|
||||
}
|
||||
|
||||
private static JsonNode statementWithSid(String topic, String sid) throws Exception {
|
||||
String policy = sns.getTopicAttributes(GetTopicAttributesRequest.builder().topicArn(topic).build())
|
||||
.attributes().get("Policy");
|
||||
for (JsonNode statement : new ObjectMapper().readTree(policy).path("Statement")) {
|
||||
if (sid.equals(statement.path("Sid").asText())) {
|
||||
return statement;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static SMSSandboxPhoneNumberVerificationStatus sandboxStatus(String phoneNumber) {
|
||||
return sns.listSMSSandboxPhoneNumbersPaginator(ListSmsSandboxPhoneNumbersRequest.builder().build())
|
||||
.phoneNumbers().stream()
|
||||
.filter(number -> phoneNumber.equals(number.phoneNumber()))
|
||||
.map(SMSSandboxPhoneNumber::status)
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new AssertionError(phoneNumber + " is not in the SMS sandbox list"));
|
||||
}
|
||||
|
||||
private static String textedCode(String phoneNumber) throws Exception {
|
||||
URI uri = URI.create(TestFixtures.endpoint() + "/_aws/sns?phone="
|
||||
+ URLEncoder.encode(phoneNumber, StandardCharsets.UTF_8));
|
||||
HttpResponse<String> response = TestFixtures.emulatorHttpClient().send(
|
||||
HttpRequest.newBuilder(uri).GET().build(), HttpResponse.BodyHandlers.ofString());
|
||||
assertThat(response.statusCode()).isEqualTo(200);
|
||||
JsonNode messages = new ObjectMapper().readTree(response.body()).path("messages");
|
||||
assertThat(messages).hasSize(1);
|
||||
Matcher code = Pattern.compile("[0-9]{6}").matcher(messages.get(0).path("Message").asText());
|
||||
assertThat(code.find()).isTrue();
|
||||
return code.group();
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac
|
||||
|---|---|---|---|
|
||||
| [SSM](ssm.md) | `POST /` + `X-Amz-Target: AmazonSSM.*` / `AmazonSSMMessageDeliveryService.*` | JSON 1.1 | 22 |
|
||||
| [SQS](sqs.md) | `POST /` with `Action=` param | Query / JSON | 23 |
|
||||
| [SNS](sns.md) | `POST /` with `Action=` param | Query / JSON | 17 |
|
||||
| [SNS](sns.md) | `POST /` with `Action=` param | Query / JSON | 39 |
|
||||
| [S3](s3.md) | `/{bucket}/{key}` | REST XML | 58 |
|
||||
| [S3 Vectors](s3vectors.md) | `POST /{OperationName}` | REST JSON | 12 |
|
||||
| [S3 Tables](s3tables.md) | `/buckets`, `/namespaces/*`, `/tables/*` | REST JSON | 25 |
|
||||
|
||||
@@ -12,8 +12,18 @@
|
||||
| `ListTopics` | List all topics |
|
||||
| `GetTopicAttributes` | Get topic configuration |
|
||||
| `SetTopicAttributes` | Update topic configuration |
|
||||
| `AddPermission` | Add a labelled statement to a topic's access policy |
|
||||
| `RemovePermission` | Remove a labelled statement from a topic's access policy |
|
||||
| `GetSMSAttributes` | Read account SMS preferences in this region |
|
||||
| `SetSMSAttributes` | Set account SMS preferences in this region |
|
||||
| `GetSMSSandboxAccountStatus` | Report whether the account is in the SMS sandbox (always outside it) |
|
||||
| `CreateSMSSandboxPhoneNumber` | Add a sandbox destination number and text it a verification code |
|
||||
| `VerifySMSSandboxPhoneNumber` | Verify a sandbox number with the code it was sent |
|
||||
| `DeleteSMSSandboxPhoneNumber` | Remove a sandbox number |
|
||||
| `ListSMSSandboxPhoneNumbers` | List sandbox numbers and their verification status |
|
||||
| `CheckIfPhoneNumberIsOptedOut` | Check whether a number has opted out of SMS (never, in Floci) |
|
||||
| `ListPhoneNumbersOptedOut` | List numbers opted out of SMS (always empty) |
|
||||
| `OptInPhoneNumber` | Opt a number back in to SMS |
|
||||
| `Subscribe` | Subscribe an endpoint (SQS, HTTP, Lambda, email) |
|
||||
| `Unsubscribe` | Remove a subscription |
|
||||
| `ListSubscriptions` | List all subscriptions |
|
||||
@@ -43,6 +53,32 @@ clears that preference. SMS delivery
|
||||
remains mocked, so these settings do not generate delivery
|
||||
reports or CloudWatch delivery logs.
|
||||
|
||||
`AddPermission` and `RemovePermission` edit the topic's `Policy` attribute the way AWS does: the
|
||||
label becomes the statement's `Sid` and each account becomes its `root` principal. A topic with no
|
||||
policy set starts from the default one AWS gives a new topic. Floci stores the policy and returns
|
||||
it from `GetTopicAttributes`, but does not evaluate topic policies when authorizing requests.
|
||||
|
||||
## SMS sandbox and opt-out
|
||||
|
||||
A new AWS account starts in the SMS sandbox, where it can only text verified numbers. Floci
|
||||
delivers SMS to any number, so `GetSMSSandboxAccountStatus` reports `IsInSandbox: false`, which
|
||||
is how it actually behaves. The sandbox number calls still work, per account and region, without
|
||||
the limits AWS lifts once an account leaves the sandbox (ten verified numbers, and no deleting a
|
||||
number until 24 hours after its last verification attempt). `CreateSMSSandboxPhoneNumber` texts a
|
||||
six-digit code the way every SMS is sent here, so a test reads it back from the inspection
|
||||
endpoint and passes it to `VerifySMSSandboxPhoneNumber`:
|
||||
|
||||
```bash
|
||||
aws sns create-sms-sandbox-phone-number --phone-number +15555550100 --endpoint-url $AWS_ENDPOINT_URL
|
||||
curl -s "$AWS_ENDPOINT_URL/_aws/sns?phone=%2B15555550100" # {"messages":[{"Message":"Your verification code is 123456", ...}]}
|
||||
aws sns verify-sms-sandbox-phone-number --phone-number +15555550100 --one-time-password 123456 \
|
||||
--endpoint-url $AWS_ENDPOINT_URL
|
||||
```
|
||||
|
||||
AWS opts a number out when its owner replies STOP. Nothing reaches Floci that way, so no number is
|
||||
ever opted out: `CheckIfPhoneNumberIsOptedOut` answers `false`, `ListPhoneNumbersOptedOut` is
|
||||
empty, and `OptInPhoneNumber` only checks that the number is in E.164 format.
|
||||
|
||||
## Configuration
|
||||
|
||||
| Variable | Default | Description |
|
||||
|
||||
@@ -6,8 +6,11 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
|
||||
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.services.sns.model.PlatformApplication;
|
||||
import io.github.hectorvent.floci.services.sns.model.PlatformEndpoint;
|
||||
import io.github.hectorvent.floci.services.sns.model.SmsSandboxPhoneNumber;
|
||||
import io.github.hectorvent.floci.services.sns.model.Subscription;
|
||||
import io.github.hectorvent.floci.services.sns.model.Topic;
|
||||
import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue;
|
||||
@@ -44,8 +47,18 @@ public class SnsJsonHandler {
|
||||
case "ListTopics" -> handleListTopics(request, region);
|
||||
case "GetTopicAttributes" -> handleGetTopicAttributes(request, region);
|
||||
case "SetTopicAttributes" -> handleSetTopicAttributes(request, region);
|
||||
case "AddPermission" -> handleAddPermission(request, region);
|
||||
case "RemovePermission" -> handleRemovePermission(request, region);
|
||||
case "SetSMSAttributes" -> handleSetSmsAttributes(request, region);
|
||||
case "GetSMSAttributes" -> handleGetSmsAttributes(request, region);
|
||||
case "GetSMSSandboxAccountStatus" -> handleGetSmsSandboxAccountStatus();
|
||||
case "CreateSMSSandboxPhoneNumber" -> handleCreateSmsSandboxPhoneNumber(request, region);
|
||||
case "VerifySMSSandboxPhoneNumber" -> handleVerifySmsSandboxPhoneNumber(request, region);
|
||||
case "DeleteSMSSandboxPhoneNumber" -> handleDeleteSmsSandboxPhoneNumber(request, region);
|
||||
case "ListSMSSandboxPhoneNumbers" -> handleListSmsSandboxPhoneNumbers(request, region);
|
||||
case "CheckIfPhoneNumberIsOptedOut" -> handleCheckIfPhoneNumberIsOptedOut(request);
|
||||
case "ListPhoneNumbersOptedOut" -> handleListPhoneNumbersOptedOut();
|
||||
case "OptInPhoneNumber" -> handleOptInPhoneNumber(request);
|
||||
case "Subscribe" -> handleSubscribe(request, region);
|
||||
case "Unsubscribe" -> handleUnsubscribe(request, region);
|
||||
case "ListSubscriptions" -> handleListSubscriptions(request, region);
|
||||
@@ -127,6 +140,18 @@ public class SnsJsonHandler {
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private Response handleAddPermission(JsonNode request, String region) {
|
||||
snsService.addPermission(request.path("TopicArn").asText(null), request.path("Label").asText(null),
|
||||
stringList(request.path("AWSAccountId")), stringList(request.path("ActionName")), region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private Response handleRemovePermission(JsonNode request, String region) {
|
||||
snsService.removePermission(request.path("TopicArn").asText(null), request.path("Label").asText(null),
|
||||
region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private Response handleSetSmsAttributes(JsonNode request, String region) {
|
||||
Map<String, String> attributes = jsonNodeToMap(request.path("attributes"));
|
||||
if (attributes.isEmpty()) {
|
||||
@@ -152,6 +177,64 @@ public class SnsJsonHandler {
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleGetSmsSandboxAccountStatus() {
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("IsInSandbox", snsService.isInSmsSandbox());
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleCreateSmsSandboxPhoneNumber(JsonNode request, String region) {
|
||||
snsService.createSmsSandboxPhoneNumber(request.path("PhoneNumber").asText(null),
|
||||
request.path("LanguageCode").asText(null), region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private Response handleVerifySmsSandboxPhoneNumber(JsonNode request, String region) {
|
||||
snsService.verifySmsSandboxPhoneNumber(request.path("PhoneNumber").asText(null),
|
||||
request.path("OneTimePassword").asText(null), region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private Response handleDeleteSmsSandboxPhoneNumber(JsonNode request, String region) {
|
||||
snsService.deleteSmsSandboxPhoneNumber(request.path("PhoneNumber").asText(null), region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private Response handleListSmsSandboxPhoneNumbers(JsonNode request, String region) {
|
||||
PaginatedResult<SmsSandboxPhoneNumber> page = snsService.listSmsSandboxPhoneNumbers(
|
||||
Pagination.parseMaxResults(request.path("MaxResults").asText(null), "InvalidParameter"),
|
||||
request.path("NextToken").asText(null), region);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode numbers = response.putArray("PhoneNumbers");
|
||||
for (SmsSandboxPhoneNumber number : page.items()) {
|
||||
numbers.addObject()
|
||||
.put("PhoneNumber", number.getPhoneNumber())
|
||||
.put("Status", number.getStatus());
|
||||
}
|
||||
if (page.nextToken() != null) {
|
||||
response.put("NextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleCheckIfPhoneNumberIsOptedOut(JsonNode request) {
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("isOptedOut", snsService.isPhoneNumberOptedOut(request.path("phoneNumber").asText(null)));
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleListPhoneNumbersOptedOut() {
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode phoneNumbers = response.putArray("phoneNumbers");
|
||||
snsService.listPhoneNumbersOptedOut().forEach(phoneNumbers::add);
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleOptInPhoneNumber(JsonNode request) {
|
||||
snsService.optInPhoneNumber(request.path("phoneNumber").asText(null));
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private Response handleSubscribe(JsonNode request, String region) {
|
||||
String topicArn = request.path("TopicArn").asText(null);
|
||||
String protocol = request.path("Protocol").asText(null);
|
||||
@@ -447,6 +530,16 @@ public class SnsJsonHandler {
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private static List<String> stringList(JsonNode node) {
|
||||
List<String> values = new ArrayList<>();
|
||||
if (node.isArray()) {
|
||||
for (JsonNode value : node) {
|
||||
values.add(value.asText());
|
||||
}
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
private Map<String, String> jsonNodeToMap(JsonNode node) {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
if (node != null && node.isObject()) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.sns;
|
||||
import io.github.hectorvent.floci.core.common.*;
|
||||
import io.github.hectorvent.floci.services.sns.model.PlatformApplication;
|
||||
import io.github.hectorvent.floci.services.sns.model.PlatformEndpoint;
|
||||
import io.github.hectorvent.floci.services.sns.model.SmsSandboxPhoneNumber;
|
||||
import io.github.hectorvent.floci.services.sns.model.Subscription;
|
||||
import io.github.hectorvent.floci.services.sns.model.Topic;
|
||||
import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue;
|
||||
@@ -43,8 +44,18 @@ public class SnsQueryHandler {
|
||||
case "ListTopics" -> handleListTopics(params, region);
|
||||
case "GetTopicAttributes" -> handleGetTopicAttributes(params, region);
|
||||
case "SetTopicAttributes" -> handleSetTopicAttributes(params, region);
|
||||
case "AddPermission" -> handleAddPermission(params, region);
|
||||
case "RemovePermission" -> handleRemovePermission(params, region);
|
||||
case "SetSMSAttributes" -> handleSetSmsAttributes(params, region);
|
||||
case "GetSMSAttributes" -> handleGetSmsAttributes(params, region);
|
||||
case "GetSMSSandboxAccountStatus" -> handleGetSmsSandboxAccountStatus();
|
||||
case "CreateSMSSandboxPhoneNumber" -> handleCreateSmsSandboxPhoneNumber(params, region);
|
||||
case "VerifySMSSandboxPhoneNumber" -> handleVerifySmsSandboxPhoneNumber(params, region);
|
||||
case "DeleteSMSSandboxPhoneNumber" -> handleDeleteSmsSandboxPhoneNumber(params, region);
|
||||
case "ListSMSSandboxPhoneNumbers" -> handleListSmsSandboxPhoneNumbers(params, region);
|
||||
case "CheckIfPhoneNumberIsOptedOut" -> handleCheckIfPhoneNumberIsOptedOut(params);
|
||||
case "ListPhoneNumbersOptedOut" -> handleListPhoneNumbersOptedOut();
|
||||
case "OptInPhoneNumber" -> handleOptInPhoneNumber(params);
|
||||
case "Subscribe" -> handleSubscribe(params, region);
|
||||
case "Unsubscribe" -> handleUnsubscribe(params, region);
|
||||
case "ListSubscriptions" -> handleListSubscriptions(params, region);
|
||||
@@ -134,6 +145,25 @@ public class SnsQueryHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleAddPermission(MultivaluedMap<String, String> params, String region) {
|
||||
try {
|
||||
snsService.addPermission(getParam(params, "TopicArn"), getParam(params, "Label"),
|
||||
memberList(params, "AWSAccountId"), memberList(params, "ActionName"), region);
|
||||
return Response.ok(AwsQueryResponse.envelopeNoResult("AddPermission", AwsNamespaces.SNS)).build();
|
||||
} catch (AwsException e) {
|
||||
return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleRemovePermission(MultivaluedMap<String, String> params, String region) {
|
||||
try {
|
||||
snsService.removePermission(getParam(params, "TopicArn"), getParam(params, "Label"), region);
|
||||
return Response.ok(AwsQueryResponse.envelopeNoResult("RemovePermission", AwsNamespaces.SNS)).build();
|
||||
} catch (AwsException e) {
|
||||
return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleSetSmsAttributes(MultivaluedMap<String, String> params, String region) {
|
||||
Map<String, String> attributes = extractSnsAttributes(params, "attributes");
|
||||
if (attributes.isEmpty()) {
|
||||
@@ -161,6 +191,102 @@ public class SnsQueryHandler {
|
||||
return Response.ok(AwsQueryResponse.envelope("GetSMSAttributes", AwsNamespaces.SNS, xml.build())).build();
|
||||
}
|
||||
|
||||
private Response handleGetSmsSandboxAccountStatus() {
|
||||
String result = new XmlBuilder()
|
||||
.elem("IsInSandbox", String.valueOf(snsService.isInSmsSandbox()))
|
||||
.build();
|
||||
return Response.ok(AwsQueryResponse.envelope("GetSMSSandboxAccountStatus", AwsNamespaces.SNS, result))
|
||||
.build();
|
||||
}
|
||||
|
||||
private Response handleCreateSmsSandboxPhoneNumber(MultivaluedMap<String, String> params, String region) {
|
||||
try {
|
||||
snsService.createSmsSandboxPhoneNumber(getParam(params, "PhoneNumber"),
|
||||
getParam(params, "LanguageCode"), region);
|
||||
return Response.ok(AwsQueryResponse.envelopeEmptyResult("CreateSMSSandboxPhoneNumber",
|
||||
AwsNamespaces.SNS)).build();
|
||||
} catch (AwsException e) {
|
||||
return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleVerifySmsSandboxPhoneNumber(MultivaluedMap<String, String> params, String region) {
|
||||
try {
|
||||
snsService.verifySmsSandboxPhoneNumber(getParam(params, "PhoneNumber"),
|
||||
getParam(params, "OneTimePassword"), region);
|
||||
return Response.ok(AwsQueryResponse.envelopeEmptyResult("VerifySMSSandboxPhoneNumber",
|
||||
AwsNamespaces.SNS)).build();
|
||||
} catch (AwsException e) {
|
||||
return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleDeleteSmsSandboxPhoneNumber(MultivaluedMap<String, String> params, String region) {
|
||||
try {
|
||||
snsService.deleteSmsSandboxPhoneNumber(getParam(params, "PhoneNumber"), region);
|
||||
return Response.ok(AwsQueryResponse.envelopeEmptyResult("DeleteSMSSandboxPhoneNumber",
|
||||
AwsNamespaces.SNS)).build();
|
||||
} catch (AwsException e) {
|
||||
return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleListSmsSandboxPhoneNumbers(MultivaluedMap<String, String> params, String region) {
|
||||
try {
|
||||
PaginatedResult<SmsSandboxPhoneNumber> page = snsService.listSmsSandboxPhoneNumbers(
|
||||
Pagination.parseMaxResults(getParam(params, "MaxResults"), "InvalidParameter"),
|
||||
getParam(params, "NextToken"), region);
|
||||
XmlBuilder xml = new XmlBuilder().start("PhoneNumbers");
|
||||
for (SmsSandboxPhoneNumber number : page.items()) {
|
||||
xml.start("member")
|
||||
.elem("PhoneNumber", number.getPhoneNumber())
|
||||
.elem("Status", number.getStatus())
|
||||
.end("member");
|
||||
}
|
||||
xml.end("PhoneNumbers");
|
||||
if (page.nextToken() != null) {
|
||||
xml.elem("NextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(AwsQueryResponse.envelope("ListSMSSandboxPhoneNumbers", AwsNamespaces.SNS,
|
||||
xml.build())).build();
|
||||
} catch (AwsException e) {
|
||||
return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleCheckIfPhoneNumberIsOptedOut(MultivaluedMap<String, String> params) {
|
||||
try {
|
||||
String result = new XmlBuilder()
|
||||
.elem("isOptedOut", String.valueOf(snsService.isPhoneNumberOptedOut(
|
||||
getParam(params, "phoneNumber"))))
|
||||
.build();
|
||||
return Response.ok(AwsQueryResponse.envelope("CheckIfPhoneNumberIsOptedOut", AwsNamespaces.SNS,
|
||||
result)).build();
|
||||
} catch (AwsException e) {
|
||||
return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleListPhoneNumbersOptedOut() {
|
||||
XmlBuilder xml = new XmlBuilder().start("phoneNumbers");
|
||||
for (String phoneNumber : snsService.listPhoneNumbersOptedOut()) {
|
||||
xml.elem("member", phoneNumber);
|
||||
}
|
||||
xml.end("phoneNumbers");
|
||||
return Response.ok(AwsQueryResponse.envelope("ListPhoneNumbersOptedOut", AwsNamespaces.SNS,
|
||||
xml.build())).build();
|
||||
}
|
||||
|
||||
private Response handleOptInPhoneNumber(MultivaluedMap<String, String> params) {
|
||||
try {
|
||||
snsService.optInPhoneNumber(getParam(params, "phoneNumber"));
|
||||
return Response.ok(AwsQueryResponse.envelopeEmptyResult("OptInPhoneNumber", AwsNamespaces.SNS))
|
||||
.build();
|
||||
} catch (AwsException e) {
|
||||
return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleSubscribe(MultivaluedMap<String, String> params, String region) {
|
||||
String topicArn = getParam(params, "TopicArn");
|
||||
String protocol = getParam(params, "Protocol");
|
||||
@@ -529,6 +655,18 @@ public class SnsQueryHandler {
|
||||
return params.getFirst(name);
|
||||
}
|
||||
|
||||
/** A list parameter's {@code <name>.member.N} values, in order, up to the first gap. */
|
||||
private List<String> memberList(MultivaluedMap<String, String> params, String name) {
|
||||
List<String> values = new ArrayList<>();
|
||||
for (int i = 1; ; i++) {
|
||||
String value = getParam(params, name + ".member." + i);
|
||||
if (value == null) {
|
||||
return values;
|
||||
}
|
||||
values.add(value);
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, MessageAttributeValue> parseMessageAttributes(
|
||||
MultivaluedMap<String, String> params, String prefix) {
|
||||
Map<String, MessageAttributeValue> attributes = new HashMap<>();
|
||||
|
||||
@@ -5,11 +5,15 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.AwsRegions;
|
||||
import io.github.hectorvent.floci.core.common.Digests;
|
||||
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.Resettable;
|
||||
import io.github.hectorvent.floci.core.common.ServicePrincipals;
|
||||
@@ -29,6 +33,7 @@ import io.github.hectorvent.floci.services.sns.model.PlatformApplication;
|
||||
import io.github.hectorvent.floci.services.sns.model.PlatformEndpoint;
|
||||
import io.github.hectorvent.floci.services.sns.model.PushNotification;
|
||||
import io.github.hectorvent.floci.services.sns.model.SentSms;
|
||||
import io.github.hectorvent.floci.services.sns.model.SmsSandboxPhoneNumber;
|
||||
import io.github.hectorvent.floci.services.sns.model.Subscription;
|
||||
import io.github.hectorvent.floci.services.sns.model.Topic;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
@@ -46,6 +51,7 @@ import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.SecureRandom;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
@@ -75,6 +81,43 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
"DeliveryStatusIAMRole", "DeliveryStatusSuccessSamplingRate", "DefaultSenderID",
|
||||
"DefaultSMSType", "UsageReportS3Bucket");
|
||||
private static final Pattern SMS_SENDER_ID = Pattern.compile("(?=.*[A-Za-z])[A-Za-z0-9]{1,11}");
|
||||
/** The format AWS accepts for a number it opts in or checks the opt-out status of. */
|
||||
private static final Pattern E164_PHONE_NUMBER = Pattern.compile("\\+[1-9][0-9]{1,14}");
|
||||
/** A sandbox destination number, as the {@code PhoneNumberString} shape in SNS's model constrains it. */
|
||||
private static final Pattern SANDBOX_PHONE_NUMBER = Pattern.compile("\\+[0-9]{8,}|[0-9]{0,9}");
|
||||
private static final int SANDBOX_PHONE_NUMBER_MAX_LENGTH = 20;
|
||||
/** The {@code OTPCode} shape in SNS's model. */
|
||||
private static final Pattern ONE_TIME_PASSWORD = Pattern.compile("[0-9]{5,8}");
|
||||
/** The {@code LanguageCodeString} enum in SNS's model, the languages a verification text can be sent in. */
|
||||
private static final Set<String> SANDBOX_LANGUAGE_CODES = Set.of("en-US", "en-GB", "es-419", "es-ES",
|
||||
"de-DE", "fr-CA", "fr-FR", "it-IT", "ja-JP", "pt-BR", "kr-KR", "zh-CN", "zh-TW");
|
||||
/** An AWS account ID, the only principal {@code AddPermission} takes. */
|
||||
private static final Pattern ACCOUNT_ID = Pattern.compile("[0-9]{12}");
|
||||
/** The {@code MaxItems} ceiling on {@code ListSMSSandboxPhoneNumbers}, which is also its page size. */
|
||||
private static final int SANDBOX_PAGE_SIZE = 100;
|
||||
/**
|
||||
* The actions a topic policy statement can grant, which are SNS's actions in AWS's service
|
||||
* reference (https://servicereference.us-east-1.amazonaws.com/v1/sns/sns.json).
|
||||
* {@code AddPermission} refuses any other name.
|
||||
*/
|
||||
private static final Set<String> SNS_POLICY_ACTIONS = Set.of(
|
||||
"AddPermission", "CheckIfPhoneNumberIsOptedOut", "ConfirmSubscription",
|
||||
"CreatePlatformApplication", "CreatePlatformEndpoint", "CreateSMSSandboxPhoneNumber",
|
||||
"CreateTopic", "DeleteEndpoint", "DeletePlatformApplication", "DeleteSMSSandboxPhoneNumber",
|
||||
"DeleteTopic", "GetDataProtectionPolicy", "GetEndpointAttributes",
|
||||
"GetPlatformApplicationAttributes", "GetSMSAttributes", "GetSMSSandboxAccountStatus",
|
||||
"GetSubscriptionAttributes", "GetTopicAttributes", "ListEndpointsByPlatformApplication",
|
||||
"ListOriginationNumbers", "ListPhoneNumbersOptedOut", "ListPlatformApplications",
|
||||
"ListSMSSandboxPhoneNumbers", "ListSubscriptions", "ListSubscriptionsByTopic",
|
||||
"ListTagsForResource", "ListTopics", "OptInPhoneNumber", "Publish", "PutDataProtectionPolicy",
|
||||
"RemovePermission", "SetEndpointAttributes", "SetPlatformApplicationAttributes",
|
||||
"SetSMSAttributes", "SetSubscriptionAttributes", "SetTopicAttributes", "Subscribe",
|
||||
"TagResource", "Unsubscribe", "UntagResource", "VerifySMSSandboxPhoneNumber");
|
||||
/** What the default statement of a topic's policy grants its owner. */
|
||||
private static final List<String> DEFAULT_TOPIC_POLICY_ACTIONS = List.of(
|
||||
"SNS:GetTopicAttributes", "SNS:SetTopicAttributes", "SNS:AddPermission",
|
||||
"SNS:RemovePermission", "SNS:DeleteTopic", "SNS:Subscribe",
|
||||
"SNS:ListSubscriptionsByTopic", "SNS:Publish");
|
||||
/** Includes the original send and two immediate retries for local SQS fan-out. */
|
||||
private static final int SQS_SUBSCRIPTION_DELIVERY_ATTEMPTS = 3;
|
||||
private static final String SUBSCRIPTION_REDRIVE_POLICY = "RedrivePolicy";
|
||||
@@ -119,6 +162,8 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
private final Deque<PushNotification> pushCapture = new ConcurrentLinkedDeque<>();
|
||||
private final StorageBackend<String, SentSms> smsStore;
|
||||
private final StorageBackend<String, Map<String, String>> smsAttributesStore;
|
||||
private final StorageBackend<String, SmsSandboxPhoneNumber> smsSandboxStore;
|
||||
private final SecureRandom random = new SecureRandom();
|
||||
private final RegionResolver regionResolver;
|
||||
private final SqsService sqsService;
|
||||
private final LambdaService lambdaService;
|
||||
@@ -154,6 +199,9 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
storageFactory.create("sns", "sns-sms-attributes.json",
|
||||
new TypeReference<Map<String, Map<String, String>>>() {
|
||||
}),
|
||||
storageFactory.create("sns", "sns-sms-sandbox.json",
|
||||
new TypeReference<Map<String, SmsSandboxPhoneNumber>>() {
|
||||
}),
|
||||
regionResolver,
|
||||
sqsService,
|
||||
lambdaService,
|
||||
@@ -208,6 +256,7 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
this.platformEndpointStore = platformEndpointStore;
|
||||
this.smsStore = new InMemoryStorage<>();
|
||||
this.smsAttributesStore = new InMemoryStorage<>();
|
||||
this.smsSandboxStore = new InMemoryStorage<>();
|
||||
this.regionResolver = regionResolver;
|
||||
this.sqsService = sqsService;
|
||||
this.lambdaService = lambdaService;
|
||||
@@ -265,12 +314,28 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
RegionResolver regionResolver, SqsService sqsService,
|
||||
LambdaService lambdaService, FirehoseService firehoseService,
|
||||
String baseUrl, ObjectMapper objectMapper, SnsMessageSigner messageSigner) {
|
||||
this(topicStore, subscriptionStore, platformAppStore, platformEndpointStore, smsStore,
|
||||
smsAttributesStore, new InMemoryStorage<>(), regionResolver, sqsService,
|
||||
lambdaService, firehoseService, baseUrl, objectMapper, messageSigner);
|
||||
}
|
||||
|
||||
SnsService(StorageBackend<String, Topic> topicStore,
|
||||
StorageBackend<String, Subscription> subscriptionStore,
|
||||
StorageBackend<String, PlatformApplication> platformAppStore,
|
||||
StorageBackend<String, PlatformEndpoint> platformEndpointStore,
|
||||
StorageBackend<String, SentSms> smsStore,
|
||||
StorageBackend<String, Map<String, String>> smsAttributesStore,
|
||||
StorageBackend<String, SmsSandboxPhoneNumber> smsSandboxStore,
|
||||
RegionResolver regionResolver, SqsService sqsService,
|
||||
LambdaService lambdaService, FirehoseService firehoseService,
|
||||
String baseUrl, ObjectMapper objectMapper, SnsMessageSigner messageSigner) {
|
||||
this.topicStore = topicStore;
|
||||
this.subscriptionStore = subscriptionStore;
|
||||
this.platformAppStore = platformAppStore;
|
||||
this.platformEndpointStore = platformEndpointStore;
|
||||
this.smsStore = smsStore;
|
||||
this.smsAttributesStore = smsAttributesStore;
|
||||
this.smsSandboxStore = smsSandboxStore;
|
||||
this.regionResolver = regionResolver;
|
||||
this.sqsService = sqsService;
|
||||
this.lambdaService = lambdaService;
|
||||
@@ -346,6 +411,144 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
return selected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the account is in the SMS sandbox, where AWS sends SMS only to verified numbers. A
|
||||
* new AWS account starts in it, but Floci delivers SMS to any number, so it answers as an
|
||||
* account that has left the sandbox, which is how it actually behaves. The sandbox numbers
|
||||
* below still work, without the limits AWS lifts on leaving: ten verified numbers, and no
|
||||
* deleting one until 24 hours after its last verification attempt.
|
||||
*/
|
||||
public boolean isInSmsSandbox() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a destination number to the SMS sandbox and texts it a one-time password, which
|
||||
* {@code GET /_aws/sns?phone=} then shows, as it does any SMS. The text is Floci's; AWS does
|
||||
* not publish its own. A number already pending gets a fresh password, and one already
|
||||
* verified stays verified with nothing sent. If the text cannot be recorded, the number is
|
||||
* put back as it was, since a password nobody received could never verify it.
|
||||
*
|
||||
* <p>The sandbox edits are synchronized: each reads a number's record, changes it and writes
|
||||
* it back, so two overlapping requests could otherwise lose one's change, or a verification
|
||||
* could write back a number deleted meanwhile.
|
||||
*/
|
||||
public synchronized void createSmsSandboxPhoneNumber(String phoneNumber, String languageCode, String region) {
|
||||
requireSandboxPhoneNumber(phoneNumber);
|
||||
if (languageCode != null && !SANDBOX_LANGUAGE_CODES.contains(languageCode)) {
|
||||
throw new AwsException("InvalidParameter", "Invalid parameter: LanguageCode", 400);
|
||||
}
|
||||
String key = smsSandboxKey(region, phoneNumber);
|
||||
SmsSandboxPhoneNumber previous = smsSandboxStore.get(key).orElse(null);
|
||||
if (previous != null && SmsSandboxPhoneNumber.VERIFIED.equals(previous.getStatus())) {
|
||||
return;
|
||||
}
|
||||
String oneTimePassword = String.format("%06d", random.nextInt(1_000_000));
|
||||
smsSandboxStore.put(key,
|
||||
new SmsSandboxPhoneNumber(phoneNumber, SmsSandboxPhoneNumber.PENDING, oneTimePassword));
|
||||
try {
|
||||
recordSms(phoneNumber, "Your verification code is " + oneTimePassword, null, region);
|
||||
} catch (RuntimeException e) {
|
||||
try {
|
||||
if (previous == null) {
|
||||
smsSandboxStore.delete(key);
|
||||
} else {
|
||||
smsSandboxStore.put(key, previous);
|
||||
}
|
||||
} catch (RuntimeException restoreFailure) {
|
||||
e.addSuppressed(restoreFailure);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/** Verifies a pending sandbox number with the password it was sent. A verified one stays so. */
|
||||
public synchronized void verifySmsSandboxPhoneNumber(String phoneNumber, String oneTimePassword, String region) {
|
||||
requireSandboxPhoneNumber(phoneNumber);
|
||||
if (oneTimePassword == null || !ONE_TIME_PASSWORD.matcher(oneTimePassword).matches()) {
|
||||
throw new AwsException("InvalidParameter", "Invalid parameter: OneTimePassword", 400);
|
||||
}
|
||||
String key = smsSandboxKey(region, phoneNumber);
|
||||
SmsSandboxPhoneNumber number = smsSandboxStore.get(key)
|
||||
.orElseThrow(SnsService::sandboxPhoneNumberNotFound);
|
||||
if (SmsSandboxPhoneNumber.VERIFIED.equals(number.getStatus())) {
|
||||
return;
|
||||
}
|
||||
if (!oneTimePassword.equals(number.getOneTimePassword())) {
|
||||
throw new AwsException("VerificationException",
|
||||
"The one-time password does not match the one sent to " + phoneNumber + ".", 400);
|
||||
}
|
||||
number.setStatus(SmsSandboxPhoneNumber.VERIFIED);
|
||||
number.setOneTimePassword(null);
|
||||
smsSandboxStore.put(key, number);
|
||||
}
|
||||
|
||||
public synchronized void deleteSmsSandboxPhoneNumber(String phoneNumber, String region) {
|
||||
requireSandboxPhoneNumber(phoneNumber);
|
||||
String key = smsSandboxKey(region, phoneNumber);
|
||||
if (smsSandboxStore.get(key).isEmpty()) {
|
||||
throw sandboxPhoneNumberNotFound();
|
||||
}
|
||||
smsSandboxStore.delete(key);
|
||||
}
|
||||
|
||||
public PaginatedResult<SmsSandboxPhoneNumber> listSmsSandboxPhoneNumbers(Integer maxResults,
|
||||
String nextToken,
|
||||
String region) {
|
||||
String prefix = smsAttributesKey(region) + "::";
|
||||
return Pagination.paginate(smsSandboxStore.scan(key -> key.startsWith(prefix)),
|
||||
SmsSandboxPhoneNumber::getPhoneNumber, maxResults, nextToken, SANDBOX_PAGE_SIZE,
|
||||
"InvalidParameter");
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a number has opted out of SMS from this account. AWS opts a number out when its
|
||||
* owner replies STOP, and nothing reaches Floci that way, so no number is ever opted out.
|
||||
*/
|
||||
public boolean isPhoneNumberOptedOut(String phoneNumber) {
|
||||
requireE164PhoneNumber(phoneNumber);
|
||||
return false;
|
||||
}
|
||||
|
||||
/** The numbers opted out of SMS from this account, which for the reason above is none. */
|
||||
public List<String> listPhoneNumbersOptedOut() {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
/**
|
||||
* Opts a number back in to SMS. None is ever opted out here, and AWS answers success for a
|
||||
* number that is not opted out, so only the format is checked. AWS's limit of one opt-in per
|
||||
* number every 30 days can therefore never apply.
|
||||
*/
|
||||
public void optInPhoneNumber(String phoneNumber) {
|
||||
requireE164PhoneNumber(phoneNumber);
|
||||
}
|
||||
|
||||
private static void requireE164PhoneNumber(String phoneNumber) {
|
||||
if (phoneNumber == null || !E164_PHONE_NUMBER.matcher(phoneNumber).matches()) {
|
||||
throw invalidPhoneNumber();
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireSandboxPhoneNumber(String phoneNumber) {
|
||||
if (phoneNumber == null || phoneNumber.isEmpty()
|
||||
|| phoneNumber.length() > SANDBOX_PHONE_NUMBER_MAX_LENGTH
|
||||
|| !SANDBOX_PHONE_NUMBER.matcher(phoneNumber).matches()) {
|
||||
throw invalidPhoneNumber();
|
||||
}
|
||||
}
|
||||
|
||||
/** The answer AWS gives {@code OptInPhoneNumber} for a malformed number. */
|
||||
private static AwsException invalidPhoneNumber() {
|
||||
return new AwsException("InvalidParameter",
|
||||
"Invalid parameter: PhoneNumber Reason: input incorrectly formatted", 400);
|
||||
}
|
||||
|
||||
private static AwsException sandboxPhoneNumberNotFound() {
|
||||
return new AwsException("ResourceNotFound",
|
||||
"The phone number is not in the SMS sandbox.", 404);
|
||||
}
|
||||
|
||||
public Topic createTopic(String name, Map<String, String> attributes,
|
||||
Map<String, String> tags, String region) {
|
||||
if (name == null || name.isBlank()) {
|
||||
@@ -431,14 +634,33 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
attrs.putIfAbsent("Owner", regionResolver.getAccountId());
|
||||
attrs.putIfAbsent("EffectiveDeliveryPolicy", "{\"http\":{\"defaultHealthyRetryPolicy\":{\"minDelayTarget\":20,\"maxDelayTarget\":20,\"numRetries\":3,\"numMaxDelayRetries\":0,\"numNoDelayRetries\":0,\"numMinDelayRetries\":0,\"backoffFunction\":\"linear\"},\"disableSubscriptionOverrides\":false}}");
|
||||
if (!attrs.containsKey("Policy") || attrs.get("Policy") == null || attrs.get("Policy").isBlank()) {
|
||||
String account = regionResolver.getAccountId();
|
||||
attrs.put("Policy", "{\"Version\":\"2012-10-17\",\"Id\":\"__default_policy_ID\",\"Statement\":[{\"Sid\":\"__default_statement_ID\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":[\"SNS:GetTopicAttributes\",\"SNS:SetTopicAttributes\",\"SNS:AddPermission\",\"SNS:RemovePermission\",\"SNS:DeleteTopic\",\"SNS:Subscribe\",\"SNS:ListSubscriptionsByTopic\",\"SNS:Publish\"],\"Resource\":\"" + topicArn + "\",\"Condition\":{\"StringEquals\":{\"AWS:SourceAccount\":\"" + account + "\"}}}]}");
|
||||
attrs.put("Policy", defaultTopicPolicy(topicArn).toString());
|
||||
}
|
||||
return attrs;
|
||||
}
|
||||
|
||||
public void setTopicAttributes(String topicArn, String attributeName,
|
||||
String attributeValue, String region) {
|
||||
/**
|
||||
* The policy a topic has until one is set, as AWS returns it for a new topic: its owner may
|
||||
* manage it, subscribe to it and publish to it.
|
||||
*/
|
||||
private ObjectNode defaultTopicPolicy(String topicArn) {
|
||||
ObjectNode policy = objectMapper.createObjectNode();
|
||||
policy.put("Version", "2008-10-17");
|
||||
policy.put("Id", "__default_policy_ID");
|
||||
ObjectNode statement = policy.putArray("Statement").addObject();
|
||||
statement.put("Sid", "__default_statement_ID");
|
||||
statement.put("Effect", "Allow");
|
||||
statement.putObject("Principal").put("AWS", "*");
|
||||
ArrayNode actions = statement.putArray("Action");
|
||||
DEFAULT_TOPIC_POLICY_ACTIONS.forEach(actions::add);
|
||||
statement.put("Resource", topicArn);
|
||||
statement.putObject("Condition").putObject("StringEquals")
|
||||
.put("AWS:SourceOwner", regionResolver.getAccountId());
|
||||
return policy;
|
||||
}
|
||||
|
||||
public synchronized void setTopicAttributes(String topicArn, String attributeName,
|
||||
String attributeValue, String region) {
|
||||
String key = topicKey(region, topicArn);
|
||||
Topic topic = topicStore.get(key)
|
||||
.orElseThrow(() -> new AwsException("NotFound", "Topic does not exist.", 404));
|
||||
@@ -450,6 +672,125 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
topicStore.put(key, topic);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a statement to the topic's policy granting {@code accountIds} the named SNS actions,
|
||||
* written as AWS writes it: the label is its {@code Sid}, each account becomes its
|
||||
* {@code root} principal, and a single account or action is a string rather than a list of
|
||||
* one. A topic with no policy set gets the statement added to its default one.
|
||||
*
|
||||
* <p>The policy is stored and returned by {@code GetTopicAttributes}. Floci does not evaluate
|
||||
* topic policies when authorizing a request, so the grant has no effect beyond that.
|
||||
*
|
||||
* <p>This, {@link #removePermission} and {@link #setTopicAttributes} are synchronized: each
|
||||
* reads the policy, changes it and writes it back, so overlapping edits would otherwise lose
|
||||
* all but the last, and two grants under one label could both pass the duplicate check.
|
||||
*/
|
||||
public synchronized void addPermission(String topicArn, String label, List<String> accountIds,
|
||||
List<String> actionNames, String region) {
|
||||
requirePermissionParameter("TopicArn", topicArn != null && !topicArn.isBlank());
|
||||
requirePermissionParameter("Label", label != null && !label.isBlank());
|
||||
requirePermissionParameter("AWSAccountId", accountIds != null && !accountIds.isEmpty()
|
||||
&& accountIds.stream().allMatch(id -> id != null && ACCOUNT_ID.matcher(id).matches()));
|
||||
requirePermissionParameter("ActionName", actionNames != null && !actionNames.isEmpty());
|
||||
String key = topicKey(region, topicArn);
|
||||
Topic topic = topicStore.get(key).orElseThrow(SnsService::permissionTopicNotFound);
|
||||
for (String action : actionNames) {
|
||||
if (!SNS_POLICY_ACTIONS.contains(action)) {
|
||||
throw new AwsException("InvalidParameter",
|
||||
"Invalid parameter: Policy statement action out of service scope!", 400);
|
||||
}
|
||||
}
|
||||
ObjectNode policy = topicPolicy(topic, topicArn);
|
||||
ArrayNode statements = (ArrayNode) policy.get("Statement");
|
||||
for (JsonNode existing : statements) {
|
||||
if (label.equals(existing.path("Sid").asText(null))) {
|
||||
throw new AwsException("InvalidParameter", "Invalid parameter: Statement already exists", 400);
|
||||
}
|
||||
}
|
||||
String partition = AwsRegions.partitionFor(region);
|
||||
ObjectNode statement = statements.addObject();
|
||||
statement.put("Sid", label);
|
||||
statement.put("Effect", "Allow");
|
||||
putOneOrMany(statement.putObject("Principal"), "AWS", accountIds.stream()
|
||||
.map(accountId -> AwsArnUtils.Arn.global(partition, "iam", accountId, "root").toString())
|
||||
.toList());
|
||||
putOneOrMany(statement, "Action", actionNames.stream().map(action -> "SNS:" + action).toList());
|
||||
statement.put("Resource", topicArn);
|
||||
topic.getAttributes().put("Policy", policy.toString());
|
||||
topicStore.put(key, topic);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the statement {@code label} names from the topic's policy. A label the policy does
|
||||
* not hold leaves it as it is: AWS's reference names no error for that case and no recording
|
||||
* shows one, so this answers success rather than invent one.
|
||||
*/
|
||||
public synchronized void removePermission(String topicArn, String label, String region) {
|
||||
requirePermissionParameter("TopicArn", topicArn != null && !topicArn.isBlank());
|
||||
requirePermissionParameter("Label", label != null && !label.isBlank());
|
||||
String key = topicKey(region, topicArn);
|
||||
Topic topic = topicStore.get(key).orElseThrow(SnsService::permissionTopicNotFound);
|
||||
ObjectNode policy = topicPolicy(topic, topicArn);
|
||||
ArrayNode statements = (ArrayNode) policy.get("Statement");
|
||||
boolean removed = false;
|
||||
for (int i = statements.size() - 1; i >= 0; i--) {
|
||||
if (label.equals(statements.get(i).path("Sid").asText(null))) {
|
||||
statements.remove(i);
|
||||
removed = true;
|
||||
}
|
||||
}
|
||||
if (removed) {
|
||||
topic.getAttributes().put("Policy", policy.toString());
|
||||
topicStore.put(key, topic);
|
||||
}
|
||||
}
|
||||
|
||||
/** The topic's policy as a document to edit, the default one if none is set, with its statements in a list. */
|
||||
private ObjectNode topicPolicy(Topic topic, String topicArn) {
|
||||
String stored = topic.getAttributes().get("Policy");
|
||||
if (stored == null || stored.isBlank()) {
|
||||
return defaultTopicPolicy(topicArn);
|
||||
}
|
||||
JsonNode parsed;
|
||||
try {
|
||||
parsed = objectMapper.readTree(stored);
|
||||
} catch (JsonProcessingException e) {
|
||||
parsed = null;
|
||||
}
|
||||
if (!(parsed instanceof ObjectNode policy)) {
|
||||
throw new AwsException("InvalidParameter",
|
||||
"Invalid parameter: Policy Error: the topic's policy is not a JSON object", 400);
|
||||
}
|
||||
JsonNode statement = policy.get("Statement");
|
||||
if (!(statement instanceof ArrayNode)) {
|
||||
ArrayNode statements = objectMapper.createArrayNode();
|
||||
if (statement != null && !statement.isNull()) {
|
||||
statements.add(statement);
|
||||
}
|
||||
policy.set("Statement", statements);
|
||||
}
|
||||
return policy;
|
||||
}
|
||||
|
||||
private static void putOneOrMany(ObjectNode node, String field, List<String> values) {
|
||||
if (values.size() == 1) {
|
||||
node.put(field, values.get(0));
|
||||
} else {
|
||||
values.forEach(node.putArray(field)::add);
|
||||
}
|
||||
}
|
||||
|
||||
private static void requirePermissionParameter(String name, boolean present) {
|
||||
if (!present) {
|
||||
throw new AwsException("InvalidParameter", "Invalid parameter: " + name, 400);
|
||||
}
|
||||
}
|
||||
|
||||
/** What AWS answers {@code AddPermission} and {@code RemovePermission} for a missing topic. */
|
||||
private static AwsException permissionTopicNotFound() {
|
||||
return new AwsException("NotFound", "Topic does not exist", 404);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse and range-check a {@code MaximumMessageSize} attribute value, returning it. AWS
|
||||
* parses strictly: surrounding whitespace, a decimal point and an empty value are all
|
||||
@@ -683,13 +1024,7 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
// Send SMS
|
||||
if (phoneNumber != null) {
|
||||
requireWithinMaxMessageSize(payloadSize, DEFAULT_MAX_MESSAGE_SIZE);
|
||||
String messageId = UUID.randomUUID().toString();
|
||||
String effectiveRegion = region != null ? region : regionResolver.getDefaultRegion();
|
||||
SentSms sms = new SentSms(messageId, effectiveRegion, phoneNumber,
|
||||
message, subject, Instant.now());
|
||||
smsStore.put("sms::" + effectiveRegion + "::" + messageId, sms);
|
||||
LOG.infov("SNS SMS published: messageId={0} phone={1}", messageId, phoneNumber);
|
||||
return messageId;
|
||||
return recordSms(phoneNumber, message, subject, region);
|
||||
}
|
||||
|
||||
// Send a message to topic or directly to a target ARN
|
||||
@@ -2343,6 +2678,20 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
return regionResolver.getAccountId() + "::" + region;
|
||||
}
|
||||
|
||||
private String smsSandboxKey(String region, String phoneNumber) {
|
||||
return smsAttributesKey(region) + "::" + phoneNumber;
|
||||
}
|
||||
|
||||
/** Sends an SMS, which here means storing it for {@code GET /_aws/sns} to read back. */
|
||||
private String recordSms(String phoneNumber, String message, String subject, String region) {
|
||||
String messageId = UUID.randomUUID().toString();
|
||||
String effectiveRegion = region != null ? region : regionResolver.getDefaultRegion();
|
||||
SentSms sms = new SentSms(messageId, effectiveRegion, phoneNumber, message, subject, Instant.now());
|
||||
smsStore.put("sms::" + effectiveRegion + "::" + messageId, sms);
|
||||
LOG.infov("SNS SMS published: messageId={0} phone={1}", messageId, phoneNumber);
|
||||
return messageId;
|
||||
}
|
||||
|
||||
private static String topicKey(String region, String arn) {
|
||||
return "topic::" + region + "::" + arn;
|
||||
}
|
||||
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package io.github.hectorvent.floci.services.sns.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
/**
|
||||
* A destination phone number in an account's SMS sandbox for one region, with its verification
|
||||
* status and the one-time password sent to it while it is still {@code Pending}.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class SmsSandboxPhoneNumber {
|
||||
|
||||
public static final String PENDING = "Pending";
|
||||
public static final String VERIFIED = "Verified";
|
||||
|
||||
@JsonProperty("PhoneNumber")
|
||||
private String phoneNumber;
|
||||
|
||||
@JsonProperty("Status")
|
||||
private String status;
|
||||
|
||||
@JsonProperty("OneTimePassword")
|
||||
private String oneTimePassword;
|
||||
|
||||
public SmsSandboxPhoneNumber() {}
|
||||
|
||||
public SmsSandboxPhoneNumber(String phoneNumber, String status, String oneTimePassword) {
|
||||
this.phoneNumber = phoneNumber;
|
||||
this.status = status;
|
||||
this.oneTimePassword = oneTimePassword;
|
||||
}
|
||||
|
||||
public String getPhoneNumber() { return phoneNumber; }
|
||||
public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; }
|
||||
|
||||
public String getStatus() { return status; }
|
||||
public void setStatus(String status) { this.status = status; }
|
||||
|
||||
public String getOneTimePassword() { return oneTimePassword; }
|
||||
public void setOneTimePassword(String oneTimePassword) { this.oneTimePassword = oneTimePassword; }
|
||||
}
|
||||
+286
@@ -0,0 +1,286 @@
|
||||
package io.github.hectorvent.floci.services.sns;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.sns.model.SentSms;
|
||||
import io.github.hectorvent.floci.services.sns.model.SmsSandboxPhoneNumber;
|
||||
import io.github.hectorvent.floci.services.sns.model.Topic;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
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.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.ThreadStates.awaitBlockedOrDone;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
|
||||
/**
|
||||
* Edits to a topic's policy and to an SMS sandbox number read a record, change it and write it
|
||||
* back. These tests park the first of two overlapping edits inside that window and check the
|
||||
* second waits for it, and that a sandbox number whose verification text fails is put back.
|
||||
*/
|
||||
class SnsPolicyAndSandboxEditsTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final String GRANTEE = "111122223333";
|
||||
private static final String BASE_URL = "http://localhost:4566";
|
||||
private static final String PHONE_NUMBER = "+15555550100";
|
||||
|
||||
@Test
|
||||
void anOverlappingGrantWaitsSoBothGrantsLand() throws Exception {
|
||||
ObjectMapper mapper = Mockito.spy(new ObjectMapper());
|
||||
SnsService service = service(new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
mapper);
|
||||
String topicArn = service.createTopic("edits", null, null, REGION).getTopicArn();
|
||||
service.addPermission(topicArn, "existing", List.of(GRANTEE), List.of("Publish"), REGION);
|
||||
|
||||
whileAGrantIsParked(mapper, service, topicArn,
|
||||
() -> service.addPermission(topicArn, "second", List.of(GRANTEE), List.of("Subscribe"), REGION));
|
||||
|
||||
String policy = service.getTopicAttributes(topicArn, REGION).get("Policy");
|
||||
for (String sid : List.of("existing", "first", "second")) {
|
||||
assertTrue(policy.contains("\"Sid\":\"" + sid + "\""), sid + " is missing from " + policy);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPolicySetWhileAGrantIsBeingWrittenIsNotUndone() throws Exception {
|
||||
ObjectMapper mapper = Mockito.spy(new ObjectMapper());
|
||||
SnsService service = service(new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
mapper);
|
||||
String topicArn = service.createTopic("replaced", null, null, REGION).getTopicArn();
|
||||
service.addPermission(topicArn, "existing", List.of(GRANTEE), List.of("Publish"), REGION);
|
||||
String replacement = "{\"Version\":\"2008-10-17\",\"Statement\":[]}";
|
||||
|
||||
whileAGrantIsParked(mapper, service, topicArn,
|
||||
() -> service.setTopicAttributes(topicArn, "Policy", replacement, REGION));
|
||||
|
||||
assertEquals(replacement, service.getTopicAttributes(topicArn, REGION).get("Policy"),
|
||||
"the grant wrote back a policy read before it was replaced");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRemovalWhileAGrantIsBeingWrittenIsNotUndone() throws Exception {
|
||||
ObjectMapper mapper = Mockito.spy(new ObjectMapper());
|
||||
SnsService service = service(new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
mapper);
|
||||
String topicArn = service.createTopic("removed", null, null, REGION).getTopicArn();
|
||||
service.addPermission(topicArn, "existing", List.of(GRANTEE), List.of("Publish"), REGION);
|
||||
|
||||
whileAGrantIsParked(mapper, service, topicArn,
|
||||
() -> service.removePermission(topicArn, "existing", REGION));
|
||||
|
||||
String policy = service.getTopicAttributes(topicArn, REGION).get("Policy");
|
||||
assertTrue(policy.contains("\"Sid\":\"first\""), "the grant is missing from " + policy);
|
||||
assertTrue(!policy.contains("\"Sid\":\"existing\""), "the removed statement came back in " + policy);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aVerificationCannotBringBackANumberDeletedMeanwhile() throws Exception {
|
||||
ParkingStore<SmsSandboxPhoneNumber> sandbox = new ParkingStore<>();
|
||||
InMemoryStorage<String, SentSms> texts = new InMemoryStorage<>();
|
||||
SnsService service = service(new InMemoryStorage<>(), texts, sandbox, new ObjectMapper());
|
||||
service.createSmsSandboxPhoneNumber(PHONE_NUMBER, null, REGION);
|
||||
String code = onlyCodeTexted(service);
|
||||
|
||||
// The verification parks right after reading the number, before writing it back verified.
|
||||
sandbox.parkNextRead();
|
||||
ExecutorService pool = Executors.newFixedThreadPool(2);
|
||||
try {
|
||||
Future<?> verify = pool.submit(() -> service.verifySmsSandboxPhoneNumber(PHONE_NUMBER, code, REGION));
|
||||
assertTrue(sandbox.awaitParked(), "the verification must reach its read of the number");
|
||||
AtomicReference<Thread> deleteThread = new AtomicReference<>();
|
||||
Future<?> delete = pool.submit(() -> {
|
||||
deleteThread.set(Thread.currentThread());
|
||||
service.deleteSmsSandboxPhoneNumber(PHONE_NUMBER, REGION);
|
||||
});
|
||||
awaitBlockedOrDone(deleteThread, delete);
|
||||
|
||||
sandbox.release();
|
||||
verify.get(2, TimeUnit.SECONDS);
|
||||
delete.get(2, TimeUnit.SECONDS);
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
|
||||
assertTrue(service.listSmsSandboxPhoneNumbers(null, null, REGION).items().isEmpty(),
|
||||
"a number deleted while it was being verified came back");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aResendCannotBringBackANumberDeletedMeanwhile() throws Exception {
|
||||
ParkingStore<SmsSandboxPhoneNumber> sandbox = new ParkingStore<>();
|
||||
SnsService service = service(new InMemoryStorage<>(), new InMemoryStorage<>(), sandbox, new ObjectMapper());
|
||||
service.createSmsSandboxPhoneNumber(PHONE_NUMBER, null, REGION);
|
||||
|
||||
// The resend parks right after reading the number, before storing its fresh code.
|
||||
sandbox.parkNextRead();
|
||||
ExecutorService pool = Executors.newFixedThreadPool(2);
|
||||
try {
|
||||
Future<?> resend = pool.submit(() -> service.createSmsSandboxPhoneNumber(PHONE_NUMBER, null, REGION));
|
||||
assertTrue(sandbox.awaitParked(), "the resend must reach its read of the number");
|
||||
AtomicReference<Thread> deleteThread = new AtomicReference<>();
|
||||
Future<?> delete = pool.submit(() -> {
|
||||
deleteThread.set(Thread.currentThread());
|
||||
service.deleteSmsSandboxPhoneNumber(PHONE_NUMBER, REGION);
|
||||
});
|
||||
awaitBlockedOrDone(deleteThread, delete);
|
||||
|
||||
sandbox.release();
|
||||
resend.get(2, TimeUnit.SECONDS);
|
||||
delete.get(2, TimeUnit.SECONDS);
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
|
||||
assertTrue(service.listSmsSandboxPhoneNumbers(null, null, REGION).items().isEmpty(),
|
||||
"a number deleted while a code was being resent came back");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aNumberWhoseTextFailsIsNotLeftBehind() {
|
||||
FailingTexts texts = new FailingTexts();
|
||||
SnsService service = service(new InMemoryStorage<>(), texts, new InMemoryStorage<>(), new ObjectMapper());
|
||||
|
||||
texts.failing = true;
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> service.createSmsSandboxPhoneNumber(PHONE_NUMBER, null, REGION));
|
||||
|
||||
assertTrue(service.listSmsSandboxPhoneNumbers(null, null, REGION).items().isEmpty(),
|
||||
"a number nobody could receive a code for was left pending");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aResendWhoseTextFailsKeepsTheCodeAlreadySent() {
|
||||
FailingTexts texts = new FailingTexts();
|
||||
SnsService service = service(new InMemoryStorage<>(), texts, new InMemoryStorage<>(), new ObjectMapper());
|
||||
service.createSmsSandboxPhoneNumber(PHONE_NUMBER, null, REGION);
|
||||
String firstCode = onlyCodeTexted(service);
|
||||
|
||||
texts.failing = true;
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> service.createSmsSandboxPhoneNumber(PHONE_NUMBER, null, REGION));
|
||||
texts.failing = false;
|
||||
|
||||
service.verifySmsSandboxPhoneNumber(PHONE_NUMBER, firstCode, REGION);
|
||||
assertEquals(SmsSandboxPhoneNumber.VERIFIED,
|
||||
service.listSmsSandboxPhoneNumbers(null, null, REGION).items().get(0).getStatus());
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a grant labelled {@code first} and parks it right after it parses the stored policy,
|
||||
* before it writes the policy back, then runs {@code overlapping} on another thread. Correct
|
||||
* code leaves {@code overlapping} blocked until the grant is released.
|
||||
*/
|
||||
private static void whileAGrantIsParked(ObjectMapper mapper, SnsService service, String topicArn,
|
||||
Runnable overlapping) throws Exception {
|
||||
CountDownLatch parsed = new CountDownLatch(1);
|
||||
CountDownLatch resume = new CountDownLatch(1);
|
||||
AtomicBoolean parkNext = new AtomicBoolean(true);
|
||||
doAnswer(invocation -> {
|
||||
Object tree = invocation.callRealMethod();
|
||||
if (parkNext.getAndSet(false)) {
|
||||
parsed.countDown();
|
||||
resume.await();
|
||||
}
|
||||
return tree;
|
||||
}).when(mapper).readTree(anyString());
|
||||
|
||||
ExecutorService pool = Executors.newFixedThreadPool(2);
|
||||
try {
|
||||
Future<?> grant = pool.submit(() ->
|
||||
service.addPermission(topicArn, "first", List.of(GRANTEE), List.of("Publish"), REGION));
|
||||
assertTrue(parsed.await(2, TimeUnit.SECONDS), "the grant must reach its parse of the policy");
|
||||
AtomicReference<Thread> overlappingThread = new AtomicReference<>();
|
||||
Future<?> other = pool.submit(() -> {
|
||||
overlappingThread.set(Thread.currentThread());
|
||||
overlapping.run();
|
||||
});
|
||||
awaitBlockedOrDone(overlappingThread, other);
|
||||
|
||||
resume.countDown();
|
||||
grant.get(2, TimeUnit.SECONDS);
|
||||
other.get(2, TimeUnit.SECONDS);
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
private static SnsService service(InMemoryStorage<String, Topic> topics, InMemoryStorage<String, SentSms> texts,
|
||||
InMemoryStorage<String, SmsSandboxPhoneNumber> sandbox, ObjectMapper mapper) {
|
||||
return new SnsService(topics, new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
texts, new InMemoryStorage<>(), sandbox, new RegionResolver(REGION, ACCOUNT), null, null, null,
|
||||
BASE_URL, mapper, SnsMessageSigner.inMemory(BASE_URL));
|
||||
}
|
||||
|
||||
private static String onlyCodeTexted(SnsService service) {
|
||||
List<SentSms> sent = service.getSentMessages();
|
||||
assertEquals(1, sent.size(), "expected exactly one verification text, got " + sent.size());
|
||||
Matcher code = Pattern.compile("[0-9]{6}").matcher(sent.get(0).getMessage());
|
||||
assertTrue(code.find(), "no code in " + sent);
|
||||
return code.group();
|
||||
}
|
||||
|
||||
/** A store whose next read, once armed, parks after reading until released. */
|
||||
private static final class ParkingStore<V> extends InMemoryStorage<String, V> {
|
||||
|
||||
private final AtomicBoolean parkNext = new AtomicBoolean();
|
||||
private final CountDownLatch parked = new CountDownLatch(1);
|
||||
private final CountDownLatch released = new CountDownLatch(1);
|
||||
|
||||
void parkNextRead() {
|
||||
parkNext.set(true);
|
||||
}
|
||||
|
||||
boolean awaitParked() throws InterruptedException {
|
||||
return parked.await(2, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
void release() {
|
||||
released.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<V> get(String key) {
|
||||
Optional<V> value = super.get(key);
|
||||
if (parkNext.getAndSet(false)) {
|
||||
parked.countDown();
|
||||
try {
|
||||
released.await();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
/** Where SNS records the SMS it sends, made to fail on demand. */
|
||||
private static final class FailingTexts extends InMemoryStorage<String, SentSms> {
|
||||
|
||||
private volatile boolean failing;
|
||||
|
||||
@Override
|
||||
public void put(String key, SentSms value) {
|
||||
if (failing) {
|
||||
throw new IllegalStateException("the SMS store is unavailable");
|
||||
}
|
||||
super.put(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
+236
@@ -0,0 +1,236 @@
|
||||
package io.github.hectorvent.floci.services.sns;
|
||||
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.path.xml.XmlPath;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.empty;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* The SMS sandbox and opt-out operations. Floci answers as an account that has left the sandbox,
|
||||
* since it delivers SMS to any number, and a verification code is "texted" the way every SMS is
|
||||
* here: stored for {@code GET /_aws/sns} to read back.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class SnsSmsSandboxIntegrationTest {
|
||||
|
||||
private static final Pattern CODE = Pattern.compile("[0-9]{6}");
|
||||
private static final String INVALID_PHONE_NUMBER =
|
||||
"Invalid parameter: PhoneNumber Reason: input incorrectly formatted";
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void theAccountIsOutsideTheSandbox() {
|
||||
query("GetSMSSandboxAccountStatus", Map.of())
|
||||
.statusCode(200)
|
||||
.body("GetSMSSandboxAccountStatusResponse.GetSMSSandboxAccountStatusResult.IsInSandbox",
|
||||
equalTo("false"));
|
||||
json("GetSMSSandboxAccountStatus", "{}")
|
||||
.statusCode(200)
|
||||
.body("IsInSandbox", equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSandboxNumberIsVerifiedWithTheCodeTextedToIt() {
|
||||
String phoneNumber = addSandboxNumber();
|
||||
assertEquals("Pending", statusOf(phoneNumber));
|
||||
|
||||
String code = codeTextedTo(phoneNumber);
|
||||
String wrong = String.format("%06d", (Integer.parseInt(code) + 1) % 1_000_000);
|
||||
query("VerifySMSSandboxPhoneNumber", Map.of("PhoneNumber", phoneNumber, "OneTimePassword", wrong))
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("VerificationException"));
|
||||
assertEquals("Pending", statusOf(phoneNumber));
|
||||
|
||||
query("VerifySMSSandboxPhoneNumber", Map.of("PhoneNumber", phoneNumber, "OneTimePassword", code))
|
||||
.statusCode(200);
|
||||
assertEquals("Verified", statusOf(phoneNumber));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDeletedNumberIsGoneAndCannotBeDeletedTwice() {
|
||||
String phoneNumber = addSandboxNumber();
|
||||
|
||||
query("DeleteSMSSandboxPhoneNumber", Map.of("PhoneNumber", phoneNumber)).statusCode(200);
|
||||
|
||||
assertTrue(!allSandboxNumbers().containsKey(phoneNumber), phoneNumber + " is still listed");
|
||||
query("DeleteSMSSandboxPhoneNumber", Map.of("PhoneNumber", phoneNumber))
|
||||
.statusCode(404)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ResourceNotFound"));
|
||||
query("VerifySMSSandboxPhoneNumber", Map.of("PhoneNumber", phoneNumber, "OneTimePassword", "123456"))
|
||||
.statusCode(404)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ResourceNotFound"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theListPagesThroughMaxResults() {
|
||||
List<String> added = List.of(addSandboxNumber(), addSandboxNumber(), addSandboxNumber());
|
||||
|
||||
List<String> seen = new ArrayList<>();
|
||||
String nextToken = null;
|
||||
do {
|
||||
Map<String, String> params = new HashMap<>(Map.of("MaxResults", "1"));
|
||||
if (nextToken != null) {
|
||||
params.put("NextToken", nextToken);
|
||||
}
|
||||
XmlPath page = query("ListSMSSandboxPhoneNumbers", params).statusCode(200).extract().xmlPath();
|
||||
List<String> numbers = page.getList(
|
||||
"ListSMSSandboxPhoneNumbersResponse.ListSMSSandboxPhoneNumbersResult.PhoneNumbers.member.PhoneNumber");
|
||||
assertTrue(numbers.size() <= 1, "a page held " + numbers);
|
||||
seen.addAll(numbers);
|
||||
nextToken = page.getString(
|
||||
"ListSMSSandboxPhoneNumbersResponse.ListSMSSandboxPhoneNumbersResult.NextToken");
|
||||
} while (nextToken != null && !nextToken.isEmpty());
|
||||
|
||||
assertTrue(seen.containsAll(added), "paging returned " + seen + ", missing some of " + added);
|
||||
query("ListSMSSandboxPhoneNumbers", Map.of("MaxResults", "101"))
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameter"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMalformedSandboxNumberIsRefused() {
|
||||
query("CreateSMSSandboxPhoneNumber", Map.of("PhoneNumber", "not-a-number"))
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameter"))
|
||||
.body("ErrorResponse.Error.Message", equalTo(INVALID_PHONE_NUMBER));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theJsonProtocolReachesTheSameSandbox() {
|
||||
String phoneNumber = randomPhoneNumber();
|
||||
cleanup.register(() -> query("DeleteSMSSandboxPhoneNumber", Map.of("PhoneNumber", phoneNumber)));
|
||||
json("CreateSMSSandboxPhoneNumber", "{\"PhoneNumber\": \"%s\", \"LanguageCode\": \"en-US\"}"
|
||||
.formatted(phoneNumber))
|
||||
.statusCode(200);
|
||||
|
||||
json("VerifySMSSandboxPhoneNumber", "{\"PhoneNumber\": \"%s\", \"OneTimePassword\": \"%s\"}"
|
||||
.formatted(phoneNumber, codeTextedTo(phoneNumber)))
|
||||
.statusCode(200);
|
||||
|
||||
assertEquals("Verified", statusOf(phoneNumber));
|
||||
json("ListSMSSandboxPhoneNumbers", "{\"MaxResults\": 100}")
|
||||
.statusCode(200)
|
||||
.body("PhoneNumbers.PhoneNumber", hasItem(phoneNumber));
|
||||
}
|
||||
|
||||
@Test
|
||||
void noNumberIsEverOptedOut() {
|
||||
query("CheckIfPhoneNumberIsOptedOut", Map.of("phoneNumber", "+4411111111"))
|
||||
.statusCode(200)
|
||||
.body("CheckIfPhoneNumberIsOptedOutResponse.CheckIfPhoneNumberIsOptedOutResult.isOptedOut",
|
||||
equalTo("false"));
|
||||
List<String> optedOut = query("ListPhoneNumbersOptedOut", Map.of())
|
||||
.statusCode(200)
|
||||
.extract().xmlPath()
|
||||
.getList("ListPhoneNumbersOptedOutResponse.ListPhoneNumbersOptedOutResult.phoneNumbers.member");
|
||||
assertTrue(optedOut.isEmpty(), "opted out: " + optedOut);
|
||||
json("ListPhoneNumbersOptedOut", "{}")
|
||||
.statusCode(200)
|
||||
.body("phoneNumbers", empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void optingInANumberThatIsNotOptedOutSucceeds() {
|
||||
query("OptInPhoneNumber", Map.of("phoneNumber", "+4411111111")).statusCode(200);
|
||||
json("OptInPhoneNumber", "{\"phoneNumber\": \"+4411111111\"}").statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMalformedNumberIsRefusedByTheOptOutCalls() {
|
||||
query("OptInPhoneNumber", Map.of("phoneNumber", "invalid"))
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameter"))
|
||||
.body("ErrorResponse.Error.Message", equalTo(INVALID_PHONE_NUMBER));
|
||||
query("CheckIfPhoneNumberIsOptedOut", Map.of("phoneNumber", "invalid"))
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameter"));
|
||||
}
|
||||
|
||||
private String addSandboxNumber() {
|
||||
String phoneNumber = randomPhoneNumber();
|
||||
cleanup.register(() -> query("DeleteSMSSandboxPhoneNumber", Map.of("PhoneNumber", phoneNumber)));
|
||||
query("CreateSMSSandboxPhoneNumber", Map.of("PhoneNumber", phoneNumber, "LanguageCode", "en-US"))
|
||||
.statusCode(200);
|
||||
return phoneNumber;
|
||||
}
|
||||
|
||||
/** A number of its own for each test, so one test's sandbox entry and texts are not another's. */
|
||||
private static String randomPhoneNumber() {
|
||||
return String.format("+1555%07d", ThreadLocalRandom.current().nextInt(10_000_000));
|
||||
}
|
||||
|
||||
private static String codeTextedTo(String phoneNumber) {
|
||||
List<String> texts = given()
|
||||
.queryParam("phone", phoneNumber)
|
||||
.when().get("/_aws/sns")
|
||||
.then().statusCode(200)
|
||||
.extract().jsonPath().getList("messages.Message");
|
||||
assertEquals(1, texts.size(), "expected one verification text to " + phoneNumber + ", got " + texts);
|
||||
Matcher code = CODE.matcher(texts.get(0));
|
||||
assertTrue(code.find(), "no six-digit code in " + texts.get(0));
|
||||
return code.group();
|
||||
}
|
||||
|
||||
private static String statusOf(String phoneNumber) {
|
||||
String status = allSandboxNumbers().get(phoneNumber);
|
||||
assertTrue(status != null, phoneNumber + " is not listed");
|
||||
return status;
|
||||
}
|
||||
|
||||
private static Map<String, String> allSandboxNumbers() {
|
||||
XmlPath page = query("ListSMSSandboxPhoneNumbers", Map.of("MaxResults", "100"))
|
||||
.statusCode(200).extract().xmlPath();
|
||||
String members = "ListSMSSandboxPhoneNumbersResponse.ListSMSSandboxPhoneNumbersResult.PhoneNumbers.member";
|
||||
List<String> numbers = page.getList(members + ".PhoneNumber");
|
||||
List<String> statuses = page.getList(members + ".Status");
|
||||
Map<String, String> byNumber = new HashMap<>();
|
||||
for (int i = 0; i < numbers.size(); i++) {
|
||||
byNumber.put(numbers.get(i), statuses.get(i));
|
||||
}
|
||||
return byNumber;
|
||||
}
|
||||
|
||||
private static ValidatableResponse query(String action, Map<String, String> params) {
|
||||
RequestSpecification spec = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", action)
|
||||
.formParam("Version", "2010-03-31");
|
||||
params.forEach(spec::formParam);
|
||||
return spec.when().post("/").then();
|
||||
}
|
||||
|
||||
private static ValidatableResponse json(String action, String body) {
|
||||
return given()
|
||||
.contentType("application/x-amz-json-1.0")
|
||||
.header("X-Amz-Target", "SNS_20100331." + action)
|
||||
.body(body)
|
||||
.when().post("/").then();
|
||||
}
|
||||
}
|
||||
+231
@@ -0,0 +1,231 @@
|
||||
package io.github.hectorvent.floci.services.sns;
|
||||
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.path.json.JsonPath;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* {@code AddPermission} and {@code RemovePermission}, which edit a topic's policy. The expected
|
||||
* documents are the ones AWS returned in LocalStack's recordings of the same calls against a live
|
||||
* account ({@code test_topic_add_permission}, {@code test_topic_add_multiple_permissions},
|
||||
* {@code test_topic_remove_permission}, {@code test_add_permission_errors}), and the default
|
||||
* policy is the one AWS returns for a new topic ({@code test_create_topic_with_attributes}).
|
||||
*/
|
||||
@QuarkusTest
|
||||
class SnsTopicPermissionIntegrationTest {
|
||||
|
||||
private static final String ACCOUNT_ID = "000000000000";
|
||||
private static final String GRANTEE = "111122223333";
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void aNewTopicCarriesTheDefaultPolicyAwsReturns() {
|
||||
String topicArn = createTopic();
|
||||
|
||||
JsonPath policy = policyOf(topicArn);
|
||||
assertEquals("2008-10-17", policy.getString("Version"));
|
||||
assertEquals("__default_policy_ID", policy.getString("Id"));
|
||||
assertEquals(1, policy.getList("Statement").size());
|
||||
assertEquals("__default_statement_ID", policy.getString("Statement[0].Sid"));
|
||||
assertEquals("*", policy.getString("Statement[0].Principal.AWS"));
|
||||
assertEquals(List.of("SNS:GetTopicAttributes", "SNS:SetTopicAttributes", "SNS:AddPermission",
|
||||
"SNS:RemovePermission", "SNS:DeleteTopic", "SNS:Subscribe",
|
||||
"SNS:ListSubscriptionsByTopic", "SNS:Publish"),
|
||||
policy.getList("Statement[0].Action"));
|
||||
assertEquals(topicArn, policy.getString("Statement[0].Resource"));
|
||||
assertEquals(ACCOUNT_ID, policy.getString("Statement[0].Condition.StringEquals.'AWS:SourceOwner'"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void addPermissionAppendsALabelledStatementToTheDefaultPolicy() {
|
||||
String topicArn = createTopic();
|
||||
|
||||
query("AddPermission", Map.of("TopicArn", topicArn, "Label", "publish-grant",
|
||||
"AWSAccountId.member.1", GRANTEE, "ActionName.member.1", "Publish"))
|
||||
.statusCode(200);
|
||||
|
||||
JsonPath policy = policyOf(topicArn);
|
||||
assertEquals(2, policy.getList("Statement").size());
|
||||
assertEquals("__default_statement_ID", policy.getString("Statement[0].Sid"));
|
||||
assertEquals("publish-grant", policy.getString("Statement[1].Sid"));
|
||||
assertEquals("Allow", policy.getString("Statement[1].Effect"));
|
||||
// One account and one action are written as strings, not lists of one.
|
||||
assertEquals("arn:aws:iam::" + GRANTEE + ":root", policy.getString("Statement[1].Principal.AWS"));
|
||||
assertEquals("SNS:Publish", policy.getString("Statement[1].Action"));
|
||||
assertEquals(topicArn, policy.getString("Statement[1].Resource"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void severalAccountsAndActionsAreWrittenAsLists() {
|
||||
String topicArn = createTopic();
|
||||
|
||||
query("AddPermission", Map.of("TopicArn", topicArn, "Label", "two-grants",
|
||||
"AWSAccountId.member.1", GRANTEE, "AWSAccountId.member.2", ACCOUNT_ID,
|
||||
"ActionName.member.1", "Publish", "ActionName.member.2", "Subscribe"))
|
||||
.statusCode(200);
|
||||
|
||||
JsonPath policy = policyOf(topicArn);
|
||||
assertEquals(List.of("arn:aws:iam::" + GRANTEE + ":root", "arn:aws:iam::" + ACCOUNT_ID + ":root"),
|
||||
policy.getList("Statement[1].Principal.AWS"));
|
||||
assertEquals(List.of("SNS:Publish", "SNS:Subscribe"), policy.getList("Statement[1].Action"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLabelAlreadyInThePolicyIsRefused() {
|
||||
String topicArn = createTopic();
|
||||
query("AddPermission", Map.of("TopicArn", topicArn, "Label", "taken",
|
||||
"AWSAccountId.member.1", GRANTEE, "ActionName.member.1", "Publish"))
|
||||
.statusCode(200);
|
||||
|
||||
query("AddPermission", Map.of("TopicArn", topicArn, "Label", "taken",
|
||||
"AWSAccountId.member.1", GRANTEE, "ActionName.member.1", "AddPermission"))
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameter"))
|
||||
.body("ErrorResponse.Error.Message", equalTo("Invalid parameter: Statement already exists"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anActionSnsDoesNotHaveIsRefused() {
|
||||
String topicArn = createTopic();
|
||||
|
||||
query("AddPermission", Map.of("TopicArn", topicArn, "Label", "bogus",
|
||||
"AWSAccountId.member.1", GRANTEE, "ActionName.member.1", "InvalidAction"))
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameter"))
|
||||
.body("ErrorResponse.Error.Message",
|
||||
equalTo("Invalid parameter: Policy statement action out of service scope!"));
|
||||
assertEquals(1, policyOf(topicArn).getList("Statement").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAccountIdThatIsNotTwelveDigitsIsRefusedOnBothProtocols() {
|
||||
String topicArn = createTopic();
|
||||
|
||||
query("AddPermission", Map.of("TopicArn", topicArn, "Label", "bad-account",
|
||||
"AWSAccountId.member.1", GRANTEE, "AWSAccountId.member.2", "abc:def",
|
||||
"ActionName.member.1", "Publish"))
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameter"));
|
||||
json("AddPermission", """
|
||||
{"TopicArn": "%s", "Label": "bad-account", "AWSAccountId": ["abc:def"], "ActionName": ["Publish"]}
|
||||
""".formatted(topicArn))
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameter"));
|
||||
|
||||
assertEquals(1, policyOf(topicArn).getList("Statement").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void bothCallsAnswerNotFoundForAMissingTopic() {
|
||||
String missing = createTopic() + "-not-existing";
|
||||
|
||||
query("AddPermission", Map.of("TopicArn", missing, "Label", "any",
|
||||
"AWSAccountId.member.1", GRANTEE, "ActionName.member.1", "Publish"))
|
||||
.statusCode(404)
|
||||
.body("ErrorResponse.Error.Code", equalTo("NotFound"))
|
||||
.body("ErrorResponse.Error.Message", equalTo("Topic does not exist"));
|
||||
query("RemovePermission", Map.of("TopicArn", missing, "Label", "any"))
|
||||
.statusCode(404)
|
||||
.body("ErrorResponse.Error.Code", equalTo("NotFound"))
|
||||
.body("ErrorResponse.Error.Message", equalTo("Topic does not exist"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void removePermissionTakesTheStatementBackOut() {
|
||||
String topicArn = createTopic();
|
||||
query("AddPermission", Map.of("TopicArn", topicArn, "Label", "temporary",
|
||||
"AWSAccountId.member.1", GRANTEE, "ActionName.member.1", "Publish"))
|
||||
.statusCode(200);
|
||||
|
||||
query("RemovePermission", Map.of("TopicArn", topicArn, "Label", "temporary")).statusCode(200);
|
||||
|
||||
JsonPath policy = policyOf(topicArn);
|
||||
assertEquals(1, policy.getList("Statement").size());
|
||||
assertEquals("__default_statement_ID", policy.getString("Statement[0].Sid"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void removingALabelThePolicyDoesNotHoldLeavesItAsItIs() {
|
||||
String topicArn = createTopic();
|
||||
String before = rawPolicyOf(topicArn);
|
||||
|
||||
query("RemovePermission", Map.of("TopicArn", topicArn, "Label", "never-added")).statusCode(200);
|
||||
|
||||
assertEquals(before, rawPolicyOf(topicArn));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theJsonProtocolEditsTheSamePolicy() {
|
||||
String topicArn = createTopic();
|
||||
|
||||
json("AddPermission", """
|
||||
{"TopicArn": "%s", "Label": "json-grant", "AWSAccountId": ["%s"], "ActionName": ["Publish"]}
|
||||
""".formatted(topicArn, GRANTEE))
|
||||
.statusCode(200);
|
||||
assertEquals("json-grant", policyOf(topicArn).getString("Statement[1].Sid"));
|
||||
|
||||
json("RemovePermission", """
|
||||
{"TopicArn": "%s", "Label": "json-grant"}
|
||||
""".formatted(topicArn))
|
||||
.statusCode(200);
|
||||
assertEquals(1, policyOf(topicArn).getList("Statement").size());
|
||||
}
|
||||
|
||||
private String createTopic() {
|
||||
String topicArn = query("CreateTopic", Map.of("Name", "permission-" + UUID.randomUUID()))
|
||||
.statusCode(200)
|
||||
.extract().xmlPath().getString("CreateTopicResponse.CreateTopicResult.TopicArn");
|
||||
cleanup.register(() -> query("DeleteTopic", Map.of("TopicArn", topicArn)).statusCode(200));
|
||||
return topicArn;
|
||||
}
|
||||
|
||||
private static JsonPath policyOf(String topicArn) {
|
||||
return JsonPath.from(rawPolicyOf(topicArn));
|
||||
}
|
||||
|
||||
private static String rawPolicyOf(String topicArn) {
|
||||
return query("GetTopicAttributes", Map.of("TopicArn", topicArn))
|
||||
.statusCode(200)
|
||||
.extract().xmlPath()
|
||||
.getString("GetTopicAttributesResponse.GetTopicAttributesResult.Attributes.entry"
|
||||
+ ".find { it.key == 'Policy' }.value");
|
||||
}
|
||||
|
||||
private static ValidatableResponse query(String action, Map<String, String> params) {
|
||||
RequestSpecification spec = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", action)
|
||||
.formParam("Version", "2010-03-31");
|
||||
params.forEach(spec::formParam);
|
||||
return spec.when().post("/").then();
|
||||
}
|
||||
|
||||
private static ValidatableResponse json(String action, String body) {
|
||||
return given()
|
||||
.contentType("application/x-amz-json-1.0")
|
||||
.header("X-Amz-Target", "SNS_20100331." + action)
|
||||
.body(body)
|
||||
.when().post("/").then();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package io.github.hectorvent.floci.testing;
|
||||
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/** Waiting for a thread to reach a lock, for tests that force two requests to overlap. */
|
||||
public final class ThreadStates {
|
||||
|
||||
private ThreadStates() {}
|
||||
|
||||
/**
|
||||
* Returns once {@code thread} is blocked entering a monitor, or {@code task} has finished,
|
||||
* whichever comes first. Code that serializes correctly leaves the second request blocked
|
||||
* behind the first; code that does not lets it run to the end, which the caller's assertions
|
||||
* then catch. Polls every 5 ms and gives up after five seconds rather than hang a broken test.
|
||||
*/
|
||||
public static void awaitBlockedOrDone(AtomicReference<Thread> thread, Future<?> task)
|
||||
throws InterruptedException {
|
||||
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
|
||||
while (System.nanoTime() < deadline && !task.isDone()) {
|
||||
Thread current = thread.get();
|
||||
if (current != null && current.getState() == Thread.State.BLOCKED) {
|
||||
return;
|
||||
}
|
||||
Thread.sleep(5);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,19 +7,17 @@
|
||||
# and the Version the request declares, and AwsQueryServiceResolver backs
|
||||
# IamEnforcementFilter too, so an entry here is also authorized under the wrong namespace.
|
||||
#
|
||||
# Nearly every entry is a name missing from its own service's routing set, and adding it
|
||||
# there is the fix, as #5032 did for IAM. The third column only says where the request
|
||||
# goes meanwhile: sqs when no set holds the name, since that is the default, or the
|
||||
# service whose set does. A name two services define needs nothing more than being in
|
||||
# both sets, because the Version a request carries decides between them.
|
||||
# Every entry is a name missing from its own service's routing set, and adding it there
|
||||
# is the fix, as #5032 did for IAM. The third column only says where the request goes
|
||||
# meanwhile: sqs when no set holds the name, since that is the default, or the service
|
||||
# whose set does. A name two services define needs nothing more than being in both sets,
|
||||
# because the Version a request carries decides between them.
|
||||
#
|
||||
# The exception is a request without a Version, which only a hand-rolled client sends.
|
||||
# A name two services define then goes to the one tried first, which is a gap only when
|
||||
# that one does not handle it. SNS defines AddPermission and RemovePermission, but its
|
||||
# handler does not implement them, so SQS's reach SNS that way. Implementing them in SNS
|
||||
# is the fix.
|
||||
# A request without a Version, which only a hand-rolled client sends, is the one other
|
||||
# way to land here. A name two services define then goes to the one tried first, which
|
||||
# is a gap when that one does not handle the name.
|
||||
#
|
||||
# 173 entries: 171 unrouted, 2 claimed by another service.
|
||||
# 171 entries: 171 unrouted, 0 claimed by another service.
|
||||
#
|
||||
# Shrink this file, never grow it. Removing a line is the point; adding one needs a
|
||||
# reason in the PR that adds it. AwsQueryActionRoutingParityTest fails on an entry that
|
||||
@@ -211,7 +209,3 @@ redshift DescribeOrderableClusterOptions sqs
|
||||
redshift DisableLogging sqs
|
||||
redshift EnableLogging sqs
|
||||
redshift ModifyClusterIamRoles sqs
|
||||
|
||||
# sqs: 2 (0 unrouted, 2 claimed elsewhere)
|
||||
sqs AddPermission sns
|
||||
sqs RemovePermission sns
|
||||
|
Reference in new issue
Block a user