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-07-02 08:56:50 +03:00
|
|
|
gojmespath "github.com/jmespath/go-jmespath"
|
2020-10-07 11:12:31 -07:00
|
|
|
kyverno "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/context"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/variables"
|
2021-03-02 10:01:06 +05:30
|
|
|
"github.com/kyverno/kyverno/pkg/utils"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-04-28 14:21:11 -07:00
|
|
|
filterVars := []string{"request.object", "request.namespace", "images"}
|
2020-02-14 11:59:28 -08:00
|
|
|
ctx := context.NewContext(filterVars...)
|
2021-02-22 12:08:26 -08:00
|
|
|
|
2021-03-11 22:06:04 +02:00
|
|
|
for _, contextEntry := range rule.Context {
|
2021-02-22 12:08:26 -08:00
|
|
|
if contextEntry.APICall != nil {
|
|
|
|
ctx.AddBuiltInVars(contextEntry.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if contextEntry.ConfigMap != nil {
|
|
|
|
ctx.AddBuiltInVars(contextEntry.Name)
|
|
|
|
}
|
|
|
|
}
|
2021-04-06 20:56:06 +03:00
|
|
|
err = validateBackgroundModeVars(ctx, rule)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rule, err = variables.SubstituteAllInRule(log.Log, ctx, rule); !checkNotFoundErr(err) {
|
|
|
|
return fmt.Errorf("variable substitution failed for rule %s: %s", rule.Name, err.Error())
|
|
|
|
}
|
2021-02-22 12:08:26 -08:00
|
|
|
|
2021-03-02 10:01:06 +05:30
|
|
|
if rule.AnyAllConditions != nil {
|
2021-04-06 20:56:06 +03:00
|
|
|
if err = validatePreConditions(idx, ctx, rule.AnyAllConditions); !checkNotFoundErr(err) {
|
2021-03-02 10:01:06 +05:30
|
|
|
return err
|
2020-01-07 15:13:57 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 19:46:32 +05:30
|
|
|
if rule.Validation.Deny != nil {
|
2021-04-06 20:56:06 +03:00
|
|
|
if err = validateDenyConditions(idx, ctx, rule.Validation.Deny.AnyAllConditions); !checkNotFoundErr(err) {
|
2021-03-02 10:01:06 +05:30
|
|
|
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-03-02 10:01:06 +05:30
|
|
|
func validatePreConditions(idx int, ctx context.EvalInterface, anyAllConditions apiextensions.JSON) error {
|
|
|
|
var err error
|
2021-03-11 22:06:04 +02:00
|
|
|
|
|
|
|
anyAllConditions, err = substituteVarsInJSON(ctx, anyAllConditions)
|
2021-03-02 10:01:06 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-11 22:06:04 +02:00
|
|
|
_, err = utils.ApiextensionsJsonToKyvernoConditions(anyAllConditions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-03-02 10:01:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateDenyConditions(idx int, ctx context.EvalInterface, denyConditions apiextensions.JSON) error {
|
2021-03-11 22:06:04 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
denyConditions, err = substituteVarsInJSON(ctx, denyConditions)
|
2021-03-02 10:01:06 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-11 22:06:04 +02:00
|
|
|
|
|
|
|
_, err = utils.ApiextensionsJsonToKyvernoConditions(denyConditions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-03-02 10:01:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-06 22:27:06 +05:30
|
|
|
func checkNotFoundErr(err error) bool {
|
|
|
|
if err != nil {
|
2020-05-13 10:06:21 +05:30
|
|
|
switch err.(type) {
|
2021-07-02 08:56:50 +03:00
|
|
|
case gojmespath.NotFoundError:
|
2020-05-06 22:27:06 +05:30
|
|
|
return true
|
2021-02-22 12:08:26 -08:00
|
|
|
case context.InvalidVariableErr:
|
|
|
|
// non-white-listed variable is found
|
|
|
|
return false
|
2020-05-13 10:06:21 +05:30
|
|
|
default:
|
2020-05-06 22:27:06 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2020-05-13 10:06:21 +05:30
|
|
|
|
2020-05-06 22:27:06 +05:30
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-04-06 20:56:06 +03:00
|
|
|
func validateBackgroundModeVars(ctx context.EvalInterface, document apiextensions.JSON) error {
|
|
|
|
jsonByte, err := json.Marshal(document)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsonInterface interface{}
|
|
|
|
err = json.Unmarshal(jsonByte, &jsonInterface)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = variables.ValidateBackgroundModeVars(log.Log, ctx, jsonInterface)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|