forked from semaphoreui/semaphore
/
config.go
417 lines (324 loc) · 10.5 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package util
import (
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"os"
"path"
"net/url"
"github.com/gorilla/securecookie"
"golang.org/x/crypto/bcrypt"
"strings"
"io"
)
// Cookie is a runtime generated secure cookie used for authentication
var Cookie *securecookie.SecureCookie
// Migration indicates that the user wishes to run database migrations, deprecated
var Migration bool
// InteractiveSetup indicates that the cli should perform interactive setup mode
var InteractiveSetup bool
// Upgrade indicates that we should perform an upgrade action
var Upgrade bool
// WebHostURL is the public route to the semaphore server
var WebHostURL *url.URL
const (
longPos = "yes"
shortPos = "y"
)
type mySQLConfig struct {
Hostname string `json:"host"`
Username string `json:"user"`
Password string `json:"pass"`
DbName string `json:"name"`
}
type ldapMappings struct {
DN string `json:"dn"`
Mail string `json:"mail"`
UID string `json:"uid"`
CN string `json:"cn"`
}
type configType struct {
MySQL mySQLConfig `json:"mysql"`
// Format `:port_num` eg, :3000
Port string `json:"port"`
// semaphore stores ephemeral projects here
TmpPath string `json:"tmp_path"`
// cookie hashing & encryption
CookieHash string `json:"cookie_hash"`
CookieEncryption string `json:"cookie_encryption"`
// email alerting
EmailSender string `json:"email_sender"`
EmailHost string `json:"email_host"`
EmailPort string `json:"email_port"`
// web host
WebHost string `json:"web_host"`
// ldap settings
LdapBindDN string `json:"ldap_binddn"`
LdapBindPassword string `json:"ldap_bindpassword"`
LdapServer string `json:"ldap_server"`
LdapSearchDN string `json:"ldap_searchdn"`
LdapSearchFilter string `json:"ldap_searchfilter"`
LdapMappings ldapMappings `json:"ldap_mappings"`
// telegram alerting
TelegramChat string `json:"telegram_chat"`
TelegramToken string `json:"telegram_token"`
// task concurrency
ConcurrencyMode string `json:"concurrency_mode"`
MaxParallelTasks int `json:"max_parallel_tasks"`
// configType field ordering with bools at end reduces struct size
// (maligned check)
// feature switches
EmailAlert bool `json:"email_alert"`
TelegramAlert bool `json:"telegram_alert"`
LdapEnable bool `json:"ldap_enable"`
LdapNeedTLS bool `json:"ldap_needtls"`
}
// Config exposes the application configuration storage for use in the application
var Config *configType
var confPath *string
// NewConfig returns a reference to a new blank configType
// nolint: golint
func NewConfig() *configType {
return &configType{}
}
// ConfigInit reads in cli flags, and switches actions appropriately on them
func ConfigInit() {
flag.BoolVar(&InteractiveSetup, "setup", false, "perform interactive setup")
flag.BoolVar(&Migration, "migrate", false, "execute migrations")
flag.BoolVar(&Upgrade, "upgrade", false, "upgrade semaphore")
confPath = flag.String("config", "", "config path")
var unhashedPwd string
flag.StringVar(&unhashedPwd, "hash", "", "generate hash of given password")
var printConfig bool
flag.BoolVar(&printConfig, "printConfig", false, "print example configuration")
var printVersion bool
flag.BoolVar(&printVersion, "version", false, "print the semaphore version")
flag.Parse()
if InteractiveSetup {
return
}
if printVersion {
fmt.Println(Version)
os.Exit(0)
}
if printConfig {
cfg := &configType{
MySQL: mySQLConfig{
Hostname: "127.0.0.1:3306",
Username: "root",
DbName: "semaphore",
},
Port: ":3000",
TmpPath: "/tmp/semaphore",
}
cfg.GenerateCookieSecrets()
b, _ := json.MarshalIndent(cfg, "", "\t")
fmt.Println(string(b))
os.Exit(0)
}
if len(unhashedPwd) > 0 {
password, _ := bcrypt.GenerateFromPassword([]byte(unhashedPwd), 11)
fmt.Println("Generated password: ", string(password))
os.Exit(0)
}
loadConfig()
validateConfig()
var encryption []byte
hash, _ := base64.StdEncoding.DecodeString(Config.CookieHash)
if len(Config.CookieEncryption) > 0 {
encryption, _ = base64.StdEncoding.DecodeString(Config.CookieEncryption)
}
Cookie = securecookie.New(hash, encryption)
WebHostURL, _ = url.Parse(Config.WebHost)
if len(WebHostURL.String()) == 0 {
WebHostURL = nil
}
}
func loadConfig() {
//If the confPath option has been set try to load and decode it
if confPath != nil && len(*confPath) > 0 {
file, err := os.Open(*confPath)
exitOnConfigError(err)
decodeConfig(file)
} else {
// if no confPath look in the cwd
cwd, err := os.Getwd()
exitOnConfigError(err)
cwd = cwd + "/config.json"
confPath = &cwd
file, err := os.Open(*confPath)
exitOnConfigError(err)
decodeConfig(file)
}
fmt.Println("Using config file: " + *confPath)
}
func validateConfig() {
validatePort()
if len(Config.TmpPath) == 0 {
Config.TmpPath = "/tmp/semaphore"
}
if Config.MaxParallelTasks < 1 {
Config.MaxParallelTasks = 10
}
}
func validatePort() {
//TODO - why do we do this only with this variable?
if len(os.Getenv("PORT")) > 0 {
Config.Port = ":" + os.Getenv("PORT")
}
if len(Config.Port) == 0 {
Config.Port = ":3000"
}
if !strings.HasPrefix(Config.Port, ":") {
Config.Port = ":" + Config.Port
}
}
func exitOnConfigError(err error) {
if err != nil {
fmt.Println("Cannot Find configuration! Use -c parameter to point to a JSON file generated by -setup.\n\n Hint: have you run `-setup` ?")
os.Exit(1)
}
}
func decodeConfig(file io.Reader) {
if err := json.NewDecoder(file).Decode(&Config); err != nil {
fmt.Println("Could not decode configuration!")
panic(err)
}
}
func (conf *configType) GenerateCookieSecrets() {
hash := securecookie.GenerateRandomKey(32)
encryption := securecookie.GenerateRandomKey(32)
conf.CookieHash = base64.StdEncoding.EncodeToString(hash)
conf.CookieEncryption = base64.StdEncoding.EncodeToString(encryption)
}
//nolint: gocyclo
func (conf *configType) Scan() {
fmt.Print(" > DB Hostname (default 127.0.0.1:3306): ")
ScanErrorChecker(fmt.Scanln(&conf.MySQL.Hostname))
if len(conf.MySQL.Hostname) == 0 {
conf.MySQL.Hostname = "127.0.0.1:3306"
}
fmt.Print(" > DB User (default root): ")
ScanErrorChecker(fmt.Scanln(&conf.MySQL.Username))
if len(conf.MySQL.Username) == 0 {
conf.MySQL.Username = "root"
}
fmt.Print(" > DB Password: ")
ScanErrorChecker(fmt.Scanln(&conf.MySQL.Password))
fmt.Print(" > DB Name (default semaphore): ")
ScanErrorChecker(fmt.Scanln(&conf.MySQL.DbName))
if len(conf.MySQL.DbName) == 0 {
conf.MySQL.DbName = "semaphore"
}
fmt.Print(" > Playbook path (default /tmp/semaphore): ")
ScanErrorChecker(fmt.Scanln(&conf.TmpPath))
if len(conf.TmpPath) == 0 {
conf.TmpPath = "/tmp/semaphore"
}
conf.TmpPath = path.Clean(conf.TmpPath)
fmt.Print(" > Web root URL (optional, example http://localhost:8010/): ")
ScanErrorChecker(fmt.Scanln(&conf.WebHost))
var EmailAlertAnswer string
fmt.Print(" > Enable email alerts (y/n, default n): ")
ScanErrorChecker(fmt.Scanln(&EmailAlertAnswer))
if EmailAlertAnswer == longPos || EmailAlertAnswer == shortPos {
conf.EmailAlert = true
fmt.Print(" > Mail server host (default localhost): ")
ScanErrorChecker(fmt.Scanln(&conf.EmailHost))
if len(conf.EmailHost) == 0 {
conf.EmailHost = "localhost"
}
fmt.Print(" > Mail server port (default 25): ")
ScanErrorChecker(fmt.Scanln(&conf.EmailPort))
if len(conf.EmailPort) == 0 {
conf.EmailPort = "25"
}
fmt.Print(" > Mail sender address (default semaphore@localhost): ")
ScanErrorChecker(fmt.Scanln(&conf.EmailSender))
if len(conf.EmailSender) == 0 {
conf.EmailSender = "semaphore@localhost"
}
} else {
conf.EmailAlert = false
}
var TelegramAlertAnswer string
fmt.Print(" > Enable telegram alerts (y/n, default n): ")
ScanErrorChecker(fmt.Scanln(&TelegramAlertAnswer))
if TelegramAlertAnswer == longPos || TelegramAlertAnswer == shortPos {
conf.TelegramAlert = true
fmt.Print(" > Telegram bot token (you can get it from @BotFather) (default ''): ")
ScanErrorChecker(fmt.Scanln(&conf.TelegramToken))
if len(conf.TelegramToken) == 0 {
conf.TelegramToken = ""
}
fmt.Print(" > Telegram chat ID (default ''): ")
ScanErrorChecker(fmt.Scanln(&conf.TelegramChat))
if len(conf.TelegramChat) == 0 {
conf.TelegramChat = ""
}
} else {
conf.TelegramAlert = false
}
var LdapAnswer string
fmt.Print(" > Enable LDAP authentication (y/n, default n): ")
ScanErrorChecker(fmt.Scanln(&LdapAnswer))
if LdapAnswer == longPos || LdapAnswer == shortPos {
conf.LdapEnable = true
fmt.Print(" > LDAP server host (default localhost:389): ")
ScanErrorChecker(fmt.Scanln(&conf.LdapServer))
if len(conf.LdapServer) == 0 {
conf.LdapServer = "localhost:389"
}
var LdapTLSAnswer string
fmt.Print(" > Enable LDAP TLS connection (y/n, default n): ")
ScanErrorChecker(fmt.Scanln(&LdapTLSAnswer))
if LdapTLSAnswer == longPos || LdapTLSAnswer == shortPos {
conf.LdapNeedTLS = true
} else {
conf.LdapNeedTLS = false
}
fmt.Print(" > LDAP DN for bind (default cn=user,ou=users,dc=example): ")
ScanErrorChecker(fmt.Scanln(&conf.LdapBindDN))
if len(conf.LdapBindDN) == 0 {
conf.LdapBindDN = "cn=user,ou=users,dc=example"
}
fmt.Print(" > Password for LDAP bind user (default pa55w0rd): ")
ScanErrorChecker(fmt.Scanln(&conf.LdapBindPassword))
if len(conf.LdapBindPassword) == 0 {
conf.LdapBindPassword = "pa55w0rd"
}
fmt.Print(" > LDAP DN for user search (default ou=users,dc=example): ")
ScanErrorChecker(fmt.Scanln(&conf.LdapSearchDN))
if len(conf.LdapSearchDN) == 0 {
conf.LdapSearchDN = "ou=users,dc=example"
}
fmt.Print(" > LDAP search filter (default (uid=" + "%" + "s)): ")
ScanErrorChecker(fmt.Scanln(&conf.LdapSearchFilter))
if len(conf.LdapSearchFilter) == 0 {
conf.LdapSearchFilter = "(uid=%s)"
}
fmt.Print(" > LDAP mapping for DN field (default dn): ")
ScanErrorChecker(fmt.Scanln(&conf.LdapMappings.DN))
if len(conf.LdapMappings.DN) == 0 {
conf.LdapMappings.DN = "dn"
}
fmt.Print(" > LDAP mapping for username field (default uid): ")
ScanErrorChecker(fmt.Scanln(&conf.LdapMappings.UID))
if len(conf.LdapMappings.UID) == 0 {
conf.LdapMappings.UID = "uid"
}
fmt.Print(" > LDAP mapping for full name field (default cn): ")
ScanErrorChecker(fmt.Scanln(&conf.LdapMappings.CN))
if len(conf.LdapMappings.CN) == 0 {
conf.LdapMappings.CN = "cn"
}
fmt.Print(" > LDAP mapping for email field (default mail): ")
ScanErrorChecker(fmt.Scanln(&conf.LdapMappings.Mail))
if len(conf.LdapMappings.Mail) == 0 {
conf.LdapMappings.Mail = "mail"
}
} else {
conf.LdapEnable = false
}
}