Skip to content

Commit ba8f183

Browse files
committed
feat(config): add secret support
1 parent 85ee681 commit ba8f183

9 files changed

Lines changed: 115 additions & 83 deletions

File tree

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ env:
8585
@echo export CGO_LDFLAGS=\"$(CGO_LDFLAGS)\"
8686
@echo export CARTESI_LOG_LEVEL="info"
8787
@echo export CARTESI_BLOCKCHAIN_DEFAULT_BLOCK="latest"
88-
@echo export CARTESI_BLOCKCHAIN_HTTP_ENDPOINT="http://localhost:8545"
89-
@echo export CARTESI_BLOCKCHAIN_WS_ENDPOINT="ws://localhost:8545"
88+
@echo export CARTESI_BLOCKCHAIN_HTTP_ENDPOINT="test/secrets/blockchain_http_endpoint.txt"
89+
@echo export CARTESI_BLOCKCHAIN_WS_ENDPOINT="test/secrets/blockchain_ws_endpoint.txt"
9090
@echo export CARTESI_BLOCKCHAIN_ID="13370"
9191
@echo export CARTESI_CONTRACTS_INPUT_BOX_ADDRESS="0xc70074BDD26d8cF983Ca6A5b89b8db52D5850051"
9292
@echo export CARTESI_CONTRACTS_AUTHORITY_FACTORY_ADDRESS="0xC7003566dD09Aa0fC0Ce201aC2769aFAe3BF0051"
9393
@echo export CARTESI_CONTRACTS_APPLICATION_FACTORY_ADDRESS="0xc7006f70875BaDe89032001262A846D3Ee160051"
9494
@echo export CARTESI_CONTRACTS_SELF_HOSTED_APPLICATION_FACTORY_ADDRESS="0xc700285Ab555eeB5201BC00CFD4b2CC8DED90051"
95-
@echo export CARTESI_AUTH_MNEMONIC=\"test test test test test test test test test test test junk\"
95+
@echo export CARTESI_AUTH_MNEMONIC_FILE="test/secrets/auth_mnemonic.txt"
9696
@echo export CARTESI_DATABASE_CONNECTION="postgres://postgres:password@localhost:5432/rollupsdb?sslmode=disable"
9797
@echo export CARTESI_SNAPSHOTS_DIR="snapshots"
9898
@echo export CARTESI_TEST_DATABASE_CONNECTION="postgres://test_user:password@localhost:5432/test_rollupsdb?sslmode=disable"

internal/config/auth/auth.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"context"
88
"fmt"
99
"math/big"
10-
"os"
1110

1211
"github.com/ethereum/go-ethereum/accounts/abi/bind"
1312
"github.com/ethereum/go-ethereum/core/types"
@@ -42,24 +41,6 @@ func GetTransactOpts(chainId *big.Int) (*bind.TransactOpts, error) {
4241
return nil, err
4342
}
4443
return bind.NewKeyedTransactorWithChainID(privateKey, chainId)
45-
case AuthKindMnemonicFile:
46-
mnemonicFile, err := GetAuthMnemonicFile()
47-
if err != nil {
48-
return nil, err
49-
}
50-
mnemonic, err := os.ReadFile(mnemonicFile)
51-
if err != nil {
52-
return nil, err
53-
}
54-
accountIndex, err := GetAuthMnemonicAccountIndex()
55-
if err != nil {
56-
return nil, err
57-
}
58-
privateKey, err := ethutil.MnemonicToPrivateKey(string(mnemonic), accountIndex.Value)
59-
if err != nil {
60-
return nil, err
61-
}
62-
return bind.NewKeyedTransactorWithChainID(privateKey, chainId)
6344
case AuthKindPrivateKeyVar:
6445
privateKey, err := GetAuthPrivateKey()
6546
if err != nil {
@@ -70,20 +51,6 @@ func GetTransactOpts(chainId *big.Int) (*bind.TransactOpts, error) {
7051
return nil, err
7152
}
7253
return bind.NewKeyedTransactorWithChainID(key, chainId)
73-
case AuthKindPrivateKeyFile:
74-
privateKeyFile, err := GetAuthPrivateKeyFile()
75-
if err != nil {
76-
return nil, err
77-
}
78-
privateKey, err := os.ReadFile(privateKeyFile)
79-
if err != nil {
80-
return nil, err
81-
}
82-
key, err := crypto.HexToECDSA(ethutil.TrimHex(string(privateKey)))
83-
if err != nil {
84-
return nil, err
85-
}
86-
return bind.NewKeyedTransactorWithChainID(key, chainId)
8754
case AuthKindAWS:
8855
awsc, err := aws_cfg.LoadDefaultConfig(context.Background())
8956
if err != nil {

internal/config/generate/Config.toml

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,14 @@ An unique identifier representing a blockchain network."""
100100
used-by = ["evmreader", "claimer", "node"]
101101

102102
[blockchain.CARTESI_BLOCKCHAIN_HTTP_ENDPOINT]
103+
file = true
103104
go-type = "URL"
104105
description = """
105106
HTTP endpoint for the blockchain RPC provider."""
106107
used-by = ["evmreader", "claimer", "node"]
107108

108109
[blockchain.CARTESI_BLOCKCHAIN_WS_ENDPOINT]
110+
file = true
109111
go-type = "URL"
110112
description = """
111113
WebSocket endpoint for the blockchain RPC provider."""
@@ -220,34 +222,21 @@ omit = true
220222
used-by = ["claimer", "node", "cli"]
221223

222224
[auth.CARTESI_AUTH_PRIVATE_KEY]
225+
file = true
223226
go-type = "RedactedString"
224227
description = """
225228
The node will use this private key to sign transactions."""
226229
omit = true
227230
used-by = ["claimer", "node", "cli"]
228231

229-
[auth.CARTESI_AUTH_PRIVATE_KEY_FILE]
230-
go-type = "string"
231-
description = """
232-
The node will use the private key contained in this file to sign transactions."""
233-
omit = true
234-
used-by = ["claimer", "node", "cli"]
235-
236232
[auth.CARTESI_AUTH_MNEMONIC]
233+
file = true
237234
go-type = "RedactedString"
238235
description = """
239236
The node will use the private key generated from this mnemonic to sign transactions."""
240237
omit = true
241238
used-by = ["claimer", "node", "cli"]
242239

243-
[auth.CARTESI_AUTH_MNEMONIC_FILE]
244-
go-type = "string"
245-
description = """
246-
The node will use the private key generated from the mnemonic contained in this file
247-
to sign transactions."""
248-
omit = true
249-
used-by = ["claimer", "node", "cli"]
250-
251240
[auth.CARTESI_AUTH_MNEMONIC_ACCOUNT_INDEX]
252241
default = "0"
253242
go-type = "RedactedUint"
@@ -281,6 +270,7 @@ used-by = ["claimer", "node", "cli"]
281270

282271
[database.CARTESI_DATABASE_CONNECTION]
283272
default = ""
273+
file = true
284274
go-type = "URL"
285275
description = """
286276
Postgres endpoint in the 'postgres://user:password@hostname:port/database' format (URL).

internal/config/generate/code.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ package config
117117
118118
import (
119119
"fmt"
120+
"os"
121+
"strings"
120122
121123
"github.com/spf13/viper"
122124
)
@@ -131,6 +133,10 @@ func init() {
131133
const (
132134
{{ range . -}}
133135
{{ toConstName .Name }} = "{{ .Name }}"
136+
{{ end }}
137+
// File variants
138+
{{ range . -}}
139+
{{ if .File -}}{{ toConstName .Name }}_FILE = "{{ .Name }}_FILE"{{ end }}
134140
{{ end -}}
135141
)
136142
@@ -209,6 +215,14 @@ func (c *NodeConfig) To{{ capitalize $service }}Config() *{{ capitalize $service
209215
// Get{{ toFieldName .Name }} returns the value for the environment variable {{ .Name }}.
210216
func Get{{ toFieldName .Name }}() ({{ .GoType }}, error) {
211217
s := viper.GetString({{ toConstName .Name }})
218+
{{ if .File -}} if s == "" {
219+
filename := viper.GetString({{toConstName .Name}}_FILE)
220+
contents, err := os.ReadFile(filename)
221+
if err != nil {
222+
return notDefined{{ .GoType }}(), fmt.Errorf("failed to parse %s: %w", {{ toConstName .Name }}_FILE, err)
223+
}
224+
s = strings.TrimSpace(string(contents))
225+
} {{- end }}
212226
if s != "" {
213227
v, err := {{ toGoFunc .GoType }}(s)
214228
if err != nil {

internal/config/generate/env.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ type Env struct {
2323
// If defined omit the field from the generated Config struct
2424
Omit bool `toml:"omit"`
2525

26+
// Configuration also has a "_FILE" variant that should be searched
27+
File bool `toml:"file"`
28+
2629
// List of services that use this environment variable.
2730
// Possible values: "advancer", "claimer", "cli", "evm-reader", "jsonrpc-api", "node", "validator"
2831
UsedBy []string `toml:"used-by"`

0 commit comments

Comments
 (0)