Skip to content

Commit 822d189

Browse files
authored
HTTP datasource: allow GET/HEAD request for checking if the datasource is working (#3710)
1 parent 25726b0 commit 822d189

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

pkg/acquisition/modules/http/http.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"io"
1212
"net"
1313
"net/http"
14+
"net/http/httputil"
1415
"os"
1516
"time"
1617

@@ -280,6 +281,16 @@ func (h *HTTPSource) processRequest(w http.ResponseWriter, r *http.Request, hc *
280281

281282
defer r.Body.Close()
282283

284+
if h.logger.Logger.IsLevelEnabled(log.TraceLevel) {
285+
h.logger.Tracef("processing request from '%s' with method '%s' and path '%s'", r.RemoteAddr, r.Method, r.URL.Path)
286+
bodyContent, err := httputil.DumpRequest(r, true)
287+
if err != nil {
288+
h.logger.Errorf("failed to dump request: %s", err)
289+
} else {
290+
h.logger.Tracef("request body: %s", bodyContent)
291+
}
292+
}
293+
283294
reader := r.Body
284295

285296
if r.Header.Get("Content-Encoding") == "gzip" {
@@ -338,20 +349,26 @@ func (h *HTTPSource) processRequest(w http.ResponseWriter, r *http.Request, hc *
338349
func (h *HTTPSource) RunServer(out chan types.Event, t *tomb.Tomb) error {
339350
mux := http.NewServeMux()
340351
mux.HandleFunc(h.Config.Path, func(w http.ResponseWriter, r *http.Request) {
341-
if r.Method != http.MethodPost {
342-
h.logger.Errorf("method not allowed: %s", r.Method)
343-
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
344-
345-
return
346-
}
347-
348352
if err := authorizeRequest(r, &h.Config); err != nil {
349353
h.logger.Errorf("failed to authorize request from '%s': %s", r.RemoteAddr, err)
350354
http.Error(w, "Unauthorized", http.StatusUnauthorized)
351355

352356
return
353357
}
354358

359+
switch r.Method {
360+
case http.MethodGet, http.MethodHead: // Return a 200 if the auth was successful
361+
h.logger.Infof("successful %s request from '%s'", r.Method, r.RemoteAddr)
362+
w.WriteHeader(http.StatusOK)
363+
w.Write([]byte("OK"))
364+
return
365+
case http.MethodPost: // POST is handled below
366+
default:
367+
h.logger.Errorf("method not allowed: %s", r.Method)
368+
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
369+
return
370+
}
371+
355372
if r.RemoteAddr == "@" {
356373
//We check if request came from unix socket and if so we set to loopback
357374
r.RemoteAddr = "127.0.0.1:65535"

pkg/acquisition/modules/http/http_test.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ func SetupAndRunHTTPSource(t *testing.T, h *HTTPSource, config []byte, metricLev
244244
return out, testRegistry, &tomb
245245
}
246246

247-
func TestStreamingAcquisitionWrongHTTPMethod(t *testing.T) {
247+
func TestStreamingAcquisitionHTTPMethod(t *testing.T) {
248248
h := &HTTPSource{}
249249
_, _, tomb := SetupAndRunHTTPSource(t, h, []byte(`
250250
source: http
@@ -259,13 +259,32 @@ basic_auth:
259259

260260
ctx := t.Context()
261261

262-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, testHTTPServerAddr+"/test", http.NoBody)
262+
req, err := http.NewRequestWithContext(ctx, http.MethodPut, testHTTPServerAddr+"/test", http.NoBody)
263263
require.NoError(t, err)
264264

265+
// Method validity is checked after auth
266+
req.SetBasicAuth("test", "test")
267+
265268
res, err := http.DefaultClient.Do(req)
266269
require.NoError(t, err)
267270
assert.Equal(t, http.StatusMethodNotAllowed, res.StatusCode)
268271

272+
// Check that GET/HEAD requests return a 200
273+
274+
req, err = http.NewRequestWithContext(ctx, http.MethodGet, testHTTPServerAddr+"/test", http.NoBody)
275+
require.NoError(t, err)
276+
req.SetBasicAuth("test", "test")
277+
res, err = http.DefaultClient.Do(req)
278+
require.NoError(t, err)
279+
assert.Equal(t, http.StatusOK, res.StatusCode)
280+
281+
req, err = http.NewRequestWithContext(ctx, http.MethodHead, testHTTPServerAddr+"/test", http.NoBody)
282+
require.NoError(t, err)
283+
req.SetBasicAuth("test", "test")
284+
res, err = http.DefaultClient.Do(req)
285+
require.NoError(t, err)
286+
assert.Equal(t, http.StatusOK, res.StatusCode)
287+
269288
h.Server.Close()
270289
tomb.Kill(nil)
271290
err = tomb.Wait()

0 commit comments

Comments
 (0)