Skip to content

Commit

Permalink
eventpolicy webhook validation for oidc feature will be called for cr…
Browse files Browse the repository at this point in the history
…eate or spec updates
  • Loading branch information
dharmjit committed Jul 13, 2024
1 parent 7c4c759 commit 5cb354f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pkg/apis/eventing/v1alpha1/eventpolicy_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ import (
)

func (ep *EventPolicy) Validate(ctx context.Context) *apis.FieldError {
// To not allow creation or spec updates of EventPolicy CRs
// if the oidc-authentication feature is not enabled
if apis.IsInCreate(ctx) || apis.IsInSpec(ctx) {
if !feature.FromContext(ctx).IsOIDCAuthentication() {
return apis.ErrGeneric("oidc-authentication feature not enabled")
}
}
return ep.Spec.Validate(ctx).ViaField("spec")
}

func (ets *EventPolicySpec) Validate(ctx context.Context) *apis.FieldError {
if !feature.FromContext(ctx).IsOIDCAuthentication() {
return apis.ErrGeneric("oidc-authentication feature not enabled")
}
var err *apis.FieldError
for i, f := range ets.From {
if f.Ref == nil && (f.Sub == nil || *f.Sub == "") {
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/eventing/v1alpha1/eventpolicy_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ func TestEventPolicySpecValidationWithOIDCAuthenticationFeatureFlagDisabled(t *t
ctx := feature.ToContext(context.TODO(), feature.Flags{
feature.OIDCAuthentication: feature.Disabled,
})
ctx = apis.WithinCreate(ctx)
got := test.ep.Validate(ctx)
if diff := cmp.Diff(test.want.Error(), got.Error()); diff != "" {
t.Errorf("%s: Validate EventPolicySpec (-want, +got) = %v", test.name, diff)
}
})
}
}

func TestEventPolicySpecValidationWithOIDCAuthenticationFeatureFlagEnabled(t *testing.T) {
tests := []struct {
name string
Expand Down
4 changes: 4 additions & 0 deletions pkg/reconciler/channel/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

eventingv1alpha1 "knative.dev/eventing/pkg/apis/eventing/v1alpha1"
"knative.dev/eventing/pkg/apis/feature"

v1 "knative.dev/eventing/pkg/apis/messaging/v1"

Expand Down Expand Up @@ -576,6 +577,9 @@ func TestReconcile(t *testing.T) {
table.Test(t, MakeFactory(func(ctx context.Context, listers *Listers, cmw configmap.Watcher) controller.Reconciler {
ctx = channelable.WithDuck(ctx)
ctx = v1addr.WithDuck(ctx)
ctx = feature.ToContext(ctx, feature.Flags{
feature.OIDCAuthentication: feature.Enabled,
})
r := &Reconciler{
dynamicClientSet: fakedynamicclient.Get(ctx),
channelLister: listers.GetMessagingChannelLister(),
Expand Down

0 comments on commit 5cb354f

Please sign in to comment.