mirror of
https://github.com/wealdtech/ethdo.git
synced 2026-01-09 14:07:56 -05:00
Provide support for additional S3 store options.
This commit is contained in:
11
util/misc.go
11
util/misc.go
@@ -46,14 +46,19 @@ func SetupStore() error {
|
||||
if GetBaseDir() != "" {
|
||||
return errors.New("basedir does not apply to the s3 store")
|
||||
}
|
||||
store, err = s3.New(s3.WithPassphrase([]byte(GetStorePassphrase())))
|
||||
store, err = s3.New(s3.WithPassphrase([]byte(GetStorePassphrase("s3"))),
|
||||
s3.WithEndpoint(viper.GetString("stores.s3.endpoint")),
|
||||
s3.WithRegion(viper.GetString("stores.s3.region")),
|
||||
s3.WithBucket(viper.GetString("stores.s3.bucket")),
|
||||
s3.WithPath(viper.GetString("stores.s3.path")),
|
||||
)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to access Amazon S3 wallet store")
|
||||
}
|
||||
case "filesystem":
|
||||
opts := make([]filesystem.Option, 0)
|
||||
if GetStorePassphrase() != "" {
|
||||
opts = append(opts, filesystem.WithPassphrase([]byte(GetStorePassphrase())))
|
||||
if GetStorePassphrase("filesystem") != "" {
|
||||
opts = append(opts, filesystem.WithPassphrase([]byte(GetStorePassphrase("filesystem"))))
|
||||
}
|
||||
if GetBaseDir() != "" {
|
||||
opts = append(opts, filesystem.WithLocation(GetBaseDir()))
|
||||
|
||||
@@ -14,13 +14,20 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// GetStorePassphrase fetches the store passphrase supplied by the user.
|
||||
func GetStorePassphrase() string {
|
||||
storePassphrase := viper.GetString("store-passphrase")
|
||||
func GetStorePassphrase(store string) string {
|
||||
// Try store-specific passphrase.
|
||||
storePassphrase := viper.GetString(fmt.Sprintf("stores.%s.passphrase", store))
|
||||
if storePassphrase == "" {
|
||||
// Try generic passphrase.
|
||||
storePassphrase = viper.GetString("store-passphrase")
|
||||
}
|
||||
if storePassphrase == "" {
|
||||
// Try deprecated name.
|
||||
storePassphrase = viper.GetString("storepassphrase")
|
||||
|
||||
@@ -24,10 +24,16 @@ import (
|
||||
func TestGetStorePassphrase(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
env map[string]string
|
||||
store string
|
||||
passphrase string
|
||||
}{
|
||||
{
|
||||
name: "Good",
|
||||
name: "Default",
|
||||
env: map[string]string{
|
||||
"store-passphrase": "pass",
|
||||
},
|
||||
store: "test",
|
||||
passphrase: "pass",
|
||||
},
|
||||
}
|
||||
@@ -35,8 +41,10 @@ func TestGetStorePassphrase(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
viper.Reset()
|
||||
viper.Set("store-passphrase", test.passphrase)
|
||||
require.Equal(t, test.passphrase, util.GetStorePassphrase())
|
||||
for k, v := range test.env {
|
||||
viper.Set(k, v)
|
||||
}
|
||||
require.Equal(t, test.passphrase, util.GetStorePassphrase(test.passphrase))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -165,6 +173,7 @@ func TestStorePassphrase(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
inputs map[string]interface{}
|
||||
store string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
@@ -192,6 +201,16 @@ func TestStorePassphrase(t *testing.T) {
|
||||
},
|
||||
expected: "secret2",
|
||||
},
|
||||
{
|
||||
name: "StoreSpecific",
|
||||
inputs: map[string]interface{}{
|
||||
"storepassphrase": "secret",
|
||||
"store-passphrase": "secret2",
|
||||
"stores.test.passphrase": "secret3",
|
||||
},
|
||||
store: "test",
|
||||
expected: "secret3",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
@@ -200,7 +219,7 @@ func TestStorePassphrase(t *testing.T) {
|
||||
for k, v := range test.inputs {
|
||||
viper.Set(k, v)
|
||||
}
|
||||
res := util.GetStorePassphrase()
|
||||
res := util.GetStorePassphrase(test.store)
|
||||
require.Equal(t, test.expected, res)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user