main.tf 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 tfsec:ignore:aws-s3-block-public-policy tfsec:ignore:aws-s3-ignore-public-acls
  49. #tfsec:ignore:aws-s3-no-public-buckets Certificate CRLs need to be publicly accessible
  50. resource "aws_s3_bucket" "xdr-shared-amis" {
  51. bucket = var.ami_bucket_name
  52. tags = merge(local.standard_tags, var.tags)
  53. }
  54. resource "aws_s3_bucket_acl" "s3_acl_xdr-shared-amis" {
  55. bucket = aws_s3_bucket.xdr-shared-amis.id
  56. acl = "private"
  57. }
  58. resource "aws_s3_bucket_server_side_encryption_configuration" "s3_sse_xdr-shared-amis" {
  59. bucket = aws_s3_bucket.xdr-shared-amis.id
  60. rule {
  61. apply_server_side_encryption_by_default {
  62. kms_master_key_id = module.shared_ami_key.key_arn
  63. sse_algorithm = "aws:kms"
  64. }
  65. }
  66. }
  67. resource "aws_iam_role" "vmimport" {
  68. name = "vmimport"
  69. description = "Required role for importing AMIs from S3"
  70. assume_role_policy = <<EOF
  71. {
  72. "Version": "2012-10-17",
  73. "Statement": [
  74. {
  75. "Effect": "Allow",
  76. "Principal": { "Service": "vmie.amazonaws.com" },
  77. "Action": "sts:AssumeRole",
  78. "Condition": {
  79. "StringEquals":{
  80. "sts:Externalid": "vmimport"
  81. }
  82. }
  83. }
  84. ]
  85. }
  86. EOF
  87. }
  88. resource "aws_iam_role_policy" "vmimport" {
  89. name = "vmimport"
  90. role = aws_iam_role.vmimport.id
  91. policy = <<EOF
  92. {
  93. "Version":"2012-10-17",
  94. "Statement": [
  95. {
  96. "Sid": "AllowAccesstoImportsBucket",
  97. "Effect": "Allow",
  98. "Action": [
  99. "s3:GetBucketLocation",
  100. "s3:GetObject",
  101. "s3:GetBucketAcl",
  102. "s3:ListBucket",
  103. "s3:PutObject"
  104. ],
  105. "Resource": ${jsonencode(local.bucket_resources)}
  106. },
  107. {
  108. "Sid": "AllowAccesstodoImportExportActions",
  109. "Effect": "Allow",
  110. "Action": [
  111. "ec2:ModifySnapshotAttribute",
  112. "ec2:CopySnapshot",
  113. "ec2:RegisterImage",
  114. "ec2:Describe*"
  115. ],
  116. "Resource": "*"
  117. },
  118. {
  119. "Sid": "AllowAccesstotheKMSkey",
  120. "Effect": "Allow",
  121. "Action": [
  122. "kms:CreateGrant",
  123. "kms:Decrypt",
  124. "kms:DescribeKey",
  125. "kms:Encrypt",
  126. "kms:GenerateDataKey*",
  127. "kms:ReEncrypt*"
  128. ],
  129. "Resource": ${jsonencode(local.all_keys)}
  130. }
  131. ]
  132. }
  133. EOF
  134. }
  135. //AWS Provider outdated arguments <4.4.0
  136. /*resource "aws_s3_bucket" "xdr-shared-amis" {
  137. bucket = var.ami_bucket_name
  138. acl = "private"
  139. tags = merge(local.standard_tags, var.tags)
  140. server_side_encryption_configuration {
  141. rule {
  142. apply_server_side_encryption_by_default {
  143. kms_master_key_id = module.shared_ami_key.key_arn
  144. sse_algorithm = "aws:kms"
  145. }
  146. }
  147. }
  148. }
  149. */