2020-04-10 08:27:43 +02:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-05-03 22:08:17 +02:00
|
|
|
"crypto/sha256"
|
2020-04-10 08:27:43 +02:00
|
|
|
"database/sql"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/dosco/super-graph/core/internal/allow"
|
|
|
|
"github.com/dosco/super-graph/core/internal/qcode"
|
|
|
|
)
|
|
|
|
|
|
|
|
type preparedItem struct {
|
|
|
|
sd *sql.Stmt
|
|
|
|
st stmt
|
|
|
|
roleArg bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sg *SuperGraph) initPrepared() error {
|
|
|
|
ct := context.Background()
|
|
|
|
|
|
|
|
if sg.allowList.IsPersist() {
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-11 08:45:06 +02:00
|
|
|
sg.prepared = make(map[string]*preparedItem)
|
2020-04-10 08:27:43 +02:00
|
|
|
|
|
|
|
tx, err := sg.db.BeginTx(ct, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback() //nolint: errcheck
|
|
|
|
|
|
|
|
if err = sg.prepareRoleStmt(tx); err != nil {
|
|
|
|
return fmt.Errorf("prepareRoleStmt: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
success := 0
|
|
|
|
|
|
|
|
list, err := sg.allowList.Load()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range list {
|
|
|
|
if len(v.Query) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err := sg.prepareStmt(v)
|
2020-04-19 18:54:37 +02:00
|
|
|
if err != nil {
|
|
|
|
sg.log.Printf("WRN %s: %v", v.Name, err)
|
|
|
|
} else {
|
2020-04-10 08:27:43 +02:00
|
|
|
success++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 18:54:37 +02:00
|
|
|
sg.log.Printf("INF allow list: prepared %d / %d queries", success, len(list))
|
2020-04-10 08:27:43 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sg *SuperGraph) prepareStmt(item allow.Item) error {
|
|
|
|
query := item.Query
|
|
|
|
qb := []byte(query)
|
|
|
|
vars := item.Vars
|
|
|
|
|
|
|
|
qt := qcode.GetQType(query)
|
|
|
|
ct := context.Background()
|
|
|
|
switch qt {
|
|
|
|
case qcode.QTQuery:
|
|
|
|
var stmts1 []stmt
|
|
|
|
var err error
|
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
if sg.abacEnabled {
|
2020-04-10 08:27:43 +02:00
|
|
|
stmts1, err = sg.buildMultiStmt(qb, vars)
|
|
|
|
} else {
|
|
|
|
stmts1, err = sg.buildRoleStmt(qb, vars, "user")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
//logger.Debug().Msgf("Prepared statement 'query %s' (user)", item.Name)
|
|
|
|
|
2020-04-19 18:54:37 +02:00
|
|
|
err = sg.prepare(ct, stmts1, stmtHash(item.Name, "user"))
|
2020-04-10 08:27:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
if sg.anonExists {
|
2020-04-10 08:27:43 +02:00
|
|
|
// logger.Debug().Msgf("Prepared statement 'query %s' (anon)", item.Name)
|
|
|
|
|
|
|
|
stmts2, err := sg.buildRoleStmt(qb, vars, "anon")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-19 18:54:37 +02:00
|
|
|
err = sg.prepare(ct, stmts2, stmtHash(item.Name, "anon"))
|
2020-04-10 08:27:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case qcode.QTMutation:
|
|
|
|
for _, role := range sg.conf.Roles {
|
|
|
|
// logger.Debug().Msgf("Prepared statement 'mutation %s' (%s)", item.Name, role.Name)
|
|
|
|
|
|
|
|
stmts, err := sg.buildRoleStmt(qb, vars, role.Name)
|
|
|
|
if err != nil {
|
2020-04-19 18:54:37 +02:00
|
|
|
return err
|
2020-04-10 08:27:43 +02:00
|
|
|
}
|
|
|
|
|
2020-04-19 18:54:37 +02:00
|
|
|
err = sg.prepare(ct, stmts, stmtHash(item.Name, role.Name))
|
2020-04-10 08:27:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-19 18:54:37 +02:00
|
|
|
func (sg *SuperGraph) prepare(ct context.Context, st []stmt, key string) error {
|
2020-05-27 01:41:28 +02:00
|
|
|
sd, err := sg.db.PrepareContext(ct, st[0].sql)
|
2020-04-10 08:27:43 +02:00
|
|
|
if err != nil {
|
2020-05-27 01:41:28 +02:00
|
|
|
return fmt.Errorf("prepare failed: %v: %s", err, st[0].sql)
|
2020-04-10 08:27:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sg.prepared[key] = &preparedItem{
|
|
|
|
sd: sd,
|
|
|
|
st: st[0],
|
|
|
|
roleArg: len(st) > 1,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// nolint: errcheck
|
|
|
|
func (sg *SuperGraph) prepareRoleStmt(tx *sql.Tx) error {
|
|
|
|
var err error
|
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
if !sg.abacEnabled {
|
2020-04-10 08:27:43 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
rq := strings.ReplaceAll(sg.conf.RolesQuery, "$user_id", "$1")
|
2020-04-10 08:27:43 +02:00
|
|
|
w := &bytes.Buffer{}
|
|
|
|
|
|
|
|
io.WriteString(w, `SELECT (CASE WHEN EXISTS (`)
|
2020-05-27 01:41:28 +02:00
|
|
|
io.WriteString(w, rq)
|
2020-04-10 08:27:43 +02:00
|
|
|
io.WriteString(w, `) THEN `)
|
|
|
|
|
|
|
|
io.WriteString(w, `(SELECT (CASE`)
|
|
|
|
for _, role := range sg.conf.Roles {
|
|
|
|
if len(role.Match) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
io.WriteString(w, ` WHEN `)
|
|
|
|
io.WriteString(w, role.Match)
|
|
|
|
io.WriteString(w, ` THEN '`)
|
|
|
|
io.WriteString(w, role.Name)
|
|
|
|
io.WriteString(w, `'`)
|
|
|
|
}
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
io.WriteString(w, ` ELSE $2 END) FROM (`)
|
2020-04-10 08:27:43 +02:00
|
|
|
io.WriteString(w, sg.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; `)
|
|
|
|
|
2020-05-27 01:41:28 +02:00
|
|
|
sg.getRole, err = tx.Prepare(w.String())
|
2020-04-10 08:27:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sg *SuperGraph) initAllowList() error {
|
|
|
|
var ac allow.Config
|
|
|
|
var err error
|
|
|
|
|
2020-05-31 08:34:28 +02:00
|
|
|
if sg.conf.AllowListFile == "" {
|
|
|
|
sg.conf.AllowListFile = "allow.list"
|
2020-04-11 08:45:06 +02:00
|
|
|
}
|
|
|
|
|
2020-04-19 18:54:37 +02:00
|
|
|
// When list is not eabled it is still created and
|
|
|
|
// and new queries are saved to it.
|
2020-04-18 23:42:17 +02:00
|
|
|
if !sg.conf.UseAllowList {
|
2020-05-31 08:34:28 +02:00
|
|
|
ac = allow.Config{CreateIfNotExists: true, Persist: true, Log: sg.log}
|
2020-04-10 08:27:43 +02:00
|
|
|
}
|
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
sg.allowList, err = allow.New(sg.conf.AllowListFile, ac)
|
2020-04-10 08:27:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to initialize allow list: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// nolint: errcheck
|
|
|
|
func stmtHash(name string, role string) string {
|
2020-05-03 22:08:17 +02:00
|
|
|
h := sha256.New()
|
2020-04-10 08:27:43 +02:00
|
|
|
io.WriteString(h, strings.ToLower(name))
|
|
|
|
io.WriteString(h, role)
|
|
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
|
|
}
|