Skip to content

Commit

Permalink
fix: change the /version to /apisix/admin/tool/version (#1429)
Browse files Browse the repository at this point in the history
  • Loading branch information
starsz authored Feb 5, 2021
1 parent 661e48f commit 61d8118
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 44 deletions.
87 changes: 46 additions & 41 deletions api/internal/filter/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,50 +45,55 @@ func (mw *AuthenticationMiddleware) Handle(ctx droplet.Context) error {

req := httpReq.(*http.Request)

if req.URL.Path != "/apisix/admin/user/login" && strings.HasPrefix(req.URL.Path, "/apisix") {
tokenStr := req.Header.Get("Authorization")

// verify token
token, err := jwt.ParseWithClaims(tokenStr, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(conf.AuthConf.Secret), nil
})

// TODO: design the response error code
response := data.Response{Code: 010013, Message: "request unauthorized"}

if err != nil || token == nil || !token.Valid {
log.Warnf("token validate failed: %s", err)
ctx.SetOutput(&data.SpecCodeResponse{StatusCode: http.StatusUnauthorized, Response: response})
return nil
}

claims, ok := token.Claims.(*jwt.StandardClaims)
if !ok {
log.Warnf("token validate failed: %s, %v", err, token.Valid)
ctx.SetOutput(&data.SpecCodeResponse{StatusCode: http.StatusUnauthorized, Response: response})
return nil
}

if err := token.Claims.Valid(); err != nil {
log.Warnf("token claims validate failed: %s", err)
ctx.SetOutput(&data.SpecCodeResponse{StatusCode: http.StatusUnauthorized, Response: response})
return nil
}

if claims.Subject == "" {
log.Warn("token claims subject empty")
ctx.SetOutput(&data.SpecCodeResponse{StatusCode: http.StatusUnauthorized, Response: response})
return nil
}

if _, ok := conf.UserList[claims.Subject]; !ok {
log.Warnf("user not exists by token claims subject %s", claims.Subject)
ctx.SetOutput(&data.SpecCodeResponse{StatusCode: http.StatusUnauthorized, Response: response})
return nil
}
if req.URL.Path == "/apisix/admin/tool/version" || req.URL.Path == "/apisix/admin/user/login" {
return mw.BaseMiddleware.Handle(ctx)
}

if !strings.HasPrefix(req.URL.Path, "/apisix") {
return mw.BaseMiddleware.Handle(ctx)
}

// Need check the auth header
tokenStr := req.Header.Get("Authorization")

// verify token
token, err := jwt.ParseWithClaims(tokenStr, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(conf.AuthConf.Secret), nil
})

// TODO: design the response error code
response := data.Response{Code: 010013, Message: "request unauthorized"}

if err != nil || token == nil || !token.Valid {
log.Warnf("token validate failed: %s", err)
ctx.SetOutput(&data.SpecCodeResponse{StatusCode: http.StatusUnauthorized, Response: response})
return nil
}

claims, ok := token.Claims.(*jwt.StandardClaims)
if !ok {
log.Warnf("token validate failed: %s, %v", err, token.Valid)
ctx.SetOutput(&data.SpecCodeResponse{StatusCode: http.StatusUnauthorized, Response: response})
return nil
}

if err := token.Claims.Valid(); err != nil {
log.Warnf("token claims validate failed: %s", err)
ctx.SetOutput(&data.SpecCodeResponse{StatusCode: http.StatusUnauthorized, Response: response})
return nil
}

if claims.Subject == "" {
log.Warn("token claims subject empty")
ctx.SetOutput(&data.SpecCodeResponse{StatusCode: http.StatusUnauthorized, Response: response})
return nil
}

if _, ok := conf.UserList[claims.Subject]; !ok {
log.Warnf("user not exists by token claims subject %s", claims.Subject)
ctx.SetOutput(&data.SpecCodeResponse{StatusCode: http.StatusUnauthorized, Response: response})
return nil
}

return mw.BaseMiddleware.Handle(ctx)
}
2 changes: 1 addition & 1 deletion api/internal/handler/tool/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewHandler() (handler.RouteRegister, error) {
}

func (h *Handler) ApplyRoute(r *gin.Engine) {
r.GET("/version", wgin.Wraps(h.Version))
r.GET("/apisix/admin/tool/version", wgin.Wraps(h.Version))
}

func (h *Handler) Version(_ droplet.Context) (interface{}, error) {
Expand Down
2 changes: 1 addition & 1 deletion api/test/e2e/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestInfo(t *testing.T) {
Desc: "get info",
Object: ManagerApiExpect(t),
Method: http.MethodGet,
Path: "/version",
Path: "/apisix/admin/tool/version",
ExpectStatus: http.StatusOK,
ExpectBody: []string{"commit_hash", "\"version\""},
},
Expand Down
12 changes: 11 additions & 1 deletion api/test/shell/cli_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,16 @@ if [ "$respCode" != "0" ] || [ $respMessage != "\"\"" ]; then
exit 1
fi

./manager-api stop
# check the version api
resp=$(curl http://127.0.0.1:9000/apisix/admin/tool/version)
if [[ `echo ${resp} | grep -c "${VERSION}"` -ne '1' ]]; then
echo "failed: can't through api to get version info"
exit 1
fi

if [[ `echo ${resp} | grep -c "${GITHASH}"` -ne '1' ]]; then
echo "failed: can't through api to get githash info"
exit 1
fi

check_logfile

0 comments on commit 61d8118

Please sign in to comment.