main.tf 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. # Contains cloudtrail configuration for the c2 account only
  2. module "s3_logging_bucket" {
  3. source = "../../thirdparty/terraform-aws-s3logging-bucket"
  4. bucket_name = "xdr-cloudtrail-logs-${var.environment}-access-logs"
  5. lifecycle_rules = list(
  6. {
  7. id = "expire-old-logs"
  8. enabled = true
  9. prefix = ""
  10. expiration = 30
  11. noncurrent_version_expiration = 30
  12. abort_incomplete_multipart_upload_days = 7
  13. })
  14. tags = merge(var.standard_tags, var.tags)
  15. versioning_enabled = true
  16. }
  17. module "cloudtrail_logging_bucket" {
  18. source = "../../thirdparty/terraform-aws-cloudtrail-bucket"
  19. allowed_account_ids = var.account_list
  20. bucket_name = "xdr-cloudtrail-logs-${var.environment}"
  21. logging_bucket = module.s3_logging_bucket.s3_bucket_name
  22. region = var.aws_region
  23. tags = merge(var.standard_tags, var.tags)
  24. lifecycle_rules = list(
  25. {
  26. id = "expire-old-logs"
  27. enabled = true
  28. prefix = ""
  29. expiration = 30
  30. noncurrent_version_expiration = 30
  31. abort_incomplete_multipart_upload_days = 7
  32. })
  33. }
  34. resource "aws_s3_bucket_notification" "on_new_object" {
  35. bucket = module.cloudtrail_logging_bucket.s3_bucket_name
  36. topic {
  37. topic_arn = aws_sns_topic.new_object_event.arn
  38. events = [
  39. "s3:ObjectCreated:*",
  40. ]
  41. filter_suffix = ""
  42. }
  43. }
  44. resource "aws_sns_topic" "new_object_event" {
  45. name = "s3-notification-topic-${module.cloudtrail_logging_bucket.s3_bucket_name}"
  46. kms_master_key_id = aws_kms_key.new_object_key.id
  47. }
  48. resource "aws_sns_topic_policy" "this" {
  49. arn = aws_sns_topic.new_object_event.arn
  50. policy = data.aws_iam_policy_document.bucket_can_publish.json
  51. }
  52. data "aws_iam_policy_document" "bucket_can_publish" {
  53. statement {
  54. actions = [
  55. "SNS:Publish",
  56. ]
  57. effect = "Allow"
  58. condition {
  59. test = "ArnLike"
  60. variable = "aws:SourceArn"
  61. values = [
  62. module.cloudtrail_logging_bucket.s3_bucket_arn
  63. ]
  64. }
  65. principals {
  66. type = "AWS"
  67. identifiers = ["*"]
  68. }
  69. resources = [
  70. aws_sns_topic.new_object_event.arn
  71. ]
  72. sid = "allowpublish"
  73. }
  74. statement {
  75. actions = [
  76. "SNS:Subscribe",
  77. "SNS:Receive",
  78. ]
  79. effect = "Allow"
  80. principals {
  81. type = "AWS"
  82. identifiers = ["*"]
  83. }
  84. condition {
  85. test = "ArnEquals"
  86. values = [ aws_sqs_queue.new_s3_object.arn ]
  87. variable = "aws:SourceArn"
  88. }
  89. resources = [
  90. aws_sns_topic.new_object_event.arn
  91. ]
  92. sid = "sid_allow_subscribe"
  93. }
  94. }
  95. # This is the queue for splunk to subscribe to
  96. resource "aws_sqs_queue" "new_s3_object" {
  97. name = "new-objects-for-${module.cloudtrail_logging_bucket.s3_bucket_name}"
  98. visibility_timeout_seconds = 300 # wait 5 minutes before allowing a different splunk instance to process the same message
  99. message_retention_seconds = 604800 # Keep a message in the queue for 7 days
  100. receive_wait_time_seconds = 0 # how long to wait for a message before returning
  101. redrive_policy = "{\"deadLetterTargetArn\":\"${aws_sqs_queue.dlq.arn}\",\"maxReceiveCount\":4}"
  102. tags = merge(var.standard_tags, var.tags)
  103. kms_master_key_id = aws_kms_key.new_object_key.id
  104. kms_data_key_reuse_period_seconds = 3600
  105. }
  106. data "aws_iam_policy_document" "sns_topic_can_publish" {
  107. statement {
  108. effect = "Allow"
  109. principals {
  110. identifiers = [
  111. "*",
  112. ]
  113. type = "AWS"
  114. }
  115. actions = [
  116. "SQS:SendMessage",
  117. ]
  118. resources = [
  119. aws_sqs_queue.new_s3_object.arn
  120. ]
  121. condition {
  122. test = "ArnEquals"
  123. values = [
  124. aws_sns_topic.new_object_event.arn
  125. ]
  126. variable = "aws:SourceArn"
  127. }
  128. }
  129. }
  130. // Dead Letter queue, use same parameters as main queue
  131. resource "aws_sqs_queue" "dlq" {
  132. name = "new-objects-for-${module.cloudtrail_logging_bucket.s3_bucket_name}-dlq"
  133. message_retention_seconds = 300
  134. receive_wait_time_seconds = 0
  135. tags = merge(var.standard_tags, var.tags)
  136. kms_master_key_id = aws_kms_key.new_object_key.id
  137. kms_data_key_reuse_period_seconds = 3600
  138. }
  139. resource "aws_sqs_queue_policy" "bucket_can_publish" {
  140. policy = data.aws_iam_policy_document.sns_topic_can_publish.json
  141. queue_url = aws_sqs_queue.new_s3_object.id
  142. }
  143. resource "aws_sns_topic_subscription" "bucket_change_notification_to_queue" {
  144. topic_arn = aws_sns_topic.new_object_event.arn
  145. protocol = "sqs"
  146. endpoint = aws_sqs_queue.new_s3_object.arn
  147. }
  148. resource "aws_kms_key" "new_object_key" {
  149. description = "Encryption of SNS and SQS queues on new S3 objects"
  150. enable_key_rotation = true
  151. policy = data.aws_iam_policy_document.new_object_key_kms_policy.json
  152. }
  153. data "aws_iam_policy_document" "new_object_key_kms_policy" {
  154. statement {
  155. effect = "Allow"
  156. principals {
  157. identifiers = ["s3.amazonaws.com", "sns.amazonaws.com", "sqs.amazonaws.com"]
  158. type = "Service"
  159. }
  160. actions = [
  161. "kms:GenerateDataKey",
  162. "kms:Decrypt"
  163. ]
  164. resources = [ "*" ]
  165. }
  166. # allow account to modify/manage key
  167. statement {
  168. effect = "Allow"
  169. principals {
  170. identifiers = ["arn:${var.aws_partition}:iam::${var.aws_account_id}:root"]
  171. type = "AWS"
  172. }
  173. actions = [
  174. "kms:*"
  175. ]
  176. resources = ["*"]
  177. }
  178. }
  179. resource "aws_kms_alias" "new_object_key_alias" {
  180. name = "alias/new_object_key"
  181. target_key_id = aws_kms_key.new_object_key.key_id
  182. }