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

cluster: unique validator_keys dir #2958

Merged
merged 2 commits into from
Mar 14, 2024
Merged
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
25 changes: 25 additions & 0 deletions cluster/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"io"
"math"
"net/http"
"os"
"path"
"strings"
"time"

Expand Down Expand Up @@ -57,6 +59,29 @@
return res, nil
}

// CreateValidatorKeysDir creates a new directory for validator keys.
// If the directory "validator_keys" exists, it checks if the directory
// is empty.
func CreateValidatorKeysDir(parentDir string) (string, error) {
vkdir := path.Join(parentDir, "validator_keys")
err := os.Mkdir(vkdir, os.ModePerm)
if err == nil {
return vkdir, nil
}
if !os.IsExist(err) {
return "", errors.Wrap(err, "mkdir", z.Str("path", vkdir))
}
files, err := os.ReadDir(vkdir)
if err != nil {
return "", errors.Wrap(err, "readdir", z.Str("path", vkdir))
}

Check warning on line 77 in cluster/helpers.go

View check run for this annotation

Codecov / codecov/patch

cluster/helpers.go#L76-L77

Added lines #L76 - L77 were not covered by tests
if len(files) == 0 {
return vkdir, nil
}

return "", errors.New("directory not empty", z.Str("path", vkdir))
}

// uuid returns a random uuid.
func uuid(random io.Reader) string {
b := make([]byte, 16)
Expand Down
30 changes: 30 additions & 0 deletions cluster/helpers_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"math/rand"
"net/http"
"net/http/httptest"
"os"
"path"
"strings"
"testing"

Expand Down Expand Up @@ -130,3 +132,31 @@ func TestFetchDefinition(t *testing.T) {
})
}
}

func TestCreateValidatorKeysDir(t *testing.T) {
tmp := t.TempDir()

// First attempt must succeed.
dir, err := CreateValidatorKeysDir(tmp)
require.NoError(t, err)
require.True(t, strings.HasPrefix(dir, tmp))
require.True(t, strings.HasSuffix(dir, "validator_keys"))

// Second attempt shall succeed as long as the dir is empty.
dir, err = CreateValidatorKeysDir(tmp)
require.NoError(t, err)
require.True(t, strings.HasPrefix(dir, tmp))
require.True(t, strings.HasSuffix(dir, "validator_keys"))

// Create a file in the directory to make it non-empty.
err = os.WriteFile(path.Join(dir, "file"), []byte("data"), 0o644)
require.NoError(t, err)
_, err = CreateValidatorKeysDir(tmp)
require.ErrorContains(t, err, "directory not empty")

t.Run("mkdir error", func(t *testing.T) {
// Parent directory does not exist
_, err := CreateValidatorKeysDir(path.Join(tmp, "nonexistent"))
require.ErrorContains(t, err, "mkdir")
})
}
6 changes: 3 additions & 3 deletions cmd/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,9 +758,9 @@
secrets = append(secrets, shares[i])
}

keysDir := path.Join(nodeDir(clusterDir, i), "/validator_keys")
if err := os.MkdirAll(keysDir, 0o755); err != nil {
return errors.Wrap(err, "mkdir validator_keys")
keysDir, err := cluster.CreateValidatorKeysDir(nodeDir(clusterDir, i))
if err != nil {
return err

Check warning on line 763 in cmd/createcluster.go

View check run for this annotation

Codecov / codecov/patch

cmd/createcluster.go#L763

Added line #L763 was not covered by tests
}

if insecureKeys {
Expand Down
7 changes: 3 additions & 4 deletions dkg/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,9 @@
secrets = append(secrets, s.SecretShare)
}

keysDir := path.Join(conf.DataDir, "/validator_keys")

if err := os.Mkdir(keysDir, os.ModePerm); err != nil {
return errors.Wrap(err, "mkdir /validator_keys")
keysDir, err := cluster.CreateValidatorKeysDir(conf.DataDir)
if err != nil {
return err

Check warning on line 135 in dkg/disk.go

View check run for this annotation

Codecov / codecov/patch

dkg/disk.go#L135

Added line #L135 was not covered by tests
}

storeKeysFunc := keystore.StoreKeys
Expand Down
Loading