2020-03-12 01:14:23 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
|
2020-10-07 18:12:31 +00:00
|
|
|
client "github.com/kyverno/kyverno/pkg/dclient"
|
2020-03-12 01:14:23 +00:00
|
|
|
authorizationv1 "k8s.io/api/authorization/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
)
|
|
|
|
|
2020-08-30 08:43:20 +00:00
|
|
|
//CanIOptions provides utility to check if user has authorization for the given operation
|
2020-03-12 01:14:23 +00:00
|
|
|
type CanIOptions struct {
|
|
|
|
namespace string
|
|
|
|
verb string
|
|
|
|
kind string
|
2022-05-03 05:30:04 +00:00
|
|
|
client client.Interface
|
2020-03-12 01:14:23 +00:00
|
|
|
}
|
|
|
|
|
2020-08-30 08:43:20 +00:00
|
|
|
//NewCanI returns a new instance of operation access controller evaluator
|
2022-05-03 05:30:04 +00:00
|
|
|
func NewCanI(client client.Interface, kind, namespace, verb string) *CanIOptions {
|
2022-04-27 13:34:08 +00:00
|
|
|
return &CanIOptions{
|
|
|
|
namespace: namespace,
|
|
|
|
kind: kind,
|
|
|
|
verb: verb,
|
|
|
|
client: client,
|
2020-03-12 01:14:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//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
|
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
|
|
|
|
func (o *CanIOptions) RunAccessCheck() (bool, error) {
|
|
|
|
// get GroupVersionResource from RESTMapper
|
|
|
|
// get GVR from kind
|
2022-05-03 05:30:04 +00:00
|
|
|
gvr, err := o.client.Discovery().GetGVRFromKind(o.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)
|
|
|
|
}
|
|
|
|
|
2020-03-12 01:14:23 +00:00
|
|
|
if reflect.DeepEqual(gvr, schema.GroupVersionResource{}) {
|
|
|
|
// 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{
|
|
|
|
Namespace: o.namespace,
|
|
|
|
Verb: o.verb,
|
|
|
|
Group: gvr.Group,
|
|
|
|
Resource: gvr.Resource,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// 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
|
2020-08-07 04:17:33 +00:00
|
|
|
resp, err := o.client.CreateResource("", "SelfSubjectAccessReview", "", sar, false)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// status.allowed
|
|
|
|
allowed, ok, err := unstructured.NestedBool(resp.Object, "status", "allowed")
|
|
|
|
if !ok {
|
|
|
|
if err != nil {
|
2020-03-17 18:05:20 +00:00
|
|
|
logger.Error(err, "failed to get the field", "field", "status.allowed")
|
2020-03-12 01:14:23 +00:00
|
|
|
}
|
2020-03-17 18:05:20 +00:00
|
|
|
logger.Info("field not found", "field", "status.allowed")
|
2020-03-12 01:14:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !allowed {
|
|
|
|
// status.reason
|
|
|
|
reason, ok, err := unstructured.NestedString(resp.Object, "status", "reason")
|
|
|
|
if !ok {
|
|
|
|
if err != nil {
|
2020-03-17 18:05:20 +00:00
|
|
|
logger.Error(err, "failed to get the field", "field", "status.reason")
|
2020-03-12 01:14:23 +00:00
|
|
|
}
|
2020-03-17 18:05:20 +00:00
|
|
|
logger.Info("field not found", "field", "status.reason")
|
2020-03-12 01:14:23 +00:00
|
|
|
}
|
|
|
|
// status.evaluationError
|
2020-12-04 03:19:36 +00:00
|
|
|
evaluationError, ok, err := unstructured.NestedString(resp.Object, "status", "evaluationError")
|
2020-03-12 01:14:23 +00:00
|
|
|
if !ok {
|
|
|
|
if err != nil {
|
2020-03-17 18:05:20 +00:00
|
|
|
logger.Error(err, "failed to get the field", "field", "status.evaluationError")
|
2020-03-12 01:14:23 +00:00
|
|
|
}
|
2020-03-17 18:05:20 +00:00
|
|
|
logger.Info("field not found", "field", "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
|
|
|
}
|
|
|
|
|
|
|
|
return allowed, nil
|
|
|
|
}
|