-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
core.go
344 lines (274 loc) · 6.55 KB
/
core.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package core
import (
"context"
"database/sql"
"fmt"
"strconv"
"time"
"github.com/dosco/graphjin/core/internal/psql"
"github.com/dosco/graphjin/core/internal/qcode"
"github.com/dosco/graphjin/core/internal/sdata"
)
type OpType int
const (
OpUnknown OpType = iota
OpQuery
OpSubscription
OpMutation
)
type extensions struct {
Tracing *trace `json:"tracing,omitempty"`
}
type trace struct {
Version int `json:"version"`
StartTime time.Time `json:"startTime"`
EndTime time.Time `json:"endTime"`
Duration time.Duration `json:"duration"`
Execution execution `json:"execution"`
}
type execution struct {
Resolvers []resolver `json:"resolvers"`
}
type resolver struct {
Path []string `json:"path"`
ParentType string `json:"parentType"`
FieldName string `json:"fieldName"`
ReturnType string `json:"returnType"`
StartOffset int `json:"startOffset"`
Duration time.Duration `json:"duration"`
}
type scontext struct {
context.Context
gj *GraphJin
op qcode.QType
rc *ReqConfig
name string
}
type qres struct {
q *cquery
data []byte
role string
}
func (gj *GraphJin) initSchema() error {
var err error
// If gj.di is not null then it's probably set
// for tests
if gj.dbinfo == nil {
gj.dbinfo, err = sdata.GetDBInfo(gj.db, gj.conf.DBType, gj.conf.Blocklist)
if err != nil {
return err
}
}
if len(gj.dbinfo.Tables) == 0 {
return fmt.Errorf("no tables found in database")
}
if err = addTables(gj.conf, gj.dbinfo); err != nil {
return err
}
if err = addForeignKeys(gj.conf, gj.dbinfo); err != nil {
return err
}
gj.schema, err = sdata.NewDBSchema(gj.dbinfo, getDBTableAliases(gj.conf))
return err
}
func (gj *GraphJin) initCompilers() error {
var err error
qcc := qcode.Config{
DefaultBlock: gj.conf.DefaultBlock,
DefaultLimit: gj.conf.DefaultLimit,
}
if gj.allowList != nil && gj.conf.EnforceAllowList {
qcc.FragmentFetcher = gj.allowList.FragmentFetcher()
}
gj.qc, err = qcode.NewCompiler(gj.schema, qcc)
if err != nil {
return err
}
if err := addRoles(gj.conf, gj.qc); err != nil {
return err
}
gj.pc = psql.NewCompiler(psql.Config{Vars: gj.conf.Vars})
return nil
}
func (c *scontext) execQuery(query string, vars []byte, role string) (qres, error) {
res, err := c.resolveSQL(query, vars, role)
if err != nil {
return res, err
}
if c.gj.conf.Debug {
c.debugLog(&res.q.st)
}
if len(res.data) == 0 || res.q.st.qc.Remotes == 0 {
return res, nil
}
return c.execRemoteJoin(res)
}
func (c *scontext) resolveSQL(query string, vars []byte, role string) (qres, error) {
var res qres
rq := rquery{op: c.op, name: c.name, query: []byte(query), vars: vars}
cq := &cquery{q: rq}
res.q = cq
res.role = role
conn, err := c.gj.db.Conn(c)
if err != nil {
return res, err
}
defer conn.Close()
if c.gj.conf.SetUserID {
if err := c.setLocalUserID(conn); err != nil {
return res, err
}
}
if v := c.Value(UserRoleKey); v != nil {
res.role = v.(string)
} else if c.gj.abacEnabled && c.op == qcode.QTMutation {
res.role, err = c.executeRoleQuery(conn)
}
if err != nil {
return res, err
}
if err = c.gj.compileQuery(cq, res.role); err != nil {
return res, err
}
args, err := c.gj.argList(c, cq.st.md, vars, c.rc)
if err != nil {
return res, err
}
// var stime time.Time
// if c.gj.conf.EnableTracing {
// stime = time.Now()
// }
row := conn.QueryRowContext(c, cq.st.sql, args.values...)
if cq.roleArg {
err = row.Scan(&res.role, &res.data)
} else {
err = row.Scan(&res.data)
}
if err == sql.ErrNoRows {
return res, err
} else if err != nil {
return res, err
}
cur, err := c.gj.encryptCursor(cq.st.qc, res.data)
if err != nil {
return res, err
}
res.data = cur.data
if c.gj.allowList != nil {
if err := c.gj.allowList.Set(vars, query); err != nil {
return res, err
}
}
// if len(stmts) > 1 {
// if st = findStmt(role, stmts); st == nil {
// return nil, nil, fmt.Errorf("invalid role '%s' returned", role)
// }
// }
// if c.gj.conf.EnableTracing {
// for _, id := range st.qc.Roots {
// c.addTrace(st.qc.Selects, id, stime)
// }
// }
return res, nil
}
func (c *scontext) executeRoleQuery(conn *sql.Conn) (string, error) {
var role string
var ar args
var err error
if c.Value(UserIDKey) == nil {
return "anon", nil
}
if ar, err = c.gj.roleQueryArgList(c); err != nil {
return "", err
}
err = conn.QueryRowContext(c, c.gj.roleStmt, ar.values...).Scan(&role)
return role, err
}
func (c *scontext) setLocalUserID(conn *sql.Conn) error {
var err error
if v := c.Value(UserIDKey); v == nil {
return nil
} else {
switch v1 := v.(type) {
case string:
_, err = conn.ExecContext(c, `SET SESSION "user.id" = '`+v1+`'`)
case int:
_, err = conn.ExecContext(c, `SET SESSION "user.id" = `+strconv.Itoa(v1))
}
}
return err
}
func (r *Result) Operation() OpType {
switch r.op {
case qcode.QTQuery:
return OpQuery
case qcode.QTMutation, qcode.QTInsert, qcode.QTUpdate, qcode.QTUpsert, qcode.QTDelete:
return OpMutation
default:
return -1
}
}
func (r *Result) OperationName() string {
return r.op.String()
}
func (r *Result) QueryName() string {
return r.name
}
func (r *Result) Role() string {
return r.role
}
func (r *Result) SQL() string {
return r.sql
}
// func (c *scontext) addTrace(sel []qcode.Select, id int32, st time.Time) {
// et := time.Now()
// du := et.Sub(st)
// if c.res.Extensions == nil {
// c.res.Extensions = &extensions{&trace{
// Version: 1,
// StartTime: st,
// Execution: execution{},
// }}
// }
// c.res.Extensions.Tracing.EndTime = et
// c.res.Extensions.Tracing.Duration = du
// n := 1
// for i := id; i != -1; i = sel[i].ParentID {
// n++
// }
// path := make([]string, n)
// n--
// for i := id; ; i = sel[i].ParentID {
// path[n] = sel[i].Name
// if sel[i].ParentID == -1 {
// break
// }
// n--
// }
// tr := resolver{
// Path: path,
// ParentType: "Query",
// FieldName: sel[id].Name,
// ReturnType: "object",
// StartOffset: 1,
// Duration: du,
// }
// c.res.Extensions.Tracing.Execution.Resolvers =
// append(c.res.Extensions.Tracing.Execution.Resolvers, tr)
// }
func (c *scontext) debugLog(st *stmt) {
for _, sel := range st.qc.Selects {
if sel.SkipRender == qcode.SkipTypeUserNeeded {
c.gj.log.Printf("INF field '%s' skipped as it requires $user_id", sel.FieldName)
}
}
}
// func findStmt(role string, stmts []stmt) *stmt {
// for i := range stmts {
// if stmts[i].role.Name != role {
// continue
// }
// return &stmts[i]
// }
// return nil
// }