iam.tf 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. data "aws_iam_policy_document" "assume_role_policy" {
  2. statement {
  3. sid = "AllowRoles"
  4. effect = "Allow"
  5. actions = ["sts:AssumeRole"]
  6. principals {
  7. type = "AWS"
  8. identifiers = var.role_assumers
  9. }
  10. }
  11. }
  12. resource "aws_iam_role" "role" {
  13. name = local.fullname
  14. path = "/service/"
  15. force_detach_policies = true # causes "DeleteConflict" if not present
  16. assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
  17. tags = merge(local.standard_tags, var.tags)
  18. }
  19. # Appears the role can automatically create presigned URLs
  20. #resource "aws_iam_role_policy_attachment" "policy_attach_presigned_url" {
  21. # count = var.allow_presigned ? 1 : 0
  22. #
  23. # role = aws_iam_role.role.name
  24. # policy_arn = aws_iam_policy.policy_presigned_url.arn
  25. #}
  26. #
  27. #resource "aws_iam_policy" "policy_presigned_url" {
  28. # count = var.allow_presigned ? 1 : 0
  29. #
  30. # name_prefix = var.name
  31. # path = "/service/"
  32. # description = "Policy to allow signing of URLs for the ${local.fullname} bucket"
  33. # policy = data.aws_iam_policy_document.policy_doc_presigned_url.json
  34. #}
  35. #
  36. #data "aws_iam_policy_document" "policy_doc_presigned_url" {
  37. # count = var.allow_presigned ? 1 : 0
  38. #
  39. # statement {
  40. # sid = "TODO"
  41. # effect = "Allow"
  42. # actions = [
  43. # "s3:ListAllMyBuckets",
  44. # ]
  45. # resources = [ "*" ]
  46. # }
  47. #}
  48. resource "aws_iam_role_policy_attachment" "policy_attach" {
  49. role = aws_iam_role.role.name
  50. policy_arn = aws_iam_policy.policy.arn
  51. }
  52. resource "aws_iam_policy" "policy" {
  53. name_prefix = var.name
  54. path = "/service/"
  55. description = "Policy to allow use of the ${local.fullname} bucket"
  56. policy = data.aws_iam_policy_document.policy_doc.json
  57. }
  58. data "aws_iam_policy_document" "policy_doc" {
  59. statement {
  60. sid = "GeneralBucketAccess"
  61. effect = "Allow"
  62. actions = [
  63. "s3:ListAllMyBuckets",
  64. ]
  65. resources = ["*"]
  66. }
  67. statement {
  68. sid = "S3BucketAccess"
  69. effect = "Allow"
  70. actions = [
  71. "s3:GetLifecycleConfiguration",
  72. "s3:DeleteObjectVersion",
  73. "s3:ListBucketVersions",
  74. "s3:GetBucketLogging",
  75. "s3:RestoreObject",
  76. "s3:ListBucket",
  77. "s3:GetBucketVersioning",
  78. "s3:PutObject",
  79. "s3:GetObject",
  80. "s3:PutLifecycleConfiguration",
  81. "s3:GetBucketCORS",
  82. "s3:DeleteObject",
  83. "s3:GetBucketLocation",
  84. "s3:GetObjectVersion",
  85. ]
  86. # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
  87. resources = [
  88. aws_s3_bucket.bucket.arn,
  89. "${aws_s3_bucket.bucket.arn}/*",
  90. ]
  91. }
  92. statement {
  93. sid = "S3ReadOnlyBucketAccess"
  94. effect = "Allow"
  95. actions = [
  96. "s3:ListBucketVersions",
  97. "s3:ListBucket",
  98. "s3:GetBucketVersioning",
  99. "s3:GetObject",
  100. "s3:GetBucketCORS",
  101. "s3:GetBucketLocation",
  102. "s3:GetObjectVersion",
  103. ]
  104. # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
  105. resources = [
  106. aws_s3_bucket.bucket.arn,
  107. "${aws_s3_bucket.bucket.arn}/*",
  108. ]
  109. }
  110. statement {
  111. sid = "KMSKeyAccess"
  112. effect = "Allow"
  113. actions = [
  114. "kms:Decrypt",
  115. "kms:GenerateDataKeyWithoutPlaintext",
  116. "kms:Verify",
  117. "kms:GenerateDataKeyPairWithoutPlaintext",
  118. "kms:GenerateDataKeyPair",
  119. "kms:ReEncryptFrom",
  120. "kms:Encrypt",
  121. "kms:GenerateDataKey",
  122. "kms:ReEncryptTo",
  123. "kms:Sign",
  124. ]
  125. resources = [aws_kms_key.bucketkey.arn]
  126. }
  127. }