Skip to content

Commit bbd0a00

Browse files
authored
feat: Add ability to create deny insecure transport policy (terraform-aws-modules#77)
1 parent 560622e commit bbd0a00

File tree

5 files changed

+83
-26
lines changed

5 files changed

+83
-26
lines changed

.github/workflows/pre-commit.yml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,30 @@ on:
77
- master
88

99
jobs:
10-
# Min Terraform version(s)
10+
# Min Terraform version(s)
1111
getDirectories:
12-
name: Get root directories
13-
runs-on: ubuntu-latest
14-
steps:
15-
- name: Checkout
16-
uses: actions/checkout@v2
17-
- name: Install Python
18-
uses: actions/setup-python@v2
19-
- name: Build matrix
20-
id: matrix
21-
run: |
22-
DIRS=$(python -c "import json; import glob; print(json.dumps([x.replace('/versions.tf', '') for x in glob.glob('./**/versions.tf', recursive=True)]))")
23-
echo "::set-output name=directories::$DIRS"
24-
outputs:
25-
directories: ${{ steps.matrix.outputs.directories }}
12+
name: Get root directories
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v2
17+
- name: Install Python
18+
uses: actions/setup-python@v2
19+
- name: Build matrix
20+
id: matrix
21+
run: |
22+
DIRS=$(python -c "import json; import glob; print(json.dumps([x.replace('/versions.tf', '') for x in glob.glob('./**/versions.tf', recursive=True)]))")
23+
echo "::set-output name=directories::$DIRS"
24+
outputs:
25+
directories: ${{ steps.matrix.outputs.directories }}
2626

2727
preCommitMinVersions:
2828
name: Min TF validate
2929
needs: getDirectories
3030
runs-on: ubuntu-latest
3131
strategy:
32-
matrix:
33-
directory: ${{ fromJson(needs.getDirectories.outputs.directories) }}
32+
matrix:
33+
directory: ${{ fromJson(needs.getDirectories.outputs.directories) }}
3434
steps:
3535
- name: Checkout
3636
uses: actions/checkout@v2
@@ -59,7 +59,7 @@ jobs:
5959
pre-commit run terraform_validate --color=always --show-diff-on-failure --files $(ls *.tf)
6060

6161

62-
# Max Terraform version
62+
# Max Terraform version
6363
getBaseVersion:
6464
name: Module max TF version
6565
runs-on: ubuntu-latest
@@ -94,7 +94,7 @@ jobs:
9494
- name: Install pre-commit dependencies
9595
run: |
9696
pip install pre-commit
97-
curl -L "$(curl -s https://api.github.com/repos/terraform-docs/terraform-docs/releases/latest | grep -o -E "https://.+?-v0.12.0-linux-amd64" | head -n1)" > terraform-docs && chmod +x terraform-docs && sudo mv terraform-docs /usr/bin/
97+
curl -L "$(curl -s https://api.github.com/repos/terraform-docs/terraform-docs/releases/latest | grep -o -E "https://.+?-v0.12\..+?-linux-amd64" | head -n1)" > terraform-docs && chmod +x terraform-docs && sudo mv terraform-docs /usr/bin/
9898
curl -L "$(curl -s https://api.github.com/repos/terraform-linters/tflint/releases/latest | grep -o -E "https://.+?_linux_amd64.zip")" > tflint.zip && unzip tflint.zip && rm tflint.zip && sudo mv tflint /usr/bin/
9999
- name: Execute pre-commit
100100
# Run all pre-commit checks on max version supported

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ No modules.
103103
| [aws_s3_bucket_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_policy) | resource |
104104
| [aws_s3_bucket_public_access_block.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_public_access_block) | resource |
105105
| [aws_elb_service_account.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/elb_service_account) | data source |
106+
| [aws_iam_policy_document.combined](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
107+
| [aws_iam_policy_document.deny_insecure_transport](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
106108
| [aws_iam_policy_document.elb_log_delivery](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
107109

108110
## Inputs
@@ -111,6 +113,7 @@ No modules.
111113
|------|-------------|------|---------|:--------:|
112114
| <a name="input_acceleration_status"></a> [acceleration\_status](#input\_acceleration\_status) | (Optional) Sets the accelerate configuration of an existing bucket. Can be Enabled or Suspended. | `string` | `null` | no |
113115
| <a name="input_acl"></a> [acl](#input\_acl) | (Optional) The canned ACL to apply. Defaults to 'private'. Conflicts with `grant` | `string` | `"private"` | no |
116+
| <a name="input_attach_deny_insecure_transport_policy"></a> [attach\_deny\_insecure\_transport\_policy](#input\_attach\_deny\_insecure\_transport\_policy) | Controls if S3 bucket should have deny non-SSL transport policy attached | `bool` | `false` | no |
114117
| <a name="input_attach_elb_log_delivery_policy"></a> [attach\_elb\_log\_delivery\_policy](#input\_attach\_elb\_log\_delivery\_policy) | Controls if S3 bucket should have ELB log delivery policy attached | `bool` | `false` | no |
115118
| <a name="input_attach_policy"></a> [attach\_policy](#input\_attach\_policy) | Controls if S3 bucket should have bucket policy attached (set to `true` to use value of `policy` as bucket policy) | `bool` | `false` | no |
116119
| <a name="input_attach_public_policy"></a> [attach\_public\_policy](#input\_attach\_public\_policy) | Controls if a user defined public bucket policy will be attached (set to `false` to allow upstream to apply defaults to the bucket) | `bool` | `true` | no |

examples/complete/main.tf

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ data "aws_iam_policy_document" "bucket_policy" {
5151
module "log_bucket" {
5252
source = "../../"
5353

54-
bucket = "logs-${random_pet.this.id}"
55-
acl = "log-delivery-write"
56-
force_destroy = true
57-
attach_elb_log_delivery_policy = true
54+
bucket = "logs-${random_pet.this.id}"
55+
acl = "log-delivery-write"
56+
force_destroy = true
57+
attach_elb_log_delivery_policy = true
58+
attach_deny_insecure_transport_policy = true
5859
}
5960

6061
module "cloudfront_log_bucket" {
@@ -86,6 +87,8 @@ module "s3_bucket" {
8687
attach_policy = true
8788
policy = data.aws_iam_policy_document.bucket_policy.json
8889

90+
attach_deny_insecure_transport_policy = true
91+
8992
tags = {
9093
Owner = "Anton"
9194
}

main.tf

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
locals {
2+
attach_policy = var.attach_elb_log_delivery_policy || var.attach_deny_insecure_transport_policy || var.attach_policy
3+
}
4+
15
resource "aws_s3_bucket" "this" {
26
count = var.create_bucket ? 1 : 0
37

@@ -231,10 +235,20 @@ resource "aws_s3_bucket" "this" {
231235
}
232236

233237
resource "aws_s3_bucket_policy" "this" {
234-
count = var.create_bucket && (var.attach_elb_log_delivery_policy || var.attach_policy) ? 1 : 0
238+
count = var.create_bucket && local.attach_policy ? 1 : 0
235239

236240
bucket = aws_s3_bucket.this[0].id
237-
policy = var.attach_elb_log_delivery_policy ? data.aws_iam_policy_document.elb_log_delivery[0].json : var.policy
241+
policy = data.aws_iam_policy_document.combined[0].json
242+
}
243+
244+
data "aws_iam_policy_document" "combined" {
245+
count = var.create_bucket && local.attach_policy ? 1 : 0
246+
247+
source_policy_documents = compact([
248+
var.attach_elb_log_delivery_policy ? data.aws_iam_policy_document.elb_log_delivery[0].json : "",
249+
var.attach_deny_insecure_transport_policy ? data.aws_iam_policy_document.deny_insecure_transport[0].json : "",
250+
var.attach_policy ? var.policy : ""
251+
])
238252
}
239253

240254
# AWS Load Balancer access log delivery policy
@@ -265,12 +279,43 @@ data "aws_iam_policy_document" "elb_log_delivery" {
265279
}
266280
}
267281

282+
data "aws_iam_policy_document" "deny_insecure_transport" {
283+
count = var.create_bucket && var.attach_deny_insecure_transport_policy ? 1 : 0
284+
285+
statement {
286+
sid = "denyInsecureTransport"
287+
effect = "Deny"
288+
289+
actions = [
290+
"s3:*",
291+
]
292+
293+
resources = [
294+
aws_s3_bucket.this[0].arn,
295+
"${aws_s3_bucket.this[0].arn}/*",
296+
]
297+
298+
principals {
299+
type = "*"
300+
identifiers = ["*"]
301+
}
302+
303+
condition {
304+
test = "Bool"
305+
variable = "aws:SecureTransport"
306+
values = [
307+
"false"
308+
]
309+
}
310+
}
311+
}
312+
268313
resource "aws_s3_bucket_public_access_block" "this" {
269314
count = var.create_bucket && var.attach_public_policy ? 1 : 0
270315

271316
# Chain resources (s3_bucket -> s3_bucket_policy -> s3_bucket_public_access_block)
272317
# to prevent "A conflicting conditional operation is currently in progress against this resource."
273-
bucket = (var.attach_elb_log_delivery_policy || var.attach_policy) ? aws_s3_bucket_policy.this[0].id : aws_s3_bucket.this[0].id
318+
bucket = local.attach_policy ? aws_s3_bucket_policy.this[0].id : aws_s3_bucket.this[0].id
274319

275320
block_public_acls = var.block_public_acls
276321
block_public_policy = var.block_public_policy

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ variable "attach_elb_log_delivery_policy" {
1010
default = false
1111
}
1212

13+
variable "attach_deny_insecure_transport_policy" {
14+
description = "Controls if S3 bucket should have deny non-SSL transport policy attached"
15+
type = bool
16+
default = false
17+
}
18+
1319
variable "attach_policy" {
1420
description = "Controls if S3 bucket should have bucket policy attached (set to `true` to use value of `policy` as bucket policy)"
1521
type = bool

0 commit comments

Comments
 (0)