2019-06-18 11:47:45 -07:00
|
|
|
package webhooks
|
|
|
|
|
|
|
|
import (
|
2019-07-15 16:07:56 -07:00
|
|
|
"fmt"
|
2019-06-18 11:47:45 -07:00
|
|
|
"strings"
|
2019-06-19 14:05:23 -07:00
|
|
|
|
2020-03-17 11:05:20 -07:00
|
|
|
"github.com/go-logr/logr"
|
2021-10-29 18:13:20 +02:00
|
|
|
kyverno "github.com/kyverno/kyverno/api/kyverno/v1"
|
2022-04-25 20:20:40 +08:00
|
|
|
urkyverno "github.com/kyverno/kyverno/api/kyverno/v1beta1"
|
2022-03-28 16:01:27 +02:00
|
|
|
"github.com/kyverno/kyverno/pkg/autogen"
|
2022-03-31 17:34:10 +02:00
|
|
|
enginectx "github.com/kyverno/kyverno/pkg/engine/context"
|
2020-10-07 11:12:31 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/response"
|
|
|
|
engineutils "github.com/kyverno/kyverno/pkg/engine/utils"
|
2022-04-05 14:30:00 +02:00
|
|
|
engineutils2 "github.com/kyverno/kyverno/pkg/utils/engine"
|
2022-03-31 17:34:10 +02:00
|
|
|
"github.com/pkg/errors"
|
2020-04-22 20:45:15 +05:30
|
|
|
yamlv2 "gopkg.in/yaml.v2"
|
2022-04-06 22:43:07 +02:00
|
|
|
admissionv1 "k8s.io/api/admission/v1"
|
2019-06-18 11:47:45 -07:00
|
|
|
)
|
|
|
|
|
2019-09-06 10:18:45 -07:00
|
|
|
// returns true -> if there is even one policy that blocks resource request
|
2019-08-23 18:34:23 -07:00
|
|
|
// returns false -> if all the policies are meant to report only, we dont block resource request
|
2020-12-23 15:10:07 -08:00
|
|
|
func toBlockResource(engineReponses []*response.EngineResponse, log logr.Logger) bool {
|
2019-08-23 18:34:23 -07:00
|
|
|
for _, er := range engineReponses {
|
2022-04-05 14:30:00 +02:00
|
|
|
if engineutils2.CheckEngineResponse(er) {
|
2021-06-30 00:43:11 +03:00
|
|
|
log.Info("spec.ValidationFailureAction set to enforce blocking resource request", "policy", er.PolicyResponse.Policy.Name)
|
2019-08-23 18:34:23 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2021-07-09 18:01:46 -07:00
|
|
|
|
2020-11-25 00:21:51 -08:00
|
|
|
log.V(4).Info("spec.ValidationFailureAction set to audit for all applicable policies, won't block resource operation")
|
2019-08-23 18:34:23 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-01-16 11:57:28 -08:00
|
|
|
// getEnforceFailureErrorMsg gets the error messages for failed enforce policy
|
2020-12-23 15:10:07 -08:00
|
|
|
func getEnforceFailureErrorMsg(engineResponses []*response.EngineResponse) string {
|
2020-03-06 17:11:33 +05:30
|
|
|
policyToRule := make(map[string]interface{})
|
|
|
|
var resourceName string
|
2020-03-16 14:08:13 +05:30
|
|
|
for _, er := range engineResponses {
|
2022-04-05 14:30:00 +02:00
|
|
|
if engineutils2.CheckEngineResponse(er) {
|
2020-03-06 17:11:33 +05:30
|
|
|
ruleToReason := make(map[string]string)
|
2020-01-16 11:57:28 -08:00
|
|
|
for _, rule := range er.PolicyResponse.Rules {
|
2021-09-26 02:12:31 -07:00
|
|
|
if rule.Status != response.RuleStatusPass {
|
2020-03-06 17:11:33 +05:30
|
|
|
ruleToReason[rule.Name] = rule.Message
|
2020-01-16 11:57:28 -08:00
|
|
|
}
|
|
|
|
}
|
2021-07-09 18:01:46 -07:00
|
|
|
resourceName = fmt.Sprintf("%s/%s/%s", er.PolicyResponse.Resource.Kind, er.PolicyResponse.Resource.Namespace, er.PolicyResponse.Resource.Name)
|
2021-06-30 00:43:11 +03:00
|
|
|
policyToRule[er.PolicyResponse.Policy.Name] = ruleToReason
|
2020-01-16 11:57:28 -08:00
|
|
|
}
|
|
|
|
}
|
2020-03-06 17:11:33 +05:30
|
|
|
result, _ := yamlv2.Marshal(policyToRule)
|
|
|
|
return "\n\nresource " + resourceName + " was blocked due to the following policies\n\n" + string(result)
|
2020-01-16 11:57:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// getErrorMsg gets all failed engine response message
|
2020-12-23 15:10:07 -08:00
|
|
|
func getErrorMsg(engineReponses []*response.EngineResponse) string {
|
2019-08-23 18:34:23 -07:00
|
|
|
var str []string
|
2019-11-06 17:14:32 -08:00
|
|
|
var resourceInfo string
|
2019-08-23 18:34:23 -07:00
|
|
|
for _, er := range engineReponses {
|
2020-06-30 11:53:27 -07:00
|
|
|
if !er.IsSuccessful() {
|
2019-11-06 17:14:32 -08:00
|
|
|
// resource in engineReponses is identical as this was called per admission request
|
|
|
|
resourceInfo = fmt.Sprintf("%s/%s/%s", er.PolicyResponse.Resource.Kind, er.PolicyResponse.Resource.Namespace, er.PolicyResponse.Resource.Name)
|
2021-06-30 00:43:11 +03:00
|
|
|
str = append(str, fmt.Sprintf("failed policy %s:", er.PolicyResponse.Policy.Name))
|
2019-08-23 18:34:23 -07:00
|
|
|
for _, rule := range er.PolicyResponse.Rules {
|
2021-09-26 02:12:31 -07:00
|
|
|
if rule.Status != response.RuleStatusPass {
|
2019-08-23 18:34:23 -07:00
|
|
|
str = append(str, rule.ToString())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-06 17:07:11 -08:00
|
|
|
return fmt.Sprintf("Resource %s %s", resourceInfo, strings.Join(str, ";"))
|
2019-08-23 18:34:23 -07:00
|
|
|
}
|
|
|
|
|
2022-03-31 17:34:10 +02:00
|
|
|
// patchRequest applies patches to the request.Object and returns a new copy of the request
|
2022-04-06 22:43:07 +02:00
|
|
|
func patchRequest(patches []byte, request *admissionv1.AdmissionRequest, logger logr.Logger) *admissionv1.AdmissionRequest {
|
2022-03-31 17:34:10 +02:00
|
|
|
patchedResource := processResourceWithPatches(patches, request.Object.Raw, logger)
|
|
|
|
newRequest := request.DeepCopy()
|
|
|
|
newRequest.Object.Raw = patchedResource
|
|
|
|
return newRequest
|
|
|
|
}
|
|
|
|
|
2020-03-17 11:05:20 -07:00
|
|
|
func processResourceWithPatches(patch []byte, resource []byte, log logr.Logger) []byte {
|
2019-08-23 18:34:23 -07:00
|
|
|
if patch == nil {
|
2020-01-13 10:15:52 -08:00
|
|
|
return resource
|
2019-08-23 18:34:23 -07:00
|
|
|
}
|
2019-10-07 18:31:14 -07:00
|
|
|
|
2020-01-07 17:06:17 -08:00
|
|
|
resource, err := engineutils.ApplyPatchNew(resource, patch)
|
2019-08-23 18:34:23 -07:00
|
|
|
if err != nil {
|
2021-01-19 11:08:06 -08:00
|
|
|
log.Error(err, "failed to patch resource:", "patch", string(patch), "resource", string(resource))
|
2019-08-23 18:34:23 -07:00
|
|
|
return nil
|
|
|
|
}
|
2021-07-09 18:01:46 -07:00
|
|
|
|
|
|
|
log.V(6).Info("", "patchedResource", string(resource))
|
2019-08-23 18:34:23 -07:00
|
|
|
return resource
|
|
|
|
}
|
2019-11-11 14:52:09 -08:00
|
|
|
|
2022-03-30 16:28:09 +02:00
|
|
|
func containsRBACInfo(policies ...[]kyverno.PolicyInterface) bool {
|
2020-07-02 12:49:10 -07:00
|
|
|
for _, policySlice := range policies {
|
|
|
|
for _, policy := range policySlice {
|
2022-03-28 16:01:27 +02:00
|
|
|
for _, rule := range autogen.ComputeRules(policy) {
|
2022-01-20 13:59:16 +05:30
|
|
|
if checkForRBACInfo(rule) {
|
|
|
|
return true
|
|
|
|
}
|
2022-01-05 22:38:24 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkForRBACInfo(rule kyverno.Rule) bool {
|
|
|
|
if len(rule.MatchResources.Roles) > 0 || len(rule.MatchResources.ClusterRoles) > 0 || len(rule.ExcludeResources.Roles) > 0 || len(rule.ExcludeResources.ClusterRoles) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if len(rule.MatchResources.All) > 0 {
|
|
|
|
for _, rf := range rule.MatchResources.All {
|
|
|
|
if len(rf.UserInfo.Roles) > 0 || len(rf.UserInfo.ClusterRoles) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(rule.MatchResources.Any) > 0 {
|
|
|
|
for _, rf := range rule.MatchResources.Any {
|
|
|
|
if len(rf.UserInfo.Roles) > 0 || len(rf.UserInfo.ClusterRoles) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(rule.ExcludeResources.All) > 0 {
|
|
|
|
for _, rf := range rule.ExcludeResources.All {
|
|
|
|
if len(rf.UserInfo.Roles) > 0 || len(rf.UserInfo.ClusterRoles) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(rule.ExcludeResources.Any) > 0 {
|
|
|
|
for _, rf := range rule.ExcludeResources.Any {
|
|
|
|
if len(rf.UserInfo.Roles) > 0 || len(rf.UserInfo.ClusterRoles) > 0 {
|
|
|
|
return true
|
2019-11-11 14:52:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2019-11-13 13:13:07 -08:00
|
|
|
|
2020-05-18 20:01:20 -07:00
|
|
|
func excludeKyvernoResources(kind string) bool {
|
|
|
|
switch kind {
|
2020-12-01 22:50:40 -08:00
|
|
|
case "ClusterPolicyReport":
|
|
|
|
return true
|
|
|
|
case "PolicyReport":
|
|
|
|
return true
|
|
|
|
case "ReportChangeRequest":
|
|
|
|
return true
|
|
|
|
case "GenerateRequest":
|
|
|
|
return true
|
|
|
|
case "ClusterReportChangeRequest":
|
2020-05-18 20:01:20 -07:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 17:34:10 +02:00
|
|
|
|
2022-04-25 20:20:40 +08:00
|
|
|
func newVariablesContext(request *admissionv1.AdmissionRequest, userRequestInfo *urkyverno.RequestInfo) (enginectx.Interface, error) {
|
2022-03-31 17:34:10 +02:00
|
|
|
ctx := enginectx.NewContext()
|
|
|
|
if err := ctx.AddRequest(request); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to load incoming request in context")
|
|
|
|
}
|
|
|
|
if err := ctx.AddUserInfo(*userRequestInfo); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to load userInfo in context")
|
|
|
|
}
|
|
|
|
if err := ctx.AddServiceAccount(userRequestInfo.AdmissionUserInfo.Username); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to load service account in context")
|
|
|
|
}
|
|
|
|
return ctx, nil
|
|
|
|
}
|