2019-12-30 17:08:50 -08:00
|
|
|
package policy
|
|
|
|
|
|
|
|
import (
|
2021-03-11 22:06:04 +02:00
|
|
|
"encoding/json"
|
2019-12-30 17:08:50 -08:00
|
|
|
"fmt"
|
2021-11-03 11:16:55 -07:00
|
|
|
"strings"
|
2019-12-30 17:08:50 -08:00
|
|
|
|
2021-10-29 18:13:20 +02:00
|
|
|
kyverno "github.com/kyverno/kyverno/api/kyverno/v1"
|
2020-10-07 11:12:31 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/context"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/variables"
|
2021-03-02 10:01:06 +05:30
|
|
|
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
|
2020-03-17 16:25:34 -07:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
2019-12-30 17:08:50 -08:00
|
|
|
)
|
|
|
|
|
2021-11-03 11:16:55 -07:00
|
|
|
//ContainsUserVariables returns error if variable that does not start from request.object
|
|
|
|
func containsUserVariables(policy *kyverno.ClusterPolicy, vars [][]string) error {
|
|
|
|
for _, s := range vars {
|
|
|
|
if strings.Contains(s[0], "userInfo") {
|
|
|
|
return fmt.Errorf("variable %s is not allowed", s[0])
|
2019-12-30 17:08:50 -08:00
|
|
|
}
|
2021-11-03 11:16:55 -07:00
|
|
|
}
|
2019-12-30 17:08:50 -08:00
|
|
|
|
2021-11-03 11:16:55 -07:00
|
|
|
for idx := range policy.Spec.Rules {
|
|
|
|
if err := hasUserMatchExclude(idx, &policy.Spec.Rules[idx]); err != nil {
|
2021-04-06 20:56:06 +03:00
|
|
|
return err
|
|
|
|
}
|
2019-12-30 17:08:50 -08:00
|
|
|
}
|
2020-11-13 16:25:51 -08:00
|
|
|
|
2019-12-30 17:08:50 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-03 11:16:55 -07:00
|
|
|
func hasUserMatchExclude(idx int, rule *kyverno.Rule) error {
|
|
|
|
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 != "" {
|
|
|
|
return fmt.Errorf("invalid variable used at path: spec/rules[%d]/exclude/any[%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.Any) > 0 {
|
|
|
|
for i, value := range rule.ExcludeResources.Any {
|
|
|
|
if path := userInfoDefined(value.UserInfo); path != "" {
|
|
|
|
return fmt.Errorf("invalid variable used at path: spec/rules[%d]/exclude/all[%d]/%s", idx, i, path)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2021-03-11 22:06:04 +02:00
|
|
|
|
|
|
|
func substituteVarsInJSON(ctx context.EvalInterface, document apiextensions.JSON) (apiextensions.JSON, error) {
|
|
|
|
jsonByte, err := json.Marshal(document)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsonInterface interface{}
|
|
|
|
err = json.Unmarshal(jsonByte, &jsonInterface)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonInterface, err = variables.SubstituteAll(log.Log, ctx, jsonInterface)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonByte, err = json.Marshal(jsonInterface)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(jsonByte, &document)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return document, nil
|
|
|
|
}
|