2020-03-11 18:14:23 -07:00
|
|
|
package validate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-10-07 11:12:31 -07:00
|
|
|
kyverno "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
|
|
|
|
commonAnchors "github.com/kyverno/kyverno/pkg/engine/anchor/common"
|
|
|
|
"github.com/kyverno/kyverno/pkg/policy/common"
|
2020-03-11 18:14:23 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Validate provides implementation to validate 'validate' rule
|
|
|
|
type Validate struct {
|
|
|
|
// rule to hold 'validate' rule specifications
|
|
|
|
rule kyverno.Validation
|
|
|
|
}
|
|
|
|
|
|
|
|
//NewValidateFactory returns a new instance of Mutate validation checker
|
|
|
|
func NewValidateFactory(rule kyverno.Validation) *Validate {
|
|
|
|
m := Validate{
|
|
|
|
rule: rule,
|
|
|
|
}
|
|
|
|
return &m
|
|
|
|
}
|
|
|
|
|
|
|
|
//Validate validates the 'validate' rule
|
|
|
|
func (v *Validate) Validate() (string, error) {
|
|
|
|
rule := v.rule
|
|
|
|
if err := v.validateOverlayPattern(); err != nil {
|
|
|
|
// no need to proceed ahead
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule.Pattern != nil {
|
2020-08-29 06:52:22 +05:30
|
|
|
if path, err := common.ValidatePattern(rule.Pattern, "/", []commonAnchors.IsAnchor{commonAnchors.IsConditionAnchor, commonAnchors.IsExistenceAnchor, commonAnchors.IsEqualityAnchor, commonAnchors.IsNegationAnchor}); err != nil {
|
2020-03-11 18:14:23 -07:00
|
|
|
return fmt.Sprintf("pattern.%s", path), err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 16:25:51 -08:00
|
|
|
if rule.AnyPattern != nil {
|
|
|
|
anyPattern, err := rule.DeserializeAnyPattern()
|
|
|
|
if err != nil {
|
2021-02-19 09:09:41 -08:00
|
|
|
return "anyPattern", fmt.Errorf("failed to deserialize anyPattern, expect array: %v", err)
|
2020-11-13 16:25:51 -08:00
|
|
|
}
|
|
|
|
for i, pattern := range anyPattern {
|
2020-08-29 06:52:22 +05:30
|
|
|
if path, err := common.ValidatePattern(pattern, "/", []commonAnchors.IsAnchor{commonAnchors.IsConditionAnchor, commonAnchors.IsExistenceAnchor, commonAnchors.IsEqualityAnchor, commonAnchors.IsNegationAnchor}); err != nil {
|
2020-03-11 18:14:23 -07:00
|
|
|
return fmt.Sprintf("anyPattern[%d].%s", i, path), err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateOverlayPattern checks one of pattern/anyPattern must exist
|
|
|
|
func (v *Validate) validateOverlayPattern() error {
|
|
|
|
rule := v.rule
|
2020-11-13 16:25:51 -08:00
|
|
|
if rule.Pattern == nil && rule.AnyPattern == nil && rule.Deny == nil {
|
2020-04-27 16:02:02 +05:30
|
|
|
return fmt.Errorf("pattern, anyPattern or deny must be specified")
|
2020-03-11 18:14:23 -07:00
|
|
|
}
|
|
|
|
|
2020-11-13 16:25:51 -08:00
|
|
|
if rule.Pattern != nil && rule.AnyPattern != nil {
|
2020-03-11 18:14:23 -07:00
|
|
|
return fmt.Errorf("only one operation allowed per validation rule(pattern or anyPattern)")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|