Skip to content

Commit 3a0cd6e

Browse files
authored
test: add OverrideKmdConfig to libgoalFixture (#6269)
1 parent f53eaeb commit 3a0cd6e

File tree

5 files changed

+75
-7
lines changed

5 files changed

+75
-7
lines changed

daemon/kmd/config/config.go

+16
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ type ScryptParams struct {
6969
ScryptP int `json:"scrypt_p"`
7070
}
7171

72+
// DefaultConfig returns the default KMDConfig
73+
func DefaultConfig(dataDir string) KMDConfig {
74+
return defaultConfig(dataDir)
75+
}
76+
7277
// defaultConfig returns the default KMDConfig
7378
func defaultConfig(dataDir string) KMDConfig {
7479
return KMDConfig{
@@ -121,3 +126,14 @@ func LoadKMDConfig(dataDir string) (cfg KMDConfig, err error) {
121126
err = cfg.Validate()
122127
return
123128
}
129+
130+
// SaveKMDConfig writes the kmd configuration to disk
131+
func SaveKMDConfig(dataDir string, cfg KMDConfig) error {
132+
err := cfg.Validate()
133+
if err != nil {
134+
return err
135+
}
136+
configFilename := filepath.Join(dataDir, kmdConfigFilename)
137+
138+
return codecs.SaveObjectToFile(configFilename, cfg, true)
139+
}

netdeploy/network.go

+7
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ func OverrideConsensusVersion(ver protocol.ConsensusVersion) TemplateOverride {
7878
}
7979
}
8080

81+
// OverrideKmdConfig changes the KMD config.
82+
func OverrideKmdConfig(kmdConfig TemplateKMDConfig) TemplateOverride {
83+
return func(template *NetworkTemplate) {
84+
template.kmdConfig = kmdConfig
85+
}
86+
}
87+
8188
// CreateNetworkFromTemplate uses the specified template to deploy a new private network
8289
// under the specified root directory.
8390
func CreateNetworkFromTemplate(name, rootDir string, templateReader io.Reader, binDir string, importKeys bool, nodeExitCallback nodecontrol.AlgodExitErrorCallback, consensus config.ConsensusProtocols, overrides ...TemplateOverride) (Network, error) {

netdeploy/networkTemplate.go

+42-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929

3030
"github.com/algorand/go-algorand/config"
3131
"github.com/algorand/go-algorand/crypto"
32+
kmdconfig "github.com/algorand/go-algorand/daemon/kmd/config"
3233
"github.com/algorand/go-algorand/data/bookkeeping"
3334
"github.com/algorand/go-algorand/gen"
3435
"github.com/algorand/go-algorand/libgoal"
@@ -42,6 +43,20 @@ type NetworkTemplate struct {
4243
Genesis gen.GenesisData
4344
Nodes []remote.NodeConfigGoal
4445
Consensus config.ConsensusProtocols
46+
kmdConfig TemplateKMDConfig // set by OverrideKmdConfig
47+
}
48+
49+
// TemplateKMDConfig is a subset of the kmd configuration that can be overridden in the network template
50+
// by using OverrideKmdConfig TemplateOverride opts.
51+
// The reason why config.KMDConfig cannot be used directly is that it contains DataDir field which is
52+
// is not known until the template instantiation.
53+
type TemplateKMDConfig struct {
54+
SessionLifetimeSecs uint64
55+
}
56+
57+
func (c TemplateKMDConfig) apply(cfg kmdconfig.KMDConfig) kmdconfig.KMDConfig {
58+
cfg.SessionLifetimeSecs = c.SessionLifetimeSecs
59+
return cfg
4560
}
4661

4762
var defaultNetworkTemplate = NetworkTemplate{
@@ -131,10 +146,27 @@ func (t NetworkTemplate) createNodeDirectories(targetFolder string, binDir strin
131146
}
132147
}
133148

149+
var kmdDir string
150+
if (t.kmdConfig != TemplateKMDConfig{}) {
151+
kmdDir = filepath.Join(nodeDir, libgoal.DefaultKMDDataDir)
152+
err = os.MkdirAll(kmdDir, 0700) // kmd requires 700 permissions
153+
if err != nil {
154+
return
155+
}
156+
err = createKMDConfigFile(t.kmdConfig, kmdDir)
157+
if err != nil {
158+
return
159+
}
160+
}
161+
134162
if importKeys && hasWallet {
135163
var client libgoal.Client
136-
client, err = libgoal.MakeClientWithBinDir(binDir, nodeDir, "", libgoal.KmdClient)
137-
if err != nil {
164+
if client, err = libgoal.MakeClientFromConfig(libgoal.ClientConfig{
165+
AlgodDataDir: nodeDir,
166+
KMDDataDir: kmdDir,
167+
CacheDir: "",
168+
BinDir: binDir,
169+
}, libgoal.KmdClient); err != nil {
138170
return
139171
}
140172
_, err = client.CreateWallet(libgoal.UnencryptedWalletName, nil, crypto.MasterDerivationKey{})
@@ -241,12 +273,12 @@ func (t NetworkTemplate) Validate() error {
241273
return fmt.Errorf("invalid template: at least one relay is required when more than a single node presents")
242274
}
243275

244-
// Validate JSONOverride decoding
276+
// Validate ConfigJSONOverride decoding
245277
for _, cfg := range t.Nodes {
246278
local := config.GetDefaultLocal()
247279
err := decodeJSONOverride(cfg.ConfigJSONOverride, &local)
248280
if err != nil {
249-
return fmt.Errorf("invalid template: unable to decode JSONOverride: %w", err)
281+
return fmt.Errorf("invalid template: unable to decode ConfigJSONOverride: %w", err)
250282
}
251283
}
252284

@@ -293,7 +325,7 @@ func countRelayNodes(nodeCfgs []remote.NodeConfigGoal) (relayCount int) {
293325
return
294326
}
295327

296-
func decodeJSONOverride(override string, cfg *config.Local) error {
328+
func decodeJSONOverride[T any](override string, cfg *T) error {
297329
if override != "" {
298330
reader := strings.NewReader(override)
299331
dec := json.NewDecoder(reader)
@@ -340,3 +372,8 @@ func createConfigFile(node remote.NodeConfigGoal, configFile string, numNodes in
340372

341373
return cfg, cfg.SaveToFile(configFile)
342374
}
375+
376+
func createKMDConfigFile(kmdConfig TemplateKMDConfig, kmdDir string) error {
377+
cfg := kmdConfig.apply(kmdconfig.DefaultConfig(kmdDir))
378+
return kmdconfig.SaveKMDConfig(kmdDir, cfg)
379+
}

netdeploy/networkTemplates_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func TestDevModeValidate(t *testing.T) {
235235
},
236236
},
237237
}
238-
require.ErrorContains(t, tmpl.Validate(), "unable to decode JSONOverride")
238+
require.ErrorContains(t, tmpl.Validate(), "unable to decode ConfigJSONOverride")
239239
})
240240

241241
t.Run("ConfigJSONOverride unknown key", func(t *testing.T) {

test/framework/fixtures/libgoalFixture.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,15 @@ func (f *LibGoalFixture) setup(test TestingTB, testName string, templateFile str
127127
importKeys := false // Don't automatically import root keys when creating folders, we'll import on-demand
128128
file, err := os.Open(templateFile)
129129
f.failOnError(err, "Template file could not be opened: %v")
130-
network, err := netdeploy.CreateNetworkFromTemplate("test", f.rootDir, file, f.binDir, importKeys, f.nodeExitWithError, f.consensus, overrides...)
130+
defer file.Close()
131+
132+
// Override the kmd session lifetime to 5 minutes to prevent kmd wallet handles from expiring
133+
kmdConfOverride := netdeploy.OverrideKmdConfig(netdeploy.TemplateKMDConfig{SessionLifetimeSecs: 300})
134+
// copy overrides to prevent caller's data from being modified
135+
extraOverrides := append([]netdeploy.TemplateOverride(nil), overrides...)
136+
extraOverrides = append(extraOverrides, kmdConfOverride)
137+
138+
network, err := netdeploy.CreateNetworkFromTemplate("test", f.rootDir, file, f.binDir, importKeys, f.nodeExitWithError, f.consensus, extraOverrides...)
131139
f.failOnError(err, "CreateNetworkFromTemplate failed: %v")
132140
f.network = network
133141

0 commit comments

Comments
 (0)