8000 add variable to configure SNS sender by bentsku · Pull Request #11930 · localstack/localstack · GitHub
[go: up one dir, main page]

Skip to content

add variable to configure SNS sender #11930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions localstack-core/localstack/config.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,8 @@ def populate_edge_configuration(
# Whether to really publish to GCM while using SNS Platform Application (needs credentials)
LEGACY_SNS_GCM_PUBLISHING = is_env_true("LEGACY_SNS_GCM_PUBLISHING")

SNS_SES_SENDER_ADDRESS = os.environ.get("SNS_SES_SENDER_ADDRESS", "").strip()

# Whether the Next Gen APIGW invocation logic is enabled (on by default)
APIGW_NEXT_GEN_PROVIDER = os.environ.get("PROVIDER_OVERRIDE_APIGATEWAY", "") in ("next_gen", "")

Expand Down Expand Up @@ -1334,6 +1336,7 @@ def use_custom_dns():
"SNAPSHOT_LOAD_STRATEGY",
"SNAPSHOT_SAVE_STRATEGY",
"SNAPSHOT_FLUSH_INTERVAL",
"SNS_SES_SENDER_ADDRESS",
"SQS_DELAY_PURGE_RETRY",
"SQS_DELAY_RECENTLY_DELETED",
"SQS_ENABLE_MESSAGE_RETENTION_PERIOD",
Expand Down
7 changes: 5 additions & 2 deletions localstack-core/localstack/services/sns/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,13 +569,16 @@ def _publish(self, context: SnsPublishContext, subscriber: SnsSubscription):
region = extract_region_from_arn(subscriber["Endpoint"])
ses_client = connect_to(aws_access_key_id=account_id, region_name=region).ses
if endpoint := subscriber.get("Endpoint"):
# TODO: legacy value, replace by a more sane value in the future
# no-reply@sns-localstack.cloud or similar
sender = config.SNS_SES_SENDER_ADDRESS or "admin@localstack.com"
ses_client.verify_email_address(EmailAddress=endpoint)
ses_client.verify_email_address(EmailAddress="admin@localstack.com")
ses_client.verify_email_address(EmailAddress=sender)
message_body = self.prepare_message(
context.message, subscriber, topic_attributes=context.topic_attributes
)
ses_client.send_email(
Source="admin@localstack.com",
Source=sender,
Message={
"Body": {"Text": {"Data": message_body}},
"Subject": {"Data": "SNS-Subscriber-Endpoint"},
Expand Down
43 changes: 43 additions & 0 deletions tests/aws/services/sns/test_sns.py
Original file line number Diff line number Diff line change
Expand Up @@ -3003,6 +3003,49 @@ def check_subscription():

retry(check_subscription, retries=PUBLICATION_RETRIES, sleep=PUBLICATION_TIMEOUT)

@markers.aws.only_localstack
def test_email_sender(
self,
sns_create_topic,
sns_subscription,
aws_client,
monkeypatch,
):
# make sure to reset all received emails in SES
requests.delete("http://localhost:4566/_aws/ses")

topic_arn = sns_create_topic()["TopicArn"]
sns_subscription(
TopicArn=topic_arn,
Protocol="email",
Endpoint="localstack@yopmail.com",
)

aws_client.sns.publish(
Message="Test message",
TopicArn=topic_arn,
)

def _get_messages(amount: int) -> list[dict]:
response = requests.get("http://localhost:4566/_aws/ses").json()
assert len(response["messages"]) == amount
return response["messages"]

messages = retry(lambda: _get_messages(1), retries=PUBLICATION_RETRIES, sleep=1)
# legacy default value, should be replaced at some point
assert messages[0]["Source"] == "admin@localstack.com"
requests.delete("http://localhost:4566/_aws/ses")

sender_address = "no-reply@sns.localstack.cloud"
monkeypatch.setattr(config, "SNS_SES_SENDER_ADDRESS", sender_address)

aws_client.sns.publish(
Message="Test message",
TopicArn=topic_arn,
)
messages = retry(lambda: _get_messages(1), retries=PUBLICATION_RETRIES, sleep=1)
assert messages[0]["Source"] == sender_address


class TestSNSPlatformEndpoint:
@markers.aws.only_localstack
Expand Down
Loading
0