Skip to content

Commit 72b3bd6

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

11 files changed

Lines changed: 166 additions & 84 deletions

compose.individual-services.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

@@ -71,11 +78,15 @@ services:
7178
condition: service_healthy
7279
volumes:
7380
- node_data:/var/lib/cartesi-rollups-node/data
81+
- $PWD/applications:/var/lib/cartesi-rollups-node/snapshots
7482
networks:
7583
- devnet
7684
ports:
7785
- "10002:10002" # Supervisor
7886
- "10012:10012" # Inspect Service
87+
secrets:
88+
- auth_mnemonic
89+
- database_connection
7990
environment:
8091
<<: *env
8192

@@ -89,6 +100,8 @@ services:
89100
- devnet
90101
ports:
91102
- "10003:10003" # Supervisor
103+
secrets:
104+
- database_connection
92105
environment:
93106
<<: *env
94107

@@ -104,6 +117,10 @@ services:
104117
- devnet
105118
ports:
106119
- "10004:10004" # Supervisor
120+
secrets:
121+
- auth_mnemonic
122+
- blockchain_http_endpoint
123+
- database_connection
107124
environment:
108125
<<: *env
109126

@@ -118,10 +135,24 @@ services:
118135
ports:
119136
- "10005:10005" # Supervisor
120137
- "10011:10011" # Jsonrpc API service
138+
secrets:
139+
- auth_mnemonic
140+
- blockchain_http_endpoint
141+
- database_connection
121142
environment:
122143
<<: *env
123144
volumes:
124145
node_data:
125146

126147
networks:
127148
devnet:
149+
150+
secrets:
151+
auth_mnemonic:
152+
file: test/secrets/auth_mnemonic.txt
153+
blockchain_http_endpoint:
154+
file: test/secrets/blockchain_http_endpoint.txt
155+
blockchain_ws_endpoint:
156+
file: test/secrets/blockchain_ws_endpoint.txt
157+
database_connection:
158+
file: test/secrets/database_connection.txt

compose.yaml

Lines changed: 22 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
@@ -56,12 +58,18 @@ services:
5658
condition: service_healthy
5759
volumes:
5860
- node_data:/var/lib/cartesi-rollups-node/data
61+
- /home/m/dev/cartesi/rollups-node/applications:/var/lib/cartesi-rollups-node/snapshots
5962
networks:
6063
- devnet
6164
ports:
6265
- "10000:10000" # Supervisor
6366
- "10011:10011" # Jsonrpc API service
6467
- "10012:10012" # Inspect Service
68+
secrets:
69+
- auth_mnemonic
70+
- blockchain_http_endpoint
71+
- blockchain_ws_endpoint
72+
- database_connection
6573
environment:
6674
<<: *env
6775

@@ -70,3 +78,13 @@ volumes:
7078

7179
networks:
7280
devnet:
81+
82+
secrets:
83+
auth_mnemonic:
84+
file: test/secrets/auth_mnemonic.txt
85+
blockchain_http_endpoint:
86+
file: test/secrets/blockchain_http_endpoint.txt
87+
blockchain_ws_endpoint:
88+
file: test/secrets/blockchain_ws_endpoint.txt
89+
database_connection:
90+
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: 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)