Skip to content

Commit 78df370

Browse files
authored
Use SelfSubjectAccessReview to check ODLM's permission on getting catalog (#1100)
Signed-off-by: Daniel Fan <[email protected]>
1 parent 1b6ff4c commit 78df370

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

controllers/operator/manager.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (m *ODLMOperator) GetCatalogSourceAndChannelFromPackage(ctx context.Context
174174
continue
175175
}
176176

177-
hasCatalogPermission := m.CheckResAuth(ctx, namespace, "operators.coreos.com", "catalogsources", "get")
177+
hasCatalogPermission := m.CheckResAuth(ctx, pm.Status.CatalogSourceNamespace, "operators.coreos.com", "catalogsources", "get")
178178
if !hasCatalogPermission {
179179
klog.V(2).Infof("No permission to get CatalogSource %s in the namespace %s", pm.Status.CatalogSource, pm.Status.CatalogSourceNamespace)
180180
continue
@@ -230,8 +230,8 @@ func (m *ODLMOperator) GetCatalogSourceAndChannelFromPackage(ctx context.Context
230230
}
231231

232232
func (m *ODLMOperator) CheckResAuth(ctx context.Context, namespace, group, resource, verb string) bool {
233-
sar := &authorizationv1.SubjectAccessReview{
234-
Spec: authorizationv1.SubjectAccessReviewSpec{
233+
sar := &authorizationv1.SelfSubjectAccessReview{
234+
Spec: authorizationv1.SelfSubjectAccessReviewSpec{
235235
ResourceAttributes: &authorizationv1.ResourceAttributes{
236236
Namespace: namespace,
237237
Group: group,
@@ -241,14 +241,13 @@ func (m *ODLMOperator) CheckResAuth(ctx context.Context, namespace, group, resou
241241
},
242242
}
243243
if err := m.Create(ctx, sar); err != nil {
244+
klog.Errorf("Failed to check operator permission for Kind %s in namespace %s: %v", resource, namespace, err)
244245
return false
245246
}
246247

247-
if !sar.Status.Allowed {
248-
return false
249-
}
248+
klog.V(2).Infof("Operator %s permission in namespace %s for Kind: %s, Allowed: %t, Denied: %t, Reason: %s", verb, namespace, resource, sar.Status.Allowed, sar.Status.Denied, sar.Status.Reason)
250249

251-
return true
250+
return sar.Status.Allowed
252251
}
253252

254253
func channelCheck(channelName string, channelList []operatorsv1.PackageChannel) bool {

controllers/operator/manager_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ func TestGetCatalogSourceAndChannelFromPackage(t *testing.T) {
313313
}
314314

315315
mockClient.mock.On("Create", ctx, mock.Anything, mock.Anything).Return(nil).Run(func(args mock.Arguments) {
316-
sar := args.Get(1).(*authorizationv1.SubjectAccessReview)
316+
sar := args.Get(1).(*authorizationv1.SelfSubjectAccessReview)
317317
sar.Status.Allowed = true
318318
})
319319

@@ -434,20 +434,20 @@ func TestCheckResAuth(t *testing.T) {
434434
resource := "test-resource"
435435
verb := "get"
436436

437-
// Test when SubjectAccessReview is allowed
437+
// Test when SelfSubjectAccessReview is allowed
438438
mockClient.mock.On("Create", ctx, mock.Anything, mock.Anything).Return(nil).Run(func(args mock.Arguments) {
439-
sar := args.Get(1).(*authorizationv1.SubjectAccessReview)
439+
sar := args.Get(1).(*authorizationv1.SelfSubjectAccessReview)
440440
sar.Status.Allowed = true
441441
})
442442

443443
if !operator.CheckResAuth(ctx, namespace, group, resource, verb) {
444444
t.Errorf("Expected CheckResAuth to return true, but got false")
445445
}
446446

447-
// Test when SubjectAccessReview is not allowed
447+
// Test when SelfSubjectAccessReview is not allowed
448448
mockClient.mock.ExpectedCalls = nil
449449
mockClient.mock.On("Create", ctx, mock.Anything, mock.Anything).Return(nil).Run(func(args mock.Arguments) {
450-
sar := args.Get(1).(*authorizationv1.SubjectAccessReview)
450+
sar := args.Get(1).(*authorizationv1.SelfSubjectAccessReview)
451451
sar.Status.Allowed = false
452452
})
453453

0 commit comments

Comments
 (0)