Skip to content

Commit

Permalink
feat(middleware-logging): implement http.Hijacker
Browse files Browse the repository at this point in the history
http.Hijacker is mandatory to forward websocket connexions

Signed-off-by: Guilhem Lettron <[email protected]>
  • Loading branch information
guilhem committed Aug 19, 2024
1 parent 70aa9be commit c28ba10
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 12 deletions.
27 changes: 15 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions interceptor/middleware/responsewriter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package middleware

import (
"bufio"
"errors"
"net"
"net/http"
)

Expand Down Expand Up @@ -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")
}
46 changes: 46 additions & 0 deletions interceptor/middleware/responsewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"net/http/httptest"

"github.com/gorilla/websocket"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
Expand Down Expand Up @@ -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{

Check failure on line 147 in interceptor/middleware/responsewriter_test.go

View workflow job for this annotation

GitHub Actions / Static Checks

lrw declared and not used

Check failure on line 147 in interceptor/middleware/responsewriter_test.go

View workflow job for this annotation

GitHub Actions / Static Checks

undefined: loggingResponseWriter
downstreamResponseWriter: w.Client(),
}

// Create a client that will connect to the server
dialer := websocket.Dialer{}
conn, _, err := dialer.Dial(w.URL, nil)

Check failure on line 153 in interceptor/middleware/responsewriter_test.go

View workflow job for this annotation

GitHub Actions / Static Checks

conn declared and not used

Check failure on line 153 in interceptor/middleware/responsewriter_test.go

View workflow job for this annotation

GitHub Actions / Static Checks

err declared and not used
})

It("returns an error when http.Hijacker is not implemented", func() {
w := httptest.NewRecorder()
lrw := &loggingResponseWriter{

Check failure on line 158 in interceptor/middleware/responsewriter_test.go

View workflow job for this annotation

GitHub Actions / Static Checks

undefined: loggingResponseWriter (typecheck)
downstreamResponseWriter: w,
}

c, r, err := lrw.Hijack()
Expect(err).To(MatchError("http.Hijacker not implemented"))
Expect(c).To(BeNil())
Expect(r).To(BeNil())
})
})
})

0 comments on commit c28ba10

Please sign in to comment.