Files
tenant-registry/internal/scopes/scopes.go
T
sharang 97284b5008
ci / shared (push) Successful in 14s
ci / test (push) Successful in 25m3s
ci / image (push) Successful in 39s
feat(api-keys): Scope catalogue, kinds, expiry and sha256 keys (#70)
2026-10-13 21:21:16 +00:00

113 lines
3.6 KiB
Go

// Package scopes is the catalogue of API key scopes (ADR: tenant-bound,
// scoped API keys). A scope is "<product>:<resource>.<action>"; the product
// part names the catalogue product whose entitlement the scope needs. The
// catalogue is code, not data: every scope here is implemented by a product.
package scopes
import (
"fmt"
"sort"
"strings"
)
// Scope describes one grantable permission.
type Scope struct {
Name string `json:"name"`
Product string `json:"product"` // catalogue product the tenant must be entitled to
Description string `json:"description"`
}
// catalogue — keep sorted by name. werkbank:* predate the naming scheme and
// keep their names; they need the pruefwerk entitlement (Werkbank is an
// add-on of Prüfwerk).
var catalogue = []Scope{
{"firmwerk:licence", "firmwerk", "Fetch a Firmwerk CLI licence without a browser (CI)"},
{"firmwerk:projects.read", "firmwerk", "Read Firmwerk cloud projects"},
{"firmwerk:projects.write", "firmwerk", "Create and change Firmwerk cloud projects"},
{"pruefwerk:enisa.read", "pruefwerk", "Read ENISA report drafts"},
{"pruefwerk:findings.read", "pruefwerk", "Read findings"},
{"pruefwerk:findings.write", "pruefwerk", "Set finding status and feedback"},
{"pruefwerk:sbom.read", "pruefwerk", "Read SBOMs, licences and SBOM quality"},
{"pruefwerk:scans.read", "pruefwerk", "Read scan runs"},
{"pruefwerk:scans.run", "pruefwerk", "Start scans"},
{"werkbank:admin", "pruefwerk", "Manage Werkbank runners"},
{"werkbank:runner", "pruefwerk", "Act as a Werkbank runner"},
{"werkpilot:licence", "werkpilot", "Fetch a Werkpilot CLI licence without a browser (CI)"},
}
var byName = func() map[string]Scope {
m := make(map[string]Scope, len(catalogue))
for _, s := range catalogue {
m[s.Name] = s
}
return m
}()
// All returns the catalogue, sorted by name.
func All() []Scope {
out := append([]Scope(nil), catalogue...)
sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
return out
}
// Lookup returns the catalogue entry for name.
func Lookup(name string) (Scope, bool) {
s, ok := byName[name]
return s, ok
}
// Validate checks that every name is in the catalogue and, when product is
// set, that every scope belongs to it. It returns the normalised (sorted,
// de-duplicated) list.
func Validate(names []string, product string) ([]string, error) {
seen := map[string]bool{}
out := make([]string, 0, len(names))
for _, n := range names {
n = strings.TrimSpace(n)
s, ok := byName[n]
if !ok {
return nil, fmt.Errorf("unknown scope %q", n)
}
if product != "" && s.Product != product {
return nil, fmt.Errorf("scope %q belongs to product %q, not %q", n, s.Product, product)
}
if !seen[n] {
seen[n] = true
out = append(out, n)
}
}
sort.Strings(out)
return out, nil
}
// Products returns the distinct products the given scopes need. Unknown
// names are skipped.
func Products(names []string) []string {
set := map[string]bool{}
for _, n := range names {
if s, ok := byName[n]; ok {
set[s.Product] = true
}
}
out := make([]string, 0, len(set))
for p := range set {
out = append(out, p)
}
sort.Strings(out)
return out
}
// Effective filters a key's stored scopes down to those whose product is in
// entitled. Names outside the catalogue (keys created before it existed) are
// kept unchanged: the products that issued them still interpret them.
func Effective(names []string, entitled map[string]bool) []string {
out := make([]string, 0, len(names))
for _, n := range names {
if s, ok := byName[n]; ok && !entitled[s.Product] {
continue
}
out = append(out, n)
}
return out
}