-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathconfig.go
More file actions
259 lines (232 loc) · 9.01 KB
/
Copy pathconfig.go
File metadata and controls
259 lines (232 loc) · 9.01 KB
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
// Copyright 2024 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package config
import (
"errors"
"os"
"path/filepath"
"strings"
"github.com/joho/godotenv"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/sourcenetwork/corelog"
)
const (
ConfigStoreBadger = "badger"
ConfigStoreMemory = "memory"
ConfigLogFormatJSON = "json"
ConfigLogFormatCSV = "csv"
ConfigLogLevelInfo = "info"
ConfigLogLevelDebug = "debug"
ConfigLogLevelError = "error"
ConfigLogLevelFatal = "fatal"
// defaultTLSCertDir is the directory, relative to the rootdir, that is
// searched for the default TLS certificate and key files.
defaultTLSCertDir = "certs"
// defaultTLSCertFile and defaultTLSKeyFile are the certificate and private
// key file names looked up under <rootdir>/certs to automatically enable
// TLS for the HTTP API.
defaultTLSCertFile = "server.crt"
defaultTLSKeyFile = "server.key"
)
// ConfigPaths are config keys that will be made relative to the rootdir
var ConfigPaths = []string{
"datastore.path",
"api.pubkeypath",
"api.privkeypath",
"keyring.path",
}
// ConfigFlags is a mapping of cli flag names to config keys to bind.
var ConfigFlags = map[string]string{
"log-level": "log.level",
"log-output": "log.output",
"log-format": "log.format",
"log-stacktrace": "log.stacktrace",
"log-source": "log.source",
"log-overrides": "log.overrides",
"no-log-color": "log.colordisabled",
"url": "api.address",
"audience": "api.audience",
"max-txn-retries": "datastore.maxtxnretries",
"store": "datastore.store",
"no-encryption": "datastore.noencryption",
"no-searchable-encryption": "datastore.nosearchableencryption",
"no-signing": "datastore.nosigning",
"default-key-type": "datastore.defaultkeytype",
"valuelogfilesize": "datastore.badger.valuelogfilesize",
"peers": "net.peers",
"p2paddr": "net.p2paddresses",
"no-p2p": "net.p2pdisabled",
"pubsub": "net.pubsubenabled",
"relay": "net.relay",
"p2p-block-sync-timeout": "net.p2pblocksynctimeout",
"allowed-origins": "api.allowed-origins",
"pubkeypath": "api.pubkeypath",
"privkeypath": "api.privkeypath",
"keyring-namespace": "keyring.namespace",
"keyring-backend": "keyring.backend",
"keyring-path": "keyring.path",
"no-keyring": "keyring.disabled",
"document-acp-type": "acp.document.type",
"source-hub-address": "acp.document.sourceHub.address",
"development": "development",
"secret-file": "secretfile",
"no-telemetry": "telemetry.disabled",
"replicator-retry-intervals": "replicator.retryintervals",
}
// ConfigDefaults contains default values for config entries.
var ConfigDefaults = map[string]any{
"api.address": "127.0.0.1:9181",
"api.audience": "",
"api.allowed-origins": []string{},
"datastore.path": "data",
"datastore.maxtxnretries": 5,
"datastore.store": "badger",
"datastore.badger.valuelogfilesize": 1 << 30,
"development": false,
"net.p2pdisabled": false,
"net.p2paddresses": []string{"/ip4/127.0.0.1/tcp/9171"},
"net.peers": []string{},
"net.pubSubEnabled": true,
"net.relay": false,
"net.p2pblocksynctimeout": 30,
"keyring.backend": "file",
"keyring.disabled": false,
"keyring.namespace": "defradb",
"keyring.path": "keys",
"log.caller": false,
"log.colordisabled": false,
"log.format": "text",
"log.level": "info",
"log.output": "stderr",
"log.source": false,
"log.stacktrace": false,
"secretfile": ".env",
"telemetry.disabled": false,
"datastore.nosigning": false,
"datastore.nosearchableencryption": false,
"datastore.defaultkeytype": "secp256k1",
"acp.document.type": "local",
"replicator.retryintervals": []int{30, 60, 120, 240, 480, 960, 1920},
}
// DefaultConfig returns a new config with default values.
func DefaultConfig() *viper.Viper {
cfg := viper.New()
cfg.AutomaticEnv()
cfg.SetEnvPrefix("DEFRA")
cfg.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
cfg.SetConfigName("config")
cfg.SetConfigType("yaml")
for key, val := range ConfigDefaults {
cfg.SetDefault(key, val)
}
return cfg
}
// CreateConfig writes the default config file if one does not exist.
func CreateConfig(rootdir string, flags *pflag.FlagSet) error {
cfg := DefaultConfig()
cfg.AddConfigPath(rootdir)
if err := bindConfigFlags(cfg, flags); err != nil {
return err
}
// make sure rootdir exists
if err := os.MkdirAll(rootdir, 0755); err != nil {
return err
}
err := cfg.SafeWriteConfig()
// error type is known and shouldn't be wrapped
//
//nolint:errorlint
if _, ok := err.(viper.ConfigFileAlreadyExistsError); ok {
return nil
}
return err
}
// LoadConfig returns a new config with values from the config in the given rootdir.
func LoadConfig(rootdir string, flags *pflag.FlagSet) (*viper.Viper, error) {
cfg := DefaultConfig()
cfg.AddConfigPath(rootdir)
// attempt to read the existing config
err := cfg.ReadInConfig()
// error type is known and shouldn't be wrapped
//
//nolint:errorlint
if _, ok := err.(viper.ConfigFileNotFoundError); err != nil && !ok {
return nil, err
}
// bind cli flags to config keys
if err := bindConfigFlags(cfg, flags); err != nil {
return nil, err
}
// make paths relative to the rootdir
for _, key := range ConfigPaths {
path := cfg.GetString(key)
if path != "" && !filepath.IsAbs(path) {
cfg.Set(key, filepath.Join(rootdir, path))
}
}
// load environment variables from .env file if one exists
err = godotenv.Load(cfg.GetString("secretfile"))
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, err
}
// set logging config
corelog.SetConfig(corelog.Config{
Level: cfg.GetString("log.level"),
Format: cfg.GetString("log.format"),
Output: cfg.GetString("log.output"),
EnableStackTrace: cfg.GetBool("log.stacktrace"),
EnableSource: cfg.GetBool("log.source"),
DisableColor: cfg.GetBool("log.colordisabled"),
})
// set logging config overrides
corelog.SetConfigOverrides(cfg.GetString("log.overrides"))
// Automatically enable TLS when the default certificate files are present
// and the user has not configured certificate paths explicitly. Done after
// the logging config is applied so any warning is formatted as configured.
autoDetectTLSCertPaths(cfg, rootdir)
return cfg, nil
}
// autoDetectTLSCertPaths sets the TLS certificate and key paths from the
// default certs directory (<rootdir>/certs) when the user has not configured
// them explicitly. It sets whichever of server.crt (pubkeypath) and server.key
// (privkeypath) are present: when both exist TLS is enabled, and when only one
// exists the resulting incomplete pair is rejected by the start command, so a
// half-populated certs directory is a clear error rather than a silently
// ignored certificate.
func autoDetectTLSCertPaths(cfg *viper.Viper, rootdir string) {
// Respect explicitly-configured paths (flag, config file, or env var); if
// either is set the user is in control of TLS and we must not override it.
if cfg.GetString("api.pubkeypath") != "" || cfg.GetString("api.privkeypath") != "" {
return
}
certPath := filepath.Join(rootdir, defaultTLSCertDir, defaultTLSCertFile)
keyPath := filepath.Join(rootdir, defaultTLSCertDir, defaultTLSKeyFile)
if isRegularFile(certPath) {
cfg.Set("api.pubkeypath", certPath)
}
if isRegularFile(keyPath) {
cfg.Set("api.privkeypath", keyPath)
}
}
// isRegularFile reports whether the path exists and is a regular file
// (symlinks are followed). Directories and other irregular files return false.
func isRegularFile(path string) bool {
info, err := os.Stat(path)
return err == nil && info.Mode().IsRegular()
}
// bindConfigFlags binds the set of cli flags to config values.
func bindConfigFlags(cfg *viper.Viper, flags *pflag.FlagSet) error {
var errs []error
flags.VisitAll(func(f *pflag.Flag) {
errs = append(errs, cfg.BindPFlag(ConfigFlags[f.Name], f))
})
return errors.Join(errs...)
}