2019-08-19 16:10:10 -07:00
|
|
|
package webhooks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-02-06 08:05:27 +05:30
|
|
|
"strings"
|
|
|
|
|
2021-02-25 15:21:55 -08:00
|
|
|
jsonpatch "github.com/evanphx/json-patch/v5"
|
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"
|
2021-02-07 20:26:56 -08:00
|
|
|
yamlv2 "gopkg.in/yaml.v2"
|
2019-08-19 16:10:10 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-02-06 08:05:27 +05:30
|
|
|
policyAnnotation = "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"`
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:08:50 -08:00
|
|
|
type annresponse struct {
|
2019-08-19 16:10:10 -07:00
|
|
|
Op string `json:"op"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
Value interface{} `json:"value"`
|
|
|
|
}
|
|
|
|
|
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",
|
|
|
|
}
|
|
|
|
|
2020-12-23 15:10:07 -08:00
|
|
|
func generateAnnotationPatches(engineResponses []*response.EngineResponse, log logr.Logger) []byte {
|
2019-11-11 18:52:26 -08:00
|
|
|
var annotations map[string]string
|
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
|
|
|
}
|
2019-10-07 18:31:14 -07:00
|
|
|
|
2019-12-30 17:08:50 -08:00
|
|
|
var patchResponse annresponse
|
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
|
|
|
|
}
|
2019-10-07 18:31:14 -07:00
|
|
|
|
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
|
2019-12-30 17:08:50 -08:00
|
|
|
patchResponse = annresponse{
|
2019-08-26 16:10:19 -07:00
|
|
|
Op: "replace",
|
2019-10-07 18:31:14 -07:00
|
|
|
Path: "/metadata/annotations/" + policyAnnotation,
|
2019-08-23 18:34:23 -07:00
|
|
|
Value: string(value),
|
|
|
|
}
|
|
|
|
} else {
|
2019-11-11 18:52:26 -08:00
|
|
|
// mutate rule has annotation patches
|
|
|
|
if len(annotations) > 0 {
|
2019-12-30 17:08:50 -08:00
|
|
|
patchResponse = annresponse{
|
2019-11-11 18:52:26 -08:00
|
|
|
Op: "add",
|
|
|
|
Path: "/metadata/annotations/" + policyAnnotation,
|
|
|
|
Value: string(value),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// insert 'policies.kyverno.patches' entry in annotation map
|
2020-02-06 08:05:27 +05:30
|
|
|
annotations[strings.ReplaceAll(policyAnnotation, "~1", "/")] = string(value)
|
2019-12-30 17:08:50 -08:00
|
|
|
patchResponse = annresponse{
|
2019-11-11 18:52:26 -08:00
|
|
|
Op: "add",
|
|
|
|
Path: "/metadata/annotations",
|
|
|
|
Value: annotations,
|
|
|
|
}
|
2019-08-23 18:34:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
patchByte, _ := json.Marshal(patchResponse)
|
|
|
|
|
|
|
|
// check the patch
|
|
|
|
_, err := jsonpatch.DecodePatch([]byte("[" + string(patchByte) + "]"))
|
|
|
|
if err != nil {
|
2021-07-09 18:01:46 -07:00
|
|
|
log.Error(err, "failed to build JSON patch for annotation", "patch", string(patchByte))
|
2019-08-23 18:34:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return patchByte
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2019-08-23 18:34:23 -07:00
|
|
|
|
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),
|
|
|
|
Path: patchmap["path"].(string)}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|