2019-12-30 17:08:50 -08:00
|
|
|
package response
|
2019-08-23 18:34:23 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2022-04-05 12:41:08 +02:00
|
|
|
"github.com/kyverno/go-wildcard"
|
2022-05-17 13:12:43 +02:00
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2019-08-23 18:34:23 -07:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
)
|
|
|
|
|
2022-05-17 08:19:03 +02:00
|
|
|
// EngineResponse engine response to the action
|
2019-10-08 10:57:24 -07:00
|
|
|
type EngineResponse struct {
|
2019-08-23 18:34:23 -07:00
|
|
|
// Resource patched with the engine action changes
|
|
|
|
PatchedResource unstructured.Unstructured
|
2021-12-30 00:34:43 +08:00
|
|
|
|
|
|
|
// Original policy
|
2022-05-17 13:12:43 +02:00
|
|
|
Policy kyvernov1.PolicyInterface
|
2021-12-30 00:34:43 +08:00
|
|
|
|
2019-08-23 18:34:23 -07:00
|
|
|
// Policy Response
|
|
|
|
PolicyResponse PolicyResponse
|
|
|
|
}
|
|
|
|
|
2022-05-17 08:19:03 +02:00
|
|
|
// PolicyResponse policy application response
|
2019-08-23 18:34:23 -07:00
|
|
|
type PolicyResponse struct {
|
2021-06-30 00:43:11 +03:00
|
|
|
// policy details
|
|
|
|
Policy PolicySpec `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"`
|
2021-07-09 18:01:46 -07:00
|
|
|
// ValidationFailureAction: audit (default) or enforce
|
2022-05-17 13:12:43 +02:00
|
|
|
ValidationFailureAction kyvernov1.ValidationFailureAction
|
2022-01-21 18:06:44 +05:30
|
|
|
|
|
|
|
ValidationFailureActionOverrides []ValidationFailureActionOverride
|
2019-08-23 18:34:23 -07:00
|
|
|
}
|
|
|
|
|
2022-05-17 08:19:03 +02:00
|
|
|
// PolicySpec policy
|
2021-06-30 00:43:11 +03:00
|
|
|
type PolicySpec struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Namespace string `json:"namespace"`
|
|
|
|
}
|
|
|
|
|
2022-05-17 08:19:03 +02:00
|
|
|
// ResourceSpec resource action applied on
|
2019-08-23 18:34:23 -07:00
|
|
|
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
|
|
|
|
2022-05-17 08:19:03 +02:00
|
|
|
// GetKey returns the key
|
2019-12-26 11:50:41 -08:00
|
|
|
func (rs ResourceSpec) GetKey() string {
|
|
|
|
return rs.Kind + "/" + rs.Namespace + "/" + rs.Name
|
|
|
|
}
|
2019-08-23 18:34:23 -07:00
|
|
|
|
2022-05-17 08:19:03 +02:00
|
|
|
// PolicyStats stores statistics for the single policy application
|
2019-08-23 18:34:23 -07:00
|
|
|
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"`
|
2021-09-26 02:12:31 -07:00
|
|
|
|
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"`
|
2021-09-26 02:12:31 -07:00
|
|
|
|
|
|
|
// Count of rules that with execution errors
|
|
|
|
RulesErrorCount int `json:"rulesErrorCount"`
|
|
|
|
|
2021-05-15 19:15:04 +05:30
|
|
|
// Timestamp of the instant the Policy was triggered
|
|
|
|
PolicyExecutionTimestamp int64 `json:"policyExecutionTimestamp"`
|
2019-08-23 18:34:23 -07:00
|
|
|
}
|
|
|
|
|
2022-04-06 21:04:08 +02:00
|
|
|
type RuleType string
|
|
|
|
|
|
|
|
const (
|
2022-05-17 08:19:03 +02:00
|
|
|
// Mutation type for mutation rule
|
2022-04-06 21:04:08 +02:00
|
|
|
Mutation RuleType = "Mutation"
|
2022-05-17 08:19:03 +02:00
|
|
|
// Validation type for validation rule
|
2022-04-06 21:04:08 +02:00
|
|
|
Validation RuleType = "Validation"
|
2022-05-17 08:19:03 +02:00
|
|
|
// Generation type for generation rule
|
2022-04-06 21:04:08 +02:00
|
|
|
Generation RuleType = "Generation"
|
|
|
|
// ImageVerify type for image verification
|
2022-04-27 08:09:52 -07:00
|
|
|
ImageVerify RuleType = "ImageVerify"
|
2022-04-06 21:04:08 +02:00
|
|
|
)
|
|
|
|
|
2022-05-17 08:19:03 +02: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"`
|
2021-09-26 02:12:31 -07:00
|
|
|
|
2019-08-23 18:34:23 -07:00
|
|
|
// rule type (Mutation,Generation,Validation) for Kyverno Policy
|
2022-04-06 21:04:08 +02:00
|
|
|
Type RuleType `json:"type"`
|
2021-09-26 02:12:31 -07:00
|
|
|
|
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"`
|
2021-09-26 02:12:31 -07:00
|
|
|
|
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"`
|
2021-09-26 02:12:31 -07:00
|
|
|
|
|
|
|
// rule status
|
|
|
|
Status RuleStatus `json:"status"`
|
|
|
|
|
2019-08-23 18:34:23 -07:00
|
|
|
// statistics
|
2021-09-27 23:40:05 -07:00
|
|
|
RuleStats `json:",inline"`
|
2022-04-25 20:20:40 +08:00
|
|
|
|
|
|
|
// PatchedTarget is the patched resource for mutate.targets
|
|
|
|
PatchedTarget *unstructured.Unstructured
|
2019-08-23 18:34:23 -07:00
|
|
|
}
|
|
|
|
|
2022-05-17 08:19:03 +02:00
|
|
|
// ToString ...
|
2019-08-23 18:34:23 -07:00
|
|
|
func (rr RuleResponse) ToString() string {
|
|
|
|
return fmt.Sprintf("rule %s (%s): %v", rr.Name, rr.Type, rr.Message)
|
|
|
|
}
|
|
|
|
|
2022-05-17 08:19:03 +02: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"`
|
2021-05-15 19:15:04 +05:30
|
|
|
// Timestamp of the instant the rule got triggered
|
|
|
|
RuleExecutionTimestamp int64 `json:"ruleExecutionTimestamp"`
|
2019-08-23 18:34:23 -07:00
|
|
|
}
|
|
|
|
|
2022-05-17 08:19:03 +02:00
|
|
|
// IsSuccessful checks if any rule has failed or produced an error during execution
|
2020-06-30 11:53:27 -07:00
|
|
|
func (er EngineResponse) IsSuccessful() bool {
|
2019-08-23 18:34:23 -07:00
|
|
|
for _, r := range er.PolicyResponse.Rules {
|
2021-10-03 03:15:22 -07:00
|
|
|
if r.Status == RuleStatusFail || r.Status == RuleStatusError {
|
2019-08-23 18:34:23 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2021-09-26 02:12:31 -07:00
|
|
|
|
2019-08-23 18:34:23 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-05-17 08:19:03 +02:00
|
|
|
// IsFailed checks if any rule has succeeded or not
|
2021-06-30 00:43:11 +03:00
|
|
|
func (er EngineResponse) IsFailed() bool {
|
|
|
|
for _, r := range er.PolicyResponse.Rules {
|
2021-10-02 18:29:25 -07:00
|
|
|
if r.Status == RuleStatusFail {
|
|
|
|
return true
|
2021-06-30 00:43:11 +03:00
|
|
|
}
|
|
|
|
}
|
2021-09-26 02:12:31 -07:00
|
|
|
|
2021-10-02 18:29:25 -07:00
|
|
|
return false
|
2021-06-30 00:43:11 +03:00
|
|
|
}
|
|
|
|
|
2022-05-17 08:19:03 +02:00
|
|
|
// IsEmpty checks if any rule results are present
|
2022-04-27 08:09:52 -07:00
|
|
|
func (er EngineResponse) IsEmpty() bool {
|
|
|
|
return len(er.PolicyResponse.Rules) == 0
|
|
|
|
}
|
|
|
|
|
2022-05-17 08:19:03 +02:00
|
|
|
// 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
|
|
|
|
2022-05-17 08:19:03 +02:00
|
|
|
// GetFailedRules returns failed rules
|
2019-10-08 10:57:24 -07:00
|
|
|
func (er EngineResponse) GetFailedRules() []string {
|
2021-09-26 02:12:31 -07:00
|
|
|
return er.getRules(RuleStatusFail)
|
2019-08-26 13:34:42 -07:00
|
|
|
}
|
|
|
|
|
2022-05-17 08:19:03 +02:00
|
|
|
// GetSuccessRules returns success rules
|
2019-10-08 10:57:24 -07:00
|
|
|
func (er EngineResponse) GetSuccessRules() []string {
|
2021-09-26 02:12:31 -07:00
|
|
|
return er.getRules(RuleStatusPass)
|
2019-08-26 13:34:42 -07:00
|
|
|
}
|
|
|
|
|
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()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-26 02:12:31 -07:00
|
|
|
func (er EngineResponse) getRules(status RuleStatus) []string {
|
2019-08-26 13:34:42 -07:00
|
|
|
var rules []string
|
|
|
|
for _, r := range er.PolicyResponse.Rules {
|
2021-09-27 23:40:05 -07:00
|
|
|
if r.Status == status {
|
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
|
|
|
|
}
|
2022-01-21 18:06:44 +05:30
|
|
|
|
2022-05-17 13:12:43 +02:00
|
|
|
func (er *EngineResponse) GetValidationFailureAction() kyvernov1.ValidationFailureAction {
|
2022-04-05 12:41:08 +02:00
|
|
|
for _, v := range er.PolicyResponse.ValidationFailureActionOverrides {
|
2022-05-17 13:12:43 +02:00
|
|
|
if v.Action != kyvernov1.Enforce && v.Action != kyvernov1.Audit {
|
2022-04-05 12:41:08 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, ns := range v.Namespaces {
|
|
|
|
if wildcard.Match(ns, er.PatchedResource.GetNamespace()) {
|
|
|
|
return v.Action
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return er.PolicyResponse.ValidationFailureAction
|
|
|
|
}
|
|
|
|
|
2022-01-21 18:06:44 +05:30
|
|
|
type ValidationFailureActionOverride struct {
|
2022-05-17 13:12:43 +02:00
|
|
|
Action kyvernov1.ValidationFailureAction `json:"action"`
|
|
|
|
Namespaces []string `json:"namespaces"`
|
2022-01-21 18:06:44 +05:30
|
|
|
}
|