This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccount_aws.go
94 lines (78 loc) · 1.87 KB
/
account_aws.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
package autosignr
import (
"fmt"
"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"
log "github.com/sirupsen/logrus"
)
// AccountAWS encapsulates the account information for AWS
type AccountAWS struct {
Name string `yaml:"name"`
Key string `yaml:"key"`
Secret string `yaml:"secret"`
Regions []string `yaml:"regions"`
Attribute string `yaml:"attribute"`
awsCreds *credentials.Credentials
}
// Init setup the account
func (a *AccountAWS) Init() error {
if a.Attribute == "" {
a.Attribute = "instance-id"
}
a.awsCreds = credentials.NewStaticCredentials(
a.Key,
a.Secret,
"")
return nil
}
// Type returns the type of account
func (a *AccountAWS) Type() string {
return "aws"
}
// Check look for the instanceID in the account
func (a *AccountAWS) Check(instanceID string) bool {
for _, region := range a.Regions {
log.WithFields(log.Fields{
"instance": instanceID,
"region": region,
"account": a.Name,
}).Debug("checking")
svc := ec2.New(session.New(), &aws.Config{
Credentials: a.awsCreds,
Region: aws.String(region),
})
params := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
{
Name: aws.String(a.Attribute),
Values: []*string{
aws.String(instanceID),
},
},
},
}
// Call the DescribeInstances Operation
resp, err := svc.DescribeInstances(params)
if err != nil {
log.Println(err)
continue
}
found := len(resp.Reservations) > 0
log.WithFields(log.Fields{
"instance": instanceID,
"region": region,
"account": a.Name,
"found": found,
}).Debug("check-result")
if found {
return true
}
}
return false
}
// String returns the account info
func (a *AccountAWS) String() string {
return fmt.Sprintf("aws account: %s", a.Name)
}