-
Notifications
You must be signed in to change notification settings - Fork 23
/
signer.go
215 lines (188 loc) · 6.99 KB
/
signer.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package config
import (
kms "cloud.google.com/go/kms/apiv1"
"context"
"errors"
"fmt"
"github.com/davecgh/go-spew/spew"
"github.com/jftuga/ellipsis"
"github.com/synapsecns/sanguine/core"
"github.com/synapsecns/sanguine/ethergo/signer/signer"
"github.com/synapsecns/sanguine/ethergo/signer/signer/awssigner"
"github.com/synapsecns/sanguine/ethergo/signer/signer/gcpsigner"
"github.com/synapsecns/sanguine/ethergo/signer/signer/localsigner"
"github.com/synapsecns/sanguine/ethergo/signer/wallet"
"google.golang.org/api/option"
"gopkg.in/yaml.v2"
"os"
"path/filepath"
"strings"
)
// SignerConfig contains a signer config. Currently this config
// only supports local based signers due to a lack of isomorphic types
// when we parse yaml.
type SignerConfig struct {
// Type is the driver used for the signer
Type string
// File is the file used for the key.
File string
}
// IsValid determines if the config is valid.
func (s SignerConfig) IsValid(_ context.Context) (ok bool, err error) {
if !strings.EqualFold(s.Type, FileType.String()) {
return false, fmt.Errorf("%w: %s. must be one of: %s", ErrUnsupportedSignerType, s.Type, allSignerTypesList())
}
// TODO: we'll need to switch validity here based on type once we have more then one supported configuration type
// alternatively, we could try to use an awsconfig type file, but this makes the virtual box setup more tedious. A third option is a json blob
_, err = wallet.FromKeyFile(s.File)
if err != nil {
return false, fmt.Errorf("file %s invalid: %w", s.File, err)
}
return true, nil
}
// ErrUnsupportedSignerType indicates the signer type being used is unsupported.
var ErrUnsupportedSignerType = errors.New("unsupported signer type")
// SignerType is the signer type
//
//go:generate go run golang.org/x/tools/cmd/stringer -type=SignerType -linecomment
type SignerType int
const (
// FileType is a file-based signer.
FileType SignerType = iota + 1 // File
// AWSType is an aws kms based signer.
AWSType // AWS
// GCPType is a gcp cloud based signer.
GCPType // GCP
)
// AllSignerTypes is a list of all contract types. Since we use stringer and this is a testing library, instead
// of manually copying all these out we pull the names out of stringer. In order to make sure stringer is updated, we panic on
// any method called where the index is higher than the stringer array length.
var AllSignerTypes []SignerType
// set all contact types.
func init() {
for i := 0; i < len(_SignerType_index); i++ {
contractType := SignerType(i)
AllSignerTypes = append(AllSignerTypes, contractType)
}
}
// allSignerTypesList prints a list of all signer types. This is useful for returning errors.
func allSignerTypesList() string {
var res []string
for _, signerType := range AllSignerTypes {
res = append(res, signerType.String())
}
return strings.Join(res, ",")
}
// SignerFromConfig creates a new signer from a signer config.
// TODO: this needs to be moved to some kind of common package.
// in the old code configs were split into responsible packages. Maybe something like that works here?
func SignerFromConfig(ctx context.Context, config SignerConfig) (signer.Signer, error) {
switch config.Type {
case FileType.String():
wall, err := wallet.FromKeyFile(core.ExpandOrReturnPath(config.File))
if err != nil {
return nil, fmt.Errorf("could not add signer: %w", err)
}
res := localsigner.NewSigner(wall.PrivateKey())
return res, nil
case AWSType.String():
awsConfig, err := DecodeAWSConfig(config.File)
if err != nil {
return nil, fmt.Errorf("could not decode aws config: %w", err)
}
res, err := awssigner.NewKmsSigner(ctx, awsConfig.Region, awsConfig.AccessKey, awsConfig.AccessSecret, awsConfig.KeyID)
if err != nil {
return nil, fmt.Errorf("could not decode aws config: %w", err)
}
return res, nil
case GCPType.String():
gcpConfig, err := DecodeGCPConfig(config.File)
if err != nil {
return nil, fmt.Errorf("could not decode gcp config: %w", err)
}
return makeGCPSigner(ctx, gcpConfig)
default:
return nil, fmt.Errorf("could not create signer: %w", ErrUnsupportedSignerType)
}
}
func makeGCPSigner(ctx context.Context, gcpConfig GCPConfig) (signer.Signer, error) {
var options []option.ClientOption
if gcpConfig.CredentialFile != "" {
options = append(options, option.WithCredentialsFile(gcpConfig.CredentialFile))
}
if gcpConfig.Endpoint != "" {
options = append(options, option.WithEndpoint(gcpConfig.Endpoint))
}
keyClient, err := kms.NewKeyManagementClient(ctx, options...)
if err != nil {
return nil, fmt.Errorf("could not create key client: %w", err)
}
res, err := gcpsigner.NewManagedKey(ctx, keyClient, gcpConfig.KeyName)
if err != nil {
return nil, fmt.Errorf("could not create managed key: %w", err)
}
return res, nil
}
// GCPConfig is the config for a GCP signer.
type GCPConfig struct {
// KeyName is the name of the key to use.
KeyName string `yaml:"key_name"`
// CredentialFile is the path to the credentials file.
// note: this is not recommended for production use.
// workload identity federation is recommended.
CredentialFile string `yaml:"credential_file"`
// Endpoint is the endpoint to use. This is useful for testing.
Endpoint string `yaml:"endpoint"`
}
// Encode encodes the config to yaml.
func (a GCPConfig) Encode() ([]byte, error) {
output, err := yaml.Marshal(&a)
if err != nil {
return nil, fmt.Errorf("could not unmarshall config %s: %w", ellipsis.Shorten(spew.Sdump(a), 20), err)
}
return output, nil
}
// DecodeGCPConfig decodes the config from a file.
func DecodeGCPConfig(filePath string) (cfg GCPConfig, err error) {
input, err := os.ReadFile(filepath.Clean(filePath))
if err != nil {
return GCPConfig{}, fmt.Errorf("failed to read file: %w", err)
}
err = yaml.Unmarshal(input, &cfg)
if err != nil {
return GCPConfig{}, fmt.Errorf("could not unmarshall config %s: %w", ellipsis.Shorten(string(input), 30), err)
}
return cfg, nil
}
// AWSConfig is the config for an AWS signer.
// this should match the schema of the file passed in.
type AWSConfig struct {
// Region is the region the signer is in.
Region string `yaml:"region"`
// AccessKey is the access key for the signer.
AccessKey string `yaml:"access_key"`
// AccessSecret is the access secret for the signer.
AccessSecret string `yaml:"access_secret"`
// KeyID is the key id for the signer.
KeyID string `yaml:"key_id"`
}
// Encode encodes the config to yaml.
func (a AWSConfig) Encode() ([]byte, error) {
output, err := yaml.Marshal(&a)
if err != nil {
return nil, fmt.Errorf("could not unmarshall config %s: %w", ellipsis.Shorten(spew.Sdump(a), 20), err)
}
return output, nil
}
// DecodeAWSConfig decodes the config from a file.
func DecodeAWSConfig(filePath string) (cfg AWSConfig, err error) {
input, err := os.ReadFile(filepath.Clean(filePath))
if err != nil {
return AWSConfig{}, fmt.Errorf("failed to read file: %w", err)
}
err = yaml.Unmarshal(input, &cfg)
if err != nil {
return AWSConfig{}, fmt.Errorf("could not unmarshall config %s: %w", ellipsis.Shorten(string(input), 30), err)
}
return cfg, nil
}