From ba126dff512759a2a962b88ebd1db8ab3d4f58e1 Mon Sep 17 00:00:00 2001 From: "M. Mert Yildiran" Date: Sun, 11 Aug 2024 01:35:54 +0300 Subject: [PATCH] Add `X-Kubeshark-Capture: ignore` header to all of the HTTP requests (#1579) * Add `X-Kubeshark-Capture: ignore` header to all of the HTTP requests * Add `X-Kubeshark-Capture: ignore` header to WebSocket requests * Reduce duplication --- cmd/console.go | 1 + internal/connect/hub.go | 2 ++ utils/http.go | 18 +++++++++++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/cmd/console.go b/cmd/console.go index 8298489fe..d716f88ce 100644 --- a/cmd/console.go +++ b/cmd/console.go @@ -59,6 +59,7 @@ func runConsole() { Path: "/scripts/logs", } headers := http.Header{} + headers.Set(utils.X_KUBESHARK_CAPTURE_HEADER_KEY, utils.X_KUBESHARK_CAPTURE_HEADER_IGNORE_VALUE) headers.Set("License-Key", config.Config.License) c, _, err := websocket.DefaultDialer.Dial(u.String(), headers) diff --git a/internal/connect/hub.go b/internal/connect/hub.go index e61a25eb5..881ef6df9 100644 --- a/internal/connect/hub.go +++ b/internal/connect/hub.go @@ -189,6 +189,7 @@ func (connector *Connector) PutScript(script *misc.Script, index int64) (err err log.Error().Err(err).Send() return } + utils.AddIgnoreCaptureHeader(req) req.Header.Set("Content-Type", "application/json") req.Header.Set("License-Key", config.Config.License) @@ -228,6 +229,7 @@ func (connector *Connector) DeleteScript(index int64) (err error) { log.Error().Err(err).Send() return } + utils.AddIgnoreCaptureHeader(req) req.Header.Set("Content-Type", "application/json") req.Header.Set("License-Key", config.Config.License) diff --git a/utils/http.go b/utils/http.go index ef6313977..ac48700b4 100644 --- a/utils/http.go +++ b/utils/http.go @@ -8,10 +8,21 @@ import ( "strings" ) +const ( + X_KUBESHARK_CAPTURE_HEADER_KEY = "X-Kubeshark-Capture" + X_KUBESHARK_CAPTURE_HEADER_IGNORE_VALUE = "ignore" +) + // Get - When err is nil, resp always contains a non-nil resp.Body. // Caller should close resp.Body when done reading from it. func Get(url string, client *http.Client) (*http.Response, error) { - return checkError(client.Get(url)) + req, err := http.NewRequest(http.MethodPost, url, nil) + if err != nil { + return nil, err + } + AddIgnoreCaptureHeader(req) + + return checkError(client.Do(req)) } // Post - When err is nil, resp always contains a non-nil resp.Body. @@ -21,6 +32,7 @@ func Post(url, contentType string, body io.Reader, client *http.Client, licenseK if err != nil { return nil, err } + AddIgnoreCaptureHeader(req) req.Header.Set("Content-Type", "application/json") req.Header.Set("License-Key", licenseKey) @@ -51,3 +63,7 @@ func checkError(response *http.Response, errInOperation error) (*http.Response, return response, nil } + +func AddIgnoreCaptureHeader(req *http.Request) { + req.Header.Set(X_KUBESHARK_CAPTURE_HEADER_KEY, X_KUBESHARK_CAPTURE_HEADER_IGNORE_VALUE) +}