2019-12-30 17:08:50 -08:00
|
|
|
package policy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-10-20 14:17:33 +02:00
|
|
|
"regexp"
|
2019-12-30 17:08:50 -08:00
|
|
|
|
2022-05-17 13:12:43 +02:00
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2022-03-28 16:01:27 +02:00
|
|
|
"github.com/kyverno/kyverno/pkg/autogen"
|
2019-12-30 17:08:50 -08:00
|
|
|
)
|
|
|
|
|
2022-10-20 14:17:33 +02:00
|
|
|
var forbidden = []*regexp.Regexp{
|
|
|
|
regexp.MustCompile(`[^\.](serviceAccountName)\b`),
|
|
|
|
regexp.MustCompile(`[^\.](serviceAccountNamespace)\b`),
|
|
|
|
regexp.MustCompile(`[^\.](request.userInfo)\b`),
|
|
|
|
regexp.MustCompile(`[^\.](request.roles)\b`),
|
|
|
|
regexp.MustCompile(`[^\.](request.clusterRoles)\b`),
|
|
|
|
}
|
|
|
|
|
2023-04-28 21:54:17 +08:00
|
|
|
// containsUserVariables returns error if variable that does not start from request.object
|
2022-05-17 13:12:43 +02:00
|
|
|
func containsUserVariables(policy kyvernov1.PolicyInterface, vars [][]string) error {
|
2022-10-20 14:17:33 +02:00
|
|
|
rules := autogen.ComputeRules(policy)
|
|
|
|
for idx := range rules {
|
|
|
|
if err := hasUserMatchExclude(idx, &rules[idx]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-04-25 20:20:40 +08:00
|
|
|
for _, rule := range policy.GetSpec().Rules {
|
|
|
|
if rule.IsMutateExisting() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2021-11-03 11:16:55 -07:00
|
|
|
for _, s := range vars {
|
2022-10-20 14:17:33 +02:00
|
|
|
for _, banned := range forbidden {
|
2022-12-12 07:20:20 -08:00
|
|
|
if banned.Match([]byte(s[2])) {
|
|
|
|
return fmt.Errorf("variable %s is not allowed", s[2])
|
2022-10-20 14:17:33 +02:00
|
|
|
}
|
2021-04-06 20:56:06 +03:00
|
|
|
}
|
2019-12-30 17:08:50 -08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-17 13:12:43 +02:00
|
|
|
func hasUserMatchExclude(idx int, rule *kyvernov1.Rule) error {
|
2021-11-03 11:16:55 -07:00
|
|
|
if path := userInfoDefined(rule.MatchResources.UserInfo); path != "" {
|
|
|
|
return fmt.Errorf("invalid variable used at path: spec/rules[%d]/match/%s", idx, path)
|
2021-03-02 10:01:06 +05:30
|
|
|
}
|
|
|
|
|
2021-11-03 11:16:55 -07:00
|
|
|
if path := userInfoDefined(rule.ExcludeResources.UserInfo); path != "" {
|
|
|
|
return fmt.Errorf("invalid variable used at path: spec/rules[%d]/exclude/%s", idx, path)
|
2021-03-02 10:01:06 +05:30
|
|
|
}
|
|
|
|
|
2021-11-03 11:16:55 -07:00
|
|
|
if len(rule.MatchResources.Any) > 0 {
|
|
|
|
for i, value := range rule.MatchResources.Any {
|
|
|
|
if path := userInfoDefined(value.UserInfo); path != "" {
|
|
|
|
return fmt.Errorf("invalid variable used at path: spec/rules[%d]/match/any[%d]/%s", idx, i, path)
|
|
|
|
}
|
|
|
|
}
|
2021-03-02 10:01:06 +05:30
|
|
|
}
|
2021-03-11 22:06:04 +02:00
|
|
|
|
2021-11-03 11:16:55 -07:00
|
|
|
if len(rule.MatchResources.All) > 0 {
|
|
|
|
for i, value := range rule.MatchResources.All {
|
|
|
|
if path := userInfoDefined(value.UserInfo); path != "" {
|
|
|
|
return fmt.Errorf("invalid variable used at path: spec/rules[%d]/match/all[%d]/%s", idx, i, path)
|
|
|
|
}
|
|
|
|
}
|
2021-03-02 10:01:06 +05:30
|
|
|
}
|
|
|
|
|
2021-11-03 11:16:55 -07:00
|
|
|
if len(rule.ExcludeResources.All) > 0 {
|
|
|
|
for i, value := range rule.ExcludeResources.All {
|
|
|
|
if path := userInfoDefined(value.UserInfo); path != "" {
|
2023-06-28 19:13:10 +09:00
|
|
|
return fmt.Errorf("invalid variable used at path: spec/rules[%d]/exclude/all[%d]/%s", idx, i, path)
|
2021-11-03 11:16:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-02 10:01:06 +05:30
|
|
|
|
2021-11-03 11:16:55 -07:00
|
|
|
if len(rule.ExcludeResources.Any) > 0 {
|
|
|
|
for i, value := range rule.ExcludeResources.Any {
|
|
|
|
if path := userInfoDefined(value.UserInfo); path != "" {
|
2023-06-28 19:13:10 +09:00
|
|
|
return fmt.Errorf("invalid variable used at path: spec/rules[%d]/exclude/any[%d]/%s", idx, i, path)
|
2021-11-03 11:16:55 -07:00
|
|
|
}
|
2020-05-06 22:27:06 +05:30
|
|
|
}
|
|
|
|
}
|
2020-05-13 10:06:21 +05:30
|
|
|
|
2021-11-03 11:16:55 -07:00
|
|
|
return nil
|
2020-05-06 22:27:06 +05:30
|
|
|
}
|
|
|
|
|
2022-05-17 13:12:43 +02:00
|
|
|
func userInfoDefined(ui kyvernov1.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
|
|
|
}
|