fix(36): compile token scope catalog once in pier boot coverage warning
This commit is contained in:
@@ -64,45 +64,64 @@ func TokenScopeLabels(ctx context.Context, app *backpack.App, names []string) []
|
||||
return out
|
||||
}
|
||||
|
||||
func tokenScopeCatalogFor(app *backpack.App, plugins []party.Plugin) *tokenScopeCatalog {
|
||||
func tokenScopeCatalogFor(app *backpack.App, plugins []party.Plugin) (*tokenScopeCatalog, error) {
|
||||
if guard := publishedResolverGuard(app); guard != nil && guard.reg != nil && guard.reg.tokenScopes != nil {
|
||||
return guard.reg.tokenScopes
|
||||
return guard.reg.tokenScopes, nil
|
||||
}
|
||||
if len(plugins) > 0 {
|
||||
reg, err := compileTokenCatalog(plugins)
|
||||
if err == nil && reg != nil {
|
||||
return reg.tokenScopes
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil
|
||||
if reg != nil {
|
||||
return reg.tokenScopes, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
reg, err := tokenCatalogForApp(app)
|
||||
if err == nil && reg != nil {
|
||||
return reg.tokenScopes
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil
|
||||
if reg != nil {
|
||||
return reg.tokenScopes, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// TokenScopeCovers reports whether at least one non-"*" personal API token
|
||||
// scope allows method and covers every permission code in permissions. It uses
|
||||
// the activated cabana catalog on app when available, else compiles from
|
||||
// plugins (falling back to party.Activated). Returns false when permissions is
|
||||
// empty on a non-read-only method, when method is unsupported, or when no
|
||||
// non-"*" scope covers every code.
|
||||
func TokenScopeCovers(app *backpack.App, plugins []party.Plugin, method string, permissions []string) bool {
|
||||
cat := tokenScopeCatalogFor(app, plugins)
|
||||
if cat == nil {
|
||||
// TokenScopeMatcher caches a resolved token scope catalog so callers checking
|
||||
// many routes or MCP tools (such as pier.Mount) do not recompile plugin scopes
|
||||
// per tool when cabana has not been activated on app.
|
||||
type TokenScopeMatcher struct {
|
||||
cat *tokenScopeCatalog
|
||||
}
|
||||
|
||||
// NewTokenScopeMatcher resolves the activated cabana token scope catalog on app
|
||||
// when available, or compiles it once from plugins (falling back to
|
||||
// party.Activated).
|
||||
func NewTokenScopeMatcher(app *backpack.App, plugins []party.Plugin) (*TokenScopeMatcher, error) {
|
||||
cat, err := tokenScopeCatalogFor(app, plugins)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &TokenScopeMatcher{cat: cat}, nil
|
||||
}
|
||||
|
||||
// Covers reports whether at least one non-"*" personal API token scope in the
|
||||
// matcher allows method and covers every permission code in permissions.
|
||||
func (m *TokenScopeMatcher) Covers(method string, permissions []string) bool {
|
||||
if m == nil || m.cat == nil {
|
||||
return false
|
||||
}
|
||||
m := strings.ToUpper(strings.TrimSpace(method))
|
||||
if m == "" {
|
||||
methodName := strings.ToUpper(strings.TrimSpace(method))
|
||||
if methodName == "" {
|
||||
return false
|
||||
}
|
||||
var scopes [][]string
|
||||
for _, scope := range cat.ordered {
|
||||
for _, scope := range m.cat.ordered {
|
||||
if scope.Name == scopeAll {
|
||||
continue
|
||||
}
|
||||
if !scope.Methods[m] {
|
||||
if !scope.Methods[methodName] {
|
||||
continue
|
||||
}
|
||||
if scope.Full {
|
||||
@@ -112,3 +131,17 @@ func TokenScopeCovers(app *backpack.App, plugins []party.Plugin, method string,
|
||||
}
|
||||
return scopeCoversAll(&bouncer.ScopeLimit{Scopes: scopes}, permissions)
|
||||
}
|
||||
|
||||
// TokenScopeCovers reports whether at least one non-"*" personal API token
|
||||
// scope allows method and covers every permission code in permissions. It uses
|
||||
// the activated cabana catalog on app when available, else compiles from
|
||||
// plugins (falling back to party.Activated). Returns false when permissions is
|
||||
// empty on a non-read-only method, when method is unsupported, or when no
|
||||
// non-"*" scope covers every code.
|
||||
func TokenScopeCovers(app *backpack.App, plugins []party.Plugin, method string, permissions []string) bool {
|
||||
matcher, err := NewTokenScopeMatcher(app, plugins)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return matcher.Covers(method, permissions)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user