-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathsigner.go
More file actions
251 lines (211 loc) · 6.73 KB
/
signer.go
File metadata and controls
251 lines (211 loc) · 6.73 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Package aws implements a signer.Signer backed by AWS KMS.
// It delegates signing to a remote KMS key and caches the public key locally.
package aws
import (
"context"
"crypto/ed25519"
"crypto/sha256"
"crypto/x509"
"errors"
"fmt"
"net"
"sync"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/kms"
"github.com/aws/aws-sdk-go-v2/service/kms/types"
"github.com/aws/smithy-go"
"github.com/libp2p/go-libp2p/core/crypto"
)
// KMSClient is the subset of the AWS KMS client API that KmsSigner needs.
// This allows mocking in tests.
type KMSClient interface {
Sign(ctx context.Context, params *kms.SignInput, optFns ...func(*kms.Options)) (*kms.SignOutput, error)
GetPublicKey(ctx context.Context, params *kms.GetPublicKeyInput, optFns ...func(*kms.Options)) (*kms.GetPublicKeyOutput, error)
}
// Options configures optional KmsSigner behaviour.
type Options struct {
// Timeout for individual KMS Sign API calls. Default: 10s.
Timeout time.Duration
// MaxRetries for transient KMS failures during Sign. Default: 3.
MaxRetries int
}
func (o *Options) timeout() time.Duration { return o.Timeout }
func (o *Options) maxRetries() int { return o.MaxRetries }
// KmsSigner implements the signer.Signer interface using AWS KMS.
type KmsSigner struct {
client KMSClient
keyID string
opts Options
mu sync.RWMutex
pubKey crypto.PubKey
address []byte
}
// NewKmsSigner creates a new Signer backed by an AWS KMS Ed25519 key.
// It uses the standard AWS credential chain (env vars, ~/.aws/credentials, IAM roles, etc.).
func NewKmsSigner(ctx context.Context, region string, profile string, keyID string, opts *Options) (*KmsSigner, error) {
if keyID == "" {
return nil, fmt.Errorf("aws kms key ID is required")
}
var cfgOpts []func(*awsconfig.LoadOptions) error
if region != "" {
cfgOpts = append(cfgOpts, awsconfig.WithRegion(region))
}
if profile != "" {
cfgOpts = append(cfgOpts, awsconfig.WithSharedConfigProfile(profile))
}
cfg, err := awsconfig.LoadDefaultConfig(ctx, cfgOpts...)
if err != nil {
return nil, fmt.Errorf("failed to load AWS config: %w", err)
}
client := kms.NewFromConfig(cfg)
return kmsSignerFromClient(ctx, client, keyID, opts)
}
// kmsSignerFromClient creates a KmsSigner from an existing KMS client.
// Useful for testing with a mock client.
func kmsSignerFromClient(ctx context.Context, client KMSClient, keyID string, opts *Options) (*KmsSigner, error) {
if keyID == "" {
return nil, fmt.Errorf("aws kms key ID is required")
}
if client == nil {
return nil, fmt.Errorf("aws kms client is required")
}
o := Options{Timeout: 1 * time.Second, MaxRetries: 3}
if opts != nil {
if opts.Timeout > 0 {
o.Timeout = opts.Timeout
}
if opts.MaxRetries >= 0 {
o.MaxRetries = opts.MaxRetries
}
}
s := &KmsSigner{
client: client,
keyID: keyID,
opts: o,
}
// Fetch and cache the public key eagerly so we fail fast on misconfiguration.
if err := s.fetchPublicKey(ctx); err != nil {
return nil, fmt.Errorf("failed to fetch public key from KMS: %w", err)
}
return s, nil
}
// fetchPublicKey retrieves the public key from KMS and caches it.
func (s *KmsSigner) fetchPublicKey(ctx context.Context) error {
out, err := s.client.GetPublicKey(ctx, &kms.GetPublicKeyInput{
KeyId: aws.String(s.keyID),
})
if err != nil {
return fmt.Errorf("KMS GetPublicKey failed: %w", err)
}
if out.KeyId == nil || *out.KeyId != s.keyID {
return fmt.Errorf("KMS returned unexpected key ID: %v", out.KeyId)
}
// AWS returns the public key as a DER-encoded X.509 SubjectPublicKeyInfo.
pub, err := x509.ParsePKIXPublicKey(out.PublicKey)
if err != nil {
return fmt.Errorf("failed to parse KMS public key: %w", err)
}
edPubKey, ok := pub.(ed25519.PublicKey)
if !ok {
return fmt.Errorf("unsupported key type from KMS: expected ed25519, got %T", pub)
}
cryptoPubKey, err := crypto.UnmarshalEd25519PublicKey(edPubKey)
if err != nil {
return fmt.Errorf("failed to convert to libp2p pubkey: %w", err)
}
bz, err := cryptoPubKey.Raw()
if err != nil {
return fmt.Errorf("failed to get raw pubkey bytes: %w", err)
}
address := sha256.Sum256(bz)
s.mu.Lock()
defer s.mu.Unlock()
s.pubKey = cryptoPubKey
s.address = address[:]
return nil
}
// Sign signs a message using the remote KMS key with configurable timeout
// and retry with exponential backoff.
func (s *KmsSigner) Sign(ctx context.Context, message []byte) ([]byte, error) {
var lastErr error
maxRetries := s.opts.maxRetries()
timeout := s.opts.timeout()
maxAttempts := maxRetries + 1
for attempt := range maxAttempts {
if err := ctx.Err(); err != nil {
return nil, err
}
if attempt > 0 {
// Exponential backoff: 100ms, 200ms, 400ms, ...
backoff := time.Duration(100<<uint(attempt-1)) * time.Millisecond
select {
case <-ctx.Done():
return nil, fmt.Errorf("KMS Sign canceled: %w", ctx.Err())
case <-time.After(backoff):
}
}
callCtx, cancel := context.WithTimeout(ctx, timeout)
out, err := s.client.Sign(callCtx, &kms.SignInput{
KeyId: aws.String(s.keyID),
Message: message,
MessageType: types.MessageTypeRaw,
SigningAlgorithm: types.SigningAlgorithmSpecEd25519Sha512,
})
cancel()
if err != nil {
lastErr = err
if !isRetryableKMSError(err) {
return nil, fmt.Errorf("AWS KMS sign failed with non-retryable error: %w", err)
}
continue
}
if out.KeyId == nil || *out.KeyId != s.keyID {
return nil, fmt.Errorf("KMS returned unexpected key ID: %v", out.KeyId)
}
return out.Signature, nil
}
return nil, fmt.Errorf("AWS KMS sign failed after %d attempts: %w", maxAttempts, lastErr)
}
// GetPublic returns the cached public key.
func (s *KmsSigner) GetPublic() (crypto.PubKey, error) {
s.mu.RLock()
pubKey := s.pubKey
s.mu.RUnlock()
if pubKey == nil {
return nil, fmt.Errorf("public key not loaded")
}
return pubKey, nil
}
// GetAddress returns the cached address derived from the public key.
func (s *KmsSigner) GetAddress() ([]byte, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.address == nil {
return nil, fmt.Errorf("address not loaded")
}
r := make([]byte, len(s.address))
copy(r, s.address)
return r, nil
}
func isRetryableKMSError(err error) bool {
if errors.Is(err, context.Canceled) {
return false
}
if errors.Is(err, context.DeadlineExceeded) {
return true
}
var netErr net.Error
if errors.As(err, &netErr) {
return true
}
var apiErr smithy.APIError
if errors.As(err, &apiErr) {
switch apiErr.ErrorCode() {
case "DependencyTimeoutException", "KMSInternalException", "KeyUnavailableException", "ThrottlingException", "ServiceUnavailableException", "InternalFailure", "InternalException", "RequestTimeout", "RequestTimeoutException":
return true
}
}
return false
}