mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -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
@@ -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; }
|
||||
}
|
||||
Reference in new issue
Block a user