|
1 |
| -package proxy |
| 1 | +package rbac |
2 | 2 |
|
3 | 3 | import (
|
| 4 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
4 | 5 | metainternal "k8s.io/apimachinery/pkg/apis/meta/internalversion"
|
5 | 6 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
6 | 7 | "k8s.io/apimachinery/pkg/runtime"
|
7 | 8 | apirequest "k8s.io/apiserver/pkg/endpoints/request"
|
8 | 9 | "k8s.io/apiserver/pkg/registry/rest"
|
| 10 | + "k8s.io/kubernetes/pkg/api" |
| 11 | + "k8s.io/kubernetes/pkg/apis/rbac" |
| 12 | + "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/rbac/internalversion" |
9 | 13 |
|
10 |
| - authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization" |
11 |
| - clusterpolicyregistry "github.com/openshift/origin/pkg/authorization/registry/clusterpolicy" |
12 |
| - "github.com/openshift/origin/pkg/authorization/registry/clusterrole" |
13 |
| - roleregistry "github.com/openshift/origin/pkg/authorization/registry/role" |
14 |
| - rolestorage "github.com/openshift/origin/pkg/authorization/registry/role/policybased" |
15 |
| - "github.com/openshift/origin/pkg/authorization/rulevalidation" |
| 14 | + authzapi "github.com/openshift/origin/pkg/authorization/apis/authorization" |
16 | 15 | )
|
17 | 16 |
|
18 |
| -type ClusterRoleStorage struct { |
19 |
| - roleStorage rolestorage.VirtualStorage |
| 17 | +func rbacToClusterRole(in *rbac.ClusterRole) (authzapi.ClusterRole, error) { |
| 18 | + var out authzapi.ClusterRole |
| 19 | + err := authzapi.Convert_rbac_ClusterRole_To_authorization_ClusterRole(in, &out, nil) |
| 20 | + return out, err |
20 | 21 | }
|
21 | 22 |
|
22 |
| -func NewClusterRoleStorage(clusterPolicyRegistry clusterpolicyregistry.Registry, liveRuleResolver, cachedRuleResolver rulevalidation.AuthorizationRuleResolver) clusterrole.Storage { |
23 |
| - return &ClusterRoleStorage{ |
24 |
| - roleStorage: rolestorage.VirtualStorage{ |
25 |
| - PolicyStorage: clusterpolicyregistry.NewSimulatedRegistry(clusterPolicyRegistry), |
| 23 | +func rbacFromClusterRole(in *authzapi.ClusterRole) (rbac.ClusterRole, error) { |
| 24 | + var out rbac.ClusterRole |
| 25 | + err := authzapi.Convert_authorization_ClusterRole_To_rbac_ClusterRole(in, &out, nil) |
| 26 | + return out, err |
| 27 | +} |
26 | 28 |
|
27 |
| - RuleResolver: liveRuleResolver, |
28 |
| - CachedRuleResolver: cachedRuleResolver, |
| 29 | +type ClusterRoleStorage struct { |
| 30 | + client internalversion.ClusterRoleInterface |
| 31 | +} |
29 | 32 |
|
30 |
| - CreateStrategy: roleregistry.ClusterStrategy, |
31 |
| - UpdateStrategy: roleregistry.ClusterStrategy, |
32 |
| - Resource: authorizationapi.Resource("clusterrole"), |
33 |
| - }, |
34 |
| - } |
| 33 | +func NewREST(client internalversion.ClusterRoleInterface) *ClusterRoleStorage { |
| 34 | + return &ClusterRoleStorage{client} |
35 | 35 | }
|
36 | 36 |
|
37 | 37 | func (s *ClusterRoleStorage) New() runtime.Object {
|
38 |
| - return &authorizationapi.ClusterRole{} |
| 38 | + return &authzapi.ClusterRole{} |
39 | 39 | }
|
40 | 40 | func (s *ClusterRoleStorage) NewList() runtime.Object {
|
41 |
| - return &authorizationapi.ClusterRoleList{} |
| 41 | + return &authzapi.ClusterRoleList{} |
42 | 42 | }
|
43 | 43 |
|
44 | 44 | func (s *ClusterRoleStorage) List(ctx apirequest.Context, options *metainternal.ListOptions) (runtime.Object, error) {
|
45 |
| - ret, err := s.roleStorage.List(ctx, options) |
46 |
| - if ret == nil { |
| 45 | + optv1 := metav1.ListOptions{} |
| 46 | + if err := metainternal.Convert_internalversion_ListOptions_To_v1_ListOptions(options, &optv1, nil); err != nil { |
47 | 47 | return nil, err
|
48 | 48 | }
|
49 |
| - return authorizationapi.ToClusterRoleList(ret.(*authorizationapi.RoleList)), err |
| 49 | + roles, err := s.client.List(optv1) |
| 50 | + if roles == nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + ret := &authzapi.ClusterRoleList{} |
| 54 | + for _, curr := range roles.Items { |
| 55 | + role, err := rbacToClusterRole(&curr) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + ret.Items = append(ret.Items, role) |
| 60 | + } |
| 61 | + return ret, err |
50 | 62 | }
|
51 | 63 |
|
52 | 64 | func (s *ClusterRoleStorage) Get(ctx apirequest.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
|
53 |
| - ret, err := s.roleStorage.Get(ctx, name, options) |
54 |
| - if ret == nil { |
| 65 | + ret, err := s.client.Get(name, *options) |
| 66 | + if err != nil { |
55 | 67 | return nil, err
|
56 | 68 | }
|
57 |
| - |
58 |
| - return authorizationapi.ToClusterRole(ret.(*authorizationapi.Role)), err |
| 69 | + role, err := rbacToClusterRole(ret) |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + return &role, err |
59 | 74 | }
|
| 75 | + |
60 | 76 | func (s *ClusterRoleStorage) Delete(ctx apirequest.Context, name string, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
|
61 |
| - ret, immediate, err := s.roleStorage.Delete(ctx, name, options) |
62 |
| - if ret == nil { |
63 |
| - return nil, immediate, err |
| 77 | + if err := s.client.Delete(name, options); err != nil { |
| 78 | + return nil, false, err |
64 | 79 | }
|
65 | 80 |
|
66 |
| - return ret.(*metav1.Status), false, err |
| 81 | + return &metav1.Status{Status: metav1.StatusSuccess}, true, nil |
67 | 82 | }
|
68 | 83 |
|
69 | 84 | func (s *ClusterRoleStorage) Create(ctx apirequest.Context, obj runtime.Object) (runtime.Object, error) {
|
70 |
| - clusterObj := obj.(*authorizationapi.ClusterRole) |
71 |
| - convertedObj := authorizationapi.ToRole(clusterObj) |
| 85 | + clusterObj := obj.(*authzapi.ClusterRole) |
| 86 | + convertedObj, err := rbacFromClusterRole(clusterObj) |
72 | 87 |
|
73 |
| - ret, err := s.roleStorage.Create(ctx, convertedObj) |
74 |
| - if ret == nil { |
| 88 | + ret, err := s.client.Create(&convertedObj) |
| 89 | + if err != nil { |
75 | 90 | return nil, err
|
76 | 91 | }
|
77 |
| - |
78 |
| - return authorizationapi.ToClusterRole(ret.(*authorizationapi.Role)), err |
79 |
| -} |
80 |
| - |
81 |
| -type convertingObjectInfo struct { |
82 |
| - rest.UpdatedObjectInfo |
83 |
| -} |
84 |
| - |
85 |
| -func (i convertingObjectInfo) UpdatedObject(ctx apirequest.Context, old runtime.Object) (runtime.Object, error) { |
86 |
| - oldObj := old.(*authorizationapi.Role) |
87 |
| - convertedOldObj := authorizationapi.ToClusterRole(oldObj) |
88 |
| - obj, err := i.UpdatedObjectInfo.UpdatedObject(ctx, convertedOldObj) |
| 92 | + role, err := rbacToClusterRole(ret) |
89 | 93 | if err != nil {
|
90 | 94 | return nil, err
|
91 | 95 | }
|
92 |
| - clusterObj := obj.(*authorizationapi.ClusterRole) |
93 |
| - convertedObj := authorizationapi.ToRole(clusterObj) |
94 |
| - return convertedObj, nil |
| 96 | + return &role, err |
95 | 97 | }
|
96 | 98 |
|
97 | 99 | func (s *ClusterRoleStorage) Update(ctx apirequest.Context, name string, objInfo rest.UpdatedObjectInfo) (runtime.Object, bool, error) {
|
98 |
| - ret, created, err := s.roleStorage.Update(ctx, name, convertingObjectInfo{objInfo}) |
99 |
| - if ret == nil { |
100 |
| - return nil, created, err |
| 100 | + old, err := s.client.Get(name, metav1.GetOptions{}) |
| 101 | + if err != nil { |
| 102 | + if apierrors.IsNotFound(err) { |
| 103 | + err = apierrors.NewNotFound(rbac.Resource("clusterrole"), name) |
| 104 | + } |
| 105 | + return nil, false, err |
101 | 106 | }
|
102 | 107 |
|
103 |
| - return authorizationapi.ToClusterRole(ret.(*authorizationapi.Role)), created, err |
104 |
| -} |
| 108 | + oldRole, err := rbacToClusterRole(old) |
| 109 | + if err != nil { |
| 110 | + return nil, false, err |
| 111 | + } |
105 | 112 |
|
106 |
| -func (m *ClusterRoleStorage) CreateClusterRoleWithEscalation(ctx apirequest.Context, obj *authorizationapi.ClusterRole) (*authorizationapi.ClusterRole, error) { |
107 |
| - in := authorizationapi.ToRole(obj) |
108 |
| - ret, err := m.roleStorage.CreateRoleWithEscalation(ctx, in) |
109 |
| - return authorizationapi.ToClusterRole(ret), err |
110 |
| -} |
| 113 | + obj, err := objInfo.UpdatedObject(ctx, &oldRole) |
| 114 | + if err != nil { |
| 115 | + return nil, false, err |
| 116 | + } |
| 117 | + |
| 118 | + updatedRole, err := rbacFromClusterRole(obj.(*authzapi.ClusterRole)) |
| 119 | + if err != nil { |
| 120 | + return nil, false, err |
| 121 | + } |
111 | 122 |
|
112 |
| -func (m *ClusterRoleStorage) UpdateClusterRoleWithEscalation(ctx apirequest.Context, obj *authorizationapi.ClusterRole) (*authorizationapi.ClusterRole, bool, error) { |
113 |
| - in := authorizationapi.ToRole(obj) |
114 |
| - ret, created, err := m.roleStorage.UpdateRoleWithEscalation(ctx, in) |
115 |
| - return authorizationapi.ToClusterRole(ret), created, err |
| 123 | + ret, err := s.client.Update(&updatedRole) |
| 124 | + if err != nil { |
| 125 | + return nil, false, err |
| 126 | + } |
| 127 | + |
| 128 | + role, err := rbacToClusterRole(ret) |
| 129 | + if err != nil { |
| 130 | + return nil, false, err |
| 131 | + } |
| 132 | + return &role, false, err |
116 | 133 | }
|
117 | 134 |
|
118 |
| -func (m *ClusterRoleStorage) CreateRoleWithEscalation(ctx apirequest.Context, obj *authorizationapi.Role) (*authorizationapi.Role, error) { |
119 |
| - return m.roleStorage.CreateRoleWithEscalation(ctx, obj) |
| 135 | +// FIXME: what's escalation exactly ? |
| 136 | +func (m *ClusterRoleStorage) CreateClusterRoleWithEscalation(ctx apirequest.Context, obj *authzapi.ClusterRole) (*authzapi.ClusterRole, error) { |
| 137 | + ret, err := m.Create(ctx, obj) |
| 138 | + if err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + return ret.(*authzapi.ClusterRole), err |
120 | 142 | }
|
121 | 143 |
|
122 |
| -func (m *ClusterRoleStorage) UpdateRoleWithEscalation(ctx apirequest.Context, obj *authorizationapi.Role) (*authorizationapi.Role, bool, error) { |
123 |
| - return m.roleStorage.UpdateRoleWithEscalation(ctx, obj) |
| 144 | +func (m *ClusterRoleStorage) UpdateClusterRoleWithEscalation(ctx apirequest.Context, obj *authzapi.ClusterRole) (*authzapi.ClusterRole, bool, error) { |
| 145 | + ret, ignored, err := m.Update(ctx, obj.Name, rest.DefaultUpdatedObjectInfo(obj, api.Scheme)) |
| 146 | + if err != nil { |
| 147 | + return nil, false, err |
| 148 | + } |
| 149 | + return ret.(*authzapi.ClusterRole), ignored, err |
124 | 150 | }
|
0 commit comments