s3.tf 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # tfsec:ignore:aws-s3-enable-bucket-logging TODO: enable everywhere at a later date if required
  2. # tfsec:ignore:aws-s3-enable-versioning versioning Suspended for this bucket
  3. resource "aws_s3_bucket" "bucket" {
  4. # checkov:skip=CKV_AWS_18: see tfsec S3 logging above
  5. # checkov:skip=CKV_AWS_21: versioning Suspended for this bucket
  6. # checkov:skip=CKV_AWS_144: TODO: cross replication
  7. bucket = "xdr-portal-lambda-${var.environment}"
  8. force_destroy = true
  9. }
  10. resource "aws_s3_bucket_acl" "s3_acl_bucket" {
  11. bucket = aws_s3_bucket.bucket.id
  12. acl = "private"
  13. }
  14. resource "aws_s3_bucket_server_side_encryption_configuration" "s3_sse_bucket" {
  15. bucket = aws_s3_bucket.bucket.id
  16. rule {
  17. apply_server_side_encryption_by_default {
  18. kms_master_key_id = aws_kms_key.key.arn
  19. sse_algorithm = "aws:kms"
  20. }
  21. }
  22. }
  23. resource "aws_s3_bucket_public_access_block" "public_access_block" {
  24. bucket = aws_s3_bucket.bucket.id
  25. block_public_acls = true
  26. block_public_policy = true
  27. ignore_public_acls = true
  28. restrict_public_buckets = true
  29. # Not technically dependent, but prevents a "Conflicting conditional operation" conflict.
  30. # See https://github.com/hashicorp/terraform-provider-aws/issues/7628
  31. depends_on = [aws_s3_bucket_policy.policy]
  32. }
  33. data "aws_iam_policy_document" "s3_policy_document" {
  34. statement {
  35. sid = "AllowS3Access"
  36. actions = ["s3:GetObject", "s3:GetObjectVersion"]
  37. effect = "Allow"
  38. resources = [
  39. aws_s3_bucket.bucket.arn,
  40. "${aws_s3_bucket.bucket.arn}/*"
  41. ]
  42. principals {
  43. type = "AWS"
  44. identifiers = ["arn:${var.aws_partition}:iam::${var.aws_account_id}:root"]
  45. }
  46. }
  47. }
  48. resource "aws_s3_bucket_policy" "policy" {
  49. bucket = aws_s3_bucket.bucket.id
  50. policy = data.aws_iam_policy_document.s3_policy_document.json
  51. }
  52. resource "aws_kms_key" "key" {
  53. description = "Encryption of S3 code for portal-scheduler"
  54. policy = data.aws_iam_policy_document.kms_policy_document.json
  55. enable_key_rotation = true
  56. tags = merge(local.standard_tags, var.tags)
  57. }
  58. data "aws_iam_policy_document" "kms_policy_document" {
  59. # checkov:skip=CKV_AWS_109: see tfsec aws-iam-no-policy-wildcard ignore comment
  60. # checkov:skip=CKV_AWS_111: see tfsec aws-iam-no-policy-wildcard ignore comment
  61. statement {
  62. sid = "AllowServices"
  63. effect = "Allow"
  64. principals {
  65. type = "AWS"
  66. identifiers = [
  67. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/user/mdr_terraformer",
  68. "arn:${var.aws_partition}:iam::${var.aws_account_id}:user/MDRAdmin"
  69. ]
  70. }
  71. actions = ["kms:*"]
  72. resources = ["*"]
  73. }
  74. # allow account to modify/manage key
  75. statement {
  76. sid = "AllowThisAccount"
  77. effect = "Allow"
  78. principals {
  79. identifiers = ["arn:${var.aws_partition}:iam::${var.aws_account_id}:root"]
  80. type = "AWS"
  81. }
  82. actions = [
  83. "kms:*"
  84. ]
  85. resources = ["*"]
  86. }
  87. }
  88. resource "aws_kms_alias" "key_alias" {
  89. name = "alias/portal-s3-key"
  90. target_key_id = aws_kms_key.key.key_id
  91. }
  92. //AWS Provider outdated arguments <4.4.0
  93. /*resource "aws_s3_bucket" "bucket" {
  94. bucket = "xdr-portal-lambda-${var.environment}"
  95. force_destroy = true
  96. acl = "private"
  97. server_side_encryption_configuration {
  98. rule {
  99. apply_server_side_encryption_by_default {
  100. kms_master_key_id = aws_kms_key.key.arn
  101. sse_algorithm = "aws:kms"
  102. }
  103. }
  104. }
  105. }
  106. */