forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policybinding.go
154 lines (122 loc) · 4.43 KB
/
policybinding.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
package test
import (
"errors"
"fmt"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kapierrors "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
authorizationapi "github.com/openshift/origin/pkg/authorization/api"
)
type PolicyBindingRegistry struct {
// PolicyBindings is a of namespace->name->PolicyBinding
PolicyBindings map[string]map[string]authorizationapi.PolicyBinding
Err error
}
func NewPolicyBindingRegistry(bindings []authorizationapi.PolicyBinding, err error) *PolicyBindingRegistry {
bindingMap := make(map[string]map[string]authorizationapi.PolicyBinding)
for _, binding := range bindings {
addPolicyBinding(bindingMap, binding)
}
return &PolicyBindingRegistry{bindingMap, err}
}
// ListPolicyBindings obtains a list of policyBinding that match a selector.
func (r *PolicyBindingRegistry) ListPolicyBindings(ctx kapi.Context, label labels.Selector, field fields.Selector) (*authorizationapi.PolicyBindingList, error) {
if r.Err != nil {
return nil, r.Err
}
namespace := kapi.NamespaceValue(ctx)
list := make([]authorizationapi.PolicyBinding, 0)
if namespace == kapi.NamespaceAll {
for _, curr := range r.PolicyBindings {
for _, binding := range curr {
list = append(list, binding)
}
}
} else {
if namespacedBindings, ok := r.PolicyBindings[namespace]; ok {
for _, curr := range namespacedBindings {
list = append(list, curr)
}
}
}
return &authorizationapi.PolicyBindingList{
Items: list,
},
nil
}
// GetPolicyBinding retrieves a specific policyBinding.
func (r *PolicyBindingRegistry) GetPolicyBinding(ctx kapi.Context, id string) (*authorizationapi.PolicyBinding, error) {
if r.Err != nil {
return nil, r.Err
}
namespace := kapi.NamespaceValue(ctx)
if len(namespace) == 0 {
return nil, errors.New("invalid request. Namespace parameter required.")
}
if namespacedBindings, ok := r.PolicyBindings[namespace]; ok {
if binding, ok := namespacedBindings[id]; ok {
return &binding, nil
}
}
return nil, kapierrors.NewNotFound("PolicyBinding", id)
}
// CreatePolicyBinding creates a new policyBinding.
func (r *PolicyBindingRegistry) CreatePolicyBinding(ctx kapi.Context, policyBinding *authorizationapi.PolicyBinding) error {
if r.Err != nil {
return r.Err
}
namespace := kapi.NamespaceValue(ctx)
if len(namespace) == 0 {
return errors.New("invalid request. Namespace parameter required.")
}
if existing, _ := r.GetPolicyBinding(ctx, policyBinding.Name); existing != nil {
return fmt.Errorf("PolicyBinding %v::%v already exists", namespace, policyBinding.Name)
}
addPolicyBinding(r.PolicyBindings, *policyBinding)
return nil
}
// UpdatePolicyBinding updates a policyBinding.
func (r *PolicyBindingRegistry) UpdatePolicyBinding(ctx kapi.Context, policyBinding *authorizationapi.PolicyBinding) error {
if r.Err != nil {
return r.Err
}
namespace := kapi.NamespaceValue(ctx)
if len(namespace) == 0 {
return errors.New("invalid request. Namespace parameter required.")
}
if existing, _ := r.GetPolicyBinding(ctx, policyBinding.Name); existing == nil {
return kapierrors.NewNotFound("PolicyBinding", policyBinding.Name)
}
addPolicyBinding(r.PolicyBindings, *policyBinding)
return nil
}
// DeletePolicyBinding deletes a policyBinding.
func (r *PolicyBindingRegistry) DeletePolicyBinding(ctx kapi.Context, id string) error {
if r.Err != nil {
return r.Err
}
namespace := kapi.NamespaceValue(ctx)
if len(namespace) == 0 {
return errors.New("invalid request. Namespace parameter required.")
}
namespacedBindings, ok := r.PolicyBindings[namespace]
if ok {
delete(namespacedBindings, id)
}
return nil
}
func (r *PolicyBindingRegistry) WatchPolicyBindings(ctx kapi.Context, label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
return nil, errors.New("unsupported action for test registry")
}
func addPolicyBinding(bindings map[string]map[string]authorizationapi.PolicyBinding, binding authorizationapi.PolicyBinding) {
resourceVersion += 1
binding.ResourceVersion = fmt.Sprintf("%d", resourceVersion)
namespacedBindings, ok := bindings[binding.Namespace]
if !ok {
namespacedBindings = make(map[string]authorizationapi.PolicyBinding)
bindings[binding.Namespace] = namespacedBindings
}
namespacedBindings[binding.Name] = binding
}