2020-03-12 01:14:23 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2022-11-29 13:59:40 +00:00
|
|
|
"context"
|
2020-03-12 01:14:23 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-03-10 07:15:48 +00:00
|
|
|
kubeutils "github.com/kyverno/kyverno/pkg/utils/kube"
|
2020-03-12 01:14:23 +00:00
|
|
|
authorizationv1 "k8s.io/api/authorization/v1"
|
2022-12-21 17:12:26 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2020-03-12 01:14:23 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2022-12-22 05:24:37 +00:00
|
|
|
authorizationv1client "k8s.io/client-go/kubernetes/typed/authorization/v1"
|
2020-03-12 01:14:23 +00:00
|
|
|
)
|
|
|
|
|
2022-12-22 05:24:37 +00:00
|
|
|
// Discovery provides interface to mange Kind and GVR mapping
|
|
|
|
type Discovery interface {
|
2023-03-10 07:15:48 +00:00
|
|
|
GetGVRFromGVK(schema.GroupVersionKind) (schema.GroupVersionResource, error)
|
2022-12-22 05:24:37 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 06:19:03 +00:00
|
|
|
// CanIOptions provides utility to check if user has authorization for the given operation
|
2022-09-07 06:54:34 +00:00
|
|
|
type CanIOptions interface {
|
|
|
|
// RunAccessCheck checks if the caller can perform the operation
|
|
|
|
// - operation is a combination of namespace, kind, verb
|
|
|
|
// - can only evaluate a single verb
|
|
|
|
// - group version resource is determined from the kind using the discovery client REST mapper
|
|
|
|
// - If disallowed, the reason and evaluationError is available in the logs
|
|
|
|
// - each can generates a SelfSubjectAccessReview resource and response is evaluated for permissions
|
2022-11-29 13:59:40 +00:00
|
|
|
RunAccessCheck(context.Context) (bool, error)
|
2022-09-07 06:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type canIOptions struct {
|
2022-12-21 17:12:26 +00:00
|
|
|
namespace string
|
|
|
|
verb string
|
|
|
|
kind string
|
|
|
|
subresource string
|
2022-12-22 05:24:37 +00:00
|
|
|
discovery Discovery
|
|
|
|
ssarClient authorizationv1client.SelfSubjectAccessReviewInterface
|
2020-03-12 01:14:23 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 06:19:03 +00:00
|
|
|
// NewCanI returns a new instance of operation access controller evaluator
|
2022-12-22 05:24:37 +00:00
|
|
|
func NewCanI(discovery Discovery, ssarClient authorizationv1client.SelfSubjectAccessReviewInterface, kind, namespace, verb, subresource string) CanIOptions {
|
2022-09-07 06:54:34 +00:00
|
|
|
return &canIOptions{
|
2022-12-21 17:12:26 +00:00
|
|
|
namespace: namespace,
|
|
|
|
verb: verb,
|
|
|
|
kind: kind,
|
|
|
|
subresource: subresource,
|
2022-12-22 05:24:37 +00:00
|
|
|
discovery: discovery,
|
|
|
|
ssarClient: ssarClient,
|
2020-03-12 01:14:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 06:19:03 +00:00
|
|
|
// RunAccessCheck checks if the caller can perform the operation
|
2020-03-12 01:14:23 +00:00
|
|
|
// - operation is a combination of namespace, kind, verb
|
|
|
|
// - can only evaluate a single verb
|
|
|
|
// - group version resource is determined from the kind using the discovery client REST mapper
|
2020-08-30 08:43:20 +00:00
|
|
|
// - If disallowed, the reason and evaluationError is available in the logs
|
2020-03-12 01:14:23 +00:00
|
|
|
// - each can generates a SelfSubjectAccessReview resource and response is evaluated for permissions
|
2022-11-29 13:59:40 +00:00
|
|
|
func (o *canIOptions) RunAccessCheck(ctx context.Context) (bool, error) {
|
2020-03-12 01:14:23 +00:00
|
|
|
// get GroupVersionResource from RESTMapper
|
|
|
|
// get GVR from kind
|
2023-03-10 07:15:48 +00:00
|
|
|
apiVersion, kind := kubeutils.GetKindFromGVK(o.kind)
|
|
|
|
gv, err := schema.ParseGroupVersion(apiVersion)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to parse group/version %s", apiVersion)
|
|
|
|
}
|
|
|
|
gvr, err := o.discovery.GetGVRFromGVK(gv.WithKind(kind))
|
2020-12-04 03:19:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to get GVR for kind %s", o.kind)
|
|
|
|
}
|
|
|
|
|
2023-03-22 04:46:35 +00:00
|
|
|
if gvr.Empty() {
|
2020-03-12 01:14:23 +00:00
|
|
|
// cannot find GVR
|
|
|
|
return false, fmt.Errorf("failed to get the Group Version Resource for kind %s", o.kind)
|
|
|
|
}
|
|
|
|
|
2020-03-18 01:34:44 +00:00
|
|
|
sar := &authorizationv1.SelfSubjectAccessReview{
|
2020-03-12 01:14:23 +00:00
|
|
|
Spec: authorizationv1.SelfSubjectAccessReviewSpec{
|
|
|
|
ResourceAttributes: &authorizationv1.ResourceAttributes{
|
2022-12-21 17:12:26 +00:00
|
|
|
Namespace: o.namespace,
|
|
|
|
Verb: o.verb,
|
|
|
|
Group: gvr.Group,
|
|
|
|
Resource: gvr.Resource,
|
|
|
|
Subresource: o.subresource,
|
2020-03-12 01:14:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// Set self subject access review
|
|
|
|
// - namespace
|
|
|
|
// - verb
|
|
|
|
// - resource
|
|
|
|
// - subresource
|
2022-04-27 13:34:08 +00:00
|
|
|
logger := logger.WithValues("kind", sar.Kind, "namespace", sar.Namespace, "name", sar.Name)
|
2020-03-12 01:14:23 +00:00
|
|
|
|
|
|
|
// Create the Resource
|
2022-12-22 05:24:37 +00:00
|
|
|
resp, err := o.ssarClient.Create(ctx, sar, metav1.CreateOptions{})
|
2020-03-12 01:14:23 +00:00
|
|
|
if err != nil {
|
2020-03-17 18:05:20 +00:00
|
|
|
logger.Error(err, "failed to create resource")
|
2020-03-12 01:14:23 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2022-12-21 17:12:26 +00:00
|
|
|
if !resp.Status.Allowed {
|
|
|
|
reason := resp.Status.Reason
|
|
|
|
evaluationError := resp.Status.EvaluationError
|
2020-03-12 01:14:23 +00:00
|
|
|
// Reporting ? (just logs)
|
2020-03-17 18:05:20 +00:00
|
|
|
logger.Info("disallowed operation", "reason", reason, "evaluationError", evaluationError)
|
2020-03-12 01:14:23 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 17:12:26 +00:00
|
|
|
return resp.Status.Allowed, nil
|
2020-03-12 01:14:23 +00:00
|
|
|
}
|