fix: return 404 instead of links page on bad /api/v2 requests (#21950)

* fix: return 404 instead of links page on bad /api/v2 requests

* chore: update CHANGELOG
pull/21957/head
William Baker 2021-07-27 11:35:29 -06:00 committed by GitHub
parent 9d81f4a096
commit 096a478ff6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 1 deletions

View File

@ -68,6 +68,7 @@ This release adds an embedded SQLite database for storing metadata required by t
1. [21850](https://github.com/influxdata/influxdb/pull/21850): Systemd unit should block on startup until http endpoint is ready
1. [21925](https://github.com/influxdata/influxdb/pull/21925): Upgrade to golang-jwt 3.2.1.
1. [21946](https://github.com/influxdata/influxdb/pull/21946): Prevent silently dropped writes when there are overlapping shards.
1. [21950](https://github.com/influxdata/influxdb/pull/21950): Invalid requests to /api/v2 subroutes now return 404 instead of a list of links.
## v2.0.7 [2021-06-04]

View File

@ -140,7 +140,7 @@ func NewAPIHandler(b *APIBackend, opts ...APIHandlerOptFn) *APIHandler {
b.UserResourceMappingService = authorizer.NewURMService(b.OrgLookupService, b.UserResourceMappingService)
h.Mount("/api/v2", serveLinksHandler(b.HTTPErrorHandler))
h.Handle("/api/v2", serveLinksHandler(b.HTTPErrorHandler))
checkBackend := NewCheckBackend(b.Logger.With(zap.String("handler", "check")), b)
checkBackend.CheckService = authorizer.NewCheckService(b.CheckService,

View File

@ -11,11 +11,66 @@ import (
"github.com/google/go-cmp/cmp"
kithttp "github.com/influxdata/influxdb/v2/kit/transport/http"
"github.com/influxdata/influxdb/v2/pkg/httpc"
"github.com/stretchr/testify/require"
"github.com/yudai/gojsondiff"
"github.com/yudai/gojsondiff/formatter"
"go.uber.org/zap/zaptest"
)
func TestAPIHandlerServeLinks(t *testing.T) {
tests := []struct {
name string
path string
method string
want int
}{
{
name: "correct path - GET",
path: "/api/v2",
method: "GET",
want: http.StatusOK,
},
{
name: "correct path with slash - GET",
path: "/api/v2/",
method: "GET",
want: http.StatusOK,
},
{
name: "correct path - POST",
path: "/api/v2",
method: "POST",
want: http.StatusOK,
},
{
name: "incorrect arbitrary path",
path: "/api/v2/asdf",
method: "GET",
want: http.StatusNotFound,
},
{
// regression test for https://github.com/influxdata/influxdb/issues/21620
name: "incorrect path at a subroute",
path: "/api/v2/query&foo=bar",
method: "GET",
want: http.StatusNotFound,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := httptest.NewRequest(tt.method, tt.path, nil)
w := httptest.NewRecorder()
h := NewAPIHandler(&APIBackend{Logger: zaptest.NewLogger(t)})
h.ServeHTTP(w, r)
res := w.Result()
require.Equal(t, tt.want, res.StatusCode)
})
}
}
func TestAPIHandler_NotFound(t *testing.T) {
type args struct {
method string