iam.tf 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. # ]
  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. resources = [
  87. aws_s3_bucket.bucket.arn,
  88. "${aws_s3_bucket.bucket.arn}/*",
  89. ]
  90. }
  91. statement {
  92. sid = "S3ReadOnlyBucketAccess"
  93. effect = "Allow"
  94. actions = [
  95. "s3:ListBucketVersions",
  96. "s3:ListBucket",
  97. "s3:GetBucketVersioning",
  98. "s3:GetObject",
  99. "s3:GetBucketCORS",
  100. "s3:GetBucketLocation",
  101. "s3:GetObjectVersion",
  102. ]
  103. resources = [
  104. aws_s3_bucket.bucket.arn,
  105. "${aws_s3_bucket.bucket.arn}/*",
  106. ]
  107. }
  108. statement {
  109. sid = "KMSKeyAccess"
  110. effect = "Allow"
  111. actions = [
  112. "kms:Decrypt",
  113. "kms:GenerateDataKeyWithoutPlaintext",
  114. "kms:Verify",
  115. "kms:GenerateDataKeyPairWithoutPlaintext",
  116. "kms:GenerateDataKeyPair",
  117. "kms:ReEncryptFrom",
  118. "kms:Encrypt",
  119. "kms:GenerateDataKey",
  120. "kms:ReEncryptTo",
  121. "kms:Sign",
  122. ]
  123. resources = [aws_kms_key.bucketkey.arn]
  124. }
  125. }