forked from canonical/candid
/
auth.go
553 lines (506 loc) · 16.2 KB
/
auth.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package auth
import (
"sort"
"strings"
"github.com/juju/loggo"
"golang.org/x/net/context"
"gopkg.in/CanonicalLtd/candidclient.v1/params"
"gopkg.in/errgo.v1"
"gopkg.in/macaroon-bakery.v2/bakery"
"gopkg.in/macaroon-bakery.v2/bakery/checkers"
"gopkg.in/macaroon-bakery.v2/bakery/identchecker"
macaroon "gopkg.in/macaroon.v2"
"github.com/CanonicalLtd/candid/idp"
"github.com/CanonicalLtd/candid/store"
)
var logger = loggo.GetLogger("candid.internal.auth")
const (
AdminUsername = "admin@candid"
SSHKeyGetterGroup = "sshkeygetter@candid"
GroupListGroup = "grouplist@candid"
)
var AdminProviderID = store.MakeProviderIdentity("idm", "admin")
const (
kindGlobal = "global"
kindUser = "u"
)
// The following constants define possible operation actions.
const (
ActionRead = "read"
ActionVerify = "verify"
ActionDischargeFor = "dischargeFor"
ActionDischarge = "discharge"
ActionCreateAgent = "createAgent"
ActionReadAdmin = "readAdmin"
ActionWriteAdmin = "writeAdmin"
ActionReadGroups = "readGroups"
ActionWriteGroups = "writeGroups"
ActionReadSSHKeys = "readSSHKeys"
ActionWriteSSHKeys = "writeSSHKeys"
ActionLogin = "login"
ActionReadDischargeToken = "read-discharge-token"
)
// TODO(mhilton) make the admin ACL configurable
var AdminACL = []string{AdminUsername}
// An Authorizer is used to authorize operations in the identity server.
type Authorizer struct {
adminPassword string
location string
checker *identchecker.Checker
store store.Store
groupResolvers map[string]groupResolver
}
// Params specifify the configuration parameters for a new Authroizer.
type Params struct {
// AdminPassword is the password of the admin user in the
// identity server.
AdminPassword string
// Location is the url of the discharger that third-party caveats
// will be addressed to. This should be the address of this
// identity server.
Location string
// MacaroonOpStore is the store of macaroon operations and root
// keys.
MacaroonVerifier bakery.MacaroonVerifier
// Store is the identity store.
Store store.Store
// IdentityProviders contains the set of identity providers that
// are configured for the service. The authenticatore uses these
// to get group information for authenticated users.
IdentityProviders []idp.IdentityProvider
}
// New creates a new Authorizer for authorizing identity server
// operations.
func New(params Params) *Authorizer {
a := &Authorizer{
adminPassword: params.AdminPassword,
location: params.Location,
store: params.Store,
}
resolvers := make(map[string]groupResolver)
for _, idp := range params.IdentityProviders {
idp := idp
resolvers[idp.Name()] = idpGroupResolver{idp}
}
// Add a group resolver for the built-in candid provider.
resolvers["idm"] = candidGroupResolver{
store: params.Store,
resolvers: resolvers,
}
a.groupResolvers = resolvers
a.checker = identchecker.NewChecker(identchecker.CheckerParams{
Checker: NewChecker(a),
Authorizer: identchecker.ACLAuthorizer{
GetACL: func(ctx context.Context, op bakery.Op) ([]string, bool, error) {
return a.aclForOp(ctx, op)
},
},
IdentityClient: identityClient{a},
MacaroonVerifier: params.MacaroonVerifier,
})
return a
}
func (a *Authorizer) aclForOp(ctx context.Context, op bakery.Op) (acl []string, public bool, _ error) {
kind, name := splitEntity(op.Entity)
switch kind {
case kindGlobal:
if name != "" {
return nil, false, nil
}
switch op.Action {
case ActionRead:
// Only admins are allowed to read global information.
return AdminACL, true, nil
case ActionDischargeFor:
// Only admins are allowed to discharge for other users.
return AdminACL, true, nil
case ActionVerify:
// Everyone is allowed to verify a macaroon.
return []string{identchecker.Everyone}, true, nil
case ActionLogin:
// Everyone is allowed to log in.
return []string{identchecker.Everyone}, true, nil
case ActionDischarge:
// Everyone is allowed to discharge, but they must authenticate themselves
// first.
return []string{identchecker.Everyone}, false, nil
case ActionCreateAgent:
// Anyone can create an agent, as long as they've authenticated
// themselves.
return []string{identchecker.Everyone}, false, nil
}
case kindUser:
if name == "" {
return nil, false, nil
}
username := name
acl := make([]string, 0, len(AdminACL)+2)
acl = append(acl, AdminACL...)
switch op.Action {
case ActionRead:
return append(acl, username), false, nil
case ActionReadAdmin:
return acl, false, nil
case ActionWriteAdmin:
return acl, false, nil
case ActionReadGroups:
// Administrators, users with GroupList permissions and the user
// themselves can list their groups.
return append(acl, username, GroupListGroup), false, nil
case ActionWriteGroups:
// Only administrators can set a user's groups.
return acl, false, nil
case ActionReadSSHKeys:
return append(acl, username, SSHKeyGetterGroup), false, nil
case ActionWriteSSHKeys:
return append(acl, username), false, nil
}
case "groups":
switch op.Action {
case ActionDischarge:
return strings.Fields(name), true, nil
}
}
logger.Infof("no ACL found for op %#v", op)
return nil, false, nil
}
// SetAdminPublicKey configures the public key on the admin user. This is
// to allow agent login as the admin user.
func (a *Authorizer) SetAdminPublicKey(ctx context.Context, pk *bakery.PublicKey) error {
var pks []bakery.PublicKey
if pk != nil {
pks = append(pks, *pk)
}
return errgo.Mask(a.store.UpdateIdentity(
ctx,
&store.Identity{
ProviderID: AdminProviderID,
Username: AdminUsername,
PublicKeys: pks,
},
store.Update{
store.Username: store.Set,
store.Groups: store.Set,
store.PublicKeys: store.Set,
},
))
}
// Auth checks that client, as identified by the given context and
// macaroons, is authorized to perform the given operations. It may
// return an bakery.DischargeRequiredError when further checks are
// required, or params.ErrUnauthorized if the user is authenticated but
// does not have the required authorization.
func (a *Authorizer) Auth(ctx context.Context, mss []macaroon.Slice, ops ...bakery.Op) (*identchecker.AuthInfo, error) {
authInfo, err := a.checker.Auth(mss...).Allow(ctx, ops...)
if err != nil {
if errgo.Cause(err) == bakery.ErrPermissionDenied {
return nil, errgo.WithCausef(err, params.ErrUnauthorized, "")
}
return nil, errgo.Mask(err, isDischargeRequiredError)
}
return authInfo, nil
}
func isDischargeRequiredError(err error) bool {
_, ok := errgo.Cause(err).(*bakery.DischargeRequiredError)
return ok
}
// Identity creates a new identity for the user with the given username,
// such a user must exist in the store.
func (a *Authorizer) Identity(ctx context.Context, username string) (*Identity, error) {
id := &Identity{
id: store.Identity{
Username: username,
},
authorizer: a,
}
if err := id.lookup(ctx); err != nil {
return nil, errgo.Mask(err, errgo.Is(params.ErrNotFound))
}
return id, nil
}
// An identityClient is an implementation of identchecker.IdentityClient that
// uses the identity server's data store to get identity information.
type identityClient struct {
authorizer *Authorizer
}
// IdentityFromContext implements
// identchecker.IdentityClient.IdentityFromContext by looking for admin
// credentials in the context.
func (c identityClient) IdentityFromContext(ctx context.Context) (identchecker.Identity, []checkers.Caveat, error) {
if username := usernameFromContext(ctx); username != "" {
if err := CheckUserDomain(ctx, username); err != nil {
return nil, nil, errgo.Mask(err)
}
id, err := c.authorizer.Identity(ctx, username)
if err != nil {
return nil, nil, errgo.Mask(err, errgo.Is(params.ErrNotFound))
}
return id, nil, nil
}
if username, password, ok := userCredentialsFromContext(ctx); ok {
// TODO the mismatch between the username in the basic auth
// credentials and the admin username is unfortunate but we'll
// leave it for now. We should probably remove basic-auth authentication
// entirely.
if username+"@candid" == AdminUsername && c.authorizer.adminPassword != "" && password == c.authorizer.adminPassword {
return &Identity{
id: store.Identity{
Username: AdminUsername,
},
authorizer: c.authorizer,
}, nil, nil
}
return nil, nil, errgo.WithCausef(nil, params.ErrUnauthorized, "invalid credentials")
}
return nil, []checkers.Caveat{
checkers.NeedDeclaredCaveat(
checkers.Caveat{
Location: c.authorizer.location,
Condition: "is-authenticated-user",
},
"username",
),
}, nil
}
// CheckUserDomain checks that the given user name has
// a valid domain name with respect to the given context
// (see also ContextWithRequiredDomain).
func CheckUserDomain(ctx context.Context, username string) error {
domain, ok := ctx.Value(requiredDomainKey).(string)
if ok && !strings.HasSuffix(username, "@"+domain) {
return errgo.Newf("%q not in required domain %q", username, domain)
}
return nil
}
// DeclaredIdentity implements identchecker.IdentityClient.DeclaredIdentity by
// retrieving the user information from the declared map.
func (c identityClient) DeclaredIdentity(ctx context.Context, declared map[string]string) (identchecker.Identity, error) {
username, ok := declared["username"]
if !ok {
return nil, errgo.Newf("no declared user")
}
if err := CheckUserDomain(ctx, username); err != nil {
return nil, errgo.Mask(err)
}
return &Identity{
id: store.Identity{
Username: username,
},
authorizer: c.authorizer,
}, nil
}
// An Identity is the implementation of identchecker.Identity used in the
// identity server.
type Identity struct {
// Initially id is populated only with the Username field,
// but calls that require more information call Identity.lookup
// which fills out the rest.
id store.Identity
authorizer *Authorizer
resolvedGroups []string
}
// Id implements identchecker.Identity.Id.
func (id *Identity) Id() string {
return string(id.id.Username)
}
// Domain implements identchecker.Identity.Domain.
func (id *Identity) Domain() string {
return ""
}
// Allow implements identchecker.ACLIdentity.Allow by checking whether the
// given identity is in any of the required groups or users.
func (id *Identity) Allow(ctx context.Context, acl []string) (bool, error) {
if ok, isTrivial := trivialAllow(id.id.Username, acl); isTrivial {
return ok, nil
}
groups, err := id.Groups(ctx)
if err != nil {
return false, errgo.Mask(err)
}
for _, a := range acl {
for _, g := range groups {
if g == a {
return true, nil
}
}
}
return false, nil
}
// Groups returns all the groups associated with the user. The groups
// include those stored in the identity server's database along with any
// retrieved by the relevent identity provider's GetGroups method. Once
// the set of groups has been determined it is cached in the Identity.
func (id *Identity) Groups(ctx context.Context) ([]string, error) {
if id.resolvedGroups != nil {
return id.resolvedGroups, nil
}
if err := id.lookup(ctx); err != nil {
return nil, errgo.Mask(err, errgo.Is(params.ErrNotFound))
}
groups := id.id.Groups
if gr := id.authorizer.groupResolvers[id.id.ProviderID.Provider()]; gr != nil {
var err error
groups, err = gr.resolveGroups(ctx, &id.id)
if err != nil {
logger.Warningf("error resolving groups: %s", err)
} else {
id.resolvedGroups = groups
}
}
return groups, nil
}
// StoreIdentity returns the store identity document.
// Callers must not mutate the contents of the returned
// value.
func (id *Identity) StoreIdentity(ctx context.Context) (*store.Identity, error) {
if err := id.lookup(ctx); err != nil {
return nil, errgo.Mask(err)
}
return &id.id, nil
}
func (id *Identity) lookup(ctx context.Context) error {
if id.id.ID != "" {
return nil
}
if err := id.authorizer.store.Identity(ctx, &id.id); err != nil {
if errgo.Cause(err) == store.ErrNotFound {
return errgo.WithCausef(err, params.ErrNotFound, "")
}
return errgo.Mask(err)
}
return nil
}
// trivialAllow reports whether the username should be allowed
// access to the given ACL based on a superficial inspection
// of the ACL. If there is a definite answer, it will return
// a true isTrivial; otherwise it will return (false, false).
func trivialAllow(username string, acl []string) (allow, isTrivial bool) {
if len(acl) == 0 {
return false, true
}
for _, name := range acl {
if name == "everyone" || name == username {
return true, true
}
}
return false, false
}
// DomainDischargeOp creates an operation that is discharging the
// specified domain.
func DomainDischargeOp(domain string) bakery.Op {
return op("domain-"+domain, ActionDischarge)
}
// GroupsDischargeOp creates an operation that is discharging as a user
// in one of the specified groups.
func GroupsDischargeOp(groups []string) bakery.Op {
return op("groups-"+strings.Join(groups, " "), ActionDischarge)
}
func UserOp(u params.Username, action string) bakery.Op {
return op(kindUser+"-"+string(u), action)
}
func GlobalOp(action string) bakery.Op {
return op(kindGlobal, action)
}
func op(entity, action string) bakery.Op {
return bakery.Op{
Entity: entity,
Action: action,
}
}
func splitEntity(entity string) (string, string) {
if i := strings.Index(entity, "-"); i > 0 {
return entity[0:i], entity[i+1:]
}
return entity, ""
}
// A groupResolver is used to update the groups associated with an
// identity.
type groupResolver interface {
// resolveGroups returns the group information for the given
// identity. If a non-nil error is returned it will be logged,
// but the returned list of groups will still be taken as the set
// of groups to be associated with the identity.
resolveGroups(context.Context, *store.Identity) ([]string, error)
}
type candidGroupResolver struct {
store store.Store
resolvers map[string]groupResolver
}
// resolveGroups implements groupResolver by checking returning
// groups that are in both the identity and the owner of the
// identity.
func (r candidGroupResolver) resolveGroups(ctx context.Context, identity *store.Identity) ([]string, error) {
if len(identity.ProviderInfo["owner"]) == 0 {
// No owner - no groups. This applies to admin@candid, but for
// other users, it's probably an internal inconsistency error.
return nil, nil
}
ownerID := store.ProviderIdentity(identity.ProviderInfo["owner"][0])
if ownerID == AdminProviderID {
// The admin user is a member of all groups by definition.
return identity.Groups, nil
}
ownerIdentity := store.Identity{
ProviderID: ownerID,
}
if err := r.store.Identity(ctx, &ownerIdentity); err != nil {
if errgo.Cause(err) != store.ErrNotFound {
return nil, errgo.Mask(err)
}
return nil, nil
}
resolver := r.resolvers[ownerID.Provider()]
if resolver == nil {
// Owner is somehow in an unknown provider.
// TODO log/return an error?
return nil, nil
}
ownerGroups, err := resolver.resolveGroups(ctx, &ownerIdentity)
if err != nil {
return nil, errgo.Mask(err)
}
allowedGroups := make([]string, 0, len(identity.Groups))
for _, g1 := range identity.Groups {
for _, g2 := range ownerGroups {
if g2 == g1 {
allowedGroups = append(allowedGroups, g1)
break
}
}
}
return allowedGroups, nil
}
type idpGroupResolver struct {
idp idp.IdentityProvider
}
// resolveGroups implements groupResolver by getting the groups from the
// idp and adding them to the set stored in the identity server.
func (r idpGroupResolver) resolveGroups(ctx context.Context, id *store.Identity) ([]string, error) {
groups, err := r.idp.GetGroups(ctx, id)
if err != nil {
// We couldn't get the groups, so return only those stored in the database.
return id.Groups, errgo.Mask(err)
}
return uniqueStrings(append(groups, id.Groups...)), nil
}
// uniqueStrings removes all duplicates from the supplied
// string slice, updating the slice in place.
// The values will be in lexicographic order.
func uniqueStrings(ss []string) []string {
if len(ss) < 2 {
return ss
}
sort.Strings(ss)
prev := ss[0]
out := ss[:1]
for _, s := range ss[1:] {
if s == prev {
continue
}
out = append(out, s)
prev = s
}
return out
}