forked from awsdocs/aws-doc-sdk-examples
/
create_trail.go
152 lines (129 loc) · 5.28 KB
/
create_trail.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
// snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
// snippet-sourceauthor:[Doug-AWS]
// snippet-sourcedescription:[create_trail.go demonstrates how to create a CloudTrail trail.]
// snippet-keyword:[Amazon CloudTrail]
// snippet-keyword:[CreateTrail function]
// snippet-keyword:[Go]
// snippet-sourcesyntax:[go]
// snippet-service:[cloudtrail]
// snippet-keyword:[Code Sample]
// snippet-sourcetype:[full-example]
// snippet-sourcedate:[2020-1-6]
/*
Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
This file is licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License. A copy of
the License is located at
http://aws.amazon.com/apache2.0/
This file 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.
*/
// snippet-start:[cloudtrail.go.create_trail.complete]
package main
// snippet-start:[cloudtrail.go.create_trail.imports]
import (
"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"
"encoding/json"
"flag"
"fmt"
)
// snippet-end:[cloudtrail.go.create_trail.imports]
func main() {
// Get required trail name and bucket name
// snippet-start:[cloudtrail.go.create_trail.vars]
trailNamePtr := flag.String("n", "", "The name of the trail")
bucketNamePtr := flag.String("b", "", "the name of the bucket to which the trails are uploaded")
addPolicyPtr := flag.Bool("p", false, "Whether to add the CloudTrail policy to the bucket")
flag.Parse()
if *trailNamePtr == "" || *bucketNamePtr == "" {
fmt.Println("You must supply a trail name and bucket name.")
return
}
// snippet-end:[cloudtrail.go.create_trail.vars]
// Initialize a session that the SDK will use to load
// credentials from the shared credentials file ~/.aws/credentials.
// snippet-start:[cloudtrail.go.create_trail.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// snippet-end:[cloudtrail.go.create_trail.session]
// snippet-start:[cloudtrail.go.create_trail.policy]
if *addPolicyPtr {
svc := sts.New(sess)
input := &sts.GetCallerIdentityInput{}
result, err := svc.GetCallerIdentity(input)
if err != nil {
fmt.Println("Got error snarfing caller identity:")
fmt.Println(err.Error())
return
}
accountID := aws.StringValue(result.Account)
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:::" + *bucketNamePtr,
},
{
"Sid": "AWSCloudTrailWrite20150319",
"Effect": "Allow",
"Principal": map[string]interface{}{
"Service": "cloudtrail.amazonaws.com",
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::" + *bucketNamePtr + "/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 {
fmt.Println("Error marshalling request")
return
}
// Create S3 service
s3Svc := s3.New(sess)
// Now set the policy on the bucket
_, err = s3Svc.PutBucketPolicy(&s3.PutBucketPolicyInput{
Bucket: aws.String(*bucketNamePtr),
Policy: aws.String(string(policy)),
})
if err != nil {
fmt.Print("Got error adding bucket policy:")
fmt.Print(err.Error())
return
}
fmt.Printf("Successfully set bucket %q's policy\n", *bucketNamePtr)
}
// snippet-end:[cloudtrail.go.create_trail.policy]
// snippet-start:[cloudtrail.go.create_trail.create]
svc := cloudtrail.New(sess)
input := &cloudtrail.CreateTrailInput{
Name: aws.String(*trailNamePtr),
S3BucketName: aws.String(*bucketNamePtr),
}
_, err := svc.CreateTrail(input)
if err != nil {
fmt.Println("Got error calling CreateTrail:")
fmt.Println(err.Error())
return
}
fmt.Println("Created the trail", *trailNamePtr, "for bucket", *bucketNamePtr)
// snippet-end:[cloudtrail.go.create_trail.create]
}
// snippet-end:[cloudtrail.go.create_trail.complete]