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

Added EventPolicy Webhook #8091

Merged
merged 4 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
kubeclient "knative.dev/pkg/client/injection/kube/client"
configmapinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/configmap/filtered"

eventingv1alpha1 "knative.dev/eventing/pkg/apis/eventing/v1alpha1"
eventingv1beta3 "knative.dev/eventing/pkg/apis/eventing/v1beta3"
"knative.dev/eventing/pkg/apis/feature"
"knative.dev/eventing/pkg/apis/sinks"
Expand Down Expand Up @@ -75,6 +76,8 @@ func init() {

var ourTypes = map[schema.GroupVersionKind]resourcesemantics.GenericCRD{
// For group eventing.knative.dev.
// v1alpha1
eventingv1alpha1.SchemeGroupVersion.WithKind("EventPolicy"): &eventingv1alpha1.EventPolicy{},
// v1beta1
eventingv1beta1.SchemeGroupVersion.WithKind("EventType"): &eventingv1beta1.EventType{},
// v1beta2
Expand Down
8 changes: 8 additions & 0 deletions pkg/apis/eventing/v1alpha1/eventpolicy_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ import (
"context"
"strings"

"knative.dev/eventing/pkg/apis/feature"
"knative.dev/pkg/apis"
)

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) {
dharmjit marked this conversation as resolved.
Show resolved Hide resolved
if !feature.FromContext(ctx).IsOIDCAuthentication() {
return apis.ErrGeneric("oidc-authentication feature not enabled")
}
}
return ep.Spec.Validate(ctx).ViaField("spec")
}

Expand Down
53 changes: 51 additions & 2 deletions pkg/apis/eventing/v1alpha1/eventpolicy_validation_test.go
dharmjit marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,57 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"knative.dev/eventing/pkg/apis/feature"
"knative.dev/pkg/apis"
"knative.dev/pkg/ptr"
)

func TestEventPolicySpecValidation(t *testing.T) {
func TestEventPolicySpecValidationWithOIDCAuthenticationFeatureFlagDisabled(t *testing.T) {
tests := []struct {
name string
ep *EventPolicy
want *apis.FieldError
}{
{
name: "valid, from.sub exactly '*'",
ep: &EventPolicy{
Spec: EventPolicySpec{
From: []EventPolicySpecFrom{{
Sub: ptr.String("*"),
}},
},
},
want: func() *apis.FieldError {
return apis.ErrGeneric("oidc-authentication feature not enabled")
}(),
},
{
name: "invalid, missing from.ref and from.sub",
ep: &EventPolicy{
Spec: EventPolicySpec{
From: []EventPolicySpecFrom{{}},
},
},
want: func() *apis.FieldError {
return apis.ErrGeneric("oidc-authentication feature not enabled")
}(),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.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
ep *EventPolicy
Expand Down Expand Up @@ -252,7 +298,10 @@ func TestEventPolicySpecValidation(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := test.ep.Validate(context.TODO())
ctx := feature.ToContext(context.TODO(), feature.Flags{
feature.OIDCAuthentication: feature.Enabled,
})
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)
}
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{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you are trying to turn on OIDC for all the tests. Should we try to use the similar pattern that is used here in broker?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Leo6Leo I think currently all test cases are written assuming that the oidc-authentication feature is enabled. We could refactor this if we wanted to add tests with oidc-authentication feature disabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Leo6Leo Any inputs on this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think not all the test cases here are being assumed to run with oidc-authentication feature is enabled. For example here

WithChannelEventPoliciesReadyBecauseOIDCDisabled()),

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, following up on what @Leo6Leo said, we generally test many different configurations in these unit tests. In this scenario that means both with OIDC enabled and without OIDC enabled

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the individual test cases in the newer commit. PTAL.

feature.OIDCAuthentication: feature.Enabled,
})
r := &Reconciler{
dynamicClientSet: fakedynamicclient.Get(ctx),
channelLister: listers.GetMessagingChannelLister(),
Expand Down
Loading