-
Notifications
You must be signed in to change notification settings - Fork 13
/
configuration.go
277 lines (238 loc) · 8.81 KB
/
configuration.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
// Copyright (c) 2014-2018 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/bitmark-inc/bitmarkd/chain"
"github.com/bitmark-inc/bitmarkd/configuration"
"github.com/bitmark-inc/bitmarkd/payment"
"github.com/bitmark-inc/bitmarkd/peer"
"github.com/bitmark-inc/bitmarkd/proof"
"github.com/bitmark-inc/bitmarkd/publish"
"github.com/bitmark-inc/bitmarkd/rpc"
"github.com/bitmark-inc/bitmarkd/util"
"github.com/bitmark-inc/logger"
)
// basic defaults (directories and files are relative to the "DataDirectory" from Configuration file)
const (
defaultDataDirectory = "" // this will error; use "." for the same directory as the config file
defaultPeerPublicKeyFile = "peer.private"
defaultPeerPrivateKeyFile = "peer.public"
defaultProofPublicKeyFile = "proof.private"
defaultProofPrivateKeyFile = "proof.public"
defaultProofSigningKeyFile = "proof.sign"
defaultKeyFile = "rpc.key"
defaultCertificateFile = "rpc.crt"
defaultLevelDBDirectory = "data"
defaultBitmarkDatabase = chain.Bitmark
defaultTestingDatabase = chain.Testing
defaultLocalDatabase = chain.Local
defaultBitmarkPeerFile = "peers-" + chain.Bitmark + ".json"
defaultTestingPeerFile = "peers-" + chain.Testing + ".json"
defaultLocalPeerFile = "peers-" + chain.Local + ".json"
defaultBitmarkReservoirFile = "reservoir-" + chain.Bitmark + ".cache"
defaultTestingReservoirFile = "reservoir-" + chain.Testing + ".cache"
defaultLocalReservoirFile = "reservoir-" + chain.Local + ".cache"
defaultLogDirectory = "log"
defaultLogFile = "bitmarkd.log"
defaultLogCount = 10 // number of log files retained
defaultLogSize = 1024 * 1024 // rotate when <logfile> exceeds this size
defaultRPCClients = 10
defaultPeers = 125
defaultMines = 125
)
// to hold log levels
type LoglevelMap map[string]string
// path expanded or calculated defaults
var (
defaultLogLevels = LoglevelMap{
logger.DefaultTag: "critical",
}
)
type HTTPSType struct {
MaximumConnections int `libucl:"maximum_connections" json:"maximum_connections"`
Listen []string `libucl:"listen" json:"listen"`
StatusAllowIP []string `libucl:"status_allow_ip" json:"status_allow_ip"`
Certificate string `libucl:"certificate" json:"certificate"`
PrivateKey string `libucl:"private_key" json:"private_key"`
}
type DatabaseType struct {
Directory string `libucl:"directory" json:"directory"`
Name string `libucl:"name" json:"name"`
}
type Configuration struct {
DataDirectory string `libucl:"data_directory" json:"data_directory"`
PidFile string `libucl:"pidfile" json:"pidfile"`
Chain string `libucl:"chain" json:"chain"`
Nodes string `libucl:"nodes" json:"nodes"`
Database DatabaseType `libucl:"database" json:"database"`
PeerFile string `libucl:"peer_file" json:"peer_file"`
ReservoirFile string `libucl:"reservoir_file" json:"reservoir_file"`
ClientRPC rpc.RPCConfiguration `libucl:"client_rpc" json:"client_rpc"`
HttpsRPC rpc.HTTPSConfiguration `libucl:"https_rpc" json:"https_rpc"`
Peering peer.Configuration `libucl:"peering" json:"peering"`
Publishing publish.Configuration `libucl:"publishing" json:"publishing"`
Proofing proof.Configuration `libucl:"proofing" json:"proofing"`
Payment payment.Configuration `libucl:"payment" json:"payment"`
Logging logger.Configuration `libucl:"logging" json:"logging"`
}
// will read decode and verify the configuration
func getConfiguration(configurationFileName string, variables map[string]string) (*Configuration, error) {
configurationFileName, err := filepath.Abs(filepath.Clean(configurationFileName))
if nil != err {
return nil, err
}
// absolute path to the main directory
dataDirectory, _ := filepath.Split(configurationFileName)
options := &Configuration{
DataDirectory: defaultDataDirectory,
PidFile: "", // no PidFile by default
Chain: chain.Bitmark,
PeerFile: defaultBitmarkPeerFile,
ReservoirFile: defaultBitmarkReservoirFile,
Database: DatabaseType{
Directory: defaultLevelDBDirectory,
Name: defaultBitmarkDatabase,
},
ClientRPC: rpc.RPCConfiguration{
MaximumConnections: defaultRPCClients,
Certificate: defaultCertificateFile,
PrivateKey: defaultKeyFile,
},
// default: share config with normal RPC
HttpsRPC: rpc.HTTPSConfiguration{
MaximumConnections: defaultRPCClients,
Certificate: defaultCertificateFile,
PrivateKey: defaultKeyFile,
},
Peering: peer.Configuration{
//MaximumConnections: defaultPeers,
PublicKey: defaultPeerPublicKeyFile,
PrivateKey: defaultPeerPrivateKeyFile,
},
Publishing: publish.Configuration{
PublicKey: defaultPeerPublicKeyFile,
PrivateKey: defaultPeerPrivateKeyFile,
},
Proofing: proof.Configuration{
//MaximumConnections: defaultProofers,
PublicKey: defaultProofPublicKeyFile,
PrivateKey: defaultProofPrivateKeyFile,
SigningKey: defaultProofSigningKeyFile,
},
Logging: logger.Configuration{
Directory: defaultLogDirectory,
File: defaultLogFile,
Size: defaultLogSize,
Count: defaultLogCount,
Levels: defaultLogLevels,
},
}
if err := configuration.ParseConfigurationFile(configurationFileName, options, variables); err != nil {
return nil, err
}
// if any test mode and the database file was not specified
// switch to appropriate default. Abort if then chain name is
// not recognised.
options.Chain = strings.ToLower(options.Chain)
if !chain.Valid(options.Chain) {
return nil, errors.New(fmt.Sprintf("Chain: %q is not supported", options.Chain))
}
// if database was not changed from default
if options.Database.Name == defaultBitmarkDatabase {
switch options.Chain {
case chain.Bitmark:
// already correct default
case chain.Testing:
options.Database.Name = defaultTestingDatabase
options.PeerFile = defaultTestingPeerFile
options.ReservoirFile = defaultTestingReservoirFile
case chain.Local:
options.Database.Name = defaultLocalDatabase
options.PeerFile = defaultLocalPeerFile
options.ReservoirFile = defaultLocalReservoirFile
default:
return nil, errors.New(fmt.Sprintf("Chain: %s no default database setting", options.Chain))
}
}
// ensure absolute data directory
if "" == options.DataDirectory || "~" == options.DataDirectory {
return nil, errors.New(fmt.Sprintf("Path: %q is not a valid directory", options.DataDirectory))
} else if "." == options.DataDirectory {
options.DataDirectory = dataDirectory // same directory as the configuration file
} else {
options.DataDirectory = filepath.Clean(options.DataDirectory)
}
// this directory must exist - i.e. must be created prior to running
if fileInfo, err := os.Stat(options.DataDirectory); nil != err {
return nil, err
} else if !fileInfo.IsDir() {
return nil, errors.New(fmt.Sprintf("Path: %q is not a directory", options.DataDirectory))
}
// force all relevant items to be absolute paths
// if not, assign them to the data directory
mustBeAbsolute := []*string{
&options.PeerFile,
&options.ReservoirFile,
&options.Database.Directory,
&options.ClientRPC.Certificate,
&options.ClientRPC.PrivateKey,
&options.HttpsRPC.Certificate,
&options.HttpsRPC.PrivateKey,
&options.Peering.PublicKey,
&options.Peering.PrivateKey,
&options.Publishing.PublicKey,
&options.Publishing.PrivateKey,
&options.Proofing.PublicKey,
&options.Proofing.PrivateKey,
&options.Proofing.SigningKey,
&options.Logging.Directory,
}
for _, f := range mustBeAbsolute {
*f = util.EnsureAbsolute(options.DataDirectory, *f)
}
// optional absolute paths i.e. blank or an absolute path
optionalAbsolute := []*string{
&options.PidFile,
}
for _, f := range optionalAbsolute {
if "" != *f {
*f = util.EnsureAbsolute(options.DataDirectory, *f)
}
}
// fail if any of these are not simple file names i.e. must
// not contain path seperator, then add the correct directory
// prefix, file item is first and corresponding directory is
// second (or nil if no prefix can be added)
mustNotBePaths := [][2]*string{
{&options.Database.Name, &options.Database.Directory},
{&options.Logging.File, nil},
}
for _, f := range mustNotBePaths {
switch filepath.Dir(*f[0]) {
case "", ".":
if nil != f[1] {
*f[0] = util.EnsureAbsolute(*f[1], *f[0])
}
default:
return nil, errors.New(fmt.Sprintf("Files: %q is not plain name", *f[0]))
}
}
// make absolute and create directories if they do not already exist
for _, d := range []*string{
&options.Database.Directory,
&options.Logging.Directory,
} {
*d = util.EnsureAbsolute(options.DataDirectory, *d)
if err := os.MkdirAll(*d, 0700); nil != err {
return nil, err
}
}
// done
return options, nil
}