forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_common.go
212 lines (197 loc) · 7.29 KB
/
access_common.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
package globalnamespaceaccess
import (
"encoding/base32"
"fmt"
"strings"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/slice"
"github.com/rancher/norman/types/values"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/client/management/v3"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
type MemberAccess struct {
Users v3.UserInterface
PrtbLister v3.ProjectRoleTemplateBindingLister
RoleTemplateLister v3.RoleTemplateLister
}
const (
ImpersonateUserHeader = "Impersonate-User"
ImpersonateGroupHeader = "Impersonate-Group"
AllAccess = "all"
UpdateAccess = "update"
ReadonlyAccess = "readonly"
localPrincipalPrefix = "local://"
)
var accessVerbs = map[string][]string{
AllAccess: {"create", "update", "delete", "list"},
UpdateAccess: {"update", "get", "list"},
ReadonlyAccess: {"get", "list"},
}
func (ma *MemberAccess) CheckCreatorAndMembersAccessToTargets(request *types.APIContext, targets []string, data map[string]interface{}, resourceType, apiGroup, resource string) error {
creatorID := request.Request.Header.Get(ImpersonateUserHeader)
if err := ma.getGroupsAndCheckAccess(data, targets, resource, resourceType); err != nil {
return err
}
memberIDAccessType, err := ma.getMemberAccessTypeMap(creatorID, data)
if err != nil {
return err
}
defer request.Request.Header.Set(ImpersonateUserHeader, creatorID)
for userID, accessType := range memberIDAccessType {
userAPIContext := *request
userRequest := *(request.Request)
userAPIContext.Request = &userRequest
userAPIContext.Request.Header.Set(ImpersonateUserHeader, userID)
for _, targetID := range targets {
switch resourceType {
case client.GlobalDNSType:
newObj := make(map[string]interface{})
newObj["id"] = targetID
if _, err := checkAccess(&userAPIContext, accessVerbs[ReadonlyAccess], newObj, apiGroup, resource); err != nil {
return fmt.Errorf("user %v cannot access %v %v", userAPIContext.Request.Header.Get(ImpersonateUserHeader), resource, targetID)
}
case client.MultiClusterAppType:
split := strings.SplitN(targetID, ":", 2)
if len(split) != 2 {
return fmt.Errorf("project Id is incorrect")
}
projectNamespace := split[1]
newObj := make(map[string]interface{})
newObj["namespaceId"] = projectNamespace
if verb, err := checkAccess(&userAPIContext, accessVerbs[accessType], newObj, apiGroup, resource); err != nil {
return fmt.Errorf("user %v cannot %v apps in project %v", userAPIContext.Request.Header.Get(ImpersonateUserHeader), verb, targetID)
}
}
}
}
return nil
}
func (ma *MemberAccess) getMemberAccessTypeMap(creatorID string, data map[string]interface{}) (map[string]string, error) {
memberIDAccessType := make(map[string]string)
memberIDAccessType[creatorID] = AllAccess
membersMapSlice, _ := values.GetSlice(data, client.MultiClusterAppFieldMembers)
// find user and pass ID
for _, m := range membersMapSlice {
if userPrincipalID, ok := m[client.MemberFieldUserPrincipalID].(string); ok {
if _, ok := m[client.MemberFieldGroupPrincipalID].(string); ok {
return memberIDAccessType, fmt.Errorf("member cannot have both userPrincipalID and groupPrincipalID set")
}
user, err := ma.getUserFromUserPrincipalID(userPrincipalID)
if err != nil {
return memberIDAccessType, err
}
if user == nil {
return memberIDAccessType, fmt.Errorf("no user found for principal %v", userPrincipalID)
}
if accessType, exists := m[client.MemberFieldAccessType].(string); exists {
memberIDAccessType[user.Name] = accessType
} else {
memberIDAccessType[user.Name] = ReadonlyAccess
}
}
}
return memberIDAccessType, nil
}
func (ma *MemberAccess) getGroupsAndCheckAccess(data map[string]interface{}, targets []string, resource, resourceType string) error {
membersMapSlice, _ := values.GetSlice(data, client.MultiClusterAppFieldMembers)
groupAccessType := make(map[string]string)
var groups []string
for _, m := range membersMapSlice {
if groupPrincipalID, ok := m[client.MemberFieldGroupPrincipalID].(string); ok && groupPrincipalID != "" {
groups = append(groups, groupPrincipalID)
if accessType, exists := m[client.MemberFieldAccessType].(string); exists {
groupAccessType[groupPrincipalID] = accessType
} else {
groupAccessType[groupPrincipalID] = ReadonlyAccess
}
}
}
return CheckGroupAccess(groupAccessType, targets, ma.PrtbLister, ma.RoleTemplateLister, resource, resourceType)
}
func CheckGroupAccess(groups map[string]string, targets []string, prtbLister v3.ProjectRoleTemplateBindingLister, rtLister v3.RoleTemplateLister, resource, resourceType string) error {
for _, t := range targets {
split := strings.SplitN(t, ":", 2)
if len(split) != 2 {
return fmt.Errorf("Target project name %s is invalid", t)
}
projectNS := split[1]
prtbs, err := prtbLister.List(projectNS, labels.NewSelector())
if err != nil {
return err
}
prtbMap := make(map[string]string)
for _, prtb := range prtbs {
if prtb.GroupPrincipalName != "" {
prtbMap[prtb.GroupPrincipalName] = prtb.RoleTemplateName
}
}
// see if all groups are in prtbMap
for g, accessType := range groups {
if rtName, ok := prtbMap[g]; ok {
if resourceType == client.MultiClusterAppType {
rt, err := rtLister.Get("", rtName)
if err != nil {
return err
}
for _, r := range rt.Rules {
if !slice.ContainsString(r.Resources, resource) {
continue
}
if slice.ContainsString(r.Verbs, "*") {
continue
}
for _, v := range accessVerbs[accessType] {
if !slice.ContainsString(r.Verbs, v) {
return fmt.Errorf("member (group) %v cannot %v %v in project %v", g, v, resource, t)
}
}
}
}
} else {
return fmt.Errorf("member (group) %v does not have access to project %v", g, t)
}
}
}
return nil
}
func (ma *MemberAccess) getUserFromUserPrincipalID(userPrincipalID string) (*v3.User, error) {
encodedPrincipalID := base32.HexEncoding.WithPadding(base32.NoPadding).EncodeToString([]byte(userPrincipalID))
if len(encodedPrincipalID) > 63 {
encodedPrincipalID = encodedPrincipalID[:63]
}
set := labels.Set(map[string]string{encodedPrincipalID: "hashed-principal-name"})
usersList, err := ma.Users.List(v1.ListOptions{LabelSelector: set.String()})
if err != nil {
return nil, err
}
if len(usersList.Items) == 0 {
// check for local auth principals
if strings.HasPrefix(userPrincipalID, localPrincipalPrefix) {
userID := strings.TrimPrefix(userPrincipalID, localPrincipalPrefix)
user, err := ma.Users.Controller().Lister().Get("", userID)
return user, err
}
return nil, nil
}
var match *v3.User
for _, u := range usersList.Items {
if slice.ContainsString(u.PrincipalIDs, userPrincipalID) {
if match != nil {
// error out on duplicates
return nil, fmt.Errorf("can't find unique user for principal %v", userPrincipalID)
}
match = &u
}
}
return match, nil
}
func checkAccess(apiContext *types.APIContext, verbs []string, newObj map[string]interface{}, apiGroup, resource string) (string, error) {
for _, verb := range verbs {
if err := apiContext.AccessControl.CanDo(apiGroup, resource, verb, apiContext, newObj, apiContext.Schema); err != nil {
return verb, err
}
}
return "", nil
}