Skip to content

Commit c3f4581

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

11 files changed

Lines changed: 170 additions & 88 deletions

compose.individual-services.yaml

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
x-env: &env
22
CARTESI_LOG_LEVEL: info
3-
CARTESI_BLOCKCHAIN_HTTP_ENDPOINT: http://ethereum_provider:8545
4-
CARTESI_BLOCKCHAIN_WS_ENDPOINT: ws://ethereum_provider:8545
3+
CARTESI_BLOCKCHAIN_HTTP_ENDPOINT_FILE: /run/secrets/blockchain_http_endpoint
4+
CARTESI_BLOCKCHAIN_WS_ENDPOINT_FILE: /run/secrets/blockchain_http_endpoint
55
CARTESI_BLOCKCHAIN_ID: 13370
66
CARTESI_CONTRACTS_INPUT_BOX_ADDRESS: 0xc70074BDD26d8cF983Ca6A5b89b8db52D5850051
77
CARTESI_CONTRACTS_AUTHORITY_FACTORY_ADDRESS: 0xC7003566dD09Aa0fC0Ce201aC2769aFAe3BF0051
88
CARTESI_CONTRACTS_APPLICATION_FACTORY_ADDRESS: 0xc7006f70875BaDe89032001262A846D3Ee160051
99
CARTESI_CONTRACTS_SELF_HOSTED_APPLICATION_FACTORY_ADDRESS: 0xc700285Ab555eeB5201BC00CFD4b2CC8DED90051
10-
CARTESI_DATABASE_CONNECTION: postgres://postgres:password@database:5432/rollupsdb?sslmode=disable
11-
CARTESI_AUTH_MNEMONIC: "test test test test test test test test test test test junk"
10+
CARTESI_DATABASE_CONNECTION_FILE: /run/secrets/database_connection
11+
CARTESI_AUTH_MNEMONIC_FILE: /run/secrets/auth_mnemonic
1212

1313
services:
1414
ethereum_provider:
@@ -43,6 +43,8 @@ services:
4343
networks:
4444
- devnet
4545
restart: "no"
46+
secrets:
47+
- database_connection
4648
environment:
4749
<<: *env
4850

@@ -58,6 +60,11 @@ services:
5860
- devnet
5961
ports:
6062
- "10001:10001" # Supervisor
63+
secrets:
64+
- auth_mnemonic
65+
- blockchain_http_endpoint
66+
- blockchain_ws_endpoint
67+
- database_connection
6168
environment:
6269
<<: *env
6370

@@ -76,6 +83,9 @@ services:
7683
ports:
7784
- "10002:10002" # Supervisor
7885
- "10012:10012" # Inspect Service
86+
secrets:
87+
- auth_mnemonic
88+
- database_connection
7989
environment:
8090
<<: *env
8191

@@ -89,6 +99,8 @@ services:
8999
- devnet
90100
ports:
91101
- "10003:10003" # Supervisor
102+
secrets:
103+
- database_connection
92104
environment:
93105
<<: *env
94106

@@ -104,6 +116,10 @@ services:
104116
- devnet
105117
ports:
106118
- "10004:10004" # Supervisor
119+
secrets:
120+
- auth_mnemonic
121+
- blockchain_http_endpoint
122+
- database_connection
107123
environment:
108124
<<: *env
109125

@@ -118,10 +134,24 @@ services:
118134
ports:
119135
- "10005:10005" # Supervisor
120136
- "10011:10011" # Jsonrpc API service
137+
secrets:
138+
- auth_mnemonic
139+
- blockchain_http_endpoint
140+
- database_connection
121141
environment:
122142
<<: *env
123143
volumes:
124144
node_data:
125145

126146
networks:
127147
devnet:
148+
149+
secrets:
150+
auth_mnemonic:
151+
file: test/secrets/auth_mnemonic.txt
152+
blockchain_http_endpoint:
153+
file: test/secrets/blockchain_http_endpoint.txt
154+
blockchain_ws_endpoint:
155+
file: test/secrets/blockchain_ws_endpoint.txt
156+
database_connection:
157+
file: test/secrets/database_connection.txt

compose.yaml

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
x-env: &env
22
CARTESI_LOG_LEVEL: info
3-
CARTESI_BLOCKCHAIN_HTTP_ENDPOINT: http://ethereum_provider:8545
4-
CARTESI_BLOCKCHAIN_WS_ENDPOINT: ws://ethereum_provider:8545
3+
CARTESI_BLOCKCHAIN_HTTP_ENDPOINT_FILE: /run/secrets/blockchain_http_endpoint
4+
CARTESI_BLOCKCHAIN_WS_ENDPOINT_FILE: /run/secrets/blockchain_http_endpoint
55
CARTESI_BLOCKCHAIN_ID: 13370
66
CARTESI_CONTRACTS_INPUT_BOX_ADDRESS: 0xc70074BDD26d8cF983Ca6A5b89b8db52D5850051
77
CARTESI_CONTRACTS_AUTHORITY_FACTORY_ADDRESS: 0xC7003566dD09Aa0fC0Ce201aC2769aFAe3BF0051
88
CARTESI_CONTRACTS_APPLICATION_FACTORY_ADDRESS: 0xc7006f70875BaDe89032001262A846D3Ee160051
99
CARTESI_CONTRACTS_SELF_HOSTED_APPLICATION_FACTORY_ADDRESS: 0xc700285Ab555eeB5201BC00CFD4b2CC8DED90051
10-
CARTESI_DATABASE_CONNECTION: postgres://postgres:password@database:5432/rollupsdb?sslmode=disable
11-
CARTESI_AUTH_MNEMONIC: "test test test test test test test test test test test junk"
10+
CARTESI_DATABASE_CONNECTION_FILE: /run/secrets/database_connection
11+
CARTESI_AUTH_MNEMONIC_FILE: /run/secrets/auth_mnemonic
1212

1313
services:
1414
ethereum_provider:
@@ -42,6 +42,8 @@ services:
4242
condition: service_healthy
4343
networks:
4444
- devnet
45+
secrets:
46+
- database_connection
4547
restart: "no"
4648
environment:
4749
<<: *env
@@ -62,6 +64,11 @@ services:
6264
- "10000:10000" # Supervisor
6365
- "10011:10011" # Jsonrpc API service
6466
- "10012:10012" # Inspect Service
67+
secrets:
68+
- auth_mnemonic
69+
- blockchain_http_endpoint
70+
- blockchain_ws_endpoint
71+
- database_connection
6572
environment:
6673
<<: *env
6774

@@ -70,3 +77,13 @@ volumes:
7077

7178
networks:
7279
devnet:
80+
81+
secrets:
82+
auth_mnemonic:
83+
file: test/secrets/auth_mnemonic.txt
84+
blockchain_http_endpoint:
85+
file: test/secrets/blockchain_http_endpoint.txt
86+
blockchain_ws_endpoint:
87+
file: test/secrets/blockchain_ws_endpoint.txt
88+
database_connection:
89+
file: test/secrets/database_connection.txt

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: 16 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,16 @@ 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 -}}
219+
if s == "" {
220+
filename := viper.GetString({{toConstName .Name}}_FILE)
221+
contents, err := os.ReadFile(filename)
222+
if err != nil {
223+
return notDefined{{ .GoType }}(), fmt.Errorf("failed to parse %s: %w", {{ toConstName .Name }}_FILE, err)
224+
}
225+
s = strings.TrimSpace(string(contents))
226+
}
227+
{{- end }}
212228
if s != "" {
213229
v, err := {{ toGoFunc .GoType }}(s)
214230
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)