2023-11-01 13:52:03 +00:00
|
|
|
package validatingadmissionpolicy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/kyverno/kyverno/pkg/auth/checker"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2024-04-17 10:01:00 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
2023-11-01 13:52:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func hasPermissions(resource schema.GroupVersionResource, s checker.AuthChecker) bool {
|
|
|
|
can, err := checker.Check(context.TODO(), s, resource.Group, resource.Version, resource.Resource, "", "", "create", "update", "list", "delete")
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return can
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasValidatingAdmissionPolicyPermission check if the admission controller has the required permissions to generate
|
|
|
|
// Kubernetes ValidatingAdmissionPolicy
|
|
|
|
func HasValidatingAdmissionPolicyPermission(s checker.AuthChecker) bool {
|
2024-08-29 15:31:25 +00:00
|
|
|
gvr := schema.GroupVersionResource{Group: "admissionregistration.k8s.io", Version: "v1beta1", Resource: "validatingadmissionpolicies"}
|
2023-11-01 13:52:03 +00:00
|
|
|
return hasPermissions(gvr, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasValidatingAdmissionPolicyBindingPermission check if the admission controller has the required permissions to generate
|
|
|
|
// Kubernetes ValidatingAdmissionPolicyBinding
|
|
|
|
func HasValidatingAdmissionPolicyBindingPermission(s checker.AuthChecker) bool {
|
2024-08-29 15:31:25 +00:00
|
|
|
gvr := schema.GroupVersionResource{Group: "admissionregistration.k8s.io", Version: "v1beta1", Resource: "validatingadmissionpolicybindings"}
|
2023-11-01 13:52:03 +00:00
|
|
|
return hasPermissions(gvr, s)
|
|
|
|
}
|
2024-04-17 10:01:00 +00:00
|
|
|
|
|
|
|
// IsValidatingAdmissionPolicyRegistered checks if ValidatingAdmissionPolicies are registered in the API Server
|
|
|
|
func IsValidatingAdmissionPolicyRegistered(kubeClient kubernetes.Interface) (bool, error) {
|
2024-08-29 15:31:25 +00:00
|
|
|
groupVersion := schema.GroupVersion{Group: "admissionregistration.k8s.io", Version: "v1beta1"}
|
2024-04-17 10:01:00 +00:00
|
|
|
if _, err := kubeClient.Discovery().ServerResourcesForGroupVersion(groupVersion.String()); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|