Skip to content

Commit

Permalink
skip: Merge pull request #32 from epam/feature/ecc-aws-560-unused_sns…
Browse files Browse the repository at this point in the history
…_topic

new: added policy ecc-aws-560-unused_sns_topic
  • Loading branch information
Astr1k authored and anna-shcherbak committed Sep 20, 2023
2 parents f88945c + 12d19e3 commit e6f958a
Show file tree
Hide file tree
Showing 19 changed files with 279 additions and 0 deletions.
26 changes: 26 additions & 0 deletions policies/ecc-aws-560-unused_sns_topic.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright (c) 2023 EPAM Systems, Inc.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.


policies:
- name: ecc-aws-560-unused_sns_topic
comment: '010002142000'
description: |
Amazon SNS topics unused
resource: aws.sns
filters:
- or:
- type: value
key: SubscriptionsConfirmed
value: "0"
- type: metrics
name: NumberOfMessagesPublished
statistics: Sum
missing-value: 0
days: 30
value: 0
op: eq
period: 2592000
20 changes: 20 additions & 0 deletions terraform/ecc-aws-560-unused_sns_topic/green/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4"
}
}
}

provider "aws" {
profile = var.profile
region = var.default-region

default_tags {
tags = {
CustodianRule = "ecc-aws-560-unused_sns_topic"
ComplianceStatus = "Green"
}
}
}
28 changes: 28 additions & 0 deletions terraform/ecc-aws-560-unused_sns_topic/green/sns.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
resource "aws_sns_topic" "this" {
name = "560-sns-green"
}

resource "aws_sqs_queue" "this" {
name = "560-sqs-green"
}

resource "aws_sns_topic_subscription" "this" {
topic_arn = aws_sns_topic.this.arn
protocol = "sqs"
endpoint = aws_sqs_queue.this.arn
}

resource "null_resource" "this" {
provisioner "local-exec" {
command = join(" ", [
"aws sns publish ",
"--topic-arn ${aws_sns_topic.this.arn}",
"--message 'Hello World!'",
"--profile ${var.profile}",
"--region ${var.default-region}"
]
)
}

depends_on = [aws_sns_topic_subscription.this]
}
2 changes: 2 additions & 0 deletions terraform/ecc-aws-560-unused_sns_topic/green/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
profile = "c7n"
default-region = "us-east-1"
9 changes: 9 additions & 0 deletions terraform/ecc-aws-560-unused_sns_topic/green/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "default-region" {
type = string
description = "Default region for resources will be created"
}

variable "profile" {
type = string
description = "Profile name configured before running apply"
}
15 changes: 15 additions & 0 deletions terraform/ecc-aws-560-unused_sns_topic/iam/560-policy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"sns:ListTopics",
"sns:GetTopicAttributes",
"tag:GetResources",
"cloudwatch:GetMetricStatistics"
],
"Resource": "*"
}
]
}
20 changes: 20 additions & 0 deletions terraform/ecc-aws-560-unused_sns_topic/red/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4"
}
}
}

provider "aws" {
profile = var.profile
region = var.default-region

default_tags {
tags = {
CustodianRule = "ecc-aws-560-unused_sns_topic"
ComplianceStatus = "Red"
}
}
}
3 changes: 3 additions & 0 deletions terraform/ecc-aws-560-unused_sns_topic/red/sns.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "aws_sns_topic" "this" {
name = "560-sns-red"
}
2 changes: 2 additions & 0 deletions terraform/ecc-aws-560-unused_sns_topic/red/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
profile = "c7n"
default-region = "us-east-1"
9 changes: 9 additions & 0 deletions terraform/ecc-aws-560-unused_sns_topic/red/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "default-region" {
type = string
description = "Default region for resources will be created"
}

variable "profile" {
type = string
description = "Profile name configured before running apply"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"status_code": 200,
"data": {
"Label": "NumberOfMessagesPublished",
"Datapoints": [
{
"Timestamp": {
"__class__": "datetime",
"year": 2023,
"month": 8,
"day": 20,
"hour": 18,
"minute": 55,
"second": 0,
"microsecond": 0
},
"Sum": 1.0,
"Unit": "Count"
}
],
"ResponseMetadata": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"status_code": 200,
"data": {
"Attributes": {
"Policy": "{\"Version\":\"2008-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\":\"arn:aws:sns:us-east-1:111111111111:560-sns-green\",\"Condition\":{\"StringEquals\":{\"AWS:SourceOwner\":\"111111111111\"}}}]}",
"LambdaSuccessFeedbackSampleRate": "0",
"Owner": "111111111111",
"SubscriptionsPending": "0",
"TopicArn": "arn:aws:sns:us-east-1:111111111111:560-sns-green",
"EffectiveDeliveryPolicy": "{\"http\":{\"defaultHealthyRetryPolicy\":{\"minDelayTarget\":20,\"maxDelayTarget\":20,\"numRetries\":3,\"numMaxDelayRetries\":0,\"numNoDelayRetries\":0,\"numMinDelayRetries\":0,\"backoffFunction\":\"linear\"},\"disableSubscriptionOverrides\":false,\"defaultRequestPolicy\":{\"headerContentType\":\"text/plain; charset=UTF-8\"}}}",
"FirehoseSuccessFeedbackSampleRate": "0",
"SubscriptionsConfirmed": "1",
"SQSSuccessFeedbackSampleRate": "0",
"HTTPSuccessFeedbackSampleRate": "0",
"ApplicationSuccessFeedbackSampleRate": "0",
"DisplayName": "",
"SubscriptionsDeleted": "0"
},
"ResponseMetadata": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"status_code": 200,
"data": {
"Topics": [
{
"TopicArn": "arn:aws:sns:us-east-1:111111111111:560-sns-green"
}
],
"ResponseMetadata": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"status_code": 200,
"data": {
"PaginationToken": "",
"ResourceTagMappingList": [
{
"ResourceARN": "arn:aws:sns:us-east-1:111111111111:560-sns-green",
"Tags": [
{
"Key": "CustodianRule",
"Value": "ecc-aws-560-unused_sns_topic"
},
{
"Key": "ComplianceStatus",
"Value": "Green"
}
]
}
],
"ResponseMetadata": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"status_code": 200,
"data": {
"Label": "NumberOfMessagesPublished",
"Datapoints": [],
"ResponseMetadata": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"status_code": 200,
"data": {
"Attributes": {
"Policy": "{\"Version\":\"2008-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\":\"arn:aws:sns:us-east-1:111111111111:560-sns-red\",\"Condition\":{\"StringEquals\":{\"AWS:SourceOwner\":\"111111111111\"}}}]}",
"LambdaSuccessFeedbackSampleRate": "0",
"Owner": "111111111111",
"SubscriptionsPending": "0",
"TopicArn": "arn:aws:sns:us-east-1:111111111111:560-sns-red",
"EffectiveDeliveryPolicy": "{\"http\":{\"defaultHealthyRetryPolicy\":{\"minDelayTarget\":20,\"maxDelayTarget\":20,\"numRetries\":3,\"numMaxDelayRetries\":0,\"numNoDelayRetries\":0,\"numMinDelayRetries\":0,\"backoffFunction\":\"linear\"},\"disableSubscriptionOverrides\":false,\"defaultRequestPolicy\":{\"headerContentType\":\"text/plain; charset=UTF-8\"}}}",
"FirehoseSuccessFeedbackSampleRate": "0",
"SubscriptionsConfirmed": "0",
"SQSSuccessFeedbackSampleRate": "0",
"HTTPSuccessFeedbackSampleRate": "0",
"ApplicationSuccessFeedbackSampleRate": "0",
"DisplayName": "",
"SubscriptionsDeleted": "0"
},
"ResponseMetadata": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"status_code": 200,
"data": {
"Topics": [
{
"TopicArn": "arn:aws:sns:us-east-1:111111111111:560-sns-red"
}
],
"ResponseMetadata": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"status_code": 200,
"data": {
"PaginationToken": "",
"ResourceTagMappingList": [
{
"ResourceARN": "arn:aws:sns:us-east-1:111111111111:560-sns-red",
"Tags": [
{
"Key": "CustodianRule",
"Value": "ecc-aws-560-unused_sns_topic"
},
{
"Key": "ComplianceStatus",
"Value": "Red"
}
]
}
],
"ResponseMetadata": {}
}
}
6 changes: 6 additions & 0 deletions tests/ecc-aws-560-unused_sns_topic/red_policy_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class PolicyTest(object):

def test_resources(self, base_test, resources):
base_test.assertEqual(len(resources), 1)
base_test.assertEqual(resources[0]["SubscriptionsConfirmed"], "0")
base_test.assertEqual(resources[0]["c7n.metrics"]["AWS/SNS.NumberOfMessagesPublished.Sum.30"][0]["Sum"], 0)

0 comments on commit e6f958a

Please sign in to comment.