|
| 1 | +/* |
| 2 | +* # GuardDuty Malware Protection for S3 |
| 3 | +* |
| 4 | +* Sets up a GuardDuty Malware Protection plan for an S3 bucket. This will asynchronously scan new objects in the specified S3 bucket for malware and tag the objects with a `GuardDutyMalwareScanStatus` scan verdict. |
| 5 | +* |
| 6 | +* It is expected that GuardDuty is already enabled in the region where this module is deployed. |
| 7 | +* |
| 8 | +* :warning: You are charged based on the number and size of the objects scanned. |
| 9 | +*/ |
| 10 | + |
| 11 | +resource "aws_guardduty_malware_protection_plan" "this" { |
| 12 | + role = aws_iam_role.this.arn |
| 13 | + |
| 14 | + protected_resource { |
| 15 | + s3_bucket { |
| 16 | + bucket_name = var.s3_bucket_name |
| 17 | + object_prefixes = length(var.s3_object_prefixes) > 0 ? var.s3_object_prefixes : null |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + actions { |
| 22 | + tagging { |
| 23 | + status = var.tagging_status |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + tags = local.common_tags |
| 28 | +} |
| 29 | + |
| 30 | +resource "aws_iam_role" "this" { |
| 31 | + name = "MalwareS3-${var.s3_bucket_name}" |
| 32 | + assume_role_policy = data.aws_iam_policy_document.this_assume.json |
| 33 | + |
| 34 | + tags = local.common_tags |
| 35 | +} |
| 36 | + |
| 37 | +data "aws_iam_policy_document" "this_assume" { |
| 38 | + statement { |
| 39 | + actions = ["sts:AssumeRole"] |
| 40 | + effect = "Allow" |
| 41 | + |
| 42 | + principals { |
| 43 | + type = "Service" |
| 44 | + identifiers = ["malware-protection-plan.guardduty.amazonaws.com"] |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +resource "aws_iam_role_policy_attachment" "this" { |
| 50 | + role = aws_iam_role.this.name |
| 51 | + policy_arn = aws_iam_policy.this.arn |
| 52 | +} |
| 53 | + |
| 54 | +resource "aws_iam_policy" "this" { |
| 55 | + name = "MalwareS3-${var.s3_bucket_name}" |
| 56 | + path = "/" |
| 57 | + policy = data.aws_iam_policy_document.this.json |
| 58 | + |
| 59 | + tags = local.common_tags |
| 60 | +} |
| 61 | + |
| 62 | +data "aws_iam_policy_document" "this" { |
| 63 | + |
| 64 | + statement { |
| 65 | + effect = "Allow" |
| 66 | + |
| 67 | + actions = [ |
| 68 | + "s3:ListBucket", |
| 69 | + "s3:GetObject", |
| 70 | + "s3:PutObject", |
| 71 | + "s3:GetObjectTagging", |
| 72 | + "s3:GetObjectVersion", |
| 73 | + "s3:GetObjectVersionTagging", |
| 74 | + "s3:PutObjectTagging", |
| 75 | + "s3:PutObjectVersionTagging", |
| 76 | + "s3:PutBucketNotification", |
| 77 | + "s3:GetBucketNotification", |
| 78 | + |
| 79 | + ] |
| 80 | + |
| 81 | + resources = [ |
| 82 | + local.s3_bucket_arn, |
| 83 | + "${local.s3_bucket_arn}/*" |
| 84 | + ] |
| 85 | + } |
| 86 | + |
| 87 | + statement { |
| 88 | + effect = "Allow" |
| 89 | + actions = [ |
| 90 | + "events:PutRule", |
| 91 | + "events:DeleteRule", |
| 92 | + "events:PutTargets", |
| 93 | + "events:RemoveTargets" |
| 94 | + ] |
| 95 | + resources = [ |
| 96 | + "arn:aws:events:${local.region}:${local.account_id}:rule/DO-NOT-DELETE-AmazonGuardDutyMalwareProtectionS3*" |
| 97 | + ] |
| 98 | + |
| 99 | + condition { |
| 100 | + test = "StringLike" |
| 101 | + variable = "events:ManagedBy" |
| 102 | + values = ["malware-protection-plan.guardduty.amazonaws.com"] |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + statement { |
| 107 | + effect = "Allow" |
| 108 | + actions = [ |
| 109 | + "events:DescribeRule", |
| 110 | + "events:ListTargetsByRule" |
| 111 | + ] |
| 112 | + resources = [ |
| 113 | + "arn:aws:events:${local.region}:${local.account_id}:rule/DO-NOT-DELETE-AmazonGuardDutyMalwareProtectionS3*" |
| 114 | + ] |
| 115 | + } |
| 116 | +} |
0 commit comments