-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
prepare.go
167 lines (134 loc) · 3.27 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package core
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strconv"
"strings"
"github.com/dosco/graphjin/core/internal/allow"
"github.com/dosco/graphjin/core/internal/graph"
"github.com/dosco/graphjin/core/internal/qcode"
)
type queryReq struct {
op qcode.QType
ns string
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) `)
switch gj.dbtype {
case "mysql":
io.WriteString(w, `ELSE 'anon' END) FROM (VALUES ROW(1)) AS _sg_auth_filler LIMIT 1; `)
default:
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
if gj.conf.DisableAllowList {
gj.allowList, err = allow.NewReadOnly(gj.fs)
} else {
gj.allowList, err = allow.New(allow.Config{Log: gj.log}, gj.fs)
}
if err != nil {
return fmt.Errorf("failed to initialize allow list: %w", err)
}
gj.queries = make(map[string]*queryComp)
list, err := gj.allowList.Load()
if err != nil {
return err
}
for _, item := range list {
if item.Query == "" {
continue
}
q := strings.TrimSpace(item.Query)
h, err := graph.FastParse(q)
if err != nil {
return err
}
qr := queryReq{
op: qcode.GetQType(h.Type),
name: h.Name,
query: []byte(q),
vars: []byte(item.Vars),
}
ov := item.Metadata.Order.Var
qk := gj.generateQueryKeys(item)
for _, v := range qk {
qc := &queryComp{qr: qr}
if ov != "" {
qc.qr.order = [2]string{ov, strconv.Quote(v.val)}
}
gj.queries[v.key] = qc
}
}
return nil
}
type queryKey struct {
key string
val string
}
func (gj *graphjin) generateQueryKeys(item allow.Item) []queryKey {
var qk []queryKey
for roleName := range gj.roles {
k1 := (item.Namespace + item.Name + roleName)
qk = append(qk, queryKey{key: k1})
for _, v := range item.Metadata.Order.Values {
k2 := k1 + v
qk = append(qk, queryKey{key: k2, val: v})
}
}
return qk
}
func (gj *graphjin) getQuery(qr queryReq, role string, vm map[string]json.RawMessage) (*queryComp, error) {
qk := (qr.ns + qr.name + role)
qc, ok := gj.queries[qk]
if !ok {
return nil, ErrNotFound
}
ov := qc.qr.order[0]
if ov == "" {
return qc, nil
}
v, ok := vm[ov]
if !ok || v[0] != '"' || len(v) == 2 {
return nil, fmt.Errorf("required variable not set: %s", ov)
}
oval := string(v[1:(len(v) - 1)])
qc, ok = gj.queries[(qk + oval)]
if !ok {
return nil, fmt.Errorf("invalid value for variable (%s): %s", ov, oval)
}
return qc, nil
}