2020-04-10 08:27:43 +02:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-06-03 07:03:12 +02:00
|
|
|
"hash/maphash"
|
2020-04-10 08:27:43 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/dosco/super-graph/core/internal/psql"
|
|
|
|
"github.com/dosco/super-graph/core/internal/qcode"
|
|
|
|
)
|
|
|
|
|
2020-05-20 06:03:05 +02:00
|
|
|
type OpType int
|
|
|
|
|
2020-04-25 02:45:03 +02:00
|
|
|
const (
|
2020-05-20 06:03:05 +02:00
|
|
|
OpQuery OpType = iota
|
2020-04-25 02:45:03 +02:00
|
|
|
OpMutation
|
|
|
|
)
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
sg *SuperGraph
|
|
|
|
query string
|
|
|
|
vars json.RawMessage
|
|
|
|
role string
|
|
|
|
res Result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sg *SuperGraph) initCompilers() error {
|
2020-04-23 07:42:16 +02:00
|
|
|
var err error
|
2020-05-20 06:03:05 +02:00
|
|
|
var schema string
|
|
|
|
|
|
|
|
if sg.conf.DBSchema == "" {
|
|
|
|
schema = "public"
|
|
|
|
} else {
|
|
|
|
schema = sg.conf.DBSchema
|
|
|
|
}
|
2020-04-23 07:42:16 +02:00
|
|
|
|
|
|
|
// If sg.di is not null then it's probably set
|
|
|
|
// for tests
|
|
|
|
if sg.dbinfo == nil {
|
2020-05-20 06:03:05 +02:00
|
|
|
sg.dbinfo, err = psql.GetDBInfo(sg.db, schema)
|
2020-04-23 07:42:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-10 08:27:43 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 06:03:05 +02:00
|
|
|
if len(sg.dbinfo.Tables) == 0 {
|
|
|
|
return fmt.Errorf("no tables found in database (schema: %s)", schema)
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:42:16 +02:00
|
|
|
if err = addTables(sg.conf, sg.dbinfo); err != nil {
|
2020-04-10 08:27:43 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:42:16 +02:00
|
|
|
if err = addForeignKeys(sg.conf, sg.dbinfo); err != nil {
|
2020-04-10 08:27:43 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:42:16 +02:00
|
|
|
sg.schema, err = psql.NewDBSchema(sg.dbinfo, getDBTableAliases(sg.conf))
|
2020-04-10 08:27:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sg.qc, err = qcode.NewCompiler(qcode.Config{
|
2020-05-28 06:07:01 +02:00
|
|
|
DefaultBlock: sg.conf.DefaultBlock,
|
|
|
|
Blocklist: sg.conf.Blocklist,
|
2020-04-10 08:27:43 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := addRoles(sg.conf, sg.qc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sg.pc = psql.NewCompiler(psql.Config{
|
|
|
|
Schema: sg.schema,
|
|
|
|
Vars: sg.conf.Vars,
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *scontext) execQuery() ([]byte, error) {
|
|
|
|
var data []byte
|
2020-04-13 02:35:31 +02:00
|
|
|
var st *stmt
|
2020-04-10 08:27:43 +02:00
|
|
|
var err error
|
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
if c.sg.conf.UseAllowList {
|
2020-04-13 02:35:31 +02:00
|
|
|
data, st, err = c.resolvePreparedSQL()
|
2020-04-10 08:27:43 +02:00
|
|
|
} else {
|
2020-04-13 02:35:31 +02:00
|
|
|
data, st, err = c.resolveSQL()
|
2020-04-19 18:54:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-04-10 08:27:43 +02:00
|
|
|
}
|
|
|
|
|
2020-06-05 03:55:52 +02:00
|
|
|
if len(data) == 0 || st.md.Skipped() == 0 {
|
2020-04-13 02:35:31 +02:00
|
|
|
return data, nil
|
|
|
|
}
|
2020-04-10 08:27:43 +02:00
|
|
|
|
2020-04-13 02:35:31 +02:00
|
|
|
// return c.sg.execRemoteJoin(st, data, c.req.hdr)
|
|
|
|
return c.sg.execRemoteJoin(st, data, nil)
|
2020-04-10 08:27:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *scontext) resolvePreparedSQL() ([]byte, *stmt, error) {
|
|
|
|
var tx *sql.Tx
|
|
|
|
var err error
|
|
|
|
|
|
|
|
mutation := (c.res.op == qcode.QTMutation)
|
2020-04-11 08:45:06 +02:00
|
|
|
useRoleQuery := c.sg.abacEnabled && mutation
|
2020-04-10 08:27:43 +02:00
|
|
|
useTx := useRoleQuery || c.sg.conf.SetUserID
|
|
|
|
|
|
|
|
if useTx {
|
|
|
|
if tx, err = c.sg.db.BeginTx(c, nil); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback() //nolint: errcheck
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.sg.conf.SetUserID {
|
|
|
|
if err := setLocalUserID(c, tx); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var role string
|
|
|
|
|
|
|
|
if useRoleQuery {
|
|
|
|
if role, err = c.executeRoleQuery(tx); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if v := c.Value(UserRoleKey); v != nil {
|
|
|
|
role = v.(string)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
role = c.role
|
|
|
|
}
|
|
|
|
|
|
|
|
c.res.role = role
|
|
|
|
|
2020-06-03 07:03:12 +02:00
|
|
|
h := maphash.Hash{}
|
|
|
|
h.SetSeed(c.sg.hashSeed)
|
2020-06-10 01:06:16 +02:00
|
|
|
id := queryID(&h, c.res.name, role)
|
2020-06-03 07:03:12 +02:00
|
|
|
|
2020-06-10 01:06:16 +02:00
|
|
|
q, ok := c.sg.queries[id]
|
2020-04-10 08:27:43 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, nil, errNotFound
|
|
|
|
}
|
2020-06-03 07:03:12 +02:00
|
|
|
|
|
|
|
if q.sd == nil {
|
2020-06-10 01:06:16 +02:00
|
|
|
q.Do(func() { c.sg.prepare(q, role) })
|
2020-06-03 07:03:12 +02:00
|
|
|
|
|
|
|
if q.err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.res.sql = q.st.sql
|
2020-04-10 08:27:43 +02:00
|
|
|
|
|
|
|
var root []byte
|
|
|
|
var row *sql.Row
|
|
|
|
|
2020-06-03 07:03:12 +02:00
|
|
|
varsList, err := c.argList(q.st.md)
|
2020-04-10 08:27:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if useTx {
|
2020-06-03 07:03:12 +02:00
|
|
|
row = tx.Stmt(q.sd).QueryRow(varsList...)
|
2020-04-10 08:27:43 +02:00
|
|
|
} else {
|
2020-06-03 07:03:12 +02:00
|
|
|
row = q.sd.QueryRow(varsList...)
|
2020-04-10 08:27:43 +02:00
|
|
|
}
|
|
|
|
|
2020-06-03 07:03:12 +02:00
|
|
|
if q.roleArg {
|
2020-04-10 08:27:43 +02:00
|
|
|
err = row.Scan(&role, &root)
|
|
|
|
} else {
|
|
|
|
err = row.Scan(&root)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.role = role
|
|
|
|
|
|
|
|
if useTx {
|
|
|
|
if err := tx.Commit(); err != nil {
|
2020-06-03 07:03:12 +02:00
|
|
|
return nil, nil, q.err
|
2020-04-10 08:27:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 07:03:12 +02:00
|
|
|
if root, err = c.sg.encryptCursor(q.st.qc, root); err != nil {
|
2020-04-10 08:27:43 +02:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-03 07:03:12 +02:00
|
|
|
return root, &q.st, nil
|
2020-04-10 08:27:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *scontext) resolveSQL() ([]byte, *stmt, error) {
|
|
|
|
var tx *sql.Tx
|
|
|
|
var err error
|
|
|
|
|
|
|
|
mutation := (c.res.op == qcode.QTMutation)
|
2020-04-11 08:45:06 +02:00
|
|
|
useRoleQuery := c.sg.abacEnabled && mutation
|
2020-04-10 08:27:43 +02:00
|
|
|
useTx := useRoleQuery || c.sg.conf.SetUserID
|
|
|
|
|
|
|
|
if useTx {
|
|
|
|
if tx, err = c.sg.db.BeginTx(c, nil); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback() //nolint: errcheck
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.sg.conf.SetUserID {
|
|
|
|
if err := setLocalUserID(c, tx); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if useRoleQuery {
|
|
|
|
if c.role, err = c.executeRoleQuery(tx); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if v := c.Value(UserRoleKey); v != nil {
|
|
|
|
c.role = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
stmts, err := c.sg.buildStmt(c.res.op, []byte(c.query), c.vars, c.role)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
st := &stmts[0]
|
2020-05-27 01:41:28 +02:00
|
|
|
c.res.sql = st.sql
|
2020-04-10 08:27:43 +02:00
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
varList, err := c.argList(st.md)
|
2020-04-10 08:27:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-05-27 01:41:28 +02:00
|
|
|
// finalSQL := buf.String()
|
|
|
|
|
|
|
|
////
|
|
|
|
|
|
|
|
// _, err = t.ExecuteFunc(buf, c.argMap(st.md))
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, nil, err
|
|
|
|
// }
|
|
|
|
// finalSQL := buf.String()
|
|
|
|
|
|
|
|
/////
|
2020-04-10 08:27:43 +02:00
|
|
|
|
|
|
|
// var stime time.Time
|
|
|
|
|
|
|
|
// if c.sg.conf.EnableTracing {
|
|
|
|
// stime = time.Now()
|
|
|
|
// }
|
|
|
|
|
|
|
|
var root []byte
|
|
|
|
var role string
|
|
|
|
var row *sql.Row
|
|
|
|
|
|
|
|
// defaultRole := c.role
|
|
|
|
|
|
|
|
if useTx {
|
2020-05-27 01:41:28 +02:00
|
|
|
row = tx.QueryRowContext(c, st.sql, varList...)
|
2020-04-10 08:27:43 +02:00
|
|
|
} else {
|
2020-05-27 01:41:28 +02:00
|
|
|
row = c.sg.db.QueryRowContext(c, st.sql, varList...)
|
2020-04-10 08:27:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(stmts) > 1 {
|
|
|
|
err = row.Scan(&role, &root)
|
|
|
|
} else {
|
|
|
|
err = row.Scan(&root)
|
|
|
|
}
|
|
|
|
|
2020-06-15 16:16:47 +02:00
|
|
|
if role == "" {
|
2020-04-10 08:27:43 +02:00
|
|
|
c.res.role = c.role
|
|
|
|
} else {
|
|
|
|
c.res.role = role
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if useTx {
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if root, err = c.sg.encryptCursor(st.qc, root); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.sg.allowList.IsPersist() {
|
|
|
|
if err := c.sg.allowList.Set(c.vars, c.query, ""); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(stmts) > 1 {
|
|
|
|
if st = findStmt(role, stmts); st == nil {
|
|
|
|
return nil, nil, fmt.Errorf("invalid role '%s' returned", role)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if c.sg.conf.EnableTracing {
|
|
|
|
// for _, id := range st.qc.Roots {
|
|
|
|
// c.addTrace(st.qc.Selects, id, stime)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
return root, st, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *scontext) executeRoleQuery(tx *sql.Tx) (string, error) {
|
|
|
|
userID := c.Value(UserIDKey)
|
|
|
|
|
|
|
|
if userID == nil {
|
|
|
|
return "anon", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var role string
|
|
|
|
row := c.sg.getRole.QueryRow(userID, c.role)
|
|
|
|
|
|
|
|
if err := row.Scan(&role); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return role, nil
|
|
|
|
}
|
|
|
|
|
2020-05-20 06:03:05 +02:00
|
|
|
func (r *Result) Operation() OpType {
|
2020-04-25 02:45:03 +02:00
|
|
|
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 {
|
2020-04-10 08:27:43 +02:00
|
|
|
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 findStmt(role string, stmts []stmt) *stmt {
|
|
|
|
for i := range stmts {
|
|
|
|
if stmts[i].role.Name != role {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return &stmts[i]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|