|
| 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 | +} |