Skip to content

Commit 0d0a773

Browse files
Maxim Mironenkogoruha
authored andcommitted
policy to allow only encrypted uploads (#5)
* added policy to allow only encrypted uploads
1 parent 6a04266 commit 0d0a773

File tree

5 files changed

+55
-7
lines changed

5 files changed

+55
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Available targets:
8080
| Name | Description | Type | Default | Required |
8181
|------|-------------|:----:|:-----:|:-----:|
8282
| acl | The canned ACL to apply. We recommend `private` to avoid exposing sensitive information | string | `private` | no |
83+
| allow_encrypted_uploads_only | Set to `true` to prevent uploads of unencrypted objects to S3 bucket | string | `false` | no |
8384
| allowed_bucket_actions | List of actions the user is permitted to perform on the S3 bucket | list | `<list>` | no |
8485
| attributes | Additional attributes (e.g. `1`) | list | `<list>` | no |
8586
| delimiter | Delimiter to be used between `namespace`, `stage`, `name` and `attributes` | string | `-` | no |
@@ -105,7 +106,6 @@ Available targets:
105106
| bucket_domain_name | FQDN of bucket |
106107
| bucket_id | Bucket Name (aka ID) |
107108
| enabled | Is module enabled |
108-
| s3_bucket_arn | S3 bucket ARN |
109109
| secret_access_key | The secret access key. This will be written to the state file in plain-text |
110110
| user_arn | The ARN assigned by AWS for the user |
111111
| user_enabled | Is user creation enabled |

docs/terraform.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
| Name | Description | Type | Default | Required |
44
|------|-------------|:----:|:-----:|:-----:|
55
| acl | The canned ACL to apply. We recommend `private` to avoid exposing sensitive information | string | `private` | no |
6+
| allow_encrypted_uploads_only | Set to `true` to prevent uploads of unencrypted objects to S3 bucket | string | `false` | no |
67
| allowed_bucket_actions | List of actions the user is permitted to perform on the S3 bucket | list | `<list>` | no |
78
| attributes | Additional attributes (e.g. `1`) | list | `<list>` | no |
89
| delimiter | Delimiter to be used between `namespace`, `stage`, `name` and `attributes` | string | `-` | no |
@@ -28,7 +29,6 @@
2829
| bucket_domain_name | FQDN of bucket |
2930
| bucket_id | Bucket Name (aka ID) |
3031
| enabled | Is module enabled |
31-
| s3_bucket_arn | S3 bucket ARN |
3232
| secret_access_key | The secret access key. This will be written to the state file in plain-text |
3333
| user_arn | The ARN assigned by AWS for the user |
3434
| user_enabled | Is user creation enabled |

main.tf

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,50 @@ module "s3_user" {
4646
s3_actions = ["${var.allowed_bucket_actions}"]
4747
s3_resources = ["${join("", aws_s3_bucket.default.*.arn)}/*", "${join("", aws_s3_bucket.default.*.arn)}"]
4848
}
49+
50+
data "aws_iam_policy_document" "bucket_policy" {
51+
count = "${var.enabled == "true" && var.allow_encrypted_uploads_only == "true" ? 1 : 0}"
52+
53+
statement {
54+
sid = "DenyIncorrectEncryptionHeader"
55+
effect = "Deny"
56+
actions = ["s3:PutObject"]
57+
resources = ["arn:aws:s3:::${aws_s3_bucket.default.id}/*"]
58+
59+
principals {
60+
identifiers = ["*"]
61+
type = "*"
62+
}
63+
64+
condition {
65+
test = "StringNotEquals"
66+
values = ["${var.sse_algorithm}"]
67+
variable = "s3:x-amz-server-side-encryption"
68+
}
69+
}
70+
71+
statement {
72+
sid = "DenyUnEncryptedObjectUploads"
73+
effect = "Deny"
74+
actions = ["s3:PutObject"]
75+
resources = ["arn:aws:s3:::${aws_s3_bucket.default.id}/*"]
76+
77+
principals {
78+
identifiers = ["*"]
79+
type = "*"
80+
}
81+
82+
condition {
83+
test = "Null"
84+
values = ["true"]
85+
variable = "s3:x-amz-server-side-encryption"
86+
}
87+
}
88+
}
89+
90+
resource "aws_s3_bucket_policy" "default" {
91+
count = "${var.enabled == "true" && var.allow_encrypted_uploads_only == "true" ? 1 : 0}"
92+
bucket = "${join("", aws_s3_bucket.default.*.id)}"
93+
94+
policy = "${join("", data.aws_iam_policy_document.bucket_policy.*.json)}"
95+
}

outputs.tf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,3 @@ output "secret_access_key" {
4949
value = "${module.s3_user.secret_access_key}"
5050
description = "The secret access key. This will be written to the state file in plain-text"
5151
}
52-
53-
output "s3_bucket_arn" {
54-
value = "${join("", aws_s3_bucket.default.*.arn)}"
55-
description = "S3 bucket ARN"
56-
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,9 @@ variable "allowed_bucket_actions" {
9090
default = ["s3:PutObject", "s3:PutObjectAcl", "s3:GetObject", "s3:DeleteObject", "s3:ListBucket", "s3:ListBucketMultipartUploads", "s3:GetBucketLocation", "s3:AbortMultipartUpload"]
9191
description = "List of actions the user is permitted to perform on the S3 bucket"
9292
}
93+
94+
variable "allow_encrypted_uploads_only" {
95+
type = "string"
96+
default = "false"
97+
description = "Set to `true` to prevent uploads of unencrypted objects to S3 bucket"
98+
}

0 commit comments

Comments
 (0)