Feature: let the web console move a host to another group
The Go server now keeps a per-device group override (device_groups.json) edited from the card's "..." menu below Edit remark. The override wins over the group the host reported (or the offline history record) in the device list, the group filter, get_groups and the MCP host tools, so offline records can be regrouped and the change survives restarts. An empty value clears the override; names are capped at 23 characters, matching the Win32 regroup dialog. The command is admin-only and honours the device ACL; it refuses IDs the server has never seen (online or in history) so a stale card cannot create an orphan entry, and the remark handler gets the same guard. Each change is audited as admin.device.group, deleting a device drops its override, and values loaded from disk are trimmed and capped like remarks. The menu entry is gated on the groupEdit capability, so the shared page hides it on the MFC server. Like remarks, the override is a Go-side label: no CMD_SET_GROUP is sent, so the host keeps reporting its own group to other masters. Verified with go test ./..., go vet, gofmt and a node --check pass on the page. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
5f764b2425
commit
034b11fa6a
16 files changed
+895
-54
No files matched your search
+43
-12
@@ -58,6 +58,17 @@ type deviceRemarkSnapshotter interface {
|
||||
Notes() map[string]string
|
||||
}
|
||||
|
||||
// DeviceGroups reports the web console's group override for a device ("" when
|
||||
// the host keeps the group it reported). Implemented by web.DeviceGroups.
|
||||
type DeviceGroups interface {
|
||||
Group(deviceID string) string
|
||||
}
|
||||
|
||||
// deviceGroupSnapshotter mirrors deviceRemarkSnapshotter for the group table.
|
||||
type deviceGroupSnapshotter interface {
|
||||
Groups() map[string]string
|
||||
}
|
||||
|
||||
// deviceOwnerSnapshotter is an optional DeviceOwner capability: returning the
|
||||
// whole binding table lets a long operation (the audit scan) apply the policy
|
||||
// without a read-lock acquisition per record. Implemented by web.DeviceACL.
|
||||
@@ -79,19 +90,35 @@ func (s *Server) ownerChecker() func(deviceID string) bool {
|
||||
return func(id string) bool { return s.cfg.DeviceACL.Owner(id) == "" }
|
||||
}
|
||||
|
||||
// remarkLookup returns a per-call remark lookup, or nil when no remark table
|
||||
// is configured. A table that can snapshot its whole map is read once, so a
|
||||
// large listing neither takes a read lock per device nor observes a save
|
||||
// halfway through; other tables fall back to one Note() per device.
|
||||
func (s *Server) remarkLookup() func(deviceID string) string {
|
||||
if s.cfg.DeviceRemarks == nil {
|
||||
return nil
|
||||
// hostView carries the per-call label snapshots used by one host listing. A
|
||||
// table that can snapshot its whole map is read once, so a large listing
|
||||
// neither takes a read lock per device nor observes a save halfway through;
|
||||
// other tables fall back to one lookup per device. nil means the table is not
|
||||
// configured.
|
||||
type hostView struct {
|
||||
remarkOf func(deviceID string) string
|
||||
groupOf func(deviceID string) string
|
||||
}
|
||||
|
||||
func (s *Server) hostView() hostView {
|
||||
var v hostView
|
||||
if s.cfg.DeviceRemarks != nil {
|
||||
if snap, ok := s.cfg.DeviceRemarks.(deviceRemarkSnapshotter); ok {
|
||||
notes := snap.Notes()
|
||||
v.remarkOf = func(id string) string { return notes[id] }
|
||||
} else {
|
||||
v.remarkOf = func(id string) string { return s.cfg.DeviceRemarks.Note(id) }
|
||||
}
|
||||
}
|
||||
if snap, ok := s.cfg.DeviceRemarks.(deviceRemarkSnapshotter); ok {
|
||||
notes := snap.Notes()
|
||||
return func(id string) string { return notes[id] }
|
||||
if s.cfg.DeviceGroups != nil {
|
||||
if snap, ok := s.cfg.DeviceGroups.(deviceGroupSnapshotter); ok {
|
||||
groups := snap.Groups()
|
||||
v.groupOf = func(id string) string { return groups[id] }
|
||||
} else {
|
||||
v.groupOf = func(id string) string { return s.cfg.DeviceGroups.Group(id) }
|
||||
}
|
||||
}
|
||||
return func(id string) string { return s.cfg.DeviceRemarks.Note(id) }
|
||||
return v
|
||||
}
|
||||
|
||||
// Config is filled by cmd/main.go from the YAMA_MCP_* environment variables;
|
||||
@@ -124,6 +151,10 @@ type Config struct {
|
||||
// DeviceRemarks, when non-nil, adds the web console's remark to
|
||||
// list_hosts/get_host_detail and lets the name/query filters match it.
|
||||
DeviceRemarks DeviceRemarks
|
||||
|
||||
// DeviceGroups, when non-nil, applies the web console's group override to
|
||||
// the group field and the group filter in the host tools.
|
||||
DeviceGroups DeviceGroups
|
||||
}
|
||||
|
||||
// toolFunc implements one tools/call. It returns either a result value or a
|
||||
@@ -517,5 +548,5 @@ const serverInstructions = `这是 YAMA 服务端的 MCP 端点(Go 实现)
|
||||
4) get_screenshot 只支持 mode=screen(按链路状况自适应缩放,max_width 省略时最大 1024);本实现没有 CLIENT_CAP_SCREEN_REGION 扩展,mode=region/window/monitor 与区域类参数一律返回 -32005。
|
||||
5) list_files / read_file 走文件传输通道;list_files 支持 ids 批量(最多 50 台、整批 50 秒)。read_file 只接受单个普通文件且整文件不超过 2 MiB,目录用 list_files;本实现没有 download_file / search_file,超限文件只能改用 terminal_exec 分段查看,或连 Windows 主控端的完整服务端。
|
||||
6) terminal_exec 需要客户端支持终端(CLIENT_CAP_TERMINAL),会在目标主机上创建持久会话;用完调用 terminal_close 释放,闲置会话由服务端回收。调用被取消时返回的 message 里带 session_id,可用它轮询或关闭。
|
||||
7) 本实现不维护授权状态与 TLS 标记:list_hosts / get_host_detail 不返回 authStatus/tls;备注(remark)由 Web 控制台维护,设置后会随主机返回,query 也匹配备注。get_server_status 只报版本、在线台数与监听地址,没有 switches/sessions。
|
||||
7) 本实现不维护授权状态与 TLS 标记:list_hosts / get_host_detail 不返回 authStatus/tls;备注(remark)与分组(group)可由 Web 控制台维护:备注设置后随主机返回且 query/name 会匹配,分组覆盖值同时用于返回字段和 group 过滤。get_server_status 只报版本、在线台数与监听地址,没有 switches/sessions。
|
||||
8) 列表与详情都是调用时刻的快照,主机可能在操作期间掉线;写操作前先读一次现状,动作后再复核。`
|
||||
@@ -142,9 +142,15 @@ func (s *Server) callListHosts(_ context.Context, raw json.RawMessage) (any, *rp
|
||||
ctF := strings.ToLower(args.ClientType)
|
||||
|
||||
matched := make([]hub.DeviceInfo, 0, len(pool))
|
||||
remarkOf := s.remarkLookup()
|
||||
view := s.hostView()
|
||||
for _, d := range pool {
|
||||
group := d.Group
|
||||
if view.groupOf != nil {
|
||||
// A web-console override wins over the group the host reported.
|
||||
if g := view.groupOf(d.ID); g != "" {
|
||||
group = g
|
||||
}
|
||||
}
|
||||
if group == "" {
|
||||
group = "default"
|
||||
}
|
||||
@@ -157,8 +163,8 @@ func (s *Server) callListHosts(_ context.Context, raw json.RawMessage) (any, *rp
|
||||
clientType = s.clientType(d.ID)
|
||||
}
|
||||
remark := ""
|
||||
if remarkOf != nil {
|
||||
remark = remarkOf(d.ID)
|
||||
if view.remarkOf != nil {
|
||||
remark = view.remarkOf(d.ID)
|
||||
}
|
||||
// query matches computer name, remark and IP; name matches computer
|
||||
// name and remark, like the Win32 server. The remark is the web
|
||||
@@ -194,7 +200,7 @@ func (s *Server) callListHosts(_ context.Context, raw json.RawMessage) (any, *rp
|
||||
end := min(begin+limit, total)
|
||||
hosts := make([]map[string]any, 0, end-begin)
|
||||
for _, d := range matched[begin:end] {
|
||||
hosts = append(hosts, s.hostJSON(d, remarkOf))
|
||||
hosts = append(hosts, s.hostJSON(d, view))
|
||||
}
|
||||
|
||||
structured := map[string]any{
|
||||
@@ -215,11 +221,16 @@ func (s *Server) callListHosts(_ context.Context, raw json.RawMessage) (any, *rp
|
||||
|
||||
// hostJSON projects a hub device onto the list_hosts/get_host_detail schema.
|
||||
// Fields the Go server does not know (authStatus, tls, clientType of an
|
||||
// offline entry) are omitted rather than fabricated. remark is included when
|
||||
// the web console has one for the device; remarkOf is the per-call snapshot
|
||||
// (nil when no remark table is configured).
|
||||
func (s *Server) hostJSON(d hub.DeviceInfo, remarkOf func(string) string) map[string]any {
|
||||
// offline entry) are omitted rather than fabricated. remark and group come
|
||||
// from the web console when it has them (view carries the per-call snapshots).
|
||||
func (s *Server) hostJSON(d hub.DeviceInfo, view hostView) map[string]any {
|
||||
group := d.Group
|
||||
if view.groupOf != nil {
|
||||
// A web-console override wins over the group the host reported.
|
||||
if g := view.groupOf(d.ID); g != "" {
|
||||
group = g
|
||||
}
|
||||
}
|
||||
if group == "" {
|
||||
group = "default"
|
||||
}
|
||||
@@ -229,8 +240,8 @@ func (s *Server) hostJSON(d hub.DeviceInfo, remarkOf func(string) string) map[st
|
||||
"group": group,
|
||||
"online": d.Online,
|
||||
}
|
||||
if remarkOf != nil {
|
||||
if remark := remarkOf(d.ID); remark != "" {
|
||||
if view.remarkOf != nil {
|
||||
if remark := view.remarkOf(d.ID); remark != "" {
|
||||
h["remark"] = remark
|
||||
}
|
||||
}
|
||||
@@ -316,7 +327,7 @@ func (s *Server) callGetHostDetail(_ context.Context, raw json.RawMessage) (any,
|
||||
return nil, errf(CodeInvalidParams, "Missing required parameter: id or ids")
|
||||
}
|
||||
online, offline := s.hostMaps()
|
||||
host, rerr := s.lookupHost(args.ID, online, offline, s.remarkLookup())
|
||||
host, rerr := s.lookupHost(args.ID, online, offline, s.hostView())
|
||||
if rerr != nil {
|
||||
return nil, rerr
|
||||
}
|
||||
@@ -329,11 +340,11 @@ func (s *Server) hostDetailBatch(ids []string) (any, *rpcError) {
|
||||
"Too many ids: %d (max %d per batch)", len(ids), batchMaxIDs)
|
||||
}
|
||||
online, offline := s.hostMaps()
|
||||
remarkOf := s.remarkLookup() // one snapshot for the whole batch
|
||||
view := s.hostView() // one snapshot for the whole batch
|
||||
results := make([]map[string]any, 0, len(ids))
|
||||
succeeded, failed := 0, 0
|
||||
for _, id := range ids {
|
||||
host, rerr := s.lookupHost(id, online, offline, remarkOf)
|
||||
host, rerr := s.lookupHost(id, online, offline, view)
|
||||
if rerr != nil {
|
||||
results = append(results, map[string]any{
|
||||
"id": id,
|
||||
@@ -392,17 +403,17 @@ func (s *Server) hostMaps() (online, offline map[string]hub.DeviceInfo) {
|
||||
// lookupHost resolves one host: online first, then the last-known history
|
||||
// record. It mirrors the Win32 server, which answers offline hosts with their
|
||||
// final snapshot instead of an error.
|
||||
func (s *Server) lookupHost(id string, online, offline map[string]hub.DeviceInfo, remarkOf func(string) string) (map[string]any, *rpcError) {
|
||||
func (s *Server) lookupHost(id string, online, offline map[string]hub.DeviceInfo, view hostView) (map[string]any, *rpcError) {
|
||||
if !isDecimalID(id) {
|
||||
return nil, errf(CodeInvalidParams, "Invalid id: expected a decimal host id string")
|
||||
}
|
||||
if d, ok := online[id]; ok {
|
||||
h := s.hostJSON(d, remarkOf)
|
||||
h := s.hostJSON(d, view)
|
||||
h["capabilities"] = s.capabilities(d)
|
||||
return h, nil
|
||||
}
|
||||
if d, ok := offline[id]; ok {
|
||||
return s.hostJSON(d, remarkOf), nil
|
||||
return s.hostJSON(d, view), nil
|
||||
}
|
||||
return nil, errf(CodeOffline, "Host not found or offline: %s", id)
|
||||
}
|
||||
|
||||
@@ -226,9 +226,22 @@ type fakeRemarkSnapshot map[string]string
|
||||
func (f fakeRemarkSnapshot) Note(deviceID string) string { return f[deviceID] }
|
||||
func (f fakeRemarkSnapshot) Notes() map[string]string { return f }
|
||||
|
||||
// fakeGroups stands in for web.DeviceGroups (per-device fallback path).
|
||||
type fakeGroups map[string]string
|
||||
|
||||
func (f fakeGroups) Group(deviceID string) string { return f[deviceID] }
|
||||
|
||||
// fakeGroupSnapshot also implements Groups(), the snapshot capability the
|
||||
// shipped web.DeviceGroups provides.
|
||||
type fakeGroupSnapshot map[string]string
|
||||
|
||||
func (f fakeGroupSnapshot) Group(deviceID string) string { return f[deviceID] }
|
||||
func (f fakeGroupSnapshot) Groups() map[string]string { return f }
|
||||
|
||||
func TestListHostsPoolsAndProjection(t *testing.T) {
|
||||
f := newLocalFixture(t)
|
||||
f.srv.cfg.DeviceRemarks = fakeRemarkSnapshot{"1001": "Reception PC"}
|
||||
f.srv.cfg.DeviceGroups = fakeGroupSnapshot{"1002": "Remote"}
|
||||
|
||||
text, sc := mustCallLocal(t, f.srv, "list_hosts", `{}`)
|
||||
hosts := hostList(t, sc)
|
||||
@@ -285,6 +298,9 @@ func TestListHostsPoolsAndProjection(t *testing.T) {
|
||||
if got := strField(t, alpha, "remark"); got != "Reception PC" {
|
||||
t.Fatalf("remark = %q, want the web console's label", got)
|
||||
}
|
||||
if !strings.Contains(text, "1002\tBETA\t-\t192.168.1.2\tRemote\tWindows 11\ttrue\tEXE\t-") {
|
||||
t.Fatalf("text = %q, want BETA row with the group override", text)
|
||||
}
|
||||
if _, ok := hosts[1]["remark"]; ok {
|
||||
t.Fatalf("unset remark present on %v", hosts[1]["id"])
|
||||
}
|
||||
@@ -378,6 +394,33 @@ func TestListHostsFilters(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestListHostsGroupOverride: the web console's group override drives both the
|
||||
// projected group and the group filter, including for offline records.
|
||||
func TestListHostsGroupOverride(t *testing.T) {
|
||||
f := newLocalFixture(t)
|
||||
f.srv.cfg.DeviceGroups = fakeGroupSnapshot{"1002": "Remote", "1003": "Remote", "2001": "Archive"}
|
||||
|
||||
_, sc := mustCallLocal(t, f.srv, "list_hosts", `{"group":"remote"}`)
|
||||
wantIDs(t, hostList(t, sc), "1002", "1003")
|
||||
|
||||
_, sc = mustCallLocal(t, f.srv, "list_hosts", `{"online":false,"group":"archive"}`)
|
||||
wantIDs(t, hostList(t, sc), "2001")
|
||||
if got := strField(t, hostList(t, sc)[0], "group"); got != "Archive" {
|
||||
t.Fatalf("offline group = %q, want the override", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestListHostsGroupOverrideFallback covers a DeviceGroups implementation that
|
||||
// participates through Group() only (no whole-table snapshot).
|
||||
func TestListHostsGroupOverrideFallback(t *testing.T) {
|
||||
f := newLocalFixture(t)
|
||||
f.srv.cfg.DeviceGroups = fakeGroups{"1002": "Remote"}
|
||||
_, sc := mustCallLocal(t, f.srv, "list_hosts", `{}`)
|
||||
if got := strField(t, hostList(t, sc)[1], "group"); got != "Remote" {
|
||||
t.Fatalf("group = %q, want the override", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListHostsPaging(t *testing.T) {
|
||||
f := newLocalFixture(t)
|
||||
|
||||
|
||||
Reference in new issue
Block a user