forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbac_common.go
269 lines (248 loc) · 8.68 KB
/
rbac_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
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
package globalnamespacerbac
import (
"fmt"
"k8s.io/apimachinery/pkg/types"
"reflect"
"sort"
"github.com/rancher/rancher/pkg/namespace"
"github.com/rancher/types/apis/management.cattle.io/v3"
rbacv1 "github.com/rancher/types/apis/rbac.authorization.k8s.io/v1"
"github.com/rancher/types/config"
k8srbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
ownerAccess = "owner"
memberAccess = "member"
readOnlyAccess = "read-only"
MultiClusterAppResource = "multiclusterapps"
MultiClusterAppRevisionResource = "multiclusterapprevisions"
GlobalDNSResource = "globaldnses"
GlobalDNSProviderResource = "globaldnsproviders"
CloudCredentialResource = "secrets"
CreatorIDAnn = "field.cattle.io/creatorId"
)
func CreateRoleAndRoleBinding(resource string, name string, UID types.UID, members []v3.Member, creatorID string,
managementContext *config.ManagementContext, apiGroup ...string) error {
/* Create 3 Roles containing the resource (multiclusterapp or globalDNS), and the current multiclusterapp/globalDNS in resourceNames list
1. Role with owner verbs (Owner access, includes creator); name multiclusterapp.Name + "-ma" / (globalDNS.Name + "-ga")
2. Role with "get", "list" "watch" verbs (ReadOnly access); name multiclusterapp.Name + "-mr" / (globalDNS.Name + "-gr")
3. Role with "update" verb (Member access); name multiclusterapp.Name + "-mu" / (globalDNS.Name + "-gu")
*/
api := []string{"management.cattle.io"}
if len(apiGroup) > 0 {
api = apiGroup
}
if _, err := createRole(resource, ownerAccess, name, UID, managementContext, api); err != nil {
return err
}
// Create a roleBinding referring the role with everything access, and containing creator of the multiclusterapp, along with
// any members that have everything access
var ownerAccessSubjects, readOnlyAccessSubjects, memberAccessSubjects []k8srbacv1.Subject
ownerAccessSubjects = append(ownerAccessSubjects, k8srbacv1.Subject{Kind: "User", Name: creatorID, APIGroup: rbacv1.GroupName})
for _, m := range members {
s, err := buildSubjectForMember(m, managementContext)
if err != nil {
return err
}
switch m.AccessType {
case ownerAccess:
ownerAccessSubjects = append(ownerAccessSubjects, s)
case memberAccess:
memberAccessSubjects = append(memberAccessSubjects, s)
case readOnlyAccess:
readOnlyAccessSubjects = append(readOnlyAccessSubjects, s)
default:
if resource == GlobalDNSProviderResource || resource == GlobalDNSResource {
ownerAccessSubjects = append(ownerAccessSubjects, s)
} else {
readOnlyAccessSubjects = append(readOnlyAccessSubjects, s)
}
}
}
if _, err := createRoleBinding(ownerAccess, name, UID, ownerAccessSubjects, managementContext, resource, api); err != nil {
return err
}
// Check if there are members with readonly or member(update) access; if found then create rolebindings for those
if len(readOnlyAccessSubjects) > 0 {
if _, err := createRole(resource, readOnlyAccess, name, UID, managementContext, api); err != nil {
return err
}
if _, err := createRoleBinding(readOnlyAccess, name, UID, readOnlyAccessSubjects, managementContext, resource, api); err != nil {
return err
}
}
if len(memberAccessSubjects) > 0 {
if _, err := createRole(resource, memberAccess, name, UID, managementContext, api); err != nil {
return err
}
if _, err := createRoleBinding(memberAccess, name, UID, memberAccessSubjects, managementContext, resource, api); err != nil {
return err
}
}
return nil
}
func createRole(resource string, roleAccess string, resourceName string, resourceUID types.UID,
managementContext *config.ManagementContext, apiGroups []string) (*k8srbacv1.Role, error) {
roleName, verbs := getRoleNameAndVerbs(roleAccess, resourceName, resource)
apiVersion := "management.cattle.io/v3"
if apiGroups[0] != "management.cattle.io" {
apiVersion = "v1" // for cloud credential
}
ownerReference := metav1.OwnerReference{
APIVersion: apiVersion,
Kind: resource,
Name: resourceName,
UID: resourceUID,
}
newRole := &k8srbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: roleName,
Namespace: namespace.GlobalNamespace,
OwnerReferences: []metav1.OwnerReference{ownerReference},
},
Rules: []k8srbacv1.PolicyRule{
{
APIGroups: apiGroups,
Resources: []string{resource},
ResourceNames: []string{resourceName},
Verbs: verbs,
},
},
}
role, err := managementContext.RBAC.Roles("").GetNamespaced(namespace.GlobalNamespace, roleName, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
role, err = managementContext.RBAC.Roles("").Create(newRole)
if err != nil {
return nil, err
}
} else {
return nil, err
}
} else if role != nil {
if !reflect.DeepEqual(newRole, role) {
toUpdate := newRole.DeepCopy()
updated, err := managementContext.RBAC.Roles("").Update(toUpdate)
if err != nil {
return updated, err
}
}
}
return role, nil
}
func createRoleBinding(roleAccess string, name string, UID types.UID,
subjects []k8srbacv1.Subject, managementContext *config.ManagementContext, resource string, apiGroups []string) (*k8srbacv1.RoleBinding, error) {
roleName, _ := getRoleNameAndVerbs(roleAccess, name, resource)
// we can define the rolebinding first, since if it's not already present we can call create. And if it's present then we'll
// still need to compare the current members' list
sort.Slice(subjects, func(i, j int) bool { return subjects[i].Name < subjects[j].Name })
apiVersion := "management.cattle.io/v3"
if apiGroups[0] != "management.cattle.io" {
apiVersion = "v1" // for cloud credential
}
ownerReference := metav1.OwnerReference{
APIVersion: apiVersion,
Kind: resource,
Name: name,
UID: UID,
}
newRoleBinding := &k8srbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: roleName,
Namespace: namespace.GlobalNamespace,
OwnerReferences: []metav1.OwnerReference{ownerReference},
},
RoleRef: k8srbacv1.RoleRef{
Name: roleName,
Kind: "Role",
},
Subjects: subjects,
}
roleBinding, err := managementContext.RBAC.RoleBindings("").GetNamespaced(namespace.GlobalNamespace, roleName, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
_, err = managementContext.RBAC.RoleBindings("").Create(newRoleBinding)
if err != nil && !apierrors.IsAlreadyExists(err) {
return nil, err
}
} else {
return nil, err
}
} else if roleBinding != nil {
if !reflect.DeepEqual(roleBinding, newRoleBinding) {
toUpdate := newRoleBinding.DeepCopy()
updated, err := managementContext.RBAC.RoleBindings("").Update(toUpdate)
if err != nil {
return updated, err
}
}
}
return newRoleBinding, nil
}
func getRoleNameAndVerbs(roleAccess string, resourceName string, resource string) (string, []string) {
var roleName string
var verbs []string
switch resource {
case MultiClusterAppResource:
resourceName += "-m-"
case GlobalDNSResource:
resourceName += "-g-"
case GlobalDNSProviderResource:
resourceName += "-gp-"
}
switch roleAccess {
case ownerAccess:
roleName = resourceName + "a"
verbs = []string{"*"}
case memberAccess:
roleName = resourceName + "u"
verbs = []string{"update", "get", "list", "watch"}
case readOnlyAccess:
roleName = resourceName + "r"
verbs = []string{"get", "list", "watch"}
}
return roleName, verbs
}
func buildSubjectForMember(member v3.Member, managementContext *config.ManagementContext) (k8srbacv1.Subject, error) {
var name, kind string
member, err := checkAndSetUserFields(member, managementContext)
if err != nil {
return k8srbacv1.Subject{}, err
}
if member.UserName != "" {
name = member.UserName
kind = "User"
}
if member.GroupPrincipalName != "" {
if name != "" {
return k8srbacv1.Subject{}, fmt.Errorf("member %v has both username and groupPrincipalId set", member)
}
name = member.GroupPrincipalName
kind = "Group"
}
if name == "" {
return k8srbacv1.Subject{}, fmt.Errorf("member %v doesn't have any name fields set", member)
}
return k8srbacv1.Subject{
Kind: kind,
Name: name,
APIGroup: rbacv1.GroupName,
}, nil
}
func checkAndSetUserFields(m v3.Member, managementContext *config.ManagementContext) (v3.Member, error) {
if m.GroupPrincipalName != "" || (m.UserPrincipalName != "" && m.UserName != "") {
return m, nil
}
if m.UserPrincipalName != "" && m.UserName == "" {
displayName := m.DisplayName
user, err := managementContext.UserManager.EnsureUser(m.UserPrincipalName, displayName)
if err != nil {
return m, err
}
m.UserName = user.Name
return m, nil
}
return m, nil
}