feat(sns): support SMS account preferences (#4597)

* feat(sns): support SMS account preferences

* fix(sns): include result element in SetSMSAttributes response

* fix(sns): validate SMS sender IDs and cover JSON updates

* refactor(sns): centralize SMS preference storage key
This commit is contained in:
XuJian authored and GitHub committed 2026-09-29 14:20:34 -04:00
1 parent 18ef1f58e9
commit 78c06239be
7 files changed
+280

No files matched your search

@@ -44,6 +44,8 @@ public class SnsJsonHandler {
case "ListTopics" -> handleListTopics(request, region);
case "GetTopicAttributes" -> handleGetTopicAttributes(request, region);
case "SetTopicAttributes" -> handleSetTopicAttributes(request, region);
case "SetSMSAttributes" -> handleSetSmsAttributes(request, region);
case "GetSMSAttributes" -> handleGetSmsAttributes(request, region);
case "Subscribe" -> handleSubscribe(request, region);
case "Unsubscribe" -> handleUnsubscribe(request, region);
case "ListSubscriptions" -> handleListSubscriptions(request, region);
@@ -125,6 +127,31 @@ public class SnsJsonHandler {
return Response.ok(objectMapper.createObjectNode()).build();
}
private Response handleSetSmsAttributes(JsonNode request, String region) {
Map<String, String> attributes = jsonNodeToMap(request.path("attributes"));
if (attributes.isEmpty()) {
throw new AwsException("InvalidParameter", "SMS attributes are required.", 400);
}
snsService.setSmsAttributes(attributes, region);
return Response.ok(objectMapper.createObjectNode()).build();
}
private Response handleGetSmsAttributes(JsonNode request, String region) {
List<String> names = new ArrayList<>();
JsonNode requested = request.path("attributes");
if (requested.isArray()) {
for (JsonNode name : requested) {
names.add(name.asText());
}
}
ObjectNode response = objectMapper.createObjectNode();
ObjectNode attributes = response.putObject("attributes");
for (Map.Entry<String, String> entry : snsService.getSmsAttributes(names, region).entrySet()) {
attributes.put(entry.getKey(), entry.getValue());
}
return Response.ok(response).build();
}
private Response handleSubscribe(JsonNode request, String region) {
String topicArn = request.path("TopicArn").asText(null);
String protocol = request.path("Protocol").asText(null);
@@ -43,6 +43,8 @@ public class SnsQueryHandler {
case "ListTopics" -> handleListTopics(params, region);
case "GetTopicAttributes" -> handleGetTopicAttributes(params, region);
case "SetTopicAttributes" -> handleSetTopicAttributes(params, region);
case "SetSMSAttributes" -> handleSetSmsAttributes(params, region);
case "GetSMSAttributes" -> handleGetSmsAttributes(params, region);
case "Subscribe" -> handleSubscribe(params, region);
case "Unsubscribe" -> handleUnsubscribe(params, region);
case "ListSubscriptions" -> handleListSubscriptions(params, region);
@@ -132,6 +134,33 @@ public class SnsQueryHandler {
}
}
private Response handleSetSmsAttributes(MultivaluedMap<String, String> params, String region) {
Map<String, String> attributes = extractSnsAttributes(params, "attributes");
if (attributes.isEmpty()) {
return xmlErrorResponse("InvalidParameter", "SMS attributes are required.", 400);
}
snsService.setSmsAttributes(attributes, region);
return Response.ok(AwsQueryResponse.envelopeEmptyResult("SetSMSAttributes", AwsNamespaces.SNS)).build();
}
private Response handleGetSmsAttributes(MultivaluedMap<String, String> params, String region) {
List<String> names = new ArrayList<>();
for (int i = 1; ; i++) {
String name = getParam(params, "attributes.member." + i);
if (name == null) {
break;
}
names.add(name);
}
Map<String, String> attributes = snsService.getSmsAttributes(names, region);
XmlBuilder xml = new XmlBuilder().start("attributes");
for (Map.Entry<String, String> entry : attributes.entrySet()) {
xml.start("entry").elem("key", entry.getKey()).elem("value", entry.getValue()).end("entry");
}
xml.end("attributes");
return Response.ok(AwsQueryResponse.envelope("GetSMSAttributes", AwsNamespaces.SNS, xml.build())).build();
}
private Response handleSubscribe(MultivaluedMap<String, String> params, String region) {
String topicArn = getParam(params, "TopicArn");
String protocol = getParam(params, "Protocol");
@@ -53,6 +53,7 @@ import java.util.Collections;
import java.util.Deque;
import java.util.HexFormat;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -68,6 +69,10 @@ public class SnsService implements Resettable, ResourceProvider {
private static final Logger LOG = Logger.getLogger(SnsService.class);
private static final Duration FIFO_DEDUP_WINDOW = Duration.ofMinutes(5);
private static final Set<String> SMS_ATTRIBUTE_NAMES = Set.of("MonthlySpendLimit",
"DeliveryStatusIAMRole", "DeliveryStatusSuccessSamplingRate", "DefaultSenderID",
"DefaultSMSType", "UsageReportS3Bucket");
private static final Pattern SMS_SENDER_ID = Pattern.compile("(?=.*[A-Za-z])[A-Za-z0-9]{1,11}");
/** 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";
@@ -111,6 +116,7 @@ public class SnsService implements Resettable, ResourceProvider {
private final StorageBackend<String, PlatformEndpoint> platformEndpointStore;
private final Deque<PushNotification> pushCapture = new ConcurrentLinkedDeque<>();
private final StorageBackend<String, SentSms> smsStore;
private final StorageBackend<String, Map<String, String>> smsAttributesStore;
private final RegionResolver regionResolver;
private final SqsService sqsService;
private final LambdaService lambdaService;
@@ -142,6 +148,9 @@ public class SnsService implements Resettable, ResourceProvider {
storageFactory.create("sns", "sns-sms.json",
new TypeReference<Map<String, SentSms>>() {
}),
storageFactory.create("sns", "sns-sms-attributes.json",
new TypeReference<Map<String, Map<String, String>>>() {
}),
regionResolver,
sqsService,
lambdaService,
@@ -194,6 +203,7 @@ public class SnsService implements Resettable, ResourceProvider {
this.platformAppStore = platformAppStore;
this.platformEndpointStore = platformEndpointStore;
this.smsStore = new InMemoryStorage<>();
this.smsAttributesStore = new InMemoryStorage<>();
this.regionResolver = regionResolver;
this.sqsService = sqsService;
this.lambdaService = lambdaService;
@@ -222,11 +232,26 @@ public class SnsService implements Resettable, ResourceProvider {
RegionResolver regionResolver, SqsService sqsService,
LambdaService lambdaService, FirehoseService firehoseService,
String baseUrl, ObjectMapper objectMapper) {
this(topicStore, subscriptionStore, platformAppStore, platformEndpointStore,
smsStore, new InMemoryStorage<>(), regionResolver, sqsService,
lambdaService, firehoseService, baseUrl, objectMapper);
}
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,
RegionResolver regionResolver, SqsService sqsService,
LambdaService lambdaService, FirehoseService firehoseService,
String baseUrl, ObjectMapper objectMapper) {
this.topicStore = topicStore;
this.subscriptionStore = subscriptionStore;
this.platformAppStore = platformAppStore;
this.platformEndpointStore = platformEndpointStore;
this.smsStore = smsStore;
this.smsAttributesStore = smsAttributesStore;
this.regionResolver = regionResolver;
this.sqsService = sqsService;
this.lambdaService = lambdaService;
@@ -241,6 +266,66 @@ public class SnsService implements Resettable, ResourceProvider {
fifoDeduplicationCache.clear();
}
public synchronized void setSmsAttributes(Map<String, String> attributes, String region) {
String key = smsAttributesKey(region);
Map<String, String> updated = new LinkedHashMap<>(smsAttributesStore.get(key).orElse(Map.of()));
for (Map.Entry<String, String> entry : attributes.entrySet()) {
String name = entry.getKey();
String value = entry.getValue();
if (!SMS_ATTRIBUTE_NAMES.contains(name) || value == null) {
throw new AwsException("InvalidParameter", "Invalid SMS attribute: " + name, 400);
}
if (value.isEmpty()) {
updated.remove(name);
continue;
}
if ("DefaultSMSType".equals(name)
&& !"Promotional".equals(value) && !"Transactional".equals(value)) {
throw new AwsException("InvalidParameter", "Invalid DefaultSMSType: " + value, 400);
}
if ("DefaultSenderID".equals(name) && !SMS_SENDER_ID.matcher(value).matches()) {
throw new AwsException("InvalidParameter", "Invalid DefaultSenderID: " + value, 400);
}
if ("MonthlySpendLimit".equals(name)) {
try {
if (new BigDecimal(value).signum() < 0) {
throw new NumberFormatException("negative amount");
}
} catch (NumberFormatException e) {
throw new AwsException("InvalidParameter", "Invalid MonthlySpendLimit: " + value, 400);
}
}
if ("DeliveryStatusSuccessSamplingRate".equals(name)) {
try {
int rate = Integer.parseInt(value);
if (rate < 0 || rate > 100) {
throw new NumberFormatException("out of range");
}
} catch (NumberFormatException e) {
throw new AwsException("InvalidParameter",
"Invalid DeliveryStatusSuccessSamplingRate: " + value, 400);
}
}
updated.put(name, value);
}
smsAttributesStore.put(key, updated);
}
public Map<String, String> getSmsAttributes(List<String> names, String region) {
String key = smsAttributesKey(region);
Map<String, String> stored = smsAttributesStore.get(key).orElse(Map.of());
if (names == null || names.isEmpty()) {
return new LinkedHashMap<>(stored);
}
Map<String, String> selected = new LinkedHashMap<>();
for (String name : names) {
if (stored.containsKey(name)) {
selected.put(name, stored.get(name));
}
}
return selected;
}
public Topic createTopic(String name, Map<String, String> attributes,
Map<String, String> tags, String region) {
if (name == null || name.isBlank()) {
@@ -2217,6 +2302,10 @@ public class SnsService implements Resettable, ResourceProvider {
}
}
private String smsAttributesKey(String region) {
return regionResolver.getAccountId() + "::" + region;
}
private static String topicKey(String region, String arn) {
return "topic::" + region + "::" + arn;
}