fix(36): compile token scope catalog once in pier boot coverage warning
This commit is contained in:
@@ -17,6 +17,7 @@ func warnUncoveredBackendWriteTools(app *backpack.App, plugins []party.Plugin, c
|
||||
if cat == nil || len(cat.tools) == 0 {
|
||||
return
|
||||
}
|
||||
var matcher *cabana.TokenScopeMatcher
|
||||
byPlugin := map[string][]string{}
|
||||
var pluginOrder []string
|
||||
for _, t := range cat.tools {
|
||||
@@ -26,7 +27,18 @@ func warnUncoveredBackendWriteTools(app *backpack.App, plugins []party.Plugin, c
|
||||
if t.method == http.MethodGet || t.method == http.MethodHead {
|
||||
continue
|
||||
}
|
||||
if cabana.TokenScopeCovers(app, plugins, t.method, t.decl.Permissions) {
|
||||
if matcher == nil {
|
||||
var err error
|
||||
matcher, err = cabana.NewTokenScopeMatcher(app, plugins)
|
||||
if err != nil {
|
||||
logger().Warn(
|
||||
"pier: could not compile token scope catalog while checking backend write MCP tool coverage",
|
||||
"err", err,
|
||||
)
|
||||
return
|
||||
}
|
||||
}
|
||||
if matcher.Covers(t.method, t.decl.Permissions) {
|
||||
continue
|
||||
}
|
||||
if _, seen := byPlugin[t.pluginID]; !seen {
|
||||
|
||||
@@ -319,4 +319,45 @@ func TestUncoveredBackendWriteToolsWarning(t *testing.T) {
|
||||
t.Fatalf("expected 0 warning lines for user-guarded write tool, got %d:\n%s", got, buf.String())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("warns_on_invalid_token_scope_catalog_without_misleading_tool_warnings", func(t *testing.T) {
|
||||
buf := captureLogs(t)
|
||||
app := pierAppWith(t, map[string]string{"mcp.yaml": "enabled: false\n"}, &acmeGuard{})
|
||||
plugin := warningScopedPlugin{
|
||||
warningBackendPlugin: warningBackendPlugin{
|
||||
id: "acme.badscope",
|
||||
mountMW: true,
|
||||
registerMW: true,
|
||||
perms: []pact.Permission{
|
||||
{Code: "acme.badscope.manage", Tab: "Bad", Label: "Manage"},
|
||||
},
|
||||
tools: []pact.MCPTool{
|
||||
{
|
||||
Name: "badscope_create_item", Title: "Create item", Description: "Create item.",
|
||||
Method: http.MethodPost, Path: "/acme-badscope/items/{id}", Input: warningTestInput{},
|
||||
Permissions: []string{"acme.badscope.manage"},
|
||||
},
|
||||
},
|
||||
},
|
||||
scopes: []pact.TokenScope{
|
||||
{
|
||||
Name: "INVALID SCOPE", Label: "Bad", Description: "Invalid scope name",
|
||||
Permissions: []string{"acme.badscope.*"}, Methods: allMethods,
|
||||
},
|
||||
},
|
||||
}
|
||||
host, err := mountPier(t, app, []party.Plugin{plugin})
|
||||
if err != nil {
|
||||
t.Fatalf("mountPier must not fail boot: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = host.pier.Close() })
|
||||
|
||||
logs := buf.String()
|
||||
if got := countWarningLines(logs); got != 0 {
|
||||
t.Fatalf("expected 0 misleading per-plugin uncovered-tool warnings on catalog error, got %d:\n%s", got, logs)
|
||||
}
|
||||
if !strings.Contains(logs, "could not compile token scope catalog") {
|
||||
t.Fatalf("expected catalog compile warning in logs:\n%s", logs)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user