Skip to content

Commit eeba44c

Browse files
committed
edited custom admin middleware to only handle admin signed requests
1 parent ec5bbd3 commit eeba44c

6 files changed

Lines changed: 96 additions & 23 deletions

File tree

middlewares/access_control_middlewares.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,23 @@ import (
2323
"github.com/google/uuid"
2424
"github.com/l3montree-dev/devguard/accesscontrol"
2525
"github.com/l3montree-dev/devguard/database/models"
26+
"github.com/l3montree-dev/devguard/dtos"
2627
"github.com/l3montree-dev/devguard/shared"
2728
"github.com/l3montree-dev/devguard/utils"
2829
"github.com/labstack/echo/v4"
2930
)
3031

31-
func InstanceAdminMiddleware() echo.MiddlewareFunc {
32+
func InstanceAdminMiddleware(pat shared.PersonalAccessTokenService) echo.MiddlewareFunc {
3233
return func(next echo.HandlerFunc) echo.HandlerFunc {
3334
return func(ctx echo.Context) error {
34-
session := shared.GetSession(ctx)
35-
if !session.IsInstanceAdmin() {
36-
slog.Error("access denied in InstanceAdminMiddleware - user is not an instance admin", "user", session.GetUserID())
37-
return echo.NewHTTPError(403, "you do not have access to this resource")
35+
isAdmin, err := pat.VerifyAdminRequest(ctx.Request())
36+
if err == nil {
37+
if isAdmin {
38+
ctx.Set("session", accesscontrol.NewSession("admin", dtos.AllowedScopes, true))
39+
return next(ctx)
40+
}
3841
}
39-
return next(ctx)
42+
return fmt.Errorf("could not verify admin request: %v", err)
4043
}
4144
}
4245
}

mocks/mock_AdminService.go

Lines changed: 19 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mocks/mock_PersonalAccessTokenService.go

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

router/admin_router.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,20 @@
1616
package router
1717

1818
import (
19+
"github.com/l3montree-dev/devguard/cmd/devguard/api"
1920
"github.com/l3montree-dev/devguard/controllers"
2021
"github.com/l3montree-dev/devguard/middlewares"
22+
"github.com/l3montree-dev/devguard/shared"
2123
"github.com/labstack/echo/v4"
2224
)
2325

2426
type AdminRouter struct {
2527
*echo.Group
2628
}
2729

28-
func NewAdminRouter(sessionRouter SessionRouter, adminController *controllers.AdminController) AdminRouter {
29-
adminRouter := sessionRouter.Group.Group("/admin",
30-
middlewares.InstanceAdminMiddleware(),
30+
func NewAdminRouter(server api.Server, adminController *controllers.AdminController, patService shared.PersonalAccessTokenService) AdminRouter {
31+
adminRouter := server.Echo.Group("/admin",
32+
middlewares.InstanceAdminMiddleware(patService),
3133
)
3234

3335
adminRouter.GET("/", func(ctx echo.Context) error {

services/pat_service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func (p *PatService) markAsLastUsedNow(ctx context.Context, fingerprint string)
232232
return p.patRepository.MarkAsLastUsedNow(ctx, nil, fingerprint)
233233
}
234234

235-
func (p *PatService) verifyAdminRequest(req *http.Request) (bool, error) {
235+
func (p *PatService) VerifyAdminRequest(req *http.Request) (bool, error) {
236236
verifier, _ := httpsign.NewP256Verifier(p.adminPubKey, nil,
237237
httpsign.Headers("@method", "content-digest"))
238238

@@ -262,7 +262,7 @@ func (p *PatService) VerifyRequestSignature(ctx context.Context, req *http.Reque
262262
fingerprint := req.Header.Get("X-Fingerprint")
263263
if fingerprint == "" {
264264
// check if it's an admin request
265-
isAdmin, err := p.verifyAdminRequest(req)
265+
isAdmin, err := p.VerifyAdminRequest(req)
266266
if err != nil {
267267
return nil, fmt.Errorf("could not verify admin request: %v", err)
268268
}

shared/common_interfaces.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type ReleaseService interface {
7070

7171
type PersonalAccessTokenService interface {
7272
VerifyRequestSignature(ctx context.Context, req *http.Request) (AuthSession, error)
73+
VerifyAdminRequest(req *http.Request) (bool, error)
7374
RevokeByPrivateKey(ctx context.Context, privKey string) error
7475
ToModel(ctx context.Context, request dtos.PatCreateRequest, userID string) models.PAT
7576
}

0 commit comments

Comments
 (0)