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
|
package imds
import (
"net/http"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"
)
type APIHandlers interface {
GetAPITokenHandler() http.Handler
GetAPIHandler() http.Handler
}
func newTestServeMux(t *testing.T, handlers APIHandlers) *http.ServeMux {
mux := http.NewServeMux()
mux.Handle(getTokenPath, validateAPITokenRequest(t, handlers.GetAPITokenHandler()))
mux.Handle("/latest/", handlers.GetAPIHandler())
return mux
}
func validateAPITokenRequest(t *testing.T, handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if e, a := "PUT", r.Method; e != a {
t.Errorf("expect %v, http method got %v", e, a)
http.Error(w, http.StatusText(400), 400)
return
}
if len(r.Header.Get(tokenTTLHeader)) == 0 {
t.Errorf("expect token TTL header to be present in the request headers, got none")
http.Error(w, http.StatusText(400), 400)
return
}
handler.ServeHTTP(w, r)
})
}
type secureAPIHandler struct {
t *testing.T
tokens []string
tokenTTL time.Duration
apiHandler http.Handler
activeToken atomic.Value
}
func newSecureAPIHandler(t *testing.T, tokens []string, tokenTTL time.Duration, apiHandler http.Handler) *secureAPIHandler {
return &secureAPIHandler{
t: t,
tokens: tokens,
tokenTTL: tokenTTL,
apiHandler: apiHandler,
}
}
func (h *secureAPIHandler) GetAPITokenHandler() http.Handler {
return http.HandlerFunc(h.handleAPIToken)
}
func (h *secureAPIHandler) handleAPIToken(w http.ResponseWriter, r *http.Request) {
token := h.tokens[0]
// set the active token
h.storeActiveToken(token)
// rotate the token
if len(h.tokens) > 1 {
h.tokens = h.tokens[1:]
}
var tokenTTLHeaderVal string
if h.tokenTTL == 0 {
tokenTTLHeaderVal = r.Header.Get(tokenTTLHeader)
} else {
tokenTTLHeaderVal = strconv.Itoa(int(h.tokenTTL / time.Second))
}
// set the header and response body
w.Header().Set(tokenTTLHeader, tokenTTLHeaderVal)
activeToken := h.getActiveToken()
w.Write([]byte(activeToken))
}
func (h *secureAPIHandler) GetAPIHandler() http.Handler {
return http.HandlerFunc(h.handleAPI)
}
func (h *secureAPIHandler) handleAPI(w http.ResponseWriter, r *http.Request) {
token := h.getActiveToken()
if len(token) == 0 {
h.t.Errorf("expect token to have been requested, was not")
http.Error(w, http.StatusText(401), 401)
return
}
if e, a := token, r.Header.Get(tokenHeader); e != a {
h.t.Errorf("expect %v token, got %v", e, a)
http.Error(w, http.StatusText(401), 401)
return
}
// delegate to configure handler for the request
h.apiHandler.ServeHTTP(w, r)
}
func (h *secureAPIHandler) storeActiveToken(t string) {
h.activeToken.Store(t)
}
func (h *secureAPIHandler) getActiveToken() string {
activeToken := h.activeToken.Load()
v, ok := activeToken.(string)
if !ok {
h.t.Errorf("expect valid active token string, got %T, %v", v, v)
}
return v
}
type insecureAPIHandler struct {
t *testing.T
apiTokenErrCode int
apiHandler http.Handler
}
func newInsecureAPIHandler(t *testing.T, apiTokenErrCode int, apiHandler http.Handler) *insecureAPIHandler {
return &insecureAPIHandler{
t: t,
apiTokenErrCode: apiTokenErrCode,
apiHandler: apiHandler,
}
}
func (h *insecureAPIHandler) GetAPITokenHandler() http.Handler {
return http.HandlerFunc(h.handleAPIToken)
}
func (h *insecureAPIHandler) handleAPIToken(w http.ResponseWriter, r *http.Request) {
http.Error(w, http.StatusText(h.apiTokenErrCode), h.apiTokenErrCode)
}
func (h *insecureAPIHandler) GetAPIHandler() http.Handler {
return http.HandlerFunc(h.handleAPI)
}
func (h *insecureAPIHandler) handleAPI(w http.ResponseWriter, r *http.Request) {
if len(r.Header.Get(tokenHeader)) != 0 {
h.t.Errorf("request token found, expected none")
http.Error(w, http.StatusText(400), 400)
return
}
// delegate to configure handler for the request
h.apiHandler.ServeHTTP(w, r)
}
type unauthorizedAPIHandler struct {
t *testing.T
enabled bool
secureAPIHandler *secureAPIHandler
}
func newUnauthorizedAPIHandler(t *testing.T, secureHandler *secureAPIHandler) *unauthorizedAPIHandler {
return &unauthorizedAPIHandler{
t: t,
secureAPIHandler: secureHandler,
}
}
func (h *unauthorizedAPIHandler) GetAPITokenHandler() http.Handler {
return http.HandlerFunc(h.handleAPIToken)
}
func (h *unauthorizedAPIHandler) handleAPIToken(w http.ResponseWriter, r *http.Request) {
// Respond with 404 first, then token after 401 API handler response
if !h.enabled {
http.Error(w, http.StatusText(404), 404)
return
}
h.secureAPIHandler.GetAPITokenHandler().ServeHTTP(w, r)
}
func (h *unauthorizedAPIHandler) GetAPIHandler() http.Handler {
return http.HandlerFunc(h.handleAPI)
}
func (h *unauthorizedAPIHandler) handleAPI(w http.ResponseWriter, r *http.Request) {
// Respond with 401 first, then 200 for second. When enabled switch to
// secure flow.
if !h.enabled {
h.enabled = true
http.Error(w, http.StatusText(401), 401)
return
}
h.secureAPIHandler.GetAPIHandler().ServeHTTP(w, r)
}
type requestTrace struct {
requests []string
mu sync.Mutex
}
func newRequestTrace() *requestTrace {
return &requestTrace{}
}
func (t *requestTrace) WrapHandler(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.mu.Lock()
t.requests = append(t.requests, r.URL.Path)
t.mu.Unlock()
handler.ServeHTTP(w, r)
})
}
|