2020-04-10 08:27:43 +02:00
|
|
|
package core
|
2019-10-24 08:07:42 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2019-11-25 08:22:33 +01:00
|
|
|
"fmt"
|
2019-10-24 08:07:42 +02:00
|
|
|
"io"
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
"github.com/dosco/super-graph/core/internal/psql"
|
|
|
|
"github.com/dosco/super-graph/core/internal/qcode"
|
2019-10-24 08:07:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type stmt struct {
|
2020-04-11 08:45:06 +02:00
|
|
|
role *Role
|
2019-10-24 08:07:42 +02:00
|
|
|
qc *qcode.QCode
|
|
|
|
skipped uint32
|
|
|
|
sql string
|
|
|
|
}
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
func (sg *SuperGraph) buildStmt(qt qcode.QType, query, vars []byte, role string) ([]stmt, error) {
|
2019-11-25 08:22:33 +01:00
|
|
|
switch qt {
|
|
|
|
case qcode.QTMutation:
|
2020-04-10 08:27:43 +02:00
|
|
|
return sg.buildRoleStmt(query, vars, role)
|
2019-10-24 08:07:42 +02:00
|
|
|
|
2019-11-25 08:22:33 +01:00
|
|
|
case qcode.QTQuery:
|
2019-12-09 07:48:18 +01:00
|
|
|
if role == "anon" {
|
2020-04-10 08:27:43 +02:00
|
|
|
return sg.buildRoleStmt(query, vars, "anon")
|
2019-12-09 07:48:18 +01:00
|
|
|
}
|
2019-11-25 08:22:33 +01:00
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
if sg.abacEnabled {
|
2020-04-10 08:27:43 +02:00
|
|
|
return sg.buildMultiStmt(query, vars)
|
2019-11-25 08:22:33 +01:00
|
|
|
}
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
return sg.buildRoleStmt(query, vars, "user")
|
2019-12-09 07:48:18 +01:00
|
|
|
|
2019-11-25 08:22:33 +01:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown query type '%d'", qt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
func (sg *SuperGraph) buildRoleStmt(query, vars []byte, role string) ([]stmt, error) {
|
2020-04-11 08:45:06 +02:00
|
|
|
ro, ok := sg.roles[role]
|
|
|
|
if !ok {
|
2020-04-10 08:27:43 +02:00
|
|
|
return nil, fmt.Errorf(`roles '%s' not defined in c.sg.config`, role)
|
2019-11-25 08:22:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var vm map[string]json.RawMessage
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if len(vars) != 0 {
|
|
|
|
if err := json.Unmarshal(vars, &vm); err != nil {
|
2019-10-24 08:07:42 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
qc, err := sg.qc.Compile(query, ro.Name)
|
2019-11-25 08:22:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-24 08:07:42 +02:00
|
|
|
|
2019-11-25 08:22:33 +01:00
|
|
|
stmts := []stmt{stmt{role: ro, qc: qc}}
|
|
|
|
w := &bytes.Buffer{}
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
skipped, err := sg.pc.Compile(qc, w, psql.Variables(vm))
|
2019-10-24 08:07:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-25 08:22:33 +01:00
|
|
|
stmts[0].skipped = skipped
|
|
|
|
stmts[0].sql = w.String()
|
|
|
|
|
|
|
|
return stmts, nil
|
|
|
|
}
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
func (sg *SuperGraph) buildMultiStmt(query, vars []byte) ([]stmt, error) {
|
2019-11-25 08:22:33 +01:00
|
|
|
var vm map[string]json.RawMessage
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if len(vars) != 0 {
|
|
|
|
if err := json.Unmarshal(vars, &vm); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
if len(sg.conf.RolesQuery) == 0 {
|
2020-03-06 11:08:54 +01:00
|
|
|
return nil, errors.New("roles_query not defined")
|
2019-11-25 08:22:33 +01:00
|
|
|
}
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
stmts := make([]stmt, 0, len(sg.conf.Roles))
|
2019-10-24 08:07:42 +02:00
|
|
|
w := &bytes.Buffer{}
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
for i := 0; i < len(sg.conf.Roles); i++ {
|
|
|
|
role := &sg.conf.Roles[i]
|
2019-10-24 08:07:42 +02:00
|
|
|
|
2020-03-06 11:08:54 +01:00
|
|
|
// skip anon as it's not included in the combined multi-statement
|
2019-12-02 16:52:22 +01:00
|
|
|
if role.Name == "anon" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
qc, err := sg.qc.Compile(query, role.Name)
|
2019-11-07 08:37:24 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-24 08:07:42 +02:00
|
|
|
stmts = append(stmts, stmt{role: role, qc: qc})
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
skipped, err := sg.pc.Compile(qc, w, psql.Variables(vm))
|
2019-11-25 08:22:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-10-24 08:07:42 +02:00
|
|
|
}
|
2019-11-25 08:22:33 +01:00
|
|
|
|
|
|
|
s := &stmts[len(stmts)-1]
|
|
|
|
s.skipped = skipped
|
|
|
|
s.sql = w.String()
|
|
|
|
w.Reset()
|
2019-10-24 08:07:42 +02:00
|
|
|
}
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
sql, err := sg.renderUserQuery(stmts)
|
2019-11-25 08:22:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-10-24 08:07:42 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 08:22:33 +01:00
|
|
|
stmts[0].sql = sql
|
|
|
|
return stmts, nil
|
|
|
|
}
|
|
|
|
|
2019-11-28 07:25:46 +01:00
|
|
|
//nolint: errcheck
|
2020-04-10 08:27:43 +02:00
|
|
|
func (sg *SuperGraph) renderUserQuery(stmts []stmt) (string, error) {
|
2019-11-25 08:22:33 +01:00
|
|
|
w := &bytes.Buffer{}
|
|
|
|
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(w, `SELECT "_sg_auth_info"."role", (CASE "_sg_auth_info"."role" `)
|
|
|
|
|
|
|
|
for _, s := range stmts {
|
2019-11-25 08:22:33 +01:00
|
|
|
if len(s.role.Match) == 0 &&
|
|
|
|
s.role.Name != "user" && s.role.Name != "anon" {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(w, `WHEN '`)
|
|
|
|
io.WriteString(w, s.role.Name)
|
|
|
|
io.WriteString(w, `' THEN (`)
|
2019-12-02 16:52:22 +01:00
|
|
|
io.WriteString(w, s.sql)
|
2019-10-24 08:07:42 +02:00
|
|
|
io.WriteString(w, `) `)
|
|
|
|
}
|
|
|
|
|
2019-11-25 08:22:33 +01:00
|
|
|
io.WriteString(w, `END) FROM (SELECT (CASE WHEN EXISTS (`)
|
2020-04-10 08:27:43 +02:00
|
|
|
io.WriteString(w, sg.conf.RolesQuery)
|
2019-11-25 08:22:33 +01:00
|
|
|
io.WriteString(w, `) THEN `)
|
2019-10-24 08:07:42 +02:00
|
|
|
|
2019-11-25 08:22:33 +01:00
|
|
|
io.WriteString(w, `(SELECT (CASE`)
|
|
|
|
for _, s := range stmts {
|
|
|
|
if len(s.role.Match) == 0 {
|
|
|
|
continue
|
2019-10-24 08:07:42 +02:00
|
|
|
}
|
2019-11-25 08:22:33 +01:00
|
|
|
io.WriteString(w, ` WHEN `)
|
|
|
|
io.WriteString(w, s.role.Match)
|
|
|
|
io.WriteString(w, ` THEN '`)
|
|
|
|
io.WriteString(w, s.role.Name)
|
|
|
|
io.WriteString(w, `'`)
|
2019-10-24 08:07:42 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 08:22:33 +01:00
|
|
|
io.WriteString(w, ` ELSE 'user' END) FROM (`)
|
2020-04-10 08:27:43 +02:00
|
|
|
io.WriteString(w, sg.conf.RolesQuery)
|
2019-11-25 08:22:33 +01:00
|
|
|
io.WriteString(w, `) AS "_sg_auth_roles_query" LIMIT 1) `)
|
|
|
|
io.WriteString(w, `ELSE 'anon' END) FROM (VALUES (1)) AS "_sg_auth_filler") AS "_sg_auth_info"(role) LIMIT 1; `)
|
2019-10-24 08:07:42 +02:00
|
|
|
|
2019-11-25 08:22:33 +01:00
|
|
|
return w.String(), nil
|
2019-10-24 08:07:42 +02:00
|
|
|
}
|
2019-11-21 08:14:12 +01:00
|
|
|
|
2020-05-01 08:20:13 +02:00
|
|
|
// func (sg *SuperGraph) hasTablesWithConfig(qc *qcode.QCode, role *Role) bool {
|
|
|
|
// for _, id := range qc.Roots {
|
|
|
|
// t, err := sg.schema.GetTable(qc.Selects[id].Name)
|
|
|
|
// if err != nil {
|
|
|
|
// return false
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if r := role.GetTable(t.Name); r == nil {
|
|
|
|
// return false
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return true
|
|
|
|
// }
|