/
config.go
152 lines (131 loc) · 4.17 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
package chainlink
import (
"fmt"
"github.com/pelletier/go-toml/v2"
"github.com/smartcontractkit/chainlink/core/chains/starknet"
"github.com/smartcontractkit/chainlink/core/chains/terra"
evmcfg "github.com/smartcontractkit/chainlink/core/chains/evm/config/v2"
"github.com/smartcontractkit/chainlink/core/chains/solana"
config "github.com/smartcontractkit/chainlink/core/config/v2"
"github.com/smartcontractkit/chainlink/core/store/models"
)
// Config is the root type used for TOML configuration.
//
// See docs at /docs/CONFIG.md generated via config.GenerateDocs from /internal/config/docs.toml
//
// When adding a new field:
// - consider including a unit suffix with the field name
// - TOML is limited to int64/float64, so fields requiring greater range/precision must use non-standard types
// implementing encoding.TextMarshaler/TextUnmarshaler, like utils.Big and decimal.Decimal
// - std lib types that don't implement encoding.TextMarshaler/TextUnmarshaler (time.Duration, url.URL, big.Int) won't
// work as expected, and require wrapper types. See models.Duration, models.URL, utils.Big.
type Config struct {
config.Core
EVM evmcfg.EVMConfigs `toml:",omitempty"`
Solana solana.SolanaConfigs `toml:",omitempty"`
Starknet starknet.StarknetConfigs `toml:",omitempty"`
Terra terra.TerraConfigs `toml:",omitempty"`
}
// TOMLString returns a TOML encoded string.
func (c *Config) TOMLString() (string, error) {
b, err := toml.Marshal(c)
if err != nil {
return "", err
}
return string(b), nil
}
func (c *Config) Validate() error {
if err := config.Validate(c); err != nil {
return fmt.Errorf("invalid configuration: %w", err)
}
return nil
}
// setDefaults initializes unset fields with default values.
func (c *Config) setDefaults() {
core := config.CoreDefaults()
core.SetFrom(&c.Core)
c.Core = core
for i := range c.EVM {
if input := c.EVM[i]; input == nil {
c.EVM[i] = &evmcfg.EVMConfig{Chain: evmcfg.Defaults(nil)}
} else {
input.Chain = evmcfg.Defaults(input.ChainID, &input.Chain)
}
}
for i := range c.Solana {
if c.Solana[i] == nil {
c.Solana[i] = new(solana.SolanaConfig)
}
c.Solana[i].Chain.SetDefaults()
}
for i := range c.Starknet {
if c.Starknet[i] == nil {
c.Starknet[i] = new(starknet.StarknetConfig)
}
c.Starknet[i].Chain.SetDefaults()
}
for i := range c.Terra {
if c.Terra[i] == nil {
c.Terra[i] = new(terra.TerraConfig)
}
c.Terra[i].Chain.SetDefaults()
}
}
func (c *Config) SetFrom(f *Config) {
c.Core.SetFrom(&f.Core)
c.EVM.SetFrom(&f.EVM)
c.Solana.SetFrom(&f.Solana)
c.Starknet.SetFrom(&f.Starknet)
c.Terra.SetFrom(&f.Terra)
}
type Secrets struct {
config.Secrets
}
// TOMLString returns a TOML encoded string with secret values redacted.
func (s *Secrets) TOMLString() (string, error) {
b, err := toml.Marshal(s)
if err != nil {
return "", err
}
return string(b), nil
}
func (s *Secrets) Validate() error {
if err := config.Validate(s); err != nil {
return fmt.Errorf("invalid secrets: %w", err)
}
return nil
}
// setEnv overrides fields from ENV vars, if present.
func (s *Secrets) setEnv() error {
if dbURL := config.EnvDatabaseURL.Get(); dbURL != "" {
s.Database.URL = new(models.SecretURL)
if err := s.Database.URL.UnmarshalText([]byte(dbURL)); err != nil {
return err
}
}
if dbBackupUrl := config.EnvDatabaseBackupURL.Get(); dbBackupUrl != "" {
s.Database.BackupURL = new(models.SecretURL)
if err := s.Database.BackupURL.UnmarshalText([]byte(dbBackupUrl)); err != nil {
return err
}
}
if config.EnvDatabaseAllowSimplePasswords.IsTrue() {
s.Database.AllowSimplePasswords = true
}
if explorerKey := config.EnvExplorerAccessKey.Get(); explorerKey != "" {
s.Explorer.AccessKey = &explorerKey
}
if explorerSecret := config.EnvExplorerSecret.Get(); explorerSecret != "" {
s.Explorer.Secret = &explorerSecret
}
if keystorePassword := config.EnvPasswordKeystore.Get(); keystorePassword != "" {
s.Password.Keystore = &keystorePassword
}
if vrfPassword := config.EnvPasswordVRF.Get(); vrfPassword != "" {
s.Password.VRF = &vrfPassword
}
if pyroscopeAuthToken := config.EnvPyroscopeAuthToken.Get(); pyroscopeAuthToken != "" {
s.Pyroscope.AuthToken = &pyroscopeAuthToken
}
return nil
}