Skip to content
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

feat: Add attach_sns_policy #89

Merged

Conversation

kaykhan
Copy link
Contributor

@kaykhan kaykhan commented Jun 15, 2023

Description

This pull request adds the attach_sns_policy flag to attach a permission policy to the sns target to allow the sending of event bridge notifications to sns.

Motivation and Context

Without assigning resource permissions to the sns target, event bridge rule fails to send the notification to sns.
https://repost.aws/knowledge-center/sns-not-getting-eventbridge-notification

Fixes #88

Breaking Changes

How Has This Been Tested?

  • I have updated at least one of the examples/* to demonstrate and validate my change(s)
  • I have tested and validated these changes using one or more of the provided examples/* projects
  • I have executed pre-commit run -a on my pull request

@kaykhan kaykhan changed the title feat: add attach_sns_policy feat: Add attach_sns_policy Jun 15, 2023
Copy link
Member

@antonbabenko antonbabenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good. There is just a minor comment.

statement {
sid = "SNSAccess"
effect = "Allow"
actions = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split this policy statement into 2 - one for SNS and one for KMS actions. They should have different resources.

Copy link
Contributor Author

@kaykhan kaykhan Jun 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean they should have different resources?. Should the second statement which allows KMS actions not target the SNS resource?. I added the KMS actions as an afterthought to be able to publish encrypted messages - as descirbed here https://repost.aws/knowledge-center/sns-not-getting-eventbridge-notification

But I can see the existing SQS example uses a single policy statement. https://github.com/terraform-aws-modules/terraform-aws-eventbridge/blob/master/iam.tf#L139-L153

What should the resources be?

data "aws_iam_policy_document" "sns" {
  count = local.create_role && var.attach_sns_policy ? 1 : 0

  statement {
    sid    = "SNSAccess"
    effect = "Allow"
    actions = [
      "sns:Publish",
    ]
    resources = var.sns_target_arns
  }

  statement {
    sid    = "SNSKMSAccess"
    effect = "Allow"
    actions = [
      "kms:Decrypt",
      "kms:GenerateDataKey"
    ]
    resources = ???
  }

}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resources = ??? => resources = ["*"] as it says in "Example IAM policy statement that allows EventBridge to publish messages to an encrypted Amazon SNS topic" section on https://repost.aws/knowledge-center/sns-not-getting-eventbridge-notification

Copy link
Contributor Author

@kaykhan kaykhan Jun 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh i see

@kaykhan kaykhan requested a review from antonbabenko June 15, 2023 15:51
@antonbabenko antonbabenko merged commit 6e09aa1 into terraform-aws-modules:master Jun 15, 2023
antonbabenko pushed a commit that referenced this pull request Jun 15, 2023
## [2.2.0](v2.1.0...v2.2.0) (2023-06-15)

### Features

* Add attach_sns_policy ([#89](#89)) ([6e09aa1](6e09aa1))
@antonbabenko
Copy link
Member

This PR is included in version 2.2.0 🎉

@denkojonasz
Copy link

Hello, based on my observations attach_sns_policy probably doesn't make sense and doesn't solve EventBridge -> SNS permission issue.

As you can see in the docs For Lambda, Amazon SNS, Amazon SQS, and Amazon CloudWatch Logs resources, EventBridge uses resource-based policies, which means that attaching identity-based permission to the EventBridge via attach_sns_policy doesn't fix the problem.

The same is mentioned in the https://repost.aws/knowledge-center/sns-not-getting-eventbridge-notification, what was the motivation for this PR - Your Amazon SNS topic's resource-based policy must allow EventBridge to publish messages to the topic

The real resolution on the permission problem, pointed in the motivation is to attach appropriate topic_policy_statements resource based policy, that allows publish SNS messages from EventBridge on the SNS resource level.

Because of the above I would propose to revert this commit, as it doesn't resolve anything and only makes a confusion.

@kaykhan
Copy link
Contributor Author

kaykhan commented Aug 7, 2023

Hello, based on my observations attach_sns_policy probably doesn't make sense and doesn't solve EventBridge -> SNS permission issue.

As you can see in the docs For Lambda, Amazon SNS, Amazon SQS, and Amazon CloudWatch Logs resources, EventBridge uses resource-based policies, which means that attaching identity-based permission to the EventBridge via attach_sns_policy doesn't fix the problem.

The same is mentioned in the https://repost.aws/knowledge-center/sns-not-getting-eventbridge-notification, what was the motivation for this PR - Your Amazon SNS topic's resource-based policy must allow EventBridge to publish messages to the topic

The real resolution on the permission problem, pointed in the motivation is to attach appropriate topic_policy_statements resource based policy, that allows publish SNS messages from EventBridge on the SNS resource level.

Because of the above I would propose to revert this commit, as it doesn't resolve anything and only makes a confusion.

Hi, i agree there might be confusion but i just want to push back on the idea that this commit did not resolve anything

if i check the SNS Topic, i can see that the access policy was updated to include the right permissions.

image

I'm not sure if this commit is unique it seems to be following the same architecture of this module.


Topic policy statements are new to me, i can see resource definition here https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic_policy.html

What is the major difference between specifying an aws_sns_topic_policy compared to this https://github.com/terraform-aws-modules/terraform-aws-eventbridge/blob/master/iam.tf#L213 both seem to be use the underlining aws_iam_policy_document ?

@denkojonasz
Copy link

if i check the SNS Topic, i can see that the access policy was updated to include the right permissions.

I don't think that SNS Access policy from your Screenshot was updated because of adding identity-based policy to the AWS EventBridge (this one created via attach_sns_policy flag).

AWS automatically sets proper policy once AWS EventBridge rule is created/modified via Console. Any chance that your AWS EventBridge rule were updated via Console? Did you modify anything via console:
Screenshot 2023-08-07 at 11 15 24

To test it, you can create completely new EventBridge rule and SNS topic with attach_sns_policy flag enabled, via Terraform, and confirm if the Access policy from your screenshot was attached or not.


What is the major difference between specifying an aws_sns_topic_policy compared to this https://github.com/terraform-aws-modules/terraform-aws-eventbridge/blob/master/iam.tf#L213 both seem to be use the underlining aws_iam_policy_document ?

Topic policy sets permissions on a resource level (it's resource-based policy on SNS directly). Specifying policy in this module, sets permissions on EventBridge service level.

Even if they are the same policies (both grant access to publish SNS messages on specific SNS queue), because of the some reasons AWS require to set them on resource level. One more time docs - For Lambda, Amazon SNS, Amazon SQS, and Amazon CloudWatch Logs resources, EventBridge uses resource-based policies.

@denkojonasz
Copy link

@kaykhan have you had a chance to verify my last comment?

@kushan-nv
Copy link

I can also confirm that setting attach_sns_policy doesn't update the SNS access policy.

KamranBiglari pushed a commit to KamranBiglari/terraform-aws-eventbridge that referenced this pull request Jan 10, 2024
## 1.0.0 (2024-01-10)

### ⚠ BREAKING CHANGES

* Upgraded AWS provider to v5 (required for Pipes) (terraform-aws-modules#94)
* Bump Terraform version to 1.0 and updated `ecs_target` arguments (terraform-aws-modules#85)

### Features

* Add attach_sns_policy ([terraform-aws-modules#89](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/89)) ([6e09aa1](6e09aa1))
* Add example for ECS + scheduled events ([terraform-aws-modules#14](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/14)) ([32ea196](32ea196))
* Add schema discoverer ([terraform-aws-modules#64](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/64)) ([0099c43](0099c43))
* Add support for EventBridge Pipes ([terraform-aws-modules#92](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/92)) ([ff131eb](ff131eb))
* Add support for setting the condition field in Event Bus permissions ([terraform-aws-modules#84](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/84)) ([49f1dff](49f1dff))
* Added Name tag for IAM policies and roles ([terraform-aws-modules#62](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/62)) ([8ca8835](8ca8835))
* Added support for API destinations ([terraform-aws-modules#27](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/27)) ([b4f1ef8](b4f1ef8))
* Added support for custom role_arn in targets ([terraform-aws-modules#42](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/42)) ([45311f7](45311f7))
* Bump Terraform version to 1.0 and updated `ecs_target` arguments ([terraform-aws-modules#85](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/85)) ([04a3249](04a3249))
* first commit ([terraform-aws-modules#1](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/1)) ([48eeb94](48eeb94))
* Simplified outputs (no this_) ([terraform-aws-modules#6](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/6)) ([eb8051c](eb8051c))
* Some refactoring and added ability to handle default bus ([terraform-aws-modules#5](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/5)) ([32f75c1](32f75c1))
* Support for Eventbridge Scheduler Schedules ([terraform-aws-modules#83](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/83)) ([e3c4ffe](e3c4ffe))
* Support for existing event buses ([terraform-aws-modules#22](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/22)) ([6a499b6](6a499b6))
* support http_target argument ([terraform-aws-modules#11](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/11)) ([ee5d963](ee5d963))
* Upgraded AWS provider to v5 (required for Pipes) ([terraform-aws-modules#94](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/94)) ([ba4d055](ba4d055))
* Upgraded AWS provider version to 4.7 ([terraform-aws-modules#66](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/66)) ([7690287](7690287))

### Bug Fixes

* `create_rules = false` causes error ([terraform-aws-modules#19](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/19)) ([6d8b8d7](6d8b8d7))
* Add explicit to_map for empty object for aws_cloudwatch_event_target ([terraform-aws-modules#24](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/24)) ([9b49848](9b49848))
* Amend batch_target to be correct value ([terraform-aws-modules#35](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/35)) ([babb4d6](babb4d6))
* Amend ecs_target network_configuration to work when no ecs_target supplied ([terraform-aws-modules#25](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/25)) ([852ea30](852ea30))
* Enable adding event_source_name to an Event Bus to enable receiving events from an SaaS partner ([terraform-aws-modules#82](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/82)) ([f92a78c](f92a78c))
* Enable run_command_targets support for target ([terraform-aws-modules#54](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/54)) ([e153898](e153898))
* Fix tomap call for terraform 0.15 ([terraform-aws-modules#10](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/10)) ([d303324](d303324))
* Fixed function name from to_map to tomap ([terraform-aws-modules#26](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/26)) ([e601dec](e601dec))
* Fixed inappropriate values for subnets and security_groups in example ([terraform-aws-modules#63](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/63)) ([fd7a25b](fd7a25b))
* Fixed incorrect tomap() ([terraform-aws-modules#39](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/39)) ([05bceba](05bceba))
* Fixed misleading descriptions of IAM role (not Lambda) ([terraform-aws-modules#76](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/76)) ([aa92195](aa92195))
* Fixed outputs when create=false ([terraform-aws-modules#33](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/33)) ([3dcc882](3dcc882))
* Make it optional to append postfix to the name, connection, or API destination  ([terraform-aws-modules#58](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/58)) ([980b910](980b910))
* Problems found when importing resources previously already created ([terraform-aws-modules#61](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/61)) ([015122e](015122e))
* property lookup in ecs_target block ([terraform-aws-modules#8](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/8)) ([af29da3](af29da3))
* remove create_bus as a blocker for role_arn ([terraform-aws-modules#13](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/13)) ([5453970](5453970))
* update CI/CD process to enable auto-release workflow ([terraform-aws-modules#31](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/31)) ([ad31225](ad31225))
* update sqs access policy ([terraform-aws-modules#16](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/16)) ([2de06bd](2de06bd))
* Use a version for  to avoid GitHub API rate limiting on CI workflows ([terraform-aws-modules#75](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/75)) ([e9a7813](e9a7813))
* Wrong value of api destination output ([terraform-aws-modules#79](https://github.com/KamranBiglari/terraform-aws-eventbridge/issues/79)) ([03ef4ff](03ef4ff))
Copy link

github-actions bot commented Mar 9, 2024

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Attach SNS policy with SNS target
4 participants