-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
config.go
375 lines (313 loc) · 9.3 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package core
import (
"fmt"
"log"
"path"
"path/filepath"
"strings"
"time"
"github.com/dosco/graphjin/core/internal/qcode"
"github.com/spf13/viper"
)
// Core struct contains core specific config value
type Config struct {
// SecretKey is used to encrypt opaque values such as
// the cursor. Auto-generated if not set
SecretKey string `mapstructure:"secret_key"`
// DisableAllowList when set to true entirely disables the
// allow list workflow and all queries are always compiled
// even in production. (Warning possible security concern)
DisableAllowList bool `mapstructure:"disable_allow_list"`
// EnforceAllowList (aka production mode) when set to true ensures
// only queries saved to the allow list folders can be used.
EnforceAllowList bool `mapstructure:"enforce_allow_list"`
// AllowListFile if the path to allow list file if not set the
// path is assumed to be 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"`
// 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"`
// 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"`
// HeaderVars is a map of dynamic variables that map to http header
// values.
HeaderVars map[string]string `mapstructure:"header_variables"`
// Blocklist is a list of tables and columns that should be filtered
// out from any and all queries
Blocklist []string
// Resolvers contain the configs for custom resolvers. For example the `remote_api`
// resolver would join json from a remote API into your query response.
Resolvers []ResolverConfig
// 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 used 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.
//
// If you're using the RolesQuery config to enable atribute based acess control then
// you can add more custom roles.
Roles []Role
// Inflections is to add additionally singular to plural mappings
// to the engine (eg. sheep: sheep)
Inflections []string `mapstructure:"inflections"`
// Database type name. Defaults to 'postgres' (options: mysql, postgres)
DBType string `mapstructure:"db_type"`
// Log warnings and other debug information
Debug bool
// Useful for quickly debugging. Please set to false in production
CredsInVars bool `mapstructure:"creds_in_vars"`
// Subscriptions poll the database to query for updates
// this sets the duration (in seconds) between requests.
// Defaults to 5 seconds
PollDuration time.Duration `mapstructure:"poll_every_seconds"`
// DefaultLimit sets the default max limit (number of rows) when a
// limit is not defined in the query or the table role config.
// Default to 20
DefaultLimit int `mapstructure:"default_limit"`
rtmap map[string]resFn
}
// Table struct defines a database table
type Table struct {
Name string
Table string
Type string
Blocklist []string
Columns []Column
}
// Column struct defines a database column
type Column struct {
Name string
Type string
Primary bool
Array bool
ForeignKey string `mapstructure:"related_to"`
}
// 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
}
// RoleTable struct contains role specific access control values for a database table
type RoleTable struct {
Name string
ReadOnly bool `mapstructure:"read_only"`
Query *Query
Insert *Insert
Update *Update
Upsert *Upsert
Delete *Delete
}
// Query struct contains access control values for query operations
type Query struct {
Limit int
Filters []string
Columns []string
DisableFunctions bool `mapstructure:"disable_functions"`
Block bool
}
// Insert struct contains access control values for insert operations
type Insert struct {
Filters []string
Columns []string
Presets map[string]string
Block bool
}
// Insert struct contains access control values for update operations
type Update struct {
Filters []string
Columns []string
Presets map[string]string
Block bool
}
type Upsert struct {
Filters []string
Columns []string
Presets map[string]string
Block bool
}
// Delete struct contains access control values for delete operations
type Delete struct {
Filters []string
Columns []string
Block bool
}
// Resolver interface is used to create custom resolvers
// Custom resolvers must return a JSON value to be merged into
// the response JSON.
//
// Example Redis Resolver:
/*
type Redis struct {
Addr string
client redis.Client
}
func newRedis(v map[string]interface{}) (*Redis, error) {
re := &Redis{}
if err := mapstructure.Decode(v, re); err != nil {
return nil, err
}
re.client := redis.NewClient(&redis.Options{
Addr: re.Addr,
Password: "", // no password set
DB: 0, // use default DB
})
return re, nil
}
func (r *remoteAPI) Resolve(req ResolverReq) ([]byte, error) {
val, err := rdb.Get(ctx, req.ID).Result()
if err != nil {
return err
}
return val, nil
}
func main() {
conf := core.Config{
Resolvers: []Resolver{
Name: "cached_profile",
Type: "redis",
Table: "users",
Column: "id",
Props: []ResolverProps{
"addr": "localhost:6379",
},
},
}
gj.conf.SetResolver("redis", func(v ResolverProps) (Resolver, error) {
return newRedis(v)
})
gj, err := core.NewGraphJin(conf, db)
if err != nil {
log.Fatal(err)
}
}
*/
type Resolver interface {
Resolve(ResolverReq) ([]byte, error)
}
// ResolverProps is a map of properties from the resolver config to be passed
// to the customer resolver's builder (new) function
type ResolverProps map[string]interface{}
// ResolverConfig struct defines a custom resolver
type ResolverConfig struct {
Name string
Type string
Table string
Column string
StripPath string `mapstructure:"strip_path"`
Props ResolverProps `mapstructure:",remain"`
}
type ResolverReq struct {
ID string
Sel *qcode.Select
Log *log.Logger
*ReqConfig
}
// AddRoleTable function is a helper function to make it easy to add per-table
// row-level config
func (c *Config) AddRoleTable(role, table string, conf interface{}) error {
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 = &c.Roles[len(c.Roles)-1]
}
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 = &r.Tables[len(r.Tables)-1]
}
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
}
func (c *Config) SetResolver(name string, fn resFn) error {
if c.rtmap == nil {
c.rtmap = make(map[string]resFn)
}
if _, ok := c.rtmap[name]; ok {
return fmt.Errorf("resolver defined: %s", name)
}
c.rtmap[name] = fn
return nil
}
// 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 GraphJin config.
func ReadInConfig(configFile string) (*Config, error) {
cp := path.Dir(configFile)
vi := newViper(cp, path.Base(configFile))
if err := vi.ReadInConfig(); err != nil {
return nil, err
}
if pcf := vi.GetString("inherits"); pcf != "" {
cf := vi.ConfigFileUsed()
vi = newViper(cp, pcf)
if err := vi.ReadInConfig(); err != nil {
return nil, err
}
if v := vi.GetString("inherits"); v != "" {
return nil, fmt.Errorf("inherited config (%s) cannot itself inherit (%s)", pcf, v)
}
vi.SetConfigFile(cf)
if err := vi.MergeInConfig(); err != nil {
return nil, err
}
}
c := &Config{}
if err := vi.Unmarshal(&c); err != nil {
return nil, fmt.Errorf("failed to decode config, %v", err)
}
if c.AllowListFile == "" {
c.AllowListFile = path.Join(cp, "allow.list")
}
return c, nil
}
func newViper(configPath, configFile string) *viper.Viper {
vi := viper.New()
vi.SetEnvPrefix("SG")
vi.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
vi.AutomaticEnv()
if filepath.Ext(configFile) != "" {
vi.SetConfigFile(path.Join(configPath, configFile))
} else {
vi.SetConfigName(configFile)
vi.AddConfigPath(configPath)
vi.AddConfigPath("./config")
}
return vi
}