main.tf 5.8 KB

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