mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-09 17:37:12 +00:00
* chore: refactor Signed-off-by: ShutingZhao <shuting@nirmata.com> * feat: add foreach for generate.daya to api Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: refactor generator Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: linter Signed-off-by: ShutingZhao <shuting@nirmata.com> * feat: update rule validation Signed-off-by: ShutingZhao <shuting@nirmata.com> * feat: update rule validation -2 Signed-off-by: ShutingZhao <shuting@nirmata.com> * feat: support foreach.data Signed-off-by: ShutingZhao <shuting@nirmata.com> * fix: policy validation Signed-off-by: ShutingZhao <shuting@nirmata.com> * fix: context variables Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: add a chainsaw test Signed-off-by: ShutingZhao <shuting@nirmata.com> * fix: sync on policy deletion Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: enable new chainsaw tests in CI Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: update code-gen Signed-off-by: ShutingZhao <shuting@nirmata.com> * fix: validate targets scope for ns-policies Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: add missing files Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: remove unreasonable test Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: update docs Signed-off-by: ShutingZhao <shuting@nirmata.com> * chore: update install.yaml Signed-off-by: ShutingZhao <shuting@nirmata.com> --------- Signed-off-by: ShutingZhao <shuting@nirmata.com> Co-authored-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
140 lines
4.7 KiB
Go
140 lines
4.7 KiB
Go
package generate
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/go-logr/logr"
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
|
"github.com/kyverno/kyverno/ext/wildcard"
|
|
"github.com/kyverno/kyverno/pkg/clients/dclient"
|
|
"github.com/kyverno/kyverno/pkg/engine/variables/regex"
|
|
"github.com/kyverno/kyverno/pkg/policy/auth"
|
|
"github.com/kyverno/kyverno/pkg/policy/common"
|
|
kubeutils "github.com/kyverno/kyverno/pkg/utils/kube"
|
|
)
|
|
|
|
// Generate provides implementation to validate 'generate' rule
|
|
type Generate struct {
|
|
user string
|
|
// rule to hold 'generate' rule specifications
|
|
rule kyvernov1.Generation
|
|
// authCheck to check access for operations
|
|
authCheck auth.Operations
|
|
// logger
|
|
log logr.Logger
|
|
}
|
|
|
|
// NewGenerateFactory returns a new instance of Generate validation checker
|
|
func NewGenerateFactory(client dclient.Interface, rule kyvernov1.Generation, user string, log logr.Logger) *Generate {
|
|
g := Generate{
|
|
user: user,
|
|
rule: rule,
|
|
authCheck: auth.NewAuth(client, user, log),
|
|
log: log,
|
|
}
|
|
|
|
return &g
|
|
}
|
|
|
|
// Validate validates the 'generate' rule
|
|
func (g *Generate) Validate(ctx context.Context) (string, error) {
|
|
rule := g.rule
|
|
if rule.CloneList.Selector != nil {
|
|
if wildcard.ContainsWildcard(rule.CloneList.Selector.String()) {
|
|
return "selector", fmt.Errorf("wildcard characters `*/?` not supported")
|
|
}
|
|
}
|
|
|
|
if target := rule.GetData(); target != nil {
|
|
// TODO: is this required ?? as anchors can only be on pattern and not resource
|
|
// we can add this check by not sure if its needed here
|
|
if path, err := common.ValidatePattern(target, "/", nil); err != nil {
|
|
return fmt.Sprintf("data.%s", path), fmt.Errorf("anchors not supported on generate resources: %v", err)
|
|
}
|
|
}
|
|
|
|
// Kyverno generate-controller create/update/deletes the resources specified in generate rule of policy
|
|
// kyverno uses SA 'kyverno' and has default ClusterRoles and ClusterRoleBindings
|
|
// instructions to modify the RBAC for kyverno are mentioned at https://github.com/kyverno/kyverno/blob/master/documentation/installation.md
|
|
// - operations required: create/update/delete/get
|
|
// If kind and namespace contain variables, then we cannot resolve then so we skip the processing
|
|
if rule.ForEachGeneration != nil {
|
|
for _, forEach := range rule.ForEachGeneration {
|
|
if err := g.canIGeneratePatterns(ctx, forEach.GeneratePatterns); err != nil {
|
|
return "foreach", err
|
|
}
|
|
}
|
|
} else {
|
|
if err := g.canIGeneratePatterns(ctx, rule.GeneratePatterns); err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
return "", nil
|
|
}
|
|
|
|
func (g *Generate) canIGeneratePatterns(ctx context.Context, generate kyvernov1.GeneratePatterns) error {
|
|
if len(generate.CloneList.Kinds) != 0 {
|
|
for _, kind := range generate.CloneList.Kinds {
|
|
gvk, sub := parseCloneKind(kind)
|
|
return g.canIGenerate(ctx, gvk, generate.Namespace, sub)
|
|
}
|
|
} else {
|
|
k, sub := kubeutils.SplitSubresource(generate.Kind)
|
|
return g.canIGenerate(ctx, strings.Join([]string{generate.APIVersion, k}, "/"), generate.Namespace, sub)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// canIGenerate returns a error if kyverno cannot perform operations
|
|
func (g *Generate) canIGenerate(ctx context.Context, gvk, namespace, subresource string) error {
|
|
// Skip if there is variable defined
|
|
authCheck := g.authCheck
|
|
if !regex.IsVariable(gvk) {
|
|
ok, err := authCheck.CanICreate(ctx, gvk, namespace, "", subresource)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !ok {
|
|
return fmt.Errorf("%s does not have permissions to 'create' resource %s/%s/%s. Grant proper permissions to the background controller", g.user, gvk, subresource, namespace)
|
|
}
|
|
|
|
ok, err = authCheck.CanIUpdate(ctx, gvk, namespace, "", subresource)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !ok {
|
|
return fmt.Errorf("%s does not have permissions to 'update' resource %s/%s/%s. Grant proper permissions to the background controller", g.user, gvk, subresource, namespace)
|
|
}
|
|
|
|
ok, err = authCheck.CanIGet(ctx, gvk, namespace, "", subresource)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !ok {
|
|
return fmt.Errorf("%s does not have permissions to 'get' resource %s/%s/%s. Grant proper permissions to the background controller", g.user, gvk, subresource, namespace)
|
|
}
|
|
|
|
ok, err = authCheck.CanIDelete(ctx, gvk, namespace, "", subresource)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !ok {
|
|
return fmt.Errorf("%s does not have permissions to 'delete' resource %s/%s/%s. Grant proper permissions to the background controller", g.user, gvk, subresource, namespace)
|
|
}
|
|
} else {
|
|
g.log.V(2).Info("resource Kind uses variables, so cannot be resolved. Skipping Auth Checks.")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func parseCloneKind(gvks string) (gvk, sub string) {
|
|
gv, ks := kubeutils.GetKindFromGVK(gvks)
|
|
k, sub := kubeutils.SplitSubresource(ks)
|
|
if !strings.Contains(gv, "*") {
|
|
k = strings.Join([]string{gv, k}, "/")
|
|
}
|
|
return k, sub
|
|
}
|