forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy.go
151 lines (119 loc) · 3.73 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
package test
import (
"errors"
"fmt"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/watch"
authorizationapi "github.com/openshift/origin/pkg/authorization/api"
)
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}
}
// ListPolicies obtains a list of policies that match a selector.
func (r *PolicyRegistry) ListPolicies(ctx kapi.Context, options *kapi.ListOptions) (*authorizationapi.PolicyList, error) {
if r.Err != nil {
return nil, r.Err
}
namespace := kapi.NamespaceValue(ctx)
list := make([]authorizationapi.Policy, 0)
if namespace == kapi.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 kapi.Context, id string) (*authorizationapi.Policy, 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 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 kapi.Context, policy *authorizationapi.Policy) 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.GetPolicy(ctx, policy.Name); 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 kapi.Context, policy *authorizationapi.Policy) 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.GetPolicy(ctx, policy.Name); 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 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.")
}
namespacedPolicies, ok := r.Policies[namespace]
if ok {
delete(namespacedPolicies, id)
}
return nil
}
func (r *PolicyRegistry) WatchPolicies(ctx kapi.Context, options *kapi.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
}