Staff roles (viewer, operator, billing) and the console's Audit view
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
f8fcecd640
commit
b60cf45adf
19 files changed
+352
-24
No files matched your search
@@ -649,3 +649,17 @@ func TestGuardrailsSayWhereTheyApplyFrom(t *testing.T) {
|
||||
t.Error("no Guardrails… in the menu")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheAuditViewListsChangesAndOpensOne(t *testing.T) {
|
||||
m, _ := consoleWith(t, cAudit)
|
||||
m.width, m.height = 160, 40
|
||||
m.data.audit = []store.AuditEvent{{At: time.Now(), Actor: "ilker", Source: "api",
|
||||
Action: "POST /api/v1/models", Target: "qwen", Status: 200, Detail: `{"name":"qwen"}`}}
|
||||
if v := m.View(); !strings.Contains(v, "POST /api/v1/models") || !strings.Contains(v, "ilker") {
|
||||
t.Fatalf("not listed:\n%s", v)
|
||||
}
|
||||
tm := press(m, "enter")
|
||||
if v := tm.View(); !strings.Contains(v, `"name":"qwen"`) {
|
||||
t.Errorf("not opened:\n%s", v)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
||||
)
|
||||
|
||||
// The audit view: who changed what, newest first — the same as `llmgw ctl
|
||||
// audit`.
|
||||
|
||||
func (m consoleModel) auditPanel(pw int) (string, string, string) {
|
||||
keys := "→ or Enter open · ↑↓ choose · Q leave"
|
||||
if m.inPanel {
|
||||
keys = "↑↓ row · Enter what it was given · ← back · Q leave"
|
||||
}
|
||||
head := "\n " + sDim.Render("Every change through ctl, this console and the /admin pages, the last week.") + "\n\n"
|
||||
var rows [][]string
|
||||
for _, e := range m.data.audit {
|
||||
status := strconv.Itoa(e.Status)
|
||||
if e.Status >= 400 {
|
||||
status = sBad.Render(status)
|
||||
}
|
||||
rows = append(rows, []string{ageShort(e.At), e.Actor, e.Source, e.Action, dash(e.Target), status})
|
||||
}
|
||||
if len(rows) == 0 {
|
||||
return "Audit", head + " " + sDim.Render("Nothing changed in the last week, or this control plane has no store."), keys
|
||||
}
|
||||
return "Audit", head + table([]string{"WHEN", "WHO", "VIA", "WHAT", "TARGET", "STATUS"}, rows, m.row, pw, m.inPanel), keys
|
||||
}
|
||||
|
||||
// auditDialog is one change in full.
|
||||
func auditDialog(e store.AuditEvent) (string, string) {
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "%s, by %s through %s\nstatus %d\n", e.At.Format(time.DateTime), e.Actor, e.Source, e.Status)
|
||||
if e.Target != "" {
|
||||
fmt.Fprintf(&b, "target %s\n", e.Target)
|
||||
}
|
||||
if e.Detail != "" && e.Detail != "{}" {
|
||||
fmt.Fprintf(&b, "\n%s\n", wrap(e.Detail, 80, ""))
|
||||
}
|
||||
b.WriteString("\n" + sDim.Render("any key"))
|
||||
return e.Action, b.String()
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/docgen"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/hf"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/settings"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
||||
)
|
||||
|
||||
// The console: what a bare `sudo llmgw` opens on a control plane. Down the
|
||||
@@ -46,6 +47,7 @@ const (
|
||||
cAccounts
|
||||
cKeys
|
||||
cOrgs
|
||||
cAudit
|
||||
cManual
|
||||
cSettings
|
||||
cAddNode
|
||||
@@ -58,7 +60,7 @@ const (
|
||||
cCount
|
||||
)
|
||||
|
||||
var consoleLabels = [cCount]string{"Overview", "Monitor", "Nodes", "GPUs", "Models", "Replicas", "Usage", "Quotas", "Mirror", "Jobs", "Logs", "Accounts", "API keys", "Organizations", "Manual",
|
||||
var consoleLabels = [cCount]string{"Overview", "Monitor", "Nodes", "GPUs", "Models", "Replicas", "Usage", "Quotas", "Mirror", "Jobs", "Logs", "Accounts", "API keys", "Organizations", "Audit", "Manual",
|
||||
"server.yaml", "Add a node", "Replicate the store", "Serve a model", "Upgrade the cluster",
|
||||
"Restart all replicas", "Back up now", "Leave"}
|
||||
|
||||
@@ -75,6 +77,7 @@ type consoleData struct {
|
||||
models []adminapi.ModelView
|
||||
replicas []adminapi.ReplicaView
|
||||
jobs []modelserve.Job
|
||||
audit []store.AuditEvent
|
||||
err error
|
||||
}
|
||||
|
||||
@@ -228,6 +231,7 @@ func (m consoleModel) fetch() tea.Cmd {
|
||||
_ = c.get(ctx, "/api/v1/models", &d.models)
|
||||
_ = c.get(ctx, "/api/v1/replicas", &d.replicas)
|
||||
_ = c.get(ctx, "/api/v1/jobs", &d.jobs)
|
||||
_ = c.get(ctx, "/api/v1/audit?since=7d&limit=300", &d.audit)
|
||||
return consoleDataMsg(d)
|
||||
}
|
||||
}
|
||||
@@ -474,6 +478,9 @@ func (m consoleModel) key(k tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
case cJobs:
|
||||
m.dialog = "error"
|
||||
m.dialogTitle, m.dialogText = jobDialog(m.data.jobs[m.row])
|
||||
case cAudit:
|
||||
m.dialog = "error"
|
||||
m.dialogTitle, m.dialogText = auditDialog(m.data.audit[m.row])
|
||||
case cServe:
|
||||
return m, m.fetchRepo(m.hf.models[m.row].ID)
|
||||
case cAccounts:
|
||||
@@ -873,6 +880,8 @@ func (m consoleModel) rowCount() int {
|
||||
return len(m.mirrorGroups())
|
||||
case cJobs:
|
||||
return len(m.data.jobs)
|
||||
case cAudit:
|
||||
return len(m.data.audit)
|
||||
case cServe:
|
||||
return len(m.hf.models)
|
||||
case cAccounts:
|
||||
@@ -1108,6 +1117,8 @@ func (m consoleModel) panel() (title, body, keys string) {
|
||||
return m.mirrorPanel(pw)
|
||||
case cJobs:
|
||||
return m.jobsPanel(pw)
|
||||
case cAudit:
|
||||
return m.auditPanel(pw)
|
||||
case cMonitor:
|
||||
return m.monitorPanel(pw)
|
||||
case cServe:
|
||||
|
||||
+28
-10
@@ -204,11 +204,10 @@ func (m consoleModel) userMenu(u store.User) *menuDialog {
|
||||
return nil
|
||||
}})
|
||||
}
|
||||
if u.IsAdmin {
|
||||
items = append(items, cli("Take away the cluster dashboard", "users", "demote", u.ID))
|
||||
} else {
|
||||
items = append(items, cli("Give the cluster dashboard (/admin)", "users", "promote", u.ID))
|
||||
}
|
||||
items = append(items, menuItem{"Staff role (" + or(u.StaffRole(), "none") + ")…", func(m *consoleModel) tea.Cmd {
|
||||
m.menuD = staffRoleMenu(u)
|
||||
return nil
|
||||
}})
|
||||
items = append(items,
|
||||
menuItem{"Add to an organization, team or project…", func(m *consoleModel) tea.Cmd {
|
||||
m.openForm(&formDialog{title: "Membership for " + who,
|
||||
@@ -264,6 +263,28 @@ func (m consoleModel) userMenu(u store.User) *menuDialog {
|
||||
return &menuDialog{title: who, items: items}
|
||||
}
|
||||
|
||||
// staffRoleMenu sets what a person may do on the pages under /admin.
|
||||
func staffRoleMenu(u store.User) *menuDialog {
|
||||
about := map[string]string{
|
||||
store.RoleViewer: "looks at every administration page, changes nothing",
|
||||
store.RoleOperator: "also runs the cluster: models, the mirror, nodes",
|
||||
store.RoleBilling: "sees usage and quotas",
|
||||
store.RoleAdmin: "may do anything",
|
||||
"none": "no part in running the gateway",
|
||||
}
|
||||
var items []menuItem
|
||||
for _, role := range append(slices.Clone(store.StaffRoles), "none") {
|
||||
label := role + " — " + about[role]
|
||||
if role == or(u.StaffRole(), "none") {
|
||||
label = "✓ " + label
|
||||
}
|
||||
items = append(items, menuItem{label, func(m *consoleModel) tea.Cmd {
|
||||
return m.runCLI("Staff role for "+u.Email, "users", "role", u.ID, role)
|
||||
}})
|
||||
}
|
||||
return &menuDialog{title: "Staff role · " + u.Email, items: items}
|
||||
}
|
||||
|
||||
func (m *consoleModel) createUserForm() {
|
||||
m.openForm(&formDialog{title: "New account",
|
||||
note: "Created already approved. The password should be changed by its owner.",
|
||||
@@ -565,16 +586,13 @@ func (m consoleModel) peoplePanel(pw int) (string, string, string) {
|
||||
var rows [][]string
|
||||
switch m.menu {
|
||||
case cAccounts:
|
||||
head = []string{"EMAIL", "STATE", "ADMIN", "MEMBER OF", "SIGNED UP", "LAST LOGIN"}
|
||||
head = []string{"EMAIL", "STATE", "STAFF", "MEMBER OF", "SIGNED UP", "LAST LOGIN"}
|
||||
for _, u := range m.people.users {
|
||||
state := string(u.State)
|
||||
if u.State == store.UserPending {
|
||||
state = sWarn.Render(state)
|
||||
}
|
||||
admin := ""
|
||||
if u.IsAdmin {
|
||||
admin = "yes"
|
||||
}
|
||||
admin := u.StaffRole()
|
||||
where := strings.Join(m.people.where[u.ID], "; ")
|
||||
if where == "" {
|
||||
where = sDim.Render("on their own")
|
||||
|
||||
+38
-1
@@ -37,7 +37,8 @@ func newUsersCmd() *cobra.Command {
|
||||
newUserCreateCmd(open), newUserResetPasswordCmd(open),
|
||||
newUserJoinCmd(open), newUserLeaveCmd(open), newUserDeleteCmd(open),
|
||||
newUserAdminCmd(open, "promote", true, "Let an account see the cluster dashboard at /admin"),
|
||||
newUserAdminCmd(open, "demote", false, "Take away access to the cluster dashboard"))
|
||||
newUserAdminCmd(open, "demote", false, "Take away access to the cluster dashboard"),
|
||||
newUserRoleCmd(open))
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -177,6 +178,42 @@ func newUserStateCmd(open userOpener, verb string, state store.UserState, short
|
||||
}
|
||||
}
|
||||
|
||||
func newUserRoleCmd(open userOpener) *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "role <user> <viewer|operator|billing|admin|none>",
|
||||
Short: "Give an account a part in running the gateway, from the pages under /admin",
|
||||
Long: "viewer looks at every administration page and changes nothing. operator\n" +
|
||||
"also runs the cluster from them: models through the mirror, a node's\n" +
|
||||
"engines. billing sees usage and quotas. admin may do anything. none takes\n" +
|
||||
"the role away. Every change made from those pages is in `llmgw ctl audit`.",
|
||||
Args: cobra.ExactArgs(2),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
role := args[1]
|
||||
if role == "none" {
|
||||
role = ""
|
||||
}
|
||||
s, err := open(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer s.Close()
|
||||
u, err := resolveUser(cmd.Context(), s, args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.SetStaffRole(cmd.Context(), u.ID, role); err != nil {
|
||||
return err
|
||||
}
|
||||
if role == "" {
|
||||
fmt.Fprintf(cmd.OutOrStdout(), "%s has no part in running the gateway now\n", u.Email)
|
||||
} else {
|
||||
fmt.Fprintf(cmd.OutOrStdout(), "%s is now %s\n", u.Email, role)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newUserAdminCmd(open userOpener, verb string, admin bool, short string) *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: verb + " <user>",
|
||||
|
||||
Reference in new issue
Block a user