2020-03-05 20:24:36 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-04-28 20:12:44 +00:00
|
|
|
"github.com/ghodss/yaml"
|
2020-10-07 18:12:31 +00:00
|
|
|
kyverno "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/context"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/mutate"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/response"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/utils"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/variables"
|
2020-03-05 20:24:36 +00:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2020-03-18 00:23:18 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
2020-03-05 20:24:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func mutateResourceWithOverlay(resource unstructured.Unstructured, overlay interface{}) (unstructured.Unstructured, error) {
|
2020-07-05 02:32:11 +00:00
|
|
|
logger := log.Log.WithValues("resource", resource.GetKind(), "overlay", overlay)
|
2020-03-05 20:24:36 +00:00
|
|
|
patches, err := mutate.MutateResourceWithOverlay(resource.UnstructuredContent(), overlay)
|
|
|
|
if err != nil {
|
2020-07-05 02:32:11 +00:00
|
|
|
logger.V(4).Info("failed to mutate resource with overlay")
|
|
|
|
return resource, err
|
2020-03-05 20:24:36 +00:00
|
|
|
}
|
2020-07-05 02:32:11 +00:00
|
|
|
|
2020-03-05 20:24:36 +00:00
|
|
|
if len(patches) == 0 {
|
|
|
|
return resource, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert to RAW
|
|
|
|
resourceRaw, err := resource.MarshalJSON()
|
|
|
|
if err != nil {
|
2020-07-05 02:32:11 +00:00
|
|
|
logger.V(4).Info("failed to marshall resource JSON")
|
|
|
|
return resource, err
|
2020-03-05 20:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var patchResource []byte
|
|
|
|
patchResource, err = utils.ApplyPatches(resourceRaw, patches)
|
|
|
|
if err != nil {
|
2020-07-05 02:32:11 +00:00
|
|
|
logger.V(4).Info("failed to apply patches")
|
|
|
|
return resource, err
|
2020-03-05 20:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource = unstructured.Unstructured{}
|
|
|
|
err = resource.UnmarshalJSON(patchResource)
|
|
|
|
if err != nil {
|
2020-07-05 02:32:11 +00:00
|
|
|
logger.V(4).Info("failed to unmarshal patched resource JSON")
|
|
|
|
return resource, err
|
2020-03-05 20:24:36 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 02:32:11 +00:00
|
|
|
logger.V(4).Info("mutated resource with overlay")
|
2020-03-05 20:24:36 +00:00
|
|
|
return resource, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForceMutate does not check any conditions, it simply mutates the given resource
|
2021-03-26 17:47:59 +00:00
|
|
|
// It is used to validate mutation logic, and for tests.
|
2020-03-05 20:24:36 +00:00
|
|
|
func ForceMutate(ctx context.EvalInterface, policy kyverno.ClusterPolicy, resource unstructured.Unstructured) (unstructured.Unstructured, error) {
|
|
|
|
var err error
|
2020-07-09 18:48:34 +00:00
|
|
|
logger := log.Log.WithName("EngineForceMutate").WithValues("policy", policy.Name, "kind", resource.GetKind(),
|
|
|
|
"namespace", resource.GetNamespace(), "name", resource.GetName())
|
|
|
|
|
2020-03-05 20:24:36 +00:00
|
|
|
for _, rule := range policy.Spec.Rules {
|
|
|
|
if !rule.HasMutate() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-03-11 20:06:04 +00:00
|
|
|
rule, err = variables.SubstituteAllForceMutate(log.Log, ctx, rule)
|
|
|
|
if err != nil {
|
|
|
|
return unstructured.Unstructured{}, err
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:24:36 +00:00
|
|
|
mutation := rule.Mutation.DeepCopy()
|
|
|
|
|
|
|
|
if mutation.Overlay != nil {
|
|
|
|
overlay := mutation.Overlay
|
|
|
|
|
|
|
|
resource, err = mutateResourceWithOverlay(resource, overlay)
|
|
|
|
if err != nil {
|
2020-07-05 02:32:11 +00:00
|
|
|
detailedErr := fmt.Errorf("failed to mutate resource %s with overlay rule %v:%v", resource.GetKind(), rule.Name, err)
|
|
|
|
return unstructured.Unstructured{}, detailedErr
|
2020-03-05 20:24:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule.Mutation.Patches != nil {
|
|
|
|
var resp response.RuleResponse
|
2020-08-05 16:11:23 +00:00
|
|
|
resp, resource = mutate.ProcessPatches(logger.WithValues("rule", rule.Name), rule.Name, rule.Mutation, resource)
|
2020-03-05 20:24:36 +00:00
|
|
|
if !resp.Success {
|
|
|
|
return unstructured.Unstructured{}, fmt.Errorf(resp.Message)
|
|
|
|
}
|
|
|
|
}
|
2021-04-28 20:12:44 +00:00
|
|
|
|
|
|
|
if rule.Mutation.PatchesJSON6902 != "" {
|
|
|
|
var resp response.RuleResponse
|
|
|
|
jsonPatches, err := yaml.YAMLToJSON([]byte(rule.Mutation.PatchesJSON6902))
|
|
|
|
if err != nil {
|
|
|
|
return unstructured.Unstructured{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, resource = mutate.ProcessPatchJSON6902(rule.Name, jsonPatches, resource, logger.WithValues("rule", rule.Name))
|
|
|
|
if !resp.Success {
|
|
|
|
return unstructured.Unstructured{}, fmt.Errorf(resp.Message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resource, nil
|
|
|
|
}
|