Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

netdeploy: add KmdJSONOverride #6269

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions daemon/kmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
ScryptP int `json:"scrypt_p"`
}

// DefaultConfig returns the default KMDConfig
func DefaultConfig(dataDir string) KMDConfig {
return defaultConfig(dataDir)

Check warning on line 74 in daemon/kmd/config/config.go

View check run for this annotation

Codecov / codecov/patch

daemon/kmd/config/config.go#L73-L74

Added lines #L73 - L74 were not covered by tests
}

// defaultConfig returns the default KMDConfig
func defaultConfig(dataDir string) KMDConfig {
return KMDConfig{
Expand Down Expand Up @@ -121,3 +126,14 @@
err = cfg.Validate()
return
}

// SaveKMDConfig writes the kmd configuration to disk
func SaveKMDConfig(dataDir string, cfg KMDConfig) error {
err := cfg.Validate()
if err != nil {
return err

Check warning on line 134 in daemon/kmd/config/config.go

View check run for this annotation

Codecov / codecov/patch

daemon/kmd/config/config.go#L131-L134

Added lines #L131 - L134 were not covered by tests
}
configFilename := filepath.Join(dataDir, kmdConfigFilename)

Check warning on line 136 in daemon/kmd/config/config.go

View check run for this annotation

Codecov / codecov/patch

daemon/kmd/config/config.go#L136

Added line #L136 was not covered by tests

return codecs.SaveObjectToFile(configFilename, cfg, true)

Check warning on line 138 in daemon/kmd/config/config.go

View check run for this annotation

Codecov / codecov/patch

daemon/kmd/config/config.go#L138

Added line #L138 was not covered by tests
}
47 changes: 42 additions & 5 deletions netdeploy/networkTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/crypto"
kmdconfig "github.com/algorand/go-algorand/daemon/kmd/config"
"github.com/algorand/go-algorand/data/bookkeeping"
"github.com/algorand/go-algorand/gen"
"github.com/algorand/go-algorand/libgoal"
Expand Down Expand Up @@ -131,10 +132,27 @@
}
}

var kmdDir string
if len(cfg.KmdJSONOverride) > 0 {
kmdDir = filepath.Join(nodeDir, libgoal.DefaultKMDDataDir)
err = os.MkdirAll(kmdDir, 0700) // kmd requires 700 permissions
if err != nil {
return

Check warning on line 140 in netdeploy/networkTemplate.go

View check run for this annotation

Codecov / codecov/patch

netdeploy/networkTemplate.go#L135-L140

Added lines #L135 - L140 were not covered by tests
}
err = createKMDConfigFile(cfg, kmdDir)
if err != nil {
return

Check warning on line 144 in netdeploy/networkTemplate.go

View check run for this annotation

Codecov / codecov/patch

netdeploy/networkTemplate.go#L142-L144

Added lines #L142 - L144 were not covered by tests
}
}

if importKeys && hasWallet {
var client libgoal.Client
client, err = libgoal.MakeClientWithBinDir(binDir, nodeDir, "", libgoal.KmdClient)
if err != nil {
if client, err = libgoal.MakeClientFromConfig(libgoal.ClientConfig{
AlgodDataDir: nodeDir,
KMDDataDir: kmdDir,
CacheDir: "",
BinDir: binDir,
}, libgoal.KmdClient); err != nil {

Check warning on line 155 in netdeploy/networkTemplate.go

View check run for this annotation

Codecov / codecov/patch

netdeploy/networkTemplate.go#L150-L155

Added lines #L150 - L155 were not covered by tests
return
}
_, err = client.CreateWallet(libgoal.UnencryptedWalletName, nil, crypto.MasterDerivationKey{})
Expand Down Expand Up @@ -241,12 +259,21 @@
return fmt.Errorf("invalid template: at least one relay is required when more than a single node presents")
}

// Validate JSONOverride decoding
// Validate ConfigJSONOverride decoding
for _, cfg := range t.Nodes {
local := config.GetDefaultLocal()
err := decodeJSONOverride(cfg.ConfigJSONOverride, &local)
if err != nil {
return fmt.Errorf("invalid template: unable to decode JSONOverride: %w", err)
return fmt.Errorf("invalid template: unable to decode ConfigJSONOverride: %w", err)
}
}

// Validate KmdJSONOverride decoding
for _, cfg := range t.Nodes {
kmdconf := kmdconfig.KMDConfig{}
err := decodeJSONOverride(cfg.KmdJSONOverride, &kmdconf)
if err != nil {
return fmt.Errorf("invalid template: unable to decode KmdJSONOverride: %w", err)
}
}

Expand Down Expand Up @@ -293,7 +320,7 @@
return
}

func decodeJSONOverride(override string, cfg *config.Local) error {
func decodeJSONOverride[T any](override string, cfg *T) error {
if override != "" {
reader := strings.NewReader(override)
dec := json.NewDecoder(reader)
Expand Down Expand Up @@ -340,3 +367,13 @@

return cfg, cfg.SaveToFile(configFile)
}

func createKMDConfigFile(node remote.NodeConfigGoal, kmdDir string) error {
cfg := kmdconfig.DefaultConfig(kmdDir)
err := decodeJSONOverride(node.KmdJSONOverride, &cfg)
if err != nil {
return err

Check warning on line 375 in netdeploy/networkTemplate.go

View check run for this annotation

Codecov / codecov/patch

netdeploy/networkTemplate.go#L371-L375

Added lines #L371 - L375 were not covered by tests
}

return kmdconfig.SaveKMDConfig(kmdDir, cfg)

Check warning on line 378 in netdeploy/networkTemplate.go

View check run for this annotation

Codecov / codecov/patch

netdeploy/networkTemplate.go#L378

Added line #L378 was not covered by tests
}
30 changes: 29 additions & 1 deletion netdeploy/networkTemplates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,21 @@ func TestDevModeValidate(t *testing.T) {
},
},
}
require.ErrorContains(t, tmpl.Validate(), "unable to decode JSONOverride")
require.ErrorContains(t, tmpl.Validate(), "unable to decode ConfigJSONOverride")
})

t.Run("KmdJSONOverride does not parse", func(t *testing.T) {
t.Parallel()
tmpl := NetworkTemplate{
Genesis: devmodeGenesis,
Nodes: []remote.NodeConfigGoal{
{
IsRelay: false,
KmdJSONOverride: "DOES NOT PARSE",
},
},
}
require.ErrorContains(t, tmpl.Validate(), "unable to decode KmdJSONOverride")
})

t.Run("ConfigJSONOverride unknown key", func(t *testing.T) {
Expand All @@ -252,6 +266,20 @@ func TestDevModeValidate(t *testing.T) {
require.ErrorContains(t, tmpl.Validate(), "json: unknown field \"Unknown Key\"")
})

t.Run("KmdJSONOverride unknown key", func(t *testing.T) {
t.Parallel()
tmpl := NetworkTemplate{
Genesis: devmodeGenesis,
Nodes: []remote.NodeConfigGoal{
{
IsRelay: false,
KmdJSONOverride: "{\"Unknown Key\": \"Valid JSON\"}",
},
},
}
require.ErrorContains(t, tmpl.Validate(), "json: unknown field \"Unknown Key\"")
})

t.Run("Valid multi-node DevMode", func(t *testing.T) {
t.Parallel()
tmpl := NetworkTemplate{
Expand Down
1 change: 1 addition & 0 deletions netdeploy/remote/nodeConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ type NodeConfigGoal struct {
P2PPeerID string `json:",omitempty"`
DeadlockDetection int `json:"-"`
ConfigJSONOverride string `json:",omitempty"` // Raw json to merge into config.json after other modifications are complete
KmdJSONOverride string `json:",omitempty"` // Raw json to merge into default kmd-config.json
PeerList string `json:",omitempty"` // Semicolon separated list of peers to connect to. Only applicable for non-relays
}
6 changes: 4 additions & 2 deletions test/testdata/nettemplates/TwoNodes50Each.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
"Wallets": [
{ "Name": "Wallet1",
"ParticipationOnly": false }
]
],
"KmdJSONOverride": "{\"session_lifetime_secs\": 300}"
},
{
"Name": "Node",
"Wallets": [
{ "Name": "Wallet2",
"ParticipationOnly": false }
]
],
"KmdJSONOverride": "{\"session_lifetime_secs\": 300}"
}
]
}
6 changes: 4 additions & 2 deletions test/testdata/nettemplates/TwoNodes50EachFuture.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@
"Wallets": [
{ "Name": "Wallet1",
"ParticipationOnly": false }
]
],
"KmdJSONOverride": "{\"session_lifetime_secs\": 300}"
},
{
"Name": "Node",
"Wallets": [
{ "Name": "Wallet2",
"ParticipationOnly": false }
]
],
"KmdJSONOverride": "{\"session_lifetime_secs\": 300}"
}
]
}