mirror of
https://gitcode.com/JianFeeeee/ModelRouter.git
synced 2026-11-07 09:47:23 +00:00
## 插件管理页(用户要求:权限不要只放 config.yaml)
插件页新增两列(可见性、端点)和一块访问控制编辑器,可直接改:
- 每个插件的整体可见性(跟随插件 / 仅 admin / 仅 user / admin+user / 所有人)
- 每个注册端点的可访问角色与密钥(admin, user, @homeagent, public)
原先这些能力后端齐全但 WebUI 零入口,唯一改法是手改 config.yaml。
## 修掉的三个真缺陷
1. endpointAccess 读错了 override 的键
SetEndpointAccess 按 plugin#path 存,endpointAccess 却读 plugin 级。
后果:改了权限后 UI 仍显示插件声明的角色,再保存一次就把限制悄悄撤销。
鉴权一直是对的,只有显示和往返是坏的 —— 所以之前没被发现。
2. override 只在内存里
内存 map 让"billing 仅 admin"看着生效,直到下次重启限制凭空消失,
日志里没有任何线索。现在落盘到 plugin_dir/.access.json,
插件升级覆盖 .state.json 也不会连带放宽访问。删掉最后一个 override 就删文件,
"没有覆盖"用文件不存在来诚实表达。
3. 可见性列表泄漏内部键
端点 override 与插件可见性共用一个 map,UI 因此会看到 "probe#hello"
并可能把它当 audience 存回去。现在按已注册端点集合过滤。
另外把整体可见性接到同一条 /access 通路(path 用 *),并修了可见性下拉的
集合比较:服务端原样存 "admin, user",而选项是 "user,admin",不相等就会
显示"跟随插件",用户一保存就把访问范围放宽回插件声明 —— 比显示错更糟。
## 活跃请求:三个视图显示 0
active 是实时仪表,没有日历周期可过滤,但周期响应压根没带这个字段。
前端 `st.active || 0` 于是把"字段缺失"变成了一句自信的"0 活跃",
而 all 视图(响应里有该字段)显示真值 —— 三个视图 0、总视图正常。
现在周期响应带上 active,前端去掉 || 0 回退:缺失显示 — 而不是假装是 0。
判据:四个 period 都断言 active 存在且为数字;变异去掉字段即变红。
生产实测 day/week/month/all 均为 4。
Co-Authored-By: ModelRouter <noreply@modelrouter.dev>
(cherry picked from commit 120ba9e957)
119 lines
3.9 KiB
Go
119 lines
3.9 KiB
Go
package lua
|
|
|
|
// Per-caller state scoping.
|
|
//
|
|
// Role visibility answers "may this role see the plugin at all". It does not
|
|
// answer "which PART of its data", and for the billing plugin those are
|
|
// different questions: the global figures (total spend, every source, every
|
|
// model) are deployment data, while the per-key buckets describe one caller.
|
|
//
|
|
// So a plugin may declare a split, and the gateway then hands a non-admin
|
|
// caller only the buckets that are theirs. The plugin declares it because only
|
|
// the plugin author knows which of its buckets are global.
|
|
|
|
import "strings"
|
|
|
|
// StateScope is a plugin's declaration of which state buckets are global.
|
|
type StateScope struct {
|
|
// Global names the buckets that describe the whole deployment. Anything not
|
|
// listed is treated as per-caller and filtered.
|
|
Global []string `json:"global,omitempty"`
|
|
// Depth gives the nesting level of each PER-CALLER bucket: 1 for
|
|
// {maskedKey: bucket}, 2 for {day: {maskedKey: bucket}}. It is declared
|
|
// because both shapes are maps and sniffing would confuse an empty bucket
|
|
// with a nested one.
|
|
Depth map[string]int `json:"depth,omitempty"`
|
|
}
|
|
|
|
// IsGlobal reports whether a top-level state key is deployment-wide.
|
|
func (s *StateScope) IsGlobal(key string) bool {
|
|
if s == nil {
|
|
return true
|
|
}
|
|
for _, g := range s.Global {
|
|
if g == key {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// StateScopeFor returns a plugin's declared split, or nil when it declares none.
|
|
func (ps *Plugins) StateScopeFor(name string) *StateScope {
|
|
ps.mu.RLock()
|
|
defer ps.mu.RUnlock()
|
|
for _, p := range ps.plugins {
|
|
if p.Info.Name != name || p.UI == nil {
|
|
continue
|
|
}
|
|
return p.UI.StateScope
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// compileDepthMap normalizes the declared depth table, defaulting to 1 for a
|
|
// per-caller bucket the author did not annotate.
|
|
func compileDepthMap(in map[string]int) map[string]int {
|
|
if len(in) == 0 {
|
|
return nil
|
|
}
|
|
out := make(map[string]int, len(in))
|
|
for k, v := range in {
|
|
if v < 1 {
|
|
v = 1
|
|
}
|
|
out[strings.TrimSpace(k)] = v
|
|
}
|
|
return out
|
|
}
|
|
|
|
// DepthOf returns the declared nesting level for a per-caller bucket, 1 by
|
|
// default. A bucket the author did not annotate is assumed flat, which is the
|
|
// common shape ({maskedKey: row}).
|
|
func (s *StateScope) DepthOf(key string) int {
|
|
if s == nil || s.Depth == nil {
|
|
return 1
|
|
}
|
|
if d, ok := s.Depth[key]; ok {
|
|
return d
|
|
}
|
|
return 1
|
|
}
|
|
|
|
// VisibilityOverrides returns every operator override in force, keyed by plugin
|
|
// name. An absent name means the plugin's own declaration applies.
|
|
//
|
|
// The WebUI needs this to show which visibility is IMPOSED, not merely which is
|
|
// in effect: an operator comparing two deployments should not have to open a
|
|
// .lua to learn whether a page is admin-only by the plugin's choice or by their
|
|
// own override.
|
|
func (ps *Plugins) VisibilityOverrides() map[string]string {
|
|
ps.mu.RLock()
|
|
defer ps.mu.RUnlock()
|
|
if len(ps.visibility) == 0 {
|
|
return nil
|
|
}
|
|
epKeys := make(map[string]struct{})
|
|
for _, e := range ps.endpointAccessLocked() {
|
|
epKeys[endpointOverrideKey(e.Plugin, e.Path)] = struct{}{}
|
|
}
|
|
out := make(map[string]string, len(ps.visibility))
|
|
for k, v := range ps.visibility {
|
|
// Per-endpoint overrides live in the SAME map under a "name#path" key.
|
|
// They are endpoint policy, not a plugin audience, so leaking them here
|
|
// both mislabels them and lets the plugin page write an audience of the
|
|
// form "probe#hello" that no code ever reads as one.
|
|
// Per-endpoint overrides share this map under a "name#path" key. They
|
|
// are endpoint policy, not a plugin audience: reporting them here both
|
|
// mislabels them and invites the plugin page to save an audience of the
|
|
// form "probe#hello" that nothing reads as an audience. The set of
|
|
// endpoint keys is already computed, so ask it rather than re-deriving
|
|
// the shape from the key.
|
|
if _, isEndpoint := epKeys[k]; isEndpoint {
|
|
continue
|
|
}
|
|
out[k] = v
|
|
}
|
|
return out
|
|
}
|