-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.yml
170 lines (159 loc) · 4.77 KB
/
template.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
AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Rule based AWS resource config management
Parameters:
EnableAutoFix:
Description: 'Enable automated fix'
Type: String
Default: false
AllowedValues:
- true
- false
Resources:
ThemisLambda:
Type: AWS::Serverless::Function
Properties:
#FunctionName: themis
Handler: handler.handle
Runtime: python3.7
CodeUri: .
Description: "Themis enforces security guidelines for a aws account"
MemorySize: 128
Timeout: 180 # 3 minutes
Policies:
- ReadOnlyAccess
- Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "lambda:InvokeFunction"
Resource: '*'
- Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "config:*"
Resource: '*'
- Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "ses:SendEmail"
- "ses:SendRawEmail"
- "ec2:RevokeSecurityGroupIngress"
- "ec2:AuthorizeSecurityGroupIngress"
Resource: '*'
ConfigPermissionToCallLambda:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt ThemisLambda.Arn
Action: "lambda:InvokeFunction"
Principal: "config.amazonaws.com"
DependsOn: ThemisLambda
SsmPermissionToCallLambda:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt ThemisLambda.Arn
Action: "lambda:InvokeFunction"
Principal: "ssm.amazonaws.com"
DependsOn: ThemisLambda
SsmRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- ssm.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: /
Policies:
- PolicyName: ThemisSsmPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "lambda:InvokeAsync"
- "lambda:InvokeFunction"
Resource: !GetAtt ThemisLambda.Arn
DependsOn: ThemisLambda
######### START: bad_ingress ##########################
BadIngressRule:
Type: AWS::Config::ConfigRule
Properties:
#ConfigRuleName: BadIngressRule
InputParameters:
ThemisEvaluatorModule: bad_ingress
Scope:
#ComplianceResourceId: "sg-4bd1c8f2"
ComplianceResourceTypes:
- "AWS::EC2::SecurityGroup"
Source:
Owner: "CUSTOM_LAMBDA"
SourceDetails:
-
EventSource: "aws.config"
MessageType: "ConfigurationItemChangeNotification"
-
EventSource: "aws.config"
MessageType: "OversizedConfigurationItemChangeNotification"
SourceIdentifier: !GetAtt ThemisLambda.Arn
DependsOn: ThemisLambda
BadIngressDocument:
Type: AWS::SSM::Document
Properties:
Content:
schemaVersion: '0.3'
assumeRole: !GetAtt SsmRole.Arn
description: "calls themis lambda to fix bad_ingress"
parameters:
ResourceID:
type: String
description: id of resource to fix
#default: null
AutomationAssumeRole:
type: String
description: "required for deploying"
mainSteps:
- action: "aws:invokeLambdaFunction"
name: "invokeThemisLambda"
inputs:
InvocationType: RequestResponse
FunctionName: !Ref ThemisLambda
InputPayload:
ResourceID: "{{ResourceID}}"
ThemisFixerModule: bad_ingress
DocumentType: Automation
#Name: BadIngressDocument
TargetType: '/AWS::Lambda::Function'
DependsOn: BadIngressRule
BadIngressRemediation:
Type: "AWS::Config::RemediationConfiguration"
Properties:
Automatic: !Ref EnableAutoFix
ConfigRuleName: !Ref BadIngressRule
MaximumAutomaticAttempts: 1
RetryAttemptSeconds: 60
Parameters:
AutomationAssumeRole:
StaticValue:
Values:
- !GetAtt SsmRole.Arn
ResourceID:
ResourceValue:
Value: RESOURCE_ID
TargetId: !Ref BadIngressDocument
TargetType: "SSM_DOCUMENT"
#TargetVersion: "1"
DependsOn: BadIngressDocument
######### END: bad_ingress ##########################
Outputs:
themis:
Description: "themis lambda function arn"
Value: !GetAtt ThemisLambda.Arn
Export:
Name: themisArn