/
main.go
235 lines (204 loc) · 5.5 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
package main
// Simple AMI query tool: uses basic loops
import (
"encoding/json"
"flag"
"fmt"
aws "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
eb "github.com/aws/aws-sdk-go/service/elasticbeanstalk"
"github.com/mefellows/credulous/credulous"
"github.com/vaughan0/go-ini"
"io/ioutil"
"log"
"os"
"strings"
"sync"
"time"
)
func main() {
type config struct {
region string
id string
action string
verbose bool
credulous bool
timeout time.Duration
}
// Get arguments
c := &config{}
flag.StringVar(&c.region, "region", os.Getenv("AWS_REGION"), "Region")
flag.StringVar(&c.region, "r", os.Getenv("AWS_REGION"), "Region")
flag.StringVar(&c.id, "id", "", "Resource ID to find")
flag.StringVar(&c.action, "action", "instance", "AWS resource (one of [instance, ami, ip, public-ip, eb, eb-resources, eb-env]")
flag.BoolVar(&c.credulous, "credulous", false, "Use credulous accounts instead of the stored aws profiles (default)")
flag.BoolVar(&c.verbose, "verbose", false, "Verbose output. Warning: This may disrupt output/pipe processing")
flag.DurationVar(&c.timeout, "timeout", 5*time.Second, "Timeout for the search. Defaults to 5s")
flag.Parse()
if c.region == "" || c.id == "" || c.action == "" {
flag.Usage()
os.Exit(1)
}
if !c.verbose {
log.SetOutput(ioutil.Discard)
}
var configs []*aws.Config
if c.credulous {
configs = listCredulous()
} else {
configs = listProfiles()
}
doneChan := make(chan bool, 1)
go func() {
var done sync.WaitGroup
done.Add(len(configs))
// Loop through all of the accounts, search for instance in parallel
for _, config := range configs {
go func(config *aws.Config) {
sess := session.New(config)
svc := ec2.New(sess)
ebSvc := eb.New(sess)
var r interface{}
switch strings.ToLower(c.action) {
case "instance":
r = queryInstance(svc, "instance-id", c.id)
case "ami":
r = queryAmi(svc, c.id)
case "ip":
r = queryInstance(svc, "private-ip-address", c.id)
case "public-ip":
r = queryInstance(svc, "ip-address", c.id)
case "eb":
r = queryBeanstalk(ebSvc, c.id)
case "eb-resources":
r = queryBeanstalkResources(ebSvc, c.id)
case "eb-env":
r = queryBeanstalkEnv(ebSvc, c.id)
default:
log.Fatalf("Action '%s' is not a valid action", c.action)
}
if r != nil {
v, err := json.Marshal(r)
checkError(err)
fmt.Printf("%s", v)
doneChan <- true
}
done.Done()
}(config)
}
done.Wait()
}()
// Wait up to timeout, or when first result comes back
select {
case <-time.After(c.timeout):
log.Fatalf("Timeout waiting for all accounts to return")
case <-doneChan:
os.Exit(0)
}
}
// Return true if AMI exists
func queryAmi(service *ec2.EC2, ami string) interface{} {
input := ec2.DescribeImagesInput{
ImageIds: []*string{&ami},
}
output, err := service.DescribeImages(&input)
if len(output.Images) > 0 {
checkError(err)
image := output.Images[0]
log.Printf("Found image in account (%s): %s, with name: %s\n", *image.OwnerId, *image.Name)
log.Printf("Tags: %v", image.Tags)
return image
}
return nil
}
func queryBeanstalk(svc *eb.ElasticBeanstalk, filterVal string) interface{} {
params := &eb.DescribeApplicationsInput{
ApplicationNames: []*string{
aws.String(filterVal),
},
}
resp, err := svc.DescribeApplications(params)
checkError(err)
if len(resp.Applications) > 0 {
return resp
}
return nil
}
func queryBeanstalkResources(svc *eb.ElasticBeanstalk, filterVal string) interface{} {
params := &eb.DescribeEnvironmentResourcesInput{
EnvironmentName: aws.String(filterVal),
}
resp, err := svc.DescribeEnvironmentResources(params)
checkError(err)
if resp.EnvironmentResources != nil {
return resp
}
return nil
}
func queryBeanstalkEnv(svc *eb.ElasticBeanstalk, filterVal string) interface{} {
params := &eb.DescribeEnvironmentsInput{
EnvironmentNames: []*string{aws.String(filterVal)},
}
resp, err := svc.DescribeEnvironments(params)
checkError(err)
if len(resp.Environments) > 0 {
return resp
}
return nil
}
func queryInstance(service *ec2.EC2, filter string, filterVal string) interface{} {
params := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
{
Name: aws.String(filter),
Values: []*string{
aws.String(filterVal),
},
},
},
}
resp, err := service.DescribeInstances(params)
if len(resp.Reservations) > 0 {
checkError(err)
return resp
}
return nil
}
func listCredulous() []*aws.Config {
accts := credulous.GetAccounts()
configs := make([]*aws.Config, len(accts))
for i, acct := range accts {
key, secret, err := credulous.GetCredentials(acct.Username, acct.Account)
checkError(err)
config := &aws.Config{
Credentials: credentials.NewStaticCredentials(key, secret, ""),
}
configs[i] = config
}
return configs
}
// Lists all profiles in the default ~/.aws/credentials directory
func listProfiles() []*aws.Config {
// Make sure the config file exists
config := os.Getenv("HOME") + "/.aws/credentials"
if _, err := os.Stat(config); os.IsNotExist(err) {
fmt.Println("No credentials file found at: %s", config)
os.Exit(1)
}
file, _ := ini.LoadFile(config)
configs := make([]*aws.Config, 0)
for key, _ := range file {
config := &aws.Config{
Credentials: credentials.NewSharedCredentials("", key),
}
configs = append(configs, config)
}
return configs
}
func checkError(err error) {
if err != nil {
log.Printf("Error: ", err)
}
}