Skip to content

Commit

Permalink
cmd/combine: ensure output dir exists (#2537)
Browse files Browse the repository at this point in the history
Ensures the output folder exists before writing combined keys to it.

category: bug
ticket: #2536
  • Loading branch information
corverroos committed Aug 14, 2023
1 parent fa7f89f commit 50d2a81
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cmd/combine/combine.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ func Combine(ctx context.Context, inputDir, outputDir string, force, noverify bo
combinedKeys = append(combinedKeys, secret)
}

if err := os.MkdirAll(outputDir, 0o755); err != nil {
return errors.Wrap(err, "ensure output directory exists", z.Str("output_dir", outputDir))
}

ksPath := filepath.Join(outputDir, "keystore-0.json")
_, err = os.Stat(ksPath)
if err == nil && !force {
Expand Down
3 changes: 2 additions & 1 deletion cmd/combine/combine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"
"testing"

Expand Down Expand Up @@ -249,7 +250,7 @@ func combineTest(
}

dir := t.TempDir()
od := t.TempDir()
od := path.Join(dir, "validator_keys")

// flatten secrets, each validator slice is unpacked in a flat structure
var rawSecrets []tbls.PrivateKey
Expand Down
19 changes: 19 additions & 0 deletions eth2util/keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,17 @@ func StoreKeysInsecure(secrets []tbls.PrivateKey, dir string, _ confirmInsecure)

// StoreKeys stores the secrets in dir/keystore-%d.json EIP 2335 Keystore files
// with new random passwords stored in dir/Keystore-%d.txt.
//
// Note it doesn't ensure the folder dir exists.
func StoreKeys(secrets []tbls.PrivateKey, dir string) error {
return storeKeysInternal(secrets, dir, "keystore-%d.json")
}

func storeKeysInternal(secrets []tbls.PrivateKey, dir string, filenameFmt string, opts ...keystorev4.Option) error {
if err := checkDir(dir); err != nil {
return err
}

type data struct {
index int
secret tbls.PrivateKey
Expand Down Expand Up @@ -206,3 +212,16 @@ func randomHex32() (string, error) {

return hex.EncodeToString(b), nil
}

// checkDir checks if dir exists and is a directory.
func checkDir(dir string) error {
if info, err := os.Stat(dir); os.IsNotExist(err) {
return errors.Wrap(err, "keystore dir does not exist", z.Str("dir", dir))
} else if err != nil {
return errors.Wrap(err, "check keystore dir", z.Str("dir", dir))
} else if !info.IsDir() {
return errors.New("keystore dir is not a directory", z.Str("dir", dir))
}

return nil
}
8 changes: 8 additions & 0 deletions eth2util/keystore/keystore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,11 @@ func storeNewKeyForT(t *testing.T, target string) tbls.PrivateKey {

return secret
}

func TestCheckDir(t *testing.T) {
err := keystore.StoreKeys(nil, "foo")
require.ErrorContains(t, err, "not exist")

err = keystore.StoreKeys(nil, "testdata/keystore-scrypt.json")
require.ErrorContains(t, err, "not a directory")
}

0 comments on commit 50d2a81

Please sign in to comment.