Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: remove rbac from notification dispatch endpoint for agents (#3223)
Kyle Mendelld5d3952121a3
diff --git a/backend/api/handlers/notifications.go b/backend/api/handlers/notifications.goindex a8cd6c977..fb7bcb30e 100644--- a/backend/api/handlers/notifications.go+++ b/backend/api/handlers/notifications.go@@ -150,6 +150,8 @@ func RegisterNotifications(api huma.API, notificationSvc *services.NotificationSSecurity: []map[string][]string{{"BearerAuth": {}}, {"ApiKeyAuth": {}}},}, authz.PermNotificationsManage, h.TestNotification)+ // Environment tokens are authenticated by ApiKeyAuth and revalidated by the+ // handler. RBAC middleware cannot scope this route because it has no environment ID.huma.Register(api, huma.Operation{OperationID: "dispatch-notification",Method: http.MethodPost,@@ -157,7 +159,6 @@ func RegisterNotifications(api huma.API, notificationSvc *services.NotificationSSummary: "Dispatch notification from remote agent to manager",Tags: []string{"Notifications"},Security: []map[string][]string{{"ApiKeyAuth": {}}},- Middlewares: humamw.RequirePermission(api, authz.PermNotificationsManage),}, h.DispatchNotification)}diff --git a/backend/api/handlers/notifications_test.go b/backend/api/handlers/notifications_test.goindex 12b28c21a..a8afcb2f0 100644--- a/backend/api/handlers/notifications_test.go+++ b/backend/api/handlers/notifications_test.go@@ -1,17 +1,27 @@package handlersimport (+ "bytes""context"+ "encoding/json""net/http"+ "net/http/httptest"+ "reflect"+ "strings""testing""time""github.com/danielgtaylor/huma/v2"+ "github.com/danielgtaylor/huma/v2/adapters/humaecho"+ humamw "github.com/getarcaneapp/arcane/backend/v2/api/middleware""github.com/getarcaneapp/arcane/backend/v2/internal/config""github.com/getarcaneapp/arcane/backend/v2/internal/database""github.com/getarcaneapp/arcane/backend/v2/internal/models""github.com/getarcaneapp/arcane/backend/v2/internal/services"+ "github.com/getarcaneapp/arcane/types/v2/base"+ "github.com/getarcaneapp/arcane/types/v2/imageupdate"notificationdto "github.com/getarcaneapp/arcane/types/v2/notification"+ "github.com/labstack/echo/v4"sqlite "github.com/libtnb/sqlite""github.com/stretchr/testify/assert""github.com/stretchr/testify/require"@@ -134,3 +144,92 @@ func TestDispatchNotification_ReturnsBadRequestForUnsupportedDispatchKind(t *tesrequire.Equal(t, http.StatusBadRequest, statusErr.GetStatus())require.Contains(t, statusErr.Error(), "unsupported dispatch kind")}++func TestDispatchNotificationRoute_AuthenticatesEnvironmentAccessToken(t *testing.T) {+ ctx := context.Background()+ db, svc := setupNotificationHandlerTestService(t)+ cfg := &config.Config{}+ envSvc := services.NewEnvironmentService(db, nil, nil, nil, nil, nil)++ token := "remote-token"+ now := time.Now()+ require.NoError(t, db.WithContext(ctx).Create(&models.Environment{+ BaseModel: models.BaseModel{ID: "env-remote", CreatedAt: now, UpdatedAt: &now},+ Name: "Remote Edge",+ ApiUrl: "http://remote.example",+ Enabled: true,+ Status: string(models.EnvironmentStatusOnline),+ AccessToken: &token,+ }).Error)++ router := echo.New()+ humaConfig := huma.DefaultConfig("test", "1.0.0")+ humaConfig.Components.SecuritySchemes = map[string]*huma.SecurityScheme{+ "BearerAuth": {Type: "http", Scheme: "bearer"},+ "ApiKeyAuth": {Type: "apiKey", In: "header", Name: "X-API-Key"},+ }+ humaConfig.Components.Schemas = huma.NewMapRegistry("#/components/schemas/", func(t reflect.Type, hint string) string {+ for t.Kind() == reflect.Pointer {+ t = t.Elem()+ }+ name := huma.DefaultSchemaNamer(t, hint)+ if t.PkgPath() == "" {+ return name+ }+ return strings.NewReplacer("/", "_", ".", "_").Replace(t.PkgPath()) + "_" + name+ })+ api := humaecho.NewWithGroup(router, router.Group("/api"), humaConfig)+ api.UseMiddleware(humamw.NewAuthBridge(api, &services.AuthService{}, nil, nil, envSvc, cfg))+ RegisterNotifications(api, svc, cfg)++ payload, err := json.Marshal(notificationdto.DispatchRequest{+ Kind: notificationdto.DispatchKindImageUpdate,+ ImageUpdate: ¬ificationdto.DispatchImageUpdate{+ ImageRef: "nginx:latest",+ UpdateInfo: imageupdate.Response{+ HasUpdate: true,+ UpdateType: "digest",+ CurrentDigest: "sha256:current",+ LatestDigest: "sha256:latest",+ },+ },+ })+ require.NoError(t, err)++ t.Run("accepts stored environment token", func(t *testing.T) {+ req := httptest.NewRequest(http.MethodPost, "/api/notifications/dispatch", bytes.NewReader(payload))+ req.Header.Set("Content-Type", "application/json")+ req.Header.Set("X-API-Key", token)+ rec := httptest.NewRecorder()++ router.ServeHTTP(rec, req)++ require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())+ var response base.ApiResponse[notificationdto.DispatchResponse]+ require.NoError(t, json.NewDecoder(rec.Body).Decode(&response))+ require.True(t, response.Success)+ require.Equal(t, "Notification dispatched successfully", response.Data.Message)+ require.Zero(t, response.Data.Delivered)+ })++ for _, testCase := range []struct {+ name string+ token string+ }{+ {name: "rejects missing token"},+ {name: "rejects unknown token", token: "unknown-token"},+ } {+ t.Run(testCase.name, func(t *testing.T) {+ req := httptest.NewRequest(http.MethodPost, "/api/notifications/dispatch", bytes.NewReader(payload))+ req.Header.Set("Content-Type", "application/json")+ if testCase.token != "" {+ req.Header.Set("X-API-Key", testCase.token)+ }+ rec := httptest.NewRecorder()++ router.ServeHTTP(rec, req)++ require.Equal(t, http.StatusUnauthorized, rec.Code, rec.Body.String())+ })+ }+}