Files
floci/docs/services/sns.md
T
Abhigya f45d64a00e feat(sns): deliver topic broadcasts to application-protocol platform endpoints (#1856)
* feat(sns): deliver topic broadcasts to application-protocol platform endpoints

Add an application-protocol case to deliverMessage so publishing to a
topic fans out to subscribed mobile push platform endpoints, matching
real SNS behavior. Thread messageStructure through the delivery boundary
so platform-specific payload keys (GCM/FCM/APNS) resolve per endpoint,
and factor the push-capture tail of publishToEndpoint into a shared
capturePushForEndpoint helper. Disabled endpoints are skipped in the
fan-out rather than failing the publish. Broadcast pushes surface in the
same retrospection API keyed by EndpointArn.

* fix(sns): validate MessageStructure synchronously before topic fan-out

Topic Publish with MessageStructure=json now validates that the message is
a JSON object with a top-level default entry up front, mirroring the real
SNS API. Previously a malformed envelope was only rejected lazily inside
per-subscriber delivery, where deliverMessage's catch swallowed the
InvalidParameter and logged a warning while publish still returned a
success messageId, so application subscribers silently received no push.

* fix(sns): validate MessageStructure per entry in PublishBatch

publish() validates a MessageStructure="json" envelope synchronously before
fan-out, but publishBatch() only threaded MessageStructure into deliverMessage.
A batch entry with a malformed or default-less envelope was therefore added to
Successful while deliverMessage's catch swallowed the resulting InvalidParameter,
dropping the push for every application subscriber.

Run the same validation per entry, reporting a bad envelope as that entry's
BatchResultErrorEntry (InvalidParameter, senderFault=true) instead of throwing,
so the rest of the batch still publishes as real PublishBatch does.

* fix(sns): resolve MessageStructure=json payload per protocol on fan-out

Under MessageStructure="json", only the application protocol consumed the
envelope: sqs, lambda, http/https, email, email-json and sms each delivered
the raw JSON structure. Real SNS resolves per-protocol for every subscriber,
picking the value under the protocol key and falling back to default.

deliverMessage now resolves the payload for each subscription through the
(renamed) resolveProtocolPayload, which already keyed off an arbitrary
string; application keeps resolving against the push platform inside
capturePushForEndpoint. validateTopicMessageStructure guarantees default
exists before fan-out, so the fallback cannot come up empty.

* fix(sns): ignore non-string MessageStructure=json keys, matching AWS

Under MessageStructure=json, real SNS ignores a protocol key whose value
is not a string ("Non-string values will cause the key to be ignored"),
falling back to default rather than stringifying it. resolveProtocolPayload
was calling protocolValue.toString(), so {"default":"hi","sqs":42} delivered
the literal "42" to an SQS subscriber instead of "hi".

Only textual values are now accepted for both the protocol key and the
default fallback; non-textual values fall through. validateTopicMessageStructure
is tightened to require a textual default so a non-string default is rejected
synchronously rather than throwing (and being swallowed) during fan-out.

Adds regression tests for a non-string protocol key falling back to default
and for rejection of a non-string default.

* chore: remove unnecessary file
2026-08-20 23:10:38 -04:00

204 lines
8.7 KiB
Markdown

# SNS
**Protocol:** Query (XML) and JSON 1.0 (both supported)
**Endpoint:** `POST http://localhost:4566/`
## Supported Actions
| Action | Description |
|---|---|
| `CreateTopic` | Create a topic |
| `DeleteTopic` | Delete a topic |
| `ListTopics` | List all topics |
| `GetTopicAttributes` | Get topic configuration |
| `SetTopicAttributes` | Update topic configuration |
| `Subscribe` | Subscribe an endpoint (SQS, HTTP, Lambda, email) |
| `Unsubscribe` | Remove a subscription |
| `ListSubscriptions` | List all subscriptions |
| `ListSubscriptionsByTopic` | List subscriptions for a specific topic |
| `GetSubscriptionAttributes` | Get subscription settings |
| `SetSubscriptionAttributes` | Update subscription settings |
| `ConfirmSubscription` | Confirm a pending subscription |
| `Publish` | Publish a message to a topic |
| `PublishBatch` | Publish up to 10 messages in one call |
| `TagResource` | Tag a topic |
| `UntagResource` | Remove tags from a topic |
| `ListTagsForResource` | List tags on a topic |
| `CreatePlatformApplication` | Create a mobile push platform app (iOS or Android) |
| `DeletePlatformApplication` | Delete a platform app and its endpoints |
| `GetPlatformApplicationAttributes` | Read platform app attributes |
| `SetPlatformApplicationAttributes` | Update platform app attributes (e.g. `Enabled`) |
| `ListPlatformApplications` | List platform applications in the region |
| `CreatePlatformEndpoint` | Register a device token under a platform app |
| `DeleteEndpoint` | Delete a platform endpoint |
| `GetEndpointAttributes` | Read endpoint attributes |
| `SetEndpointAttributes` | Update endpoint attributes (e.g. `Enabled=false` to simulate token expiry) |
| `ListEndpointsByPlatformApplication` | List endpoints under a platform app |
## Configuration
| Variable | Default | Description |
|---|---|---|
| `FLOCI_SERVICES_SNS_ENABLED` | `true` | Enable or disable the service |
| `FLOCI_STORAGE_SERVICES_SNS_MODE` | *(global default)* | Storage mode override for SNS (`memory`, `persistent`, `hybrid`, `wal`) |
| `FLOCI_STORAGE_SERVICES_SNS_FLUSH_INTERVAL_MS` | `5000` | Flush interval for `hybrid`/`wal` storage modes (milliseconds) |
## Examples
```bash
export AWS_ENDPOINT_URL=http://localhost:4566
# Create a topic
TOPIC_ARN=$(aws sns create-topic --name notifications \
--query TopicArn --output text \
--endpoint-url $AWS_ENDPOINT_URL)
# Subscribe an SQS queue
QUEUE_ARN=$(aws sqs get-queue-attributes \
--queue-url $AWS_ENDPOINT_URL/000000000000/orders \
--attribute-names QueueArn \
--query Attributes.QueueArn --output text \
--endpoint-url $AWS_ENDPOINT_URL)
aws sns subscribe \
--topic-arn $TOPIC_ARN \
--protocol sqs \
--notification-endpoint $QUEUE_ARN \
--endpoint-url $AWS_ENDPOINT_URL
# Publish a message
aws sns publish \
--topic-arn $TOPIC_ARN \
--message '{"event":"user.registered"}' \
--endpoint-url $AWS_ENDPOINT_URL
# Fan-out: publish and verify the SQS queue received the message
aws sqs receive-message \
--queue-url $AWS_ENDPOINT_URL/000000000000/orders \
--endpoint-url $AWS_ENDPOINT_URL
```
## SNS → SQS Fan-Out
Floci supports real SNS → SQS fan-out. When you publish to a topic, all SQS-subscribed queues receive the message immediately.
Supported subscription protocols:
- `sqs` — delivers to a Floci SQS queue
- `lambda` — invokes a Floci Lambda function
- `http` / `https` — posts to an HTTP endpoint
- `application` — fans out to a mobile push platform endpoint (see [Mobile push](#mobile-push-mock))
## Mobile push (mock)
Floci mocks SNS mobile push for iOS and Android. No real APNS or FCM connection is
made — every push is captured in memory so tests can assert what would have been sent.
**Supported platforms:** `APNS`, `APNS_SANDBOX`, `GCM`, `FCM`. Any other platform
value returns `InvalidParameter`.
### End-to-end flow
```bash
APP_ARN=$(aws sns create-platform-application \
--name ios-app --platform APNS \
--attributes PlatformCredential=fake-cert \
--endpoint-url http://localhost:4566 --query PlatformApplicationArn --output text)
ENDPOINT_ARN=$(aws sns create-platform-endpoint \
--platform-application-arn $APP_ARN \
--token ios-device-token-abc \
--endpoint-url http://localhost:4566 --query EndpointArn --output text)
# Plain string payload
aws sns publish --target-arn $ENDPOINT_ARN --message '{"aps":{"alert":"hi"}}' \
--endpoint-url http://localhost:4566
# Platform-specific payloads with MessageStructure=json
aws sns publish --target-arn $ENDPOINT_ARN --message-structure json \
--message '{"default":"fallback","APNS":"{\"aps\":{\"alert\":\"ios\"}}","GCM":"{\"notification\":{\"body\":\"android\"}}"}' \
--endpoint-url http://localhost:4566
```
When `MessageStructure="json"`, Floci picks the key matching the endpoint's platform
(`APNS`, `APNS_SANDBOX`, `GCM`, or `FCM`), falling back to `default`. The envelope
must be a JSON object and must include `default` — otherwise `InvalidParameter`.
### Broadcast to devices via a topic
Subscribe platform endpoints to a topic with `Protocol="application"`, then publish to
the topic to fan out to every subscribed device — each endpoint is captured exactly as
if you had published to it directly (same platform-payload resolution, same `Enabled`
gating). A disabled endpoint in the fan-out is skipped; the rest still receive the push.
```bash
aws sns subscribe --topic-arn $TOPIC_ARN \
--protocol application --notification-endpoint $ENDPOINT_ARN \
--endpoint-url http://localhost:4566
aws sns publish --topic-arn $TOPIC_ARN --message-structure json \
--message '{"default":"market open","GCM":"{\"notification\":{\"body\":\"market alert\"}}"}' \
--endpoint-url http://localhost:4566
```
Broadcast pushes surface in the same retrospection API, keyed by `EndpointArn`.
### Per-protocol payloads on topic publish
`MessageStructure="json"` resolves per subscriber, not just for mobile endpoints. Each
subscription receives the value under its own protocol key — `sqs`, `lambda`, `http`,
`https`, `email`, `email-json`, `sms`, or the push platform (`APNS`, `GCM`, …) for
`application` — falling back to `default` when that key is absent.
```bash
aws sns publish --topic-arn $TOPIC_ARN --message-structure json \
--message '{"default":"hello","sqs":"hi sqs","GCM":"{\"notification\":{\"body\":\"hi device\"}}"}' \
--endpoint-url http://localhost:4566
```
The SQS subscriber receives `hi sqs`, the platform endpoint receives the `GCM` payload,
and every other subscriber receives `hello`. As with any topic publish, the envelope must
be a JSON object carrying `default`, validated before fan-out begins.
### Inspecting captured pushes
```bash
# All captured pushes (newest first), or filtered by endpoint
curl http://localhost:4566/_aws/sns/push-notifications
curl "http://localhost:4566/_aws/sns/push-notifications?EndpointArn=$ENDPOINT_ARN"
# Reset between tests
curl -X DELETE http://localhost:4566/_aws/sns/push-notifications
```
### Simulating expired tokens
Two ways to make `Publish` fail with `EndpointDisabledException`:
1. **Explicit** — call `SetEndpointAttributes` with `Enabled=false`. Matches the
real AWS flow after an async APNS/FCM failure.
2. **Sentinel** — create an endpoint whose token contains `EXPIRED`
(case-insensitive). Floci marks it `Enabled=false` on creation, so the first
publish fails. Lets you exercise the unhappy path with a single API call.
### Error codes
| Action | Condition | Error code | HTTP |
|---|---|---|---|
| `CreatePlatformApplication` | Missing `Name` | `InvalidParameter` | 400 |
| `CreatePlatformApplication` | Unsupported `Platform` (e.g. `WNS`, `ADM`) | `InvalidParameter` | 400 |
| `CreatePlatformEndpoint` | Missing `Token` | `InvalidParameter` | 400 |
| `CreatePlatformEndpoint` | Unknown `PlatformApplicationArn` | `NotFound` | 404 |
| `CreatePlatformEndpoint` | Same `Token`, different `CustomUserData` or attrs | `InvalidParameter` | 400 |
| `CreatePlatformEndpoint` | Platform app disabled | `PlatformApplicationDisabledException` | 400 |
| `Publish` | Unknown endpoint ARN | `NotFound` | 404 |
| `Publish` | `TargetArn` is a platform application ARN | `InvalidParameter` | 400 |
| `Publish` | Endpoint `Enabled=false` | `EndpointDisabledException` | 400 |
| `Publish` | Platform application `Enabled=false` | `PlatformApplicationDisabledException` | 400 |
| `Publish` | `MessageStructure=json` missing `default` key | `InvalidParameter` | 400 |
| `Publish` | `MessageStructure=json` message is not valid JSON | `InvalidParameter` | 400 |
| `GetPlatformApplicationAttributes` | Unknown ARN | `NotFound` | 404 |
| `GetEndpointAttributes` | Unknown ARN | `NotFound` | 404 |
| `SetEndpointAttributes` | Unknown ARN | `NotFound` | 404 |
`DeletePlatformApplication` and `DeleteEndpoint` are idempotent — they succeed
silently if the resource does not exist, matching real SNS behavior.