1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00
kyverno/pkg/webhooks/annotations.go

150 lines
3.8 KiB
Go
Raw Normal View History

2019-08-19 16:10:10 -07:00
package webhooks
import (
"encoding/json"
"strings"
jsonpatch "github.com/evanphx/json-patch/v5"
2020-03-17 11:05:20 -07:00
"github.com/go-logr/logr"
"github.com/kyverno/kyverno/pkg/engine/response"
yamlv2 "gopkg.in/yaml.v2"
2019-08-19 16:10:10 -07:00
)
const (
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"`
}
type annresponse struct {
2019-08-19 16:10:10 -07:00
Op string `json:"op"`
Path string `json:"path"`
Value interface{} `json:"value"`
}
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
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
if _, ok := annotations[strings.ReplaceAll(policyAnnotation, "~1", "/")]; ok {
2019-08-23 18:34:23 -07:00
// create update patch string
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 {
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
annotations[strings.ReplaceAll(policyAnnotation, "~1", "/")] = string(value)
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 {
Feature/cosign (#2078) * add image verification * inline policy list Signed-off-by: Jim Bugwadia <jim@nirmata.com> * cosign version and dependencies updates Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add registry initialization Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add build tag to exclude k8schain for cloud providers Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add build tag to exclude k8schain for cloud providers Signed-off-by: Jim Bugwadia <jim@nirmata.com> * generate deep copy and other fixtures Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix deep copy issues Signed-off-by: Jim Bugwadia <jim@nirmata.com> * mutate images to add digest Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add certificates to Kyverno container for HTTPS lookups Signed-off-by: Jim Bugwadia <jim@nirmata.com> * align flag syntax Signed-off-by: Jim Bugwadia <jim@nirmata.com> * update docs Signed-off-by: Jim Bugwadia <jim@nirmata.com> * update dependencies Signed-off-by: Jim Bugwadia <jim@nirmata.com> * update dependencies Signed-off-by: Jim Bugwadia <jim@nirmata.com> * patch image with digest and fix checks Signed-off-by: Jim Bugwadia <jim@nirmata.com> * hardcode image for demos Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add default registry (docker.io) before calling reference.Parse Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix definition Signed-off-by: Jim Bugwadia <jim@nirmata.com> * increase webhook timeout Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix args Signed-off-by: Jim Bugwadia <jim@nirmata.com> * run gofmt Signed-off-by: Jim Bugwadia <jim@nirmata.com> * rename for clarity Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix HasImageVerify check Signed-off-by: Jim Bugwadia <jim@nirmata.com> * align make test commands Signed-off-by: Jim Bugwadia <jim@nirmata.com> * align make test commands Signed-off-by: Jim Bugwadia <jim@nirmata.com> * align make test commands Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix linter error Signed-off-by: Jim Bugwadia <jim@nirmata.com> * format Signed-off-by: Jim Bugwadia <jim@nirmata.com> * handle API conflict and retry Signed-off-by: Jim Bugwadia <jim@nirmata.com> * format Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix reviewdog issues Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix make for unit tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * improve error message Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix durations Signed-off-by: Jim Bugwadia <jim@nirmata.com> * handle errors in tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * print policy name Signed-off-by: Jim Bugwadia <jim@nirmata.com> * update tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add retries and duration to error log Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix time check in tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * round creation times in test Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix retry loop Signed-off-by: Jim Bugwadia <jim@nirmata.com> * remove timing check for policy creation Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix e2e error - policy not found Signed-off-by: Shuting Zhao <shutting06@gmail.com> * update string comparison method Signed-off-by: Shuting Zhao <shutting06@gmail.com> * fix test Generate_Namespace_Label_Actions Signed-off-by: Shuting Zhao <shutting06@gmail.com> * add debug info for e2e tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix error Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix generate bug Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix format Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add check for update operations Signed-off-by: Jim Bugwadia <jim@nirmata.com> * increase time for deleteing a resource Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix check Signed-off-by: Jim Bugwadia <jim@nirmata.com> Co-authored-by: Shuting Zhao <shutting06@gmail.com>
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 {
var annotationContent = make(map[string]string)
2019-10-07 18:31:14 -07:00
for _, engineResponse := range engineResponses {
if !engineResponse.IsSuccessful() {
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
policyName := engineResponse.PolicyResponse.Policy.Name
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
if len(annotationContent) == 0 {
2019-08-23 18:34:23 -07:00
return nil
}
2019-08-19 16:10:10 -07:00
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
}