Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc00de78

Browse files
committed
feat: build-time DB enum check for api key scopes completness
1 parent1aba733 commitc00de78

File tree

3 files changed

+166
-1
lines changed

3 files changed

+166
-1
lines changed

‎Makefile‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ endif
561561

562562
# Note: we don't run zizmor in the lint target because it takes a while. CI
563563
# runs it explicitly.
564-
lint: lint/shellcheck lint/go lint/ts lint/examples lint/helm lint/site-icons lint/markdown lint/actions/actionlint
564+
lint: lint/shellcheck lint/go lint/ts lint/examples lint/helm lint/site-icons lint/markdown lint/actions/actionlint lint/check-scopes
565565
.PHONY: lint
566566

567567
lint/site-icons:
@@ -614,6 +614,11 @@ lint/actions/zizmor:
614614
.
615615
.PHONY: lint/actions/zizmor
616616

617+
# Verify api_key_scope enum contains all RBAC <resource>:<action> values.
618+
lint/check-scopes: coderd/database/dump.sql
619+
go run ./scripts/check-scopes
620+
.PHONY: lint/check-scopes
621+
617622
# All files generated by the database should be added here, and this can be used
618623
# as a target for jobs that need to run after the database is generated.
619624
DB_GEN_FILES :=\

‎scripts/check-scopes/README.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#check-scopes
2+
3+
Validates that the DB enum`api_key_scope` contains every`<resource>:<action>` derived from`coderd/rbac/policy/RBACPermissions`.
4+
5+
- Exits 0 when all scopes are present in`coderd/database/dump.sql`.
6+
- Exits 1 and prints missing values with suggested`ALTER TYPE` statements otherwise.
7+
8+
##Usage
9+
10+
Ensure the schema dump is up-to-date, then run the check:
11+
12+
```sh
13+
make -B gen/db# forces DB dump regeneration
14+
make lint/check-scopes
15+
```
16+
17+
Or directly:
18+
19+
```sh
20+
go run ./tools/check-scopes
21+
```
22+
23+
Optional flags:
24+
25+
-`-dump path` — override path to`dump.sql` (default`coderd/database/dump.sql`).
26+
27+
##Remediation
28+
29+
When the tool reports missing values:
30+
31+
1. Create a DB migration extending the enum, e.g.:
32+
33+
```sql
34+
ALTERTYPE api_key_scope ADD VALUE IF NOT EXISTS'template:view_insights';
35+
```
36+
37+
2. Regenerate and re-run:
38+
39+
```sh
40+
make -B gen/db&& make check-scopes
41+
```
42+
43+
3. Decide whether each new scope is public (exposed in the catalog) or internal-only (handled by the catalog task).

‎scripts/check-scopes/main.go‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"flag"
6+
"fmt"
7+
"os"
8+
"regexp"
9+
"sort"
10+
"strings"
11+
12+
"golang.org/x/xerrors"
13+
14+
"github.com/coder/coder/v2/coderd/rbac/policy"
15+
)
16+
17+
// defaultDumpPath is the repo-relative path to the generated schema dump.
18+
constdefaultDumpPath="coderd/database/dump.sql"
19+
20+
vardumpPathFlag=flag.String("dump",defaultDumpPath,"path to dump.sql (defaults to coderd/database/dump.sql)")
21+
22+
funcmain() {
23+
flag.Parse()
24+
25+
want:=expectedFromRBAC()
26+
have,err:=enumValuesFromDump(*dumpPathFlag)
27+
iferr!=nil {
28+
_,_=fmt.Fprintf(os.Stderr,"check-scopes: error reading dump: %v\n",err)
29+
os.Exit(2)
30+
}
31+
32+
// Compute missing: want - have
33+
varmissing []string
34+
fork:=rangewant {
35+
if_,ok:=have[k];!ok {
36+
missing=append(missing,k)
37+
}
38+
}
39+
sort.Strings(missing)
40+
41+
iflen(missing)==0 {
42+
_,_=fmt.Println("check-scopes: OK — all RBAC <resource>:<action> values exist in api_key_scope enum")
43+
return
44+
}
45+
46+
_,_=fmt.Fprintln(os.Stderr,"check-scopes: missing enum values:")
47+
for_,m:=rangemissing {
48+
_,_=fmt.Fprintf(os.Stderr," - %s\n",m)
49+
}
50+
_,_=fmt.Fprintln(os.Stderr)
51+
_,_=fmt.Fprintln(os.Stderr,"To fix: add a DB migration extending the enum, e.g.:")
52+
for_,m:=rangemissing {
53+
_,_=fmt.Fprintf(os.Stderr," ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS '%s';\n",m)
54+
}
55+
_,_=fmt.Fprintln(os.Stderr)
56+
_,_=fmt.Fprintln(os.Stderr,"Also decide if each new scope is public (exposed in the catalog) or internal-only (catalog task).")
57+
os.Exit(1)
58+
}
59+
60+
// expectedFromRBAC returns the set of <resource>:<action> pairs derived from RBACPermissions.
61+
funcexpectedFromRBAC()map[string]struct{} {
62+
want:=make(map[string]struct{})
63+
forresource,def:=rangepolicy.RBACPermissions {
64+
ifresource==policy.WildcardSymbol {
65+
// Ignore wildcard entry; it has no concrete <resource>:<action> pairs.
66+
continue
67+
}
68+
foraction:=rangedef.Actions {
69+
key:=resource+":"+string(action)
70+
want[key]=struct{}{}
71+
}
72+
}
73+
returnwant
74+
}
75+
76+
// enumValuesFromDump parses dump.sql and extracts all literals from the
77+
// `CREATE TYPE api_key_scope AS ENUM (...)` block.
78+
funcenumValuesFromDump(pathstring) (map[string]struct{},error) {
79+
f,err:=os.Open(path)
80+
iferr!=nil {
81+
returnnil,err
82+
}
83+
deferf.Close()
84+
85+
constenumHead="CREATE TYPE api_key_scope AS ENUM ("
86+
litRe:=regexp.MustCompile(`'([^']+)'`)
87+
88+
values:=make(map[string]struct{})
89+
inEnum:=false
90+
s:=bufio.NewScanner(f)
91+
fors.Scan() {
92+
line:=strings.TrimSpace(s.Text())
93+
if!inEnum {
94+
ifstrings.Contains(line,enumHead) {
95+
inEnum=true
96+
}
97+
continue
98+
}
99+
ifstrings.HasPrefix(line,");") {
100+
// End of enum block
101+
returnvalues,nil
102+
}
103+
// Collect single-quoted literals on this line.
104+
for_,m:=rangelitRe.FindAllStringSubmatch(line,-1) {
105+
iflen(m)>1 {
106+
values[m[1]]=struct{}{}
107+
}
108+
}
109+
}
110+
iferr:=s.Err();err!=nil {
111+
returnnil,err
112+
}
113+
if!inEnum {
114+
returnnil,xerrors.New("api_key_scope enum block not found in dump")
115+
}
116+
returnvalues,nil
117+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp