forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
interfaces.go
207 lines (168 loc) · 5.27 KB
/
interfaces.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
package interfaces
import (
"strings"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apiserver/pkg/authentication/serviceaccount"
"k8s.io/apiserver/pkg/authentication/user"
kapi "k8s.io/kubernetes/pkg/api"
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
)
type Role interface {
Name() string
Namespace() string
Rules() []authorizationapi.PolicyRule
}
type RoleBinding interface {
Name() string
Namespace() string
RoleRef() kapi.ObjectReference
Users() sets.String
Groups() sets.String
// AppliesToUser returns true if the provided user matches this role binding
AppliesToUser(user.Info) bool
}
func NewClusterRoleBindingAdapter(roleBinding *authorizationapi.ClusterRoleBinding) RoleBinding {
return ClusterRoleBindingAdapter{roleBinding: roleBinding}
}
func NewLocalRoleBindingAdapter(roleBinding *authorizationapi.RoleBinding) RoleBinding {
return RoleBindingAdapter{roleBinding: roleBinding}
}
func NewClusterRoleAdapter(role *authorizationapi.ClusterRole) Role {
return ClusterRoleAdapter{role: role}
}
func NewLocalRoleAdapter(role *authorizationapi.Role) Role {
return RoleAdapter{role: role}
}
type RoleAdapter struct {
role *authorizationapi.Role
}
func (a RoleAdapter) Name() string {
return a.role.Name
}
func (a RoleAdapter) Namespace() string {
return a.role.Namespace
}
func (a RoleAdapter) Rules() []authorizationapi.PolicyRule {
return a.role.Rules
}
type ClusterRoleAdapter struct {
role *authorizationapi.ClusterRole
}
func (a ClusterRoleAdapter) Name() string {
return a.role.Name
}
func (a ClusterRoleAdapter) Namespace() string {
return a.role.Namespace
}
func (a ClusterRoleAdapter) Rules() []authorizationapi.PolicyRule {
return a.role.Rules
}
type RoleBindingAdapter struct {
roleBinding *authorizationapi.RoleBinding
}
func (a RoleBindingAdapter) Name() string {
return a.roleBinding.Name
}
func (a RoleBindingAdapter) Namespace() string {
return a.roleBinding.Namespace
}
func (a RoleBindingAdapter) RoleRef() kapi.ObjectReference {
return a.roleBinding.RoleRef
}
func (a RoleBindingAdapter) Users() sets.String {
users, _ := authorizationapi.StringSubjectsFor(a.roleBinding.Namespace, a.roleBinding.Subjects)
return sets.NewString(users...)
}
func (a RoleBindingAdapter) Groups() sets.String {
_, groups := authorizationapi.StringSubjectsFor(a.roleBinding.Namespace, a.roleBinding.Subjects)
return sets.NewString(groups...)
}
// AppliesToUser returns true if this binding applies to the provided user.
func (a RoleBindingAdapter) AppliesToUser(user user.Info) bool {
if subjectsContainUser(a.roleBinding.Subjects, a.roleBinding.Namespace, user.GetName()) {
return true
}
if subjectsContainAnyGroup(a.roleBinding.Subjects, user.GetGroups()) {
return true
}
return false
}
type ClusterRoleBindingAdapter struct {
roleBinding *authorizationapi.ClusterRoleBinding
}
func (a ClusterRoleBindingAdapter) Name() string {
return a.roleBinding.Name
}
func (a ClusterRoleBindingAdapter) Namespace() string {
return a.roleBinding.Namespace
}
func (a ClusterRoleBindingAdapter) RoleRef() kapi.ObjectReference {
return a.roleBinding.RoleRef
}
func (a ClusterRoleBindingAdapter) Users() sets.String {
users, _ := authorizationapi.StringSubjectsFor(a.roleBinding.Namespace, a.roleBinding.Subjects)
return sets.NewString(users...)
}
func (a ClusterRoleBindingAdapter) Groups() sets.String {
_, groups := authorizationapi.StringSubjectsFor(a.roleBinding.Namespace, a.roleBinding.Subjects)
return sets.NewString(groups...)
}
// AppliesToUser returns true if this binding applies to the provided user.
func (a ClusterRoleBindingAdapter) AppliesToUser(user user.Info) bool {
if subjectsContainUser(a.roleBinding.Subjects, a.roleBinding.Namespace, user.GetName()) {
return true
}
if subjectsContainAnyGroup(a.roleBinding.Subjects, user.GetGroups()) {
return true
}
return false
}
// subjectsContainUser returns true if the provided subjects contain the named user. currentNamespace
// is used to identify service accounts that are defined in a relative fashion.
func subjectsContainUser(subjects []kapi.ObjectReference, currentNamespace string, user string) bool {
if !strings.HasPrefix(user, serviceaccount.ServiceAccountUsernamePrefix) {
for _, subject := range subjects {
switch subject.Kind {
case authorizationapi.UserKind, authorizationapi.SystemUserKind:
if user == subject.Name {
return true
}
}
}
return false
}
for _, subject := range subjects {
switch subject.Kind {
case authorizationapi.ServiceAccountKind:
namespace := currentNamespace
if len(subject.Namespace) > 0 {
namespace = subject.Namespace
}
if len(namespace) == 0 {
continue
}
if user == serviceaccount.MakeUsername(namespace, subject.Name) {
return true
}
case authorizationapi.UserKind, authorizationapi.SystemUserKind:
if user == subject.Name {
return true
}
}
}
return false
}
// subjectsContainAnyGroup returns true if the provided subjects any of the named groups.
func subjectsContainAnyGroup(subjects []kapi.ObjectReference, groups []string) bool {
for _, subject := range subjects {
switch subject.Kind {
case authorizationapi.GroupKind, authorizationapi.SystemGroupKind:
for _, group := range groups {
if group == subject.Name {
return true
}
}
}
}
return false
}