From 41ae56240a9bf64dc6c7009235219d506dc392ca Mon Sep 17 00:00:00 2001 From: GuwandykA Date: Sun, 25 Oct 2026 22:46:43 +0500 Subject: [PATCH] visamasteractions: register /:id/actions as its own rbac prefix The server panicked at startup: the actions group declared require-staff:cards for /visa-master-payment-orders, which cards/visamaster already registers with require:cards, and rbac allows one policy per prefix. The group is now /visa-master-payment-orders/:id/actions, the same way /callback has its own prefix; the routes are unchanged. manager.New's handler registration moves into registerRoutes, and TestRegisterRoutes runs it without a database, so a conflicting policy fails the tests instead of the server. Co-Authored-By: Claude Opus 5.5 --- internal/cards/visamasteractions/handler.go | 10 ++++--- internal/handlers/manager/manager.go | 20 +++++++++----- internal/handlers/manager/manager_test.go | 29 +++++++++++++++++++++ 3 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 internal/handlers/manager/manager_test.go diff --git a/internal/cards/visamasteractions/handler.go b/internal/cards/visamasteractions/handler.go index 97090b0..f2260b0 100644 --- a/internal/cards/visamasteractions/handler.go +++ b/internal/cards/visamasteractions/handler.go @@ -27,11 +27,13 @@ func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler } // Register only wires read endpoints: actions are recorded by the -// repositories that change orders, never through the API. +// repositories that change orders, never through the API. The group is +// its own /:id/actions prefix because rbac allows one policy per prefix +// and cards/visamaster already declares require:cards for baseURL. func (h *handler) Register(router *gin.RouterGroup) { - group := rbac.Group(router, baseURL, rbac.RequireStaff(perm.Cards)) - group.GET("/:id/actions", h.list) - group.GET("/:id/actions/:actionId", h.get) + group := rbac.Group(router, baseURL+"/:id/actions", rbac.RequireStaff(perm.Cards)) + group.GET("", h.list) + group.GET("/:actionId", h.get) } // list godoc diff --git a/internal/handlers/manager/manager.go b/internal/handlers/manager/manager.go index 61e91dd..a009f3b 100755 --- a/internal/handlers/manager/manager.go +++ b/internal/handlers/manager/manager.go @@ -74,6 +74,19 @@ func New(cfg *config.Config, client *pgxpool.Pool, logger *logging.Logger) *gin. } adminGroup.Use(rbac.Guard(rbac.NewService(client))) + registerRoutes(router, adminGroup, cfg, client, logger) + + // Deny by default: every admin route must declare its policy via rbac.Group. + if err := rbac.VerifyRoutes(router.Routes(), adminAPIPrefix); err != nil { + logger.Fatal(err) + } + + return router +} + +// registerRoutes has every bounded context register its handlers. rbac +// panics here if two groups declare different policies for one prefix. +func registerRoutes(router *gin.Engine, adminGroup *gin.RouterGroup, cfg *config.Config, client *pgxpool.Pool, logger *logging.Logger) { // Each bounded context registers its own modules' handlers. // Add new modules inside the relevant context's router.go rather than here. identity.Register(adminGroup, router, client, logger) @@ -94,13 +107,6 @@ func New(cfg *config.Config, client *pgxpool.Pool, logger *logging.Logger) *gin. logger, ) backup.NewHandler(logger, backupSvc).Register(adminGroup) - - // Deny by default: every admin route must declare its policy via rbac.Group. - if err := rbac.VerifyRoutes(router.Routes(), adminAPIPrefix); err != nil { - logger.Fatal(err) - } - - return router } // paymentGatewayClient picks the real GBÜS client outside debug mode, and diff --git a/internal/handlers/manager/manager_test.go b/internal/handlers/manager/manager_test.go new file mode 100644 index 0000000..c1103fd --- /dev/null +++ b/internal/handlers/manager/manager_test.go @@ -0,0 +1,29 @@ +package handlermanager + +import ( + "testing" + + "online-back/internal/config" + "online-back/internal/identity/rbac" + "online-back/pkg/logging" + + "github.com/gin-gonic/gin" +) + +// TestRegisterRoutes registers every handler the way New does (no database: +// handlers only touch it per request), so a startup panic such as two +// groups declaring different policies for one prefix fails here instead +// of on the server. +func TestRegisterRoutes(t *testing.T) { + gin.SetMode(gin.TestMode) + debug := true + cfg := &config.Config{IsDebug: &debug} + + router := gin.New() + adminGroup := router.Group(adminAPIPrefix) + registerRoutes(router, adminGroup, cfg, nil, logging.GetLogger()) + + if err := rbac.VerifyRoutes(router.Routes(), adminAPIPrefix); err != nil { + t.Fatal(err) + } +}