-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfundamental.go
234 lines (224 loc) · 7.02 KB
/
fundamental.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
// All fundamental services-related tagging functions
package main
import (
"fmt"
"log"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/sqs"
)
// tagiamrole tags an IAM role
func tagiamrole(rolename, key, value string) error {
log.Printf("Tagging IAM role '%s' with %s:%s", rolename, key, value)
svc := iam.New(session.New())
_, err := svc.TagRole(&iam.TagRoleInput{
RoleName: aws.String(rolename),
Tags: []*iam.Tag{
{
Key: aws.String(key),
Value: aws.String(value),
},
},
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case iam.ErrCodeNoSuchEntityException:
log.Println(iam.ErrCodeNoSuchEntityException, aerr.Error())
case iam.ErrCodeLimitExceededException:
log.Println(iam.ErrCodeLimitExceededException, aerr.Error())
case iam.ErrCodeInvalidInputException:
log.Println(iam.ErrCodeInvalidInputException, aerr.Error())
case iam.ErrCodeConcurrentModificationException:
log.Println(iam.ErrCodeConcurrentModificationException, aerr.Error())
case iam.ErrCodeServiceFailureException:
log.Println(iam.ErrCodeServiceFailureException, aerr.Error())
default:
log.Println(aerr.Error())
}
} else {
log.Println(err.Error())
}
}
return err
}
// tagiamuser tags an IAM user
func tagiamuser(username, key, value string) error {
log.Printf("Tagging IAM user '%s' with %s:%s", username, key, value)
svc := iam.New(session.New())
_, err := svc.TagUser(&iam.TagUserInput{
UserName: aws.String(username),
Tags: []*iam.Tag{
{
Key: aws.String(key),
Value: aws.String(value),
},
},
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case iam.ErrCodeNoSuchEntityException:
log.Println(iam.ErrCodeNoSuchEntityException, aerr.Error())
case iam.ErrCodeLimitExceededException:
log.Println(iam.ErrCodeLimitExceededException, aerr.Error())
case iam.ErrCodeInvalidInputException:
log.Println(iam.ErrCodeInvalidInputException, aerr.Error())
case iam.ErrCodeConcurrentModificationException:
log.Println(iam.ErrCodeConcurrentModificationException, aerr.Error())
case iam.ErrCodeServiceFailureException:
log.Println(iam.ErrCodeServiceFailureException, aerr.Error())
default:
log.Println(aerr.Error())
}
} else {
log.Println(err.Error())
}
}
return err
}
// tags3object tags an S3 object in a bucket
func tags3object(region, bucketname, objectname, key, value string) error {
log.Printf("Tagging S3 object '%s' in bucket '%s' in region '%s' with %s:%s",
objectname, bucketname, region, key, value)
svc := s3.New(session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: aws.String(region),
},
})))
_, err := svc.PutObjectTagging(&s3.PutObjectTaggingInput{
Bucket: aws.String(bucketname),
Key: aws.String(objectname),
Tagging: &s3.Tagging{
TagSet: []*s3.Tag{
{
Key: aws.String(key),
Value: aws.String(value),
},
},
},
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
log.Println(aerr.Error())
}
} else {
log.Println(err.Error())
}
}
return err
}
// tags3bucket tags an S3 bucket
func tags3bucket(region, bucketname, key, value string) error {
log.Printf("Tagging S3 bucket '%s' in region '%s' with %s:%s",
bucketname, region, key, value)
svc := s3.New(session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: aws.String(region),
},
})))
_, err := svc.PutBucketTagging(&s3.PutBucketTaggingInput{
Bucket: aws.String(bucketname),
Tagging: &s3.Tagging{
TagSet: []*s3.Tag{
{
Key: aws.String(key),
Value: aws.String(value),
},
},
},
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
log.Println(aerr.Error())
}
} else {
log.Println(err.Error())
}
}
return err
}
// taglambda tags a Lambda function
func taglambda(arnres arn.ARN, key, value string) error {
// arn:aws:lambda:us-west-2:123456789102:function:somefunc
region := arnres.Region
funcname := strings.Split(arnres.Resource, ":")[1]
log.Printf("Tagging Lambda function '%s' in region '%s' with %s:%s",
funcname, region, key, value)
svc := lambda.New(session.Must(session.NewSession()), aws.NewConfig().WithRegion(region))
_, err := svc.TagResource(&lambda.TagResourceInput{
Resource: aws.String(arnres.String()),
Tags: map[string]*string{key: aws.String(value)},
})
return err
}
// tagdynamodb tags a DynamoDB table
func tagdynamodb(arnres arn.ARN, key, value string) error {
// arn:aws:dynamodb:us-west-2:123456789102:table/TheTable
region := arnres.Region
tablename := strings.Split(arnres.Resource, "/")[1]
log.Printf("Tagging DynamoDB table '%s' in region '%s' with %s:%s",
tablename, region, key, value)
svc := dynamodb.New(session.Must(session.NewSession()), aws.NewConfig().WithRegion(region))
_, err := svc.TagResource(&dynamodb.TagResourceInput{
ResourceArn: aws.String(arnres.String()),
Tags: []*dynamodb.Tag{
{
Key: aws.String(key),
Value: aws.String(value),
},
},
})
return err
}
// tagec2 tags EC2 resources as defined in:
// https://docs.aws.amazon.com/IAM/latest/UserGuide/list_amazonec2.html#amazonec2-resources-for-iam-policies
func tagec2(arnres arn.ARN, key, value string) error {
// arn:aws:ec2:us-west-2:123456789102:*/*
region := arnres.Region
resourcetype := strings.Split(arnres.Resource, "/")[0]
resourceid := strings.Split(arnres.Resource, "/")[1]
log.Printf("Tagging EC2 resource '%s' of type '%s' in region '%s' with %s:%s",
resourceid, resourcetype, region, key, value)
svc := ec2.New(session.Must(session.NewSession()), aws.NewConfig().WithRegion(region))
_, err := svc.CreateTags(&ec2.CreateTagsInput{
Resources: []*string{aws.String(resourceid)},
Tags: []*ec2.Tag{
{
Key: aws.String(key),
Value: aws.String(value),
},
},
})
return err
}
// tagsqs tags SQS queues as defined in:
// https://docs.aws.amazon.com/IAM/latest/UserGuide/list_amazonsqs.html#amazonsqs-resources-for-iam-policies
func tagsqs(arnres arn.ARN, key, value string) error {
// arn:aws:sqs:us-west-2:123456789102:myqueue
region := arnres.Region
accountID := arnres.AccountID
queuename := arnres.Resource
log.Printf("Tagging SQS queue '%s' in region '%s' with %s:%s",
queuename, region, key, value)
// https://sqs.us-west-2.amazonaws.com/123456789102/myqueue
queueURL := fmt.Sprintf("https://sqs.%s.amazonaws.com/%s/%s", region, accountID, queuename)
svc := sqs.New(session.Must(session.NewSession()), aws.NewConfig().WithRegion(region))
_, err := svc.TagQueue(&sqs.TagQueueInput{
QueueUrl: aws.String(queueURL),
Tags: map[string]*string{key: aws.String(value)},
})
return err
}