-
Notifications
You must be signed in to change notification settings - Fork 3
/
handlers.go
152 lines (125 loc) · 3.91 KB
/
handlers.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
package finto
import (
"encoding/json"
"fmt"
"net/http"
"github.com/gorilla/mux"
)
// Contains application context.
type fintoContext struct {
set *RoleSet
instanceRole string
}
func InitFintoContext(rs *RoleSet, defrole string) (fintoContext, error) {
var fc = fintoContext{set: rs}
err := fc.setInstanceRole(defrole)
return fc, err
}
func (fc *fintoContext) setInstanceRole(role string) error {
if _, err := fc.set.Role(role); err != nil {
return err
}
fc.instanceRole = role
return nil
}
// VarsHandlerFunc accepts mux route variables as an argument.
type VarsHandlerFunc func(http.ResponseWriter, *http.Request, map[string]string)
func (f VarsHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
f(w, r, vars)
}
// List available roles.
func rolesList(fc *fintoContext) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var roles []string
if r.FormValue("status") == "active" {
roles = []string{fc.instanceRole}
} else {
roles = fc.set.Roles()
}
jsonResponse(w, map[string][]string{"roles": roles})
})
}
// Show a role's configuration.
func rolesShow(fc *fintoContext) http.Handler {
return VarsHandlerFunc(func(w http.ResponseWriter, r *http.Request, vars map[string]string) {
role, err := fc.set.Role(vars["alias"])
if err != nil {
errorResponse(w, err.Error(), http.StatusNotFound)
return
}
jsonResponse(w, map[string]string{
"arn": role.Arn(),
"session_name": role.SessionName(),
})
})
}
// Set role to be served as the instance profile role.
func rolesSetActive(fc *fintoContext) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
type activateRequest struct {
Alias string `json:"alias"`
}
var req activateRequest
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&req); err != nil {
errorResponse(w, fmt.Sprint("failed to parse body: ", err),
http.StatusBadRequest)
return
}
if err := fc.setInstanceRole(req.Alias); err != nil {
errorResponse(w, err.Error(), http.StatusBadRequest)
return
}
jsonResponse(w, map[string]string{"active_role": fc.instanceRole})
})
}
// Mock the EC2 security-credentials meta-data endpoint.
func mockProfile(fc *fintoContext) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fc.instanceRole))
})
}
// Mock the EC2 instance profile role meta-data endpoint.
func mockProfileCreds(fc *fintoContext) http.Handler {
return VarsHandlerFunc(func(w http.ResponseWriter, r *http.Request, vars map[string]string) {
role, err := fc.set.Role(vars["alias"])
if err != nil {
errorResponse(w, err.Error(), http.StatusNotFound)
return
}
creds, err := role.Credentials()
if err != nil {
errorResponse(w, fmt.Sprint("failed to assume role: ", err),
http.StatusInternalServerError)
return
}
// There's technically no reason to pretty print here, but do so to
// maintain parity in the mock service. Uses MarshalIndent as
// Encoder.Encode does not offer a means to do so.
b, err := json.MarshalIndent(map[string]string{
"Code": "Success",
"LastUpdated": "2015-07-07T23:06:33Z",
"Type": "AWS-HMAC",
"AccessKeyId": creds.AccessKeyId,
"SecretAccessKey": creds.SecretAccessKey,
"Token": creds.SessionToken,
"Expiration": creds.Expiration.Format("2006-01-02T15:04:05Z"),
}, "", " ")
if err != nil {
errorResponse(w, fmt.Sprint("failed to render: ", err),
http.StatusInternalServerError)
return
}
w.Write(b)
})
}
func jsonResponse(w http.ResponseWriter, body interface{}) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.Header().Set("Server", "EC2ws")
json.NewEncoder(w).Encode(body)
}
func errorResponse(w http.ResponseWriter, message string, code int) {
w.WriteHeader(code)
jsonResponse(w, map[string]string{"error": message})
}