/
authenticator.go
146 lines (119 loc) · 3.94 KB
/
authenticator.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
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package keystone
import (
"fmt"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/identity/v3/groups"
"github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
"github.com/gophercloud/gophercloud/openstack/identity/v3/users"
"k8s.io/apiserver/pkg/authentication/user"
)
type tokenInfo struct {
userName string
userID string
roles []string
projectName string
projectID string
domainName string
domainID string
}
type IKeystone interface {
GetTokenInfo(string) (*tokenInfo, error)
GetGroups(string, string) ([]string, error)
}
type Keystoner struct {
client *gophercloud.ServiceClient
}
func NewKeystoner(client *gophercloud.ServiceClient) *Keystoner {
return &Keystoner{
client: client,
}
}
// revive:disable:unexported-return
func (k *Keystoner) GetTokenInfo(token string) (*tokenInfo, error) {
k.client.ProviderClient.SetToken(token)
ret := tokens.Get(k.client, token)
tokenUser, err := ret.ExtractUser()
if err != nil {
return nil, fmt.Errorf("failed to extract user information from Keystone response: %v", err)
}
project, err := ret.ExtractProject()
if err != nil {
return nil, fmt.Errorf("failed to extract project information from Keystone response: %v", err)
}
roles, err := ret.ExtractRoles()
if err != nil {
return nil, fmt.Errorf("failed to extract roles information from Keystone response: %v", err)
}
userRoles := make([]string, 0, len(roles))
for _, role := range roles {
userRoles = append(userRoles, role.Name)
}
return &tokenInfo{
userName: tokenUser.Name,
userID: tokenUser.ID,
projectName: project.Name,
projectID: project.ID,
roles: userRoles,
domainID: tokenUser.Domain.ID,
domainName: tokenUser.Domain.Name,
}, nil
}
// revive:enable:unexported-return
func (k *Keystoner) GetGroups(token string, userID string) ([]string, error) {
k.client.ProviderClient.SetToken(token)
allGroupPages, err := users.ListGroups(k.client, userID).AllPages()
if err != nil {
return nil, fmt.Errorf("failed to get user groups from Keystone: %v", err)
}
allGroups, err := groups.ExtractGroups(allGroupPages)
if err != nil {
return nil, fmt.Errorf("failed to extract user groups from Keystone response: %v", err)
}
userGroups := make([]string, 0, len(allGroups))
for _, g := range allGroups {
userGroups = append(userGroups, g.Name)
}
return userGroups, nil
}
// Authenticator contacts openstack keystone to validate user's token passed in the request.
type Authenticator struct {
keystoner IKeystone
}
// AuthenticateToken checks the token via Keystone call
func (a *Authenticator) AuthenticateToken(token string) (user.Info, bool, error) {
tokenInfo, err := a.keystoner.GetTokenInfo(token)
if err != nil {
return nil, false, fmt.Errorf("failed to authenticate: %v", err)
}
userGroups, err := a.keystoner.GetGroups(token, tokenInfo.userID)
if err != nil {
return nil, false, fmt.Errorf("failed to authenticate: %v", err)
}
extra := map[string][]string{
Roles: tokenInfo.roles,
ProjectID: {tokenInfo.projectID},
ProjectName: {tokenInfo.projectName},
DomainID: {tokenInfo.domainID},
DomainName: {tokenInfo.domainName},
}
userGroups = append(userGroups, tokenInfo.projectID)
authenticatedUser := &user.DefaultInfo{
Name: tokenInfo.userName,
UID: tokenInfo.userID,
Groups: userGroups,
Extra: extra,
}
return authenticatedUser, true, nil
}