Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use validate-token endpoint in MustServer #449

Merged
merged 18 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/vela-worker/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func (w *Worker) server() (http.Handler, *tls.Config) {
// https://pkg.go.dev/github.com/go-vela/worker/router?tab=doc#Load
_server := router.Load(
middleware.RequestVersion,
middleware.ServerAddress(w.Config.Server.Address),
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
middleware.Executors(w.Executors),
middleware.Secret(w.Config.Server.Secret),
middleware.Logger(logrus.StandardLogger(), time.RFC3339, true),
)

Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ require (
github.com/docker/docker v20.10.23+incompatible
github.com/docker/go-units v0.5.0
github.com/gin-gonic/gin v1.9.0
github.com/go-vela/sdk-go v0.18.1
github.com/go-vela/server v0.18.1
github.com/go-vela/types v0.18.1
github.com/go-vela/sdk-go v0.18.2-0.20230327141933-e8d38c73b1bb
github.com/go-vela/server v0.18.2-0.20230324155739-73f83fcfd004
github.com/go-vela/types v0.18.2-0.20230321015315-6c723879639c
github.com/google/go-cmp v0.5.9
github.com/joho/godotenv v1.5.1
github.com/opencontainers/image-spec v1.0.2
Expand All @@ -34,7 +34,7 @@ require (
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect
github.com/alicebob/miniredis/v2 v2.30.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/buildkite/yaml v0.0.0-20210326113714-4a3f40911396 // indirect
github.com/buildkite/yaml v0.0.0-20230306222819-0e4e032d4835 // indirect
github.com/bytedance/sonic v1.8.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bsm/ginkgo/v2 v2.5.0 h1:aOAnND1T40wEdAtkGSkvSICWeQ8L3UASX7YVCqQx+eQ=
github.com/bsm/gomega v1.20.0 h1:JhAwLmtRzXFTx2AkALSLa8ijZafntmhSoU63Ok18Uq8=
github.com/buildkite/yaml v0.0.0-20210326113714-4a3f40911396 h1:qLN32md48xyTEqw6XEZMyNMre7njm0XXvDrea6NVwOM=
github.com/buildkite/yaml v0.0.0-20210326113714-4a3f40911396/go.mod h1:AV5wtJnn1/CRaRGlJ8xspkMWfKXV0/pkJVgGleTIrfk=
github.com/buildkite/yaml v0.0.0-20230306222819-0e4e032d4835 h1:Zfkih+Opdv9y5AOob+8iMsaMYnans+Ozrkb8wiPHbj0=
github.com/buildkite/yaml v0.0.0-20230306222819-0e4e032d4835/go.mod h1:AV5wtJnn1/CRaRGlJ8xspkMWfKXV0/pkJVgGleTIrfk=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.8.0 h1:ea0Xadu+sHlu7x5O3gKhRpQ1IKiMrSiHttPF0ybECuA=
github.com/bytedance/sonic v1.8.0/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
Expand Down Expand Up @@ -156,12 +156,12 @@ github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91
github.com/go-playground/validator/v10 v10.11.2 h1:q3SHpufmypg+erIExEKUmsgmhDTyhcJ38oeKGACXohU=
github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVLvdmjPAeV8BQlHtMnw9D7s=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-vela/sdk-go v0.18.1 h1:qsm8XWjr9btNDL8c58JC93sstRUybL/TklWgeeft860=
github.com/go-vela/sdk-go v0.18.1/go.mod h1:QmfXBAdJ9prgE78TK13XJI8YjvGZA5hc+h79CbvgYGU=
github.com/go-vela/server v0.18.1 h1:INd+nwLh0c+WA+8diIh4scLkByGBGZHiyVd5doLSolQ=
github.com/go-vela/server v0.18.1/go.mod h1:WyJEXyJYYASfqN9PDuHqlBTbhsSRIzOn1E7tM2phZMA=
github.com/go-vela/types v0.18.1 h1:V/luHLnCEaJhD1m9PZCZicIasg8Op6MCK+utkz+gQiU=
github.com/go-vela/types v0.18.1/go.mod h1:6MzMhLaXKSZ9wiJveieqnBd2+4ZMS7yv7+POGSITyS8=
github.com/go-vela/sdk-go v0.18.2-0.20230327141933-e8d38c73b1bb h1:JXEolOu+HFktExoDFcGYIdWS9LfPAQnQMIB4Rm48WS0=
github.com/go-vela/sdk-go v0.18.2-0.20230327141933-e8d38c73b1bb/go.mod h1:N8qFPxB0RsHrSYr01GVwgOOowtSfhvjXtJ1cRBaeTc4=
github.com/go-vela/server v0.18.2-0.20230324155739-73f83fcfd004 h1:yJis1sso5c0ZoeZLfZ/lYsjfxU7H9cYP/VJXssRxDa8=
github.com/go-vela/server v0.18.2-0.20230324155739-73f83fcfd004/go.mod h1:b+7XeGHO4ynIinY9mpWb6ye9psdwHpsAqMWy5oC+zJ0=
github.com/go-vela/types v0.18.2-0.20230321015315-6c723879639c h1:lnCL1knUGvgZQG4YBHSs/CZnxNBfqFUBlGhyq9LO9uk=
github.com/go-vela/types v0.18.2-0.20230321015315-6c723879639c/go.mod h1:6MzMhLaXKSZ9wiJveieqnBd2+4ZMS7yv7+POGSITyS8=
github.com/goccy/go-json v0.10.0 h1:mXKd9Qw4NuzShiRlOXKews24ufknHO7gx30lsDyokKA=
github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
Expand Down
93 changes: 85 additions & 8 deletions router/middleware/perm/perm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,106 @@ import (
"net/http"
"strings"

"github.com/go-vela/sdk-go/vela"
"github.com/go-vela/types"
"github.com/go-vela/worker/router/middleware/user"
"github.com/go-vela/worker/router/middleware/token"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)

// MustServer ensures the user is the vela server.
// MustServer ensures the caller is the vela server.
func MustServer() gin.HandlerFunc {
return func(c *gin.Context) {
u := user.Retrieve(c)
// retrieve the callers token from the request headers
tkn, err := token.Retrieve(c.Request)
if err != nil {
msg := fmt.Sprintf("error parsing token: %s", err)

err := c.Error(fmt.Errorf(msg))
if err != nil {
logrus.Error(err)
}

c.AbortWithStatusJSON(http.StatusBadRequest, err.Error())

if strings.EqualFold(u.GetName(), "vela-server") {
return
}

msg := fmt.Sprintf("User %s is not a platform admin", u.GetName())
// retrieve the configured server address from the context
addr, ok := c.MustGet("server-address").(string)
if !ok {
ecrupper marked this conversation as resolved.
Show resolved Hide resolved
msg := "error retrieving server address"

err := c.Error(fmt.Errorf(msg))
if err != nil {
logrus.Error(err)
}

c.AbortWithStatusJSON(http.StatusInternalServerError, types.Error{Message: &msg})

return
}

err := c.Error(fmt.Errorf(msg))
// create a temporary client to validate the incoming request
vela, err := vela.NewClient(addr, "vela-worker", nil)
if err != nil {
logrus.Error(err)
msg := fmt.Sprintf("error creating vela client: %s", err)

err := c.Error(fmt.Errorf(msg))
if err != nil {
logrus.Error(err)
}

c.AbortWithStatusJSON(http.StatusInternalServerError, types.Error{Message: &msg})

return
}
ecrupper marked this conversation as resolved.
Show resolved Hide resolved

c.AbortWithStatusJSON(http.StatusUnauthorized, types.Error{Message: &msg})
// validate a token was provided
if strings.EqualFold(tkn, "") {
msg := "missing token"

err := c.Error(fmt.Errorf(msg))
if err != nil {
logrus.Error(err)
}

c.AbortWithStatusJSON(http.StatusBadRequest, types.Error{Message: &msg})

return
}

// set the token auth provided in the callers request header
vela.Authentication.SetTokenAuth(tkn)
plyr4 marked this conversation as resolved.
Show resolved Hide resolved

// validate the token with the configured vela server
resp, err := vela.Authentication.ValidateToken()
if err != nil {
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
msg := fmt.Sprintf("error validating token: %s", err)

err := c.Error(fmt.Errorf(msg))
if err != nil {
logrus.Error(err)
}

c.AbortWithStatusJSON(http.StatusInternalServerError, types.Error{Message: &msg})
plyr4 marked this conversation as resolved.
Show resolved Hide resolved

return
}

// if ValidateToken returned anything other than 200 consider the token invalid
if resp.StatusCode != http.StatusOK {
msg := "unable to validate token"

err := c.Error(fmt.Errorf(msg))
if err != nil {
logrus.Error(err)
}

c.AbortWithStatusJSON(http.StatusUnauthorized, types.Error{Message: &msg})

return
}
}
}
Loading