Skip to content

Commit

Permalink
refine: feature flag middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
shhdgit committed Nov 11, 2021
1 parent 8bd2e44 commit 74a0937
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 22 deletions.
13 changes: 8 additions & 5 deletions pkg/apiserver/conprof/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ import (
"golang.org/x/sync/singleflight"

"github.com/pingcap/tidb-dashboard/pkg/apiserver/user"
"github.com/pingcap/tidb-dashboard/pkg/apiserver/utils"
apiutils "github.com/pingcap/tidb-dashboard/pkg/apiserver/utils"
"github.com/pingcap/tidb-dashboard/pkg/config"
"github.com/pingcap/tidb-dashboard/pkg/utils"
"github.com/pingcap/tidb-dashboard/pkg/utils/topology"
)

Expand Down Expand Up @@ -81,7 +82,9 @@ func newService(lc fx.Lifecycle, p ServiceParams) *Service {
func registerRouter(r *gin.RouterGroup, auth *user.AuthService, s *Service) {
endpoint := r.Group("/continuous_profiling")

endpoint.Use(FeatureFlagConprof.Middleware(s.params.Config.FeatureVersion))
endpoint.Use(apiutils.MWForbidByFeatureFlag([]*utils.FeatureFlag{
FeatureFlagConprof,
}, s.params.Config.FeatureVersion))
{
endpoint.GET("/config", auth.MWAuthRequired(), s.reverseProxy("/config"), s.conprofConfig)
endpoint.POST("/config", auth.MWAuthRequired(), auth.MWRequireWritePriv(), s.reverseProxy("/config"), s.updateConprofConfig)
Expand All @@ -107,9 +110,9 @@ func (s *Service) reverseProxy(targetPath string) gin.HandlerFunc {
c.Request.URL.Path = targetPath
token := c.Query("token")
if token != "" {
queryStr, err := utils.ParseJWTString("conprof", token)
queryStr, err := apiutils.ParseJWTString("conprof", token)
if err != nil {
utils.MakeInvalidRequestErrorFromError(c, err)
apiutils.MakeInvalidRequestErrorFromError(c, err)
return
}
c.Request.URL.RawQuery = queryStr
Expand Down Expand Up @@ -295,7 +298,7 @@ func (s *Service) conprofGroupProfileDetail(c *gin.Context) {
// @Failure 500 {object} utils.APIError
func (s *Service) genConprofActionToken(c *gin.Context) {
q := c.Query("q")
token, err := utils.NewJWTString("conprof", q)
token, err := apiutils.NewJWTString("conprof", q)
if err != nil {
_ = c.Error(err)
return
Expand Down
48 changes: 48 additions & 0 deletions pkg/apiserver/utils/mw_feature_flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2021 Suhaha
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package utils

import (
"net/http"

"github.com/gin-gonic/gin"

"github.com/pingcap/tidb-dashboard/pkg/utils"
)

var ErrFeatureNotSupported = ErrNS.NewType("feature_not_supported")

func MWForbidByFeatureFlag(featureFlags []*utils.FeatureFlag, targetVersion string) gin.HandlerFunc {
supported := true
unsupportedFeatures := make([]string, len(featureFlags))
for _, ff := range featureFlags {
if !ff.IsSupported(targetVersion) {
supported = false
unsupportedFeatures = append(unsupportedFeatures, ff.Name)
continue
}
}

return func(c *gin.Context) {
if !supported {
_ = c.Error(ErrFeatureNotSupported.New("unsupported features: %v", unsupportedFeatures))
c.Status(http.StatusForbidden)
c.Abort()
return
}

c.Next()
}
}
17 changes: 0 additions & 17 deletions pkg/utils/feature_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@
package utils

import (
"fmt"
"net/http"
"strings"

"github.com/Masterminds/semver"
"github.com/gin-gonic/gin"

"github.com/pingcap/tidb-dashboard/pkg/utils/version"
)
Expand Down Expand Up @@ -60,17 +57,3 @@ func (ff *FeatureFlag) IsSupported(targetVersion string) bool {
}
return false
}

func (ff *FeatureFlag) Middleware(targetVersion string) gin.HandlerFunc {
isSupported := !ff.IsSupported(targetVersion)
return func(c *gin.Context) {
if isSupported {
_ = c.Error(fmt.Errorf("the feature is not supported"))
c.Status(http.StatusForbidden)
c.Abort()
return
}

c.Next()
}
}

0 comments on commit 74a0937

Please sign in to comment.