-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
cloudtrailOps.go
456 lines (393 loc) · 10.6 KB
/
cloudtrailOps.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudtrail"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/sts"
)
// ConfigureBucket configures a bucket to send event activity to AWS CloudTrail
// Inputs:
//
// sess is the current session, which provides configuration for the SDK's service clients
// trailName is the name of the trail
// bucketName is the name of the bucket
//
// Output:
//
// If success, nil
// Otherwise, an error from the call to sts.GetCallerIdentity, json.Marshal, or s3.PutBucketPolicy
//
// snippet-start:[cloudtrail.go.configure_trail]
func ConfigureBucket(sess *session.Session, bucketName string) error {
stsSvc := sts.New(sess)
// snippet-start:[cloudtrail.go.configure_trail.policy]
input := &sts.GetCallerIdentityInput{}
result, err := stsSvc.GetCallerIdentity(input)
if err != nil {
return err
}
accountID := aws.StringValue(result.Account)
// Create a policy so the bucket sends events to AWS CloudTrail
s3Policy := map[string]interface{}{
"Version": "2012-10-17",
"Statement": []map[string]interface{}{
{
"Sid": "AWSCloudTrailAclCheck20150319",
"Effect": "Allow",
"Principal": map[string]interface{}{
"Service": "cloudtrail.amazonaws.com",
},
"Action": "s3:GetBucketAcl",
"Resource": "arn:aws:s3:::" + bucketName,
},
{
"Sid": "AWSCloudTrailWrite20150319",
"Effect": "Allow",
"Principal": map[string]interface{}{
"Service": "cloudtrail.amazonaws.com",
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::" + bucketName + "/AWSLogs/" + accountID + "/*",
"Condition": map[string]interface{}{
"StringEquals": map[string]interface{}{
"s3:x-amz-acl": "bucket-owner-full-control",
},
},
},
},
}
policy, err := json.Marshal(s3Policy)
if err != nil {
return err
}
// Create an S3 service
s3Svc := s3.New(sess)
// Set the policy on the bucket
_, err = s3Svc.PutBucketPolicy(&s3.PutBucketPolicyInput{
Bucket: aws.String(bucketName),
Policy: aws.String(string(policy)),
})
if err != nil {
return err
}
// snippet-end:[cloudtrail.go.configure_trail.policy]
return nil
}
// snippet-end:[cloudtrail.go.configure_trail]
// CreateTrail creates a trail to get event activity from a bucket
// Inputs:
//
// sess is the current session, which provides configuration for the SDK's service clients
// trailName is the name of the trail
// bucketName is the name of the bucket
//
// Output:
//
// If success, nil
// Otherwise, an error from the call to sts.GetCallerIdentity, json.Marshal, or s3.PutBucketPolicy
//
// snippet-start:[cloudtrail.go.create_trail]
func CreateTrail(sess *session.Session, trailName string, bucketName string) error {
// snippet-start:[cloudtrailops.go.create_trail.create]
svc := cloudtrail.New(sess)
ctInput := &cloudtrail.CreateTrailInput{
Name: aws.String(trailName),
S3BucketName: aws.String(bucketName),
}
_, err := svc.CreateTrail(ctInput)
if err != nil {
return err
}
return nil
// snippet-end:[cloudtrailops.go.create_trail.create]
}
// snippet-end:[cloudtrail.go.create_trail]
// GetTrails gets a list of trails
// Inputs:
//
// sess is the current session, which provides configuration for the SDK's service clients
//
// Output:
//
// If success, a list of Trail objects and nil
// Otherwise, a nil object and an error from a call to DescribeTrails
func GetTrails(sess *session.Session) ([]*cloudtrail.Trail, error) {
var trails []*cloudtrail.Trail
svc := cloudtrail.New(sess)
resp, err := svc.DescribeTrails(nil)
if err != nil {
return trails, err
}
return resp.TrailList, nil
}
// listTrails displays a list of your trails
// Inputs:
//
// sess is the current session, which provides configuration for the SDK's service clients
//
// Output:
//
// If success, nil
// Otherwise, an error returned by a call to DescribeTrails
//
// snippet-start:[cloudtrail.go.list_trails]
func listTrails(sess *session.Session) error {
svc := cloudtrail.New(sess)
resp, err := svc.DescribeTrails(nil)
if err != nil {
return err
}
fmt.Println("Found", len(resp.TrailList), "trail(s):")
fmt.Println("")
for _, trail := range resp.TrailList {
fmt.Println("Trail name: " + *trail.Name)
fmt.Println("Bucket name: " + *trail.S3BucketName)
fmt.Println("")
}
return nil
}
// snippet-end:[cloudtrail.go.list_trails]
// DeleteTrail deletes a trail
// Inputs:
//
// sess is the current session, which provides configuration for the SDK's service clients
// trailName is the name of the trail
//
// Output:
//
// If success, nil
// Otherwise, an error from a call to DeleteTrail
//
// snippet-start:[cloudtrail.go.delete_trail]
func DeleteTrail(sess *session.Session, trailName string) error {
svc := cloudtrail.New(sess)
input := &cloudtrail.DeleteTrailInput{
Name: aws.String(trailName),
}
_, err := svc.DeleteTrail(input)
return err
}
// snippet-end:[cloudtrail.go.delete_trail]
// GetUser retrieves the name of the logged on user
// Inputs:
//
// sess is the current session, which provides configuration for the SDK's service clients
//
// Output:
//
// If success, the user name and nil
// Otherwise, an empty string and an error from a call to GetCallerIdentity
func GetUser(sess *session.Session) (string, error) {
svc := sts.New(sess)
id, err := svc.GetCallerIdentity(nil)
if err != nil {
return "", err
}
// arn:aws:iam::ACCOUNT-ID:user/USER
parts := strings.Split(*id.Arn, "/")
userName := parts[1]
return userName, nil
}
// GetTrailEvents gets the events for a trail
// Inputs:
//
// sess is the current session, which provides configuration for the SDK's service clients
// trailName is the name of the trail
//
// Output:
//
// If success, a list of Event objects and nil
// Otherwise, a nil object and an error from a call to LookupEvents
func GetTrailEvents(sess *session.Session, trailName string) ([]*cloudtrail.Event, error) {
var events []*cloudtrail.Event
svc := cloudtrail.New(sess)
input := &cloudtrail.LookupEventsInput{EndTime: aws.Time(time.Now())}
resp, err := svc.LookupEvents(input)
if err != nil {
return events, err
}
return resp.Events, nil
}
// listTrailEvents lists the events for the user in a trail
// Inputs:
//
// sess is the current session, which provides configuration for the SDK's service clients
// trailName is the name of the trail
// userName is the name of the user
//
// Output:
//
// If success, nil
// Otherwise, an error from a call to LookupEvents
//
// snippet-start:[cloudtrail.go.list_trail_events]
func listTrailEvents(sess *session.Session, trailName string, userName string) error {
svc := cloudtrail.New(sess)
input := &cloudtrail.LookupEventsInput{EndTime: aws.Time(time.Now())}
resp, err := svc.LookupEvents(input)
if err != nil {
return err
}
fmt.Println("Events for the user " + userName + " in trail " + trailName + ":")
for _, event := range resp.Events {
if userName == *event.Username {
fmt.Println("")
fmt.Println("Event:")
fmt.Println("")
fmt.Println("Name ", aws.StringValue(event.EventName))
fmt.Println("ID: ", aws.StringValue(event.EventId))
fmt.Println("Time: ", aws.TimeValue(event.EventTime))
fmt.Println("Resources:")
for _, resource := range event.Resources {
fmt.Println(" Name:", aws.StringValue(resource.ResourceName))
fmt.Println(" Type:", aws.StringValue(resource.ResourceType))
}
}
}
return nil
}
// snippet-end:[cloudtrail.go.list_trail_events]
// usage displays information on using this app
// Inputs:
//
// none
//
// Output:
//
// none
func usage() {
fmt.Println("Usage:")
fmt.Println("")
fmt.Println(" go run sqsQueueOps.go OPERATION")
fmt.Println("")
fmt.Println("where OPERATION is one of:")
fmt.Println(" -l")
fmt.Println(" list your trails")
fmt.Println(" -c TRAIL-NAME BUCKET-NAME")
fmt.Println(" create trail TRAIL-NAME that gets events from bucket BUCKET-NAME")
fmt.Println(" -d TRAIL-NAME")
fmt.Println(" delete trail TRAIL-NAME")
fmt.Println(" -e USER-NAME TRAIL-NAME")
fmt.Println(" show events from the user USER-NAME for trail TRAIL-NAME")
fmt.Println(" -h")
fmt.Println(" print this message and quit")
fmt.Println("")
}
func main() {
// Parse args ourselves so the user can request only one operation
op := ""
numOps := 0 // So we know when the user has requested more than one operation
length := len(os.Args)
i := 1
trailName := ""
bucketName := ""
userName := ""
for i < length {
switch os.Args[i] {
case "-h":
usage()
return
case "-c":
// -c TRAIL-NAME BUCKET-NAME
if length != 3 {
fmt.Println("You must supply a trail name and bucket name")
usage()
return
}
op = "create"
i++
trailName = os.Args[i]
i++
bucketName = os.Args[i]
numOps++
case "-d":
// -d TRAIL-NAME
op = "delete"
i++
trailName = os.Args[i]
numOps++
case "-e":
// -e TRAIL-NAME
op = "events"
i++
userName = os.Args[i]
i++
trailName = os.Args[i]
numOps++
case "-l":
// -l
op = "list"
numOps++
default:
fmt.Println("Unrecognized option: " + os.Args[i])
usage()
return
}
i++
}
if numOps > 1 {
fmt.Println("You cannot request more than one operation")
usage()
return
}
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
switch op {
case "create":
if trailName == "" || bucketName == "" {
fmt.Println("You must supply a trail name and a bucket name")
usage()
return
}
err := CreateTrail(sess, trailName, bucketName)
if err != nil {
fmt.Println("Got an error creating trail:")
fmt.Println(err)
return
}
case "delete":
if trailName == "" {
fmt.Println("You must supply a trail name")
usage()
return
}
err := DeleteTrail(sess, trailName)
if err != nil {
fmt.Println("Got an error deleting trail:")
fmt.Println(err)
return
}
case "events":
if userName == "" || trailName == "" {
fmt.Println("You must supply a user name and trail name")
usage()
return
}
err := listTrailEvents(sess, trailName, userName)
if err != nil {
fmt.Println("Got an error listing trail events:")
fmt.Println(err)
return
}
case "list":
err := listTrails(sess)
if err != nil {
fmt.Println("Got an error listing trails:")
fmt.Println(err)
return
}
default:
// This should never happen
fmt.Println("Unrecognized operation (typo?): " + op)
return
}
}