2019-12-30 17:08:50 -08:00
|
|
|
package policy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
|
2020-02-14 11:59:28 -08:00
|
|
|
"github.com/nirmata/kyverno/pkg/engine/context"
|
2019-12-30 17:08:50 -08:00
|
|
|
"github.com/nirmata/kyverno/pkg/engine/variables"
|
2020-03-17 16:25:34 -07:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
2019-12-30 17:08:50 -08:00
|
|
|
)
|
|
|
|
|
2020-05-06 19:46:32 +05:30
|
|
|
//ContainsVariablesOtherThanObject returns error if variable that does not start from request.object
|
2020-05-06 00:29:40 +05:30
|
|
|
func ContainsVariablesOtherThanObject(policy kyverno.ClusterPolicy) error {
|
2020-02-14 11:59:28 -08:00
|
|
|
var err error
|
2019-12-30 17:08:50 -08:00
|
|
|
for idx, rule := range policy.Spec.Rules {
|
2020-02-14 11:59:28 -08:00
|
|
|
if path := userInfoDefined(rule.MatchResources.UserInfo); path != "" {
|
2020-05-06 19:46:32 +05:30
|
|
|
return fmt.Errorf("invalid variable used at path: spec/rules[%d]/match/%s", idx, path)
|
2019-12-30 17:08:50 -08:00
|
|
|
}
|
|
|
|
|
2020-02-14 11:59:28 -08:00
|
|
|
if path := userInfoDefined(rule.ExcludeResources.UserInfo); path != "" {
|
2020-05-06 19:46:32 +05:30
|
|
|
return fmt.Errorf("invalid variable used at path: spec/rules[%d]/exclude/%s", idx, path)
|
2019-12-30 17:08:50 -08:00
|
|
|
}
|
|
|
|
|
2020-05-06 00:29:40 +05:30
|
|
|
filterVars := []string{"request.object"}
|
2020-02-14 11:59:28 -08:00
|
|
|
ctx := context.NewContext(filterVars...)
|
2020-01-07 15:13:57 -08:00
|
|
|
for condIdx, condition := range rule.Conditions {
|
2020-03-17 11:05:20 -07:00
|
|
|
if condition.Key, err = variables.SubstituteVars(log.Log, ctx, condition.Key); err != nil {
|
2020-05-06 19:46:32 +05:30
|
|
|
return fmt.Errorf("invalid variable used at spec/rules[%d]/condition[%d]/key", idx, condIdx)
|
2020-01-07 15:13:57 -08:00
|
|
|
}
|
2020-02-14 11:59:28 -08:00
|
|
|
|
2020-03-17 11:05:20 -07:00
|
|
|
if condition.Value, err = variables.SubstituteVars(log.Log, ctx, condition.Value); err != nil {
|
2020-05-06 19:46:32 +05:30
|
|
|
return fmt.Errorf("invalid variable used at spec/rules[%d]/condition[%d]/value", idx, condIdx)
|
2020-01-07 15:13:57 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-17 11:05:20 -07:00
|
|
|
if rule.Mutation.Overlay, err = variables.SubstituteVars(log.Log, ctx, rule.Mutation.Overlay); err != nil {
|
2020-05-06 19:46:32 +05:30
|
|
|
return fmt.Errorf("invalid variable used at spec/rules[%d]/mutate/overlay", idx)
|
2019-12-30 17:08:50 -08:00
|
|
|
}
|
2020-03-17 11:05:20 -07:00
|
|
|
if rule.Validation.Pattern, err = variables.SubstituteVars(log.Log, ctx, rule.Validation.Pattern); err != nil {
|
2020-05-06 19:46:32 +05:30
|
|
|
return fmt.Errorf("invalid variable used at spec/rules[%d]/validate/pattern", idx)
|
2019-12-30 17:08:50 -08:00
|
|
|
}
|
|
|
|
for idx2, pattern := range rule.Validation.AnyPattern {
|
2020-03-17 11:05:20 -07:00
|
|
|
if rule.Validation.AnyPattern[idx2], err = variables.SubstituteVars(log.Log, ctx, pattern); err != nil {
|
2020-05-06 19:46:32 +05:30
|
|
|
return fmt.Errorf("invalid variable used at spec/rules[%d]/validate/anyPattern[%d]", idx, idx2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err = variables.SubstituteVars(log.Log, ctx, rule.Validation.Message); err != nil {
|
|
|
|
return fmt.Errorf("invalid variable used at spec/rules[%d]/validate/message", idx)
|
|
|
|
}
|
|
|
|
if rule.Validation.Deny != nil {
|
|
|
|
for i := range rule.Validation.Deny.Conditions {
|
|
|
|
if _, err = variables.SubstituteVars(log.Log, ctx, rule.Validation.Deny.Conditions[i].Key); err != nil {
|
|
|
|
return fmt.Errorf("invalid variable used at spec/rules[%d]/validate/deny/conditions[%d]/key", idx, i)
|
|
|
|
}
|
|
|
|
if _, err = variables.SubstituteVars(log.Log, ctx, rule.Validation.Deny.Conditions[i].Value); err != nil {
|
|
|
|
return fmt.Errorf("invalid variable used at spec/rules[%d]/validate/deny/conditions[%d]/value", idx, i)
|
|
|
|
}
|
2019-12-30 17:08:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-14 11:59:28 -08:00
|
|
|
func userInfoDefined(ui kyverno.UserInfo) string {
|
2019-12-30 17:08:50 -08:00
|
|
|
if len(ui.Roles) > 0 {
|
2020-02-14 11:59:28 -08:00
|
|
|
return "roles"
|
2019-12-30 17:08:50 -08:00
|
|
|
}
|
|
|
|
if len(ui.ClusterRoles) > 0 {
|
2020-02-14 11:59:28 -08:00
|
|
|
return "clusterRoles"
|
2019-12-30 17:08:50 -08:00
|
|
|
}
|
|
|
|
if len(ui.Subjects) > 0 {
|
2020-02-14 11:59:28 -08:00
|
|
|
return "subjects"
|
2019-12-30 17:08:50 -08:00
|
|
|
}
|
2020-02-14 11:59:28 -08:00
|
|
|
return ""
|
2019-12-30 17:08:50 -08:00
|
|
|
}
|