Skip to content

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.

fix: backfill notification channel tuples (#12960)

Naman Verma362d3a4fdfb0
diff --git a/pkg/signoz/provider.go b/pkg/signoz/provider.go
index 43fe0d0080..6c75945cf5 100644
--- a/pkg/signoz/provider.go
+++ b/pkg/signoz/provider.go
@@ -255,6 +255,7 @@ func NewSQLMigrationProviderFactories(
sqlmigration.NewNormalizeQuickFilterFieldsFactory(sqlstore),
sqlmigration.NewAddSpanMapperOriginFactory(sqlstore, sqlschema),
sqlmigration.NewAddCloudIntegrationTuplesFactory(sqlstore),
+ sqlmigration.NewAddNotificationChannelTuplesFactory(sqlstore),
)
}
diff --git a/pkg/sqlmigration/129_add_notification_channel_tuples.go b/pkg/sqlmigration/129_add_notification_channel_tuples.go
new file mode 100644
index 0000000000..487a708491
--- /dev/null
+++ b/pkg/sqlmigration/129_add_notification_channel_tuples.go
@@ -0,0 +1,140 @@
+package sqlmigration
+
+import (
+ "context"
+ "database/sql"
+ "time"
+
+ "github.com/SigNoz/signoz/pkg/factory"
+ "github.com/SigNoz/signoz/pkg/sqlstore"
+ "github.com/SigNoz/signoz/pkg/types/authtypes"
+ "github.com/oklog/ulid/v2"
+ "github.com/uptrace/bun"
+ "github.com/uptrace/bun/dialect"
+ "github.com/uptrace/bun/migrate"
+)
+
+type addNotificationChannelTuples struct {
+ sqlstore sqlstore.SQLStore
+}
+
+func NewAddNotificationChannelTuplesFactory(sqlstore sqlstore.SQLStore) factory.ProviderFactory[SQLMigration, Config] {
+ return factory.NewProviderFactory(factory.MustNewName("add_notification_channel_tuples"), func(ctx context.Context, ps factory.ProviderSettings, c Config) (SQLMigration, error) {
+ return &addNotificationChannelTuples{sqlstore: sqlstore}, nil
+ })
+}
+
+func (migration *addNotificationChannelTuples) Register(migrations *migrate.Migrations) error {
+ return migrations.Register(migration.Up, migration.Down)
+}
+
+func (migration *addNotificationChannelTuples) Up(ctx context.Context, db *bun.DB) error {
+ tx, err := db.BeginTx(ctx, nil)
+ if err != nil {
+ return err
+ }
+ defer func() { _ = tx.Rollback() }()
+
+ var storeID string
+ err = tx.QueryRowContext(ctx, `SELECT id FROM store WHERE name = ? LIMIT 1`, "signoz").Scan(&storeID)
+ if err != nil {
+ return err
+ }
+
+ var orgIDs []string
+ err = tx.NewSelect().
+ Table("organizations").
+ Column("id").
+ Scan(ctx, &orgIDs)
+ if err != nil && err != sql.ErrNoRows {
+ return err
+ }
+
+ isPG := migration.sqlstore.BunDB().Dialect().Name() == dialect.PG
+
+ // notification-channel moved from legacy role gates to CheckResources. Existing
+ // organizations need the same tuples that new organizations receive from the
+ // managed-role registry at bootstrap.
+ tuples := []migrationTuple{
+ {authtypes.SigNozAdminRoleName, "metaresource", "notification-channel", "create"},
+ {authtypes.SigNozAdminRoleName, "metaresource", "notification-channel", "read"},
+ {authtypes.SigNozAdminRoleName, "metaresource", "notification-channel", "update"},
+ {authtypes.SigNozAdminRoleName, "metaresource", "notification-channel", "delete"},
+ {authtypes.SigNozAdminRoleName, "metaresource", "notification-channel", "list"},
+ {authtypes.SigNozEditorRoleName, "metaresource", "notification-channel", "read"},
+ {authtypes.SigNozEditorRoleName, "metaresource", "notification-channel", "list"},
+ {authtypes.SigNozViewerRoleName, "metaresource", "notification-channel", "read"},
+ {authtypes.SigNozViewerRoleName, "metaresource", "notification-channel", "list"},
+ }
+
+ for _, orgID := range orgIDs {
+ for _, tuple := range tuples {
+ entropy := ulid.DefaultEntropy()
+ now := time.Now().UTC()
+ tupleID := ulid.MustNew(ulid.Timestamp(now), entropy).String()
+
+ objectID := "organization/" + orgID + "/" + tuple.objectName + "/*"
+ roleSubject := "organization/" + orgID + "/role/" + tuple.roleName
+
+ if isPG {
+ user := "role:" + roleSubject + "#assignee"
+ result, err := tx.ExecContext(ctx, `
+ INSERT INTO tuple (store, object_type, object_id, relation, _user, user_type, ulid, inserted_at)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
+ ON CONFLICT (store, object_type, object_id, relation, _user) DO NOTHING`,
+ storeID, tuple.objectType, objectID, tuple.relation, user, "userset", tupleID, now,
+ )
+ if err != nil {
+ return err
+ }
+ rowsAffected, err := result.RowsAffected()
+ if err != nil {
+ return err
+ }
+ if rowsAffected == 0 {
+ continue
+ }
+ _, err = tx.ExecContext(ctx, `
+ INSERT INTO changelog (store, object_type, object_id, relation, _user, operation, ulid, inserted_at)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
+ ON CONFLICT (store, ulid, object_type) DO NOTHING`,
+ storeID, tuple.objectType, objectID, tuple.relation, user, 0, tupleID, now,
+ )
+ if err != nil {
+ return err
+ }
+ } else {
+ result, err := tx.ExecContext(ctx, `
+ INSERT INTO tuple (store, object_type, object_id, relation, user_object_type, user_object_id, user_relation, user_type, ulid, inserted_at)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
+ ON CONFLICT (store, object_type, object_id, relation, user_object_type, user_object_id, user_relation) DO NOTHING`,
+ storeID, tuple.objectType, objectID, tuple.relation, "role", roleSubject, "assignee", "userset", tupleID, now,
+ )
+ if err != nil {
+ return err
+ }
+ rowsAffected, err := result.RowsAffected()
+ if err != nil {
+ return err
+ }
+ if rowsAffected == 0 {
+ continue
+ }
+ _, err = tx.ExecContext(ctx, `
+ INSERT INTO changelog (store, object_type, object_id, relation, user_object_type, user_object_id, user_relation, operation, ulid, inserted_at)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
+ ON CONFLICT (store, ulid, object_type) DO NOTHING`,
+ storeID, tuple.objectType, objectID, tuple.relation, "role", roleSubject, "assignee", 0, tupleID, now,
+ )
+ if err != nil {
+ return err
+ }
+ }
+ }
+ }
+ return tx.Commit()
+}
+
+func (migration *addNotificationChannelTuples) Down(context.Context, *bun.DB) error {
+ return nil
+}
diff --git a/pkg/types/coretypes/registry_resource.go b/pkg/types/coretypes/registry_resource.go
index 4c8c51e8b3..cac972a772 100644
--- a/pkg/types/coretypes/registry_resource.go
+++ b/pkg/types/coretypes/registry_resource.go
@@ -47,7 +47,7 @@ var (
ResourceRole = NewResourceRole()
ResourceServiceAccount = NewResourceServiceAccount()
ResourceUser = NewResourceUser()
- ResourceMetaResourceNotificationChannel = NewResourceMetaResource(KindNotificationChannel)
+ ResourceMetaResourceNotificationChannel = NewResourceMetaResource(KindNotificationChannel, VerbCreate, VerbList, VerbRead, VerbUpdate, VerbDelete)
ResourceMetaResourceRoutePolicy = NewResourceMetaResource(KindRoutePolicy)
ResourceMetaResourceApdexSetting = NewResourceMetaResource(KindApdexSetting)
ResourceMetaResourceAuthDomain = NewResourceMetaResource(KindAuthDomain)