-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
86 lines (73 loc) · 2.17 KB
/
config.go
File metadata and controls
86 lines (73 loc) · 2.17 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
package offchain
import (
"fmt"
"log/slog"
"os"
"slices"
"github.com/cordialsys/offchain/pkg/secret"
"github.com/ilyakaznacheev/cleanenv"
"github.com/sirupsen/logrus"
)
type Config struct {
Exchanges map[ExchangeId]*ExchangeConfig `yaml:"exchanges"`
}
func (cfg *Config) Init() error {
for key, exchange := range cfg.Exchanges {
if !slices.Contains(ValidExchangeIds, key) {
return fmt.Errorf("invalid exchange id: %s", key)
}
if exchange == nil {
return fmt.Errorf("empty config for exchange: %s", key)
}
exchange.ExchangeId = key
}
return nil
}
func (c *Config) GetExchange(id ExchangeId) (*ExchangeConfig, bool) {
exchange, ok := c.Exchanges[id]
if !ok {
return nil, false
}
return exchange, true
}
const ENV_OFFCHAIN_CONFIG = "OFFCHAIN_CONFIG"
func LoadUnvalidatedConfig(configPathMaybe string) (*Config, error) {
type configsection struct {
Offchain Config `yaml:"offchain"`
}
section := configsection{}
if configPathMaybe == "" {
if v := os.Getenv(ENV_OFFCHAIN_CONFIG); v != "" {
configPathMaybe = v
}
}
if configPathMaybe == "" {
return nil, fmt.Errorf("config path is required (maybe set %s)", ENV_OFFCHAIN_CONFIG)
}
logrus.WithField("config", configPathMaybe).Info("loading configuration")
err := cleanenv.ReadConfig(configPathMaybe, §ion)
if err != nil {
return nil, fmt.Errorf("could not read configuration: %v", err)
}
err = section.Offchain.Init()
if err != nil {
return nil, fmt.Errorf("invalid config: %v", err)
}
for key, exchange := range section.Offchain.Exchanges {
exchange.ExchangeId = key
if exchange.ApiKeyRef.IsType(secret.Raw) {
slog.Warn("raw api-key in config file is unsafe, should use a secret manager", "exchange", key)
}
if exchange.SecretKeyRef.IsType(secret.Raw) {
slog.Warn("raw secret-key in config file is unsafe, should use a secret manager", "exchange", key)
}
if exchange.PassphraseRef.IsType(secret.Raw) {
slog.Warn("raw passphrase in config file is unsafe, should use a secret manager", "exchange", key)
}
}
for _, exchange := range section.Offchain.Exchanges {
// apply defaults to fields not overridden by user
ApplyDefaults(exchange)
}
return §ion.Offchain, nil
}