Skip to content

Commit

Permalink
Ignore trailing slashes for Broker addresses
Browse files Browse the repository at this point in the history
Signed-off-by: Pierangelo Di Pilato <pierdipi@redhat.com>
  • Loading branch information
pierDipi committed Aug 4, 2022
1 parent 15dd7ca commit 9643971
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
7 changes: 4 additions & 3 deletions pkg/broker/ingress/ingress_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ import (
"go.uber.org/zap"
"k8s.io/apimachinery/pkg/types"

"knative.dev/pkg/network"

"knative.dev/eventing/pkg/apis/eventing"
eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1"
broker "knative.dev/eventing/pkg/broker"
"knative.dev/eventing/pkg/broker"
eventinglisters "knative.dev/eventing/pkg/client/listers/eventing/v1"
"knative.dev/eventing/pkg/kncloudevents"
"knative.dev/eventing/pkg/tracing"
"knative.dev/eventing/pkg/utils"
"knative.dev/pkg/network"
)

const (
Expand Down Expand Up @@ -120,7 +121,7 @@ func (h *Handler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(http.StatusNotFound)
return
}
nsBrokerName := strings.Split(request.RequestURI, "/")
nsBrokerName := strings.Split(strings.TrimSuffix(request.RequestURI, "/"), "/")
if len(nsBrokerName) != 3 {
h.Logger.Info("Malformed uri", zap.String("URI", request.RequestURI))
writer.WriteHeader(http.StatusBadRequest)
Expand Down
21 changes: 19 additions & 2 deletions pkg/broker/ingress/ingress_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

duckv1 "knative.dev/pkg/apis/duck/v1"

"knative.dev/eventing/pkg/apis/eventing"
eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1"
broker "knative.dev/eventing/pkg/broker"
"knative.dev/eventing/pkg/broker"
"knative.dev/eventing/pkg/kncloudevents"
reconcilertestingv1 "knative.dev/eventing/pkg/reconciler/testing/v1"
duckv1 "knative.dev/pkg/apis/duck/v1"
)

const (
Expand Down Expand Up @@ -135,6 +136,22 @@ func TestHandler_ServeHTTP(t *testing.T) {
makeBroker("name", "ns"),
},
},
{
name: "valid - ignore trailing slash (happy path POST)",
method: nethttp.MethodPost,
uri: "/ns/name/",
body: getValidEvent(),
expectedHeaders: nethttp.Header{
"Allow": []string{"PUT, OPTIONS"},
},
statusCode: senderResponseStatusCode,
handler: handler(),
reporter: &mockReporter{StatusCode: senderResponseStatusCode, EventDispatchTimeReported: true},
defaulter: broker.TTLDefaulter(logger, 100),
brokers: []*eventingv1.Broker{
makeBroker("name", "ns"),
},
},
{
name: "invalid event",
method: nethttp.MethodPost,
Expand Down

0 comments on commit 9643971

Please sign in to comment.