2019-05-13 18:17:28 -07:00
|
|
|
package engine
|
2019-05-09 22:26:22 -07:00
|
|
|
|
|
|
|
import (
|
2019-05-14 11:24:40 -07:00
|
|
|
"log"
|
|
|
|
|
2019-05-13 21:27:47 +03:00
|
|
|
kubepolicy "github.com/nirmata/kube-policy/pkg/apis/policy/v1alpha1"
|
2019-05-13 18:17:28 -07:00
|
|
|
"github.com/nirmata/kube-policy/pkg/engine/mutation"
|
2019-05-14 19:40:17 +03:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2019-05-09 22:26:22 -07:00
|
|
|
)
|
|
|
|
|
2019-05-13 21:27:47 +03:00
|
|
|
// Mutate performs mutation. Overlay first and then mutation patches
|
2019-05-14 18:20:41 -07:00
|
|
|
// TODO: return events and violations
|
2019-05-14 12:11:07 -07:00
|
|
|
func Mutate(policy kubepolicy.Policy, rawResource []byte, gvk metav1.GroupVersionKind) []mutation.PatchBytes {
|
2019-05-09 22:26:22 -07:00
|
|
|
var policyPatches []mutation.PatchBytes
|
|
|
|
|
2019-05-13 21:27:47 +03:00
|
|
|
for i, rule := range policy.Spec.Rules {
|
|
|
|
|
|
|
|
// Checks for preconditions
|
|
|
|
// TODO: Rework PolicyEngine interface that it receives not a policy, but mutation object for
|
|
|
|
// Mutate, validation for Validate and so on. It will allow to bring this checks outside of PolicyEngine
|
|
|
|
// to common part as far as they present for all: mutation, validation, generation
|
|
|
|
|
2019-05-09 22:26:22 -07:00
|
|
|
err := rule.Validate()
|
|
|
|
if err != nil {
|
2019-05-14 11:24:40 -07:00
|
|
|
log.Printf("Rule has invalid structure: rule number = %d, rule name = %s in policy %s, err: %v\n", i, rule.Name, policy.ObjectMeta.Name, err)
|
2019-05-09 22:26:22 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-05-15 14:25:32 +03:00
|
|
|
ok, err := ResourceMeetsRules(rawResource, rule.ResourceDescription, gvk)
|
2019-05-09 22:26:22 -07:00
|
|
|
if err != nil {
|
2019-05-14 11:24:40 -07:00
|
|
|
log.Printf("Rule has invalid data: rule number = %d, rule name = %s in policy %s, err: %v\n", i, rule.Name, policy.ObjectMeta.Name, err)
|
2019-05-13 21:27:47 +03:00
|
|
|
continue
|
2019-05-09 22:26:22 -07:00
|
|
|
}
|
|
|
|
|
2019-05-13 21:27:47 +03:00
|
|
|
if !ok {
|
2019-05-14 18:20:41 -07:00
|
|
|
log.Printf("Rule is not applicable to the request: rule number = %d, rule name = %s in policy %s, err: %v\n", i, rule.Name, policy.ObjectMeta.Name, err)
|
2019-05-13 21:27:47 +03:00
|
|
|
continue
|
2019-05-09 22:26:22 -07:00
|
|
|
}
|
|
|
|
|
2019-05-14 18:10:25 +03:00
|
|
|
if rule.Mutation == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-05-13 21:27:47 +03:00
|
|
|
// Process Overlay
|
2019-05-09 22:26:22 -07:00
|
|
|
|
2019-05-13 21:27:47 +03:00
|
|
|
if rule.Mutation.Overlay != nil {
|
|
|
|
overlayPatches, err := mutation.ProcessOverlay(rule.Mutation.Overlay, rawResource)
|
|
|
|
if err != nil {
|
2019-05-14 11:24:40 -07:00
|
|
|
log.Printf("Overlay application failed: rule number = %d, rule name = %s in policy %s, err: %v\n", i, rule.Name, policy.ObjectMeta.Name, err)
|
2019-05-13 21:27:47 +03:00
|
|
|
} else {
|
|
|
|
policyPatches = append(policyPatches, overlayPatches...)
|
|
|
|
}
|
2019-05-09 22:26:22 -07:00
|
|
|
}
|
|
|
|
|
2019-05-13 21:27:47 +03:00
|
|
|
// Process Patches
|
2019-05-09 22:26:22 -07:00
|
|
|
|
2019-05-13 21:27:47 +03:00
|
|
|
if rule.Mutation.Patches != nil {
|
|
|
|
processedPatches, err := mutation.ProcessPatches(rule.Mutation.Patches, rawResource)
|
|
|
|
if err != nil {
|
2019-05-14 11:24:40 -07:00
|
|
|
log.Printf("Patches application failed: rule number = %d, rule name = %s in policy %s, err: %v\n", i, rule.Name, policy.ObjectMeta.Name, err)
|
2019-05-13 21:27:47 +03:00
|
|
|
} else {
|
|
|
|
policyPatches = append(policyPatches, processedPatches...)
|
|
|
|
}
|
|
|
|
}
|
2019-05-09 22:26:22 -07:00
|
|
|
}
|
|
|
|
|
2019-05-13 21:27:47 +03:00
|
|
|
return policyPatches
|
2019-05-09 22:26:22 -07:00
|
|
|
}
|