main.tf 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. locals {
  2. action_runner_distribution_object_key = "actions-runner-${var.runner_os}.${var.runner_os == "linux" ? "tar.gz" : "zip"}"
  3. }
  4. # tfsec:ignore:aws-s3-enable-bucket-logging TODO: enable everywhere at a later date if required
  5. # tfsec:ignore:aws-s3-enable-versioning versioning Suspended for this bucket
  6. resource "aws_s3_bucket" "action_dist" {
  7. # checkov:skip=CKV_AWS_18: see tfsec S3 logging above
  8. # checkov:skip=CKV_AWS_19: False positive due to var.encryption
  9. # checkov:skip=CKV_AWS_21: versioning Suspended for this bucket
  10. # checkov:skip=CKV_AWS_144: TODO: cross replication
  11. # checkov:skip=CKV_AWS_145: False positive due to var.encryption
  12. bucket = var.distribution_bucket_name
  13. force_destroy = true
  14. tags = var.tags
  15. }
  16. resource "aws_s3_bucket_acl" "action_dist_acl" {
  17. bucket = aws_s3_bucket.action_dist.id
  18. acl = "private"
  19. }
  20. resource "aws_s3_bucket_lifecycle_configuration" "bucket-config" {
  21. bucket = aws_s3_bucket.action_dist.id
  22. rule {
  23. id = "lifecycle_config"
  24. status = "Enabled"
  25. abort_incomplete_multipart_upload {
  26. days_after_initiation = 7
  27. }
  28. transition {
  29. days = 35
  30. storage_class = "INTELLIGENT_TIERING"
  31. }
  32. }
  33. }
  34. # tfsec:ignore:aws-s3-encryption-customer-key Risk is low for AES-256 encryption
  35. resource "aws_s3_bucket_server_side_encryption_configuration" "action_dist" {
  36. bucket = aws_s3_bucket.action_dist.id
  37. count = try(var.server_side_encryption_configuration, null) != null ? 1 : 0
  38. # tfsec:ignore:aws-s3-enable-bucket-encryption FalsePos
  39. dynamic "rule" {
  40. for_each = [lookup(var.server_side_encryption_configuration, "rule", {})]
  41. content {
  42. bucket_key_enabled = lookup(rule.value, "bucket_key_enabled", null)
  43. dynamic "apply_server_side_encryption_by_default" {
  44. for_each = length(keys(lookup(rule.value, "apply_server_side_encryption_by_default", {}))) == 0 ? [] : [
  45. lookup(rule.value, "apply_server_side_encryption_by_default", {})]
  46. content {
  47. sse_algorithm = apply_server_side_encryption_by_default.value.sse_algorithm
  48. kms_master_key_id = lookup(apply_server_side_encryption_by_default.value, "kms_master_key_id", null)
  49. }
  50. }
  51. }
  52. }
  53. }
  54. resource "aws_s3_bucket_public_access_block" "action_dist" {
  55. bucket = aws_s3_bucket.action_dist.id
  56. block_public_acls = true
  57. block_public_policy = true
  58. ignore_public_acls = true
  59. restrict_public_buckets = true
  60. }
  61. data "aws_iam_policy_document" "action_dist_sse_policy" {
  62. count = try(var.server_side_encryption_configuration.rule.apply_server_side_encryption_by_default, null) != null ? 1 : 0
  63. statement {
  64. effect = "Deny"
  65. principals {
  66. type = "AWS"
  67. identifiers = [
  68. "*",
  69. ]
  70. }
  71. actions = [
  72. "s3:PutObject",
  73. ]
  74. resources = [
  75. "${aws_s3_bucket.action_dist.arn}/*",
  76. ]
  77. condition {
  78. test = "StringNotEquals"
  79. variable = "s3:x-amz-server-side-encryption"
  80. values = [var.server_side_encryption_configuration.rule.apply_server_side_encryption_by_default.sse_algorithm]
  81. }
  82. }
  83. }
  84. resource "aws_s3_bucket_policy" "action_dist_sse_policy" {
  85. count = try(var.server_side_encryption_configuration.rule.apply_server_side_encryption_by_default, null) != null ? 1 : 0
  86. bucket = aws_s3_bucket.action_dist.id
  87. policy = data.aws_iam_policy_document.action_dist_sse_policy[0].json
  88. }