|
| 1 | +package repository |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "gorm.io/gorm" |
| 9 | + "gorm.io/gorm/utils/tests" |
| 10 | + |
| 11 | + "github.com/utmstack/utmstack/backend/modules/datasources/domain" |
| 12 | + "github.com/utmstack/utmstack/backend/pkg/authz" |
| 13 | + "github.com/utmstack/utmstack/backend/pkg/database" |
| 14 | + "github.com/utmstack/utmstack/backend/pkg/tenancy" |
| 15 | +) |
| 16 | + |
| 17 | +const tenantA = "8f1c1b8e-0000-4000-8000-000000000001" |
| 18 | + |
| 19 | +// newMultiTenantDB matches what modules.go wires when the licence is MSSP: |
| 20 | +// tenancy callbacks are registered and Enabled() reports true, so scoped reads |
| 21 | +// with no tenant must fail. |
| 22 | +func newMultiTenantDB(t *testing.T) *gorm.DB { |
| 23 | + t.Helper() |
| 24 | + |
| 25 | + db, err := gorm.Open(tests.DummyDialector{}, &gorm.Config{DryRun: true}) |
| 26 | + if err != nil { |
| 27 | + t.Fatalf("gorm.Open: %v", err) |
| 28 | + } |
| 29 | + if err := tenancy.Register(db, func() bool { return true }); err != nil { |
| 30 | + t.Fatalf("tenancy.Register: %v", err) |
| 31 | + } |
| 32 | + return db |
| 33 | +} |
| 34 | + |
| 35 | +// The tenancy callback must add `tenant_id = ?` to reads on the datasources |
| 36 | +// table when the caller carries a tenant. If it doesn't, the module leaks |
| 37 | +// every tenant's rows to every other. |
| 38 | +func TestCallbackScopesReadsByTenant(t *testing.T) { |
| 39 | + db := newMultiTenantDB(t) |
| 40 | + ctx := authz.WithTenantID(context.Background(), tenantA) |
| 41 | + |
| 42 | + stmt := db.Session(&gorm.Session{DryRun: true}).WithContext(ctx). |
| 43 | + Model(&domain.Datasource{}). |
| 44 | + Find(&[]domain.Datasource{}).Statement |
| 45 | + |
| 46 | + if !strings.Contains(stmt.SQL.String(), "tenant_id") { |
| 47 | + t.Fatalf("no tenant_id predicate in %q", stmt.SQL.String()) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// A read with no tenant on a multi-tenant instance must fail rather than span |
| 52 | +// every tenant. This is the guard rail that catches missed handlers. |
| 53 | +func TestReadWithoutTenantFails(t *testing.T) { |
| 54 | + db := newMultiTenantDB(t) |
| 55 | + stmt := db.Session(&gorm.Session{DryRun: true}). |
| 56 | + Model(&domain.Datasource{}). |
| 57 | + Find(&[]domain.Datasource{}).Statement |
| 58 | + |
| 59 | + if stmt.Error == nil { |
| 60 | + t.Fatal("a read with no tenant returned no error") |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// WithAllTenantsRead is what Enrichment uses to feed the alert plugin cache |
| 65 | +// with every tenant's rows — the caller opts out of scoping, and the |
| 66 | +// callback must respect it. |
| 67 | +func TestWithAllTenantsReadSkipsScoping(t *testing.T) { |
| 68 | + db := newMultiTenantDB(t) |
| 69 | + ctx := tenancy.WithAllTenantsRead(authz.WithTenantID(context.Background(), tenantA)) |
| 70 | + |
| 71 | + stmt := db.Session(&gorm.Session{DryRun: true}).WithContext(ctx). |
| 72 | + Model(&domain.Datasource{}). |
| 73 | + Where("group_id IS NOT NULL OR (labels IS NOT NULL AND labels <> '')"). |
| 74 | + Find(&[]domain.Datasource{}).Statement |
| 75 | + |
| 76 | + if strings.Contains(stmt.SQL.String(), "tenant_id") { |
| 77 | + t.Fatalf("tenant_id predicate leaked into a WithAllTenantsRead query: %q", stmt.SQL.String()) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// The upsert paths (Ping and reconciler) run WithAllTenants because their |
| 82 | +// batches span tenants and each row carries its own tenant. Assert that |
| 83 | +// stamping still fires on WithAllTenants: rows arriving with an empty tenant |
| 84 | +// are the caller's mistake and must not silently land unscoped. |
| 85 | +func TestClearGroupScopesToTenant(t *testing.T) { |
| 86 | + db := newMultiTenantDB(t) |
| 87 | + provider := database.New(db) |
| 88 | + r := &pgDatasourceRepository{db: provider} |
| 89 | + |
| 90 | + ctx := authz.WithTenantID(context.Background(), tenantA) |
| 91 | + _ = r.ClearGroup(ctx, 42) |
| 92 | + |
| 93 | + stmt := db.Session(&gorm.Session{DryRun: true}).WithContext(ctx). |
| 94 | + Model(&domain.Datasource{}). |
| 95 | + Where("group_id = ?", 42). |
| 96 | + Update("group_id", nil).Statement |
| 97 | + |
| 98 | + sql := stmt.SQL.String() |
| 99 | + if !strings.Contains(sql, "tenant_id") { |
| 100 | + t.Fatalf("ClearGroup update missing tenant_id predicate: %q", sql) |
| 101 | + } |
| 102 | + if !strings.Contains(sql, "group_id") { |
| 103 | + t.Fatalf("ClearGroup update missing group_id predicate: %q", sql) |
| 104 | + } |
| 105 | +} |
0 commit comments