IAM: Use the new way to authorize resources (#116061)
* Use name for authz for User, SA, Team * Use VerbList
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
legacyiamv0 "github.com/grafana/grafana/pkg/apis/iam/v0alpha1"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
||||
"github.com/grafana/grafana/pkg/services/team"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
)
|
||||
|
||||
// OptonalFormatInt formats num as a string. If num is less or equal than 0
|
||||
@@ -39,23 +40,17 @@ func MapUserTeamPermission(p team.PermissionType) legacyiamv0.TeamPermission {
|
||||
}
|
||||
}
|
||||
|
||||
// Resource is required to be implemented for list return types so we can
|
||||
// perform authorization.
|
||||
type Resource interface {
|
||||
AuthID() string
|
||||
}
|
||||
|
||||
type ListResponse[T Resource] struct {
|
||||
type ListResponse[T metav1.Object] struct {
|
||||
Items []T
|
||||
RV int64
|
||||
Continue int64
|
||||
}
|
||||
|
||||
type ListFunc[T Resource] func(ctx context.Context, ns authlib.NamespaceInfo, p Pagination) (*ListResponse[T], error)
|
||||
type ListFunc[T metav1.Object] func(ctx context.Context, ns authlib.NamespaceInfo, p Pagination) (*ListResponse[T], error)
|
||||
|
||||
// List is a helper function that will perform access check on resources if
|
||||
// prvovided with a authlib.AccessClient.
|
||||
func List[T Resource](
|
||||
func List[T metav1.Object](
|
||||
ctx context.Context,
|
||||
resource utils.ResourceInfo,
|
||||
ac authlib.AccessClient,
|
||||
@@ -78,7 +73,7 @@ func List[T Resource](
|
||||
check, _, err = ac.Compile(ctx, ident, authlib.ListRequest{
|
||||
Resource: resource.GroupResource().Resource,
|
||||
Group: resource.GroupResource().Group,
|
||||
Verb: "list",
|
||||
Verb: utils.VerbList,
|
||||
Namespace: ns.Value,
|
||||
})
|
||||
|
||||
@@ -95,7 +90,7 @@ func List[T Resource](
|
||||
}
|
||||
|
||||
for _, item := range first.Items {
|
||||
if !check(item.AuthID(), "") {
|
||||
if !check(item.GetName(), "") {
|
||||
continue
|
||||
}
|
||||
res.Items = append(res.Items, item)
|
||||
@@ -118,7 +113,7 @@ outer:
|
||||
break outer
|
||||
}
|
||||
|
||||
if !check(item.AuthID(), "") {
|
||||
if !check(item.GetName(), "") {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user