2019-08-19 16:10:10 -07:00
|
|
|
package webhooks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-02-06 08:05:27 +05:30
|
|
|
"strings"
|
|
|
|
|
2020-03-17 11:05:20 -07:00
|
|
|
"github.com/go-logr/logr"
|
2020-10-07 11:12:31 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/response"
|
2022-04-01 07:26:47 +02:00
|
|
|
jsonutils "github.com/kyverno/kyverno/pkg/utils/json"
|
2021-02-07 20:26:56 -08:00
|
|
|
yamlv2 "gopkg.in/yaml.v2"
|
2019-08-19 16:10:10 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-09-20 21:56:19 +05:30
|
|
|
policyAnnotation = "policies.kyverno.io~1last-applied-patches"
|
|
|
|
oldAnnotation = "policies.kyverno.io~1patches"
|
2019-08-19 16:10:10 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type rulePatch struct {
|
|
|
|
RuleName string `json:"rulename"`
|
|
|
|
Op string `json:"op"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
}
|
|
|
|
|
2020-02-06 08:05:27 +05:30
|
|
|
var operationToPastTense = map[string]string{
|
|
|
|
"add": "added",
|
|
|
|
"remove": "removed",
|
|
|
|
"replace": "replaced",
|
|
|
|
"move": "moved",
|
|
|
|
"copy": "copied",
|
|
|
|
"test": "tested",
|
|
|
|
}
|
|
|
|
|
2021-09-20 21:56:19 +05:30
|
|
|
func generateAnnotationPatches(engineResponses []*response.EngineResponse, log logr.Logger) [][]byte {
|
2019-11-11 18:52:26 -08:00
|
|
|
var annotations map[string]string
|
2021-09-20 21:56:19 +05:30
|
|
|
var patchBytes [][]byte
|
2019-11-13 17:56:56 -08:00
|
|
|
for _, er := range engineResponses {
|
|
|
|
if ann := er.PatchedResource.GetAnnotations(); ann != nil {
|
|
|
|
annotations = ann
|
|
|
|
break
|
|
|
|
}
|
2019-11-11 18:52:26 -08:00
|
|
|
}
|
2019-08-23 18:34:23 -07:00
|
|
|
if annotations == nil {
|
2019-10-07 18:31:14 -07:00
|
|
|
annotations = make(map[string]string)
|
2019-08-23 18:34:23 -07:00
|
|
|
}
|
2022-04-01 07:26:47 +02:00
|
|
|
var patchResponse jsonutils.Patch
|
2020-03-17 11:05:20 -07:00
|
|
|
value := annotationFromEngineResponses(engineResponses, log)
|
2019-08-23 18:34:23 -07:00
|
|
|
if value == nil {
|
|
|
|
// no patches or error while processing patches
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-06 08:05:27 +05:30
|
|
|
if _, ok := annotations[strings.ReplaceAll(policyAnnotation, "~1", "/")]; ok {
|
2019-08-23 18:34:23 -07:00
|
|
|
// create update patch string
|
2021-09-20 21:56:19 +05:30
|
|
|
if _, ok := annotations["policies.kyverno.io/patches"]; ok {
|
2022-04-04 23:48:22 +02:00
|
|
|
patchResponse = jsonutils.NewPatch("/metadata/annotations/"+oldAnnotation, "remove", nil)
|
2021-09-20 21:56:19 +05:30
|
|
|
delete(annotations, "policies.kyverno.io/patches")
|
|
|
|
patchByte, _ := json.Marshal(patchResponse)
|
|
|
|
patchBytes = append(patchBytes, patchByte)
|
|
|
|
}
|
2022-04-01 07:26:47 +02:00
|
|
|
patchResponse = jsonutils.NewPatch("/metadata/annotations/"+policyAnnotation, "replace", string(value))
|
2021-09-20 21:56:19 +05:30
|
|
|
patchByte, _ := json.Marshal(patchResponse)
|
|
|
|
patchBytes = append(patchBytes, patchByte)
|
2019-08-23 18:34:23 -07:00
|
|
|
} else {
|
2019-11-11 18:52:26 -08:00
|
|
|
// mutate rule has annotation patches
|
|
|
|
if len(annotations) > 0 {
|
2021-09-20 21:56:19 +05:30
|
|
|
if _, ok := annotations["policies.kyverno.io/patches"]; ok {
|
2022-04-01 07:26:47 +02:00
|
|
|
patchResponse = jsonutils.NewPatch("/metadata/annotations/"+oldAnnotation, "remove", nil)
|
2021-09-20 21:56:19 +05:30
|
|
|
delete(annotations, "policies.kyverno.io/patches")
|
|
|
|
patchByte, _ := json.Marshal(patchResponse)
|
|
|
|
patchBytes = append(patchBytes, patchByte)
|
|
|
|
}
|
2022-04-01 07:26:47 +02:00
|
|
|
patchResponse = jsonutils.NewPatch("/metadata/annotations/"+policyAnnotation, "add", string(value))
|
2021-09-20 21:56:19 +05:30
|
|
|
patchByte, _ := json.Marshal(patchResponse)
|
|
|
|
patchBytes = append(patchBytes, patchByte)
|
2019-11-11 18:52:26 -08:00
|
|
|
} else {
|
|
|
|
// insert 'policies.kyverno.patches' entry in annotation map
|
2020-02-06 08:05:27 +05:30
|
|
|
annotations[strings.ReplaceAll(policyAnnotation, "~1", "/")] = string(value)
|
2022-04-01 07:26:47 +02:00
|
|
|
patchResponse = jsonutils.NewPatch("/metadata/annotations", "add", annotations)
|
2021-09-20 21:56:19 +05:30
|
|
|
patchByte, _ := json.Marshal(patchResponse)
|
|
|
|
patchBytes = append(patchBytes, patchByte)
|
2019-08-23 18:34:23 -07:00
|
|
|
}
|
|
|
|
}
|
2021-09-20 21:56:19 +05:30
|
|
|
for _, patchByte := range patchBytes {
|
2022-04-01 07:26:47 +02:00
|
|
|
err := jsonutils.CheckPatch(patchByte)
|
2021-09-20 21:56:19 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error(err, "failed to build JSON patch for annotation", "patch", string(patchByte))
|
|
|
|
}
|
2019-08-23 18:34:23 -07:00
|
|
|
}
|
2021-09-20 21:56:19 +05:30
|
|
|
return patchBytes
|
2019-08-23 18:34:23 -07:00
|
|
|
}
|
|
|
|
|
2020-12-23 15:10:07 -08:00
|
|
|
func annotationFromEngineResponses(engineResponses []*response.EngineResponse, log logr.Logger) []byte {
|
2020-02-06 08:05:27 +05:30
|
|
|
var annotationContent = make(map[string]string)
|
2019-10-07 18:31:14 -07:00
|
|
|
for _, engineResponse := range engineResponses {
|
2020-06-30 11:53:27 -07:00
|
|
|
if !engineResponse.IsSuccessful() {
|
2021-06-30 00:43:11 +03:00
|
|
|
log.V(3).Info("skip building annotation; policy failed to apply", "policy", engineResponse.PolicyResponse.Policy.Name)
|
2019-08-23 18:34:23 -07:00
|
|
|
continue
|
|
|
|
}
|
2020-03-17 11:05:20 -07:00
|
|
|
rulePatches := annotationFromPolicyResponse(engineResponse.PolicyResponse, log)
|
2019-10-07 18:31:14 -07:00
|
|
|
if rulePatches == nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-06-30 00:43:11 +03:00
|
|
|
policyName := engineResponse.PolicyResponse.Policy.Name
|
2020-02-06 08:05:27 +05:30
|
|
|
for _, rulePatch := range rulePatches {
|
|
|
|
annotationContent[rulePatch.RuleName+"."+policyName+".kyverno.io"] = operationToPastTense[rulePatch.Op] + " " + rulePatch.Path
|
|
|
|
}
|
2019-08-23 18:34:23 -07:00
|
|
|
}
|
2019-10-07 18:31:14 -07:00
|
|
|
|
|
|
|
// return nil if there's no patches
|
|
|
|
// otherwise result = null, len(result) = 4
|
2020-02-06 08:05:27 +05:30
|
|
|
if len(annotationContent) == 0 {
|
2019-08-23 18:34:23 -07:00
|
|
|
return nil
|
|
|
|
}
|
2019-08-19 16:10:10 -07:00
|
|
|
|
2020-02-06 08:05:27 +05:30
|
|
|
result, _ := yamlv2.Marshal(annotationContent)
|
2019-08-19 16:10:10 -07:00
|
|
|
|
2019-10-07 18:31:14 -07:00
|
|
|
return result
|
|
|
|
}
|
2019-08-19 16:10:10 -07:00
|
|
|
|
2020-03-17 11:05:20 -07:00
|
|
|
func annotationFromPolicyResponse(policyResponse response.PolicyResponse, log logr.Logger) []rulePatch {
|
2019-10-07 18:31:14 -07:00
|
|
|
var rulePatches []rulePatch
|
|
|
|
for _, ruleInfo := range policyResponse.Rules {
|
|
|
|
for _, patch := range ruleInfo.Patches {
|
|
|
|
var patchmap map[string]interface{}
|
|
|
|
if err := json.Unmarshal(patch, &patchmap); err != nil {
|
2020-03-17 11:05:20 -07:00
|
|
|
log.Error(err, "Failed to parse JSON patch bytes")
|
2019-10-07 18:31:14 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
rp := rulePatch{
|
|
|
|
RuleName: ruleInfo.Name,
|
|
|
|
Op: patchmap["op"].(string),
|
2022-04-01 07:26:47 +02:00
|
|
|
Path: patchmap["path"].(string),
|
|
|
|
}
|
2019-10-07 18:31:14 -07:00
|
|
|
rulePatches = append(rulePatches, rp)
|
2020-03-17 11:05:20 -07:00
|
|
|
log.V(4).Info("annotation value prepared", "patches", rulePatches)
|
2019-10-07 18:31:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(rulePatches) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return rulePatches
|
|
|
|
}
|