2019-05-13 18:17:28 -07:00
|
|
|
package controller
|
2019-05-09 22:26:22 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
types "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-10 12:36:55 -07:00
|
|
|
event "github.com/nirmata/kube-policy/pkg/event"
|
2019-05-13 18:17:28 -07:00
|
|
|
violation "github.com/nirmata/kube-policy/pkg/violation"
|
2019-05-09 22:26:22 -07:00
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
|
|
|
)
|
|
|
|
|
2019-05-10 12:36:55 -07:00
|
|
|
func (pc *PolicyController) runForPolicy(key string) {
|
|
|
|
|
|
|
|
policy, err := pc.getPolicyByKey(key)
|
2019-05-09 22:26:22 -07:00
|
|
|
if err != nil {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("invalid resource key: %s, err: %v", key, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if policy == nil {
|
2019-05-10 12:36:55 -07:00
|
|
|
pc.logger.Printf("Counld not find policy by key %s", key)
|
2019-05-09 22:26:22 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-10 12:36:55 -07:00
|
|
|
violations, events, err := pc.processPolicy(*policy)
|
2019-05-09 22:26:22 -07:00
|
|
|
if err != nil {
|
|
|
|
// add Error processing policy event
|
|
|
|
}
|
|
|
|
|
2019-05-10 12:36:55 -07:00
|
|
|
pc.logger.Printf("%v, %v", violations, events)
|
2019-05-09 22:26:22 -07:00
|
|
|
// TODO:
|
|
|
|
// create violations
|
2019-05-10 12:36:55 -07:00
|
|
|
// pc.violationBuilder.Add()
|
2019-05-09 22:26:22 -07:00
|
|
|
// create events
|
2019-05-10 12:36:55 -07:00
|
|
|
// pc.eventBuilder.Add()
|
2019-05-09 22:26:22 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// processPolicy process the policy to all the matched resources
|
2019-05-10 12:36:55 -07:00
|
|
|
func (pc *PolicyController) processPolicy(policy types.Policy) (
|
2019-05-13 18:17:28 -07:00
|
|
|
violations []violation.Info, events []event.Info, err error) {
|
2019-05-09 22:26:22 -07:00
|
|
|
|
|
|
|
for _, rule := range policy.Spec.Rules {
|
2019-05-10 12:36:55 -07:00
|
|
|
resources, err := pc.filterResourceByRule(rule)
|
2019-05-09 22:26:22 -07:00
|
|
|
if err != nil {
|
2019-05-10 12:36:55 -07:00
|
|
|
pc.logger.Printf("Failed to filter resources by rule %s, err: %v\n", rule.Name, err)
|
2019-05-09 22:26:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, resource := range resources {
|
|
|
|
rawResource, err := json.Marshal(resource)
|
|
|
|
if err != nil {
|
2019-05-10 12:36:55 -07:00
|
|
|
pc.logger.Printf("Failed to marshal resources map to rule %s, err: %v\n", rule.Name, err)
|
2019-05-09 22:26:22 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-05-10 12:36:55 -07:00
|
|
|
violation, eventInfos, err := pc.policyEngine.ProcessExisting(policy, rawResource)
|
2019-05-09 22:26:22 -07:00
|
|
|
if err != nil {
|
2019-05-10 12:36:55 -07:00
|
|
|
pc.logger.Printf("Failed to process rule %s, err: %v\n", rule.Name, err)
|
2019-05-09 22:26:22 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
violations = append(violations, violation...)
|
|
|
|
events = append(events, eventInfos...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return violations, events, nil
|
|
|
|
}
|
|
|
|
|
2019-05-13 21:27:47 +03:00
|
|
|
func (pc *PolicyController) filterResourceByRule(rule types.Rule) ([]runtime.Object, error) {
|
2019-05-09 22:26:22 -07:00
|
|
|
var targetResources []runtime.Object
|
|
|
|
// TODO: make this namespace all
|
|
|
|
var namespace = "default"
|
|
|
|
if err := rule.Validate(); err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid rule detected: %s, err: %v", rule.Name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the resource list from kind
|
2019-05-13 21:27:47 +03:00
|
|
|
resources, err := pc.kubeClient.ListResource(rule.ResourceDescription.Kind, namespace)
|
2019-05-09 22:26:22 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, resource := range resources {
|
|
|
|
// TODO:
|
|
|
|
rawResource, err := json.Marshal(resource)
|
|
|
|
// objKind := resource.GetObjectKind()
|
|
|
|
// codecFactory := serializer.NewCodecFactory(runtime.NewScheme())
|
|
|
|
// codecFactory.EncoderForVersion()
|
|
|
|
|
|
|
|
if err != nil {
|
2019-05-10 12:36:55 -07:00
|
|
|
pc.logger.Printf("failed to marshal object %v", resource)
|
2019-05-09 22:26:22 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter the resource by name and label
|
2019-05-13 21:27:47 +03:00
|
|
|
if ok, _ := mutation.IsRuleApplicableToResource(rawResource, rule.ResourceDescription); ok {
|
2019-05-09 22:26:22 -07:00
|
|
|
targetResources = append(targetResources, resource)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return targetResources, nil
|
|
|
|
}
|
|
|
|
|
2019-05-10 12:36:55 -07:00
|
|
|
func (pc *PolicyController) getPolicyByKey(key string) (*types.Policy, error) {
|
2019-05-09 22:26:22 -07:00
|
|
|
// Create nil Selector to grab all the policies
|
|
|
|
selector := labels.NewSelector()
|
2019-05-10 12:36:55 -07:00
|
|
|
cachedPolicies, err := pc.policyLister.List(selector)
|
2019-05-09 22:26:22 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, elem := range cachedPolicies {
|
|
|
|
if elem.Name == key {
|
|
|
|
return elem, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|