diff options
author | Péter Szilágyi <peterke@gmail.com> | 2019-07-02 19:01:47 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-02 19:01:47 +0800 |
commit | a0943b8932f2fcd28dc103689f904a3c75ea07a4 (patch) | |
tree | c94898e32a2a755d962ab78eb2cddbc09f0fc02b /signer/storage | |
parent | 6bf5555c4f79b8161b4cbedc19da9d29ca6e2305 (diff) | |
download | go-tangerine-a0943b8932f2fcd28dc103689f904a3c75ea07a4.tar go-tangerine-a0943b8932f2fcd28dc103689f904a3c75ea07a4.tar.gz go-tangerine-a0943b8932f2fcd28dc103689f904a3c75ea07a4.tar.bz2 go-tangerine-a0943b8932f2fcd28dc103689f904a3c75ea07a4.tar.lz go-tangerine-a0943b8932f2fcd28dc103689f904a3c75ea07a4.tar.xz go-tangerine-a0943b8932f2fcd28dc103689f904a3c75ea07a4.tar.zst go-tangerine-a0943b8932f2fcd28dc103689f904a3c75ea07a4.zip |
cmd/clef, signer: refresh tutorial, fix noticed issues (#19774)
* cmd/clef, signer: refresh tutorial, fix noticed issues
* cmd/clef, signer: support removing stored keys (delpw + rules)
* cmd/clef: polishes + Geth integration in the tutorial
Diffstat (limited to 'signer/storage')
-rw-r--r-- | signer/storage/aes_gcm_storage.go | 30 | ||||
-rw-r--r-- | signer/storage/aes_gcm_storage_test.go | 8 | ||||
-rw-r--r-- | signer/storage/storage.go | 46 |
3 files changed, 60 insertions, 24 deletions
diff --git a/signer/storage/aes_gcm_storage.go b/signer/storage/aes_gcm_storage.go index 900831867..e6a8f145c 100644 --- a/signer/storage/aes_gcm_storage.go +++ b/signer/storage/aes_gcm_storage.go @@ -53,7 +53,7 @@ func NewAESEncryptedStorage(filename string, key []byte) *AESEncryptedStorage { } } -// Put stores a value by key. 0-length keys results in no-op +// Put stores a value by key. 0-length keys results in noop. func (s *AESEncryptedStorage) Put(key, value string) { if len(key) == 0 { return @@ -75,27 +75,41 @@ func (s *AESEncryptedStorage) Put(key, value string) { } } -// Get returns the previously stored value, or the empty string if it does not exist or key is of 0-length -func (s *AESEncryptedStorage) Get(key string) string { +// Get returns the previously stored value, or an error if it does not exist or +// key is of 0-length. +func (s *AESEncryptedStorage) Get(key string) (string, error) { if len(key) == 0 { - return "" + return "", ErrZeroKey } data, err := s.readEncryptedStorage() if err != nil { log.Warn("Failed to read encrypted storage", "err", err, "file", s.filename) - return "" + return "", err } encrypted, exist := data[key] if !exist { log.Warn("Key does not exist", "key", key) - return "" + return "", ErrNotFound } entry, err := decrypt(s.key, encrypted.Iv, encrypted.CipherText, []byte(key)) if err != nil { log.Warn("Failed to decrypt key", "key", key) - return "" + return "", err + } + return string(entry), nil +} + +// Del removes a key-value pair. If the key doesn't exist, the method is a noop. +func (s *AESEncryptedStorage) Del(key string) { + data, err := s.readEncryptedStorage() + if err != nil { + log.Warn("Failed to read encrypted storage", "err", err, "file", s.filename) + return + } + delete(data, key) + if err = s.writeEncryptedStorage(data); err != nil { + log.Warn("Failed to write entry", "err", err) } - return string(entry) } // readEncryptedStorage reads the file with encrypted creds diff --git a/signer/storage/aes_gcm_storage_test.go b/signer/storage/aes_gcm_storage_test.go index a421a8449..0aaaf58d2 100644 --- a/signer/storage/aes_gcm_storage_test.go +++ b/signer/storage/aes_gcm_storage_test.go @@ -110,8 +110,8 @@ func TestEnd2End(t *testing.T) { } s1.Put("bazonk", "foobar") - if v := s2.Get("bazonk"); v != "foobar" { - t.Errorf("Expected bazonk->foobar, got '%v'", v) + if v, err := s2.Get("bazonk"); v != "foobar" || err != nil { + t.Errorf("Expected bazonk->foobar (nil error), got '%v' (%v error)", v, err) } } @@ -154,11 +154,11 @@ func TestSwappedKeys(t *testing.T) { } } swap() - if v := s1.Get("k1"); v != "" { + if v, _ := s1.Get("k1"); v != "" { t.Errorf("swapped value should return empty") } swap() - if v := s1.Get("k1"); v != "v1" { + if v, _ := s1.Get("k1"); v != "v1" { t.Errorf("double-swapped value should work fine") } } diff --git a/signer/storage/storage.go b/signer/storage/storage.go index 50c55e455..c1f593d96 100644 --- a/signer/storage/storage.go +++ b/signer/storage/storage.go @@ -17,11 +17,26 @@ package storage +import "errors" + +var ( + // ErrZeroKey is returned if an attempt was made to inset a 0-length key. + ErrZeroKey = errors.New("0-length key") + + // ErrNotFound is returned if an unknown key is attempted to be retrieved. + ErrNotFound = errors.New("not found") +) + type Storage interface { - // Put stores a value by key. 0-length keys results in no-op + // Put stores a value by key. 0-length keys results in noop. Put(key, value string) - // Get returns the previously stored value, or the empty string if it does not exist or key is of 0-length - Get(key string) string + + // Get returns the previously stored value, or an error if the key is 0-length + // or unknown. + Get(key string) (string, error) + + // Del removes a key-value pair. If the key doesn't exist, the method is a noop. + Del(key string) } // EphemeralStorage is an in-memory storage that does @@ -31,23 +46,29 @@ type EphemeralStorage struct { namespace string } +// Put stores a value by key. 0-length keys results in noop. func (s *EphemeralStorage) Put(key, value string) { if len(key) == 0 { return } - //fmt.Printf("storage: put %v -> %v\n", key, value) s.data[key] = value } -func (s *EphemeralStorage) Get(key string) string { +// Get returns the previously stored value, or an error if the key is 0-length +// or unknown. +func (s *EphemeralStorage) Get(key string) (string, error) { if len(key) == 0 { - return "" + return "", ErrZeroKey } - //fmt.Printf("storage: get %v\n", key) - if v, exist := s.data[key]; exist { - return v + if v, ok := s.data[key]; ok { + return v, nil } - return "" + return "", ErrNotFound +} + +// Del removes a key-value pair. If the key doesn't exist, the method is a noop. +func (s *EphemeralStorage) Del(key string) { + delete(s.data, key) } func NewEphemeralStorage() Storage { @@ -61,6 +82,7 @@ func NewEphemeralStorage() Storage { type NoStorage struct{} func (s *NoStorage) Put(key, value string) {} -func (s *NoStorage) Get(key string) string { - return "" +func (s *NoStorage) Del(key string) {} +func (s *NoStorage) Get(key string) (string, error) { + return "", errors.New("I forgot") } |