-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlambda.pp
243 lines (234 loc) · 8.98 KB
/
lambda.pp
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
query "lambda_function_concurrent_execution_limit_configured" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'reserved_concurrent_executions') is null then 'alarm'
when (attributes_std -> 'reserved_concurrent_executions')::integer = -1 then 'alarm'
else 'ok'
end as status,
split_part(address, '.', 2) || case
when (attributes_std -> 'reserved_concurrent_executions') is null then ' function-level concurrent execution limit not configured'
when (attributes_std -> 'reserved_concurrent_executions')::integer = -1 then ' function-level concurrent execution limit not configured'
else ' function-level concurrent execution limit configured'
end || '.' as reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'aws_lambda_function';
EOQ
}
query "lambda_function_dead_letter_queue_configured" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'dead_letter_config') is null then 'alarm'
else 'ok'
end as status,
split_part(address, '.', 2) || case
when (attributes_std -> 'dead_letter_config') is null then ' not configured with dead-letter queue'
else ' configured with dead-letter queue'
end || '.' as reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'aws_lambda_function';
EOQ
}
query "lambda_function_in_vpc" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'vpc_config') is null then 'alarm'
else 'ok'
end as status,
split_part(address, '.', 2) || case
when (attributes_std -> 'vpc_config') is null then ' is not in VPC'
else ' is in VPC'
end || '.' as reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'aws_lambda_function';
EOQ
}
query "lambda_function_use_latest_runtime" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std ->> 'runtime') is null then 'skip'
when (attributes_std ->> 'runtime') in ('nodejs14.x', 'nodejs12.x', 'nodejs10.x', 'python3.8', 'python3.7', 'python3.6', 'ruby2.5', 'ruby2.7', 'java11', 'java8', 'go1.x', 'dotnetcore2.1', 'dotnetcore3.1') then 'ok'
else 'alarm'
end as status,
split_part(address, '.', 2) || case
when (attributes_std ->> 'runtime') is null then ' runtime not set'
when (attributes_std ->> 'runtime') in ('nodejs14.x', 'nodejs12.x', 'nodejs10.x', 'python3.8', 'python3.7', 'python3.6', 'ruby2.5', 'ruby2.7', 'java11', 'java8', 'go1.x', 'dotnetcore2.1', 'dotnetcore3.1') then ' uses latest runtime - ' || (attributes_std ->> 'runtime') || '.'
else ' uses ' || (attributes_std ->> 'runtime')|| ' which is not the latest version.'
end as reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'aws_lambda_function';
EOQ
}
query "lambda_function_xray_tracing_enabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'tracing_config') is null then 'alarm'
when (attributes_std -> 'tracing_config' ->> 'mode') = 'Active' then 'ok'
else 'alarm'
end as status,
split_part(address, '.', 2) || case
when (attributes_std -> 'tracing_config') is null then ' has X-Ray tracing disabled'
when (attributes_std -> 'tracing_config' ->> 'mode') = 'Active' then ' has X-Ray tracing enabled'
else ' has X-Ray tracing disabled'
end || '.' as reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'aws_lambda_function';
EOQ
}
query "lambda_function_url_auth_type_configured" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std ->> 'authorization_type') = 'NONE' then 'alarm'
else 'ok'
end as status,
split_part(address, '.', 2) || case
when (attributes_std ->> 'authorization_type') = 'NONE' then ' URLs AuthType is not configured'
else ' URLs AuthType is configured'
end || '.' as reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'aws_lambda_function_url';
EOQ
}
query "lambda_function_code_signing_configured" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'code_signing_config_arn') is null then 'alarm'
else 'ok'
end as status,
split_part(address, '.', 2) || case
when (attributes_std -> 'code_signing_config_arn') is null then ' code signing not configured'
else ' code signing is configured'
end || '.' as reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'aws_lambda_function';
EOQ
}
query "lambda_function_variables_no_sensitive_data" {
sql = <<-EOQ
with function_vaiable_with_sensitive_data as (
select
distinct (address ) as name
from
terraform_resource
join jsonb_each_text(attributes_std -> 'environment' -> 'variables') d on true
where
type = 'aws_lambda_function'
and (
d.key ilike any (array['%pass%', '%secret%', '%token%', '%key%'])
or d.key ~ '(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]'
or d.value ilike any (array['%pass%', '%secret%', '%token%', '%key%'])
or d.value ~ '(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]'
)
)
select
r.address as resource,
case
when s.name is not null then 'alarm'
else 'ok'
end as status,
split_part(r.address, '.', 2) || case
when s.name is not null then ' has potential sensitive data'
else ' has no sensitive data'
end || '.' as reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource as r
left join function_vaiable_with_sensitive_data as s on s.name = r.address
where
r.type = 'aws_lambda_function';
EOQ
}
query "lambda_function_environment_encryption_enabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'environment') is not null and (attributes_std -> 'kms_key_arn') is not null then 'ok'
when (attributes_std -> 'environment') is not null and (attributes_std -> 'kms_key_arn') is null then 'alarm'
when (attributes_std -> 'environment') is null and (attributes_std -> 'kms_key_arn') is null then 'skip'
else 'alarm'
end as status,
split_part(address, '.', 2) || case
when (attributes_std -> 'environment') is not null and (attributes_std -> 'kms_key_arn') is not null then ' environment encryption enabled'
when (attributes_std -> 'environment') is not null and (attributes_std -> 'kms_key_arn') is null then ' environment encryption disabled'
when (attributes_std -> 'environment') is null and (attributes_std -> 'kms_key_arn') is null then ' no environment exists'
else ' encryption is enabled even though no environment exists'
end || '.' as reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'aws_lambda_function';
EOQ
}
query "lambda_permission_restricted_service_permission" {
sql = <<-EOQ
select
address as resource,
split_part((attributes_std ->> 'principal'), '.', 2),
case
when not (split_part((attributes_std ->> 'principal'), '.', 2) = 'amazonaws' and split_part((attributes_std ->> 'principal'), '.', 3)= 'com') then 'info'
when split_part((attributes_std ->> 'principal'), '.', 2) = 'amazonaws'
and split_part((attributes_std ->> 'principal'), '.', 3)= 'com'
and ((attributes_std -> 'source_arn') is not null
or (attributes_std -> 'source_account') is not null ) then 'ok'
else 'alarm'
end as status,
split_part(address, '.', 2) || case
when not (split_part((attributes_std ->> 'principal'), '.', 2) = 'amazonaws' and split_part((attributes_std ->> 'principal'), '.', 3) = 'com') then ' principal not set as service'
when split_part((attributes_std ->> 'principal'), '.', 2) = 'amazonaws'
and split_part((attributes_std ->> 'principal'), '.', 3)= 'com'
and ((attributes_std -> 'source_arn') is not null
or (attributes_std -> 'source_account') is not null ) then ' permissions to AWS services restricted by SourceArn or SourceAccount'
else ' permissions to AWS services not restricted by SourceArn or SourceAccount'
end || '.' as reason
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'aws_lambda_permission';
EOQ
}