From 61606289dbb1379aee0c17502993d26173815800 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Bedn=C3=A1=C5=99?= Date: Mon, 6 Apr 2020 07:36:26 +0200 Subject: [PATCH 1/4] chore(handler): add 2.0 compatible health endpoint from v1.8 (#17252) --- services/httpd/handler.go | 22 ++++++++++++++++++++++ services/httpd/handler_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/services/httpd/handler.go b/services/httpd/handler.go index e37945a6a4f..a96314e62d0 100644 --- a/services/httpd/handler.go +++ b/services/httpd/handler.go @@ -218,6 +218,10 @@ func NewHandler(c Config) *Handler { "status-head", "HEAD", "/status", false, true, authWrapper(h.serveStatus), }, + Route{ // Ping + "ping", + "GET", "/health", false, true, authWrapper(h.serveHealth), + }, Route{ "prometheus-metrics", "GET", "/metrics", false, true, authWrapper(promhttp.Handler().ServeHTTP), @@ -1000,6 +1004,24 @@ func (h *Handler) servePing(w http.ResponseWriter, r *http.Request) { } } +// serveHealth maps v2 health endpoint to ping endpoint +func (h *Handler) serveHealth(w http.ResponseWriter, r *http.Request) { + resp := map[string]interface{}{ + "name": "influxdb", + "message": "ready for queries and writes", + "status": "pass", + "checks": []string{}, + "version": h.Version, + } + b, _ := json.Marshal(resp) + h.writeHeader(w, http.StatusOK) + w.Header().Set("Content-Type", "application/json; charset=utf-8") + if _, err := w.Write(b); err != nil { + h.httpError(w, err.Error(), http.StatusInternalServerError) + return + } +} + // serveStatus has been deprecated. func (h *Handler) serveStatus(w http.ResponseWriter, r *http.Request) { h.Logger.Info("WARNING: /status has been deprecated. Use /ping instead.") diff --git a/services/httpd/handler_test.go b/services/httpd/handler_test.go index 2c06e17bae1..c483d74cec8 100644 --- a/services/httpd/handler_test.go +++ b/services/httpd/handler_test.go @@ -35,6 +35,7 @@ import ( "github.com/influxdata/influxdb/models" "github.com/influxdata/influxdb/monitor" "github.com/influxdata/influxdb/monitor/diagnostics" + "github.com/influxdata/influxdb/pkg/testing/assert" "github.com/influxdata/influxdb/prometheus/remote" "github.com/influxdata/influxdb/query" "github.com/influxdata/influxdb/services/httpd" @@ -1476,6 +1477,29 @@ func TestHandler_Ping(t *testing.T) { } } +// Ensure the handler handles health requests correctly. +func TestHandler_Health(t *testing.T) { + h := NewHandler(false) + w := httptest.NewRecorder() + h.ServeHTTP(w, MustNewRequest("GET", "/health", nil)) + if w.Code != http.StatusOK { + t.Fatalf("unexpected status: %d", w.Code) + } + + var got map[string]interface{} + if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { + t.Fatal(err) + } + + assert.Equal(t, got["name"], "influxdb", "invalid name") + assert.Equal(t, got["message"], "ready for queries and writes", "invalid message") + assert.Equal(t, got["status"], "pass", "invalid status") + assert.Equal(t, got["version"], "0.0.0", "invalid version") + if _, present := got["checks"]; !present { + t.Fatal("missing checks") + } +} + // Ensure the handler returns the version correctly from the different endpoints. func TestHandler_Version(t *testing.T) { h := NewHandler(false) From 0e458e7175545a9ffbcf127466910673502f325f Mon Sep 17 00:00:00 2001 From: Pavel Zavora Date: Tue, 9 Jun 2020 07:09:27 +0200 Subject: [PATCH 2/4] fix(handler): add User-Agent to allowed CORS headers --- services/httpd/handler.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/services/httpd/handler.go b/services/httpd/handler.go index a96314e62d0..4a9a8f6df8a 100644 --- a/services/httpd/handler.go +++ b/services/httpd/handler.go @@ -218,8 +218,8 @@ func NewHandler(c Config) *Handler { "status-head", "HEAD", "/status", false, true, authWrapper(h.serveStatus), }, - Route{ // Ping - "ping", + Route{ // Health + "health", "GET", "/health", false, true, authWrapper(h.serveHealth), }, Route{ @@ -1845,6 +1845,7 @@ func cors(inner http.Handler) http.Handler { `Authorization`, `Content-Length`, `Content-Type`, + `User-Agent`, `X-CSRF-Token`, `X-HTTP-Method-Override`, }, ", ")) From 0c6940d0a312556c21f74607d0321cc4e6e7be46 Mon Sep 17 00:00:00 2001 From: Pavel Zavora Date: Tue, 9 Jun 2020 09:09:30 +0200 Subject: [PATCH 3/4] fix(handler): allow CORS in v2 compatibility endpoints --- services/httpd/handler.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/services/httpd/handler.go b/services/httpd/handler.go index 4a9a8f6df8a..4b05b7ae6d4 100644 --- a/services/httpd/handler.go +++ b/services/httpd/handler.go @@ -194,6 +194,10 @@ func NewHandler(c Config) *Handler { "write", // Data-ingest route. "POST", "/api/v2/write", true, writeLogEnabled, h.serveWriteV2, }, + Route{ // Enable CORS + "write-options", + "OPTIONS", "/api/v2/write", false, true, h.serveOptions, + }, Route{ "prometheus-write", // Prometheus remote write "POST", "/api/v1/prom/write", false, true, h.servePromWrite, @@ -222,6 +226,10 @@ func NewHandler(c Config) *Handler { "health", "GET", "/health", false, true, authWrapper(h.serveHealth), }, + Route{ // Enable CORS + "health-options", + "OPTIONS", "/health", false, true, h.serveOptions, + }, Route{ "prometheus-metrics", "GET", "/metrics", false, true, authWrapper(promhttp.Handler().ServeHTTP), @@ -273,6 +281,10 @@ func NewHandler(c Config) *Handler { "flux-read", "POST", "/api/v2/query", true, true, nil, } + fluxRouteCors := Route{ + "flux-read-options", + "OPTIONS", "/api/v2/query", false, true, h.serveOptions, + } if !c.FluxEnabled { fluxRoute.HandlerFunc = func(w http.ResponseWriter, r *http.Request) { @@ -281,7 +293,7 @@ func NewHandler(c Config) *Handler { } else { fluxRoute.HandlerFunc = h.serveFluxQuery } - h.AddRoutes(fluxRoute) + h.AddRoutes(fluxRoute, fluxRouteCors) return h } From fe150dc768ba03148bff5c576d3814384e61a4bb Mon Sep 17 00:00:00 2001 From: Pavel Zavora Date: Wed, 10 Jun 2020 19:50:30 +0200 Subject: [PATCH 4/4] chore: update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82d7c41c70e..dd0d2a5d4c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ v1.8.1 [unreleased] ### Bugfixes - [#17638](https://github.com/influxdata/influxdb/pull/17638): Verify precision in write requests. +- [#18410](https://github.com/influxdata/influxdb/pull/18410): Enable CORS in InfluxDB 2.0 compatibility APIs. v1.8.0 [unreleased] -------------------