2019-08-19 16:10:10 -07:00
|
|
|
package webhooks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
jsonpatch "github.com/evanphx/json-patch"
|
|
|
|
"github.com/golang/glog"
|
2019-11-06 17:14:32 -08:00
|
|
|
"github.com/nirmata/kyverno/pkg/engine"
|
2019-08-19 16:10:10 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-11-11 18:52:26 -08:00
|
|
|
policyAnnotation = "policies.kyverno.patches"
|
2019-08-19 16:10:10 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type policyPatch struct {
|
2019-10-07 18:31:14 -07:00
|
|
|
PolicyName string `json:"policyname"`
|
2019-08-19 16:10:10 -07:00
|
|
|
RulePatches interface{} `json:"patches"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type rulePatch struct {
|
|
|
|
RuleName string `json:"rulename"`
|
|
|
|
Op string `json:"op"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type response struct {
|
|
|
|
Op string `json:"op"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
Value interface{} `json:"value"`
|
|
|
|
}
|
|
|
|
|
2019-11-11 18:52:26 -08:00
|
|
|
func generateAnnotationPatches(engineResponses []engine.EngineResponse) []byte {
|
|
|
|
var annotations map[string]string
|
|
|
|
if len(engineResponses) > 0 {
|
|
|
|
annotations = engineResponses[0].PatchedResource.GetAnnotations()
|
|
|
|
}
|
|
|
|
|
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-08-23 18:34:23 -07:00
|
|
|
var patchResponse response
|
2019-10-08 14:21:47 -07:00
|
|
|
value := annotationFromEngineResponses(engineResponses)
|
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
|
|
|
|
2019-08-23 18:34:23 -07:00
|
|
|
if _, ok := annotations[policyAnnotation]; ok {
|
|
|
|
// create update patch string
|
|
|
|
patchResponse = response{
|
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 = response{
|
|
|
|
Op: "add",
|
|
|
|
Path: "/metadata/annotations/" + policyAnnotation,
|
|
|
|
Value: string(value),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// insert 'policies.kyverno.patches' entry in annotation map
|
|
|
|
annotations[policyAnnotation] = string(value)
|
|
|
|
patchResponse = response{
|
|
|
|
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 {
|
|
|
|
glog.Errorf("Failed to make patch from annotation'%s', err: %v\n ", string(patchByte), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return patchByte
|
|
|
|
}
|
|
|
|
|
2019-10-08 16:23:24 -07:00
|
|
|
func annotationFromEngineResponses(engineResponses []engine.EngineResponse) []byte {
|
2019-10-07 18:31:14 -07:00
|
|
|
var policyPatches []policyPatch
|
|
|
|
for _, engineResponse := range engineResponses {
|
|
|
|
if !engineResponse.IsSuccesful() {
|
|
|
|
glog.V(3).Infof("Policy %s failed, skip preparing annotation\n", engineResponse.PolicyResponse.Policy)
|
2019-08-23 18:34:23 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-10-07 18:31:14 -07:00
|
|
|
var pp policyPatch
|
|
|
|
rulePatches := annotationFromPolicyResponse(engineResponse.PolicyResponse)
|
|
|
|
if rulePatches == nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-08-23 18:34:23 -07:00
|
|
|
|
2019-10-07 18:31:14 -07:00
|
|
|
pp.RulePatches = rulePatches
|
|
|
|
pp.PolicyName = engineResponse.PolicyResponse.Policy
|
|
|
|
policyPatches = append(policyPatches, pp)
|
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 policyPatches == nil {
|
2019-08-23 18:34:23 -07:00
|
|
|
return nil
|
|
|
|
}
|
2019-08-19 16:10:10 -07:00
|
|
|
|
2019-10-07 18:31:14 -07:00
|
|
|
result, _ := json.Marshal(policyPatches)
|
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
|
|
|
|
2019-10-07 18:31:14 -07:00
|
|
|
func annotationFromPolicyResponse(policyResponse engine.PolicyResponse) []rulePatch {
|
|
|
|
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 {
|
|
|
|
glog.Errorf("Failed to parse patch bytes, err: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
rp := rulePatch{
|
|
|
|
RuleName: ruleInfo.Name,
|
|
|
|
Op: patchmap["op"].(string),
|
|
|
|
Path: patchmap["path"].(string)}
|
|
|
|
|
|
|
|
rulePatches = append(rulePatches, rp)
|
|
|
|
glog.V(4).Infof("Annotation value prepared: %v\n", rulePatches)
|
|
|
|
}
|
|
|
|
}
|
2019-08-19 16:10:10 -07:00
|
|
|
|
2019-10-07 18:31:14 -07:00
|
|
|
if len(rulePatches) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-19 16:10:10 -07:00
|
|
|
|
2019-10-07 18:31:14 -07:00
|
|
|
return rulePatches
|
|
|
|
}
|