-
Notifications
You must be signed in to change notification settings - Fork 38
/
config.go
227 lines (207 loc) · 7.22 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
package common
import (
"crypto/tls"
"errors"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"strconv"
"github.com/BurntSushi/toml"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/mitchellh/go-homedir"
)
var (
// Conf holds our configuration info
Conf TomlConfig
// PostgreSQL configuration info
pgConfig *pgxpool.Config
// Configuration info for the PostgreSQL job queue
listenConfig *pgx.ConnConfig
)
// ReadConfig reads the server configuration file.
func ReadConfig() (err error) {
// Override config file location via environment variables
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.
var userHome string
userHome, err = homedir.Dir()
if err != nil {
log.Printf("User home directory couldn't be determined: '%s'", err)
return
}
configFile = filepath.Join(userHome, ".dbhub", "config.toml")
}
// Reads the server configuration from disk
_, err = toml.DecodeFile(configFile, &Conf)
if err != nil {
return fmt.Errorf("Config file couldn't be parsed: %s", 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: %s", 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: %s", 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 == "production" {
missingConfig = append(missingConfig, "Minio access key string")
}
if Conf.Minio.Secret == "" && Conf.Environment.Environment == "production" {
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
}
// If an SMTP2Go environment variable is already set, don't mess with it.
tempString = os.Getenv("SMTP2GO_API_KEY")
if tempString != "" {
Conf.Event.Smtp2GoKey = tempString
} else {
// If this is a production environment, and the SMTP2Go env variable wasn't set, we'd better
// warn when the key isn't in the config file either
if Conf.Event.Smtp2GoKey == "" && Conf.Environment.Environment == "production" {
log.Printf("WARN: SMTP2Go API key isn't set in the config file. Event emails won't be sent.")
} else {
os.Setenv("SMTP2GO_API_KEY", Conf.Event.Smtp2GoKey)
}
}
// Check cache directory exists
_, err = os.Stat(Conf.DiskCache.Directory)
if errors.Is(err, fs.ErrNotExist) {
if os.MkdirAll(Conf.DiskCache.Directory, 0775) != nil {
return
}
}
// Set the main PostgreSQL database configuration values
pgConfig, err = pgxpool.ParseConfig(fmt.Sprintf("host=%s port=%d user= %s password = %s dbname=%s pool_max_conns=%d connect_timeout=10", Conf.Pg.Server, uint16(Conf.Pg.Port), Conf.Pg.Username, Conf.Pg.Password, Conf.Pg.Database, Conf.Pg.NumConnections))
if err != nil {
return
}
clientTLSConfig := tls.Config{}
if Conf.Environment.Environment == "production" {
clientTLSConfig.ServerName = Conf.Pg.Server
clientTLSConfig.InsecureSkipVerify = false
} else {
clientTLSConfig.InsecureSkipVerify = true
}
if Conf.Pg.SSL {
pgConfig.ConnConfig.TLSConfig = &clientTLSConfig
} else {
pgConfig.ConnConfig.TLSConfig = nil
}
// Create the connection string for the dedicated PostgreSQL notification connection
listenConfig, err = pgx.ParseConfig(fmt.Sprintf("host=%s port=%d user= %s password = %s dbname=%s connect_timeout=10", Conf.Pg.Server, uint16(Conf.Pg.Port), Conf.Pg.Username, Conf.Pg.Password, Conf.Pg.Database))
if err != nil {
return
}
listenTLSConfig := tls.Config{}
if Conf.Environment.Environment == "production" {
listenTLSConfig.ServerName = Conf.Pg.Server
listenTLSConfig.InsecureSkipVerify = false
} else {
listenTLSConfig.InsecureSkipVerify = true
}
if Conf.Pg.SSL {
listenConfig.TLSConfig = &listenTLSConfig
} else {
listenConfig.TLSConfig = nil
}
// Environment variable override for non-production logged-in user
tempString = os.Getenv("DBHUB_USERNAME")
if tempString != "" {
Conf.Environment.UserOverride = tempString
}
// The configuration file seems good
return
}