2020-04-10 08:27:43 +02:00
|
|
|
package core
|
2019-12-09 07:48:18 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-04-11 08:45:06 +02:00
|
|
|
"path"
|
2020-05-10 17:26:48 +02:00
|
|
|
"path/filepath"
|
2019-12-09 07:48:18 +01:00
|
|
|
"strings"
|
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
"github.com/spf13/viper"
|
2019-12-09 07:48:18 +01:00
|
|
|
)
|
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
// Core struct contains core specific config value
|
|
|
|
type Config struct {
|
2020-04-24 03:21:45 +02:00
|
|
|
// SecretKey is used to encrypt opaque values such as
|
|
|
|
// the cursor. Auto-generated if not set
|
|
|
|
SecretKey string `mapstructure:"secret_key"`
|
|
|
|
|
|
|
|
// UseAllowList (aka production mode) when set to true ensures
|
|
|
|
// only queries lists in the allow.list file can be used. All
|
|
|
|
// queries are pre-prepared so no compiling happens and things are
|
|
|
|
// very fast.
|
|
|
|
UseAllowList bool `mapstructure:"use_allow_list"`
|
|
|
|
|
|
|
|
// AllowListFile if the path to allow list file if not set the
|
|
|
|
// path is assumed to tbe the same as the config path (allow.list)
|
|
|
|
AllowListFile string `mapstructure:"allow_list_file"`
|
|
|
|
|
|
|
|
// SetUserID forces the database session variable `user.id` to
|
|
|
|
// be set to the user id. This variables can be used by triggers
|
|
|
|
// or other database functions
|
|
|
|
SetUserID bool `mapstructure:"set_user_id"`
|
|
|
|
|
2020-05-28 06:07:01 +02:00
|
|
|
// DefaultBlock ensures that in anonymous mode (role 'anon') all tables
|
|
|
|
// are blocked from queries and mutations. To open access to tables in
|
|
|
|
// anonymous mode they have to be added to the 'anon' role config.
|
|
|
|
DefaultBlock bool `mapstructure:"default_block"`
|
2020-04-24 03:21:45 +02:00
|
|
|
|
|
|
|
// Vars is a map of hardcoded variables that can be leveraged in your
|
|
|
|
// queries (eg variable admin_id will be $admin_id in the query)
|
|
|
|
Vars map[string]string `mapstructure:"variables"`
|
|
|
|
|
|
|
|
// Blocklist is a list of tables and columns that should be filtered
|
|
|
|
// out from any and all queries
|
|
|
|
Blocklist []string
|
|
|
|
|
|
|
|
// Tables contains all table specific configuration such as aliased tables
|
|
|
|
// creating relationships between tables, etc
|
|
|
|
Tables []Table
|
|
|
|
|
|
|
|
// RolesQuery if set enabled attributed based access control. This query
|
|
|
|
// is use to fetch the user attributes that then dynamically define the users
|
|
|
|
// role.
|
|
|
|
RolesQuery string `mapstructure:"roles_query"`
|
|
|
|
|
|
|
|
// Roles contains all the configuration for all the roles you want to support
|
|
|
|
// `user` and `anon` are two default roles. User role is for when a user ID is
|
|
|
|
// available and Anon when it's not.
|
2020-05-28 06:07:01 +02:00
|
|
|
//
|
|
|
|
// If you're using the RolesQuery config to enable atribute based acess control then
|
|
|
|
// you can add more custom roles.
|
2020-04-24 03:21:45 +02:00
|
|
|
Roles []Role
|
|
|
|
|
|
|
|
// Inflections is to add additionally singular to plural mappings
|
|
|
|
// to the engine (eg. sheep: sheep)
|
|
|
|
Inflections map[string]string `mapstructure:"inflections"`
|
2020-05-20 06:03:05 +02:00
|
|
|
|
|
|
|
// Database schema name. Defaults to 'public'
|
|
|
|
DBSchema string `mapstructure:"db_schema"`
|
2020-01-28 06:26:53 +01:00
|
|
|
}
|
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
// Table struct defines a database table
|
|
|
|
type Table struct {
|
|
|
|
Name string
|
|
|
|
Table string
|
2020-06-15 09:06:50 +02:00
|
|
|
Type string
|
2020-04-11 08:45:06 +02:00
|
|
|
Blocklist []string
|
|
|
|
Remotes []Remote
|
|
|
|
Columns []Column
|
|
|
|
}
|
2020-01-28 06:26:53 +01:00
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
// Column struct defines a database column
|
|
|
|
type Column struct {
|
|
|
|
Name string
|
|
|
|
Type string
|
|
|
|
ForeignKey string `mapstructure:"related_to"`
|
|
|
|
}
|
2020-01-28 06:26:53 +01:00
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
// Remote struct defines a remote API endpoint
|
|
|
|
type Remote struct {
|
|
|
|
Name string
|
|
|
|
ID string
|
|
|
|
Path string
|
|
|
|
URL string
|
|
|
|
Debug bool
|
|
|
|
PassHeaders []string `mapstructure:"pass_headers"`
|
|
|
|
SetHeaders []struct {
|
|
|
|
Name string
|
|
|
|
Value string
|
|
|
|
} `mapstructure:"set_headers"`
|
|
|
|
}
|
2020-01-28 06:26:53 +01:00
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
// Role struct contains role specific access control values for for all database tables
|
|
|
|
type Role struct {
|
|
|
|
Name string
|
|
|
|
Match string
|
|
|
|
Tables []RoleTable
|
|
|
|
tm map[string]*RoleTable
|
|
|
|
}
|
2020-01-28 06:26:53 +01:00
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
// RoleTable struct contains role specific access control values for a database table
|
|
|
|
type RoleTable struct {
|
2020-05-22 08:24:13 +02:00
|
|
|
Name string
|
2020-05-28 06:07:01 +02:00
|
|
|
ReadOnly bool `mapstructure:"read_only"`
|
2020-01-28 06:26:53 +01:00
|
|
|
|
2020-05-28 06:07:01 +02:00
|
|
|
Query *Query
|
|
|
|
Insert *Insert
|
|
|
|
Update *Update
|
|
|
|
Delete *Delete
|
2020-04-11 08:45:06 +02:00
|
|
|
}
|
2020-01-28 06:26:53 +01:00
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
// Query struct contains access control values for query operations
|
|
|
|
type Query struct {
|
|
|
|
Limit int
|
|
|
|
Filters []string
|
|
|
|
Columns []string
|
|
|
|
DisableFunctions bool `mapstructure:"disable_functions"`
|
2020-05-28 06:07:01 +02:00
|
|
|
Block bool
|
2020-01-28 06:26:53 +01:00
|
|
|
}
|
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
// Insert struct contains access control values for insert operations
|
|
|
|
type Insert struct {
|
|
|
|
Filters []string
|
|
|
|
Columns []string
|
|
|
|
Presets map[string]string
|
2020-05-28 06:07:01 +02:00
|
|
|
Block bool
|
2019-12-09 07:48:18 +01:00
|
|
|
}
|
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
// Insert struct contains access control values for update operations
|
|
|
|
type Update struct {
|
|
|
|
Filters []string
|
|
|
|
Columns []string
|
|
|
|
Presets map[string]string
|
2020-05-28 06:07:01 +02:00
|
|
|
Block bool
|
2020-04-11 08:45:06 +02:00
|
|
|
}
|
2019-12-09 07:48:18 +01:00
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
// Delete struct contains access control values for delete operations
|
|
|
|
type Delete struct {
|
|
|
|
Filters []string
|
|
|
|
Columns []string
|
2020-05-28 06:07:01 +02:00
|
|
|
Block bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddRoleTable function is a helper function to make it easy to add per-table
|
|
|
|
// row-level config
|
2020-06-15 16:16:47 +02:00
|
|
|
func (c *Config) AddRoleTable(role, table string, conf interface{}) error {
|
2020-05-28 06:07:01 +02:00
|
|
|
var r *Role
|
|
|
|
|
|
|
|
for i := range c.Roles {
|
|
|
|
if strings.EqualFold(c.Roles[i].Name, role) {
|
|
|
|
r = &c.Roles[i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if r == nil {
|
|
|
|
nr := Role{Name: role}
|
|
|
|
c.Roles = append(c.Roles, nr)
|
|
|
|
r = &nr
|
|
|
|
}
|
|
|
|
|
|
|
|
var t *RoleTable
|
|
|
|
for i := range r.Tables {
|
|
|
|
if strings.EqualFold(r.Tables[i].Name, table) {
|
|
|
|
t = &r.Tables[i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if t == nil {
|
|
|
|
nt := RoleTable{Name: table}
|
|
|
|
r.Tables = append(r.Tables, nt)
|
|
|
|
t = &nt
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := conf.(type) {
|
|
|
|
case Query:
|
|
|
|
t.Query = &v
|
|
|
|
case Insert:
|
|
|
|
t.Insert = &v
|
|
|
|
case Update:
|
|
|
|
t.Update = &v
|
|
|
|
case Delete:
|
|
|
|
t.Delete = &v
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported object type: %t", v)
|
|
|
|
}
|
|
|
|
return nil
|
2020-04-11 08:45:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadInConfig function reads in the config file for the environment specified in the GO_ENV
|
|
|
|
// environment variable. This is the best way to create a new Super Graph config.
|
|
|
|
func ReadInConfig(configFile string) (*Config, error) {
|
2020-06-07 19:02:57 +02:00
|
|
|
cp := path.Dir(configFile)
|
|
|
|
vi := newViper(cp, path.Base(configFile))
|
2019-12-09 07:48:18 +01:00
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
if err := vi.ReadInConfig(); err != nil {
|
|
|
|
return nil, err
|
2019-12-09 07:48:18 +01:00
|
|
|
}
|
|
|
|
|
2020-06-07 19:02:57 +02:00
|
|
|
if pcf := vi.GetString("inherits"); pcf != "" {
|
|
|
|
cf := vi.ConfigFileUsed()
|
|
|
|
vi = newViper(cp, pcf)
|
2019-12-09 07:48:18 +01:00
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
if err := vi.ReadInConfig(); err != nil {
|
|
|
|
return nil, err
|
2019-12-09 07:48:18 +01:00
|
|
|
}
|
|
|
|
|
2020-06-07 19:02:57 +02:00
|
|
|
if v := vi.GetString("inherits"); v != "" {
|
|
|
|
return nil, fmt.Errorf("inherited config (%s) cannot itself inherit (%s)", pcf, v)
|
2020-04-11 08:45:06 +02:00
|
|
|
}
|
2019-12-09 07:48:18 +01:00
|
|
|
|
2020-06-07 19:02:57 +02:00
|
|
|
vi.SetConfigFile(cf)
|
2019-12-09 07:48:18 +01:00
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
if err := vi.MergeInConfig(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-09 07:48:18 +01:00
|
|
|
}
|
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
c := &Config{}
|
2019-12-09 07:48:18 +01:00
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
if err := vi.Unmarshal(&c); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode config, %v", err)
|
2019-12-09 07:48:18 +01:00
|
|
|
}
|
|
|
|
|
2020-05-17 09:11:56 +02:00
|
|
|
if c.AllowListFile == "" {
|
2020-06-07 19:02:57 +02:00
|
|
|
c.AllowListFile = path.Join(cp, "allow.list")
|
2019-12-09 07:48:18 +01:00
|
|
|
}
|
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
return c, nil
|
|
|
|
}
|
2019-12-09 07:48:18 +01:00
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
func newViper(configPath, configFile string) *viper.Viper {
|
|
|
|
vi := viper.New()
|
2019-12-09 07:48:18 +01:00
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
vi.SetEnvPrefix("SG")
|
|
|
|
vi.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
|
|
vi.AutomaticEnv()
|
2019-12-09 07:48:18 +01:00
|
|
|
|
2020-05-17 09:11:56 +02:00
|
|
|
if filepath.Ext(configFile) != "" {
|
2020-06-07 19:02:57 +02:00
|
|
|
vi.SetConfigFile(path.Join(configPath, configFile))
|
2020-05-10 17:26:48 +02:00
|
|
|
} else {
|
|
|
|
vi.SetConfigName(configFile)
|
|
|
|
vi.AddConfigPath(configPath)
|
|
|
|
vi.AddConfigPath("./config")
|
|
|
|
}
|
2019-12-09 07:48:18 +01:00
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
return vi
|
2019-12-09 07:48:18 +01:00
|
|
|
}
|