Skip to content

Commit

Permalink
Validator key files: allow symlinks (#5679)
Browse files Browse the repository at this point in the history
* traverse symlinks
* Merge branch 'master' into account-keys-symlinks
* unit test
* Merge branch 'account-keys-symlinks' of github.com:prysmaticlabs/prysm into account-keys-symlinks
* Merge refs/heads/master into account-keys-symlinks
* removes unncecessary line
* Merge branch 'account-keys-symlinks' of github.com:prysmaticlabs/prysm into account-keys-symlinks
* fixes tests
* better teardown of resources
  • Loading branch information
farazdagi authored Apr 29, 2020
1 parent 3416962 commit 2c1e3aa
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions shared/keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -96,6 +97,15 @@ func (ks Store) GetKeys(directory, fileprefix, password string, warnOnFail bool)
n := f.Name()
filePath := filepath.Join(directory, n)
filePath = filepath.Clean(filePath)
if f.Mode()&os.ModeSymlink == os.ModeSymlink {
if targetFilePath, err := filepath.EvalSymlinks(filePath); err == nil {
filePath = targetFilePath
// Override link stats with target file's stats.
if f, err = os.Stat(filePath); err != nil {
return nil, err
}
}
}
cp := strings.Contains(n, strings.TrimPrefix(fileprefix, "/"))
if f.Mode().IsRegular() && cp {
// #nosec G304
Expand Down
39 changes: 39 additions & 0 deletions shared/keystore/keystore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,42 @@ func TestEncryptDecryptKey(t *testing.T) {
}

}

func TestGetSymlinkedKeys(t *testing.T) {
tmpdir := testutil.TempDir() + "/symlinked-keystore"
defer func() {
if err := os.RemoveAll(tmpdir); err != nil {
t.Logf("unable to remove temporary files: %v", err)
}
}()
ks := &Store{
scryptN: LightScryptN,
scryptP: LightScryptP,
}

key, err := NewKey()
if err != nil {
t.Fatalf("key generation failed %v", err)
}

if err := ks.StoreKey(tmpdir+"/files/test-1", key, "password"); err != nil {
t.Fatalf("unable to store key %v", err)
}

if err := os.Symlink(tmpdir+"/files/test-1", tmpdir+"/test-1"); err != nil {
t.Fatalf("unable to create symlink: %v", err)
}

newkeys, err := ks.GetKeys(tmpdir, "test", "password", false)
if err != nil {
t.Fatalf("unable to get key %v", err)
}
if len(newkeys) != 1 {
t.Errorf("unexpected number of keys returned, want: %d, got: %d", 1, len(newkeys))
}
for _, s := range newkeys {
if !bytes.Equal(s.SecretKey.Marshal(), key.SecretKey.Marshal()) {
t.Fatalf("retrieved secret keys are not equal %v ", s.SecretKey.Marshal())
}
}
}

0 comments on commit 2c1e3aa

Please sign in to comment.