forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lookup.go
58 lines (46 loc) · 1.52 KB
/
lookup.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
package identitymapper
import (
"fmt"
kapi "k8s.io/kubernetes/pkg/api"
kuser "k8s.io/kubernetes/pkg/auth/user"
authapi "github.com/openshift/origin/pkg/auth/api"
"github.com/openshift/origin/pkg/user/registry/user"
"github.com/openshift/origin/pkg/user/registry/useridentitymapping"
)
var _ = authapi.UserIdentityMapper(&lookupIdentityMapper{})
// lookupIdentityMapper does not provision a new identity or user, it only allows identities already associated with users
type lookupIdentityMapper struct {
mappings useridentitymapping.Registry
users user.Registry
}
// UserFor returns info about the user for whom identity info has been provided
func (p *lookupIdentityMapper) UserFor(info authapi.UserIdentityInfo) (kuser.Info, error) {
ctx := kapi.NewContext()
mapping, err := p.mappings.GetUserIdentityMapping(ctx, info.GetIdentityName())
if err != nil {
return nil, NewLookupError(info, err)
}
u, err := p.users.GetUser(ctx, mapping.User.Name)
if err != nil {
return nil, NewLookupError(info, err)
}
return &kuser.DefaultInfo{
Name: u.Name,
UID: string(u.UID),
Groups: u.Groups,
}, nil
}
type lookupError struct {
Identity authapi.UserIdentityInfo
CausedBy error
}
func IsLookupError(err error) bool {
_, ok := err.(lookupError)
return ok
}
func NewLookupError(info authapi.UserIdentityInfo, err error) error {
return lookupError{Identity: info, CausedBy: err}
}
func (c lookupError) Error() string {
return fmt.Sprintf("lookup of user for %q failed: %v", c.Identity.GetIdentityName(), c.CausedBy)
}