feat: add multi-scope support to API keys (#19917)

# Canonicalize API Key Scopes

This PR introduces canonical API key scopes with a `coder:` namespace prefix to avoid collisions with low-level resource:action names. It:

1. Renames special API key scopes in the database:
   - `all` → `coder:all`
   - `application_connect` → `coder:application_connect`

2. Adds support for a new `scopes` field in the API key creation request, allowing multiple scopes to be specified while maintaining backward compatibility with the singular `scope` field.

3. Updates the API documentation to reflect these changes, including the new endpoint for listing public API key scopes.

4. Ensures backward compatibility by mapping between legacy and canonical scope names in relevant code paths.
This commit is contained in:
Thomas Kosiewski
2025-09-26 11:56:34 +02:00
committed by GitHub
parent 4bda39585d
commit d0db9ec88f
35 changed files with 332 additions and 113 deletions
+23 -2
View File
@@ -5,7 +5,7 @@ import (
"fmt"
"go/format"
"os"
"sort"
"slices"
"strings"
"github.com/coder/coder/v2/coderd/rbac"
@@ -26,7 +26,7 @@ func main() {
func generate() ([]byte, error) {
names := rbac.ExternalScopeNames()
sort.Strings(names)
slices.Sort(names)
var b bytes.Buffer
if _, err := b.WriteString("// Code generated by scripts/apikeyscopesgen. DO NOT EDIT.\n"); err != nil {
@@ -35,11 +35,32 @@ func generate() ([]byte, error) {
if _, err := b.WriteString("package codersdk\n\n"); err != nil {
return nil, err
}
// NOTE: Keep all APIKeyScope constants in a single generated file.
// Some tooling (e.g. swaggo) can behave non-deterministically when
// enums are spread across multiple files:
// https://github.com/swaggo/swag/issues/2038
// We generate everything into codersdk/apikey_scopes_gen.go as the
// single source of truth so doc generation remains stable.
// Constants
if _, err := b.WriteString("const (\n"); err != nil {
return nil, err
}
// Always include legacy/deprecated aliases for backward compatibility.
// These are kept in generated code to ensure consistent availability
// across releases even if hand-written files change.
if _, err := b.WriteString("\t// Deprecated: use codersdk.APIKeyScopeCoderAll instead.\n"); err != nil {
return nil, err
}
if _, err := b.WriteString("\tAPIKeyScopeAll APIKeyScope = \"all\"\n"); err != nil {
return nil, err
}
if _, err := b.WriteString("\t// Deprecated: use codersdk.APIKeyScopeCoderApplicationConnect instead.\n"); err != nil {
return nil, err
}
if _, err := b.WriteString("\tAPIKeyScopeApplicationConnect APIKeyScope = \"application_connect\"\n"); err != nil {
return nil, err
}
for _, n := range names {
res, act := splitRA(n)
if act == policy.WildcardSymbol {