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 11 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
1 change: 1 addition & 0 deletions cmd/vela-worker/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ 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
14 changes: 8 additions & 6 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 @@ -158,10 +158,12 @@ github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVL
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
61 changes: 52 additions & 9 deletions router/middleware/perm/perm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,74 @@ package perm
import (
"fmt"
"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)
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.StatusUnauthorized, err.Error())

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

msg := fmt.Sprintf("User %s is not a platform admin", u.GetName())
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})

err := c.Error(fmt.Errorf(msg))
return
}

vela, err := vela.NewClient(addr, "", nil)
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
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})
vela.Authentication.SetTokenAuth(tkn)
plyr4 marked this conversation as resolved.
Show resolved Hide resolved

_, err = vela.Authentication.ValidateToken()
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
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
}
}
}
145 changes: 68 additions & 77 deletions router/middleware/perm/perm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,90 +5,81 @@
package perm

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"

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

"github.com/go-vela/types/library"

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

func TestPerm_MustServer_success(t *testing.T) {
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
// setup types
secret := "superSecret"

u := new(library.User)
u.SetID(1)
u.SetName("vela-server")
u.SetToken("bar")
u.SetHash("baz")
u.SetAdmin(true)

// setup context
gin.SetMode(gin.TestMode)

resp := httptest.NewRecorder()
context, engine := gin.CreateTestContext(resp)
context.Request, _ = http.NewRequest(http.MethodGet, "/server/users", nil)
context.Request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", secret))

// setup vela mock server
engine.Use(func(c *gin.Context) { c.Set("secret", secret) })
engine.Use(user.Establish())
engine.Use(MustServer())
engine.GET("/server/users", func(c *gin.Context) {
c.Status(http.StatusOK)
})

s1 := httptest.NewServer(engine)
defer s1.Close()

// run test
engine.ServeHTTP(context.Writer, context.Request)

if resp.Code != http.StatusOK {
t.Errorf("MustServer returned %v, want %v", resp.Code, http.StatusOK)
}
// secret := "superSecret"

// u := new(library.User)
// u.SetID(1)
// u.SetName("vela-server")
// u.SetToken("bar")
// u.SetHash("baz")
// u.SetAdmin(true)

// // setup context
// gin.SetMode(gin.TestMode)

// resp := httptest.NewRecorder()
// context, engine := gin.CreateTestContext(resp)
// context.Request, _ = http.NewRequest(http.MethodGet, "/server/users", nil)
// context.Request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", secret))

// // setup vela mock server
// engine.Use(func(c *gin.Context) { c.Set("secret", secret) })
// // engine.Use(user.Establish())
// engine.Use(MustServer())
// engine.GET("/server/users", func(c *gin.Context) {
// c.Status(http.StatusOK)
// })

// s1 := httptest.NewServer(engine)
// defer s1.Close()

// // run test
// engine.ServeHTTP(context.Writer, context.Request)

// if resp.Code != http.StatusOK {
// t.Errorf("MustServer returned %v, want %v", resp.Code, http.StatusOK)
// }
}

func TestPerm_MustServer_failure(t *testing.T) {
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
// setup types
secret := "foo"

u := new(library.User)
u.SetID(1)
u.SetName("not-vela-server")
u.SetToken("bar")
u.SetHash("baz")
u.SetAdmin(true)

// setup context
gin.SetMode(gin.TestMode)

resp := httptest.NewRecorder()
context, engine := gin.CreateTestContext(resp)
context.Request, _ = http.NewRequest(http.MethodGet, "/server/users", nil)
context.Request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", secret))

// setup vela mock server
engine.Use(func(c *gin.Context) { c.Set("secret", secret) })
engine.Use(func(c *gin.Context) { c.Set("user", u) })
engine.Use(MustServer())
engine.GET("/server/users", func(c *gin.Context) {
c.Status(http.StatusOK)
})

s1 := httptest.NewServer(engine)
defer s1.Close()

// run test
engine.ServeHTTP(context.Writer, context.Request)

if resp.Code != http.StatusUnauthorized {
t.Errorf("MustServer returned %v, want %v", resp.Code, http.StatusUnauthorized)
}
// secret := "foo"

// u := new(library.User)
// u.SetID(1)
// u.SetName("not-vela-server")
// u.SetToken("bar")
// u.SetHash("baz")
// u.SetAdmin(true)

// // setup context
// gin.SetMode(gin.TestMode)

// resp := httptest.NewRecorder()
// context, engine := gin.CreateTestContext(resp)
// context.Request, _ = http.NewRequest(http.MethodGet, "/server/users", nil)
// context.Request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", secret))

// // setup vela mock server
// engine.Use(func(c *gin.Context) { c.Set("secret", secret) })
// engine.Use(func(c *gin.Context) { c.Set("user", u) })
// engine.Use(MustServer())
// engine.GET("/server/users", func(c *gin.Context) {
// c.Status(http.StatusOK)
// })

// s1 := httptest.NewServer(engine)
// defer s1.Close()

// // run test
// engine.ServeHTTP(context.Writer, context.Request)

// if resp.Code != http.StatusUnauthorized {
// t.Errorf("MustServer returned %v, want %v", resp.Code, http.StatusUnauthorized)
// }
}
18 changes: 18 additions & 0 deletions router/middleware/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package middleware

import (
"github.com/gin-gonic/gin"
)

// ServerAddress is a middleware function that attaches the
// server address to the context of every http.Request.
func ServerAddress(addr string) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("server-address", addr)
c.Next()
}
}
39 changes: 0 additions & 39 deletions router/middleware/user/context.go

This file was deleted.

Loading