`rbac.IsExternalScope` accepts `all` and `application_connect`, neither
of which is an `api_key_scope` enum member. The plural `Scopes` field
stored the name as given, so `{"scopes":["all"]}` passed validation and
then 500'd inside `apikey.Generate` with `invalid API key scope: "all"`.
Reachable from `coder tokens create --scope all`, from the spellings
`docs/admin/users/sessions-tokens.md` taught, and from the `codersdk`
constants exported for exactly this purpose.
- Canonicalize inside `apikey.Generate`, in the loop that already
validates each name. One statement covers every caller and all three
input paths: plural `Scopes`, deprecated singular `Scope`, and the
default. Replaces two open-coded switches and the handler's own
per-element copy, so one place decides the stored spelling and the
handler only decides what may be requested.
- Deduplicate in the same pass. An alias and its canonical spelling are
two names going in and one name in the column.
- `ExternalScopeNames()` lists every name `IsExternalScope` accepts,
instead of dropping catalog entries that fail to parse. A curated entry
that cannot be stored used to pass every test and 500 at runtime; it now
fails two.
- Split the 400. A misspelled name and an internal `api_key_scope`
member need different words, since no re-spelling makes the second
requestable. Both rejection sites share one helper that names the case
and links the docs.
- Add reject-path tests for `not_a_real_scope` and `debug_info:read`.
The second is a valid enum member the rbac catalog treats as internal,
so before this both handler guards could be deleted with the suite still
green. A further case pins that plural `Scopes` wins when a caller sets
both fields.
- `TestExternalScopesAreStorable` pins the class rather than the two
known instances: every public rbac scope name must be storable.
`coderd/rbac` cannot assert this itself, since `database` imports `rbac`
and not the reverse.
- Docs use the canonical spellings, link the `codersdk.APIKeyScope`
schema, and say which scopes a token cannot request.
Egress is unchanged: `convertAPIKey` still derives a legacy singular
name on the way out. `ExternalScopeNames()` returns the same 57 names as
before, and `codersdk/apikey_scopes_gen.go` regenerates identically.
`rbac.CanonicalScopeName` merged with #28167, so this applies to `main`
and reviews on its own. The OAuth2 provider ignores scopes entirely
today (`authorize.go:237`, `tokens.go:377`, `tokens.go:520`), so it is
unaffected until those TODOs resolve.
Deferred from review: PLAT-528, PLAT-529, PLAT-530, and PLAT-532.
PLAT-532 covers `{"scopes":[]}` and `{"allow_list":[]}` defaulting open,
which predates this PR.
---------
Co-authored-by: McKayla はな <mckayla@hey.com>
167 lines
4.9 KiB
Go
167 lines
4.9 KiB
Go
package rbac
|
|
|
|
import (
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
// externalLowLevel is the curated set of low-level scope names exposed to users.
|
|
// Any valid resource:action pair not in this set is considered internal-only
|
|
// and must not be user-requestable.
|
|
var externalLowLevel = map[ScopeName]struct{}{
|
|
// Workspaces
|
|
"workspace:read": {},
|
|
"workspace:create": {},
|
|
"workspace:update": {},
|
|
"workspace:delete": {},
|
|
"workspace:ssh": {},
|
|
"workspace:start": {},
|
|
"workspace:stop": {},
|
|
"workspace:application_connect": {},
|
|
"workspace:*": {},
|
|
|
|
// Templates
|
|
"template:read": {},
|
|
"template:create": {},
|
|
"template:update": {},
|
|
"template:delete": {},
|
|
"template:use": {},
|
|
"template:*": {},
|
|
|
|
// API keys (self-management)
|
|
"api_key:read": {},
|
|
"api_key:create": {},
|
|
"api_key:update": {},
|
|
"api_key:delete": {},
|
|
"api_key:*": {},
|
|
|
|
// Chat model configs
|
|
"chat_model_config:read": {},
|
|
"chat_model_config:share": {},
|
|
|
|
// Files
|
|
"file:read": {},
|
|
"file:create": {},
|
|
"file:*": {},
|
|
|
|
// Users
|
|
"user:read": {},
|
|
"user:read_personal": {},
|
|
"user:update_personal": {},
|
|
"user:*": {},
|
|
|
|
// User secrets
|
|
"user_secret:read": {},
|
|
"user_secret:create": {},
|
|
"user_secret:update": {},
|
|
"user_secret:delete": {},
|
|
"user_secret:*": {},
|
|
|
|
// User skills
|
|
"user_skill:read": {},
|
|
"user_skill:create": {},
|
|
"user_skill:update": {},
|
|
"user_skill:delete": {},
|
|
"user_skill:*": {},
|
|
|
|
// Tasks
|
|
"task:create": {},
|
|
"task:read": {},
|
|
"task:update": {},
|
|
"task:delete": {},
|
|
"task:*": {},
|
|
|
|
// Organizations
|
|
"organization:read": {},
|
|
"organization:update": {},
|
|
"organization:delete": {},
|
|
"organization:*": {},
|
|
}
|
|
|
|
// Public composite coder:* scopes exposed to users.
|
|
var externalComposite = map[ScopeName]struct{}{
|
|
"coder:workspaces.create": {},
|
|
"coder:workspaces.operate": {},
|
|
"coder:workspaces.delete": {},
|
|
"coder:workspaces.access": {},
|
|
"coder:templates.build": {},
|
|
"coder:templates.author": {},
|
|
"coder:apikeys.manage_self": {},
|
|
}
|
|
|
|
// scopeAliases maps the spellings accepted for backward compatibility onto the
|
|
// names the api_key_scope enum stores. IsExternalScope accepts every key and
|
|
// CanonicalScopeName rewrites it to its value, so the two agree by reading one
|
|
// table rather than by keeping two switches in step. Drift between them is
|
|
// worse in one direction than the other: a name accepted as public but not
|
|
// rewritten is declared requestable and then fails to expand on every request
|
|
// naming it.
|
|
var scopeAliases = map[ScopeName]ScopeName{
|
|
"all": ScopeAll,
|
|
"application_connect": ScopeApplicationConnect,
|
|
}
|
|
|
|
// IsExternalScope returns true if the scope is public: the `all` and
|
|
// `application_connect` aliases, the canonical `coder:all` and
|
|
// `coder:application_connect`, a curated low-level resource:action scope, or a
|
|
// curated composite `coder:*` scope.
|
|
func IsExternalScope(name ScopeName) bool {
|
|
if _, ok := scopeAliases[name]; ok {
|
|
return true
|
|
}
|
|
switch name {
|
|
case ScopeAll, ScopeApplicationConnect:
|
|
return true
|
|
}
|
|
if _, ok := externalLowLevel[name]; ok {
|
|
return true
|
|
}
|
|
if _, ok := externalComposite[name]; ok {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// CanonicalScopeName maps the backward-compatibility aliases IsExternalScope
|
|
// accepts onto the names the api_key_scope enum stores. Any other name is
|
|
// returned unchanged.
|
|
//
|
|
// IsExternalScope answers whether a name may be requested; it does not answer
|
|
// how that name is spelled once persisted. The aliases `all` and
|
|
// `application_connect` are accepted but are not enum members, so a caller
|
|
// that stores what it validated must canonicalize in between.
|
|
func CanonicalScopeName(name ScopeName) ScopeName {
|
|
if canonical, ok := scopeAliases[name]; ok {
|
|
return canonical
|
|
}
|
|
return name
|
|
}
|
|
|
|
// ExternalScopeNames returns a sorted list of all public scopes: the canonical
|
|
// `coder:all` and `coder:application_connect` spellings, the curated low-level
|
|
// resource:action names, and the curated composite coder:* scopes.
|
|
//
|
|
// This is the set IsExternalScope accepts, minus the bare `all` and
|
|
// `application_connect` aliases. Every name here is canonical, so match a
|
|
// client-supplied name through CanonicalScopeName or an alias reads as unknown.
|
|
func ExternalScopeNames() []string {
|
|
names := make([]string, 0, len(externalLowLevel)+len(externalComposite)+2)
|
|
names = append(names, string(ScopeAll))
|
|
names = append(names, string(ScopeApplicationConnect))
|
|
|
|
// curated low-level names, unfiltered: IsExternalScope accepts every key
|
|
// here, so filtering would hide an unparsable entry instead of failing on it.
|
|
for name := range externalLowLevel {
|
|
names = append(names, string(name))
|
|
}
|
|
|
|
// curated composite names
|
|
for name := range externalComposite {
|
|
names = append(names, string(name))
|
|
}
|
|
|
|
sort.Slice(names, func(i, j int) bool { return strings.Compare(names[i], names[j]) < 0 })
|
|
return names
|
|
}
|