-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
ec2info.go
215 lines (188 loc) · 5.15 KB
/
ec2info.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
package aws
import (
"fmt"
"net/http"
"os"
"strconv"
"sync"
"time"
"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"
"github.com/hairyhenderson/gomplate/v4/env"
)
var describerClient InstanceDescriber
var (
co ClientOptions
coInit sync.Once
sdkSession *session.Session
sdkSessionInit sync.Once
)
// ClientOptions -
type ClientOptions struct {
Timeout time.Duration
}
// Ec2Info -
type Ec2Info struct {
describer func() (InstanceDescriber, error)
metaClient *Ec2Meta
cache map[string]interface{}
}
// InstanceDescriber - A subset of ec2iface.EC2API that we can use to call EC2.DescribeInstances
type InstanceDescriber interface {
DescribeInstances(*ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error)
}
// GetClientOptions - Centralised reading of AWS_TIMEOUT
// ... but cannot use in vault/auth.go as different strconv.Atoi error handling
func GetClientOptions() ClientOptions {
coInit.Do(func() {
timeout := env.Getenv("AWS_TIMEOUT")
if timeout == "" {
timeout = "500"
}
t, err := strconv.Atoi(timeout)
if err != nil {
panic(fmt.Errorf("invalid AWS_TIMEOUT value '%s' - must be an integer: %w", timeout, err))
}
co.Timeout = time.Duration(t) * time.Millisecond
})
return co
}
// SDKSession -
func SDKSession(region ...string) *session.Session {
sdkSessionInit.Do(func() {
options := GetClientOptions()
timeout := options.Timeout
if timeout == 0 {
timeout = 500 * time.Millisecond
}
config := aws.NewConfig()
config = config.WithHTTPClient(&http.Client{Timeout: timeout})
if env.Getenv("AWS_ANON") == "true" {
config = config.WithCredentials(credentials.AnonymousCredentials)
}
metaRegion := ""
if len(region) > 0 {
metaRegion = region[0]
} else {
var err error
metaRegion, err = getRegion()
if err != nil {
panic(fmt.Errorf("failed to determine EC2 region: %w", err))
}
}
if metaRegion != "" && metaRegion != unknown {
config = config.WithRegion(metaRegion)
}
config = config.WithCredentialsChainVerboseErrors(true)
sdkSession = session.Must(session.NewSessionWithOptions(session.Options{
Config: *config,
SharedConfigState: session.SharedConfigEnable,
}))
})
return sdkSession
}
// Attempts to get the EC2 region to use. If we're running on an EC2 Instance
// and neither AWS_REGION nor AWS_DEFAULT_REGION are set, we'll infer from EC2
// metadata.
// Once https://github.com/aws/aws-sdk-go/issues/1103 is resolve this should be
// tidier!
func getRegion(m ...*Ec2Meta) (string, error) {
region := ""
_, default1 := os.LookupEnv("AWS_REGION")
_, default2 := os.LookupEnv("AWS_DEFAULT_REGION")
if !default1 && !default2 {
// Maybe we're in EC2, let's try to read metadata
var metaClient *Ec2Meta
if len(m) > 0 {
metaClient = m[0]
} else {
metaClient = NewEc2Meta(GetClientOptions())
}
var err error
region, err = metaClient.Region()
if err != nil {
return "", fmt.Errorf("failed to determine EC2 region: %w", err)
}
}
return region, nil
}
// NewEc2Info -
func NewEc2Info(options ClientOptions) (info *Ec2Info) {
metaClient := NewEc2Meta(options)
return &Ec2Info{
describer: func() (InstanceDescriber, error) {
if describerClient == nil {
session := SDKSession()
describerClient = ec2.New(session)
}
return describerClient, nil
},
metaClient: metaClient,
cache: make(map[string]interface{}),
}
}
// Tag -
func (e *Ec2Info) Tag(tag string, def ...string) (string, error) {
output, err := e.describeInstance()
if err != nil {
return "", err
}
if output == nil {
return returnDefault(def), nil
}
if len(output.Reservations) > 0 &&
len(output.Reservations[0].Instances) > 0 &&
len(output.Reservations[0].Instances[0].Tags) > 0 {
for _, v := range output.Reservations[0].Instances[0].Tags {
if *v.Key == tag {
return *v.Value, nil
}
}
}
return returnDefault(def), nil
}
func (e *Ec2Info) Tags() (map[string]string, error) {
tags := map[string]string{}
output, err := e.describeInstance()
if err != nil {
return tags, err
}
if output == nil {
return tags, nil
}
if len(output.Reservations) > 0 &&
len(output.Reservations[0].Instances) > 0 &&
len(output.Reservations[0].Instances[0].Tags) > 0 {
for _, v := range output.Reservations[0].Instances[0].Tags {
tags[*v.Key] = *v.Value
}
}
return tags, nil
}
func (e *Ec2Info) describeInstance() (output *ec2.DescribeInstancesOutput, err error) {
// cache the InstanceDescriber here
d, err := e.describer()
if err != nil || e.metaClient.nonAWS {
return nil, err
}
if cached, ok := e.cache["DescribeInstances"]; ok {
output = cached.(*ec2.DescribeInstancesOutput)
} else {
instanceID, err := e.metaClient.Meta("instance-id")
if err != nil {
return nil, err
}
input := &ec2.DescribeInstancesInput{
InstanceIds: aws.StringSlice([]string{instanceID}),
}
output, err = d.DescribeInstances(input)
if err != nil {
// default to nil if we can't describe the instance - this could be for any reason
return nil, nil
}
e.cache["DescribeInstances"] = output
}
return output, nil
}