main.tf 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. resource "aws_s3_bucket" "xdr-shared-amis" {
  54. # checkov:skip=CKV_AWS_21: Versioning TODO
  55. # checkov:skip=CKV_AWS_144: Cross-region replication TODO
  56. # checkov:skip=CKV_AWS_145: Risk is low for AES-256 encryption
  57. # checkov:skip=CKV2_AWS_6: see tfsec S3 block policy
  58. # checkov:skip=CKV_AWS_18: see tfsec S3 logging above
  59. bucket = var.ami_bucket_name
  60. tags = merge(local.standard_tags, var.tags)
  61. }
  62. resource "aws_s3_bucket_acl" "s3_acl_xdr-shared-amis" {
  63. bucket = aws_s3_bucket.xdr-shared-amis.id
  64. acl = "private"
  65. }
  66. resource "aws_s3_bucket_server_side_encryption_configuration" "s3_sse_xdr-shared-amis" {
  67. bucket = aws_s3_bucket.xdr-shared-amis.id
  68. rule {
  69. apply_server_side_encryption_by_default {
  70. kms_master_key_id = module.shared_ami_key.key_arn
  71. sse_algorithm = "aws:kms"
  72. }
  73. }
  74. }
  75. resource "aws_iam_role" "vmimport" {
  76. name = "vmimport"
  77. description = "Required role for importing AMIs from S3"
  78. assume_role_policy = <<EOF
  79. {
  80. "Version": "2012-10-17",
  81. "Statement": [
  82. {
  83. "Effect": "Allow",
  84. "Principal": { "Service": "vmie.amazonaws.com" },
  85. "Action": "sts:AssumeRole",
  86. "Condition": {
  87. "StringEquals":{
  88. "sts:Externalid": "vmimport"
  89. }
  90. }
  91. }
  92. ]
  93. }
  94. EOF
  95. }
  96. resource "aws_iam_role_policy" "vmimport" {
  97. name = "vmimport"
  98. role = aws_iam_role.vmimport.id
  99. # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
  100. policy = <<EOF
  101. {
  102. "Version":"2012-10-17",
  103. "Statement": [
  104. {
  105. "Sid": "AllowAccesstoImportsBucket",
  106. "Effect": "Allow",
  107. "Action": [
  108. "s3:GetBucketLocation",
  109. "s3:GetObject",
  110. "s3:GetBucketAcl",
  111. "s3:ListBucket",
  112. "s3:PutObject"
  113. ],
  114. "Resource": ${jsonencode(local.bucket_resources)}
  115. },
  116. {
  117. "Sid": "AllowAccesstodoImportExportActions",
  118. "Effect": "Allow",
  119. "Action": [
  120. "ec2:ModifySnapshotAttribute",
  121. "ec2:CopySnapshot",
  122. "ec2:RegisterImage",
  123. "ec2:Describe*"
  124. ],
  125. "Resource": "*"
  126. },
  127. {
  128. "Sid": "AllowAccesstotheKMSkey",
  129. "Effect": "Allow",
  130. "Action": [
  131. "kms:CreateGrant",
  132. "kms:Decrypt",
  133. "kms:DescribeKey",
  134. "kms:Encrypt",
  135. "kms:GenerateDataKey*",
  136. "kms:ReEncrypt*"
  137. ],
  138. "Resource": ${jsonencode(local.all_keys)}
  139. }
  140. ]
  141. }
  142. EOF
  143. }
  144. //AWS Provider outdated arguments <4.4.0
  145. /*resource "aws_s3_bucket" "xdr-shared-amis" {
  146. bucket = var.ami_bucket_name
  147. acl = "private"
  148. tags = merge(local.standard_tags, var.tags)
  149. server_side_encryption_configuration {
  150. rule {
  151. apply_server_side_encryption_by_default {
  152. kms_master_key_id = module.shared_ami_key.key_arn
  153. sse_algorithm = "aws:kms"
  154. }
  155. }
  156. }
  157. }
  158. */