Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use failurePolicy to block or allow requests, on policy errors #4183

Merged
merged 28 commits into from Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c5af278
use failurePolicy to block or allow requests, on policy errors
JimBugwadia Jul 2, 2022
7c26edf
add warnings
JimBugwadia Jul 3, 2022
6bafb40
codegen
JimBugwadia Jul 3, 2022
92c0bfb
fix linter issues
JimBugwadia Jul 4, 2022
49c5af2
merge main
JimBugwadia Jul 4, 2022
45afca7
add unit tests
JimBugwadia Jul 5, 2022
4e496fa
handle network errors
JimBugwadia Jul 6, 2022
cf73075
fix linter issues
JimBugwadia Jul 6, 2022
d2839b3
Merge branch 'main' into apply_failure_policy
JimBugwadia Jul 6, 2022
099dfc4
fix test
JimBugwadia Jul 6, 2022
9c160c0
fix title conversion
JimBugwadia Jul 6, 2022
8161f27
fix path in generated file
JimBugwadia Jul 6, 2022
64e5eef
fix test
JimBugwadia Jul 6, 2022
ce5dd8b
Merge branch 'main' into apply_failure_policy
JimBugwadia Jul 6, 2022
85a744f
Merge branch 'main' into apply_failure_policy
JimBugwadia Jul 27, 2022
9e2de24
merge and update to new metrics
JimBugwadia Jul 28, 2022
db9ea71
fix fake metrics
JimBugwadia Jul 29, 2022
fafa4c3
fix merge errors; update Cosign
JimBugwadia Jul 29, 2022
41f14f6
fix tests
JimBugwadia Jul 29, 2022
ee3e8c6
add check for klog flag initialization
JimBugwadia Jul 31, 2022
8d909c8
Merge branch 'main' into apply_failure_policy
JimBugwadia Aug 1, 2022
3d7935a
check for flag reinitialization
JimBugwadia Aug 1, 2022
073c040
check for flag reinitialization
JimBugwadia Aug 1, 2022
3c9c6dd
fix spelling
JimBugwadia Aug 1, 2022
4a3efbd
fix flag init
JimBugwadia Aug 1, 2022
d14ae55
Merge branch 'main' into apply_failure_policy
JimBugwadia Aug 2, 2022
2008578
fix klog import ver
JimBugwadia Aug 2, 2022
e52befb
Merge branch 'apply_failure_policy' of https://github.com/JimBugwadia…
JimBugwadia Aug 2, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 18 additions & 15 deletions api/kyverno/v1/image_verification_types.go
Expand Up @@ -344,25 +344,28 @@ func (iv *ImageVerification) Convert() *ImageVerification {
copy.ImageReferences = append(copy.ImageReferences, iv.Image)
}

attestor := Attestor{
Annotations: iv.Annotations,
}

if iv.Key != "" {
attestor.Keys = &StaticKeyAttestor{
PublicKeys: iv.Key,
attestorSet := AttestorSet{}
if len(iv.Annotations) > 0 || iv.Key != "" || iv.Issuer != "" {
attestor := Attestor{
Annotations: iv.Annotations,
}
} else if iv.Issuer != "" {
attestor.Keyless = &KeylessAttestor{
Issuer: iv.Issuer,
Subject: iv.Subject,
Roots: iv.Roots,

if iv.Key != "" {
attestor.Keys = &StaticKeyAttestor{
PublicKeys: iv.Key,
}
} else if iv.Issuer != "" {
attestor.Keyless = &KeylessAttestor{
Issuer: iv.Issuer,
Subject: iv.Subject,
Roots: iv.Roots,
}
}

attestorSet.Entries = append(attestorSet.Entries, attestor)
copy.Attestors = append(copy.Attestors, attestorSet)
}

attestorSet := AttestorSet{}
attestorSet.Entries = append(attestorSet.Entries, attestor)
copy.Attestors = append(copy.Attestors, attestorSet)
copy.Attestations = iv.Attestations
return copy
}
7 changes: 4 additions & 3 deletions api/kyverno/v1/spec_types.go
Expand Up @@ -37,15 +37,16 @@ type Spec struct {
// +optional
ApplyRules *ApplyRulesType `json:"applyRules,omitempty" yaml:"applyRules,omitempty"`

// FailurePolicy defines how unrecognized errors from the admission endpoint are handled.
// FailurePolicy defines how unexpected policy errors and webhook response timeout errors are handled.
// Rules within the same policy share the same failure behavior.
// Allowed values are Ignore or Fail. Defaults to Fail.
// +optional
FailurePolicy *FailurePolicyType `json:"failurePolicy,omitempty" yaml:"failurePolicy,omitempty"`

// ValidationFailureAction controls if a validation policy rule failure should disallow
// ValidationFailureAction defines if a validation policy rule violation should block
// the admission review request (enforce), or allow (audit) the admission review request
// and report an error in a policy report. Optional. The default value is "audit".
// and report an error in a policy report. Optional.
// Allowed values are audit or enforce. The default value is "audit".
// +optional
// +kubebuilder:validation:Enum=audit;enforce
ValidationFailureAction ValidationFailureAction `json:"validationFailureAction,omitempty" yaml:"validationFailureAction,omitempty"`
Expand Down
8 changes: 4 additions & 4 deletions charts/kyverno/templates/crds.yaml
Expand Up @@ -65,7 +65,7 @@ spec:
description: Background controls if rules are applied to existing resources during a background scan. Optional. Default value is "true". The value must be set to "false" if the policy rule uses variables that are only available in the admission review request (e.g. user name).
type: boolean
failurePolicy:
description: FailurePolicy defines how unrecognized errors from the admission endpoint are handled. Rules within the same policy share the same failure behavior. Allowed values are Ignore or Fail. Defaults to Fail.
description: FailurePolicy defines how unexpected policy errors and webhook response timeout errors are handled. Rules within the same policy share the same failure behavior. Allowed values are Ignore or Fail. Defaults to Fail.
enum:
- Ignore
- Fail
Expand Down Expand Up @@ -1549,7 +1549,7 @@ spec:
description: SchemaValidation skips policy validation checks. Optional. The default value is set to "true", it must be set to "false" to disable the validation checks.
type: boolean
validationFailureAction:
description: ValidationFailureAction controls if a validation policy rule failure should disallow the admission review request (enforce), or allow (audit) the admission review request and report an error in a policy report. Optional. The default value is "audit".
description: ValidationFailureAction defines if a validation policy rule violation should block the admission review request (enforce), or allow (audit) the admission review request and report an error in a policy report. Optional. Allowed values are audit or enforce. The default value is "audit".
enum:
- audit
- enforce
Expand Down Expand Up @@ -3890,7 +3890,7 @@ spec:
description: Background controls if rules are applied to existing resources during a background scan. Optional. Default value is "true". The value must be set to "false" if the policy rule uses variables that are only available in the admission review request (e.g. user name).
type: boolean
failurePolicy:
description: FailurePolicy defines how unrecognized errors from the admission endpoint are handled. Rules within the same policy share the same failure behavior. Allowed values are Ignore or Fail. Defaults to Fail.
description: FailurePolicy defines how unexpected policy errors and webhook response timeout errors are handled. Rules within the same policy share the same failure behavior. Allowed values are Ignore or Fail. Defaults to Fail.
enum:
- Ignore
- Fail
Expand Down Expand Up @@ -5374,7 +5374,7 @@ spec:
description: SchemaValidation skips policy validation checks. Optional. The default value is set to "true", it must be set to "false" to disable the validation checks.
type: boolean
validationFailureAction:
description: ValidationFailureAction controls if a validation policy rule failure should disallow the admission review request (enforce), or allow (audit) the admission review request and report an error in a policy report. Optional. The default value is "audit".
description: ValidationFailureAction defines if a validation policy rule violation should block the admission review request (enforce), or allow (audit) the admission review request and report an error in a policy report. Optional. Allowed values are audit or enforce. The default value is "audit".
enum:
- audit
- enforce
Expand Down
7 changes: 6 additions & 1 deletion cmd/cli/kubectl-kyverno/main.go
Expand Up @@ -38,10 +38,15 @@ func main() {
}

func configurelog(cli *cobra.Command) {
// clear flags initialized in static dependencies
if flag.CommandLine.Lookup("log_dir") != nil {
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
}

klog.InitFlags(nil)
cli.PersistentFlags().AddGoFlagSet(flag.CommandLine)
log.SetLogger(klogr.New())

cli.PersistentFlags().AddGoFlagSet(flag.CommandLine)
_ = cli.PersistentFlags().MarkHidden("alsologtostderr")
_ = cli.PersistentFlags().MarkHidden("logtostderr")
_ = cli.PersistentFlags().MarkHidden("log_dir")
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/kubectl-kyverno/utils/common/common.go
Expand Up @@ -1035,7 +1035,7 @@ func GetResourceFromPath(fs billy.Filesystem, path string, isGit bool, policyRes

// initializeMockController initializes a basic Generate Controller with a fake dynamic client.
func initializeMockController(objects []runtime.Object) (*generate.GenerateController, error) {
client, err := dclient.NewMockClient(runtime.NewScheme(), nil, objects...)
client, err := dclient.NewFakeClient(runtime.NewScheme(), nil, objects...)
if err != nil {
fmt.Printf("Failed to mock dynamic client")
return nil, err
Expand Down
14 changes: 9 additions & 5 deletions cmd/initContainer/main.go
Expand Up @@ -15,12 +15,12 @@ import (
kyvernoclient "github.com/kyverno/kyverno/pkg/client/clientset/versioned"
"github.com/kyverno/kyverno/pkg/config"
"github.com/kyverno/kyverno/pkg/dclient"
engineUtils "github.com/kyverno/kyverno/pkg/engine/utils"
"github.com/kyverno/kyverno/pkg/leaderelection"
"github.com/kyverno/kyverno/pkg/policyreport"
"github.com/kyverno/kyverno/pkg/signal"
"github.com/kyverno/kyverno/pkg/tls"
"github.com/kyverno/kyverno/pkg/utils"
"go.uber.org/multierr"
admissionv1 "k8s.io/api/admission/v1"
coordinationv1 "k8s.io/api/coordination/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -58,9 +58,13 @@ const (
)

func main() {
klog.InitFlags(nil)
log.SetLogger(klogr.New().WithCallDepth(1))
// arguments
// clear flags initialized in static dependencies
if flag.CommandLine.Lookup("log_dir") != nil {
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
}

klog.InitFlags(nil) // add the block above before invoking klog.InitFlags()
log.SetLogger(klogr.New())
flag.Float64Var(&clientRateLimitQPS, "clientRateLimitQPS", 0, "Configure the maximum QPS to the Kubernetes API server from Kyverno. Uses the client default if zero.")
flag.IntVar(&clientRateLimitBurst, "clientRateLimitBurst", 0, "Configure the maximum burst for throttle. Uses the client default if zero.")
if err := flag.Set("v", "2"); err != nil {
Expand Down Expand Up @@ -500,6 +504,6 @@ func convertGR(pclient kyvernoclient.Interface) error {
}
}

err = engineUtils.CombineErrors(errors)
err = multierr.Combine(errors...)
return err
}
7 changes: 6 additions & 1 deletion cmd/kyverno/main.go
Expand Up @@ -82,8 +82,13 @@ var (
)

func main() {
// clear flags initialized in static dependencies
if flag.CommandLine.Lookup("log_dir") != nil {
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
}

klog.InitFlags(nil)
log.SetLogger(klogr.New().WithCallDepth(1))
log.SetLogger(klogr.New())
flag.IntVar(&webhookTimeout, "webhookTimeout", int(webhookconfig.DefaultWebhookTimeout), "Timeout for webhook configurations.")
flag.IntVar(&genWorkers, "genWorkers", 10, "Workers for generate controller.")
flag.IntVar(&maxQueuedEvents, "maxQueuedEvents", 1000, "Maximum events to be queued.")
Expand Down
15 changes: 8 additions & 7 deletions config/crds/kyverno.io_clusterpolicies.yaml
Expand Up @@ -70,10 +70,10 @@ spec:
name).
type: boolean
failurePolicy:
description: FailurePolicy defines how unrecognized errors from the
admission endpoint are handled. Rules within the same policy share
the same failure behavior. Allowed values are Ignore or Fail. Defaults
to Fail.
description: FailurePolicy defines how unexpected policy errors and
webhook response timeout errors are handled. Rules within the same
policy share the same failure behavior. Allowed values are Ignore
or Fail. Defaults to Fail.
enum:
- Ignore
- Fail
Expand Down Expand Up @@ -2466,10 +2466,11 @@ spec:
disable the validation checks.
type: boolean
validationFailureAction:
description: ValidationFailureAction controls if a validation policy
rule failure should disallow the admission review request (enforce),
description: ValidationFailureAction defines if a validation policy
rule violation should block the admission review request (enforce),
or allow (audit) the admission review request and report an error
in a policy report. Optional. The default value is "audit".
in a policy report. Optional. Allowed values are audit or enforce.
The default value is "audit".
enum:
- audit
- enforce
Expand Down
15 changes: 8 additions & 7 deletions config/crds/kyverno.io_policies.yaml
Expand Up @@ -71,10 +71,10 @@ spec:
name).
type: boolean
failurePolicy:
description: FailurePolicy defines how unrecognized errors from the
admission endpoint are handled. Rules within the same policy share
the same failure behavior. Allowed values are Ignore or Fail. Defaults
to Fail.
description: FailurePolicy defines how unexpected policy errors and
webhook response timeout errors are handled. Rules within the same
policy share the same failure behavior. Allowed values are Ignore
or Fail. Defaults to Fail.
enum:
- Ignore
- Fail
Expand Down Expand Up @@ -2467,10 +2467,11 @@ spec:
disable the validation checks.
type: boolean
validationFailureAction:
description: ValidationFailureAction controls if a validation policy
rule failure should disallow the admission review request (enforce),
description: ValidationFailureAction defines if a validation policy
rule violation should block the admission review request (enforce),
or allow (audit) the admission review request and report an error
in a policy report. Optional. The default value is "audit".
in a policy report. Optional. Allowed values are audit or enforce.
The default value is "audit".
enum:
- audit
- enforce
Expand Down
30 changes: 16 additions & 14 deletions config/install.yaml
Expand Up @@ -87,10 +87,10 @@ spec:
name).
type: boolean
failurePolicy:
description: FailurePolicy defines how unrecognized errors from the
admission endpoint are handled. Rules within the same policy share
the same failure behavior. Allowed values are Ignore or Fail. Defaults
to Fail.
description: FailurePolicy defines how unexpected policy errors and
webhook response timeout errors are handled. Rules within the same
policy share the same failure behavior. Allowed values are Ignore
or Fail. Defaults to Fail.
enum:
- Ignore
- Fail
Expand Down Expand Up @@ -2483,10 +2483,11 @@ spec:
disable the validation checks.
type: boolean
validationFailureAction:
description: ValidationFailureAction controls if a validation policy
rule failure should disallow the admission review request (enforce),
description: ValidationFailureAction defines if a validation policy
rule violation should block the admission review request (enforce),
or allow (audit) the admission review request and report an error
in a policy report. Optional. The default value is "audit".
in a policy report. Optional. Allowed values are audit or enforce.
The default value is "audit".
enum:
- audit
- enforce
Expand Down Expand Up @@ -6078,10 +6079,10 @@ spec:
name).
type: boolean
failurePolicy:
description: FailurePolicy defines how unrecognized errors from the
admission endpoint are handled. Rules within the same policy share
the same failure behavior. Allowed values are Ignore or Fail. Defaults
to Fail.
description: FailurePolicy defines how unexpected policy errors and
webhook response timeout errors are handled. Rules within the same
policy share the same failure behavior. Allowed values are Ignore
or Fail. Defaults to Fail.
enum:
- Ignore
- Fail
Expand Down Expand Up @@ -8474,10 +8475,11 @@ spec:
disable the validation checks.
type: boolean
validationFailureAction:
description: ValidationFailureAction controls if a validation policy
rule failure should disallow the admission review request (enforce),
description: ValidationFailureAction defines if a validation policy
rule violation should block the admission review request (enforce),
or allow (audit) the admission review request and report an error
in a policy report. Optional. The default value is "audit".
in a policy report. Optional. Allowed values are audit or enforce.
The default value is "audit".
enum:
- audit
- enforce
Expand Down
30 changes: 16 additions & 14 deletions config/install_debug.yaml
Expand Up @@ -85,10 +85,10 @@ spec:
name).
type: boolean
failurePolicy:
description: FailurePolicy defines how unrecognized errors from the
admission endpoint are handled. Rules within the same policy share
the same failure behavior. Allowed values are Ignore or Fail. Defaults
to Fail.
description: FailurePolicy defines how unexpected policy errors and
webhook response timeout errors are handled. Rules within the same
policy share the same failure behavior. Allowed values are Ignore
or Fail. Defaults to Fail.
enum:
- Ignore
- Fail
Expand Down Expand Up @@ -2481,10 +2481,11 @@ spec:
disable the validation checks.
type: boolean
validationFailureAction:
description: ValidationFailureAction controls if a validation policy
rule failure should disallow the admission review request (enforce),
description: ValidationFailureAction defines if a validation policy
rule violation should block the admission review request (enforce),
or allow (audit) the admission review request and report an error
in a policy report. Optional. The default value is "audit".
in a policy report. Optional. Allowed values are audit or enforce.
The default value is "audit".
enum:
- audit
- enforce
Expand Down Expand Up @@ -6072,10 +6073,10 @@ spec:
name).
type: boolean
failurePolicy:
description: FailurePolicy defines how unrecognized errors from the
admission endpoint are handled. Rules within the same policy share
the same failure behavior. Allowed values are Ignore or Fail. Defaults
to Fail.
description: FailurePolicy defines how unexpected policy errors and
webhook response timeout errors are handled. Rules within the same
policy share the same failure behavior. Allowed values are Ignore
or Fail. Defaults to Fail.
enum:
- Ignore
- Fail
Expand Down Expand Up @@ -8468,10 +8469,11 @@ spec:
disable the validation checks.
type: boolean
validationFailureAction:
description: ValidationFailureAction controls if a validation policy
rule failure should disallow the admission review request (enforce),
description: ValidationFailureAction defines if a validation policy
rule violation should block the admission review request (enforce),
or allow (audit) the admission review request and report an error
in a policy report. Optional. The default value is "audit".
in a policy report. Optional. Allowed values are audit or enforce.
The default value is "audit".
enum:
- audit
- enforce
Expand Down