Skip to content

Commit

Permalink
Add an optional list of IP CIDRs which can access the S3 website.
Browse files Browse the repository at this point in the history
  • Loading branch information
razorsedge committed Jul 27, 2021
1 parent 2c731bd commit 5b6b181
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
28 changes: 28 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,34 @@ data "aws_iam_policy_document" "default" {
}
}

dynamic "statement" {
for_each = flatten(var.trusted_ips) != [] ? [1] : []

# https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_examples_aws_deny-ip.html
content {
sid = "AllowTrustedIPsOnly"
effect = "Deny"
actions = ["s3:*"]
resources = [local.bucket_arn, "${local.bucket_arn}/*"]

principals {
identifiers = ["*"]
type = "*"
}

condition {
test = "NotIpAddress"
values = var.trusted_ips
variable = "aws:SourceIp"
}
condition {
test = "Bool"
values = ["false"]
variable = "aws:ViaAWSService"
}
}
}

# Support replication ARNs
dynamic "statement" {
for_each = flatten(data.aws_iam_policy_document.replication.*.statement)
Expand Down
8 changes: 7 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,10 @@ variable "allow_ssl_requests_only" {
type = bool
default = false
description = "Set to `true` to require requests to use Secure Socket Layer (HTTPS/SSL). This will explicitly deny access to HTTP requests"
}
}

variable "trusted_ips" {
type = list(string)
default = []
description = "(Optional) List of IP CIDRs which can access the S3 website"
}

0 comments on commit 5b6b181

Please sign in to comment.