-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
init.go
165 lines (132 loc) · 2.87 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
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
package serv
import (
"crypto/sha256"
"fmt"
"os"
"path"
"strings"
// postgres drivers
// mysql drivers
_ "github.com/go-sql-driver/mysql"
"github.com/spf13/afero"
)
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 (s *service) initFS() error {
if s.fs != nil {
return nil
}
var cpath string
if s.conf.Serv.ConfigPath == "" {
cp, err := os.Getwd()
if err != nil {
return err
}
cpath = path.Join(cp, "config")
} else {
cpath = s.conf.Serv.ConfigPath
}
s.fs = afero.NewBasePathFs(afero.NewOsFs(), cpath)
s.conf.Auth.JWT.SetFS(s.fs)
for i := range s.conf.Auths {
s.conf.Auths[i].JWT.SetFS(s.fs)
}
return nil
}
func (s *service) initConfig() error {
c := s.conf
c.dirty = true
// 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{}{}
}
if c.HotDeploy {
if c.AdminSecretKey != "" {
s.asec = sha256.Sum256([]byte(s.conf.AdminSecretKey))
} else {
return fmt.Errorf("please set an admin_secret_key")
}
}
// 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
}
hp := strings.SplitN(s.conf.HostPort, ":", 2)
if len(hp) == 2 {
if s.conf.Host != "" {
hp[0] = s.conf.Host
}
if s.conf.Port != "" {
hp[1] = s.conf.Port
}
s.conf.hostPort = fmt.Sprintf("%s:%s", hp[0], hp[1])
}
if s.conf.hostPort == "" {
s.conf.hostPort = defaultHP
}
c.Core.Production = c.Serv.Production
return nil
}
func (s *service) initDB() error {
var err error
if s.db != nil {
return nil
}
s.db, err = newDB(s.conf, true, true, s.log, s.fs)
if err != nil {
return err
}
return nil
}