SPB Git

Repository Public

cluster_misc.go 20.3 KB 517 lines go

1package api23import (4	"context"5	"encoding/json"6	"regexp"7	"slices"8	"sort"9	"strconv"10	"strings"11	"time"12	"unicode/utf8"1314	"github.com/google/uuid"15	"github.com/jackc/pgx/v5"1617	"clustr.spboucher.ai/mcc/internal/audit"18	"clustr.spboucher.ai/mcc/internal/authz"19	"clustr.spboucher.ai/mcc/internal/httpx"20)2122// ── Dashboards (per user) ──────────────────────────────────────────────────2324var clDashNameRe = regexp.MustCompile(`^[a-z0-9][a-z0-9_-]{0,39}$`)2526const (27	clDashDefault      = "default"28	clMaxWidgets       = 6029	clMaxDashboards    = 2030	clGridColumns      = 1231	clMaxGridRow       = 100032	clMaxWidgetConfig  = 4 << 1033	clMaxDashboardSize = 64 << 1034)3536// clBuiltinLayout is the home dashboard of a user who has not saved one.37func clBuiltinLayout() []DashboardWidget {38	return []DashboardWidget{39		{Id: "capacity", Type: "cluster.capacity", X: 0, Y: 0, W: 12, H: 2, Config: map[string]any{}},40		{Id: "cpu", Type: "metric.chart", X: 0, Y: 2, W: 6, H: 4, Config: map[string]any{"metric": "cpu.usage", "range": "24h"}},41		{Id: "memory", Type: "metric.chart", X: 6, Y: 2, W: 6, H: 4, Config: map[string]any{"metric": "mem.used", "range": "24h"}},42	}43}4445func clDashName(name string) error {46	if !clDashNameRe.MatchString(name) {47		return clInvalid("name", "must be 1 to 40 characters: lowercase letters, digits, underscore or hyphen")48	}49	return nil50}5152// clLayout validates a layout: bounded widget count, unique ids, every widget53// inside the 12-column grid, bounded configuration size. Widget types are54// free (clients render unknown ones as a placeholder).55func clLayout(in []DashboardWidget) ([]DashboardWidget, []byte, error) {56	if in == nil {57		return nil, nil, clInvalid("layout", "is required")58	}59	if len(in) > clMaxWidgets {60		return nil, nil, clInvalid("layout", "at most 60 widgets")61	}62	seen := map[string]bool{}63	out := make([]DashboardWidget, 0, len(in))64	for i, w := range in {65		at := "layout[" + strconv.Itoa(i) + "]"66		switch {67		case w.Id == "" || utf8.RuneCountInString(w.Id) > 40 || !clPrintable(w.Id):68			return nil, nil, clInvalid(at+".id", "must be 1 to 40 printable characters")69		case seen[w.Id]:70			return nil, nil, clInvalid(at+".id", "is used by another widget")71		case w.Type == "" || utf8.RuneCountInString(w.Type) > 60 || !clPrintable(w.Type):72			return nil, nil, clInvalid(at+".type", "must be 1 to 60 printable characters")73		case w.W < 1 || w.W > clGridColumns:74			return nil, nil, clInvalid(at+".w", "must be between 1 and 12")75		case w.H < 1 || w.H > 12:76			return nil, nil, clInvalid(at+".h", "must be between 1 and 12")77		case w.X < 0 || w.X+w.W > clGridColumns:78			return nil, nil, clInvalid(at+".x", "the widget must fit in the 12-column grid (x + w <= 12)")79		case w.Y < 0 || w.Y > clMaxGridRow:80			return nil, nil, clInvalid(at+".y", "must be between 0 and 1000")81		case w.Config == nil:82			return nil, nil, clInvalid(at+".config", "is required (use {} when empty)")83		}84		if b, err := json.Marshal(w.Config); err != nil || len(b) > clMaxWidgetConfig {85			return nil, nil, clInvalid(at+".config", "must be at most 4096 bytes of JSON")86		}87		seen[w.Id] = true88		out = append(out, w)89	}90	raw, err := json.Marshal(out)91	if err != nil || len(raw) > clMaxDashboardSize {92		return nil, nil, clInvalid("layout", "is too large")93	}94	return out, raw, nil95}9697func clDecodeLayout(raw []byte) []DashboardWidget {98	var l []DashboardWidget99	if json.Unmarshal(raw, &l) != nil || l == nil {100		return []DashboardWidget{}101	}102	for i := range l {103		if l[i].Config == nil {104			l[i].Config = map[string]any{}105		}106	}107	return l108}109110func (h *clusterHandlers) ListMyDashboards(ctx context.Context, _ ListMyDashboardsRequestObject) (ListMyDashboardsResponseObject, error) {111	u := principalFrom(ctx).Session.User112	rows, err := h.Pool.Query(ctx, `select name, layout, updated_at from user_dashboards where user_id = $1 order by name limit $2`, u.ID, clMaxDashboards+1)113	if err != nil {114		return nil, err115	}116	out := DashboardList{Items: []Dashboard{}}117	hasDefault := false118	var name string119	var layout []byte120	var at time.Time121	if _, err := pgx.ForEachRow(rows, []any{&name, &layout, &at}, func() error {122		hasDefault = hasDefault || name == clDashDefault123		out.Items = append(out.Items, Dashboard{Name: name, Layout: clDecodeLayout(layout), UpdatedAt: at})124		return nil125	}); err != nil {126		return nil, err127	}128	if !hasDefault {129		// Nothing saved under "default": the built-in layout applies. Its date is the account's creation.130		out.Items = append([]Dashboard{{Name: clDashDefault, Layout: clBuiltinLayout(), UpdatedAt: u.CreatedAt}}, out.Items...)131	}132	return ListMyDashboards200JSONResponse{Body: out}, nil133}134135func (h *clusterHandlers) PutMyDashboard(ctx context.Context, req PutMyDashboardRequestObject) (PutMyDashboardResponseObject, error) {136	if err := clDashName(req.Name); err != nil {137		return nil, err138	}139	if req.Body == nil {140		return nil, httpx.Validation("Body required.")141	}142	layout, raw, err := clLayout(req.Body.Layout)143	if err != nil {144		return nil, err145	}146	uid := principalFrom(ctx).Session.User.ID147	tx, err := h.Pool.Begin(ctx)148	if err != nil {149		return nil, err150	}151	defer tx.Rollback(ctx)152	// Serialize this user's dashboard writes so the count cannot be exceeded concurrently.153	if _, err := tx.Exec(ctx, `select pg_advisory_xact_lock(hashtextextended('mcc.dashboards:' || $1::text, 0))`, uid.String()); err != nil {154		return nil, err155	}156	var others int157	if err := tx.QueryRow(ctx, `select count(*) from user_dashboards where user_id = $1 and name <> $2`, uid, req.Name).Scan(&others); err != nil {158		return nil, err159	}160	if others >= clMaxDashboards {161		return nil, clInvalid("name", "at most 20 dashboards per user; delete one first")162	}163	var at time.Time164	if err := tx.QueryRow(ctx, `insert into user_dashboards(user_id, name, layout, updated_at) values ($1, $2, $3, now())165		on conflict (user_id, name) do update set layout = excluded.layout, updated_at = excluded.updated_at returning updated_at`,166		uid, req.Name, raw).Scan(&at); err != nil {167		return nil, err168	}169	if err := h.audit(ctx, tx, "dashboard.put", "dashboard", req.Name, audit.Success, "", map[string]any{"widgets": len(layout)}); err != nil {170		return nil, err171	}172	if err := tx.Commit(ctx); err != nil {173		return nil, err174	}175	return PutMyDashboard200JSONResponse{Body: Dashboard{Name: req.Name, Layout: layout, UpdatedAt: at}}, nil176}177178func (h *clusterHandlers) DeleteMyDashboard(ctx context.Context, req DeleteMyDashboardRequestObject) (DeleteMyDashboardResponseObject, error) {179	if err := clDashName(req.Name); err != nil {180		return nil, err181	}182	uid := principalFrom(ctx).Session.User.ID183	tx, err := h.Pool.Begin(ctx)184	if err != nil {185		return nil, err186	}187	defer tx.Rollback(ctx)188	tag, err := tx.Exec(ctx, `delete from user_dashboards where user_id = $1 and name = $2`, uid, req.Name)189	if err != nil {190		return nil, err191	}192	if tag.RowsAffected() == 0 {193		if req.Name == clDashDefault {194			return DeleteMyDashboard204Response{}, nil // already the built-in layout195		}196		return nil, httpx.ErrNotFound197	}198	if err := h.audit(ctx, tx, "dashboard.delete", "dashboard", req.Name, audit.Success, "", nil); err != nil {199		return nil, err200	}201	if err := tx.Commit(ctx); err != nil {202		return nil, err203	}204	return DeleteMyDashboard204Response{}, nil205}206207// ── Activity ───────────────────────────────────────────────────────────────208209// clActivityRule selects audit entries shown in the activity feed. The feed210// is a redacted projection of the audit log: actor name, a fixed summary211// derived from the action name, target and outcome; never the IP, device,212// session, parameters or free-text detail of the entry.213type clActivityRule struct {214	prefix string // audit action prefix215	exact  bool216	kind   ActivityKind217	perm   authz.Permission // needed to see items of this kind218}219220// Order matters: the first matching rule wins.221var clActivityRules = []clActivityRule{222	{"maintenance.note.", false, ActivityKindMaintenanceNote, authz.MaintenanceRead},223	{"maintenance_note.", false, ActivityKindMaintenanceNote, authz.MaintenanceRead},224	{"maintenance.", false, ActivityKindMaintenance, authz.MaintenanceRead},225	{"alert.ack", false, ActivityKindAlertAck, authz.AlertsRead},226	{"alert.resolve", false, ActivityKindAlertResolve, authz.AlertsRead},227	{"incident.", false, ActivityKindIncident, authz.AlertsRead},228	{"action.", false, ActivityKindAction, authz.ActionsRead},229	{"auth.login", true, ActivityKindLogin, authz.ClusterRead},230}231232func clActivityKind(action string) (clActivityRule, bool) {233	for _, r := range clActivityRules {234		if (r.exact && action == r.prefix) || (!r.exact && strings.HasPrefix(action, r.prefix)) {235			return r, true236		}237	}238	return clActivityRule{}, false239}240241var clActivityNoun = map[ActivityKind]string{242	ActivityKindAction: "Action", ActivityKindAlertAck: "Alert acknowledged", ActivityKindAlertResolve: "Alert resolved",243	ActivityKindIncident: "Incident", ActivityKindMaintenance: "Maintenance window", ActivityKindMaintenanceNote: "Maintenance note",244	ActivityKindLogin: "Signed in",245}246247// clActivitySummary is built from the action NAME only (a fixed vocabulary248// chosen by the server), e.g. "Action: approve", "Maintenance window: create".249func clActivitySummary(r clActivityRule, action string) string {250	noun := clActivityNoun[r.kind]251	if r.exact || r.kind == ActivityKindAlertAck || r.kind == ActivityKindAlertResolve {252		return noun253	}254	verb := strings.NewReplacer(".", " ", "_", " ").Replace(strings.TrimPrefix(action, r.prefix))255	if verb == "" {256		return noun257	}258	return noun + ": " + verb259}260261func (h *clusterHandlers) ListActivity(ctx context.Context, req ListActivityRequestObject) (ListActivityResponseObject, error) {262	p := req.Params263	limit, err := clLimit(p.Limit)264	if err != nil {265		return nil, err266	}267	if p.Kind != nil && !slices.ContainsFunc(clActivityRules, func(r clActivityRule) bool { return r.kind == *p.Kind }) {268		return nil, clInvalid("kind", "is not an activity kind")269	}270	acc, err := h.clAccessFor(ctx)271	if err != nil {272		return nil, err273	}274	cur, err := clDecodeCursor(p.Cursor, "activity", 1)275	if err != nil {276		return nil, err277	}278	// Only kinds the caller may read are even queried.279	var patterns []string280	for _, r := range clActivityRules {281		if (p.Kind != nil && r.kind != *p.Kind) || !acc.can(r.perm) {282			continue283		}284		pat := clLikeEscaper.Replace(r.prefix)285		if !r.exact {286			pat += "%"287		}288		patterns = append(patterns, pat)289	}290	out := ActivityList{Items: []ActivityItem{}}291	if len(patterns) == 0 {292		return ListActivity200JSONResponse{Body: out}, nil293	}294	b := &clSQL{}295	b.and(`l.action like any(` + b.arg(patterns) + `::text[])`)296	b.and(`(l.action <> 'auth.login' or (l.outcome = 'success' and l.actor_id is not null))`)297	if cur != nil {298		seq, err := strconv.ParseInt(cur[0], 10, 64)299		if err != nil || seq < 0 {300			return nil, errClCursor301		}302		b.and("l.seq < " + b.arg(seq))303	}304	if p.NodeId != nil {305		a := b.arg(p.NodeId.String())306		b.and("((l.target_kind = 'node' and l.target_id = " + a + ") or l.params->>'node_id' = " + a + ")")307	}308	rows, err := h.Pool.Query(ctx, `select l.seq, l.ts, l.action, l.actor_kind, coalesce(u.display_name, ''), l.target_kind, l.target_id,309			n.alias::text, n.site, n.tags, l.outcome::text310		from audit_log l left join users u on u.id = l.actor_id311			left join nodes n on l.target_kind = 'node' and n.id::text = l.target_id`+312		b.where()+` order by l.seq desc limit `+b.arg(limit+1), b.args...)313	if err != nil {314		return nil, err315	}316	type entry struct {317		seq  int64318		item ActivityItem319		hide bool320	}321	nodeFilter := acc.scoped(authz.NodesRead)322	list, err := pgx.CollectRows(rows, func(r pgx.CollectableRow) (entry, error) {323		var (324			e                       entry325			action, actorKind, name string326			outcome                 string327			alias, site             *string328			tags                    []string329		)330		if err := r.Scan(&e.seq, &e.item.Ts, &action, &actorKind, &name, &e.item.TargetKind, &e.item.TargetId, &alias, &site, &tags, &outcome); err != nil {331			return e, err332		}333		rule, ok := clActivityKind(action)334		if !ok || (p.Kind != nil && rule.kind != *p.Kind) || !acc.can(rule.perm) {335			e.hide = true // matched a broader LIKE pattern only336			return e, nil337		}338		e.item.Id, e.item.Kind, e.item.Summary = strconv.FormatInt(e.seq, 10), rule.kind, clActivitySummary(rule, action)339		e.item.Outcome = ptr(AuditOutcome(outcome))340		switch {341		case name != "":342			e.item.ActorDisplayName = &name343		case actorKind != "user":344			e.item.ActorDisplayName = &actorKind // "system", "cli", "agent"345		}346		if alias != nil {347			e.item.TargetLabel = alias348			if nodeFilter && e.item.TargetId != nil {349				if id, err := uuid.Parse(*e.item.TargetId); err == nil && !acc.can(authz.NodesRead, clNodeFacets(id, clDeref(site, ""), tags)...) {350					e.hide = true351				}352			}353		}354		if rule.kind == ActivityKindLogin {355			e.item.TargetKind, e.item.TargetId = nil, nil // a sign-in targets the account itself356		}357		return e, nil358	})359	if err != nil {360		return nil, err361	}362	if len(list) > limit {363		list = list[:limit]364		out.NextCursor = clEncodeCursor("activity", strconv.FormatInt(list[limit-1].seq, 10))365	}366	for _, e := range list {367		if !e.hide {368			out.Items = append(out.Items, e.item)369		}370	}371	return ListActivity200JSONResponse{Body: out}, nil372}373374// ── Search ─────────────────────────────────────────────────────────────────375376// clSearchSpec describes how one kind of resource is searched. Every query377// takes the same parameters: $1 the text, $2 a prefix pattern, $3 a substring378// pattern (wildcards typed by the user are escaped), $4 the row limit; and379// returns: id, title, subtitle, node_id, state, score, then the node's site,380// tags and last contact when the result belongs to a node.381type clSearchSpec struct {382	kind SearchResultKind383	perm authz.Permission384	sql  string385}386387// clScore ranks a match on the main field: exact, prefix, substring; a match388// found only through a secondary field scores lowest.389func clScore(field string) string {390	return `case when lower(` + field + `) = lower($1) then 1.0 when ` + field + ` ilike $2 escape '\' then 0.8 when ` + field +391		` ilike $3 escape '\' then 0.6 else 0.3 end`392}393394func clAny(fields ...string) string {395	parts := make([]string, len(fields))396	for i, f := range fields {397		parts[i] = f + ` ilike $3 escape '\'`398	}399	return "(" + strings.Join(parts, " or ") + ")"400}401402const clNoNode = `null::text, null::text[], null::timestamptz`403404var clSearchSpecs = []clSearchSpec{405	{SearchResultKindNode, authz.NodesRead, `select n.id, n.alias::text, nullif(concat_ws(' · ', nullif(n.model, ''), n.site, n.role), ''), n.id, null::text,406			` + clScore("n.alias::text") + `, n.site, n.tags, greatest(s.received_at, n.last_seen_at)407		from nodes n left join node_state s on s.node_id = n.id408		where n.retired_at is null and (` + clAny("n.alias::text", "n.hostname", "coalesce(n.model,'')", "coalesce(n.chip,'')", "n.site", "n.role") + `409			or exists (select 1 from unnest(n.tags) t where t ilike $3 escape '\'))410		order by 6 desc, lower(n.alias::text) limit $4`},411	{SearchResultKindService, authz.ServicesRead, `select sv.id, sv.name, sv.manager::text || ' on ' || n.alias::text, sv.node_id,412			case when sv.gone_at is null then sv.state else 'gone' end, ` + clScore("sv.name") + `, n.site, n.tags, greatest(s.received_at, n.last_seen_at)413		from services sv join nodes n on n.id = sv.node_id left join node_state s on s.node_id = n.id414		where n.retired_at is null and ` + clAny("sv.name", "coalesce(sv.app,'')") + `415		order by 6 desc, (sv.gone_at is not null), lower(sv.name), sv.id limit $4`},416	{SearchResultKindAlert, authz.AlertsRead, `select a.id, a.summary, a.severity::text, a.node_id, a.state::text, ` + clScore("a.summary") + `, ` + clNoNode + `417		from alerts a where ` + clAny("a.summary") + ` order by 6 desc, (a.state = 'resolved'), a.started_at desc limit $4`},418	{SearchResultKindAction, authz.ActionsRead, `select a.id, a.type, n.alias::text, a.node_id, a.state::text, ` + clScore("a.type") + `, ` + clNoNode + `419		from actions a left join nodes n on n.id = a.node_id where ` + clAny("a.type", "coalesce(n.alias::text,'')") + `420		order by 6 desc, a.created_at desc limit $4`},421	{SearchResultKindIncident, authz.AlertsRead, `select i.id, i.title, i.severity::text, null::uuid, i.state, ` + clScore("i.title") + `, ` + clNoNode + `422		from incidents i where ` + clAny("i.title", "i.summary") + ` order by 6 desc, i.opened_at desc limit $4`},423	{SearchResultKindJob, authz.JobsRead, `select j.id, j.template, n.alias::text, j.node_id, j.state::text, ` + clScore("j.template") + `, ` + clNoNode + `424		from jobs j left join nodes n on n.id = j.node_id where ` + clAny("j.template", "coalesce(n.alias::text,'')") + `425		order by 6 desc, j.created_at desc limit $4`},426	{SearchResultKindInferenceServer, authz.ModelsRead, `select i.id, coalesce(nullif(i.model, ''), i.kind), i.kind || ' on ' || n.alias::text || ':' || i.port,427			i.node_id, case when i.gone_at is null then i.health else 'gone' end, ` + clScore("coalesce(i.model,'')") + `, n.site, n.tags,428			greatest(s.received_at, n.last_seen_at)429		from inference_servers i join nodes n on n.id = i.node_id left join node_state s on s.node_id = n.id430		where n.retired_at is null and ` + clAny("coalesce(i.model,'')", "i.kind", "n.alias::text") + ` order by 6 desc, lower(n.alias::text), i.port limit $4`},431	{SearchResultKindMaintenanceWindow, authz.MaintenanceRead, `select w.id, w.title, w.category, null::uuid, w.status, ` + clScore("w.title") + `, ` + clNoNode + `432		from maintenance_windows w where ` + clAny("w.title", "w.description") + ` order by 6 desc, w.starts_at desc limit $4`},433	{SearchResultKindMaintenanceNote, authz.MaintenanceRead, `select m.id, left(m.body, 80), null::text, m.node_id, null::text, ` + clScore("m.body") + `, ` + clNoNode + `434		from maintenance_notes m where m.deleted_at is null and ` + clAny("m.body") + ` order by 6 desc, m.created_at desc limit $4`},435}436437func (h *clusterHandlers) Search(ctx context.Context, req SearchRequestObject) (SearchResponseObject, error) {438	p := req.Params439	q := strings.TrimSpace(p.Q)440	if q == "" || utf8.RuneCountInString(q) > 120 || !clPrintable(q) {441		return nil, clInvalid("q", "must be 1 to 120 printable characters")442	}443	limit := 20444	if p.Limit != nil {445		if *p.Limit < 1 || *p.Limit > 50 {446			return nil, clInvalid("limit", "must be between 1 and 50")447		}448		limit = *p.Limit449	}450	var kinds []SearchResultKind451	if p.Kinds != nil {452		for _, k := range *p.Kinds {453			if !slices.ContainsFunc(clSearchSpecs, func(s clSearchSpec) bool { return s.kind == k }) {454				return nil, clInvalid("kinds", "unknown kind")455			}456			kinds = append(kinds, k)457		}458	}459	acc, err := h.clAccessFor(ctx)460	if err != nil {461		return nil, err462	}463	now := time.Now().UTC()464	escaped := clLikeEscaper.Replace(q)465	out := SearchResults{Query: q, Items: []SearchResult{}}466	order := map[SearchResultKind]int{}467	for i, spec := range clSearchSpecs {468		order[spec.kind] = i469		// A kind the caller cannot read is not searched at all.470		if (kinds != nil && !slices.Contains(kinds, spec.kind)) || !acc.can(spec.perm) {471			continue472		}473		rows, err := h.Pool.Query(ctx, spec.sql, q, escaped+"%", "%"+escaped+"%", limit)474		if err != nil {475			return nil, err476		}477		var (478			r        SearchResult479			score    float64480			site     *string481			tags     []string482			seen     *time.Time483			subtitle *string484		)485		_, err = pgx.ForEachRow(rows, []any{&r.Id, &r.Title, &subtitle, &r.NodeId, &r.State, &score, &site, &tags, &seen}, func() error {486			item := SearchResult{Kind: spec.kind, Id: r.Id, Title: r.Title, Subtitle: clNonEmpty(subtitle), NodeId: r.NodeId, State: clNonEmpty(r.State), Score: float32(score)}487			if site != nil && item.NodeId != nil {488				// Results that belong to a node obey the grants scoped to that node.489				facets := clNodeFacets(*item.NodeId, *site, tags)490				if spec.kind == SearchResultKindService {491					facets = clServiceFacets(item.Id, *item.NodeId, *site, tags)492				}493				if !acc.can(spec.perm, facets...) {494					return nil495				}496				item.Freshness = ptr(h.clFresh(now, seen))497			}498			out.Items = append(out.Items, item)499			return nil500		})501		if err != nil {502			return nil, err503		}504	}505	sort.SliceStable(out.Items, func(i, j int) bool {506		a, b := out.Items[i], out.Items[j]507		if a.Score != b.Score {508			return a.Score > b.Score509		}510		return order[a.Kind] < order[b.Kind]511	})512	if len(out.Items) > limit {513		out.Items = out.Items[:limit]514	}515	return Search200JSONResponse{Body: out}, nil516}517
Files main
Menu

Theme