2019-11-12 13:32:30 -08:00
|
|
|
package policyviolation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2019-11-12 23:19:38 -08:00
|
|
|
"strings"
|
2019-11-12 13:32:30 -08:00
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1alpha1"
|
|
|
|
kyvernoclient "github.com/nirmata/kyverno/pkg/client/clientset/versioned"
|
2019-11-12 16:04:00 -08:00
|
|
|
kyvernov1alpha1 "github.com/nirmata/kyverno/pkg/client/clientset/versioned/typed/kyverno/v1alpha1"
|
2019-11-12 13:32:30 -08:00
|
|
|
kyvernolister "github.com/nirmata/kyverno/pkg/client/listers/kyverno/v1alpha1"
|
2019-11-12 14:58:38 -08:00
|
|
|
dclient "github.com/nirmata/kyverno/pkg/dclient"
|
2019-11-12 13:32:30 -08:00
|
|
|
engine "github.com/nirmata/kyverno/pkg/engine"
|
|
|
|
labels "k8s.io/apimachinery/pkg/labels"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CreateNamespacePV(pvLister kyvernolister.NamespacedPolicyViolationLister, client *kyvernoclient.Clientset, engineResponses []engine.EngineResponse) {
|
2019-11-12 16:04:00 -08:00
|
|
|
// var pvs []kyverno.NamespacedPolicyViolation
|
|
|
|
// for _, er := range engineResponses {
|
|
|
|
// // ignore creation of PV for resoruces that are yet to be assigned a name
|
|
|
|
// if er.PolicyResponse.Resource.Name == "" {
|
|
|
|
// glog.V(4).Infof("resource %v, has not been assigned a name, not creating a namespace policy violation for it", er.PolicyResponse.Resource)
|
|
|
|
// continue
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if !er.IsSuccesful() {
|
|
|
|
// glog.V(4).Infof("Building namespace policy violation for engine response %v", er)
|
|
|
|
// if pv := buildNamespacedPVForPolicy(er); !reflect.DeepEqual(pv, kyverno.NamespacedPolicyViolation{}) {
|
|
|
|
// pvs = append(pvs, pv)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// createNamespacedPV(pvLister, client, pvs)
|
2019-11-12 13:32:30 -08:00
|
|
|
}
|
|
|
|
|
2019-11-12 14:58:38 -08:00
|
|
|
// CreateNamespacedPVWhenBlocked creates pv on resource owner only when admission request is denied
|
|
|
|
func CreateNamespacedPVWhenBlocked(pvLister kyvernolister.NamespacedPolicyViolationLister, client *kyvernoclient.Clientset,
|
|
|
|
dclient *dclient.Client, engineResponses []engine.EngineResponse) {
|
2019-11-12 16:04:00 -08:00
|
|
|
// var pvs []kyverno.NamespacedPolicyViolation
|
|
|
|
// for _, er := range engineResponses {
|
|
|
|
// // child resource is not created in this case thus it won't have a name
|
|
|
|
// glog.V(4).Infof("Building policy violation for denied admission request, engineResponse: %v", er)
|
|
|
|
// if pvList := buildNamespacedPVWithOwner(dclient, er); len(pvList) != 0 {
|
|
|
|
// pvs = append(pvs, pvList...)
|
|
|
|
// glog.V(3).Infof("Built policy violation for denied admission request %s/%s/%s",
|
|
|
|
// er.PatchedResource.GetKind(), er.PatchedResource.GetNamespace(), er.PatchedResource.GetName())
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// createNamespacedPV(pvLister, client, pvs)
|
2019-11-12 14:58:38 -08:00
|
|
|
}
|
|
|
|
|
2019-11-12 16:04:00 -08:00
|
|
|
func buildNamespacedPV(info Info) kyverno.NamespacedPolicyViolation {
|
|
|
|
return buildNamespacedPVObj(info.PolicyName,
|
|
|
|
kyverno.ResourceSpec{
|
|
|
|
Kind: info.Resource.GetKind(),
|
|
|
|
Namespace: info.Resource.GetNamespace(),
|
|
|
|
Name: info.Resource.GetName(),
|
|
|
|
},
|
|
|
|
info.Rules)
|
2019-11-12 13:32:30 -08:00
|
|
|
}
|
|
|
|
|
2019-11-12 16:04:00 -08:00
|
|
|
//buildNamespacedPVObj returns an value of type PolicyViolation
|
|
|
|
func buildNamespacedPVObj(policy string, resource kyverno.ResourceSpec, fRules []kyverno.ViolatedRule) kyverno.NamespacedPolicyViolation {
|
2019-11-12 13:32:30 -08:00
|
|
|
pv := kyverno.NamespacedPolicyViolation{
|
|
|
|
Spec: kyverno.PolicyViolationSpec{
|
|
|
|
Policy: policy,
|
|
|
|
ResourceSpec: resource,
|
|
|
|
ViolatedRules: fRules,
|
|
|
|
},
|
|
|
|
}
|
2019-11-12 23:19:38 -08:00
|
|
|
|
|
|
|
labelMap := map[string]string{
|
|
|
|
"policy": policy,
|
|
|
|
"resource": strings.Join([]string{resource.Kind, resource.Namespace, resource.Name}, "."),
|
|
|
|
}
|
2019-11-12 13:32:30 -08:00
|
|
|
pv.SetGenerateName("pv-")
|
2019-11-12 23:19:38 -08:00
|
|
|
pv.SetLabels(labelMap)
|
2019-11-12 13:32:30 -08:00
|
|
|
return pv
|
|
|
|
}
|
|
|
|
|
2019-11-12 16:04:00 -08:00
|
|
|
func buildNamespacedPVWithOwner(dclient *dclient.Client, info Info) (pvs []kyverno.NamespacedPolicyViolation) {
|
2019-11-12 14:58:38 -08:00
|
|
|
// create violation on resource owner (if exist) when action is set to enforce
|
2019-11-12 19:01:48 -08:00
|
|
|
owners := getOwnersOld(dclient, info.Resource)
|
2019-11-12 14:58:38 -08:00
|
|
|
|
|
|
|
// standaloneresource, set pvResourceSpec with resource itself
|
2019-11-12 19:01:48 -08:00
|
|
|
if len(owners) == 0 {
|
2019-11-12 14:58:38 -08:00
|
|
|
pvResourceSpec := kyverno.ResourceSpec{
|
2019-11-12 16:04:00 -08:00
|
|
|
Namespace: info.Resource.GetNamespace(),
|
|
|
|
Kind: info.Resource.GetKind(),
|
|
|
|
Name: info.Resource.GetName(),
|
2019-11-12 14:58:38 -08:00
|
|
|
}
|
2019-11-12 16:04:00 -08:00
|
|
|
return append(pvs, buildNamespacedPVObj(info.PolicyName, pvResourceSpec, info.Rules))
|
2019-11-12 14:58:38 -08:00
|
|
|
}
|
|
|
|
|
2019-11-12 19:01:48 -08:00
|
|
|
for _, owner := range owners {
|
2019-11-12 16:04:00 -08:00
|
|
|
pvs = append(pvs, buildNamespacedPVObj(info.PolicyName, owner, info.Rules))
|
2019-11-12 14:58:38 -08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-12 20:19:20 -08:00
|
|
|
func createNamespacedPV(dclient *dclient.Client, pvLister kyvernolister.NamespacedPolicyViolationLister, pvInterface kyvernov1alpha1.KyvernoV1alpha1Interface, pvs []kyverno.NamespacedPolicyViolation) error {
|
2019-11-12 13:32:30 -08:00
|
|
|
for _, newPv := range pvs {
|
|
|
|
glog.V(4).Infof("creating namespaced policyViolation resource for policy %s and resource %s", newPv.Spec.Policy, newPv.Spec.ResourceSpec.ToKey())
|
|
|
|
// check if there was a previous policy voilation for policy & resource combination
|
|
|
|
curPv, err := getExistingNamespacedPVIfAny(pvLister, newPv)
|
|
|
|
if err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-11-12 20:19:20 -08:00
|
|
|
// no existing policy violation, create a new one
|
|
|
|
if reflect.DeepEqual(curPv, kyverno.NamespacedPolicyViolation{}) {
|
2019-11-12 13:32:30 -08:00
|
|
|
glog.V(4).Infof("creating new namespaced policy violation for policy %s & resource %s", newPv.Spec.Policy, newPv.Spec.ResourceSpec.ToKey())
|
2019-11-12 20:19:20 -08:00
|
|
|
|
|
|
|
if err := retryGetResource(dclient, newPv.Spec.ResourceSpec); err != nil {
|
|
|
|
return err
|
2019-11-12 13:32:30 -08:00
|
|
|
}
|
2019-11-12 20:19:20 -08:00
|
|
|
|
|
|
|
if _, err := pvInterface.NamespacedPolicyViolations(newPv.Spec.ResourceSpec.Namespace).Create(&newPv); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.Infof("namespaced policy violation created for resource %s", newPv.Spec.ResourceSpec.ToKey())
|
2019-11-12 13:32:30 -08:00
|
|
|
}
|
2019-11-12 20:19:20 -08:00
|
|
|
|
2019-11-12 13:32:30 -08:00
|
|
|
// compare the policyviolation spec for existing resource if present else
|
|
|
|
if reflect.DeepEqual(curPv.Spec, newPv.Spec) {
|
|
|
|
// if they are equal there has been no change so dont update the polivy violation
|
|
|
|
glog.V(3).Infof("namespaced policy violation '%s' spec did not change so not updating it", newPv.Spec.ToKey())
|
|
|
|
glog.V(4).Infof("namespaced policy violation spec %v did not change so not updating it", newPv.Spec)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// spec changed so update the policyviolation
|
|
|
|
glog.V(4).Infof("creating new policy violation for policy %s & resource %s", curPv.Spec.Policy, curPv.Spec.ResourceSpec.ToKey())
|
|
|
|
//TODO: using a generic name, but would it be helpful to have naming convention for policy violations
|
|
|
|
// as we can only have one policy violation for each (policy + resource) combination
|
2019-11-12 20:19:20 -08:00
|
|
|
if _, err = pvInterface.NamespacedPolicyViolations(newPv.Spec.ResourceSpec.Namespace).Update(&newPv); err != nil {
|
|
|
|
return err
|
2019-11-12 13:32:30 -08:00
|
|
|
}
|
|
|
|
glog.Infof("namespaced policy violation updated for resource %s", newPv.Spec.ResourceSpec.ToKey())
|
|
|
|
}
|
2019-11-12 20:19:20 -08:00
|
|
|
return nil
|
2019-11-12 13:32:30 -08:00
|
|
|
}
|
|
|
|
|
2019-11-12 20:19:20 -08:00
|
|
|
func getExistingNamespacedPVIfAny(nspvLister kyvernolister.NamespacedPolicyViolationLister, newPv kyverno.NamespacedPolicyViolation) (kyverno.NamespacedPolicyViolation, error) {
|
2019-11-12 13:32:30 -08:00
|
|
|
// TODO(shuting): list pvs by labels
|
|
|
|
pvs, err := nspvLister.List(labels.NewSelector())
|
|
|
|
if err != nil {
|
2019-11-12 20:19:20 -08:00
|
|
|
return kyverno.NamespacedPolicyViolation{}, fmt.Errorf("failed to list namespaced policy violations err: %v", err)
|
2019-11-12 13:32:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, pv := range pvs {
|
|
|
|
if pv.Spec.Policy == newPv.Spec.Policy && reflect.DeepEqual(pv.Spec.ResourceSpec, newPv.Spec.ResourceSpec) {
|
2019-11-12 20:19:20 -08:00
|
|
|
return *pv, nil
|
2019-11-12 13:32:30 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 20:19:20 -08:00
|
|
|
return kyverno.NamespacedPolicyViolation{}, nil
|
2019-11-12 13:32:30 -08:00
|
|
|
}
|