forked from 99designs/aws-vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.go
64 lines (54 loc) · 1.65 KB
/
metadata.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"time"
)
const (
awsTimeFormat = "2006-01-02T15:04:05Z"
)
type metadataHandler struct {
http.Handler
credentials *VaultCredentials
}
func NewMetadataHandler(vc *VaultCredentials) http.Handler {
h := &metadataHandler{credentials: vc}
router := http.NewServeMux()
router.HandleFunc("/latest/meta-data/iam/security-credentials/", h.indexHandler)
router.HandleFunc("/latest/meta-data/iam/security-credentials/local-credentials", h.credentialsHandler)
h.Handler = router
return h
}
func (s *metadataHandler) indexHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "local-credentials")
}
type credentialsResponse struct {
AccessKeyID string `json:"AccessKeyId"`
Code string `json:"Code"`
Expiration string `json:"Expiration"`
LastUpdated string `json:"LastUpdated"`
SecretAccessKey string `json:"SecretAccessKey"`
Token string `json:"Token"`
Type string `json:"Type"`
}
func (s *metadataHandler) credentialsHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s", r.Method, r.RequestURI)
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
val, err := s.credentials.Get()
if err != nil {
http.Error(w, err.Error(), http.StatusGatewayTimeout)
return
}
json.NewEncoder(w).Encode(&credentialsResponse{
Code: "Success",
LastUpdated: time.Now().Format(awsTimeFormat),
Type: "AWS-HMAC",
AccessKeyID: val.AccessKeyID,
SecretAccessKey: val.SecretAccessKey,
Token: val.SessionToken,
Expiration: s.credentials.Expires().Format(awsTimeFormat),
})
}