-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
prepare.go
139 lines (110 loc) · 2.71 KB
/
prepare.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package core
import (
"bytes"
"fmt"
"io"
"strconv"
"strings"
"github.com/dosco/graphjin/core/internal/allow"
"github.com/dosco/graphjin/core/internal/qcode"
)
type queryReq struct {
op qcode.QType
name string
query []byte
vars []byte
order [2]string
}
// nolint: errcheck
func (gj *GraphJin) prepareRoleStmt() error {
if !gj.abacEnabled {
return nil
}
if !strings.Contains(gj.conf.RolesQuery, "$user_id") {
return fmt.Errorf("roles_query: $user_id variable missing")
}
w := &bytes.Buffer{}
io.WriteString(w, `SELECT (CASE WHEN EXISTS (`)
gj.pc.RenderVar(w, &gj.roleStmtMD, gj.conf.RolesQuery)
io.WriteString(w, `) THEN `)
io.WriteString(w, `(SELECT (CASE`)
for roleName, role := range gj.roles {
if role.Match == "" {
continue
}
io.WriteString(w, ` WHEN `)
io.WriteString(w, role.Match)
io.WriteString(w, ` THEN '`)
io.WriteString(w, roleName)
io.WriteString(w, `'`)
}
io.WriteString(w, ` ELSE 'user' END) FROM (`)
gj.pc.RenderVar(w, &gj.roleStmtMD, gj.conf.RolesQuery)
io.WriteString(w, `) AS "_sg_auth_roles_query" LIMIT 1) `)
io.WriteString(w, `ELSE 'anon' END) FROM (VALUES (1)) AS "_sg_auth_filler" LIMIT 1; `)
gj.roleStmt = w.String()
return nil
}
func (gj *GraphJin) initAllowList() error {
var err error
var ConfigPath string
if gj.conf.DisableAllowList {
return nil
}
if gj.conf.AllowListPath != "" {
ConfigPath = gj.conf.AllowListPath
} else {
ConfigPath = gj.conf.ConfigPath
}
gj.allowList, err = allow.New(ConfigPath, allow.Config{
Log: gj.log,
})
if err != nil {
return fmt.Errorf("failed to initialize allow list: %w", err)
}
// return if allow list diabled or not prod
if gj.allowList == nil || !gj.prod {
return nil
}
gj.queries = make(map[string]*queryComp)
list, err := gj.allowList.Load()
if err != nil {
return err
}
for _, item := range list {
if item.Query == "" {
continue
}
qt, _ := qcode.GetQType(item.Query)
qk := gj.getQueryKeys(item)
for _, v := range qk {
qc := &queryComp{qr: queryReq{
op: qt,
name: item.Name,
query: []byte(item.Query),
vars: []byte(item.Vars),
}}
if item.Metadata.Order.Var != "" {
qc.qr.order = [2]string{item.Metadata.Order.Var, strconv.Quote(v.val)}
}
gj.queries[v.key] = qc
}
op, _ := qcode.GetQType(item.Query)
gj.apq.Set(item.Name, apqInfo{op: op, name: item.Name})
}
return nil
}
type queryKey struct {
key string
val string
}
func (gj *GraphJin) getQueryKeys(item allow.Item) []queryKey {
var qk []queryKey
for roleName := range gj.roles {
qk = append(qk, queryKey{key: (item.Name + roleName)})
for _, v := range item.Metadata.Order.Values {
qk = append(qk, queryKey{key: (item.Name + roleName + v), val: v})
}
}
return qk
}