forked from juju/idmclient
/
idmtest.go
355 lines (322 loc) · 9.79 KB
/
idmtest.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
// Package idmtest holds a mock implementation of the identity manager
// suitable for testing.
package idmtest
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"sync"
"github.com/juju/httprequest"
"github.com/julienschmidt/httprouter"
"gopkg.in/errgo.v1"
"gopkg.in/macaroon-bakery.v1/bakery"
"gopkg.in/macaroon-bakery.v1/bakery/checkers"
"gopkg.in/macaroon-bakery.v1/httpbakery"
"gopkg.in/macaroon-bakery.v1/httpbakery/agent"
"github.com/juju/idmclient/params"
)
// Server represents a mock identity server.
// It currently serves only the discharge and groups endpoints.
type Server struct {
// URL holds the URL of the mock identity server.
// The discharger endpoint is located at URL/v1/discharge.
URL *url.URL
// PublicKey holds the public key of the mock identity server.
PublicKey *bakery.PublicKey
router *httprouter.Router
srv *httptest.Server
bakery *bakery.Service
// mu guards the fields below it.
mu sync.Mutex
users map[string]*user
defaultUser string
waits []chan struct{}
}
type user struct {
groups []string
key *bakery.KeyPair
}
// NewServer runs a mock identity server. It can discharge
// macaroons and return information on user group membership.
// The returned server should be closed after use.
func NewServer() *Server {
srv := &Server{
users: make(map[string]*user),
}
bsvc, err := bakery.NewService(bakery.NewServiceParams{
Locator: srv,
})
if err != nil {
panic(err)
}
srv.bakery = bsvc
srv.PublicKey = bsvc.PublicKey()
errorMapper := httprequest.ErrorMapper(errToResp)
h := &handler{
srv: srv,
}
router := httprouter.New()
for _, route := range errorMapper.Handlers(func(httprequest.Params) (*handler, error) {
return h, nil
}) {
router.Handle(route.Method, route.Path, route.Handle)
}
mux := http.NewServeMux()
httpbakery.AddDischargeHandler(mux, "/", srv.bakery, srv.check)
router.Handler("POST", "/v1/discharger/*rest", http.StripPrefix("/v1/discharger", mux))
router.Handler("GET", "/v1/discharger/*rest", http.StripPrefix("/v1/discharger", mux))
router.Handler("POST", "/discharge", mux)
router.Handler("GET", "/publickey", mux)
srv.srv = httptest.NewServer(router)
srv.URL, err = url.Parse(srv.srv.URL)
if err != nil {
panic(err)
}
return srv
}
func errToResp(err error) (int, interface{}) {
// Allow bakery errors to be returned as the bakery would
// like them, so that httpbakery.Client.Do will work.
if err, ok := errgo.Cause(err).(*httpbakery.Error); ok {
return httpbakery.ErrorToResponse(err)
}
errorBody := errorResponseBody(err)
status := http.StatusInternalServerError
switch errorBody.Code {
case params.ErrNotFound:
status = http.StatusNotFound
case params.ErrForbidden, params.ErrAlreadyExists:
status = http.StatusForbidden
case params.ErrBadRequest:
status = http.StatusBadRequest
case params.ErrUnauthorized, params.ErrNoAdminCredsProvided:
status = http.StatusUnauthorized
case params.ErrMethodNotAllowed:
status = http.StatusMethodNotAllowed
case params.ErrServiceUnavailable:
status = http.StatusServiceUnavailable
}
return status, errorBody
}
// errorResponse returns an appropriate error response for the provided error.
func errorResponseBody(err error) *params.Error {
errResp := ¶ms.Error{
Message: err.Error(),
}
cause := errgo.Cause(err)
if coder, ok := cause.(errorCoder); ok {
errResp.Code = coder.ErrorCode()
} else if errgo.Cause(err) == httprequest.ErrUnmarshal {
errResp.Code = params.ErrBadRequest
}
return errResp
}
type errorCoder interface {
ErrorCode() params.ErrorCode
}
// Close shuts down the server.
func (srv *Server) Close() {
srv.srv.Close()
}
// PublicKeyForLocation implements bakery.PublicKeyLocator
// by returning the server's public key for all locations.
func (srv *Server) PublicKeyForLocation(loc string) (*bakery.PublicKey, error) {
return srv.PublicKey, nil
}
// UserPublicKey returns the key for the given user.
// It panics if the user has not been added.
func (srv *Server) UserPublicKey(username string) *bakery.KeyPair {
u := srv.user(username)
if u == nil {
panic("no user found")
}
return u.key
}
// Client returns a bakery client that will discharge as the given user.
// If the user does not exist, it is added with no groups.
func (srv *Server) Client(username string) *httpbakery.Client {
c := httpbakery.NewClient()
u := srv.user(username)
if u == nil {
srv.AddUser(username)
u = srv.user(username)
}
c.Key = u.key
agent.SetUpAuth(c, srv.URL, username)
return c
}
// SetDefaultUser configures the server so that it will discharge for
// the given user if no agent-login cookie is found. The user does not
// need to have been added. Note that this will bypass the
// VisitURL logic.
//
// If the name is empty, there will be no default user.
func (srv *Server) SetDefaultUser(name string) {
srv.mu.Lock()
defer srv.mu.Unlock()
srv.defaultUser = name
}
// AddUser adds a new user that's in the given set of groups.
// If the user already exists, the given groups are
// added to that user's groups.
func (srv *Server) AddUser(name string, groups ...string) {
srv.mu.Lock()
defer srv.mu.Unlock()
u := srv.users[name]
if u == nil {
key, err := bakery.GenerateKey()
if err != nil {
panic(err)
}
srv.users[name] = &user{
groups: groups,
key: key,
}
return
}
for _, g := range groups {
found := false
for _, ug := range u.groups {
if ug == g {
found = true
break
}
}
if !found {
u.groups = append(u.groups, g)
}
}
}
func (srv *Server) user(name string) *user {
srv.mu.Lock()
defer srv.mu.Unlock()
return srv.users[name]
}
func (srv *Server) check(req *http.Request, cavId, cav string) ([]checkers.Caveat, error) {
if cav != "is-authenticated-user" {
return nil, errgo.Newf("unknown third party caveat %q", cav)
}
// First check if we have a login cookie so that we can avoid
// going through the VisitURL logic when an explicit default user
// has been set.
username, key, err := agent.LoginCookie(req)
if errgo.Cause(err) == agent.ErrNoAgentLoginCookie {
srv.mu.Lock()
defer srv.mu.Unlock()
if srv.defaultUser != "" {
return []checkers.Caveat{
checkers.DeclaredCaveat("username", srv.defaultUser),
}, nil
}
}
if err != nil {
return nil, errgo.Notef(err, "bad agent-login cookie in request")
}
srv.mu.Lock()
defer srv.mu.Unlock()
waitId := len(srv.waits)
srv.waits = append(srv.waits, make(chan struct{}, 1))
// Return a visit URL so that the client code is forced through that
// path, testing that its client correctly visits the URL and that
// any agent-login cookie has been appropriately set.
return nil, &httpbakery.Error{
Code: httpbakery.ErrInteractionRequired,
Info: &httpbakery.ErrorInfo{
VisitURL: fmt.Sprintf("%s/v1/login/%d", srv.URL, waitId),
WaitURL: fmt.Sprintf("%s/v1/wait/%d?username=%s&caveat-id=%s&pubkey=%v", srv.URL, waitId, username, url.QueryEscape(cavId), url.QueryEscape(key.String())),
},
}
}
type handler struct {
srv *Server
}
type groupsRequest struct {
httprequest.Route `httprequest:"GET /v1/u/:User/groups"`
User string `httprequest:",path"`
}
func (h *handler) GetGroups(p httprequest.Params, req *groupsRequest) ([]string, error) {
if err := h.checkRequest(p.Request); err != nil {
return nil, err
}
if u := h.srv.user(req.User); u != nil {
return u.groups, nil
}
return nil, params.ErrNotFound
}
func (h *handler) checkRequest(req *http.Request) error {
_, err := httpbakery.CheckRequest(h.srv.bakery, req, nil, checkers.New())
if err == nil {
return nil
}
_, ok := errgo.Cause(err).(*bakery.VerificationError)
if !ok {
return err
}
m, err := h.srv.bakery.NewMacaroon("", nil, []checkers.Caveat{{
Location: h.srv.URL.String() + "/v1/discharger",
Condition: "is-authenticated-user",
}})
if err != nil {
panic(err)
}
return httpbakery.NewDischargeRequiredErrorForRequest(m, "", err, req)
}
type loginRequest struct {
httprequest.Route `httprequest:"GET /v1/login/:WaitID"`
WaitID int `httprequest:",path"`
}
// TODO export VisitURLResponse from the bakery.
type visitURLResponse struct {
AgentLogin bool `json:"agent_login"`
}
// serveLogin provides the /login endpoint. When /login is called it should
// be provided with a test id. /login also supports some additional parameters:
// a = if set to "true" an agent URL will be added to the json response.
// i = if set to "true" a plaintext response will be sent to simulate interaction.
func (h *handler) Login(p httprequest.Params, req *loginRequest) (*visitURLResponse, error) {
h.srv.mu.Lock()
defer h.srv.mu.Unlock()
select {
case h.srv.waits[req.WaitID] <- struct{}{}:
default:
}
return &visitURLResponse{
AgentLogin: true,
}, nil
}
type waitRequest struct {
httprequest.Route `httprequest:"GET /v1/wait/:WaitID"`
WaitID int `httprequest:",path"`
Username string `httprequest:"username,form"`
CaveatID string `httprequest:"caveat-id,form"`
PublicKey *bakery.PublicKey `httprequest:"pubkey,form"`
}
func (h *handler) Wait(req *waitRequest) (*httpbakery.WaitResponse, error) {
h.srv.mu.Lock()
c := h.srv.waits[req.WaitID]
h.srv.mu.Unlock()
<-c
u := h.srv.user(req.Username)
if u == nil {
return nil, errgo.Newf("user not found")
}
if *req.PublicKey != u.key.Public {
return nil, errgo.Newf("public key mismatch")
}
checker := func(cavId, cav string) ([]checkers.Caveat, error) {
return []checkers.Caveat{
checkers.DeclaredCaveat("username", req.Username),
bakery.LocalThirdPartyCaveat(&u.key.Public),
}, nil
}
m, err := h.srv.bakery.Discharge(bakery.ThirdPartyCheckerFunc(checker), req.CaveatID)
if err != nil {
return nil, errgo.NoteMask(err, "cannot discharge", errgo.Any)
}
return &httpbakery.WaitResponse{
Macaroon: m,
}, nil
}