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