Validator key files: allow symlinks (#5679)

* 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
This commit is contained in:
Victor Farazdagi 2020-04-29 21:31:22 +03:00 committed by GitHub
parent 3416962fc2
commit 2c1e3aa4ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 0 deletions

View File

@ -28,6 +28,7 @@ import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -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

View File

@ -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())
}
}
}