Skip to content

Commit

Permalink
Additional unit tests for EventPolicy (#8405)
Browse files Browse the repository at this point in the history
* Setting a different default authorization mode
* Using glob style expressions to select source Subject
  • Loading branch information
mgencur authored Jan 16, 2025
1 parent f3ea6b5 commit 6d71005
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
46 changes: 46 additions & 0 deletions pkg/reconciler/broker/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,52 @@ func TestReconcile(t *testing.T) {
feature.AuthorizationDefaultMode: feature.AuthorizationAllowSameNamespace,
}),
},
{
Name: "Should respect setting default authorization mode",
Key: testKey,
Objects: []runtime.Object{
makeDLSServiceAsUnstructured(),
NewBroker(brokerName, testNS,
WithBrokerClass(eventing.MTChannelBrokerClassValue),
WithBrokerConfig(config()),
WithDeadLeaderSink(sinkSVCDest),
WithInitBrokerConditions),
createChannel(withChannelReady, withChannelDeadLetterSink(sinkSVCDest)),
imcConfigMap(),
NewEndpoints(filterServiceName, systemNS,
WithEndpointsLabels(FilterLabels()),
WithEndpointsAddresses(corev1.EndpointAddress{IP: "127.0.0.1"})),
NewEndpoints(ingressServiceName, systemNS,
WithEndpointsLabels(IngressLabels()),
WithEndpointsAddresses(corev1.EndpointAddress{IP: "127.0.0.1"})),
},
WantErr: false,
WantCreates: []runtime.Object{
makeEventPolicy(),
},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{{
Object: NewBroker(brokerName, testNS,
WithBrokerClass(eventing.MTChannelBrokerClassValue),
WithBrokerConfig(config()),
WithBrokerReadyWithDLS,
WithDeadLeaderSink(sinkSVCDest),
WithBrokerAddress(&duckv1.Addressable{
URL: brokerAddress,
Audience: &brokerAudience,
}),
WithBrokerStatusDLS(dls),
WithChannelAddressAnnotation(triggerChannelURL),
WithChannelAPIVersionAnnotation(triggerChannelAPIVersion),
WithChannelKindAnnotation(triggerChannelKind),
WithChannelNameAnnotation(triggerChannelName),
WithBrokerEventPoliciesReadyAndDefaultAuthorizationMode(string(feature.AuthorizationDenyAll)),
),
}},
Ctx: feature.ToContext(context.Background(), feature.Flags{
feature.OIDCAuthentication: feature.Enabled,
feature.AuthorizationDefaultMode: feature.AuthorizationDenyAll,
}),
},
{
Name: "Should list applying EventPolicies",
Key: testKey,
Expand Down
29 changes: 29 additions & 0 deletions pkg/reconciler/eventpolicy/eventpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,35 @@ func TestReconcile(t *testing.T) {
},
WantErr: false,
},
{
Name: "Multiple subjects found using glob style expression, status set to Ready",
Ctx: feature.ToContext(context.TODO(), feature.Flags{
feature.OIDCAuthentication: feature.Enabled,
}),
Key: testNS + "/" + eventPolicyName,
Objects: []runtime.Object{
apiServerSourceWithServiceAccount,
pingSourceWithServiceAccount,
NewEventPolicy(eventPolicyName, testNS,
WithInitEventPolicyConditions,
WithEventPolicyFromSub(fmt.Sprintf("system:serviceaccount:%s*", testNS)),
),
},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{
{
Object: NewEventPolicy(eventPolicyName, testNS,
WithEventPolicyFromSub(fmt.Sprintf("system:serviceaccount:%s*", testNS)),
WithEventPolicyStatusFromSub([]string{
fmt.Sprintf("system:serviceaccount:%s*", testNS),
}),
WithEventPolicyAuthenticationEnabledCondition,
WithReadyEventPolicyCondition,
WithEventPolicySubjectsResolvedSucceeded,
),
},
},
WantErr: false,
},

// test cases for authentication-oidc feature disabled afterwards
{
Expand Down
6 changes: 6 additions & 0 deletions pkg/reconciler/testing/v1/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,9 @@ func WithBrokerEventPoliciesReadyBecauseNoPolicyAndOIDCEnabled() BrokerOption {
b.Status.MarkEventPoliciesTrueWithReason("DefaultAuthorizationMode", "Default authz mode is %q", feature.AuthorizationAllowSameNamespace)
}
}

func WithBrokerEventPoliciesReadyAndDefaultAuthorizationMode(authMode string) BrokerOption {
return func(b *v1.Broker) {
b.Status.MarkEventPoliciesTrueWithReason("DefaultAuthorizationMode", "Default authz mode is %q", authMode)
}
}

0 comments on commit 6d71005

Please sign in to comment.