forked from edwardwc/better-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic-s3.go
240 lines (207 loc) · 6.23 KB
/
generic-s3.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
package badgers3
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/caddyserver/certmagic"
minio "github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"io"
"io/fs"
"io/ioutil"
"log"
"time"
)
type S3Opts struct {
Endpoint string
Bucket string
AccessKeyID string
SecretAccessKey string
ObjPrefix string
// EncryptionKey is optional. If you do not wish to encrypt your certficates and key inside the S3 bucket, leave it empty.
EncryptionKey []byte
}
type S3Storage struct {
prefix string
bucket string
s3client *minio.Client
iowrap IO
}
func NewS3Storage(opts S3Opts) (*S3Storage, error) {
gs3 := &S3Storage{
prefix: opts.ObjPrefix,
bucket: opts.Bucket,
}
if opts.EncryptionKey == nil || len(opts.EncryptionKey) == 0 {
log.Println("Clear text certificate storage active")
gs3.iowrap = &CleartextIO{}
} else if len(opts.EncryptionKey) != 32 {
return nil, errors.New("encryption key must have exactly 32 bytes")
} else {
log.Println("Encrypted certificate storage active")
sb := &SecretBoxIO{}
copy(sb.SecretKey[:], opts.EncryptionKey)
gs3.iowrap = sb
}
var err error
gs3.s3client, err = minio.New(opts.Endpoint, &minio.Options{
Creds: credentials.NewStaticV4(opts.AccessKeyID, opts.SecretAccessKey, ""),
Secure: true,
})
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
ok, err := gs3.s3client.BucketExists(ctx, opts.Bucket)
if err != nil {
return nil, err
}
if !ok {
return nil, fmt.Errorf("S3 bucket %s does not exist", opts.Bucket)
}
return gs3, nil
}
var (
LockExpiration = 2 * time.Minute
LockPollInterval = 1 * time.Second
LockTimeout = 15 * time.Second
)
func (gs *S3Storage) Lock(ctx context.Context, key string) error {
// There is no need to lock any file if it is cached so we return if it is cached
if isCacheEntryExistent([]byte(key)) {
return nil
}
var startedAt = time.Now()
for {
obj, err := gs.s3client.GetObject(ctx, gs.bucket, gs.objLockName(key), minio.GetObjectOptions{})
if err == nil {
return gs.putLockFile(key)
}
buf, err := ioutil.ReadAll(obj)
if err != nil {
// Retry
continue
}
lt, err := time.Parse(time.RFC3339, string(buf))
if err != nil {
// Lock file does not make sense, overwrite.
return gs.putLockFile(key)
}
if lt.Add(LockTimeout).Before(time.Now()) {
// Existing lock file expired, overwrite.
return gs.putLockFile(key)
}
if startedAt.Add(LockTimeout).Before(time.Now()) {
return errors.New("acquiring lock failed")
}
time.Sleep(LockPollInterval)
}
return errors.New("locking failed")
}
func (gs *S3Storage) putLockFile(key string) error {
// Object does not exist, we're creating a lock file.
r := bytes.NewReader([]byte(time.Now().Format(time.RFC3339)))
_, err := gs.s3client.PutObject(context.Background(), gs.bucket, gs.objLockName(key), r, int64(r.Len()), minio.PutObjectOptions{})
return err
}
func (gs *S3Storage) Unlock(ctx context.Context, key string) error {
// There is no need to unlock any file if it is cached so we return if it is cached
if isCacheEntryExistent([]byte(key)) {
return nil
}
return gs.s3client.RemoveObject(ctx, gs.bucket, gs.objLockName(key), minio.RemoveObjectOptions{})
}
func (gs *S3Storage) Store(ctx context.Context, key string, value []byte) error {
r := gs.iowrap.ByteReader(value)
_, err := gs.s3client.PutObject(ctx,
gs.bucket,
gs.objName(key),
r,
int64(r.Len()),
minio.PutObjectOptions{},
)
return err
}
func (gs *S3Storage) Load(ctx context.Context, key string) ([]byte, error) {
// We try to get the cached file from our storage here
if isCacheEntryExistent([]byte(key)) {
// Get the key info
rawKi := getCacheEntry([]byte(key))
if rawKi != nil {
// We have the cached file, return it as a byte array
return []byte(*rawKi), nil
}
}
r, err := gs.s3client.GetObject(ctx, gs.bucket, gs.objName(key), minio.GetObjectOptions{})
if err != nil {
return nil, fs.ErrNotExist
}
defer r.Close()
buf, err := io.ReadAll(gs.iowrap.WrapReader(r))
if err != nil {
return nil, fs.ErrNotExist
}
// We have gotten a file from S3, let's cache it, no need to do any marshalling here!
setCacheEntry([]byte(key), buf, time.Hour*1)
return buf, nil
}
func (gs *S3Storage) Delete(ctx context.Context, key string) error {
return gs.s3client.RemoveObject(ctx, gs.bucket, gs.objName(key), minio.RemoveObjectOptions{})
}
func (gs *S3Storage) Exists(ctx context.Context, key string) bool {
_, err := gs.s3client.StatObject(ctx, gs.bucket, gs.objName(key), minio.StatObjectOptions{})
return err == nil
}
func (gs *S3Storage) List(ctx context.Context, prefix string, recursive bool) ([]string, error) {
var keys []string
for obj := range gs.s3client.ListObjects(ctx, gs.bucket, minio.ListObjectsOptions{
Prefix: prefix,
Recursive: recursive,
}) {
keys = append(keys, obj.Key)
}
return keys, nil
}
func (gs *S3Storage) Stat(ctx context.Context, key string) (certmagic.KeyInfo, error) {
var ki certmagic.KeyInfo
// First we check if we've already cached the stat data for the file
if isCacheEntryExistent([]byte(key + "_ki")) {
// Get the key info
rawKi := getCacheEntry([]byte(key + "_ki"))
if rawKi != nil {
// Ensure that we only continue the cache fetch process if the key exists
// Deserialize
err := json.Unmarshal([]byte(*rawKi), &ki)
if err == nil {
// Only return if we had no errors with deserialization and actually got the value
return ki, nil
}
}
}
// This is the normal flow and will contact S3 for the data and then cache it afterwards
oi, err := gs.s3client.StatObject(ctx, gs.bucket, gs.objName(key), minio.StatObjectOptions{})
if err != nil {
return ki, err
}
ki.Key = key
ki.Size = oi.Size
ki.Modified = oi.LastModified
ki.IsTerminal = true
// Store the info in the cache storage so we don't have to contact S3 again for a while
jsonKi, err := json.Marshal(ki)
if err == nil {
// Only set when we know the JSON data is valid
setCacheEntry([]byte(key+"_ki"), jsonKi, time.Hour*1)
}
// Return
return ki, nil
}
func (gs *S3Storage) objName(key string) string {
return gs.prefix + "/" + key
}
func (gs *S3Storage) objLockName(key string) string {
return gs.objName(key) + ".lock"
}