@@ -12,16 +12,13 @@ import (
12
12
13
13
kerrors "k8s.io/apimachinery/pkg/api/errors"
14
14
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15
- restclient "k8s.io/client-go/rest"
16
- kcoreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
15
+ authorizationapi "k8s.io/kubernetes/pkg/apis/authorization/v1"
17
16
18
- authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
19
- "github.com/openshift/origin/pkg/client"
20
- "github.com/openshift/origin/pkg/cmd/util/clientcmd"
21
17
imageapi "github.com/openshift/origin/pkg/image/apis/image"
22
18
"github.com/openshift/origin/pkg/util/httprequest"
23
19
24
20
"github.com/openshift/origin/pkg/dockerregistry/server/audit"
21
+ "github.com/openshift/origin/pkg/dockerregistry/server/client"
25
22
)
26
23
27
24
type deferredErrors map [string ]error
@@ -51,40 +48,6 @@ const (
51
48
AccessControllerOptionParams = "_params"
52
49
)
53
50
54
- // RegistryClient encapsulates getting access to the OpenShift API.
55
- type RegistryClient interface {
56
- // Clients return the authenticated clients to use with the server.
57
- Clients () (client.Interface , kcoreclient.CoreInterface , error )
58
- // SafeClientConfig returns a client config without authentication info.
59
- SafeClientConfig () restclient.Config
60
- }
61
-
62
- // registryClient implements RegistryClient
63
- type registryClient struct {
64
- config * clientcmd.Config
65
- }
66
-
67
- var _ RegistryClient = & registryClient {}
68
-
69
- // NewRegistryClient creates a registry client.
70
- func NewRegistryClient (config * clientcmd.Config ) RegistryClient {
71
- return & registryClient {config : config }
72
- }
73
-
74
- // Client returns the authenticated client to use with the server.
75
- func (r * registryClient ) Clients () (client.Interface , kcoreclient.CoreInterface , error ) {
76
- oc , kc , err := r .config .Clients ()
77
- if err != nil {
78
- return nil , nil , err
79
- }
80
- return oc , kc .Core (), err
81
- }
82
-
83
- // SafeClientConfig returns a client config without authentication info.
84
- func (r * registryClient ) SafeClientConfig () restclient.Config {
85
- return clientcmd .AnonymousClientConfig (r .config .OpenShiftConfig ())
86
- }
87
-
88
51
func init () {
89
52
registryauth .Register (OpenShiftAuth , registryauth .InitFunc (newAccessController ))
90
53
}
@@ -102,10 +65,10 @@ func WithUserInfoLogger(ctx context.Context, username, userid string) context.Co
102
65
}
103
66
104
67
type AccessController struct {
105
- realm string
106
- tokenRealm * url.URL
107
- config restclient. Config
108
- auditLog bool
68
+ realm string
69
+ tokenRealm * url.URL
70
+ registryClient client. RegistryClient
71
+ auditLog bool
109
72
}
110
73
111
74
var _ registryauth.AccessController = & AccessController {}
@@ -170,8 +133,8 @@ func TokenRealm(options map[string]interface{}) (*url.URL, error) {
170
133
171
134
// AccessControllerParams is the parameters for newAccessController
172
135
type AccessControllerParams struct {
173
- Logger context.Logger
174
- SafeClientConfig restclient. Config
136
+ Logger context.Logger
137
+ RegistryClient client. RegistryClient
175
138
}
176
139
177
140
func newAccessController (options map [string ]interface {}) (registryauth.AccessController , error ) {
@@ -193,9 +156,9 @@ func newAccessController(options map[string]interface{}) (registryauth.AccessCon
193
156
}
194
157
195
158
ac := & AccessController {
196
- realm : realm ,
197
- tokenRealm : tokenRealm ,
198
- config : params .SafeClientConfig ,
159
+ realm : realm ,
160
+ tokenRealm : tokenRealm ,
161
+ registryClient : params .RegistryClient ,
199
162
}
200
163
201
164
if audit , ok := options ["audit" ]; ok {
@@ -305,9 +268,7 @@ func (ac *AccessController) Authorized(ctx context.Context, accessRecords ...reg
305
268
return nil , ac .wrapErr (ctx , err )
306
269
}
307
270
308
- copied := ac .config
309
- copied .BearerToken = bearerToken
310
- osClient , err := client .New (& copied )
271
+ osClient , err := ac .registryClient .ClientFromToken (bearerToken )
311
272
if err != nil {
312
273
return nil , ac .wrapErr (ctx , err )
313
274
}
@@ -476,8 +437,8 @@ func getOpenShiftAPIToken(ctx context.Context, req *http.Request) (string, error
476
437
return token , nil
477
438
}
478
439
479
- func verifyOpenShiftUser (ctx context.Context , client client.UsersInterface ) (string , string , error ) {
480
- userInfo , err := client .Users ().Get ("~" , metav1.GetOptions {})
440
+ func verifyOpenShiftUser (ctx context.Context , c client.UsersInterfacer ) (string , string , error ) {
441
+ userInfo , err := c .Users ().Get ("~" , metav1.GetOptions {})
481
442
if err != nil {
482
443
context .GetLogger (ctx ).Errorf ("Get user failed with error: %s" , err )
483
444
if kerrors .IsUnauthorized (err ) || kerrors .IsForbidden (err ) {
@@ -489,17 +450,19 @@ func verifyOpenShiftUser(ctx context.Context, client client.UsersInterface) (str
489
450
return userInfo .GetName (), string (userInfo .GetUID ()), nil
490
451
}
491
452
492
- func verifyWithSAR (ctx context.Context , resource , namespace , name , verb string , client client.LocalSubjectAccessReviewsNamespacer ) error {
493
- sar := authorizationapi.LocalSubjectAccessReview {
494
- Action : authorizationapi.Action {
495
- Verb : verb ,
496
- Group : imageapi .GroupName ,
497
- Resource : resource ,
498
- ResourceName : name ,
453
+ func verifyWithSAR (ctx context.Context , resource , namespace , name , verb string , c client.SelfSubjectAccessReviewsNamespacer ) error {
454
+ sar := authorizationapi.SelfSubjectAccessReview {
455
+ Spec : authorizationapi.SelfSubjectAccessReviewSpec {
456
+ ResourceAttributes : & authorizationapi.ResourceAttributes {
457
+ Namespace : namespace ,
458
+ Verb : verb ,
459
+ Group : imageapi .GroupName ,
460
+ Resource : resource ,
461
+ Name : name ,
462
+ },
499
463
},
500
464
}
501
- response , err := client .LocalSubjectAccessReviews (namespace ).Create (& sar )
502
-
465
+ response , err := c .SelfSubjectAccessReviews ().Create (& sar )
503
466
if err != nil {
504
467
context .GetLogger (ctx ).Errorf ("OpenShift client error: %s" , err )
505
468
if kerrors .IsUnauthorized (err ) || kerrors .IsForbidden (err ) {
@@ -508,40 +471,42 @@ func verifyWithSAR(ctx context.Context, resource, namespace, name, verb string,
508
471
return err
509
472
}
510
473
511
- if ! response .Allowed {
512
- context .GetLogger (ctx ).Errorf ("OpenShift access denied: %s" , response .Reason )
474
+ if ! response .Status . Allowed {
475
+ context .GetLogger (ctx ).Errorf ("OpenShift access denied: %s" , response .Status . Reason )
513
476
return ErrOpenShiftAccessDenied
514
477
}
515
478
516
479
return nil
517
480
}
518
481
519
- func verifyImageStreamAccess (ctx context.Context , namespace , imageRepo , verb string , client client.LocalSubjectAccessReviewsNamespacer ) error {
520
- return verifyWithSAR (ctx , "imagestreams/layers" , namespace , imageRepo , verb , client )
482
+ func verifyImageStreamAccess (ctx context.Context , namespace , imageRepo , verb string , c client.SelfSubjectAccessReviewsNamespacer ) error {
483
+ return verifyWithSAR (ctx , "imagestreams/layers" , namespace , imageRepo , verb , c )
521
484
}
522
485
523
- func verifyImageSignatureAccess (ctx context.Context , namespace , imageRepo string , client client.LocalSubjectAccessReviewsNamespacer ) error {
524
- return verifyWithSAR (ctx , "imagesignatures" , namespace , imageRepo , "create" , client )
486
+ func verifyImageSignatureAccess (ctx context.Context , namespace , imageRepo string , c client.SelfSubjectAccessReviewsNamespacer ) error {
487
+ return verifyWithSAR (ctx , "imagesignatures" , namespace , imageRepo , "create" , c )
525
488
}
526
489
527
- func verifyPruneAccess (ctx context.Context , client client.SubjectAccessReviews ) error {
528
- sar := authorizationapi.SubjectAccessReview {
529
- Action : authorizationapi.Action {
530
- Verb : "delete" ,
531
- Group : imageapi .GroupName ,
532
- Resource : "images" ,
490
+ func verifyPruneAccess (ctx context.Context , c client.SelfSubjectAccessReviewsNamespacer ) error {
491
+ sar := authorizationapi.SelfSubjectAccessReview {
492
+ Spec : authorizationapi.SelfSubjectAccessReviewSpec {
493
+ ResourceAttributes : & authorizationapi.ResourceAttributes {
494
+ Verb : "delete" ,
495
+ Group : imageapi .GroupName ,
496
+ Resource : "images" ,
497
+ },
533
498
},
534
499
}
535
- response , err := client . SubjectAccessReviews ().Create (& sar )
500
+ response , err := c . SelfSubjectAccessReviews ().Create (& sar )
536
501
if err != nil {
537
502
context .GetLogger (ctx ).Errorf ("OpenShift client error: %s" , err )
538
503
if kerrors .IsUnauthorized (err ) || kerrors .IsForbidden (err ) {
539
504
return ErrOpenShiftAccessDenied
540
505
}
541
506
return err
542
507
}
543
- if ! response .Allowed {
544
- context .GetLogger (ctx ).Errorf ("OpenShift access denied: %s" , response .Reason )
508
+ if ! response .Status . Allowed {
509
+ context .GetLogger (ctx ).Errorf ("OpenShift access denied: %s" , response .Status . Reason )
545
510
return ErrOpenShiftAccessDenied
546
511
}
547
512
return nil
0 commit comments