2019-12-30 17:08:50 -08:00
|
|
|
package response
|
2019-08-23 18:34:23 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
)
|
|
|
|
|
2019-10-08 10:57:24 -07:00
|
|
|
//EngineResponse engine response to the action
|
|
|
|
type EngineResponse struct {
|
2019-08-23 18:34:23 -07:00
|
|
|
// Resource patched with the engine action changes
|
|
|
|
PatchedResource unstructured.Unstructured
|
|
|
|
// Policy Response
|
|
|
|
PolicyResponse PolicyResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
//PolicyResponse policy application response
|
|
|
|
type PolicyResponse struct {
|
|
|
|
// policy name
|
2019-08-29 11:44:50 -07:00
|
|
|
Policy string `json:"policy"`
|
2019-08-23 18:34:23 -07:00
|
|
|
// resource details
|
2019-08-29 11:44:50 -07:00
|
|
|
Resource ResourceSpec `json:"resource"`
|
2019-08-23 18:34:23 -07:00
|
|
|
// policy statistics
|
2019-08-29 18:48:58 -07:00
|
|
|
PolicyStats `json:",inline"`
|
2019-08-23 18:34:23 -07:00
|
|
|
// rule response
|
2019-08-29 11:44:50 -07:00
|
|
|
Rules []RuleResponse `json:"rules"`
|
2019-09-06 10:18:45 -07:00
|
|
|
// ValidationFailureAction: audit(default if not set),enforce
|
2019-08-23 18:34:23 -07:00
|
|
|
ValidationFailureAction string
|
|
|
|
}
|
|
|
|
|
|
|
|
//ResourceSpec resource action applied on
|
|
|
|
type ResourceSpec struct {
|
2019-08-29 11:44:50 -07:00
|
|
|
Kind string `json:"kind"`
|
|
|
|
APIVersion string `json:"apiVersion"`
|
|
|
|
Namespace string `json:"namespace"`
|
|
|
|
Name string `json:"name"`
|
2020-12-21 11:04:19 -08:00
|
|
|
|
|
|
|
// UID is not used to build the unique identifier
|
|
|
|
// optional
|
|
|
|
UID string `json:"uid"`
|
2019-08-23 18:34:23 -07:00
|
|
|
}
|
2019-12-26 11:50:41 -08:00
|
|
|
|
|
|
|
//GetKey returns the key
|
|
|
|
func (rs ResourceSpec) GetKey() string {
|
|
|
|
return rs.Kind + "/" + rs.Namespace + "/" + rs.Name
|
|
|
|
}
|
2019-08-23 18:34:23 -07:00
|
|
|
|
|
|
|
//PolicyStats stores statistics for the single policy application
|
|
|
|
type PolicyStats struct {
|
|
|
|
// time required to process the policy rules on a resource
|
2019-08-29 11:44:50 -07:00
|
|
|
ProcessingTime time.Duration `json:"processingTime"`
|
2020-01-24 12:05:53 -08:00
|
|
|
// Count of rules that were applied successfully
|
2019-08-29 11:44:50 -07:00
|
|
|
RulesAppliedCount int `json:"rulesAppliedCount"`
|
2019-08-23 18:34:23 -07:00
|
|
|
}
|
|
|
|
|
2020-12-21 11:04:19 -08:00
|
|
|
//RuleResponse details for each rule application
|
2019-08-23 18:34:23 -07:00
|
|
|
type RuleResponse struct {
|
|
|
|
// rule name specified in policy
|
2019-08-29 11:44:50 -07:00
|
|
|
Name string `json:"name"`
|
2019-08-23 18:34:23 -07:00
|
|
|
// rule type (Mutation,Generation,Validation) for Kyverno Policy
|
2019-08-29 11:44:50 -07:00
|
|
|
Type string `json:"type"`
|
2019-08-23 18:34:23 -07:00
|
|
|
// message response from the rule application
|
2019-08-29 11:44:50 -07:00
|
|
|
Message string `json:"message"`
|
2019-08-23 18:34:23 -07:00
|
|
|
// JSON patches, for mutation rules
|
2019-08-29 11:44:50 -07:00
|
|
|
Patches [][]byte `json:"patches,omitempty"`
|
2019-08-23 18:34:23 -07:00
|
|
|
// success/fail
|
2019-08-29 11:44:50 -07:00
|
|
|
Success bool `json:"success"`
|
2019-08-23 18:34:23 -07:00
|
|
|
// statistics
|
2019-08-29 18:48:58 -07:00
|
|
|
RuleStats `json:",inline"`
|
2019-08-23 18:34:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//ToString ...
|
|
|
|
func (rr RuleResponse) ToString() string {
|
|
|
|
return fmt.Sprintf("rule %s (%s): %v", rr.Name, rr.Type, rr.Message)
|
|
|
|
}
|
|
|
|
|
2020-06-30 11:53:27 -07:00
|
|
|
//RuleStats stores the statistics for the single rule application
|
2019-08-23 18:34:23 -07:00
|
|
|
type RuleStats struct {
|
2020-06-30 11:53:27 -07:00
|
|
|
// time required to apply the rule on the resource
|
2019-08-29 11:44:50 -07:00
|
|
|
ProcessingTime time.Duration `json:"processingTime"`
|
2019-08-23 18:34:23 -07:00
|
|
|
}
|
|
|
|
|
2020-06-30 11:53:27 -07:00
|
|
|
//IsSuccessful checks if any rule has failed or not
|
|
|
|
func (er EngineResponse) IsSuccessful() bool {
|
2019-08-23 18:34:23 -07:00
|
|
|
for _, r := range er.PolicyResponse.Rules {
|
|
|
|
if !r.Success {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
//GetPatches returns all the patches joined
|
2019-10-08 10:57:24 -07:00
|
|
|
func (er EngineResponse) GetPatches() [][]byte {
|
2019-08-23 18:34:23 -07:00
|
|
|
var patches [][]byte
|
|
|
|
for _, r := range er.PolicyResponse.Rules {
|
|
|
|
if r.Patches != nil {
|
|
|
|
patches = append(patches, r.Patches...)
|
|
|
|
}
|
|
|
|
}
|
2020-06-30 11:53:27 -07:00
|
|
|
|
2019-08-26 16:10:19 -07:00
|
|
|
return patches
|
2019-08-23 18:34:23 -07:00
|
|
|
}
|
2019-08-26 13:34:42 -07:00
|
|
|
|
|
|
|
//GetFailedRules returns failed rules
|
2019-10-08 10:57:24 -07:00
|
|
|
func (er EngineResponse) GetFailedRules() []string {
|
2019-08-26 13:34:42 -07:00
|
|
|
return er.getRules(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
//GetSuccessRules returns success rules
|
2019-10-08 10:57:24 -07:00
|
|
|
func (er EngineResponse) GetSuccessRules() []string {
|
2019-08-26 13:34:42 -07:00
|
|
|
return er.getRules(true)
|
|
|
|
}
|
|
|
|
|
2020-12-21 11:04:19 -08:00
|
|
|
// GetResourceSpec returns resourceSpec of er
|
|
|
|
func (er EngineResponse) GetResourceSpec() ResourceSpec {
|
|
|
|
return ResourceSpec{
|
|
|
|
Kind: er.PatchedResource.GetKind(),
|
|
|
|
APIVersion: er.PatchedResource.GetAPIVersion(),
|
|
|
|
Namespace: er.PatchedResource.GetNamespace(),
|
|
|
|
Name: er.PatchedResource.GetName(),
|
|
|
|
UID: string(er.PatchedResource.GetUID()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 10:57:24 -07:00
|
|
|
func (er EngineResponse) getRules(success bool) []string {
|
2019-08-26 13:34:42 -07:00
|
|
|
var rules []string
|
|
|
|
for _, r := range er.PolicyResponse.Rules {
|
2020-07-09 11:48:34 -07:00
|
|
|
if r.Success == success {
|
2019-08-26 13:34:42 -07:00
|
|
|
rules = append(rules, r.Name)
|
|
|
|
}
|
|
|
|
}
|
2020-06-30 11:53:27 -07:00
|
|
|
|
2019-08-26 13:34:42 -07:00
|
|
|
return rules
|
|
|
|
}
|