2019-08-13 11:32:12 -07:00
package policy
import (
2019-12-26 11:50:41 -08:00
"encoding/json"
2019-08-26 13:34:42 -07:00
"fmt"
"reflect"
2019-12-26 11:50:41 -08:00
"strings"
2019-08-13 11:32:12 -07:00
"time"
jsonpatch "github.com/evanphx/json-patch"
"github.com/golang/glog"
2019-11-13 13:41:08 -08:00
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
2019-08-13 11:32:12 -07:00
"github.com/nirmata/kyverno/pkg/engine"
2019-12-30 17:08:50 -08:00
"github.com/nirmata/kyverno/pkg/engine/context"
"github.com/nirmata/kyverno/pkg/engine/response"
2019-08-13 11:32:12 -07:00
"github.com/nirmata/kyverno/pkg/utils"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
// applyPolicy applies policy on a resource
//TODO: generation rules
2020-02-22 23:35:02 +05:30
func applyPolicy ( policy kyverno . ClusterPolicy , resource unstructured . Unstructured ) ( responses [ ] response . EngineResponse ) {
2019-08-13 11:32:12 -07:00
startTime := time . Now ( )
2020-02-10 20:29:40 +05:30
2019-08-13 11:32:12 -07:00
glog . V ( 4 ) . Infof ( "Started apply policy %s on resource %s/%s/%s (%v)" , policy . Name , resource . GetKind ( ) , resource . GetNamespace ( ) , resource . GetName ( ) , startTime )
defer func ( ) {
glog . V ( 4 ) . Infof ( "Finished applying %s on resource %s/%s/%s (%v)" , policy . Name , resource . GetKind ( ) , resource . GetNamespace ( ) , resource . GetName ( ) , time . Since ( startTime ) )
} ( )
2019-08-26 13:34:42 -07:00
2019-12-30 17:08:50 -08:00
var engineResponses [ ] response . EngineResponse
var engineResponse response . EngineResponse
2019-08-26 13:34:42 -07:00
var err error
2019-12-30 17:08:50 -08:00
// build context
ctx := context . NewContext ( )
ctx . AddResource ( transformResource ( resource ) )
2019-08-13 17:24:05 -07:00
2019-08-13 11:32:12 -07:00
//MUTATION
2020-02-22 23:35:02 +05:30
engineResponse , err = mutation ( policy , resource , ctx )
2019-08-26 13:34:42 -07:00
engineResponses = append ( engineResponses , engineResponse )
2019-08-13 11:32:12 -07:00
if err != nil {
2019-08-30 01:08:54 -07:00
glog . Errorf ( "unable to process mutation rules: %v" , err )
2019-08-13 11:32:12 -07:00
}
//VALIDATION
2019-12-30 17:08:50 -08:00
engineResponse = engine . Validate ( engine . PolicyContext { Policy : policy , Context : ctx , NewResource : resource } )
2019-08-26 13:34:42 -07:00
engineResponses = append ( engineResponses , engineResponse )
2019-08-13 11:32:12 -07:00
//TODO: GENERATION
2019-08-26 13:34:42 -07:00
return engineResponses
2019-08-13 11:32:12 -07:00
}
2020-02-22 23:35:02 +05:30
func mutation ( policy kyverno . ClusterPolicy , resource unstructured . Unstructured , ctx context . EvalInterface ) ( response . EngineResponse , error ) {
2019-12-30 17:08:50 -08:00
engineResponse := engine . Mutate ( engine . PolicyContext { Policy : policy , NewResource : resource , Context : ctx } )
2019-08-26 13:34:42 -07:00
if ! engineResponse . IsSuccesful ( ) {
glog . V ( 4 ) . Infof ( "mutation had errors reporting them" )
return engineResponse , nil
2019-08-20 16:57:19 -07:00
}
2019-08-26 13:34:42 -07:00
// Verify if the JSON pathes returned by the Mutate are already applied to the resource
if reflect . DeepEqual ( resource , engineResponse . PatchedResource ) {
// resources matches
glog . V ( 4 ) . Infof ( "resource %s/%s/%s satisfies policy %s" , engineResponse . PolicyResponse . Resource . Kind , engineResponse . PolicyResponse . Resource . Namespace , engineResponse . PolicyResponse . Resource . Name , engineResponse . PolicyResponse . Policy )
return engineResponse , nil
2019-08-20 16:57:19 -07:00
}
2019-08-26 13:34:42 -07:00
return getFailedOverallRuleInfo ( resource , engineResponse )
}
2019-08-20 16:57:19 -07:00
2019-08-26 13:34:42 -07:00
// getFailedOverallRuleInfo gets detailed info for over-all mutation failure
2019-12-30 17:08:50 -08:00
func getFailedOverallRuleInfo ( resource unstructured . Unstructured , engineResponse response . EngineResponse ) ( response . EngineResponse , error ) {
2019-08-26 13:34:42 -07:00
rawResource , err := resource . MarshalJSON ( )
if err != nil {
glog . V ( 4 ) . Infof ( "unable to marshal resource: %v\n" , err )
2019-12-30 17:08:50 -08:00
return response . EngineResponse { } , err
2019-08-13 11:32:12 -07:00
}
2019-08-26 13:34:42 -07:00
// resource does not match so there was a mutation rule violated
for index , rule := range engineResponse . PolicyResponse . Rules {
2019-08-26 16:10:19 -07:00
glog . V ( 4 ) . Infof ( "veriying if policy %s rule %s was applied before to resource %s/%s/%s" , engineResponse . PolicyResponse . Policy , rule . Name , engineResponse . PolicyResponse . Resource . Kind , engineResponse . PolicyResponse . Resource . Namespace , engineResponse . PolicyResponse . Resource . Name )
2019-08-26 13:34:42 -07:00
if len ( rule . Patches ) == 0 {
continue
2019-08-13 11:32:12 -07:00
}
2019-08-26 13:34:42 -07:00
patch , err := jsonpatch . DecodePatch ( utils . JoinPatches ( rule . Patches ) )
if err != nil {
glog . V ( 4 ) . Infof ( "unable to decode patch %s: %v" , rule . Patches , err )
2019-12-30 17:08:50 -08:00
return response . EngineResponse { } , err
2019-08-26 13:34:42 -07:00
}
2019-08-13 11:32:12 -07:00
2019-08-26 13:34:42 -07:00
// apply the patches returned by mutate to the original resource
patchedResource , err := patch . Apply ( rawResource )
if err != nil {
glog . V ( 4 ) . Infof ( "unable to apply patch %s: %v" , rule . Patches , err )
2019-12-30 17:08:50 -08:00
return response . EngineResponse { } , err
2019-08-26 13:34:42 -07:00
}
if ! jsonpatch . Equal ( patchedResource , rawResource ) {
2020-01-24 12:05:53 -08:00
glog . V ( 4 ) . Infof ( "policy %s rule %s condition not satisfied by existing resource" , engineResponse . PolicyResponse . Policy , rule . Name )
2019-08-26 13:34:42 -07:00
engineResponse . PolicyResponse . Rules [ index ] . Success = false
2019-12-26 11:50:41 -08:00
engineResponse . PolicyResponse . Rules [ index ] . Message = fmt . Sprintf ( "mutation json patches not found at resource path %s" , extractPatchPath ( rule . Patches ) )
2019-08-26 13:34:42 -07:00
}
2019-08-13 11:32:12 -07:00
}
2019-08-26 13:34:42 -07:00
return engineResponse , nil
2019-08-13 11:32:12 -07:00
}
2019-12-26 11:50:41 -08:00
type jsonPatch struct {
Op string ` json:"op" `
Path string ` json:"path" `
Value interface { } ` json:"value" `
}
func extractPatchPath ( patches [ ] [ ] byte ) string {
var resultPath [ ] string
// extract the patch path and value
for _ , patch := range patches {
glog . V ( 4 ) . Infof ( "expected json patch not found in resource: %s" , string ( patch ) )
var data jsonPatch
if err := json . Unmarshal ( patch , & data ) ; err != nil {
glog . V ( 4 ) . Infof ( "Failed to decode the generated patch %v: Error %v" , string ( patch ) , err )
continue
}
resultPath = append ( resultPath , data . Path )
}
return strings . Join ( resultPath , ";" )
}