iam.tf 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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(var.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. # "s3:HeadBucket",
  45. # ]
  46. # resources = [ "*" ]
  47. # }
  48. #}
  49. resource "aws_iam_role_policy_attachment" "policy_attach" {
  50. role = aws_iam_role.role.name
  51. policy_arn = aws_iam_policy.policy.arn
  52. }
  53. resource "aws_iam_policy" "policy" {
  54. name_prefix = var.name
  55. path = "/service/"
  56. description = "Policy to allow use of the ${local.fullname} bucket"
  57. policy = data.aws_iam_policy_document.policy_doc.json
  58. }
  59. data "aws_iam_policy_document" "policy_doc" {
  60. statement {
  61. sid = "GeneralBucketAccess"
  62. effect = "Allow"
  63. actions = [
  64. "s3:ListAllMyBuckets",
  65. "s3:HeadBucket",
  66. ]
  67. resources = ["*"]
  68. }
  69. statement {
  70. sid = "S3BucketAccess"
  71. effect = "Allow"
  72. actions = [
  73. "s3:GetLifecycleConfiguration",
  74. "s3:DeleteObjectVersion",
  75. "s3:ListBucketVersions",
  76. "s3:GetBucketLogging",
  77. "s3:RestoreObject",
  78. "s3:ListBuckets",
  79. "s3:ListObjects",
  80. "s3:ListObjectsV2",
  81. "s3:GetBucketVersioning",
  82. "s3:PutObject",
  83. "s3:GetObject",
  84. "s3:PutLifecycleConfiguration",
  85. "s3:GetBucketCORS",
  86. "s3:DeleteObject",
  87. "s3:GetBucketLocation",
  88. "s3:GetObjectVersion",
  89. ]
  90. resources = [
  91. aws_s3_bucket.bucket.arn,
  92. "${aws_s3_bucket.bucket.arn}/*",
  93. ]
  94. }
  95. statement {
  96. sid = "S3ReadOnlyBucketAccess"
  97. effect = "Allow"
  98. actions = [
  99. "s3:ListBucketVersions",
  100. "s3:ListBuckets",
  101. "s3:GetBucketVersioning",
  102. "s3:GetObject",
  103. "s3:GetBucketCORS",
  104. "s3:GetBucketLocation",
  105. "s3:GetObjectVersion",
  106. ]
  107. resources = [
  108. aws_s3_bucket.bucket.arn,
  109. "${aws_s3_bucket.bucket.arn}/*",
  110. ]
  111. }
  112. statement {
  113. sid = "KMSKeyAccess"
  114. effect = "Allow"
  115. actions = [
  116. "kms:Decrypt",
  117. "kms:GenerateDataKeyWithoutPlaintext",
  118. "kms:Verify",
  119. "kms:GenerateDataKeyPairWithoutPlaintext",
  120. "kms:GenerateDataKeyPair",
  121. "kms:ReEncryptFrom",
  122. "kms:Encrypt",
  123. "kms:GenerateDataKey",
  124. "kms:ReEncryptTo",
  125. "kms:Sign",
  126. ]
  127. resources = [aws_kms_key.bucketkey.arn]
  128. }
  129. }