This repository has been archived by the owner on Mar 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
audit-logs.tf
136 lines (123 loc) · 4 KB
/
audit-logs.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Temporary old-school setup for audit logs. We can remove
* all of this once we do structured logging properly.
* For now the architecture is that auth writes to aws_kinesis_stream
* directly and then firehose pulls logs out of there and puts
* them into s3.
*/
// The following 2 cloudwatch resources are for error reporting
// from the firehose directly
resource "aws_cloudwatch_log_group" "taskcluster_audit_logs" {
name = "/aws/kinesisfirehose/taskcluster-audit-logs"
}
resource "aws_cloudwatch_log_stream" "taskcluster_audit_logs" {
name = "S3Delivery"
log_group_name = "${aws_cloudwatch_log_group.taskcluster_audit_logs.name}"
}
// This is the bucket where our audit logs end up
resource "aws_s3_bucket" "taskcluster_audit_logs" {
bucket = "taskcluster-audit-logs"
}
// The auth service writes to this kinesis stream directly
// Our firehose pulls logs out of here and stuffs them in s3.
// In addition, other teams at mozilla can pull logs off
// and stuff them into whatever services they have to use
// audit logs.
resource "aws_kinesis_stream" "taskcluster_audit_logs" {
name = "taskcluster-audit-logs"
shard_count = 1
retention_period = 48
}
// This is required to allow firehose to read our kinesis streams
resource "aws_iam_role" "taskcluster_audit_logs_firehose" {
name = "taskcluster-audit-log-firehose-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "firehose.amazonaws.com"
},
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": {
"sts:ExternalId": "${var.taskcluster_aws_account_id}"
}
}
} ]
}
EOF
}
// This policy allows firehose to log errors in addition to read
// from kinesis and put them into s3
resource "aws_iam_role_policy" "taskcluster_audit_logs_firehose" {
name = "taskcluster-audit-logs-firehose"
role = "${aws_iam_role.taskcluster_audit_logs_firehose.name}"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement":
[
{
"Effect": "Allow",
"Action": [
"s3:AbortMultipartUpload",
"s3:GetBucketLocation",
"s3:GetObject",
"s3:ListBucket",
"s3:ListBucketMultipartUploads",
"s3:PutObject"
],
"Resource": [
"${aws_s3_bucket.taskcluster_audit_logs.arn}",
"${aws_s3_bucket.taskcluster_audit_logs.arn}/*"
]
},
{
"Effect": "Allow",
"Action": [
"kinesis:DescribeStream",
"kinesis:GetShardIterator",
"kinesis:GetRecords"
],
"Resource": "${aws_kinesis_stream.taskcluster_audit_logs.arn}"
},
{
"Effect": "Allow",
"Action": [
"logs:PutLogEvents"
],
"Resource": [
"${aws_cloudwatch_log_stream.taskcluster_audit_logs.arn}"
]
}
]
}
EOF
}
// The firehose itself. This just reads logs out of kinesis
// and puts them in s3
resource "aws_kinesis_firehose_delivery_stream" "taskcluster_audit_logs_firehose" {
name = "taskcluster-audit-logs"
destination = "extended_s3"
kinesis_source_configuration {
kinesis_stream_arn = "${aws_kinesis_stream.taskcluster_audit_logs.arn}"
role_arn = "${aws_iam_role.taskcluster_audit_logs_firehose.arn}"
}
extended_s3_configuration {
role_arn = "${aws_iam_role.taskcluster_audit_logs_firehose.arn}"
bucket_arn = "${aws_s3_bucket.taskcluster_audit_logs.arn}"
prefix = "auth-audit-logs"
cloudwatch_logging_options = {
enabled = true
log_group_name = "${aws_cloudwatch_log_group.taskcluster_audit_logs.name}"
log_stream_name = "${aws_cloudwatch_log_stream.taskcluster_audit_logs.name}"
}
}
}
output "audit_logs_stream_arn" {
value = "${aws_kinesis_stream.taskcluster_audit_logs.arn}"
}