forked from securityclippy/magicstorage
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
s3store.go
307 lines (263 loc) · 7.13 KB
/
s3store.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package s3store
import (
"bytes"
"context"
"errors"
"fmt"
"io/ioutil"
"log"
"path/filepath"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
cm "github.com/caddyserver/certmagic"
)
const lockFileExists = "Lock file for already exists"
// staleLockDuration is the length of time
// before considering a lock to be stale.
const staleLockDuration = 2 * time.Hour
// fileLockPollInterval is how frequently
// to check the existence of a lock file
const fileLockPollInterval = 1 * time.Second
var StorageKeys cm.KeyBuilder
// S3Storage implements the certmagic Storage interface using amazon's
// s3 storage. An effort has been made to make the S3Storage implementation
// as similar as possible to the original filestorage type in order to
// provide a consistent approach to storage backends for certmagic
// for issues, please contact @securityclippy
// S3Storage is safe to use with multiple servers behind an AWS load balancer
// and is safe for concurrent use
type S3Store struct {
prefix string
bucket *string
client *s3.Client
}
func NewS3Store(bucketName, region string) *S3Store {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(region),
)
if err != nil {
log.Fatal(err)
}
client := s3.NewFromConfig(cfg)
store := &S3Store{
bucket: aws.String(bucketName),
client: client,
prefix: "certmagic",
}
return store
}
func NewS3StoreWithCredentials(accessKey, secretKey, bucketName, region string) *S3Store {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKey, secretKey, "")),
config.WithRegion(region),
)
if err != nil {
log.Fatal(err)
}
client := s3.NewFromConfig(cfg)
store := &S3Store{
bucket: aws.String(bucketName),
client: client,
prefix: "certmagic",
}
return store
}
// Exists returns true if key exists in s3
func (s *S3Store) Exists(key string) bool {
input := &s3.GetObjectInput{
Bucket: s.bucket,
Key: aws.String(s.Filename(key)),
}
_, err := s.client.GetObject(context.Background(), input)
if err == nil {
return true
}
var nsk *types.NoSuchKey
return !errors.As(err, &nsk)
}
// Store saves value at key.
func (s *S3Store) Store(key string, value []byte) error {
filename := s.Filename(key)
input := &s3.PutObjectInput{
Bucket: s.bucket,
Key: aws.String(filename),
Body: bytes.NewReader(value),
}
_, err := s.client.PutObject(context.Background(), input)
if err != nil {
return err
}
return nil
}
// Load retrieves the value at key.
func (s *S3Store) Load(key string) ([]byte, error) {
input := &s3.GetObjectInput{
Bucket: s.bucket,
Key: aws.String(s.Filename(key)),
}
result, err := s.client.GetObject(context.Background(), input)
if err != nil {
return nil, err
}
b, err := ioutil.ReadAll(result.Body)
if err != nil {
return nil, err
}
return b, nil
}
// Delete deletes the value at key.
func (s *S3Store) Delete(key string) error {
input := &s3.DeleteObjectInput{
Bucket: s.bucket,
Key: aws.String(s.Filename(key)),
}
_, err := s.client.DeleteObject(context.Background(), input)
if err != nil {
return err
}
return nil
}
// List returns all keys that match prefix.
// because s3 has no concept of directories, everything is an explicit path,
// there is really no such thing as recursive search. This is simply
// here to fulfill the interface requirements of the List function
func (s *S3Store) List(prefix string, recursive bool) ([]string, error) {
var keys []string
prefixPath := s.Filename(prefix)
input := &s3.ListObjectsInput{
Bucket: s.bucket,
Prefix: aws.String(prefixPath),
}
result, err := s.client.ListObjects(context.Background(), input)
if err != nil {
return nil, err
}
for _, k := range result.Contents {
if strings.HasPrefix(*k.Key, prefix) {
keys = append(keys, *k.Key)
}
}
//
return keys, nil
}
// Stat returns information about key.
func (s *S3Store) Stat(key string) (cm.KeyInfo, error) {
input := &s3.GetObjectInput{
Bucket: s.bucket,
Key: aws.String(key),
}
result, err := s.client.GetObject(context.Background(), input)
if err != nil {
return cm.KeyInfo{}, err
}
return cm.KeyInfo{
Key: key,
Size: result.ContentLength,
Modified: *result.LastModified,
IsTerminal: true,
}, nil
}
// Filename returns the key as a path on the file
// system prefixed by S3Storage.Path.
func (s *S3Store) Filename(key string) string {
return filepath.Join(s.prefix, filepath.FromSlash(key))
}
// Lock obtains a lock named by the given key. It blocks
// until the lock can be obtained or an error is returned.
func (s *S3Store) Lock(ctx context.Context, key string) error {
start := time.Now()
lockFile := s.lockFileName(key)
for {
err := s.createLockFile(lockFile)
if err == nil {
// got the lock, yay
return nil
}
if err.Error() != lockFileExists {
// unexpected error
fmt.Println(err)
return fmt.Errorf("creating lock file: %+v", err)
}
// lock file already exists
info, err := s.Stat(lockFile)
switch {
case s.errNoSuchKey(err):
// must have just been removed; try again to create it
continue
case err != nil:
// unexpected error
return fmt.Errorf("accessing lock file: %v", err)
case s.fileLockIsStale(info):
log.Printf("[INFO][%s] Lock for '%s' is stale; removing then retrying: %s",
s, key, lockFile)
s.deleteLockFile(lockFile)
continue
case time.Since(start) > staleLockDuration*2:
// should never happen, hopefully
return fmt.Errorf("possible deadlock: %s passed trying to obtain lock for %s",
time.Since(start), key)
default:
// lockfile exists and is not stale;
// just wait a moment and try again
time.Sleep(fileLockPollInterval)
}
}
}
// Unlock releases the lock for name.
func (s *S3Store) Unlock(key string) error {
return s.deleteLockFile(s.lockFileName(key))
}
func (s *S3Store) String() string {
return "S3Storage:" + s.prefix
}
func (s *S3Store) lockFileName(key string) string {
return filepath.Join(s.lockDir(), StorageKeys.Safe(key)+".lock")
}
func (s *S3Store) lockDir() string {
return filepath.Join(s.prefix, "locks")
}
func (s *S3Store) fileLockIsStale(info cm.KeyInfo) bool {
return time.Since(info.Modified) > staleLockDuration
}
func (s *S3Store) createLockFile(filename string) error {
//lf := s.lockFileName(key)
exists := s.Exists(filename)
if exists {
return fmt.Errorf(lockFileExists)
}
input := &s3.PutObjectInput{
Bucket: s.bucket,
Key: aws.String(filename),
Body: bytes.NewReader([]byte("lock")),
}
_, err := s.client.PutObject(context.Background(), input)
if err != nil {
return err
}
return nil
}
func (s *S3Store) deleteLockFile(keyPath string) error {
input := &s3.DeleteObjectInput{
Bucket: s.bucket,
Key: aws.String(keyPath),
}
_, err := s.client.DeleteObject(context.Background(), input)
if err != nil {
return err
}
return nil
}
func (s *S3Store) errNoSuchKey(err error) bool {
var nsk *types.NoSuchKey
if err != nil {
if errors.As(err, &nsk) {
return true
}
}
return false
}