From c28ba10d80bb3b69d5fd2d65692a09fa7a87f3d6 Mon Sep 17 00:00:00 2001 From: Guilhem Lettron Date: Thu, 9 Nov 2023 00:38:54 +0100 Subject: [PATCH] feat(middleware-logging): implement http.Hijacker http.Hijacker is mandatory to forward websocket connexions Signed-off-by: Guilhem Lettron --- go.mod | 27 ++++++----- interceptor/middleware/responsewriter.go | 11 +++++ interceptor/middleware/responsewriter_test.go | 46 +++++++++++++++++++ 3 files changed, 72 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 7a934175..8689b8f3 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.22.2 require ( github.com/go-logr/logr v1.4.2 github.com/google/go-cmp v0.6.0 + github.com/gorilla/websocket v1.5.1 github.com/hashicorp/go-immutable-radix/v2 v2.1.0 github.com/kedacore/keda/v2 v2.14.1-0.20240429185716-c55e306af94b github.com/kelseyhightower/envconfig v1.4.0 @@ -45,6 +46,20 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/blang/semver/v4 v4.0.0 // indirect github.com/cenkalti/backoff/v4 v4.3.0 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/go-task/slim-sprig/v3 v3.0.0 // indirect + github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect + github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect + go.opentelemetry.io/otel/trace v1.28.0 // indirect + go.opentelemetry.io/proto/otlp v1.3.1 // indirect + go.uber.org/zap v1.27.0 // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect + gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect + k8s.io/component-base v0.29.4 // indirect + k8s.io/gengo v0.0.0-20240129211411-f967bbeff4b4 // indirect +) + +require ( github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/emicklei/go-restful/v3 v3.12.1 // indirect @@ -53,12 +68,10 @@ require ( github.com/expr-lang/expr v1.16.9 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/go-errors/errors v1.5.1 // indirect - github.com/go-logr/stdr v1.2.2 // indirect github.com/go-logr/zapr v1.3.0 // indirect github.com/go-openapi/jsonpointer v0.21.0 // indirect github.com/go-openapi/jsonreference v0.21.0 // indirect github.com/go-openapi/swag v0.23.0 // indirect - github.com/go-task/slim-sprig/v3 v3.0.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.4 // indirect @@ -67,8 +80,6 @@ require ( github.com/google/pprof v0.0.0-20240528025155-186aa0362fba // indirect github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect github.com/google/uuid v1.6.0 // indirect - github.com/gorilla/websocket v1.5.1 // indirect - github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect github.com/imdario/mergo v0.3.16 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect @@ -80,7 +91,6 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect - github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/prometheus/client_golang v1.19.1 @@ -93,11 +103,8 @@ require ( go.opentelemetry.io/otel/exporters/prometheus v0.50.0 go.opentelemetry.io/otel/metric v1.28.0 go.opentelemetry.io/otel/sdk/metric v1.28.0 - go.opentelemetry.io/otel/trace v1.28.0 // indirect - go.opentelemetry.io/proto/otlp v1.3.1 // indirect go.starlark.net v0.0.0-20231121155337-90ade8b19d09 // indirect go.uber.org/multierr v1.11.0 // indirect - go.uber.org/zap v1.27.0 // indirect golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect golang.org/x/mod v0.18.0 // indirect golang.org/x/net v0.26.0 // indirect @@ -108,15 +115,11 @@ require ( golang.org/x/time v0.5.0 // indirect golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect - google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect - gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/apiextensions-apiserver v0.29.4 // indirect - k8s.io/component-base v0.29.4 // indirect - k8s.io/gengo v0.0.0-20240129211411-f967bbeff4b4 // indirect k8s.io/klog/v2 v2.120.1 // indirect k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect knative.dev/pkg v0.0.0-20240423132823-3c6badc82748 // indirect diff --git a/interceptor/middleware/responsewriter.go b/interceptor/middleware/responsewriter.go index ed18051c..bf11c537 100644 --- a/interceptor/middleware/responsewriter.go +++ b/interceptor/middleware/responsewriter.go @@ -1,6 +1,9 @@ package middleware import ( + "bufio" + "errors" + "net" "net/http" ) @@ -46,3 +49,11 @@ func (rw *responseWriter) WriteHeader(statusCode int) { rw.statusCode = statusCode } + +func (rw *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) { + if hj, ok := rw.downstreamResponseWriter.(http.Hijacker); ok { + return hj.Hijack() + } + + return nil, nil, errors.New("http.Hijacker not implemented") +} diff --git a/interceptor/middleware/responsewriter_test.go b/interceptor/middleware/responsewriter_test.go index 860b4265..0ff5bccc 100644 --- a/interceptor/middleware/responsewriter_test.go +++ b/interceptor/middleware/responsewriter_test.go @@ -4,6 +4,7 @@ import ( "net/http" "net/http/httptest" + "github.com/gorilla/websocket" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" ) @@ -119,4 +120,49 @@ var _ = Describe("responseWriter", func() { Expect(w.Code).To(Equal(sc)) }) }) + + Context("Websocket", func() { + It("returns the expected values when http.Hijacker is implemented", func() { + + // Create a server that will accept websocket connections + w := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Upgrade the connection to a websocket connection + upgrader := websocket.Upgrader{} + conn, err := upgrader.Upgrade(w, r, nil) + Expect(err).To(BeNil()) + Expect(conn).NotTo(BeNil()) + + // Write a message to the client + err = conn.WriteMessage(websocket.TextMessage, []byte("hello")) + Expect(err).To(BeNil()) + + // Close the connection + err = conn.Close() + Expect(err).To(BeNil()) + })) + defer w.Close() + + w.Client() + + lrw := &loggingResponseWriter{ + downstreamResponseWriter: w.Client(), + } + + // Create a client that will connect to the server + dialer := websocket.Dialer{} + conn, _, err := dialer.Dial(w.URL, nil) + }) + + It("returns an error when http.Hijacker is not implemented", func() { + w := httptest.NewRecorder() + lrw := &loggingResponseWriter{ + downstreamResponseWriter: w, + } + + c, r, err := lrw.Hijack() + Expect(err).To(MatchError("http.Hijacker not implemented")) + Expect(c).To(BeNil()) + Expect(r).To(BeNil()) + }) + }) })