/
main.go
324 lines (297 loc) · 8.18 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"sort"
"strings"
"text/tabwriter"
"time"
"github.com/codegangsta/cli"
)
// ListServerCertificatesResponse model
type ListServerCertificatesResponse struct {
ServerCertificateMetadataList []ServerCertificateMetadata
}
// ServerCertificateMetadata model
type ServerCertificateMetadata struct {
ServerCertificateID string `json:"ServerCertificateId"`
ServerCertificateName string
Expiration time.Time
Path string
Arn string
UploadDate time.Time
}
// CloudFrontListDistributionsResponse model
type CloudFrontListDistributionsResponse struct {
Distributionlist struct {
Items []CloudFrontDistributionConfig
}
}
// CloudFrontDistributionConfig model
type CloudFrontDistributionConfig struct {
ID string `json:"Id,omitempty"`
Status string `json:"Status,omitempty"`
WebACLID string `json:"WebACLId"`
DefaultRootObject string
PriceClass string
Enabled bool
CallerReference string
DomainName string
Comment string
CacheBehaviors struct {
Items []map[string]interface{}
Quantity int
}
Logging struct {
Bucket string
Prefix string
Enabled bool
IncludeCookies bool
}
Origins struct {
Quantity int
Items []map[string]interface{}
}
DefaultCacheBehavior struct {
TargetOriginID string `json:"TargetOriginId"`
ViewerProtocolPolicy string
MinTTL int
MaxTTL int
DefaultTTL int
SmoothStreaming bool
TrustedSigners struct {
Enabled bool
Quantity int
Items []string
}
ForwardedValues struct {
QueryString bool
Headers struct {
Items []string
Quantity int
}
Cookies struct {
Forward string
}
}
AllowedMethods struct {
Items []string
Quantity int
CachedMethods struct {
Items []string
Quantity int
}
}
}
ViewerCertificate struct {
CloudFrontDefaultCertificate bool
SSLSupportMethod string
MinimumProtocolVersion string
IAMCertificateID string `json:"IAMCertificateId"`
}
CustomErrorResponses struct {
Quantity int
Items []map[string]interface{}
}
Restrictions struct {
GeoRestriction struct {
RestrictionType string
Quantity int
}
}
Aliases struct {
Items []string
Quantity int
}
}
// DistSummary for summary output of a cloudfront distribution
type DistSummary struct {
ID string
Domain string
Alias string
Origin string
Status string
}
// ByAlias implements sort.Interface for []DistSummary based on
// the Alias field.
type ByAlias []DistSummary
func (a ByAlias) Len() int { return len(a) }
func (a ByAlias) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByAlias) Less(i, j int) bool { return a[i].Alias < a[j].Alias }
// ByOrigin implements sort.Interface for []DistSummary based on
// the Alias field.
type ByOrigin []DistSummary
func (a ByOrigin) Len() int { return len(a) }
func (a ByOrigin) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByOrigin) Less(i, j int) bool { return a[i].Origin < a[j].Origin }
// ByStatus implements sort.Interface for []DistSummary based on
// the Alias field.
type ByStatus []DistSummary
func (a ByStatus) Len() int { return len(a) }
func (a ByStatus) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByStatus) Less(i, j int) bool { return a[i].Status < a[j].Status }
// ResolveStatus resolve a certificates expiry status
func ResolveStatus(t time.Time) string {
now := time.Now()
if t.Unix() > now.AddDate(0, 1, 0).Unix() {
return "OK"
}
if t.Unix() > now.Unix() {
return "Expiring soon"
}
return "Expired"
}
func main() {
var app *cli.App
app = cli.NewApp()
app.Name = "awsutils"
app.Usage = "automation of Amazon Web Services configurations through their API"
app.EnableBashCompletion = true
app.Commands = []cli.Command{
{
Name: "iam",
Usage: "use the AWS iam API",
Subcommands: []cli.Command{
{
Name: "certs",
Usage: "summarise certificate configurations available in your AWS account",
Action: func(c *cli.Context) {
out, err := exec.Command("aws", "iam", "list-server-certificates").Output()
if err != nil {
fmt.Println(err.Error())
return
}
var r ListServerCertificatesResponse
err = json.Unmarshal(out, &r)
if err != nil {
fmt.Println(err.Error())
return
}
w := tabwriter.NewWriter(os.Stdout, 0, 4, 3, ' ', 0)
fmt.Fprint(w, "ID\tName\tStatus\n")
for _, cm := range r.ServerCertificateMetadataList {
fmt.Fprintf(w, "%s\t%s\t%s\n", cm.ServerCertificateID, cm.ServerCertificateName, ResolveStatus(cm.Expiration))
}
w.Flush()
// fmt.Printf("%+v\n", r)
},
},
},
},
{
Name: "cloudfront",
Aliases: []string{"cf"},
Usage: "use the AWS cloudfront API",
Subcommands: []cli.Command{
{
Name: "export-configs",
Usage: "export the cloudfront distribution configurations",
Action: func(c *cli.Context) {
out, err := exec.Command("aws", "cloudfront", "list-distributions").Output()
if err != nil {
fmt.Println(err.Error())
return
}
var r CloudFrontListDistributionsResponse
err = json.Unmarshal(out, &r)
if err != nil {
fmt.Println(err.Error())
return
}
w := tabwriter.NewWriter(os.Stdout, 0, 4, 3, ' ', 0)
fmt.Fprint(w, "DomainName\n")
for _, d := range r.Distributionlist.Items {
fmt.Fprintf(w, "%+v\n", d.Origins.Items[0]["DomainName"])
json, _ := json.MarshalIndent(d, "", " ")
fn := fmt.Sprintf("%s.json", d.Origins.Items[0]["Id"])
f, err := os.Create(fn)
if err != nil {
cwd, _ := os.Getwd()
fmt.Printf("Unable to create file '%s' in '%s'", fn, cwd)
continue
}
f.Write(json)
f.Close()
}
w.Flush()
},
},
{
Name: "dists",
Usage: "summarize the cloudfront distribution configurations",
Flags: []cli.Flag{
cli.StringFlag{
Name: "order-by",
Value: "alias",
Usage: "sort results on alias|origin|status",
},
cli.BoolFlag{
Name: "csv",
Usage: "output as csv",
},
},
Action: func(c *cli.Context) {
out, err := exec.Command("aws", "cloudfront", "list-distributions").Output()
if err != nil {
fmt.Println(err.Error())
return
}
var r CloudFrontListDistributionsResponse
err = json.Unmarshal(out, &r)
if err != nil {
fmt.Println(err.Error())
return
}
var list []DistSummary
for _, d := range r.Distributionlist.Items {
id := d.ID
distDomain := d.DomainName
status := d.Status
origin := ""
alias := ""
if len(d.Origins.Items) > 0 {
for _, o := range d.Origins.Items {
if op, ok := o["OriginPath"].(string); ok && op == "" {
origin = o["DomainName"].(string)
}
}
}
if len(d.Aliases.Items) > 0 {
alias = strings.Join(d.Aliases.Items, ",")
}
list = append(list, DistSummary{ID: id, Domain: distDomain, Alias: alias, Origin: origin, Status: status})
}
switch c.String("order-by") {
case "origin":
sort.Sort(ByOrigin(list))
case "status":
sort.Sort(ByStatus(list))
case "alias":
sort.Sort(ByAlias(list))
default:
fmt.Println("Unrecognised value for", c.String("order-by"), ". Sorting by alias instead.")
sort.Sort(ByAlias(list))
}
if c.Bool("csv") {
fmt.Println("ID;Domain;Alias;Origin;Status")
for _, s := range list {
fmt.Printf(`"%s";"%s";"%s";"%s";"%s"`, s.ID, s.Domain, s.Alias, s.Origin, s.Status)
fmt.Println()
}
} else {
w := tabwriter.NewWriter(os.Stdout, 0, 4, 3, ' ', 0)
fmt.Fprint(w, "ID\tDomain\tAlias\tOrigin\tStatus\n")
for _, s := range list {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", s.ID, s.Domain, s.Alias, s.Origin, s.Status)
}
w.Flush()
}
},
},
},
},
}
app.Run(os.Args)
}