-
-
Notifications
You must be signed in to change notification settings - Fork 172
/
init.go
94 lines (74 loc) · 1.75 KB
/
init.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
package serv
import (
"fmt"
"go.uber.org/zap"
// postgres drivers
// mysql drivers
_ "github.com/go-sql-driver/mysql"
)
func initLogLevel(s *Service) {
switch s.conf.LogLevel {
case "debug":
s.logLevel = logLevelDebug
case "error":
s.logLevel = logLevelError
case "warn":
s.logLevel = logLevelWarn
case "info":
s.logLevel = logLevelInfo
default:
s.logLevel = logLevelNone
}
}
func validateConf(s *Service) {
var anonFound bool
for _, r := range s.conf.Roles {
if r.Name == "anon" {
anonFound = true
}
}
if !anonFound && s.conf.DefaultBlock {
s.log.Warn("Unauthenticated requests will be blocked. no role 'anon' defined")
s.conf.AuthFailBlock = false
}
}
func initConfig(c *Config, log *zap.SugaredLogger) error {
// copy over db_type from database.type
if c.Core.DBType == "" {
c.Core.DBType = c.DB.Type
}
if c.Serv.Production {
c.Auth.CredsInHeader = false
c.Auth.SubsCredsInVars = false
}
// Auths: validate and sanitize
am := make(map[string]struct{})
for i := 0; i < len(c.Auths); i++ {
a := &c.Auths[i]
if c.Serv.Production {
a.CredsInHeader = false
a.SubsCredsInVars = false
}
if _, ok := am[a.Name]; ok {
return fmt.Errorf("Duplicate auth found: %s", a.Name)
}
am[a.Name] = struct{}{}
}
// Actions: validate and sanitize
axm := make(map[string]struct{})
for i := 0; i < len(c.Actions); i++ {
a := &c.Actions[i]
if _, ok := axm[a.Name]; ok {
return fmt.Errorf("Duplicate action found: %s", a.Name)
}
if _, ok := am[a.AuthName]; !ok {
return fmt.Errorf("Invalid auth name: %s, For auth: %s", a.AuthName, a.Name)
}
axm[a.Name] = struct{}{}
}
if c.Auth.Type == "" || c.Auth.Type == "none" {
c.DefaultBlock = false
}
c.Core.Production = c.Serv.Production
return nil
}