-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
294 lines (240 loc) · 8.52 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
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"syscall"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/aws/aws-sdk-go/service/sts/stsiface"
"github.com/oklog/run"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
sourceEnvironment = "environment"
sourceInstance = "instance"
sourceWebIdentity = "webidentity"
)
const defaultWebIdentitySessionName = "voucher"
func main() {
ctx := context.Background()
var (
listen string
serving string
metricslisten string
metricsPath string
source string
enableIMDSv1 bool
envSessionTokens bool
metadataURL string
webIdentityTokenFile string
webIdentityRole string
webIdentitySessionName string
targetRole string
awsRegion string
)
flag.StringVar(&listen, "listen", "127.0.0.1:1800", "Address to listen for requests on")
flag.StringVar(&serving, "serving", "169.254.169.254", "Address this is being served on, for host header validation")
flag.StringVar(&source, "source", "environment", "Where to get upstream creds from. One of: environment, instance, webidentity")
flag.BoolVar(&enableIMDSv1, "imdsv1-enabled", true, "Support calls via IMDSv1")
flag.StringVar(&metricslisten, "metrics-listen", "0.0.0.0:1801", "Address to listen for metrics/health requests on")
flag.StringVar(&metricsPath, "metrics-path", "/metrics", "Path prometheus metrics should be served on")
flag.BoolVar(&envSessionTokens, "session-tokens", true, "environment: Use session tokens, rather than exposing the environment credentials directly")
flag.StringVar(&metadataURL, "metadata-url", "http://169.254.169.254:8080/latest", "Base URL to upstream metadata service. Used for instance source, other proxying")
// aws standard env var
flag.StringVar(&webIdentityTokenFile, "web-identity-token-file", os.Getenv("AWS_WEB_IDENTITY_TOKEN_FILE"), "webidentity: Path to JWT token")
// aws standard env var
flag.StringVar(&webIdentityRole, "web-identity-role", os.Getenv("AWS_ROLE_ARN"), "webidentity: Role for AssumeRoleWithWebIdentity call")
flag.StringVar(&webIdentitySessionName, "web-identity-session-name", getEnvDefault("WEB_IDENTITY_SESSION_NAME", defaultWebIdentitySessionName), "webidentity: Session name")
flag.StringVar(&targetRole, "target-role", os.Getenv("TARGET_ROLE"), "Additional role to assume in to before vouching credentials")
flag.StringVar(&awsRegion, "region", "", "AWS region we are operating in. If not set, will attempt to discover via env vars or metadata API")
flag.Parse()
mreg := prometheus.NewRegistry()
mreg.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
mreg.MustRegister(prometheus.NewGoCollector())
metrics := newMetrics()
if err := metrics.RegisterWith(mreg); err != nil {
log.Fatal(err)
}
promhttp.Handler()
sess, err := session.NewSession(&aws.Config{})
if err != nil {
log.Fatalf("creating base AWS session: %v", err)
}
mdcli := ec2metadata.New(sess, &aws.Config{Endpoint: &metadataURL})
if awsRegion == "" {
log.Print("Attempting to discover AWS region")
r, err := discoverRegion(mdcli)
if err != nil {
log.Fatalf("Failed to discover region: %v", err)
}
awsRegion = r
log.Printf("Running in region %s", awsRegion)
}
sess, err = session.NewSession(&aws.Config{
Region: &awsRegion,
// Use the regional endpoint, so it will hit a VPC endpoint if
// configured.
STSRegionalEndpoint: endpoints.RegionalSTSEndpoint,
})
if err != nil {
log.Fatalf("creating regional AWS session: %v", err)
}
var upstreamCreds *credentials.Credentials
switch source {
case sourceEnvironment:
if envSessionTokens {
// wrap the creds in something that gets temporary tokens. This prevents
// us from exposing the raw, long-lived credentials to the consumer
upstreamCreds = credentials.NewCredentials(&sessionTokenProvider{
Client: sts.New(sess),
ExpiryWindow: 10 * time.Second,
})
} else {
upstreamCreds = credentials.NewEnvCredentials()
}
case sourceInstance:
ip := &ec2rolecreds.EC2RoleProvider{
Client: ec2metadata.New(sess, &aws.Config{Endpoint: &metadataURL}),
}
upstreamCreds = credentials.NewCredentials(ip)
case sourceWebIdentity:
upstreamCreds = stscreds.NewWebIdentityCredentials(
sess,
webIdentityRole,
webIdentitySessionName,
webIdentityTokenFile,
)
default:
log.Fatalf("invalid source: %s", source)
}
if targetRole != "" {
upstreamCreds = stscreds.NewCredentials(sess, targetRole)
}
var g run.Group
g.Add(run.SignalHandler(ctx, os.Interrupt, syscall.SIGTERM))
{
s, err := newServer(metrics, upstreamCreds, mdcli, awsRegion)
if err != nil {
log.Fatalf("initializing server: %v", err)
}
s.EnableIMDS1 = enableIMDSv1
mainlis, err := net.Listen("tcp", listen)
if err != nil {
log.Fatalf("listening on %s: %v", listen, err)
}
mainsvr := http.Server{Handler: checkHostHeader(serving, s)}
g.Add(func() error {
log.Printf("Serving on %s", listen)
return mainsvr.Serve(mainlis)
}, func(error) {
sdctx, c := context.WithTimeout(context.Background(), 5*time.Second)
defer c()
if err := mainsvr.Shutdown(sdctx); err != nil {
log.Printf("shutting down main listener: %v", err)
}
})
}
{
obsvmux := http.NewServeMux()
if metricsPath == "" {
metricsPath = "/" // empty becomes just root
}
obsvmux.Handle(metricsPath, promhttp.InstrumentMetricHandler(mreg, promhttp.HandlerFor(mreg, promhttp.HandlerOpts{})))
h := &healthHandler{creds: upstreamCreds}
obsvmux.HandleFunc("/health", h.Healthy)
obsvmux.HandleFunc("/ready", h.Ready)
obsvlis, err := net.Listen("tcp", metricslisten)
if err != nil {
log.Fatalf("listening on %s: %v", metricslisten, err)
}
obsvsvr := http.Server{Handler: obsvmux}
g.Add(func() error {
log.Printf("Serving metrics on %s", metricslisten)
return obsvsvr.Serve(obsvlis)
}, func(error) {
sdctx, c := context.WithTimeout(context.Background(), 5*time.Second)
defer c()
if err := obsvsvr.Shutdown(sdctx); err != nil {
log.Printf("shutting down observability listener: %v", err)
}
})
}
if err := g.Run(); err != nil {
log.Fatal(err)
}
}
type creds struct {
Code string `json:"Code"`
LastUpdated string `json:"LastUpdated"`
Type string `json:"Type"`
AccessKeyID string `json:"AccessKeyId"`
SecretAccessKey string `json:"SecretAccessKey"`
Token string `json:"Token"`
Expiration string `json:"Expiration"`
}
type awsError struct {
Code string `json:"Code"`
Message string `json:"Message"`
LastUpdated string `json:"LastUpdated"`
}
func checkHostHeader(expectHeader string, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Host != expectHeader {
http.Error(w, fmt.Sprintf("Invalid request hostname: %s", r.Host), http.StatusForbidden)
return
}
h.ServeHTTP(w, r)
})
}
func getEnvDefault(envar, defaultval string) string {
ret := os.Getenv(envar)
if ret == "" {
ret = defaultval
}
return ret
}
func discoverRegion(md *ec2metadata.EC2Metadata) (string, error) {
if os.Getenv("AWS_REGION") != "" {
return os.Getenv("AWS_REGION"), nil
} else if os.Getenv("AWS_DEFAULT_REGION") != "" {
return os.Getenv("AWS_DEFAULT_REGION"), nil
}
iid, err := md.GetInstanceIdentityDocument()
if err != nil {
return "", fmt.Errorf("getting identity document: %v", err)
}
return iid.Region, nil
}
type sessionTokenProvider struct {
credentials.Expiry
Client stsiface.STSAPI
ExpiryWindow time.Duration
}
func (e *sessionTokenProvider) Retrieve() (credentials.Value, error) {
return e.RetrieveWithContext(context.Background())
}
func (e *sessionTokenProvider) RetrieveWithContext(ctx credentials.Context) (credentials.Value, error) {
st, err := e.Client.GetSessionTokenWithContext(ctx, &sts.GetSessionTokenInput{})
if err != nil {
return credentials.Value{}, fmt.Errorf("getting session token: %v", err)
}
c, err := credentials.NewStaticCredentials(*st.Credentials.AccessKeyId, *st.Credentials.SecretAccessKey, *st.Credentials.SessionToken).Get()
if err != nil {
return credentials.Value{}, fmt.Errorf("creating static credentials: %v", err)
}
e.SetExpiration(*st.Credentials.Expiration, e.ExpiryWindow)
return c, nil
}