-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
311 lines (261 loc) · 8.31 KB
/
main.go
File metadata and controls
311 lines (261 loc) · 8.31 KB
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
package main
import (
"context"
"crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/gorilla/mux"
)
const Version = "0.0.0"
type IMDSv2Service struct {
Profile string
Configuration aws.Config
ServiceToken string
Credential aws.Credentials
}
const DefaultPort = "8080"
// helper functions
func contextWithSignal(ctx context.Context) context.Context {
newCtx, cancel := context.WithCancel(ctx)
signals := make(chan os.Signal)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
go func() {
select {
case sig := <-signals:
log.Printf("Received %s signal.\n", sig.String())
cancel()
}
}()
return newCtx
}
func getEnvOrDefault(key string, defaults ...string) string {
if val, ok := os.LookupEnv(key); ok {
return val
}
if len(defaults) > 0 {
return defaults[0]
}
return ""
}
func generateToken(length int) (string, error) {
bytes := make([]byte, length)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return base64.URLEncoding.EncodeToString(bytes), nil
}
func loadCredential(cfg aws.Config) (aws.Credentials, error) {
credential, err := cfg.Credentials.Retrieve(context.TODO())
if err != nil {
return aws.Credentials{}, err
}
return credential, nil
}
func createTemporaryCredential(cfg aws.Config) (aws.Credentials, error) {
stsClient := sts.NewFromConfig(cfg)
resp, err := stsClient.GetSessionToken(context.TODO(), &sts.GetSessionTokenInput{})
if err != nil {
return aws.Credentials{}, err
}
cred := resp.Credentials
return aws.Credentials{
Source: "createTemporaryCredentials",
AccessKeyID: *cred.AccessKeyId,
SecretAccessKey: *cred.SecretAccessKey,
SessionToken: *cred.SessionToken,
Expires: *cred.Expiration,
CanExpire: true,
}, nil
}
func loadOrCreateCredential(cfg aws.Config) (aws.Credentials, error) {
credential, err := loadCredential(cfg)
if err != nil {
return aws.Credentials{}, err
}
// issue temporary credential
if !credential.CanExpire {
credential, err = createTemporaryCredential(cfg)
if err != nil {
return aws.Credentials{}, err
}
}
return credential, nil
}
type loggingResponseWriter struct {
http.ResponseWriter
statusCode int
}
func (lrw *loggingResponseWriter) WriteHeader(code int) {
lrw.statusCode = code
lrw.ResponseWriter.WriteHeader(code)
}
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
lrw := &loggingResponseWriter{ResponseWriter: w, statusCode: http.StatusOK}
next.ServeHTTP(lrw, r)
duration := time.Since(start)
log.Printf("%s %s %d %s (from %s, ua=%q)\n",
r.Method,
r.URL.Path,
lrw.statusCode,
duration,
r.RemoteAddr,
r.UserAgent(),
)
})
}
// service entrypoint
func main() {
versionFlag := flag.Bool("version", false, "Print the version and exit.")
flag.Usage = func() {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [aws-profile]\n\n", os.Args[0])
fmt.Println("If \"aws-profile\" is provided, or the AWS_PROFILE environment variable is set, the app runs with that profile.")
}
flag.Parse()
if *versionFlag {
fmt.Printf("Version: %s\n", Version)
return
}
args := flag.Args()
log.Println("Starting MetaDock Service (IMDSv2)...")
profile := getEnvOrDefault("AWS_PROFILE", "default")
if len(args) > 0 {
profile = args[0]
}
log.Printf("Loading configuration for AWS profile %q.\n", profile)
configuration, err := config.LoadDefaultConfig(context.TODO(),
config.WithSharedConfigProfile(profile),
)
if err != nil {
log.Fatalf("Unable to load AWS configuration: %v\n", err)
}
log.Printf("Configured for %q AWS profile.\n", profile)
serviceToken, err := generateToken(32)
if err != nil {
log.Fatalf("Unable to generate token: %v\n", err)
}
// retrieve the credential at startup, as it takes too long if retrieved within
// the roleCredentialsHandler function, since the callee has a very short TTFB timeout
log.Printf("Retrieving credentials...\n")
credential, err := loadOrCreateCredential(configuration)
if err != nil {
log.Fatalf("Unable to load or create temporary credential: %v\n", err)
}
log.Printf("Retrieved credentials, expires at %q.\n", credential.Expires)
// setup service, middleware and router
service := &IMDSv2Service{
Profile: profile,
Configuration: configuration,
ServiceToken: serviceToken,
Credential: credential,
}
router := mux.NewRouter()
router.Use(loggingMiddleware)
router.HandleFunc("/", service.rootHandler).Methods("GET")
router.HandleFunc("/latest/api/token", service.tokenHandler).Methods("PUT")
router.HandleFunc("/latest/meta-data/iam/security-credentials/", service.credentialsHandler).Methods("GET")
router.HandleFunc("/latest/meta-data/iam/security-credentials/{role}", service.roleCredentialsHandler).Methods("GET")
router.HandleFunc("/health/", service.healthHandler).Methods("GET")
// setup HTTP server
port := getEnvOrDefault("PORT", DefaultPort)
server := &http.Server{
Addr: ":" + port,
Handler: router,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
}
// context for background processing
ctx := contextWithSignal(context.Background())
// run the server in a goroutine so it doesn’t block
go func() {
log.Printf("Listening on port %s for all network interfaces.\n", port)
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("Server error: %v\n", err)
}
}()
// wait for SIGINT/SIGTERM
<-ctx.Done()
log.Println("Shutting down server...")
// context with timeout for shutdown
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := server.Shutdown(shutdownCtx); err != nil {
log.Fatalf("Server forced to shutdown: %v\n", err)
} else {
log.Println("Shutdown successfully.")
}
}
// IDMS implementation
// url: / (root)
func (svc *IMDSv2Service) rootHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(w, "MetaDock")
}
// url: /latest/api/token
func (svc *IMDSv2Service) tokenHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(w, svc.ServiceToken)
}
// url: /latest/meta-data/iam/security-credentials/
func (svc *IMDSv2Service) credentialsHandler(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("X-aws-ec2-metadata-token")
if token != svc.ServiceToken {
log.Printf("Token mismatched error.\n")
http.Error(w, "", http.StatusForbidden)
return
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(w, "metadock")
}
// url: /latest/meta-data/iam/security-credentials/{role}
func (svc *IMDSv2Service) roleCredentialsHandler(w http.ResponseWriter, r *http.Request) {
// NB: this handler has to be very fast!
token := r.Header.Get("X-aws-ec2-metadata-token")
if token != svc.ServiceToken {
log.Printf("Token mismatched error.\n")
http.Error(w, "", http.StatusForbidden)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
credential := svc.Credential
response := map[string]string{
"Code": "Success",
"LastUpdated": time.Now().Format(time.RFC3339),
"Type": "AWS-HMAC",
"AccessKeyId": credential.AccessKeyID,
"SecretAccessKey": credential.SecretAccessKey,
"Token": credential.SessionToken,
"Expiration": credential.Expires.Format(time.RFC3339),
}
if err := json.NewEncoder(w).Encode(response); err != nil {
log.Printf("roleCredentialsHandler encode error: %v\n", err)
http.Error(w, "", http.StatusInternalServerError)
}
}
// url: /health/
func (svc *IMDSv2Service) healthHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
resp := map[string]string{"status": "ok"}
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("healthHandler encode error: %v\n", err)
http.Error(w, "", http.StatusInternalServerError)
}
}