forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy.go
185 lines (148 loc) · 5.21 KB
/
policy.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
package test
import (
"errors"
"fmt"
metainternal "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/watch"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
authorizationlister "github.com/openshift/origin/pkg/authorization/generated/listers/authorization/internalversion"
policyregistry "github.com/openshift/origin/pkg/cmd/server/admin/legacyetcd/policy"
)
type PolicyRegistry struct {
// policies is a of namespace->name->Policy
policies map[string]map[string]authorizationapi.Policy
Err error
}
func NewPolicyRegistry(policies []authorizationapi.Policy, err error) *PolicyRegistry {
policyMap := make(map[string]map[string]authorizationapi.Policy)
for _, policy := range policies {
addPolicy(policyMap, policy)
}
return &PolicyRegistry{policyMap, err}
}
func (r *PolicyRegistry) List(_ labels.Selector) ([]*authorizationapi.Policy, error) {
return nil, fmt.Errorf("unimplemented")
}
func (r *PolicyRegistry) Policies(namespace string) authorizationlister.PolicyNamespaceLister {
return policyLister{registry: r, namespace: namespace}
}
type policyLister struct {
registry policyregistry.Registry
namespace string
}
func (s policyLister) List(label labels.Selector) ([]*authorizationapi.Policy, error) {
list, err := s.registry.ListPolicies(apirequest.WithNamespace(apirequest.NewContext(), s.namespace), &metainternal.ListOptions{LabelSelector: label})
if err != nil {
return nil, err
}
var items []*authorizationapi.Policy
for i := range list.Items {
items = append(items, &list.Items[i])
}
return items, nil
}
func (s policyLister) Get(name string) (*authorizationapi.Policy, error) {
return s.registry.GetPolicy(apirequest.WithNamespace(apirequest.NewContext(), s.namespace), name, &metav1.GetOptions{})
}
// ListPolicies obtains a list of policies that match a selector.
func (r *PolicyRegistry) ListPolicies(ctx apirequest.Context, options *metainternal.ListOptions) (*authorizationapi.PolicyList, error) {
if r.Err != nil {
return nil, r.Err
}
namespace := apirequest.NamespaceValue(ctx)
list := make([]authorizationapi.Policy, 0)
if namespace == metav1.NamespaceAll {
for _, curr := range r.policies {
for _, policy := range curr {
list = append(list, policy)
}
}
} else {
if namespacedPolicies, ok := r.policies[namespace]; ok {
for _, curr := range namespacedPolicies {
list = append(list, curr)
}
}
}
return &authorizationapi.PolicyList{
Items: list,
},
nil
}
// GetPolicy retrieves a specific policy.
func (r *PolicyRegistry) GetPolicy(ctx apirequest.Context, id string, options *metav1.GetOptions) (*authorizationapi.Policy, error) {
if r.Err != nil {
return nil, r.Err
}
namespace := apirequest.NamespaceValue(ctx)
if len(namespace) == 0 {
return nil, errors.New("invalid request. Namespace parameter required.")
}
if namespacedPolicies, ok := r.policies[namespace]; ok {
if policy, ok := namespacedPolicies[id]; ok {
return &policy, nil
}
}
return nil, fmt.Errorf("Policy %v::%v not found", namespace, id)
}
// CreatePolicy creates a new policy.
func (r *PolicyRegistry) CreatePolicy(ctx apirequest.Context, policy *authorizationapi.Policy) error {
if r.Err != nil {
return r.Err
}
namespace := apirequest.NamespaceValue(ctx)
if len(namespace) == 0 {
return errors.New("invalid request. Namespace parameter required.")
}
if existing, _ := r.GetPolicy(ctx, policy.Name, &metav1.GetOptions{}); existing != nil {
return fmt.Errorf("Policy %v::%v already exists", namespace, policy.Name)
}
addPolicy(r.policies, *policy)
return nil
}
// UpdatePolicy updates a policy.
func (r *PolicyRegistry) UpdatePolicy(ctx apirequest.Context, policy *authorizationapi.Policy) error {
if r.Err != nil {
return r.Err
}
namespace := apirequest.NamespaceValue(ctx)
if len(namespace) == 0 {
return errors.New("invalid request. Namespace parameter required.")
}
if existing, _ := r.GetPolicy(ctx, policy.Name, &metav1.GetOptions{}); existing == nil {
return fmt.Errorf("Policy %v::%v not found", namespace, policy.Name)
}
addPolicy(r.policies, *policy)
return nil
}
// DeletePolicy deletes a policy.
func (r *PolicyRegistry) DeletePolicy(ctx apirequest.Context, id string) error {
if r.Err != nil {
return r.Err
}
namespace := apirequest.NamespaceValue(ctx)
if len(namespace) == 0 {
return errors.New("invalid request. Namespace parameter required.")
}
namespacedPolicies, ok := r.policies[namespace]
if ok {
delete(namespacedPolicies, id)
}
return nil
}
func (r *PolicyRegistry) WatchPolicies(ctx apirequest.Context, options *metainternal.ListOptions) (watch.Interface, error) {
return nil, errors.New("unsupported action for test registry")
}
func addPolicy(policies map[string]map[string]authorizationapi.Policy, policy authorizationapi.Policy) {
resourceVersion += 1
policy.ResourceVersion = fmt.Sprintf("%d", resourceVersion)
namespacedPolicies, ok := policies[policy.Namespace]
if !ok {
namespacedPolicies = make(map[string]authorizationapi.Policy)
policies[policy.Namespace] = namespacedPolicies
}
namespacedPolicies[policy.Name] = policy
}