/
conf.go
331 lines (275 loc) · 8.43 KB
/
conf.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
package main
import (
"errors"
"fmt"
"github.com/spf13/cast"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/fsnotify/fsnotify"
"os"
"regexp"
"strings"
)
type Conf struct {
Token string // Pre-shared token in configuration, never via the wire
Hostname string
TagsList []string
UseAutoTag bool
ServerEnabled bool
EndpointURI string
ServerPort int
SslCertFile string // TLS certificate file
SslPrivateKeyFile string // Private key file
AutoGenerateCert bool
ClientPort int
Debug bool
Home string //home directory
LdapConfigFile string
EnableLdap bool
//Ldap
ldapConfig *LdapConfig
ldapViper *viper.Viper
//Notifications
notificationConfigs []NotificationServiceConfig
confFlags *pflag.FlagSet
}
const defaultHomePath = "/etc/indispenso/"
func newConfig() *Conf {
c := new(Conf)
c.ldapViper = viper.New()
c.ldapConfig = &LdapConfig{}
c.notificationConfigs = []NotificationServiceConfig{}
viper.SetConfigName("indispenso")
viper.SetEnvPrefix("ind")
// Defaults
viper.SetDefault("Token", "")
viper.SetDefault("Hostname", getDefaultHostName())
viper.SetDefault("UseAutoTag", true)
viper.SetDefault("ServerEnabled", false)
viper.SetDefault("Home", defaultHomePath)
viper.SetDefault("Debug", false)
viper.SetDefault("ServerPort", 897)
viper.SetDefault("EndpointURI", "")
viper.SetDefault("SslCertFile", "cert.pem")
viper.SetDefault("SslPrivateKeyFile", "key.pem")
viper.SetDefault("AutoGenerateCert", true)
viper.SetDefault("ClientPort", 898)
viper.SetDefault("EnableLdap", false)
viper.SetDefault("LdapConfigFile", "")
//Flags
c.confFlags = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)
configFile := c.confFlags.StringP("config", "c", "", "Config file location default is /etc/indispenso/indispenso.{json,toml,yaml,yml,properties,props,prop}")
c.confFlags.BoolP("serverEnabled", "s", false, "Define if server module should be started or not")
c.confFlags.BoolP("debug", "d", false, "Enable debug mode")
c.confFlags.StringP("home", "p", defaultHomePath, "Home directory where all config files are located")
c.confFlags.StringP("endpointUri", "e", "", "URI of server interface, used by client")
c.confFlags.StringP("token", "t", "", "Secret token")
c.confFlags.StringP("hostname", "i", getDefaultHostName(), "Hostname that is use to identify itself")
c.confFlags.BoolP("enableLdap", "l", false, "Enable LDAP authentication")
c.confFlags.BoolP("help", "h", false, "Print help message")
c.confFlags.Parse(os.Args[1:])
if len(*configFile) > 2 {
viper.SetConfigFile(*configFile)
} else {
legacyConfigFile := "/etc/indispenso/indispenso.conf"
if _, err := os.Stat(legacyConfigFile); err == nil {
viper.SetConfigFile(legacyConfigFile)
viper.SetConfigType("yaml")
}
}
viper.BindPFlags(c.confFlags)
viper.AutomaticEnv()
viper.ReadInConfig()
c.setupHome(nil, viper.GetString("Home"))
c.setupHome(c.ldapViper, viper.GetString("Home"))
c.SetupNotificationConfig("slack", &SlackNotifyConfig{})
c.Update()
return c
}
func (c *Conf) SetupNotificationConfig(filename string, config NotificationServiceConfig) {
viperConfig := viper.New()
c.setupHome(viperConfig, viper.GetString("Home"))
viperConfig.SetConfigName(filename)
if err := viperConfig.ReadInConfig(); err != nil {
log.Printf("Cannot read notification config: %s", err)
}
viperConfig.Unmarshal(config)
viper.OnConfigChange(func(in fsnotify.Event) { viperConfig.ReadInConfig(); viperConfig.Unmarshal(config) })
viper.WatchConfig()
c.notificationConfigs = append(c.notificationConfigs, config)
}
func (c *Conf) GetNotifications() []NotificationServiceConfig {
return c.notificationConfigs
}
func (c *Conf) EnableAutoUpdate() {
viper.OnConfigChange(func(in fsnotify.Event) { c.Update() })
viper.WatchConfig()
}
func (c *Conf) setupHome(viperConf *viper.Viper, homePath string) {
configPath := "config"
if len(homePath) > 0 {
configPath = homePath
}
if viperConf != nil {
viperConf.AddConfigPath(configPath)
} else {
viper.AddConfigPath(configPath)
}
}
func (c *Conf) Update() {
log.Println("Updating config")
//Legacy config for client
//need to be replaced by aliases after this PR will be integrated into viper:
//https://github.com/spf13/viper/pull/155
UpdateLegacyString("seed", "endpointuri")
UpdateLegacyString("secure_token", "token")
UpdateLegacyBool("server_enabled", "serverEnabled", false)
viper.Unmarshal(c)
if c.EnableLdap {
c.setupLdapViper()
c.ldapViper.ReadInConfig()
c.ldapViper.Unmarshal(c.ldapConfig)
}
c.AutoRepair()
if c.Debug {
log.Printf("Configuration: %+v", c)
}
}
func (c *Conf) setupLdapViper() {
c.ldapViper.SetConfigFile(c.LdapConfigFile)
c.ldapViper.SetConfigName("ldap")
c.setupHome(c.ldapViper, c.Home)
}
func UpdateLegacyString(from string, to string) {
val := viper.GetString(from)
if val == "" {
return
}
if viper.GetString(to) == "" {
viper.Set(to, viper.GetString(from))
}
}
func UpdateLegacyBool(from string, to string, defaultValue bool) {
val := viper.GetBool(from)
if val == defaultValue {
return
}
if viper.GetBool(to) == defaultValue {
viper.Set(to, viper.GetBool(from))
}
}
func (c *Conf) IsHelp() bool {
return viper.GetBool("help")
}
func (c *Conf) AutoRepair() {
fullUriPattern, _ := regexp.Compile("(http[s]{0,1})://([^/:]+):?([0-9]{0,}).*")
if !fullUriPattern.MatchString(c.EndpointURI) {
protocol := "https"
host := getDefaultHostName()
port := cast.ToString(c.ServerPort)
hostWithPortPattern, _ := regexp.Compile("([^:/]+):?([0-9]{0,})")
repaired := false
if hostWithPortPattern.MatchString(c.EndpointURI) {
matches := hostWithPortPattern.FindAllStringSubmatch(c.EndpointURI, -1)
if val := matches[0][1]; val != "" {
host = val
}
if val := matches[0][2]; val != "" {
port = val
}
repaired = true
}
if repaired {
c.EndpointURI = fmt.Sprintf("%s://%s:%s/", protocol, host, port)
log.Printf("EndpointURI successfully reparied to: %s", c.EndpointURI)
}
}
}
func (c *Conf) PrintHelp() {
fmt.Println("Usage of indispenso:")
c.confFlags.PrintDefaults()
os.Exit(0)
}
func (c *Conf) GetSslPrivateKeyFile() string {
return c.HomeFile(c.SslPrivateKeyFile)
}
func (c *Conf) GetSslCertFile() string {
return c.HomeFile(c.SslCertFile)
}
func (c *Conf) ConfFile() string {
return viper.ConfigFileUsed()
}
func getDefaultHostName() string {
if hostname, err := os.Hostname(); err == nil {
return hostname
}
return "localhost"
}
func (c *Conf) GetHome() string {
if c.Home == "/" {
return c.Home
}
return strings.TrimRight(c.Home, "/")
}
func (c *Conf) HomeFile(fileName string) string {
return fmt.Sprintf("%s/%s", c.GetHome(), fileName)
}
func (c *Conf) ServerRequest(path string) string {
return fmt.Sprintf("%s/%s", strings.TrimRight(c.EndpointURI, "/"), strings.TrimLeft(path, "/"))
}
func (c *Conf) Validate() error {
// Must have token
minLen := 32
if len(strings.TrimSpace(c.Token)) < minLen {
return errors.New(fmt.Sprintf("Must have secure token with minimum length of %d", minLen))
}
if _, err := os.Stat(c.GetHome()); os.IsNotExist(err) {
return errors.New(fmt.Sprintf("Home directory doesn't exists: %s", c.GetHome()))
}
return nil
}
func (c *Conf) isClientEnabled() bool {
return len(c.EndpointURI) > 0
}
func (c *Conf) GetTags() []string {
tagsList := c.TagsList
if viper.GetBool("useautotag") {
autoTags := c.hostTagDiscovery()
tagsList = append(tagsList, autoTags...)
}
return tagsList
}
// Auto tag
func (c *Conf) hostTagDiscovery() []string {
tokens := strings.FieldsFunc(c.Hostname, func(r rune) bool {
return r == '.' || r == '-' || r == '_'
})
ret := make([]string, 0, len(tokens))
numbersOnlyRegexp, _ := regexp.Compile("^[[:digit:]]+$")
numbersRegexp, _ := regexp.Compile("[[:digit:]]")
for _, token := range tokens {
cleanTag := cleanTag(token)
// Min 2 characters && not just numbers && not only numbers
if len(cleanTag) >= 2 && !numbersOnlyRegexp.MatchString(cleanTag) {
// Count numbers
numberCount := float64(len(numbersRegexp.FindAllStringSubmatch(cleanTag, -1)))
strLen := float64(len(cleanTag))
if numberCount >= strLen*0.5 {
// More than half is numbers, ignore
continue
}
ret = append(ret, cleanTag)
}
}
return ret
}
// Clean tag
func cleanTag(in string) string {
tagRegexp, _ := regexp.Compile("^[[:alnum:]-]+$")
cleanTag := strings.ToLower(strings.TrimSpace(in))
// Must be alphanumeric
if !tagRegexp.MatchString(cleanTag) {
return ""
}
return cleanTag
}