2022-05-16 09:56:16 +02:00
|
|
|
package policycache
|
|
|
|
|
|
|
|
import (
|
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2023-10-30 00:59:53 +01:00
|
|
|
"github.com/kyverno/kyverno/ext/wildcard"
|
2023-03-13 15:44:39 +01:00
|
|
|
"github.com/kyverno/kyverno/pkg/clients/dclient"
|
2023-03-17 12:21:26 +01:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2023-03-21 14:28:00 +01:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2022-05-16 09:56:16 +02:00
|
|
|
)
|
|
|
|
|
2023-03-13 15:44:39 +01:00
|
|
|
type ResourceFinder interface {
|
2023-03-23 07:12:44 +01:00
|
|
|
FindResources(group, version, kind, subresource string) (map[dclient.TopLevelApiDescription]metav1.APIResource, error)
|
2023-03-13 15:44:39 +01:00
|
|
|
}
|
|
|
|
|
2022-05-16 09:56:16 +02:00
|
|
|
// Cache get method use for to get policy names and mostly use to test cache testcases
|
|
|
|
type Cache interface {
|
2022-05-16 18:36:19 +02:00
|
|
|
// Set inserts a policy in the cache
|
2023-03-13 15:44:39 +01:00
|
|
|
Set(string, kyvernov1.PolicyInterface, ResourceFinder) error
|
2022-05-16 18:36:19 +02:00
|
|
|
// Unset removes a policy from the cache
|
|
|
|
Unset(string)
|
2022-05-16 09:56:16 +02:00
|
|
|
// GetPolicies returns all policies that apply to a namespace, including cluster-wide policies
|
|
|
|
// If the namespace is empty, only cluster-wide policies are returned
|
2023-03-21 14:28:00 +01:00
|
|
|
GetPolicies(PolicyType, schema.GroupVersionResource, string, string) []kyvernov1.PolicyInterface
|
2022-05-16 09:56:16 +02:00
|
|
|
}
|
|
|
|
|
2022-05-16 18:36:19 +02:00
|
|
|
type cache struct {
|
|
|
|
store store
|
2022-05-16 09:56:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCache create a new Cache
|
2022-05-16 18:36:19 +02:00
|
|
|
func NewCache() Cache {
|
|
|
|
return &cache{
|
|
|
|
store: newPolicyCache(),
|
2022-05-16 09:56:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:44:39 +01:00
|
|
|
func (c *cache) Set(key string, policy kyvernov1.PolicyInterface, client ResourceFinder) error {
|
|
|
|
return c.store.set(key, policy, client)
|
2022-05-16 18:36:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cache) Unset(key string) {
|
|
|
|
c.store.unset(key)
|
|
|
|
}
|
|
|
|
|
2023-03-21 14:28:00 +01:00
|
|
|
func (c *cache) GetPolicies(pkey PolicyType, gvr schema.GroupVersionResource, subresource string, nspace string) []kyvernov1.PolicyInterface {
|
2022-05-16 09:56:16 +02:00
|
|
|
var result []kyvernov1.PolicyInterface
|
2023-03-21 14:28:00 +01:00
|
|
|
result = append(result, c.store.get(pkey, gvr, subresource, "")...)
|
2022-05-16 09:56:16 +02:00
|
|
|
if nspace != "" {
|
2023-03-21 14:28:00 +01:00
|
|
|
result = append(result, c.store.get(pkey, gvr, subresource, nspace)...)
|
2022-05-16 09:56:16 +02:00
|
|
|
}
|
2023-03-13 15:44:39 +01:00
|
|
|
// also get policies with ValidateEnforce
|
|
|
|
if pkey == ValidateAudit {
|
2023-03-21 14:28:00 +01:00
|
|
|
result = append(result, c.store.get(ValidateEnforce, gvr, subresource, "")...)
|
2022-10-13 13:29:10 +05:30
|
|
|
}
|
|
|
|
if pkey == ValidateAudit || pkey == ValidateEnforce {
|
2023-03-13 15:44:39 +01:00
|
|
|
result = filterPolicies(pkey, result, nspace)
|
2022-10-13 13:29:10 +05:30
|
|
|
}
|
2022-05-16 09:56:16 +02:00
|
|
|
return result
|
|
|
|
}
|
2022-10-13 13:29:10 +05:30
|
|
|
|
|
|
|
// Filter cluster policies using validationFailureAction override
|
2023-03-13 15:44:39 +01:00
|
|
|
func filterPolicies(pkey PolicyType, result []kyvernov1.PolicyInterface, nspace string) []kyvernov1.PolicyInterface {
|
2022-10-13 13:29:10 +05:30
|
|
|
var policies []kyvernov1.PolicyInterface
|
|
|
|
for _, policy := range result {
|
|
|
|
keepPolicy := true
|
|
|
|
switch pkey {
|
|
|
|
case ValidateAudit:
|
2022-11-01 09:56:52 +00:00
|
|
|
keepPolicy = checkValidationFailureActionOverrides(false, nspace, policy)
|
2022-10-13 13:29:10 +05:30
|
|
|
case ValidateEnforce:
|
2022-11-01 09:56:52 +00:00
|
|
|
keepPolicy = checkValidationFailureActionOverrides(true, nspace, policy)
|
2022-10-13 13:29:10 +05:30
|
|
|
}
|
2023-03-13 15:44:39 +01:00
|
|
|
// add policy to result
|
|
|
|
if keepPolicy {
|
2022-10-13 13:29:10 +05:30
|
|
|
policies = append(policies, policy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return policies
|
|
|
|
}
|
|
|
|
|
2022-11-01 09:56:52 +00:00
|
|
|
func checkValidationFailureActionOverrides(enforce bool, ns string, policy kyvernov1.PolicyInterface) bool {
|
2022-10-13 13:29:10 +05:30
|
|
|
validationFailureAction := policy.GetSpec().ValidationFailureAction
|
|
|
|
validationFailureActionOverrides := policy.GetSpec().ValidationFailureActionOverrides
|
2022-11-01 09:56:52 +00:00
|
|
|
if validationFailureAction.Enforce() != enforce && (ns == "" || len(validationFailureActionOverrides) == 0) {
|
2022-10-13 13:29:10 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, action := range validationFailureActionOverrides {
|
2022-12-22 07:39:54 +01:00
|
|
|
if action.Action.Enforce() != enforce && wildcard.CheckPatterns(action.Namespaces, ns) {
|
2022-10-13 13:29:10 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|