forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
169 lines (141 loc) · 4.24 KB
/
proxy.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
package role
import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metainternal "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"
restclient "k8s.io/client-go/rest"
rbacinternalversion "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/rbac/internalversion"
authclient "github.com/openshift/origin/pkg/auth/client"
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
"github.com/openshift/origin/pkg/authorization/registry/util"
utilregistry "github.com/openshift/origin/pkg/util/registry"
)
type REST struct {
privilegedClient restclient.Interface
}
var _ rest.Lister = &REST{}
var _ rest.Getter = &REST{}
var _ rest.CreaterUpdater = &REST{}
var _ rest.GracefulDeleter = &REST{}
func NewREST(client restclient.Interface) utilregistry.NoWatchStorage {
return utilregistry.WrapNoWatchStorageError(&REST{privilegedClient: client})
}
func (s *REST) New() runtime.Object {
return &authorizationapi.Role{}
}
func (s *REST) NewList() runtime.Object {
return &authorizationapi.RoleList{}
}
func (s *REST) List(ctx apirequest.Context, options *metainternal.ListOptions) (runtime.Object, error) {
client, err := s.getImpersonatingClient(ctx)
if err != nil {
return nil, err
}
optv1 := metav1.ListOptions{}
if err := metainternal.Convert_internalversion_ListOptions_To_v1_ListOptions(options, &optv1, nil); err != nil {
return nil, err
}
roles, err := client.List(optv1)
if err != nil {
return nil, err
}
ret := &authorizationapi.RoleList{}
for _, curr := range roles.Items {
role, err := util.RoleFromRBAC(&curr)
if err != nil {
return nil, err
}
ret.Items = append(ret.Items, *role)
}
ret.ListMeta.ResourceVersion = roles.ResourceVersion
return ret, nil
}
func (s *REST) Get(ctx apirequest.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
client, err := s.getImpersonatingClient(ctx)
if err != nil {
return nil, err
}
ret, err := client.Get(name, *options)
if err != nil {
return nil, err
}
role, err := util.RoleFromRBAC(ret)
if err != nil {
return nil, err
}
return role, nil
}
func (s *REST) Delete(ctx apirequest.Context, name string, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
client, err := s.getImpersonatingClient(ctx)
if err != nil {
return nil, false, err
}
if err := client.Delete(name, options); err != nil {
return nil, false, err
}
return &metav1.Status{Status: metav1.StatusSuccess}, true, nil
}
func (s *REST) Create(ctx apirequest.Context, obj runtime.Object, _ bool) (runtime.Object, error) {
client, err := s.getImpersonatingClient(ctx)
if err != nil {
return nil, err
}
convertedObj, err := util.RoleToRBAC(obj.(*authorizationapi.Role))
if err != nil {
return nil, err
}
ret, err := client.Create(convertedObj)
if err != nil {
return nil, err
}
role, err := util.RoleFromRBAC(ret)
if err != nil {
return nil, err
}
return role, nil
}
func (s *REST) Update(ctx apirequest.Context, name string, objInfo rest.UpdatedObjectInfo) (runtime.Object, bool, error) {
client, err := s.getImpersonatingClient(ctx)
if err != nil {
return nil, false, err
}
old, err := client.Get(name, metav1.GetOptions{})
if err != nil {
return nil, false, err
}
oldRole, err := util.RoleFromRBAC(old)
if err != nil {
return nil, false, err
}
obj, err := objInfo.UpdatedObject(ctx, oldRole)
if err != nil {
return nil, false, err
}
updatedRole, err := util.RoleToRBAC(obj.(*authorizationapi.Role))
if err != nil {
return nil, false, err
}
ret, err := client.Update(updatedRole)
if err != nil {
return nil, false, err
}
role, err := util.RoleFromRBAC(ret)
if err != nil {
return nil, false, err
}
return role, false, err
}
func (s *REST) getImpersonatingClient(ctx apirequest.Context) (rbacinternalversion.RoleInterface, error) {
namespace, ok := apirequest.NamespaceFrom(ctx)
if !ok {
return nil, apierrors.NewBadRequest("namespace parameter required")
}
rbacClient, err := authclient.NewImpersonatingRBACFromContext(ctx, s.privilegedClient)
if err != nil {
return nil, err
}
return rbacClient.Roles(namespace), nil
}