2023-03-22 21:14:57 +08:00
|
|
|
package mutate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/kyverno/kyverno/pkg/auth"
|
|
|
|
"github.com/kyverno/kyverno/pkg/clients/dclient"
|
|
|
|
)
|
|
|
|
|
|
|
|
type authChecker struct {
|
|
|
|
client dclient.Interface
|
2023-04-24 18:31:42 +08:00
|
|
|
user string
|
2023-03-22 21:14:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type AuthChecker interface {
|
|
|
|
CanICreate(ctx context.Context, kind, namespace, subresource string) (bool, error)
|
|
|
|
CanIUpdate(ctx context.Context, kind, namespace, subresource string) (bool, error)
|
|
|
|
CanIGet(ctx context.Context, kind, namespace, subresource string) (bool, error)
|
|
|
|
}
|
|
|
|
|
2023-04-24 18:31:42 +08:00
|
|
|
func newAuthChecker(client dclient.Interface, user string) AuthChecker {
|
|
|
|
return &authChecker{client: client, user: user}
|
2023-03-22 21:14:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *authChecker) CanICreate(ctx context.Context, kind, namespace, subresource string) (bool, error) {
|
2023-04-24 18:31:42 +08:00
|
|
|
checker := auth.NewCanI(a.client.Discovery(), a.client.GetKubeClient().AuthorizationV1().SubjectAccessReviews(), kind, namespace, "create", subresource, a.user)
|
2023-03-22 21:14:57 +08:00
|
|
|
return checker.RunAccessCheck(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *authChecker) CanIUpdate(ctx context.Context, kind, namespace, subresource string) (bool, error) {
|
2023-04-24 18:31:42 +08:00
|
|
|
checker := auth.NewCanI(a.client.Discovery(), a.client.GetKubeClient().AuthorizationV1().SubjectAccessReviews(), kind, namespace, "update", subresource, a.user)
|
2023-03-22 21:14:57 +08:00
|
|
|
return checker.RunAccessCheck(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *authChecker) CanIGet(ctx context.Context, kind, namespace, subresource string) (bool, error) {
|
2023-04-24 18:31:42 +08:00
|
|
|
checker := auth.NewCanI(a.client.Discovery(), a.client.GetKubeClient().AuthorizationV1().SubjectAccessReviews(), kind, namespace, "get", subresource, a.user)
|
2023-03-22 21:14:57 +08:00
|
|
|
return checker.RunAccessCheck(ctx)
|
|
|
|
}
|