eleven / ocho Public

Clone
git clone https://amadan.net/eleven/ocho

Plain git — no account needed to clone.

Download

Download this file

1# đŸ¤– Account hardening: an all-region CloudTrail with log-file validation,
2# writing to its own locked-down bucket (90-day retention — enough to
3# investigate, not an unbounded cost). Management events only, which keeps
4# the trail in the free tier.
5
6resource "aws_s3_bucket" "trail_logs" {
7 bucket = "eleven-cloudtrail-${local.account_id}"
8}
9
10resource "aws_s3_bucket_public_access_block" "trail_logs" {
11 bucket = aws_s3_bucket.trail_logs.id
12
13 block_public_acls = true
14 block_public_policy = true
15 ignore_public_acls = true
16 restrict_public_buckets = true
17}
18
19resource "aws_s3_bucket_lifecycle_configuration" "trail_logs" {
20 bucket = aws_s3_bucket.trail_logs.id
21
22 rule {
23 id = "expire-90d"
24 status = "Enabled"
25
26 filter {}
27
28 expiration {
29 days = 90
30 }
31 }
32}
33
34data "aws_iam_policy_document" "trail_logs" {
35 statement {
36 sid = "CloudTrailAclCheck"
37 actions = ["s3:GetBucketAcl"]
38 resources = [aws_s3_bucket.trail_logs.arn]
39
40 principals {
41 type = "Service"
42 identifiers = ["cloudtrail.amazonaws.com"]
43 }
44 }
45
46 statement {
47 sid = "CloudTrailWrite"
48 actions = ["s3:PutObject"]
49 resources = ["${aws_s3_bucket.trail_logs.arn}/AWSLogs/${local.account_id}/*"]
50
51 principals {
52 type = "Service"
53 identifiers = ["cloudtrail.amazonaws.com"]
54 }
55
56 condition {
57 test = "StringEquals"
58 variable = "s3:x-amz-acl"
59 values = ["bucket-owner-full-control"]
60 }
61 }
62
63 # TLS-only: deny any request arriving over plain HTTP (CloudTrail's own
64 # delivery is HTTPS; this just makes it policy).
65 statement {
66 sid = "DenyInsecureTransport"
67 effect = "Deny"
68 actions = ["s3:*"]
69 resources = [
70 aws_s3_bucket.trail_logs.arn,
71 "${aws_s3_bucket.trail_logs.arn}/*",
72 ]
73
74 principals {
75 type = "*"
76 identifiers = ["*"]
77 }
78
79 condition {
80 test = "Bool"
81 variable = "aws:SecureTransport"
82 values = ["false"]
83 }
84 }
85}
86
87resource "aws_s3_bucket_policy" "trail_logs" {
88 bucket = aws_s3_bucket.trail_logs.id
89 policy = data.aws_iam_policy_document.trail_logs.json
90}
91
92resource "aws_cloudtrail" "main" {
93 name = "eleven-account-trail"
94 s3_bucket_name = aws_s3_bucket.trail_logs.id
95 is_multi_region_trail = true
96 include_global_service_events = true
97 enable_log_file_validation = true
98
99 depends_on = [aws_s3_bucket_policy.trail_logs]
100}
101