SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
Allow deletion of multiple panels for dashboard updates made with API key (#8903)
nikhilmantri09025d9247f59161
diff --git a/pkg/http/middleware/api_key.go b/pkg/http/middleware/api_key.goindex 22d088178b..107237a429 100644--- a/pkg/http/middleware/api_key.go+++ b/pkg/http/middleware/api_key.go@@ -98,8 +98,10 @@ func (a *APIKey) Wrap(next http.Handler) http.Handler {return}+ ctx = ctxtypes.SetAuthType(ctx, ctxtypes.AuthTypeAPIKey)+comment := ctxtypes.CommentFromContext(ctx)- comment.Set("auth_type", "api_key")+ comment.Set("auth_type", ctxtypes.AuthTypeAPIKey.StringValue())comment.Set("user_id", claims.UserID)comment.Set("org_id", claims.OrgID)diff --git a/pkg/http/middleware/auth.go b/pkg/http/middleware/auth.goindex d52b33df22..19feeb41f7 100644--- a/pkg/http/middleware/auth.go+++ b/pkg/http/middleware/auth.go@@ -51,8 +51,10 @@ func (a *Auth) Wrap(next http.Handler) http.Handler {return}+ ctx = ctxtypes.SetAuthType(ctx, ctxtypes.AuthTypeJWT)+comment := ctxtypes.CommentFromContext(ctx)- comment.Set("auth_type", "jwt")+ comment.Set("auth_type", ctxtypes.AuthTypeJWT.StringValue())comment.Set("user_id", claims.UserID)comment.Set("org_id", claims.OrgID)diff --git a/pkg/modules/dashboard/dashboard.go b/pkg/modules/dashboard/dashboard.goindex bbc42f4e67..c72aa0d7f1 100644--- a/pkg/modules/dashboard/dashboard.go+++ b/pkg/modules/dashboard/dashboard.go@@ -17,7 +17,7 @@ type Module interface {List(ctx context.Context, orgID valuer.UUID) ([]*dashboardtypes.Dashboard, error)- Update(ctx context.Context, orgID valuer.UUID, id valuer.UUID, updatedBy string, data dashboardtypes.UpdatableDashboard) (*dashboardtypes.Dashboard, error)+ Update(ctx context.Context, orgID valuer.UUID, id valuer.UUID, updatedBy string, data dashboardtypes.UpdatableDashboard, diff int) (*dashboardtypes.Dashboard, error)LockUnlock(ctx context.Context, orgID valuer.UUID, id valuer.UUID, updatedBy string, role types.Role, lock bool) errordiff --git a/pkg/modules/dashboard/impldashboard/handler.go b/pkg/modules/dashboard/impldashboard/handler.goindex 1406e90568..39a5987a40 100644--- a/pkg/modules/dashboard/impldashboard/handler.go+++ b/pkg/modules/dashboard/impldashboard/handler.go@@ -13,6 +13,7 @@ import ("github.com/SigNoz/signoz/pkg/querybuilder""github.com/SigNoz/signoz/pkg/transition""github.com/SigNoz/signoz/pkg/types/authtypes"+ "github.com/SigNoz/signoz/pkg/types/ctxtypes""github.com/SigNoz/signoz/pkg/types/dashboardtypes""github.com/SigNoz/signoz/pkg/valuer""github.com/gorilla/mux"@@ -106,7 +107,13 @@ func (handler *handler) Update(rw http.ResponseWriter, r *http.Request) {return}- dashboard, err := handler.module.Update(ctx, orgID, dashboardID, claims.Email, req)+ diff := 0+ // Allow multiple deletions for API key requests; enforce for others+ if authType, ok := ctxtypes.AuthTypeFromContext(ctx); ok && authType == ctxtypes.AuthTypeJWT {+ diff = 1+ }++ dashboard, err := handler.module.Update(ctx, orgID, dashboardID, claims.Email, req, diff)if err != nil {render.Error(rw, err)returndiff --git a/pkg/modules/dashboard/impldashboard/module.go b/pkg/modules/dashboard/impldashboard/module.goindex 1e918f9e8c..c4e39586d9 100644--- a/pkg/modules/dashboard/impldashboard/module.go+++ b/pkg/modules/dashboard/impldashboard/module.go@@ -76,13 +76,13 @@ func (module *module) List(ctx context.Context, orgID valuer.UUID) ([]*dashboardreturn dashboards, nil}-func (module *module) Update(ctx context.Context, orgID valuer.UUID, id valuer.UUID, updatedBy string, updatableDashboard dashboardtypes.UpdatableDashboard) (*dashboardtypes.Dashboard, error) {+func (module *module) Update(ctx context.Context, orgID valuer.UUID, id valuer.UUID, updatedBy string, updatableDashboard dashboardtypes.UpdatableDashboard, diff int) (*dashboardtypes.Dashboard, error) {dashboard, err := module.Get(ctx, orgID, id)if err != nil {return nil, err}- err = dashboard.Update(updatableDashboard, updatedBy)+ err = dashboard.Update(ctx, updatableDashboard, updatedBy, diff)if err != nil {return nil, err}diff --git a/pkg/types/ctxtypes/auth.go b/pkg/types/ctxtypes/auth.gonew file mode 100644index 0000000000..323f781ca8--- /dev/null+++ b/pkg/types/ctxtypes/auth.go@@ -0,0 +1,16 @@+package ctxtypes++import "context"++type authTypeKey struct{}++// SetAuthType stores the auth type (e.g., AuthTypeJWT, AuthTypeAPIKey, AuthTypeInternal) in context.+func SetAuthType(ctx context.Context, authType AuthType) context.Context {+ return context.WithValue(ctx, authTypeKey{}, authType)+}++// AuthTypeFromContext retrieves the auth type from context if set.+func AuthTypeFromContext(ctx context.Context) (AuthType, bool) {+ v, ok := ctx.Value(authTypeKey{}).(AuthType)+ return v, ok+}diff --git a/pkg/types/ctxtypes/constants.go b/pkg/types/ctxtypes/constants.gonew file mode 100644index 0000000000..3bcfb15c7c--- /dev/null+++ b/pkg/types/ctxtypes/constants.go@@ -0,0 +1,13 @@+package ctxtypes++import "github.com/SigNoz/signoz/pkg/valuer"++type AuthType struct {+ valuer.String+}++var (+ AuthTypeJWT = AuthType{valuer.NewString("jwt")}+ AuthTypeAPIKey = AuthType{valuer.NewString("api_key")}+ AuthTypeInternal = AuthType{valuer.NewString("internal")}+)diff --git a/pkg/types/dashboardtypes/dashboard.go b/pkg/types/dashboardtypes/dashboard.goindex 3b90c0ba8b..b6a5c20709 100644--- a/pkg/types/dashboardtypes/dashboard.go+++ b/pkg/types/dashboardtypes/dashboard.go@@ -231,7 +231,7 @@ func (storableDashboardData *StorableDashboardData) GetWidgetIds() []string {return widgetIds}-func (dashboard *Dashboard) CanUpdate(data StorableDashboardData) error {+func (dashboard *Dashboard) CanUpdate(ctx context.Context, data StorableDashboardData, diff int) error {if dashboard.Locked {return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "cannot update a locked dashboard, please unlock the dashboard to update")}@@ -251,15 +251,17 @@ func (dashboard *Dashboard) CanUpdate(data StorableDashboardData) error {differenceMap[id] = true}}- if len(difference) > 1 {++ // Allow multiple decisions only if diff == 0+ if diff > 0 && len(difference) > diff {return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "deleting more than one panel is not supported")}return nil}-func (dashboard *Dashboard) Update(updatableDashboard UpdatableDashboard, updatedBy string) error {- err := dashboard.CanUpdate(updatableDashboard)+func (dashboard *Dashboard) Update(ctx context.Context, updatableDashboard UpdatableDashboard, updatedBy string, diff int) error {+ err := dashboard.CanUpdate(ctx, updatableDashboard, diff)if err != nil {return err}diff --git a/pkg/types/dashboardtypes/dashboard_test.go b/pkg/types/dashboardtypes/dashboard_test.gonew file mode 100644index 0000000000..0a584cf383--- /dev/null+++ b/pkg/types/dashboardtypes/dashboard_test.go@@ -0,0 +1,83 @@+package dashboardtypes++import (+ "context"+ "testing"++ "github.com/SigNoz/signoz/pkg/valuer"+ "github.com/stretchr/testify/assert"+)++func makeTestWidgets(ids ...string) []interface{} {+ widgets := []interface{}{}+ for _, id := range ids {+ widgets = append(widgets, map[string]interface{}{+ "id": id,+ "query": map[string]interface{}{},+ })+ }+ return widgets+}++func TestCanUpdate_MultipleDeletions_ByDiff(t *testing.T) {+ testCases := []struct {+ name string+ diff int+ updated []string+ wantErr bool+ }{+ {+ name: "diff-0-allows-multi-delete",+ diff: 0,+ updated: []string{"a"}, // deleting 2 widgets (b, c)+ wantErr: false,+ },+ {+ name: "diff-1-blocks-multi-delete",+ diff: 1,+ updated: []string{"a"}, // deleting 2 widgets (b, c) > diff(1)+ wantErr: true,+ },+ {+ name: "diff-1-allows-single-delete",+ diff: 1,+ updated: []string{"a", "b"}, // deleting 1 widget (c) = diff(1)+ wantErr: false,+ },+ {+ name: "diff-2-allows-two-deletions",+ diff: 2,+ updated: []string{"a"}, // deleting 2 widgets (b, c) = diff(2)+ wantErr: false,+ },+ {+ name: "diff-1-blocks-three-deletions",+ diff: 1,+ updated: []string{}, // deleting all 3 widgets > diff(1)+ wantErr: true,+ },+ }++ for _, tc := range testCases {+ t.Run(tc.name, func(t *testing.T) {+ ctx := context.Background()++ orgID := valuer.GenerateUUID()+ initial := StorableDashboardData{+ "widgets": makeTestWidgets("a", "b", "c"),+ }+ d, err := NewDashboard(orgID, "tester", initial)+ assert.NoError(t, err)++ updated := StorableDashboardData{+ "widgets": makeTestWidgets(tc.updated...),+ }+ err = d.CanUpdate(ctx, updated, tc.diff)+ if tc.wantErr {+ assert.Error(t, err)+ } else {+ assert.NoError(t, err)+ }+ })+ }+}