Skip to content

Commit b69e624

Browse files
authored
Merge pull request #54 from cloudandthings/encrypt_sns_topic
feat: Add `kms_key_id_for_sns_topic`
2 parents 6e92ad9 + 502714e commit b69e624

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Full contributing [guidelines are covered here](.github/contributing.md).
4949
| <a name="input_iam_role_arn"></a> [iam\_role\_arn](#input\_iam\_role\_arn) | Existing IAM role ARN for the lambda. Required if `create_iam_role` is set to `false` | `string` | `null` | no |
5050
| <a name="input_included_accounts"></a> [included\_accounts](#input\_included\_accounts) | List of accounts that be scanned to manual actions. If empty will scan all accounts. | `list(string)` | `[]` | no |
5151
| <a name="input_included_users"></a> [included\_users](#input\_included\_users) | List of emails that be scanned to manual actions. If empty will scan all emails. | `list(string)` | `[]` | no |
52+
| <a name="input_kms_key_id_for_sns_topic"></a> [kms\_key\_id\_for\_sns\_topic](#input\_kms\_key\_id\_for\_sns\_topic) | KMS key ID for encrypting the sns\_topic (only applicable to org deployments). | `string` | `null` | no |
5253
| <a name="input_lambda_deployment_s3_bucket"></a> [lambda\_deployment\_s3\_bucket](#input\_lambda\_deployment\_s3\_bucket) | S3 bucket for lambda deployment package. | `string` | `null` | no |
5354
| <a name="input_lambda_deployment_s3_key"></a> [lambda\_deployment\_s3\_key](#input\_lambda\_deployment\_s3\_key) | S3 object key for lambda deployment package. Otherwise, defaults to `var.naming_prefix/local.deployment_filename`. | `string` | `null` | no |
5455
| <a name="input_lambda_deployment_upload_to_s3_enabled"></a> [lambda\_deployment\_upload\_to\_s3\_enabled](#input\_lambda\_deployment\_upload\_to\_s3\_enabled) | If `true`, the lambda deployment package within this module repo will be copied to S3. If `false` then the S3 object must be uploaded separately. Ignored if `lambda_deployment_s3_bucket` is null. | `bool` | `true` | no |

deployment_organization.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ resource "aws_sns_topic" "bucket_notifications" {
2727
# Cannot use AWS managed KMS key with S3 bucket notifications
2828
# Ref: https://aws.amazon.com/premiumsupport/knowledge-center/sns-not-receiving-s3-event-notifications/
2929
# kms_master_key_id = "alias/aws/sns"
30+
kms_master_key_id = var.kms_key_id_for_sns_topic
3031

3132
tags = var.tags
3233
}

examples/basic/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,60 @@ module "clickops_notifications" {
4646
webhook = "https://fake.com"
4747
message_format = "slack"
4848
tags = local.tags
49+
50+
# Optional
51+
kms_key_id_for_sns_topic = aws_kms_key.clickops_sns_topic.arn
4952
}
5053
5154
5255
resource "aws_s3_bucket" "test_bucket" {
5356
bucket = local.naming_prefix
5457
tags = local.tags
5558
}
59+
60+
# To encrypt the SNS topic
61+
data "aws_caller_identity" "current" {}
62+
63+
data "aws_iam_policy_document" "clickops_sns_topic" {
64+
statement {
65+
sid = "Enable IAM User Permissions"
66+
principals {
67+
type = "AWS"
68+
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
69+
}
70+
actions = ["kms:*"]
71+
resources = ["*"]
72+
}
73+
statement {
74+
principals {
75+
type = "Service"
76+
identifiers = ["s3.amazonaws.com"]
77+
}
78+
actions = [
79+
"kms:GenerateDataKey*",
80+
"kms:Decrypt"
81+
]
82+
resources = ["*"]
83+
condition {
84+
test = "ArnEquals"
85+
variable = "aws:SourceArn"
86+
values = [aws_s3_bucket.test_bucket.arn]
87+
}
88+
condition {
89+
test = "StringEquals"
90+
variable = "aws:SourceAccount"
91+
values = [data.aws_caller_identity.current.account_id]
92+
}
93+
}
94+
}
95+
96+
resource "aws_kms_key" "clickops_sns_topic" {
97+
description = "KMS key for SNS topic ${local.naming_prefix}"
98+
deletion_window_in_days = 7
99+
policy = data.aws_iam_policy_document.clickops_sns_topic.json
100+
101+
tags = local.tags
102+
}
56103
```
57104
----
58105

@@ -99,8 +146,11 @@ No outputs.
99146

100147
| Name | Type |
101148
|------|------|
149+
| [aws_kms_key.clickops_sns_topic](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
102150
| [aws_s3_bucket.test_bucket](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
103151
| [random_pet.run_id](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource |
152+
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
153+
| [aws_iam_policy_document.clickops_sns_topic](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
104154

105155
----
106156
<!-- END_TF_DOCS -->

examples/basic/main.tf

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,57 @@ module "clickops_notifications" {
4242
webhook = "https://fake.com"
4343
message_format = "slack"
4444
tags = local.tags
45+
46+
# Optional
47+
kms_key_id_for_sns_topic = aws_kms_key.clickops_sns_topic.arn
4548
}
4649

4750

4851
resource "aws_s3_bucket" "test_bucket" {
4952
bucket = local.naming_prefix
5053
tags = local.tags
5154
}
55+
56+
# To encrypt the SNS topic
57+
data "aws_caller_identity" "current" {}
58+
59+
data "aws_iam_policy_document" "clickops_sns_topic" {
60+
statement {
61+
sid = "Enable IAM User Permissions"
62+
principals {
63+
type = "AWS"
64+
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
65+
}
66+
actions = ["kms:*"]
67+
resources = ["*"]
68+
}
69+
statement {
70+
principals {
71+
type = "Service"
72+
identifiers = ["s3.amazonaws.com"]
73+
}
74+
actions = [
75+
"kms:GenerateDataKey*",
76+
"kms:Decrypt"
77+
]
78+
resources = ["*"]
79+
condition {
80+
test = "ArnEquals"
81+
variable = "aws:SourceArn"
82+
values = [aws_s3_bucket.test_bucket.arn]
83+
}
84+
condition {
85+
test = "StringEquals"
86+
variable = "aws:SourceAccount"
87+
values = [data.aws_caller_identity.current.account_id]
88+
}
89+
}
90+
}
91+
92+
resource "aws_kms_key" "clickops_sns_topic" {
93+
description = "KMS key for SNS topic ${local.naming_prefix}"
94+
deletion_window_in_days = 7
95+
policy = data.aws_iam_policy_document.clickops_sns_topic.json
96+
97+
tags = local.tags
98+
}

variables.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ variable "additional_iam_policy_statements" {
201201
default = {}
202202
}
203203

204+
# Encryption configuration
205+
variable "kms_key_id_for_sns_topic" {
206+
description = "KMS key ID for encrypting the sns_topic (only applicable to org deployments)."
207+
type = string
208+
default = null
209+
}
210+
204211
# Other configuration
205212
variable "firehose_delivery_stream_name" {
206213
description = "Kinesis Firehose delivery stream name to output ClickOps events to."

0 commit comments

Comments
 (0)