123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- data "aws_iam_policy_document" "assume_role_policy" {
- statement {
- sid = "AllowRoles"
- effect = "Allow"
- actions = ["sts:AssumeRole"]
- principals {
- type = "AWS"
- identifiers = var.role_assumers
- }
- }
- }
- resource "aws_iam_role" "role" {
- name = local.fullname
- path = "/service/"
- force_detach_policies = true # causes "DeleteConflict" if not present
- assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
- tags = merge(local.standard_tags, var.tags)
- }
- # Appears the role can automatically create presigned URLs
- #resource "aws_iam_role_policy_attachment" "policy_attach_presigned_url" {
- # count = var.allow_presigned ? 1 : 0
- #
- # role = aws_iam_role.role.name
- # policy_arn = aws_iam_policy.policy_presigned_url.arn
- #}
- #
- #resource "aws_iam_policy" "policy_presigned_url" {
- # count = var.allow_presigned ? 1 : 0
- #
- # name_prefix = var.name
- # path = "/service/"
- # description = "Policy to allow signing of URLs for the ${local.fullname} bucket"
- # policy = data.aws_iam_policy_document.policy_doc_presigned_url.json
- #}
- #
- #data "aws_iam_policy_document" "policy_doc_presigned_url" {
- # count = var.allow_presigned ? 1 : 0
- #
- # statement {
- # sid = "TODO"
- # effect = "Allow"
- # actions = [
- # "s3:ListAllMyBuckets",
- # ]
- # resources = [ "*" ]
- # }
- #}
- resource "aws_iam_role_policy_attachment" "policy_attach" {
- role = aws_iam_role.role.name
- policy_arn = aws_iam_policy.policy.arn
- }
- resource "aws_iam_policy" "policy" {
- name_prefix = var.name
- path = "/service/"
- description = "Policy to allow use of the ${local.fullname} bucket"
- policy = data.aws_iam_policy_document.policy_doc.json
- }
- data "aws_iam_policy_document" "policy_doc" {
- statement {
- sid = "GeneralBucketAccess"
- effect = "Allow"
- actions = [
- "s3:ListAllMyBuckets",
- ]
- resources = ["*"]
- }
- statement {
- sid = "S3BucketAccess"
- effect = "Allow"
- actions = [
- "s3:GetLifecycleConfiguration",
- "s3:DeleteObjectVersion",
- "s3:ListBucketVersions",
- "s3:GetBucketLogging",
- "s3:RestoreObject",
- "s3:ListBucket",
- "s3:GetBucketVersioning",
- "s3:PutObject",
- "s3:GetObject",
- "s3:PutLifecycleConfiguration",
- "s3:GetBucketCORS",
- "s3:DeleteObject",
- "s3:GetBucketLocation",
- "s3:GetObjectVersion",
- ]
- # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
- resources = [
- aws_s3_bucket.bucket.arn,
- "${aws_s3_bucket.bucket.arn}/*",
- ]
- }
- statement {
- sid = "S3ReadOnlyBucketAccess"
- effect = "Allow"
- actions = [
- "s3:ListBucketVersions",
- "s3:ListBucket",
- "s3:GetBucketVersioning",
- "s3:GetObject",
- "s3:GetBucketCORS",
- "s3:GetBucketLocation",
- "s3:GetObjectVersion",
- ]
- # tfsec:ignore:aws-iam-no-policy-wildcards Allows use by the entire account
- resources = [
- aws_s3_bucket.bucket.arn,
- "${aws_s3_bucket.bucket.arn}/*",
- ]
- }
- statement {
- sid = "KMSKeyAccess"
- effect = "Allow"
- actions = [
- "kms:Decrypt",
- "kms:GenerateDataKeyWithoutPlaintext",
- "kms:Verify",
- "kms:GenerateDataKeyPairWithoutPlaintext",
- "kms:GenerateDataKeyPair",
- "kms:ReEncryptFrom",
- "kms:Encrypt",
- "kms:GenerateDataKey",
- "kms:ReEncryptTo",
- "kms:Sign",
- ]
- resources = [aws_kms_key.bucketkey.arn]
- }
- }
|