This repository has been archived by the owner on Nov 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
exec.go
414 lines (394 loc) · 14 KB
/
exec.go
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Package exec is the entry point for security automation Cloud Functions.
package exec
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import (
"context"
"encoding/json"
"log"
"os"
"cloud.google.com/go/pubsub"
"github.com/googlecloudplatform/security-response-automation/cloudfunctions/bigquery/closepublicdataset"
"github.com/googlecloudplatform/security-response-automation/cloudfunctions/cloud-sql/removepublic"
"github.com/googlecloudplatform/security-response-automation/cloudfunctions/cloud-sql/requiressl"
"github.com/googlecloudplatform/security-response-automation/cloudfunctions/cloud-sql/updatepassword"
"github.com/googlecloudplatform/security-response-automation/cloudfunctions/filter"
"github.com/googlecloudplatform/security-response-automation/cloudfunctions/gce/createsnapshot"
"github.com/googlecloudplatform/security-response-automation/cloudfunctions/gce/openfirewall"
"github.com/googlecloudplatform/security-response-automation/cloudfunctions/gce/removepublicip"
"github.com/googlecloudplatform/security-response-automation/cloudfunctions/gcs/closebucket"
"github.com/googlecloudplatform/security-response-automation/cloudfunctions/gcs/enablebucketonlypolicy"
"github.com/googlecloudplatform/security-response-automation/cloudfunctions/gke/disabledashboard"
"github.com/googlecloudplatform/security-response-automation/cloudfunctions/iam/enableauditlogs"
"github.com/googlecloudplatform/security-response-automation/cloudfunctions/iam/removenonorgmembers"
"github.com/googlecloudplatform/security-response-automation/cloudfunctions/iam/revoke"
"github.com/googlecloudplatform/security-response-automation/cloudfunctions/router"
"github.com/googlecloudplatform/security-response-automation/services"
)
var (
svcs *services.Global
projectID = os.Getenv("GCP_PROJECT")
)
func init() {
log.SetFlags(log.LstdFlags | log.Llongfile)
ctx := context.Background()
var err error
if projectID == "" {
log.Fatalf("GCP_PROJECT environment variable not set")
}
svcs, err = services.New(ctx)
if err != nil {
log.Fatalf("failed to initialize services: %q", err)
}
}
// Filter is the entry point for the Filter Cloud function.
// This function will receive all findings and filter them against
// any user-defined Rego policies before forwarding along to the
// Router function.
func Filter(ctx context.Context, m pubsub.Message) error {
ps, err := services.InitPubSub(ctx, projectID)
if err != nil {
return err
}
return filter.Execute(ctx, m, &filter.Services{
PubSub: ps,
Logger: svcs.Logger,
SecurityCommandCenter: svcs.SecurityCommandCenter,
})
}
// Router is the entry point for the router Cloud Function.
//
// This Cloud Function will receive all findings and route them to configured automation.
func Router(ctx context.Context, m pubsub.Message) error {
ps, err := services.InitPubSub(ctx, projectID)
if err != nil {
return err
}
conf, err := router.Config()
if err != nil {
return err
}
return router.Execute(ctx, &router.Values{
Finding: m.Data,
}, &router.Services{
PubSub: ps,
Configuration: conf,
Logger: svcs.Logger,
Resource: svcs.Resource,
SecurityCommandCenter: svcs.SecurityCommandCenter,
})
}
// IAMRevoke is the entry point for the IAM revoker Cloud Function.
//
// This function will attempt to revoke the external members added to the policy if they
// match the provided list of disallowed domains. Additionally this method will only remove
// members if the project they were added to is within the specified folders. This
// configuration allows you to take a remediation action only on specific members and folders.
// For example, you may have a folder "development" where users can experiment without strict
// policies. However in your "production" folder you may want to revoke any grants that ETD
// finds as long as they match the domains you specify.
//
// Permissions required
// - roles/resourcemanager.folderAdmin to revoke IAM grants.
// - roles/viewer to verify the affected project is within the enforced folder.
//
func IAMRevoke(ctx context.Context, m pubsub.Message) error {
var values revoke.Values
switch err := json.Unmarshal(m.Data, &values); err {
case nil:
return revoke.Execute(ctx, &values, &revoke.Services{
Resource: svcs.Resource,
Logger: svcs.Logger,
})
default:
return err
}
}
// SnapshotDisk is the entry point for the auto creation of GCE snapshots Cloud Function.
//
// Once a supported finding is received this Cloud Function will look for any existing disk snapshots
// for the affected instance. If there are recent snapshots then no action is taken. This is so we
// do not overwrite a recent snapshot. If we have not taken a snapshot recently, take a new snapshot
// for each disk within the instance.
//
// Permissions required
// - roles/compute.instanceAdmin.v1 to manage disk snapshots.
//
func SnapshotDisk(ctx context.Context, m pubsub.Message) error {
var values createsnapshot.Values
switch err := json.Unmarshal(m.Data, &values); err {
case nil:
output, err := createsnapshot.Execute(ctx, &values, &createsnapshot.Services{
Host: svcs.Host,
Logger: svcs.Logger,
})
if err != nil {
return err
}
for _, dest := range values.Output {
switch dest {
case "turbinia":
log.Println("turbinia output is enabled, sending each copied disk to turbinia")
turbiniaProjectID := values.Turbinia.ProjectID
turbiniaTopicName := values.Turbinia.Topic
turbiniaZone := values.Turbinia.Zone
diskNames := output.DiskNames
if err := services.SendTurbinia(ctx, turbiniaProjectID, turbiniaTopicName, turbiniaZone, diskNames); err != nil {
return err
}
svcs.Logger.Info("sent %d disks to turbinia", len(diskNames))
}
}
return nil
default:
return err
}
}
// CloseBucket will remove any public users from buckets found within the provided folders.
//
// Permissions required
// - roles/viewer to retrieve ancestry.
// - roles/storeage.admin to modify buckets.
//
func CloseBucket(ctx context.Context, m pubsub.Message) error {
var values closebucket.Values
switch err := json.Unmarshal(m.Data, &values); err {
case nil:
return closebucket.Execute(ctx, &values, &closebucket.Services{
Resource: svcs.Resource,
Logger: svcs.Logger,
})
default:
return err
}
}
// OpenFirewall will remediate an open firewall.
//
// Permissions required
// - roles/viewer to retrieve ancestry.
// - roles/compute.securityAdmin to modify firewall rules.
//
func OpenFirewall(ctx context.Context, m pubsub.Message) error {
var values openfirewall.Values
switch err := json.Unmarshal(m.Data, &values); err {
case nil:
err := openfirewall.Execute(ctx, &values, &openfirewall.Services{
Firewall: svcs.Firewall,
Resource: svcs.Resource,
Logger: svcs.Logger,
})
if err != nil {
return err
}
return nil
default:
return err
}
}
// RemoveNonOrganizationMembers removes all members that do not match the organization domain.
//
// This Cloud Function will respond to Security Health Analytics **NON_ORG_IAM_MEMBER** findings from **IAM Scanner**.
// All user member types (user:) that do not correspond to the organization will be removed from policy binding.
//
// Permissions required
// - roles/resourcemanager.organizationAdmin to get org info and policies and set policies.
//
func RemoveNonOrganizationMembers(ctx context.Context, m pubsub.Message) error {
var values removenonorgmembers.Values
switch err := json.Unmarshal(m.Data, &values); err {
case nil:
return removenonorgmembers.Execute(ctx, &values, &removenonorgmembers.Services{
Logger: svcs.Logger,
Resource: svcs.Resource,
})
default:
return err
}
}
// RemovePublicIP removes all the external IP addresses of a GCE instance.
//
// This Cloud Function will respond to Security Health Analytics **Public IP Address** findings
// from **Compute Instance Scanner**. All public IP addresses of the affected instance will be
// deleted when this function is activated.
//
// Permissions required
// - roles/compute.instanceAdmin.v1 to get instance data and delete access config.
//
func RemovePublicIP(ctx context.Context, m pubsub.Message) error {
var values removepublicip.Values
switch err := json.Unmarshal(m.Data, &values); err {
case nil:
return removepublicip.Execute(ctx, &values, &removepublicip.Services{
Host: svcs.Host,
Resource: svcs.Resource,
Logger: svcs.Logger,
})
default:
return err
}
}
// ClosePublicDataset removes public access of a BigQuery dataset.
//
// This Cloud Function will respond to Security Health Analytics **Public Dataset** findings
// from **Dataset Scanner**. All public access of the affected dataset will be
// removed when this function is activated.
//
// Permissions required
// - roles/bigquery.dataOwner to get and update dataset metadata.
//
func ClosePublicDataset(ctx context.Context, m pubsub.Message) error {
var values closepublicdataset.Values
switch err := json.Unmarshal(m.Data, &values); err {
case nil:
bigquery, err := services.InitBigQuery(ctx, values.ProjectID)
if err != nil {
return err
}
return closepublicdataset.Execute(ctx, &values, &closepublicdataset.Services{
BigQuery: bigquery,
Logger: svcs.Logger,
})
default:
return err
}
}
// EnableBucketOnlyPolicy Enable bucket only policy on a GCS bucket.
//
// This Cloud Function will respond to Security Health Analytics **BUCKET_POLICY_ONLY_DISABLED** findings
// from **STORAGE_SCANNER**. Bucket only IAM policy will be enforced on the bucket.
//
// Permissions required
// - roles/storage.admin to change the Bucket policy mode.
//
func EnableBucketOnlyPolicy(ctx context.Context, m pubsub.Message) error {
var values enablebucketonlypolicy.Values
switch err := json.Unmarshal(m.Data, &values); err {
case nil:
return enablebucketonlypolicy.Execute(ctx, &values, &enablebucketonlypolicy.Services{
Resource: svcs.Resource,
Logger: svcs.Logger,
})
default:
return err
}
}
// CloseCloudSQL removes public IP for a Cloud SQL instance.
//
// This Cloud Function will respond to Security Health Analytics **Public SQL Instance** findings
// from **SQL Scanner**. All public IP addresses of the affected instance will be
// deleted when this function is activated.
//
// Permissions required
// - roles/cloudsql.editor to get instance data and delete access config.
//
func CloseCloudSQL(ctx context.Context, m pubsub.Message) error {
var values removepublic.Values
switch err := json.Unmarshal(m.Data, &values); err {
case nil:
return removepublic.Execute(ctx, &values, &removepublic.Services{
CloudSQL: svcs.CloudSQL,
Resource: svcs.Resource,
Logger: svcs.Logger,
})
default:
return err
}
}
// CloudSQLRequireSSL enables the SSL requirement for a Cloud SQL instance.
//
// This Cloud Function will respond to Security Health Analytics **Public SQL Instance** findings
// from **SQL Scanner**. All public IP addresses of the affected instance will be
// deleted when this function is activated.
//
// Permissions required
// - roles/cloudsql.editor to get instance data and delete access config.
//
func CloudSQLRequireSSL(ctx context.Context, m pubsub.Message) error {
var values requiressl.Values
switch err := json.Unmarshal(m.Data, &values); err {
case nil:
return requiressl.Execute(ctx, &values, &requiressl.Services{
CloudSQL: svcs.CloudSQL,
Resource: svcs.Resource,
Logger: svcs.Logger,
})
default:
return err
}
}
// DisableDashboard will disable the Kubernetes dashboard addon.
//
// This Cloud Function will respond to Security Health Analytics **Web UI Enabled** findings
// from **Container Scanner**. The Kubernetes dashboard addon will be disabled when this
// function is activated.
//
// Permissions required
// - roles/container.clusterAdmin update cluster addon.
//
func DisableDashboard(ctx context.Context, m pubsub.Message) error {
var values disabledashboard.Values
switch err := json.Unmarshal(m.Data, &values); err {
case nil:
return disabledashboard.Execute(ctx, &values, &disabledashboard.Services{
Container: svcs.Container,
Resource: svcs.Resource,
Logger: svcs.Logger,
})
default:
return err
}
}
// EnableAuditLogs enables the Audit Logs to specific project
//
// This Cloud Function will respond to Security Health Analytics **AUDIT_LOGGING_DISABLED** findings
// from **LOGGING_SCANNER**.
//
// Permissions required
// - roles/resourcemanager.folderAdmin to get/update resource policy from projects in folder.
// - roles/editor to get/update resource policy to specific project.
//
func EnableAuditLogs(ctx context.Context, m pubsub.Message) error {
var values enableauditlogs.Values
switch err := json.Unmarshal(m.Data, &values); err {
case nil:
return enableauditlogs.Execute(ctx, &values, &enableauditlogs.Services{
Resource: svcs.Resource,
Logger: svcs.Logger,
})
default:
return err
}
}
// UpdatePassword updates the root password for a Cloud SQL instance.
//
// This Cloud Function will respond to Security Health Analytics **SQL No Root Password** findings
// from **SQL Scanner**. The root user of the affected instance will be updated with
// a new password when this function is activated.
//
// Permissions required
// - roles/cloudsql.admin to update a user password.
//
func UpdatePassword(ctx context.Context, m pubsub.Message) error {
var values updatepassword.Values
switch err := json.Unmarshal(m.Data, &values); err {
case nil:
return updatepassword.Execute(ctx, &values, &updatepassword.Services{
CloudSQL: svcs.CloudSQL,
Resource: svcs.Resource,
Logger: svcs.Logger,
})
default:
return err
}
}