main.tf 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. locals {
  2. account_arns = [
  3. for account in local.account_list :
  4. "arn:${var.aws_partition}:iam::${account}:root"
  5. ]
  6. terraformer_arns = [
  7. for account in local.account_list :
  8. "arn:${var.aws_partition}:iam::${account}:role/user/mdr_terraformer"
  9. ]
  10. user_arns = [
  11. "arn:${var.aws_partition}:iam::${var.aws_account_id}:role/aws_services/codebuild_packer_role"
  12. ]
  13. # All users are also attachers
  14. attacher_arns = distinct(flatten([
  15. local.terraformer_arns,
  16. local.user_arns
  17. ]))
  18. all_keys = concat([module.shared_ami_key.key_arn], var.vmimport_extra_keys)
  19. buckets = [
  20. for bucket in concat([aws_s3_bucket.xdr-shared-amis.arn], var.vmimport_extra_buckets) :
  21. bucket
  22. ]
  23. bucket_contents = [
  24. for bucket in concat([aws_s3_bucket.xdr-shared-amis.arn], var.vmimport_extra_buckets) :
  25. "${bucket}/*"
  26. ]
  27. bucket_resources = concat(local.buckets, local.bucket_contents)
  28. }
  29. output "other" {
  30. value = local.account_arns
  31. }
  32. module "shared_ami_key" {
  33. source = "../../submodules/kms/ami-key"
  34. name = "shared_ami_key"
  35. alias = "alias/shared_ami_key"
  36. description = "Key for encrypting the AMIs to be shared with other accounts."
  37. tags = merge(local.standard_tags, var.tags)
  38. key_admin_arns = []
  39. key_user_arns = local.user_arns
  40. #key_attacher_arns = local.account_arns
  41. key_attacher_arns = local.attacher_arns
  42. #key_attacher_arns = [ ]
  43. standard_tags = local.standard_tags
  44. aws_account_id = var.aws_account_id
  45. aws_partition = var.aws_partition
  46. remote_account_arns = local.account_arns
  47. }
  48. # tfsec:ignore:aws-s3-block-public-acls
  49. # tfsec:ignore:aws-s3-specify-public-access-block
  50. # tfsec:ignore:aws-s3-block-public-policy
  51. # tfsec:ignore:aws-s3-ignore-public-acls
  52. # tfsec:ignore:aws-s3-no-public-buckets Certificate CRLs need to be publicly accessible
  53. # tfsec:ignore:aws-s3-enable-bucket-logging TODO: enable everywhere at a later date if required
  54. # tfsec:ignore:aws-s3-enable-versioning versioning Suspended for this bucket
  55. resource "aws_s3_bucket" "xdr-shared-amis" {
  56. # checkov:skip=CKV2_AWS_6: see tfsec S3 block policy
  57. # checkov:skip=CKV_AWS_18: see tfsec S3 logging above
  58. # checkov:skip=CKV_AWS_21: Versioning TODO
  59. # checkov:skip=CKV_AWS_144: Cross-region replication TODO
  60. # checkov:skip=CKV_AWS_145: Risk is low for AES-256 encryption
  61. bucket = var.ami_bucket_name
  62. tags = merge(local.standard_tags, var.tags)
  63. }
  64. resource "aws_s3_bucket_acl" "s3_acl_xdr-shared-amis" {
  65. bucket = aws_s3_bucket.xdr-shared-amis.id
  66. acl = "private"
  67. }
  68. resource "aws_s3_bucket_server_side_encryption_configuration" "s3_sse_xdr-shared-amis" {
  69. bucket = aws_s3_bucket.xdr-shared-amis.id
  70. rule {
  71. apply_server_side_encryption_by_default {
  72. kms_master_key_id = module.shared_ami_key.key_arn
  73. sse_algorithm = "aws:kms"
  74. }
  75. }
  76. }
  77. resource "aws_iam_role" "vmimport" {
  78. name = "vmimport"
  79. description = "Required role for importing AMIs from S3"
  80. assume_role_policy = <<EOF
  81. {
  82. "Version": "2012-10-17",
  83. "Statement": [
  84. {
  85. "Effect": "Allow",
  86. "Principal": { "Service": "vmie.amazonaws.com" },
  87. "Action": "sts:AssumeRole",
  88. "Condition": {
  89. "StringEquals":{
  90. "sts:Externalid": "vmimport"
  91. }
  92. }
  93. }
  94. ]
  95. }
  96. EOF
  97. }
  98. resource "aws_iam_role_policy" "vmimport" {
  99. name = "vmimport"
  100. role = aws_iam_role.vmimport.id
  101. # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
  102. policy = <<EOF
  103. {
  104. "Version":"2012-10-17",
  105. "Statement": [
  106. {
  107. "Sid": "AllowAccesstoImportsBucket",
  108. "Effect": "Allow",
  109. "Action": [
  110. "s3:GetBucketLocation",
  111. "s3:GetObject",
  112. "s3:GetBucketAcl",
  113. "s3:ListBucket",
  114. "s3:PutObject"
  115. ],
  116. "Resource": ${jsonencode(local.bucket_resources)}
  117. },
  118. {
  119. "Sid": "AllowAccesstodoImportExportActions",
  120. "Effect": "Allow",
  121. "Action": [
  122. "ec2:ModifySnapshotAttribute",
  123. "ec2:CopySnapshot",
  124. "ec2:RegisterImage",
  125. "ec2:Describe*"
  126. ],
  127. "Resource": "*"
  128. },
  129. {
  130. "Sid": "AllowAccesstotheKMSkey",
  131. "Effect": "Allow",
  132. "Action": [
  133. "kms:CreateGrant",
  134. "kms:Decrypt",
  135. "kms:DescribeKey",
  136. "kms:Encrypt",
  137. "kms:GenerateDataKey*",
  138. "kms:ReEncrypt*"
  139. ],
  140. "Resource": ${jsonencode(local.all_keys)}
  141. }
  142. ]
  143. }
  144. EOF
  145. }
  146. //AWS Provider outdated arguments <4.4.0
  147. /*resource "aws_s3_bucket" "xdr-shared-amis" {
  148. bucket = var.ami_bucket_name
  149. acl = "private"
  150. tags = merge(local.standard_tags, var.tags)
  151. server_side_encryption_configuration {
  152. rule {
  153. apply_server_side_encryption_by_default {
  154. kms_master_key_id = module.shared_ami_key.key_arn
  155. sse_algorithm = "aws:kms"
  156. }
  157. }
  158. }
  159. }
  160. */