From 814f33330d30ab6905bb3590f4b8d19e4a39d63d Mon Sep 17 00:00:00 2001 From: Max Moeschinger Date: Fri, 22 Dec 2023 09:30:54 +0100 Subject: [PATCH] Add new probe user agent (#863) * Add new user agent probe Signed-off-by: Max Moeschinger * Add fix information to changelog Signed-off-by: Max Moeschinger * Update CHANGELOG.md Co-authored-by: Jorge Turrado Ferrero Signed-off-by: Max Moeschinger --------- Signed-off-by: Max Moeschinger Signed-off-by: Max Moeschinger Co-authored-by: Jorge Turrado Ferrero --- CHANGELOG.md | 1 + interceptor/middleware/routing.go | 8 +++++--- interceptor/middleware/routing_test.go | 18 +++++++++++++++--- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ccd278d..7c0e7625 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ This changelog keeps track of work items that have been completed and are ready ### Fixes +- **General**: Add new user agent probe ([#862](https://github.com/kedacore/http-add-on/issues/862)) - **General**: Increase ScaledObject polling interval to 15 seconds ([#799](https://github.com/kedacore/http-add-on/issues/799)) ### Deprecations diff --git a/interceptor/middleware/routing.go b/interceptor/middleware/routing.go index e332d13b..c92d803a 100644 --- a/interceptor/middleware/routing.go +++ b/interceptor/middleware/routing.go @@ -14,6 +14,7 @@ import ( var ( kpUserAgent = regexp.MustCompile(`(^|\s)kube-probe/`) + ghUserAgent = regexp.MustCompile(`(^|\s)GoogleHC/`) ) type Routing struct { @@ -37,7 +38,7 @@ func (rm *Routing) ServeHTTP(w http.ResponseWriter, r *http.Request) { httpso := rm.routingTable.Route(r) if httpso == nil { - if rm.isKubeProbe(r) { + if rm.isProbe(r) { rm.probeHandler.ServeHTTP(w, r) return } @@ -71,7 +72,8 @@ func (rm *Routing) streamFromHTTPSO(httpso *httpv1alpha1.HTTPScaledObject) (*url )) } -func (rm *Routing) isKubeProbe(r *http.Request) bool { +func (rm *Routing) isProbe(r *http.Request) bool { ua := r.UserAgent() - return kpUserAgent.Match([]byte(ua)) + + return kpUserAgent.Match([]byte(ua)) || ghUserAgent.Match([]byte(ua)) } diff --git a/interceptor/middleware/routing_test.go b/interceptor/middleware/routing_test.go index 8caa7ede..5d195d2a 100644 --- a/interceptor/middleware/routing_test.go +++ b/interceptor/middleware/routing_test.go @@ -182,11 +182,23 @@ var _ = Describe("RoutingMiddleware", func() { r.Header.Set(uaKey, uaVal) var rm Routing - b := rm.isKubeProbe(r) + b := rm.isProbe(r) Expect(b).To(BeTrue()) }) - It("returns false if the request is not from kube-probe", func() { + It("returns true if the request is from GoogleHC", func() { + const ( + uaVal = "Go-http-client/1.1 GoogleHC/1.0 (linux/amd64) kubernetes/4c94112" + ) + + r.Header.Set(uaKey, uaVal) + + var rm Routing + b := rm.isProbe(r) + Expect(b).To(BeTrue()) + }) + + It("returns false if the request is not from kube-probe or GoogleHC", func() { const ( uaVal = "Go-http-client/1.1 kubectl/v1.27.1 (linux/amd64) kubernetes/4c94112" ) @@ -194,7 +206,7 @@ var _ = Describe("RoutingMiddleware", func() { r.Header.Set(uaKey, uaVal) var rm Routing - b := rm.isKubeProbe(r) + b := rm.isProbe(r) Expect(b).To(BeFalse()) }) })