- Notifications
You must be signed in to change notification settings - Fork1k
feat: add lint check for API key scope enum completeness#19862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
ThomasK33 merged 1 commit intomainfromthomask33/19846-build-time-check-db-enum-contains-all-rbac-scopesSep 24, 2025
+166 −1
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
7 changes: 6 additions & 1 deletionMakefile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletionsscripts/check-scopes/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# check-scopes | ||
Validates that the DB enum `api_key_scope` contains every `<resource>:<action>` derived from `coderd/rbac/policy/RBACPermissions`. | ||
- Exits 0 when all scopes are present in `coderd/database/dump.sql`. | ||
- Exits 1 and prints missing values with suggested `ALTER TYPE` statements otherwise. | ||
## Usage | ||
Ensure the schema dump is up-to-date, then run the check: | ||
```sh | ||
make -B gen/db # forces DB dump regeneration | ||
make lint/check-scopes | ||
``` | ||
Or directly: | ||
```sh | ||
go run ./tools/check-scopes | ||
``` | ||
Optional flags: | ||
- `-dump path` — override path to `dump.sql` (default `coderd/database/dump.sql`). | ||
## Remediation | ||
When the tool reports missing values: | ||
1. Create a DB migration extending the enum, e.g.: | ||
```sql | ||
ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'template:view_insights'; | ||
``` | ||
2. Regenerate and re-run: | ||
```sh | ||
make -B gen/db && make lint/check-scopes | ||
``` | ||
3. Decide whether each new scope is public (exposed in the catalog) or internal-only (handled by the catalog task). |
117 changes: 117 additions & 0 deletionsscripts/check-scopes/main.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package main | ||
import ( | ||
"bufio" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"regexp" | ||
"sort" | ||
"strings" | ||
"golang.org/x/xerrors" | ||
"github.com/coder/coder/v2/coderd/rbac/policy" | ||
) | ||
// defaultDumpPath is the repo-relative path to the generated schema dump. | ||
const defaultDumpPath = "coderd/database/dump.sql" | ||
var dumpPathFlag = flag.String("dump", defaultDumpPath, "path to dump.sql (defaults to coderd/database/dump.sql)") | ||
func main() { | ||
flag.Parse() | ||
want := expectedFromRBAC() | ||
have, err := enumValuesFromDump(*dumpPathFlag) | ||
if err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "check-scopes: error reading dump: %v\n", err) | ||
os.Exit(2) | ||
} | ||
// Compute missing: want - have | ||
var missing []string | ||
for k := range want { | ||
if _, ok := have[k]; !ok { | ||
missing = append(missing, k) | ||
} | ||
} | ||
sort.Strings(missing) | ||
if len(missing) == 0 { | ||
_, _ = fmt.Println("check-scopes: OK — all RBAC <resource>:<action> values exist in api_key_scope enum") | ||
return | ||
} | ||
_, _ = fmt.Fprintln(os.Stderr, "check-scopes: missing enum values:") | ||
for _, m := range missing { | ||
_, _ = fmt.Fprintf(os.Stderr, " - %s\n", m) | ||
} | ||
_, _ = fmt.Fprintln(os.Stderr) | ||
_, _ = fmt.Fprintln(os.Stderr, "To fix: add a DB migration extending the enum, e.g.:") | ||
for _, m := range missing { | ||
_, _ = fmt.Fprintf(os.Stderr, " ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS '%s';\n", m) | ||
} | ||
_, _ = fmt.Fprintln(os.Stderr) | ||
_, _ = fmt.Fprintln(os.Stderr, "Also decide if each new scope is public (exposed in the catalog) or internal-only (catalog task).") | ||
os.Exit(1) | ||
} | ||
// expectedFromRBAC returns the set of <resource>:<action> pairs derived from RBACPermissions. | ||
func expectedFromRBAC() map[string]struct{} { | ||
want := make(map[string]struct{}) | ||
for resource, def := range policy.RBACPermissions { | ||
if resource == policy.WildcardSymbol { | ||
// Ignore wildcard entry; it has no concrete <resource>:<action> pairs. | ||
continue | ||
} | ||
for action := range def.Actions { | ||
key := resource + ":" + string(action) | ||
want[key] = struct{}{} | ||
} | ||
} | ||
return want | ||
} | ||
// enumValuesFromDump parses dump.sql and extracts all literals from the | ||
// `CREATE TYPE api_key_scope AS ENUM (...)` block. | ||
func enumValuesFromDump(path string) (map[string]struct{}, error) { | ||
f, err := os.Open(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
const enumHead = "CREATE TYPE api_key_scope AS ENUM (" | ||
litRe := regexp.MustCompile(`'([^']+)'`) | ||
values := make(map[string]struct{}) | ||
inEnum := false | ||
s := bufio.NewScanner(f) | ||
for s.Scan() { | ||
line := strings.TrimSpace(s.Text()) | ||
if !inEnum { | ||
if strings.Contains(line, enumHead) { | ||
inEnum = true | ||
} | ||
continue | ||
} | ||
if strings.HasPrefix(line, ");") { | ||
// End of enum block | ||
return values, nil | ||
} | ||
// Collect single-quoted literals on this line. | ||
for _, m := range litRe.FindAllStringSubmatch(line, -1) { | ||
if len(m) > 1 { | ||
values[m[1]] = struct{}{} | ||
} | ||
} | ||
} | ||
if err := s.Err(); err != nil { | ||
return nil, err | ||
} | ||
if !inEnum { | ||
return nil, xerrors.New("api_key_scope enum block not found in dump") | ||
} | ||
return values, nil | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.