forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_rules.go
166 lines (137 loc) · 5.4 KB
/
find_rules.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
package rulevalidation
import (
kapi "k8s.io/kubernetes/pkg/api"
kapierror "k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/auth/user"
kerrors "k8s.io/kubernetes/pkg/util/errors"
"k8s.io/kubernetes/pkg/util/sets"
authorizationapi "github.com/openshift/origin/pkg/authorization/api"
authorizationinterfaces "github.com/openshift/origin/pkg/authorization/interfaces"
"github.com/openshift/origin/pkg/client"
)
type DefaultRuleResolver struct {
policyGetter client.PoliciesListerNamespacer
bindingLister client.PolicyBindingsListerNamespacer
clusterPolicyGetter client.ClusterPolicyLister
clusterBindingLister client.ClusterPolicyBindingLister
}
func NewDefaultRuleResolver(policyGetter client.PoliciesListerNamespacer, bindingLister client.PolicyBindingsListerNamespacer, clusterPolicyGetter client.ClusterPolicyLister, clusterBindingLister client.ClusterPolicyBindingLister) *DefaultRuleResolver {
return &DefaultRuleResolver{policyGetter, bindingLister, clusterPolicyGetter, clusterBindingLister}
}
type AuthorizationRuleResolver interface {
GetRoleBindings(namespace string) ([]authorizationinterfaces.RoleBinding, error)
GetRole(roleBinding authorizationinterfaces.RoleBinding) (authorizationinterfaces.Role, error)
// RulesFor returns the list of rules that apply to a given user in a given namespace and error. If an error is returned, the slice of
// PolicyRules may not be complete, but it contains all retrievable rules. This is done because policy rules are purely additive and policy determinations
// can be made on the basis of those rules that are found.
RulesFor(info user.Info, namespace string) ([]authorizationapi.PolicyRule, error)
}
func (a *DefaultRuleResolver) GetRoleBindings(namespace string) ([]authorizationinterfaces.RoleBinding, error) {
clusterBindings, clusterErr := a.clusterBindingLister.List(kapi.ListOptions{})
var namespaceBindings *authorizationapi.PolicyBindingList
var namespaceErr error
if a.bindingLister != nil && len(namespace) > 0 {
namespaceBindings, namespaceErr = a.bindingLister.PolicyBindings(namespace).List(kapi.ListOptions{})
}
// return all loaded bindings
expect := 0
if clusterBindings != nil {
expect += len(clusterBindings.Items)
}
if namespaceBindings != nil {
expect += len(namespaceBindings.Items)
}
bindings := make([]authorizationinterfaces.RoleBinding, 0, expect)
if clusterBindings != nil {
for _, policyBinding := range clusterBindings.Items {
for _, value := range policyBinding.RoleBindings {
bindings = append(bindings, authorizationinterfaces.NewClusterRoleBindingAdapter(value))
}
}
}
if namespaceBindings != nil {
for _, policyBinding := range namespaceBindings.Items {
for _, value := range policyBinding.RoleBindings {
bindings = append(bindings, authorizationinterfaces.NewLocalRoleBindingAdapter(value))
}
}
}
// return all errors
var errs []error
if clusterErr != nil {
errs = append(errs, clusterErr)
}
if namespaceErr != nil {
errs = append(errs, namespaceErr)
}
return bindings, kerrors.NewAggregate(errs)
}
func (a *DefaultRuleResolver) GetRole(roleBinding authorizationinterfaces.RoleBinding) (authorizationinterfaces.Role, error) {
namespace := roleBinding.RoleRef().Namespace
name := roleBinding.RoleRef().Name
if len(namespace) == 0 {
policy, err := a.clusterPolicyGetter.Get(authorizationapi.PolicyName)
if kapierror.IsNotFound(err) {
return nil, kapierror.NewNotFound(authorizationapi.Resource("role"), name)
}
if err != nil {
return nil, err
}
role, exists := policy.Roles[name]
if !exists {
return nil, kapierror.NewNotFound(authorizationapi.Resource("role"), name)
}
return authorizationinterfaces.NewClusterRoleAdapter(role), nil
}
if a.policyGetter == nil {
return nil, kapierror.NewNotFound(authorizationapi.Resource("role"), name)
}
policy, err := a.policyGetter.Policies(namespace).Get(authorizationapi.PolicyName)
if kapierror.IsNotFound(err) {
return nil, kapierror.NewNotFound(authorizationapi.Resource("role"), name)
}
if err != nil {
return nil, err
}
role, exists := policy.Roles[name]
if !exists {
return nil, kapierror.NewNotFound(authorizationapi.Resource("role"), name)
}
return authorizationinterfaces.NewLocalRoleAdapter(role), nil
}
// RulesFor returns the list of rules that apply to a given user in a given namespace and error. If an error is returned, the slice of
// PolicyRules may not be complete, but it contains all retrievable rules. This is done because policy rules are purely additive and policy determinations
// can be made on the basis of those rules that are found.
func (a *DefaultRuleResolver) RulesFor(user user.Info, namespace string) ([]authorizationapi.PolicyRule, error) {
var errs []error
roleBindings, err := a.GetRoleBindings(namespace)
if err != nil {
errs = append(errs, err)
}
rules := make([]authorizationapi.PolicyRule, 0, len(roleBindings))
for _, roleBinding := range roleBindings {
if !roleBinding.AppliesToUser(user) {
continue
}
role, err := a.GetRole(roleBinding)
if err != nil {
errs = append(errs, err)
continue
}
for _, curr := range role.Rules() {
rules = append(rules, curr)
}
}
return rules, kerrors.NewAggregate(errs)
}
func appliesToUser(ruleUsers, ruleGroups sets.String, user user.Info) bool {
if ruleUsers.Has(user.GetName()) {
return true
}
for _, currGroup := range user.GetGroups() {
if ruleGroups.Has(currGroup) {
return true
}
}
return false
}