From 9aa142784689b5642517733199c998e82f530f31 Mon Sep 17 00:00:00 2001 From: chris-j-h Date: Mon, 8 Jul 2019 10:43:24 +0100 Subject: [PATCH] Implement Close for vaultWallet --- accounts/vault/hashicorp_config.go | 1 - accounts/vault/vault_wallet.go | 11 ++++++- accounts/vault/vault_wallet_test.go | 47 +++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/accounts/vault/hashicorp_config.go b/accounts/vault/hashicorp_config.go index db4105938..900aaef70 100644 --- a/accounts/vault/hashicorp_config.go +++ b/accounts/vault/hashicorp_config.go @@ -11,7 +11,6 @@ type hashicorpClientConfig struct { CaCert string `toml:",omitempty"` ClientCert string `toml:",omitempty"` ClientKey string `toml:",omitempty"` - EnvVarPrefix string `toml:",omitempty"` UseSecretCache bool `toml:",omitempty"` } diff --git a/accounts/vault/vault_wallet.go b/accounts/vault/vault_wallet.go index be5b114db..f28d77dfb 100644 --- a/accounts/vault/vault_wallet.go +++ b/accounts/vault/vault_wallet.go @@ -22,6 +22,7 @@ type vaultWallet struct { type vaultService interface { status() (string, error) open() error + close() error } func newHashicorpWallet(config hashicorpWalletConfig, updateFeed *event.Feed) (vaultWallet, error) { @@ -50,12 +51,14 @@ func (w vaultWallet) Open(passphrase string) error { if err := w.vault.open(); err != nil { return err } + w.updateFeed.Send(accounts.WalletEvent{Wallet: w, Kind: accounts.WalletOpened}) + return nil } func (w vaultWallet) Close() error { - panic("implement me") + return w.vault.close() } func (w vaultWallet) Accounts() []accounts.Account { @@ -213,3 +216,9 @@ func (h *hashicorpService) open() error { func usingApproleAuth(roleID, secretID string) bool { return roleID != "" && secretID != "" } + +func (h *hashicorpService) close() error { + h.client = nil + + return nil +} diff --git a/accounts/vault/vault_wallet_test.go b/accounts/vault/vault_wallet_test.go index 5294978e8..656016912 100644 --- a/accounts/vault/vault_wallet_test.go +++ b/accounts/vault/vault_wallet_test.go @@ -7,6 +7,8 @@ import ( "fmt" "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/event" + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" "github.com/hashicorp/vault/api" "io/ioutil" "net/http" @@ -503,3 +505,48 @@ func TestVaultWallet_Open_Hashicorp_SendsEventToBackendSubscribers(t *testing.T) t.Fatalf("want: %v, got: %v", want, got) } } + +func TestVaultWallet_Close_Hashicorp_ReturnsStateToBeforeOpen(t *testing.T) { + if err := os.Setenv(api.EnvVaultToken, "mytoken"); err != nil { + t.Fatal(err) + } + + config := hashicorpWalletConfig{ + Client: hashicorpClientConfig{Url: "http://url:1"}, + Secrets: []hashicorpSecretData{{Name: "secret1"}}, + } + + w, err := newHashicorpWallet(config, &event.Feed{}) + + if err != nil { + t.Fatal(err) + } + + unopened, err := newHashicorpWallet(config, &event.Feed{}) + + if err != nil { + t.Fatal(err) + } + + cmpOpts := []cmp.Option{cmp.AllowUnexported(vaultWallet{}, hashicorpService{}), cmpopts.IgnoreUnexported(event.Feed{})} + + if diff := cmp.Diff(unopened, w, cmpOpts...); diff != "" { + t.Fatalf("cmp does not consider the two wallets equal\n%v", diff) + } + + if err := w.Open(""); err != nil { + t.Fatalf("error: %v", err) + } + + if diff := cmp.Diff(unopened, w, cmpOpts...); diff == "" { + t.Fatalf("cmp does not consider the wallets different after one was opened\n%v", diff) + } + + if err := w.Close(); err != nil { + t.Fatalf("error: %v", err) + } + + if diff := cmp.Diff(unopened, w, cmpOpts...); diff != "" { + t.Fatalf("cmp does not consider the two wallets equal after one was opened and closed:\n%v", diff) + } +}