1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-15 17:51:20 +00:00
kyverno/pkg/webhooks/common.go

171 lines
5.5 KiB
Go
Raw Normal View History

2019-06-18 18:47:45 +00:00
package webhooks
import (
2019-07-15 23:07:56 +00:00
"fmt"
2019-06-18 18:47:45 +00:00
"strings"
2019-06-19 21:05:23 +00:00
2020-03-06 11:41:33 +00:00
yamlv2 "gopkg.in/yaml.v2"
2019-07-19 19:47:20 +00:00
"github.com/golang/glog"
2019-11-13 21:41:08 +00:00
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
"github.com/nirmata/kyverno/pkg/engine/response"
2020-01-11 13:03:11 +00:00
engineutils "github.com/nirmata/kyverno/pkg/engine/utils"
"k8s.io/api/admission/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
2019-06-18 18:47:45 +00:00
)
// isResponseSuccesful return true if all responses are successful
func isResponseSuccesful(engineReponses []response.EngineResponse) bool {
2019-08-24 01:34:23 +00:00
for _, er := range engineReponses {
if !er.IsSuccesful() {
return false
}
}
return true
}
// returns true -> if there is even one policy that blocks resource request
2019-08-24 01:34:23 +00:00
// returns false -> if all the policies are meant to report only, we dont block resource request
func toBlockResource(engineReponses []response.EngineResponse) bool {
2019-08-24 01:34:23 +00:00
for _, er := range engineReponses {
if !er.IsSuccesful() && er.PolicyResponse.ValidationFailureAction == Enforce {
glog.V(4).Infof("ValidationFailureAction set to enforce for policy %s , blocking resource request ", er.PolicyResponse.Policy)
2019-08-24 01:34:23 +00:00
return true
}
}
glog.V(4).Infoln("ValidationFailureAction set to audit, allowing resource request, reporting with policy violation")
2019-08-24 01:34:23 +00:00
return false
}
// getEnforceFailureErrorMsg gets the error messages for failed enforce policy
func getEnforceFailureErrorMsg(engineReponses []response.EngineResponse) string {
2020-03-06 11:41:33 +00:00
policyToRule := make(map[string]interface{})
var resourceName string
for _, er := range engineReponses {
if !er.IsSuccesful() && er.PolicyResponse.ValidationFailureAction == Enforce {
2020-03-06 11:41:33 +00:00
ruleToReason := make(map[string]string)
for _, rule := range er.PolicyResponse.Rules {
if !rule.Success {
2020-03-06 11:41:33 +00:00
ruleToReason[rule.Name] = rule.Message
}
}
2020-03-06 11:41:33 +00:00
resourceName = fmt.Sprintf("%s/%s/%s", er.PolicyResponse.Resource.Kind, er.PolicyResponse.Resource.Namespace, er.PolicyResponse.Resource.Name)
2020-03-06 11:41:33 +00:00
policyToRule[er.PolicyResponse.Policy] = ruleToReason
}
}
2020-03-06 11:41:33 +00:00
result, _ := yamlv2.Marshal(policyToRule)
return "\n\nresource " + resourceName + " was blocked due to the following policies\n\n" + string(result)
}
// getErrorMsg gets all failed engine response message
func getErrorMsg(engineReponses []response.EngineResponse) string {
2019-08-24 01:34:23 +00:00
var str []string
var resourceInfo string
2019-08-24 01:34:23 +00:00
for _, er := range engineReponses {
if !er.IsSuccesful() {
// 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)
str = append(str, fmt.Sprintf("failed policy %s:", er.PolicyResponse.Policy))
2019-08-24 01:34:23 +00:00
for _, rule := range er.PolicyResponse.Rules {
if !rule.Success {
str = append(str, rule.ToString())
}
}
}
}
return fmt.Sprintf("Resource %s %s", resourceInfo, strings.Join(str, ";"))
2019-08-24 01:34:23 +00:00
}
2019-07-23 04:55:45 +00:00
//ArrayFlags to store filterkinds
2019-06-18 18:47:45 +00:00
type ArrayFlags []string
func (i *ArrayFlags) String() string {
var sb strings.Builder
for _, str := range *i {
sb.WriteString(str)
}
return sb.String()
}
2019-07-23 04:55:45 +00:00
//Set setter for array flags
2019-06-18 18:47:45 +00:00
func (i *ArrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
2019-06-19 21:05:23 +00:00
// Policy Reporting Modes
const (
Enforce = "enforce" // blocks the request on failure
Audit = "audit" // dont block the request on failure, but report failiures as policy violations
)
2019-07-16 22:53:14 +00:00
2019-08-24 01:34:23 +00:00
func processResourceWithPatches(patch []byte, resource []byte) []byte {
if patch == nil {
return resource
2019-08-24 01:34:23 +00:00
}
2019-10-08 01:31:14 +00:00
resource, err := engineutils.ApplyPatchNew(resource, patch)
2019-08-24 01:34:23 +00:00
if err != nil {
2019-08-26 23:10:19 +00:00
glog.Errorf("failed to patch resource: %v", err)
2019-08-24 01:34:23 +00:00
return nil
}
return resource
}
2019-11-11 22:52:09 +00:00
func containRBACinfo(policies []kyverno.ClusterPolicy) bool {
2019-11-11 22:52:09 +00:00
for _, policy := range policies {
for _, rule := range policy.Spec.Rules {
2020-02-17 18:31:03 +00:00
if len(rule.MatchResources.Roles) > 0 || len(rule.MatchResources.ClusterRoles) > 0 || len(rule.ExcludeResources.Roles) > 0 || len(rule.ExcludeResources.ClusterRoles) > 0 {
2019-11-11 22:52:09 +00:00
return true
}
}
}
return false
}
// extracts the new and old resource as unstructured
func extractResources(newRaw []byte, request *v1beta1.AdmissionRequest) (unstructured.Unstructured, unstructured.Unstructured, error) {
var emptyResource unstructured.Unstructured
// New Resource
2020-01-11 13:03:11 +00:00
if newRaw == nil {
newRaw = request.Object.Raw
}
if newRaw == nil {
return emptyResource, emptyResource, fmt.Errorf("new resource is not defined")
}
new, err := convertResource(newRaw, request.Kind.Group, request.Kind.Version, request.Kind.Kind, request.Namespace)
if err != nil {
return emptyResource, emptyResource, fmt.Errorf("failed to convert new raw to unstructured: %v", err)
}
// Old Resource - Optional
oldRaw := request.OldObject.Raw
if oldRaw == nil {
return new, emptyResource, nil
}
old, err := convertResource(oldRaw, request.Kind.Group, request.Kind.Version, request.Kind.Kind, request.Namespace)
if err != nil {
return emptyResource, emptyResource, fmt.Errorf("failed to convert old raw to unstructured: %v", err)
}
return new, old, err
}
// convertResource converts raw bytes to an unstructured object
func convertResource(raw []byte, group, version, kind, namespace string) (unstructured.Unstructured, error) {
obj, err := engineutils.ConvertToUnstructured(raw)
if err != nil {
return unstructured.Unstructured{}, fmt.Errorf("failed to convert raw to unstructured: %v", err)
}
obj.SetGroupVersionKind(schema.GroupVersionKind{Group: group, Version: version, Kind: kind})
obj.SetNamespace(namespace)
return *obj, nil
}