-
Notifications
You must be signed in to change notification settings - Fork 39
/
config.go
176 lines (159 loc) · 5.44 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
package common
import (
"crypto/tls"
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"github.com/BurntSushi/toml"
"github.com/jackc/pgx"
"github.com/mitchellh/go-homedir"
)
var (
// Our configuration info
Conf TomlConfig
// PostgreSQL configuration info
pgConfig = new(pgx.ConnConfig)
)
// Read the server configuration file.
func ReadConfig() error {
// Override config file location via environment variables
var err error
configFile := os.Getenv("CONFIG_FILE")
if configFile == "" {
// TODO: Might be a good idea to add permission checks of the dir & conf file, to ensure they're not
// world readable. Similar in concept to what ssh does for its config files.
userHome, err := homedir.Dir()
if err != nil {
log.Fatalf("User home directory couldn't be determined: %s", "\n")
}
configFile = filepath.Join(userHome, ".dbhub", "config.toml")
}
// Reads the server configuration from disk
if _, err := toml.DecodeFile(configFile, &Conf); err != nil {
return fmt.Errorf("Config file couldn't be parsed: %v\n", err)
}
// Override config file via environment variables
tempString := os.Getenv("MINIO_SERVER")
if tempString != "" {
Conf.Minio.Server = tempString
}
tempString = os.Getenv("MINIO_ACCESS_KEY")
if tempString != "" {
Conf.Minio.AccessKey = tempString
}
tempString = os.Getenv("MINIO_SECRET")
if tempString != "" {
Conf.Minio.Secret = tempString
}
tempString = os.Getenv("MINIO_HTTPS")
if tempString != "" {
Conf.Minio.HTTPS, err = strconv.ParseBool(tempString)
if err != nil {
return fmt.Errorf("Failed to parse MINIO_HTTPS: %v\n", err)
}
}
tempString = os.Getenv("PG_SERVER")
if tempString != "" {
Conf.Pg.Server = tempString
}
tempString = os.Getenv("PG_PORT")
if tempString != "" {
tempInt, err := strconv.ParseInt(tempString, 10, 0)
if err != nil {
return fmt.Errorf("Failed to parse PG_PORT: %v\n", err)
}
Conf.Pg.Port = int(tempInt)
}
tempString = os.Getenv("PG_USER")
if tempString != "" {
Conf.Pg.Username = tempString
}
tempString = os.Getenv("PG_PASS")
if tempString != "" {
Conf.Pg.Password = tempString
}
tempString = os.Getenv("PG_DBNAME")
if tempString != "" {
Conf.Pg.Database = tempString
}
// Verify we have the needed configuration information
// Note - We don't check for a valid Conf.Pg.Password here, as the PostgreSQL password can also be kept
// in a .pgpass file as per https://www.postgresql.org/docs/current/static/libpq-pgpass.html
var missingConfig []string
if Conf.Minio.Server == "" {
missingConfig = append(missingConfig, "Minio server:port string")
}
if Conf.Minio.AccessKey == "" && Conf.Environment.Environment != "docker" {
missingConfig = append(missingConfig, "Minio access key string")
}
if Conf.Minio.Secret == "" && Conf.Environment.Environment != "docker" {
missingConfig = append(missingConfig, "Minio secret string")
}
if Conf.Pg.Server == "" {
missingConfig = append(missingConfig, "PostgreSQL server string")
}
if Conf.Pg.Port == 0 {
missingConfig = append(missingConfig, "PostgreSQL port number")
}
if Conf.Pg.Username == "" {
missingConfig = append(missingConfig, "PostgreSQL username string")
}
if Conf.Pg.Database == "" {
missingConfig = append(missingConfig, "PostgreSQL database string")
}
if len(missingConfig) > 0 {
// Some config is missing
returnMessage := fmt.Sprint("Missing or incomplete value(s):\n")
for _, value := range missingConfig {
returnMessage += fmt.Sprintf("\n \t→ %v", value)
}
return fmt.Errorf(returnMessage)
}
// Warn if the certificate validity period isn't set in the config file
if Conf.Sign.CertDaysValid == 0 {
log.Printf("WARN: Cert validity period for cert signing isn't set in the config file. Defaulting to 60 days.")
Conf.Sign.CertDaysValid = 60
}
// Warn if the default Memcache cache time isn't set in the config file
if Conf.Memcache.DefaultCacheTime == 0 {
log.Printf("WARN: Default Memcache cache time isn't set in the config file. Defaulting to 30 days.")
Conf.Memcache.DefaultCacheTime = 2592000
}
// Warn if the view count flush delay isn't set in the config file
if Conf.Memcache.ViewCountFlushDelay == 0 {
log.Printf("WARN: Memcache view count flush delay isn't set in the config file. Defaulting to 2 minutes.")
Conf.Memcache.ViewCountFlushDelay = 120
}
// Warn if the event processing loop delay isn't set in the config file
if Conf.Event.Delay == 0 {
log.Printf("WARN: Event processing delay isn't set in the config file. Defaulting to 3 seconds.")
Conf.Event.Delay = 3
}
// Warn if the email queue processing isn't set in the config file
if Conf.Event.EmailQueueProcessingDelay == 0 {
log.Printf("WARN: Email queue processing delay isn't set in the config file. Defaulting to 10 seconds.")
Conf.Event.EmailQueueProcessingDelay = 10
}
// Warn if the email queue directory isn't set in the config file
if Conf.Event.EmailQueueDir == "" {
log.Printf("WARN: Email queue directory isn't set in the config file. Defaulting to /tmp.")
Conf.Event.EmailQueueDir = "/tmp"
}
// Set the PostgreSQL configuration values
pgConfig.Host = Conf.Pg.Server
pgConfig.Port = uint16(Conf.Pg.Port)
pgConfig.User = Conf.Pg.Username
pgConfig.Password = Conf.Pg.Password
pgConfig.Database = Conf.Pg.Database
clientTLSConfig := tls.Config{InsecureSkipVerify: true}
if Conf.Pg.SSL {
pgConfig.TLSConfig = &clientTLSConfig
} else {
pgConfig.TLSConfig = nil
}
// TODO: Add environment variable overrides for memcached
// The configuration file seems good
return nil
}