2019-05-13 01:27:26 +02:00
|
|
|
package serv
|
|
|
|
|
2019-06-04 16:54:51 +02:00
|
|
|
import (
|
2019-10-24 08:07:42 +02:00
|
|
|
"regexp"
|
2019-06-14 06:32:15 +02:00
|
|
|
"strings"
|
2019-10-24 08:07:42 +02:00
|
|
|
"unicode"
|
2019-06-14 06:32:15 +02:00
|
|
|
|
2019-10-06 22:28:10 +02:00
|
|
|
"github.com/spf13/viper"
|
2019-06-04 16:54:51 +02:00
|
|
|
)
|
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
type config struct {
|
2019-09-08 20:56:32 +02:00
|
|
|
AppName string `mapstructure:"app_name"`
|
|
|
|
Env string
|
|
|
|
HostPort string `mapstructure:"host_port"`
|
|
|
|
Host string
|
|
|
|
Port string
|
|
|
|
WebUI bool `mapstructure:"web_ui"`
|
|
|
|
LogLevel string `mapstructure:"log_level"`
|
|
|
|
EnableTracing bool `mapstructure:"enable_tracing"`
|
|
|
|
UseAllowList bool `mapstructure:"use_allow_list"`
|
|
|
|
WatchAndReload bool `mapstructure:"reload_on_config_change"`
|
|
|
|
AuthFailBlock string `mapstructure:"auth_fail_block"`
|
2019-09-20 06:19:11 +02:00
|
|
|
SeedFile string `mapstructure:"seed_file"`
|
2019-09-26 06:35:31 +02:00
|
|
|
MigrationsPath string `mapstructure:"migrations_path"`
|
|
|
|
|
|
|
|
Inflections map[string]string
|
2019-05-13 01:27:26 +02:00
|
|
|
|
|
|
|
Auth struct {
|
2019-10-24 08:07:42 +02:00
|
|
|
Type string
|
|
|
|
Cookie string
|
|
|
|
CredsInHeader bool `mapstructure:"creds_in_header"`
|
2019-05-13 01:27:26 +02:00
|
|
|
|
|
|
|
Rails struct {
|
|
|
|
Version string
|
|
|
|
SecretKeyBase string `mapstructure:"secret_key_base"`
|
|
|
|
URL string
|
|
|
|
Password string
|
|
|
|
MaxIdle int `mapstructure:"max_idle"`
|
|
|
|
MaxActive int `mapstructure:"max_active"`
|
|
|
|
Salt string
|
|
|
|
SignSalt string `mapstructure:"sign_salt"`
|
|
|
|
AuthSalt string `mapstructure:"auth_salt"`
|
|
|
|
}
|
|
|
|
|
|
|
|
JWT struct {
|
|
|
|
Provider string
|
|
|
|
Secret string
|
|
|
|
PubKeyFile string `mapstructure:"public_key_file"`
|
|
|
|
PubKeyType string `mapstructure:"public_key_type"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DB struct {
|
|
|
|
Type string
|
|
|
|
Host string
|
2019-09-26 06:35:31 +02:00
|
|
|
Port uint16
|
2019-05-13 01:27:26 +02:00
|
|
|
DBName string
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
Schema string
|
2019-09-26 06:35:31 +02:00
|
|
|
PoolSize int32 `mapstructure:"pool_size"`
|
2019-05-13 01:27:26 +02:00
|
|
|
MaxRetries int `mapstructure:"max_retries"`
|
|
|
|
LogLevel string `mapstructure:"log_level"`
|
|
|
|
|
2019-10-24 08:07:42 +02:00
|
|
|
Vars map[string]string `mapstructure:"variables"`
|
2019-05-13 01:27:26 +02:00
|
|
|
|
|
|
|
Defaults struct {
|
2019-10-25 07:39:59 +02:00
|
|
|
Filters []string
|
2019-09-08 07:54:38 +02:00
|
|
|
Blocklist []string
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Tables []configTable
|
|
|
|
} `mapstructure:"database"`
|
2019-10-06 22:28:10 +02:00
|
|
|
|
|
|
|
Tables []configTable
|
2019-10-24 08:07:42 +02:00
|
|
|
|
|
|
|
RolesQuery string `mapstructure:"roles_query"`
|
|
|
|
Roles []configRole
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type configTable struct {
|
2019-10-14 08:51:36 +02:00
|
|
|
Name string
|
|
|
|
Table string
|
|
|
|
Blocklist []string
|
|
|
|
Remotes []configRemote
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type configRemote struct {
|
|
|
|
Name string
|
|
|
|
ID string
|
|
|
|
Path string
|
|
|
|
URL string
|
2019-06-06 14:57:00 +02:00
|
|
|
Debug bool
|
2019-05-13 01:27:26 +02:00
|
|
|
PassHeaders []string `mapstructure:"pass_headers"`
|
|
|
|
SetHeaders []struct {
|
|
|
|
Name string
|
|
|
|
Value string
|
|
|
|
} `mapstructure:"set_headers"`
|
|
|
|
}
|
|
|
|
|
2019-10-24 08:07:42 +02:00
|
|
|
type configRole struct {
|
2019-10-14 08:51:36 +02:00
|
|
|
Name string
|
2019-10-24 08:07:42 +02:00
|
|
|
Match string
|
2019-10-14 08:51:36 +02:00
|
|
|
Tables []struct {
|
|
|
|
Name string
|
|
|
|
|
|
|
|
Query struct {
|
|
|
|
Limit int
|
2019-10-25 07:39:59 +02:00
|
|
|
Filters []string
|
2019-10-14 08:51:36 +02:00
|
|
|
Columns []string
|
|
|
|
DisableAggregation bool `mapstructure:"disable_aggregation"`
|
2019-10-26 07:34:29 +02:00
|
|
|
Block bool
|
2019-10-14 08:51:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Insert struct {
|
2019-10-25 07:39:59 +02:00
|
|
|
Filters []string
|
2019-10-14 08:51:36 +02:00
|
|
|
Columns []string
|
|
|
|
Set map[string]string
|
2019-10-26 07:34:29 +02:00
|
|
|
Block bool
|
2019-10-14 08:51:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Update struct {
|
2019-10-25 07:39:59 +02:00
|
|
|
Filters []string
|
2019-10-14 08:51:36 +02:00
|
|
|
Columns []string
|
|
|
|
Set map[string]string
|
2019-10-26 07:34:29 +02:00
|
|
|
Block bool
|
2019-10-14 08:51:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Delete struct {
|
2019-10-25 07:39:59 +02:00
|
|
|
Filters []string
|
2019-10-14 08:51:36 +02:00
|
|
|
Columns []string
|
2019-10-26 07:34:29 +02:00
|
|
|
Block bool
|
2019-10-14 08:51:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-06 22:28:10 +02:00
|
|
|
func newConfig() *viper.Viper {
|
|
|
|
vi := viper.New()
|
|
|
|
|
|
|
|
vi.SetEnvPrefix("SG")
|
|
|
|
vi.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
|
|
vi.AutomaticEnv()
|
|
|
|
|
|
|
|
vi.AddConfigPath(confPath)
|
|
|
|
vi.AddConfigPath("./config")
|
|
|
|
vi.SetConfigName(getConfigName())
|
|
|
|
|
|
|
|
vi.SetDefault("host_port", "0.0.0.0:8080")
|
|
|
|
vi.SetDefault("web_ui", false)
|
|
|
|
vi.SetDefault("enable_tracing", false)
|
|
|
|
vi.SetDefault("auth_fail_block", "always")
|
|
|
|
vi.SetDefault("seed_file", "seed.js")
|
|
|
|
|
|
|
|
vi.SetDefault("database.type", "postgres")
|
|
|
|
vi.SetDefault("database.host", "localhost")
|
|
|
|
vi.SetDefault("database.port", 5432)
|
|
|
|
vi.SetDefault("database.user", "postgres")
|
|
|
|
vi.SetDefault("database.schema", "public")
|
|
|
|
|
|
|
|
vi.SetDefault("env", "development")
|
|
|
|
vi.BindEnv("env", "GO_ENV")
|
|
|
|
vi.BindEnv("HOST", "HOST")
|
|
|
|
vi.BindEnv("PORT", "PORT")
|
|
|
|
|
|
|
|
vi.SetDefault("auth.rails.max_idle", 80)
|
|
|
|
vi.SetDefault("auth.rails.max_active", 12000)
|
|
|
|
|
|
|
|
return vi
|
|
|
|
}
|
|
|
|
|
2019-10-25 06:01:22 +02:00
|
|
|
func (c *config) Validate() {
|
|
|
|
rm := make(map[string]struct{})
|
|
|
|
|
|
|
|
for i := range c.Roles {
|
|
|
|
name := strings.ToLower(c.Roles[i].Name)
|
|
|
|
if _, ok := rm[name]; ok {
|
|
|
|
logger.Fatal().Msgf("duplicate config for role '%s'", c.Roles[i].Name)
|
|
|
|
}
|
|
|
|
rm[name] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
tm := make(map[string]struct{})
|
|
|
|
|
|
|
|
for i := range c.Tables {
|
|
|
|
name := strings.ToLower(c.Tables[i].Name)
|
|
|
|
if _, ok := tm[name]; ok {
|
|
|
|
logger.Fatal().Msgf("duplicate config for table '%s'", c.Tables[i].Name)
|
|
|
|
}
|
|
|
|
tm[name] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.RolesQuery) == 0 {
|
|
|
|
logger.Warn().Msgf("no 'roles_query' defined.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 06:32:15 +02:00
|
|
|
func (c *config) getAliasMap() map[string][]string {
|
2019-10-06 22:28:10 +02:00
|
|
|
m := make(map[string][]string, len(c.Tables))
|
2019-05-13 01:27:26 +02:00
|
|
|
|
2019-10-06 22:28:10 +02:00
|
|
|
for i := range c.Tables {
|
|
|
|
t := c.Tables[i]
|
2019-05-13 01:27:26 +02:00
|
|
|
|
|
|
|
if len(t.Table) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2019-06-14 06:32:15 +02:00
|
|
|
|
|
|
|
k := strings.ToLower(t.Table)
|
|
|
|
m[k] = append(m[k], strings.ToLower(t.Name))
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
2019-10-24 08:07:42 +02:00
|
|
|
|
|
|
|
var varRe1 = regexp.MustCompile(`(?mi)\$([a-zA-Z0-9_.]+)`)
|
|
|
|
var varRe2 = regexp.MustCompile(`\{\{([a-zA-Z0-9_.]+)\}\}`)
|
|
|
|
|
|
|
|
func sanitize(s string) string {
|
|
|
|
s0 := varRe1.ReplaceAllString(s, `{{$1}}`)
|
|
|
|
|
|
|
|
s1 := strings.Map(func(r rune) rune {
|
|
|
|
if unicode.IsSpace(r) {
|
|
|
|
return ' '
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}, s0)
|
|
|
|
|
|
|
|
return varRe2.ReplaceAllStringFunc(s1, func(m string) string {
|
|
|
|
return strings.ToLower(m)
|
|
|
|
})
|
|
|
|
}
|