/
config.go
216 lines (207 loc) · 7.46 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
// Package config parses the config file for the blobstore.
package config
import (
"fmt"
"net/url"
"strings"
"github.com/go-ini/ini"
)
const (
// ConfigLocation denotes the section of the *.ini file where the server configuration can
// be found
ConfigLocation = "BlobStore"
// KeyHost is the configuration key where the value is the server host
KeyHost = "host"
// KeyMongoHost is the configuration key where the value is the MongoDB host
KeyMongoHost = "mongodb-host"
// KeyMongoDatabase is the configuration key where the value is the MongoDB database
KeyMongoDatabase = "mongodb-database"
// KeyMongoUser is the configuration key where the value is the MongoDB user name
KeyMongoUser = "mongodb-user"
// KeyMongoPwd is the configuration key where the value is the MongoDB user pwd
KeyMongoPwd = "mongodb-pwd"
// KeyS3Host is the configuration key where the value is the S3 host
KeyS3Host = "s3-host"
// KeyS3Bucket is the configuration key where the value is the S3 bucket in which files will
// be stored.
KeyS3Bucket = "s3-bucket"
// KeyS3AccessKey is the configuration key where the value is the S3 access key
KeyS3AccessKey = "s3-access-key"
// KeyS3AccessSecret is the configuration key where the value is the S3 access secret
KeyS3AccessSecret = "s3-access-secret"
// KeyS3DisableSSL is the configuration key that determines whether SSL is to be used.
// any value other than 'true' is treated as false.
KeyS3DisableSSL = "s3-disable-ssl"
// KeyS3DisableSSLVerify is the configuration key that determines whether to verify the
// SSL certificate of the remote servers (in case a self-signed cert is used).
// any value other than 'true' is treated as false.
KeyS3DisableSSLVerify = "s3-disable-ssl-verify"
// KeyS3Region is the configuration key where the value is the S3 region
KeyS3Region = "s3-region"
// KeyAuthURL is the configuration key where the value is the KBase auth server URL
KeyAuthURL = "kbase-auth-url"
// KeyAuthAdminRoles is the configuration key where the value is comma-delimited auth server
// roles that denote that a user is a blobstore admin
KeyAuthAdminRoles = "kbase-auth-admin-roles"
// KeyDontTrustXIPHeaders is the configuration key where the value determines whether to
// distrust the X-Forwarded-For and X-Real-IP headers (true) or not (anything else).
KeyDontTrustXIPHeaders = "dont-trust-x-ip-headers"
)
// Config contains the server configuration.
type Config struct {
// Host is the host for the server, e.g. localhost:[port] or 0.0.0.0:[port]
Host string
// MongoHost is the host for the MongoDB database server.
MongoHost string
// MongoDatabase is the name of the MongoDB database to use.
MongoDatabase string
// MongoUser is the user name of the account to use to contact MongoDB.
MongoUser string
// MongoPwd is the password for the MongoDB account.
MongoPwd string
// S3Host is the host for the S3 API where files will be stored.
S3Host string
// S3Bucket the S3 bucket in which files will be stored.
S3Bucket string
// S3AccessKey is the S3 access key
S3AccessKey string
// S3AccessSecret is the S3 access secret
S3AccessSecret string
// S3DisableSSL determines whether SSL should be used
S3DisableSSL bool
// S3DisableSSLVerify determines whether SSL certs should be verified
S3DisableSSLVerify bool
// S3Region is the S3 region
S3Region string
// AuthURL is the KBase auth server URL. It is never nil.
AuthURL *url.URL
// AuthAdminRoles are the auth server roles that denote that a user is a blobstore admin.
// It is never nil but may be empty.
AuthAdminRoles *[]string
// DontTrustXIPHeaders determines whether to distrust the X-Forwarded-For and X-Real-IP
// headers.
DontTrustXIPHeaders bool
}
// New creates a new config struct from the given config file.
func New(configFilePath string) (*Config, error) {
errstr := "Error opening config file " + configFilePath + ": %s"
cfg, err := ini.Load(configFilePath)
if err != nil {
return nil, fmt.Errorf(errstr, strings.TrimSpace(err.Error()))
}
sec, err := cfg.GetSection(ConfigLocation)
if err != nil {
return nil, fmt.Errorf(errstr, err.Error())
}
host, err := getString(nil, configFilePath, sec, KeyHost, true)
mongohost, err := getString(err, configFilePath, sec, KeyMongoHost, true)
mongodb, err := getString(err, configFilePath, sec, KeyMongoDatabase, true)
mongouser, err := getString(err, configFilePath, sec, KeyMongoUser, false)
mongopwd, err := getString(err, configFilePath, sec, KeyMongoPwd, false)
s3host, err := getString(err, configFilePath, sec, KeyS3Host, true)
s3bucket, err := getString(err, configFilePath, sec, KeyS3Bucket, true)
s3key, err := getString(err, configFilePath, sec, KeyS3AccessKey, true)
s3secret, err := getString(err, configFilePath, sec, KeyS3AccessSecret, true)
s3disableSSL, err := getString(err, configFilePath, sec, KeyS3DisableSSL, false)
s3disableSSLVerify, err := getString(err, configFilePath, sec, KeyS3DisableSSLVerify, false)
s3region, err := getString(err, configFilePath, sec, KeyS3Region, true)
authurl, err := getURL(err, configFilePath, sec, KeyAuthURL)
roles, err := getStringList(err, configFilePath, sec, KeyAuthAdminRoles)
xip, err := getString(err, configFilePath, sec, KeyDontTrustXIPHeaders, false)
if err != nil {
return nil, err
}
if (mongouser == "") != (mongopwd == "") { // xor
return nil, fmt.Errorf(
"Either both or neither of %s and %s must be supplied in section %s of config file %s",
KeyMongoUser, KeyMongoPwd, sec.Name(), configFilePath)
}
return &Config{
Host: host,
MongoHost: mongohost,
MongoDatabase: mongodb,
MongoUser: mongouser,
MongoPwd: mongopwd,
S3Host: s3host,
S3Bucket: s3bucket,
S3AccessKey: s3key,
S3AccessSecret: s3secret,
S3DisableSSL: "true" == s3disableSSL,
S3DisableSSLVerify: "true" == s3disableSSLVerify,
S3Region: s3region,
AuthURL: authurl,
AuthAdminRoles: roles,
DontTrustXIPHeaders: "true" == xip,
},
nil
}
func getURL(
preverr error,
filepath string,
sec *ini.Section,
key string,
) (*url.URL, error) {
if preverr != nil {
return nil, preverr
}
putativeURL, err := getString(nil, filepath, sec, key, true)
if err != nil {
return nil, err
}
u, err := url.Parse(putativeURL)
if err != nil {
return nil, fmt.Errorf(
"Value for key %s in section %s of config file %s is not a valid url: %s",
key, sec.Name(), filepath, err)
}
return u, nil
}
func getString(
preverr error,
filepath string,
sec *ini.Section,
key string,
required bool,
) (string, error) {
if preverr != nil {
return "", preverr
}
s, err := sec.GetKey(key)
if err != nil {
if !required {
return "", nil
}
// there's only one error mode in the current source.
return "", fmt.Errorf("Missing key %s in section %s of config file %s",
key, sec.Name(), filepath)
}
v := strings.TrimSpace(s.Value())
if required && v == "" {
return "", fmt.Errorf("Missing value for key %s in section %s of config file %s",
key, sec.Name(), filepath)
}
return v, nil
}
func getStringList(
preverr error,
filepath string,
sec *ini.Section,
key string,
) (*[]string, error) {
if preverr != nil {
return nil, preverr
}
lst := []string{}
s, err := sec.GetKey(key)
if err != nil {
// there's only one error mode in the current source, which is a missing key.
return &lst, nil
}
for _, v := range strings.Split(s.Value(), ",") {
v = strings.TrimSpace(v)
if v != "" {
lst = append(lst, v)
}
}
return &lst, nil
}