-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
terra.go
150 lines (128 loc) · 3.17 KB
/
terra.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package keystore
import (
"fmt"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/core/services/keystore/keys/terrakey"
)
//go:generate mockery --name Terra --output ./mocks/ --case=underscore --filename terra.go
type Terra interface {
Get(id string) (terrakey.Key, error)
GetAll() ([]terrakey.Key, error)
Create() (terrakey.Key, error)
Add(key terrakey.Key) error
Delete(id string) (terrakey.Key, error)
Import(keyJSON []byte, password string) (terrakey.Key, error)
Export(id string, password string) ([]byte, error)
EnsureKey() error
}
type terra struct {
*keyManager
}
var _ Terra = &terra{}
func newTerraKeyStore(km *keyManager) *terra {
return &terra{
km,
}
}
func (ks *terra) Get(id string) (terrakey.Key, error) {
ks.lock.RLock()
defer ks.lock.RUnlock()
if ks.isLocked() {
return terrakey.Key{}, ErrLocked
}
return ks.getByID(id)
}
func (ks *terra) GetAll() (keys []terrakey.Key, _ error) {
ks.lock.RLock()
defer ks.lock.RUnlock()
if ks.isLocked() {
return nil, ErrLocked
}
for _, key := range ks.keyRing.Terra {
keys = append(keys, key)
}
return keys, nil
}
func (ks *terra) Create() (terrakey.Key, error) {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return terrakey.Key{}, ErrLocked
}
key := terrakey.New()
return key, ks.safeAddKey(key)
}
func (ks *terra) Add(key terrakey.Key) error {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return ErrLocked
}
if _, found := ks.keyRing.Terra[key.ID()]; found {
return fmt.Errorf("key with ID %s already exists", key.ID())
}
return ks.safeAddKey(key)
}
func (ks *terra) Delete(id string) (terrakey.Key, error) {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return terrakey.Key{}, ErrLocked
}
key, err := ks.getByID(id)
if err != nil {
return terrakey.Key{}, err
}
err = ks.safeRemoveKey(key)
return key, err
}
func (ks *terra) Import(keyJSON []byte, password string) (terrakey.Key, error) {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return terrakey.Key{}, ErrLocked
}
key, err := terrakey.FromEncryptedJSON(keyJSON, password)
if err != nil {
return terrakey.Key{}, errors.Wrap(err, "TerraKeyStore#ImportKey failed to decrypt key")
}
if _, found := ks.keyRing.Terra[key.ID()]; found {
return terrakey.Key{}, fmt.Errorf("key with ID %s already exists", key.ID())
}
return key, ks.keyManager.safeAddKey(key)
}
func (ks *terra) Export(id string, password string) ([]byte, error) {
ks.lock.RLock()
defer ks.lock.RUnlock()
if ks.isLocked() {
return nil, ErrLocked
}
key, err := ks.getByID(id)
if err != nil {
return nil, err
}
return key.ToEncryptedJSON(password, ks.scryptParams)
}
func (ks *terra) EnsureKey() error {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return ErrLocked
}
if len(ks.keyRing.Terra) > 0 {
return nil
}
key := terrakey.New()
ks.logger.Infof("Created Terra key with ID %s", key.ID())
return ks.safeAddKey(key)
}
var (
ErrNoTerraKey = errors.New("no terra keys exist")
)
func (ks *terra) getByID(id string) (terrakey.Key, error) {
key, found := ks.keyRing.Terra[id]
if !found {
return terrakey.Key{}, KeyNotFoundError{ID: id, KeyType: "Terra"}
}
return key, nil
}