-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
76 lines (60 loc) · 1.82 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
package main
import (
"log"
"strings"
"github.com/kelseyhightower/envconfig"
auth "github.com/korylprince/go-ad-auth/v3"
)
// Config represents options given in the environment
type Config struct {
SessionExpiration int //in minutes; default: 60
LDAPServer string //required
LDAPPort int //default: 389
LDAPBaseDN string //required
LDAPGroup string //optional
LDAPSecurity string //default: none
ldapSecurity auth.SecurityType
SQLDriver string //required
InventoryDSN string //required
SkywardDSN string //required
APIKey string
ListenAddr string //addr format used for net.Dial; required
Prefix string //url prefix to mount api to without trailing slash
}
var config = &Config{}
func checkEmpty(val, name string) {
if val == "" {
log.Fatalf("INVENTORY_%s must be configured\n", name)
}
}
func init() {
err := envconfig.Process("INVENTORY", config)
if err != nil {
log.Fatalln("Error reading configuration from environment:", err)
}
if config.SessionExpiration == 0 {
config.SessionExpiration = 60
}
checkEmpty(config.LDAPServer, "LDAPSERVER")
if config.LDAPPort == 0 {
config.LDAPPort = 389
}
checkEmpty(config.LDAPBaseDN, "LDAPBASEDN")
switch strings.ToLower(config.LDAPSecurity) {
case "", "none":
config.ldapSecurity = auth.SecurityNone
case "tls":
config.ldapSecurity = auth.SecurityTLS
case "starttls":
config.ldapSecurity = auth.SecurityStartTLS
default:
log.Fatalln("Invalid INVENTORY_LDAPSECURITY:", config.LDAPSecurity)
}
checkEmpty(config.SQLDriver, "SQLDRIVER")
checkEmpty(config.InventoryDSN, "INVENTORYDSN")
checkEmpty(config.SkywardDSN, "SKYWARDDSN")
if config.SQLDriver == "mysql" && !strings.Contains(config.InventoryDSN, "?parseTime=true") {
log.Fatalln("mysql DSN must contain \"?parseTime=true\"")
}
checkEmpty(config.ListenAddr, "LISTENADDR")
}