Skip to content

Commit

Permalink
fixes after review: handler properly auth middleware per endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ssd04 committed Aug 16, 2023
1 parent 3b390c1 commit 0bb21e1
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 25 deletions.
24 changes: 21 additions & 3 deletions api/groups/baseGroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ var log = logger.GetOrCreate("api/groups")
type baseGroup struct {
endpoints []*shared.EndpointHandlerData
additionalMiddlewares []gin.HandlerFunc
authMiddleware gin.HandlerFunc
}

func newBaseGroup() *baseGroup {
return &baseGroup{
additionalMiddlewares: make([]gin.HandlerFunc, 0),
authMiddleware: func(ctx *gin.Context) {},
}
}

// RegisterRoutes will register all the endpoints to the given web server
Expand All @@ -28,19 +36,29 @@ func (bg *baseGroup) RegisterRoutes(
continue
}

handlers := make([]gin.HandlerFunc, 0)

if isAuthEnabled {
ws.Use(bg.GetAdditionalMiddlewares()...)
handlers = append(handlers, bg.GetAuthMiddleware())
}

ws.Handle(handlerData.Method, handlerData.Path, handlerData.Handler)
handlers = append(handlers, bg.GetAdditionalMiddlewares()...)
handlers = append(handlers, handlerData.Handler)

ws.Handle(handlerData.Method, handlerData.Path, handlers...)
}
}

// GetAdditionalMiddlewares return additional middlewares
// GetAdditionalMiddlewares returns additional middlewares
func (bg *baseGroup) GetAdditionalMiddlewares() []gin.HandlerFunc {
return bg.additionalMiddlewares
}

// GetAuthMiddleware returns auth middleware
func (bg *baseGroup) GetAuthMiddleware() gin.HandlerFunc {
return bg.authMiddleware
}

func getEndpointStatus(
ws *gin.RouterGroup,
path string,
Expand Down
12 changes: 3 additions & 9 deletions api/groups/eventsGroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ func NewEventsGroup(facade EventsFacadeHandler) (*eventsGroup, error) {
}

h := &eventsGroup{
facade: facade,
baseGroup: &baseGroup{
additionalMiddlewares: make([]gin.HandlerFunc, 0),
},
facade: facade,
baseGroup: newBaseGroup(),
}

h.createMiddlewares()
Expand Down Expand Up @@ -128,18 +126,14 @@ func (h *eventsGroup) finalizedEvents(c *gin.Context) {
}

func (h *eventsGroup) createMiddlewares() {
var middleware []gin.HandlerFunc

user, pass := h.facade.GetConnectorUserAndPass()

if user != "" && pass != "" {
basicAuth := gin.BasicAuth(gin.Accounts{
user: pass,
})
middleware = append(middleware, basicAuth)
h.authMiddleware = basicAuth
}

h.additionalMiddlewares = middleware
}

// IsInterfaceNil returns true if there is no value under the interface
Expand Down
3 changes: 1 addition & 2 deletions api/groups/eventsGroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ func TestNewEventsGroup(t *testing.T) {
eg, err := groups.NewEventsGroup(facade)
require.Nil(t, err)

require.Equal(t, 1, len(eg.GetAdditionalMiddlewares()))

require.NotNil(t, eg.GetAuthMiddleware())
})
}

Expand Down
6 changes: 2 additions & 4 deletions api/groups/hubGroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ func NewHubGroup(facade HubFacadeHandler) (*hubGroup, error) {
}

h := &hubGroup{
facade: facade,
baseGroup: &baseGroup{
additionalMiddlewares: make([]gin.HandlerFunc, 0),
},
facade: facade,
baseGroup: newBaseGroup(),
}

endpoints := []*shared.EndpointHandlerData{
Expand Down
6 changes: 2 additions & 4 deletions api/groups/statusGroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ func NewStatusGroup(facade shared.FacadeHandler) (*statusGroup, error) {
}

sg := &statusGroup{
facade: facade,
baseGroup: &baseGroup{
additionalMiddlewares: make([]gin.HandlerFunc, 0),
},
facade: facade,
baseGroup: newBaseGroup(),
}

endpoints := []*shared.EndpointHandlerData{
Expand Down
6 changes: 6 additions & 0 deletions api/shared/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,9 @@ type WebServerHandler interface {
Close() error
IsInterfaceNil() bool
}

// MiddlewareProcessor defines a processor used internally by the web server when processing requests
type MiddlewareProcessor interface {
MiddlewareHandlerFunc() gin.HandlerFunc
IsInterfaceNil() bool
}
Binary file removed cmd/notifier/event-notifier
Binary file not shown.
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type FlagsConfig struct {
RestApiInterface string
}

// LoadGeneralConfig return a GeneralConfig instance by reading the provided toml file
// LoadGeneralConfig returns a GeneralConfig instance by reading the provided toml file
func LoadGeneralConfig(filePath string) (*GeneralConfig, error) {
cfg := &GeneralConfig{}
err := core.LoadTomlFile(cfg, filePath)
Expand All @@ -88,7 +88,7 @@ func LoadGeneralConfig(filePath string) (*GeneralConfig, error) {
return cfg, err
}

// LoadAPIConfig return a APIRoutesConfig instance by reading the provided toml file
// LoadAPIConfig returns a APIRoutesConfig instance by reading the provided toml file
func LoadAPIConfig(filePath string) (*APIRoutesConfig, error) {
cfg := &APIRoutesConfig{}
err := core.LoadTomlFile(cfg, filePath)
Expand Down
2 changes: 1 addition & 1 deletion redis/redlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (r *redlockWrapper) IsEventProcessed(ctx context.Context, blockHash string)
return r.client.SetEntry(ctx, blockHash, true, r.ttl)
}

// HasConnection return true if the redis client is connected
// HasConnection returns true if the redis client is connected
func (r *redlockWrapper) HasConnection(ctx context.Context) bool {
return r.client.IsConnected(ctx)
}
Expand Down

0 comments on commit 0bb21e1

Please sign in to comment.