Skip to content

Commit

Permalink
swagger: block swagger url if disbale swagger server (tikv#6785) (tik…
Browse files Browse the repository at this point in the history
…v#6789)

close tikv#6786

/swagger/* return 404

Signed-off-by: ti-chi-bot <[email protected]>
Signed-off-by: bufferflies <[email protected]>

Co-authored-by: buffer <[email protected]>
Co-authored-by: bufferflies <[email protected]>
  • Loading branch information
ti-chi-bot and bufferflies authored Jul 14, 2023
1 parent 086871d commit f427242
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 38 deletions.
5 changes: 4 additions & 1 deletion cmd/pd-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ func main() {

// Creates server.
ctx, cancel := context.WithCancel(context.Background())
serviceBuilders := []server.HandlerBuilder{api.NewHandler, apiv2.NewV2Handler, swaggerserver.NewHandler, autoscaling.NewHandler}
serviceBuilders := []server.HandlerBuilder{api.NewHandler, apiv2.NewV2Handler, autoscaling.NewHandler}
if swaggerserver.Enabled() {
serviceBuilders = append(serviceBuilders, swaggerserver.NewHandler)
}
serviceBuilders = append(serviceBuilders, dashboard.GetServiceBuilders()...)
svr, err := server.CreateServer(ctx, cfg, serviceBuilders...)
if err != nil {
Expand Down
29 changes: 0 additions & 29 deletions pkg/swaggerserver/swagger_handler.go

This file was deleted.

12 changes: 11 additions & 1 deletion pkg/swaggerserver/swaggerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build swagger_server
// +build swagger_server

package swaggerserver

import (
"context"
"net/http"

httpSwagger "github.com/swaggo/http-swagger"
_ "github.com/tikv/pd/docs/swagger"
"github.com/tikv/pd/server"
)

Expand All @@ -32,9 +37,14 @@ var (
}
)

// Enabled return true if swagger server is disabled.
func Enabled() bool {
return true
}

// NewHandler creates a HTTP handler for Swagger.
func NewHandler(context.Context, *server.Server) (http.Handler, server.ServiceGroup, error) {
swaggerHandler := http.NewServeMux()
swaggerHandler.Handle(swaggerPrefix, handler())
swaggerHandler.Handle(swaggerPrefix, httpSwagger.Handler())
return swaggerHandler, swaggerServiceGroup, nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 TiKV Project Authors.
// Copyright 2023 TiKV Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -18,12 +18,18 @@
package swaggerserver

import (
"io"
"context"
"net/http"

"github.com/tikv/pd/server"
)

func handler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = io.WriteString(w, "Swagger UI is not built. Try `make` without `SWAGGER=1`.\n")
})
// Enabled return false if swagger server is disabled.
func Enabled() bool {
return false
}

// NewHandler creates a HTTP handler for Swagger.
func NewHandler(context.Context, *server.Server) (http.Handler, server.ServiceGroup, error) {
return nil, server.ServiceGroup{}, nil
}
5 changes: 4 additions & 1 deletion tests/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ func NewTestServer(ctx context.Context, cfg *config.Config) (*TestServer, error)
if err != nil {
return nil, err
}
serviceBuilders := []server.HandlerBuilder{api.NewHandler, apiv2.NewV2Handler, swaggerserver.NewHandler, autoscaling.NewHandler}
serviceBuilders := []server.HandlerBuilder{api.NewHandler, apiv2.NewV2Handler, autoscaling.NewHandler}
if swaggerserver.Enabled() {
serviceBuilders = append(serviceBuilders, swaggerserver.NewHandler)
}
serviceBuilders = append(serviceBuilders, dashboard.GetServiceBuilders()...)
svr, err := server.CreateServer(ctx, cfg, serviceBuilders...)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions tests/server/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,15 @@ func (suite *middlewareTestSuite) TestRateLimitMiddleware() {
}
}

func (suite *middlewareTestSuite) TestSwaggerUrl() {
leader := suite.cluster.GetServer(suite.cluster.GetLeader())
req, _ := http.NewRequest(http.MethodGet, leader.GetAddr()+"/swagger/ui/index", nil)
resp, err := dialClient.Do(req)
suite.NoError(err)
suite.True(resp.StatusCode == http.StatusNotFound)
resp.Body.Close()
}

func (suite *middlewareTestSuite) TestAuditPrometheusBackend() {
leader := suite.cluster.GetServer(suite.cluster.GetLeader())
input := map[string]interface{}{
Expand Down

0 comments on commit f427242

Please sign in to comment.