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. # TODO: Can we filter out the digests?
  42. filter_suffix = ""
  43. }
  44. }
  45. resource "aws_sns_topic" "new_object_event" {
  46. name = "s3-notification-topic-${module.cloudtrail_logging_bucket.s3_bucket_name}"
  47. kms_master_key_id = aws_kms_key.new_object_key.id
  48. }
  49. resource "aws_sns_topic_policy" "this" {
  50. arn = aws_sns_topic.new_object_event.arn
  51. policy = data.aws_iam_policy_document.bucket_can_publish.json
  52. }
  53. data "aws_iam_policy_document" "bucket_can_publish" {
  54. statement {
  55. actions = [
  56. "SNS:Publish",
  57. ]
  58. effect = "Allow"
  59. condition {
  60. test = "ArnLike"
  61. variable = "aws:SourceArn"
  62. values = [
  63. module.cloudtrail_logging_bucket.s3_bucket_arn
  64. ]
  65. }
  66. principals {
  67. type = "AWS"
  68. identifiers = ["*"]
  69. }
  70. resources = [
  71. aws_sns_topic.new_object_event.arn
  72. ]
  73. sid = "allowpublish"
  74. }
  75. statement {
  76. actions = [
  77. "SNS:Subscribe",
  78. "SNS:Receive",
  79. ]
  80. effect = "Allow"
  81. principals {
  82. type = "AWS"
  83. identifiers = ["*"]
  84. }
  85. condition {
  86. test = "ArnEquals"
  87. values = [ aws_sqs_queue.new_s3_object.arn ]
  88. variable = "aws:SourceArn"
  89. }
  90. resources = [
  91. aws_sns_topic.new_object_event.arn
  92. ]
  93. sid = "sid_allow_subscribe"
  94. }
  95. }
  96. # This is the queue for splunk to subscribe to
  97. resource "aws_sqs_queue" "new_s3_object" {
  98. name = "new-objects-for-${module.cloudtrail_logging_bucket.s3_bucket_name}"
  99. visibility_timeout_seconds = 300 # wait 5 minutes before allowing a different splunk instance to process the same message
  100. message_retention_seconds = 604800 # Keep a message in the queue for 7 days
  101. receive_wait_time_seconds = 0 # how long to wait for a message before returning
  102. redrive_policy = "{\"deadLetterTargetArn\":\"${aws_sqs_queue.dlq.arn}\",\"maxReceiveCount\":4}"
  103. tags = merge(var.standard_tags, var.tags)
  104. kms_master_key_id = aws_kms_key.new_object_key.id
  105. kms_data_key_reuse_period_seconds = 3600
  106. }
  107. data "aws_iam_policy_document" "sns_topic_can_publish" {
  108. statement {
  109. effect = "Allow"
  110. principals {
  111. identifiers = [
  112. "*",
  113. ]
  114. type = "AWS"
  115. }
  116. actions = [
  117. "SQS:SendMessage",
  118. ]
  119. resources = [
  120. aws_sqs_queue.new_s3_object.arn
  121. ]
  122. condition {
  123. test = "ArnEquals"
  124. values = [
  125. aws_sns_topic.new_object_event.arn
  126. ]
  127. variable = "aws:SourceArn"
  128. }
  129. }
  130. }
  131. // Dead Letter queue, use same parameters as main queue
  132. resource "aws_sqs_queue" "dlq" {
  133. name = "new-objects-for-${module.cloudtrail_logging_bucket.s3_bucket_name}-dlq"
  134. message_retention_seconds = 300
  135. receive_wait_time_seconds = 0
  136. tags = merge(var.standard_tags, var.tags)
  137. kms_master_key_id = aws_kms_key.new_object_key.id
  138. kms_data_key_reuse_period_seconds = 3600
  139. }
  140. resource "aws_sqs_queue_policy" "bucket_can_publish" {
  141. policy = data.aws_iam_policy_document.sns_topic_can_publish.json
  142. queue_url = aws_sqs_queue.new_s3_object.id
  143. }
  144. resource "aws_sns_topic_subscription" "bucket_change_notification_to_queue" {
  145. topic_arn = aws_sns_topic.new_object_event.arn
  146. protocol = "sqs"
  147. endpoint = aws_sqs_queue.new_s3_object.arn
  148. }
  149. resource "aws_kms_key" "new_object_key" {
  150. description = "Encryption of SNS and SQS queues on new S3 objects"
  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. }