-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhenry-sam.yml
189 lines (177 loc) · 5.77 KB
/
henry-sam.yml
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Description: "Serverless Application Model template for Henry Open S3 Buckets Alert"
Parameters:
S3LogBucketName:
Type: "String"
Description: "Name for S3 bucket for cloudtrail logs - must be globally unique"
ConstraintDescription: "[a-z0-9][a-z0-9-]{1,61}[a-z0-9]"
AlertEmailAddress:
Type: "String"
Description: "Email address to which the permission alerts will be sent by SNS"
Resources:
# S3 bucket for Cloudtrail logs
logbucket:
Type: "AWS::S3::Bucket"
Properties:
BucketName: !Ref S3LogBucketName
DeletionPolicy: "Retain"
# S3 bucket policy for Cloudtrail logging
logbucketPolicy:
Type: "AWS::S3::BucketPolicy"
Properties:
Bucket: !Ref logbucket
PolicyDocument:
Statement:
-
Action: "s3:GetBucketAcl"
Effect: "Allow"
Resource: !Sub "arn:aws:s3:::${logbucket}"
Principal: "*"
-
Action: "s3:PutObject"
Effect: "Allow"
Resource: !Sub "arn:aws:s3:::${logbucket}/*"
Principal:
Service: "cloudtrail.amazonaws.com"
# Cloudtrail log
cloudtrailLog:
Type: "AWS::CloudTrail::Trail"
Properties:
IsLogging: true
TrailName: "cloudtrail-log-sam"
S3BucketName: !Ref logbucket
S3KeyPrefix: "ctlogs"
IncludeGlobalServiceEvents: false
CloudWatchLogsLogGroupArn: !GetAtt cloudwatchLogGroup.Arn
CloudWatchLogsRoleArn: !GetAtt loggingRole.Arn
DependsOn: logbucketPolicy
# Cloudwatch log group
cloudwatchLogGroup:
Type: "AWS::Logs::LogGroup"
Properties:
LogGroupName: "cloudwatch-log-group-sam"
# IAM role for Cloudwatch logging, and logging policy
loggingRole:
Type: "AWS::IAM::Role"
Properties:
RoleName: "cloudwatch-logging-role-sam"
AssumeRolePolicyDocument:
Statement:
- Action: "sts:AssumeRole"
Effect: "Allow"
Principal:
Service: "cloudtrail.amazonaws.com"
Policies:
- PolicyName: "createLogStreamCWPolicy"
PolicyDocument:
Statement:
- Action: "logs:CreateLogStream"
Effect: "Allow"
Resource: !GetAtt cloudwatchLogGroup.Arn
- PolicyName: "putLogEventsCWPolicy"
PolicyDocument:
Statement:
- Action: "logs:PutLogEvents"
Effect: "Allow"
Resource: !GetAtt cloudwatchLogGroup.Arn
# SNS topic for alerts
bucketAlertsSNSTopic:
Type: "AWS::SNS::Topic"
Properties:
TopicName: "s3-bucket-public-access-alert-sam"
Subscription:
- Endpoint: !Ref AlertEmailAddress
Protocol: "email"
# IAM role for lambda execution and policy for lambda execution & SNS publish
lambdaExecutionRole:
Type: "AWS::IAM::Role"
Properties:
RoleName: "bucket-alert-lambda-execution-role-sam"
AssumeRolePolicyDocument:
Statement:
- Action: "sts:AssumeRole"
Effect: "Allow"
Principal:
Service: "lambda.amazonaws.com"
Policies:
- PolicyName: "createLogPolicy"
PolicyDocument:
Statement:
-
Action: "logs:CreateLogStream"
Effect: "Allow"
Resource: "arn:aws:logs:*:*:*"
- PolicyName: "putLogEventsPolicy"
PolicyDocument:
Statement:
-
Action: "logs:PutLogEvents"
Effect: "Allow"
Resource: "arn:aws:logs:*:*:*"
- PolicyName: "createLogGroupPolicy"
PolicyDocument:
Statement:
-
Action: "logs:CreateLogGroup"
Effect: "Allow"
Resource: "arn:aws:logs:*:*:*"
- PolicyName: "publishToSnsPolicy"
PolicyDocument:
Statement:
-
Action: "sns:Publish"
Effect: "Allow"
Resource: !Ref bucketAlertsSNSTopic
# Lambda function definition with environment variable for SNS topic ARN
bucketAlertLambda:
Type: "AWS::Serverless::Function"
Properties:
Handler: "index.handler"
Runtime: "nodejs6.10"
Role: !GetAtt lambdaExecutionRole.Arn
Description: "parses cloudtrail logs from cloudwatch events and alerts sns on public s3 buckets"
# CodeUri can be passed a dir or file when cloudformation package is run to set up lambda
# otherwise it needs to be given an S3 URI to location your lambda deploy package is uploaded
CodeUri: "index.js"
MemorySize: 128
Timeout: 3
Environment:
Variables:
snsTopicArn: !Ref bucketAlertsSNSTopic
# Cloudwatch event rule matching PutBucketAcl and CreateBucket S3 events
cloudwatchEventRule:
Type: "AWS::Events::Rule"
Properties:
Name: "cloudwatch-lambda-putbucketacl-rule-sam"
EventPattern: {
"source": [
"aws.s3"
],
"detail-type": [
"AWS API Call via CloudTrail"
],
"detail": {
"eventSource": [
"s3.amazonaws.com"
],
"eventName": [
"PutBucketAcl",
"CreateBucket"
]
}
}
State: "ENABLED"
Targets:
-
Arn: !GetAtt bucketAlertLambda.Arn
Id: "cloudwatch-event-rule-target-sam"
DependsOn: bucketAlertLambda
# Lambda permissions to allow execution from Cloudwatch event rule
cloudwatchEventLambdaPermission:
Type: "AWS::Lambda::Permission"
Properties:
FunctionName: !Ref bucketAlertLambda
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"
SourceArn: !GetAtt cloudwatchEventRule.Arn