forked from daxxog/certmagic-s3
/
s3.go
358 lines (288 loc) · 8 KB
/
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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package certmagic_s3
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/certmagic"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"go.uber.org/zap"
)
type FASMSLockerClient struct {
endpoint string
apiKey string
}
type FASMSLocker struct {
client *FASMSLockerClient
resourceName string
uuid string
}
type FASMSObtainMutexResponse struct {
Obtained bool `json:"obtained"`
UUID string `json:"uuid"`
}
type FASMSReleaseMutexResponse struct {
Released bool `json:"released"`
}
func (client *FASMSLockerClient) baseMutexUrl() string {
return client.endpoint + "/api/v1/mutex?api_key=" + url.QueryEscape(client.apiKey)
}
func (locker *FASMSLocker) resourceUrl() string {
return locker.client.baseMutexUrl() + "&resource_name=" + url.QueryEscape(locker.resourceName)
}
func (locker *FASMSLocker) obtainMutexUrl(ttl time.Duration) string {
return locker.resourceUrl() + "&ttl=" + url.QueryEscape(strconv.Itoa(int(ttl.Seconds())))
}
func (locker *FASMSLocker) releaseMutexUrl() string {
return locker.resourceUrl() + "&uuid=" + url.QueryEscape(locker.uuid)
}
func (locker *FASMSLocker) Lock(ctx context.Context, ttl time.Duration) error {
lockerErrChan := make(chan error)
go func() {
// keep trying to obtain the lock
for {
resp, err := locker.lock(ttl)
if err != nil {
lockerErrChan <- err
return
}
if resp.Obtained == true {
locker.uuid = resp.UUID
lockerErrChan <- nil
return
}
time.Sleep(500 * time.Millisecond)
}
}()
for {
// either obtain the lock or context expires
select {
case lockErr := <-lockerErrChan:
return lockErr
case <-ctx.Done():
return ctx.Err()
}
}
}
func (locker *FASMSLocker) lock(ttl time.Duration) (*FASMSObtainMutexResponse, error) {
if resp, err := http.Get(locker.obtainMutexUrl(ttl)); err == nil {
if resp.StatusCode == 200 {
if body, err := ioutil.ReadAll(resp.Body); err == nil {
var jsonResp FASMSObtainMutexResponse
if err := json.Unmarshal(body, &jsonResp); err == nil {
return &jsonResp, nil
} else {
return nil, err
}
} else {
return nil, err
}
} else {
err := errors.New("FASMSLocker.Lock: got status code " + strconv.Itoa(resp.StatusCode) + ", but expected 200 on endpoint " + locker.client.endpoint)
return nil, err
}
} else {
return nil, err
}
}
func (locker *FASMSLocker) Unlock(ctx context.Context) error {
unlockerErrChan := make(chan error)
go func() {
resp, err := locker.unlock()
if err != nil {
unlockerErrChan <- err
return
}
if resp.Released == true {
unlockerErrChan <- nil
return
} else {
unlockerErrChan <- errors.New("FASMSLocker.Unlock: could not unlock resource '" + locker.resourceName + "' with uuid '" + locker.uuid + "'")
return
}
}()
for {
// either release the lock or context expires
select {
case lockErr := <-unlockerErrChan:
return lockErr
case <-ctx.Done():
return ctx.Err()
}
}
}
func (locker *FASMSLocker) unlock() (*FASMSReleaseMutexResponse, error) {
client := http.Client{}
if req, err := http.NewRequest(http.MethodDelete, locker.releaseMutexUrl(), nil); err == nil {
if resp, err := client.Do(req); err == nil {
if resp.StatusCode == 200 {
if body, err := ioutil.ReadAll(resp.Body); err == nil {
var jsonResp FASMSReleaseMutexResponse
if err := json.Unmarshal(body, &jsonResp); err == nil {
return &jsonResp, nil
} else {
return nil, err
}
} else {
return nil, err
}
} else {
err := errors.New("FASMSLocker.Unlock: got status code " + strconv.Itoa(resp.StatusCode) + ", but expected 200 on endpoint " + locker.client.endpoint)
return nil, err
}
} else {
return nil, err
}
} else {
return nil, err
}
}
type S3 struct {
Logger *zap.Logger
// S3
Client *minio.Client
Host string `json:"host"`
Bucket string `json:"bucket"`
AccessKey string `json:"access_key"`
SecretKey string `json:"secret_key"`
Prefix string `json:"prefix"`
// FASMS
FASMSClient *FASMSLockerClient
FASMSLocks map[string]*FASMSLocker
FASMSEndpoint string `json:"fasms_endpoint"`
FASMSApiKey string `json:"fasms_api_key"`
}
func init() {
caddy.RegisterModule(new(S3))
}
func (s3 *S3) Provision(context caddy.Context) error {
s3.Logger = context.Logger(s3)
// S3 Client
client, _ := minio.New(s3.Host, &minio.Options{
Creds: credentials.NewStaticV4(s3.AccessKey, s3.SecretKey, ""),
Secure: true,
})
s3.Client = client
// FASMS Client
s3.FASMSClient = &FASMSLockerClient{endpoint: s3.FASMSEndpoint, apiKey: s3.FASMSApiKey}
s3.FASMSLocks = make(map[string]*FASMSLocker)
return nil
}
func (s3 *S3) Cleanup() error {
if s3.Logger != nil {
s3.Logger.Info("S3 Cleanup")
}
for _, lock := range s3.FASMSLocks {
s3.Logger.Info(fmt.Sprintf("Release FASMS Lock: %v", lock.resourceName))
_ = lock.Unlock(context.Background())
}
return nil
}
func (s3 *S3) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "caddy.storage.s3",
New: func() caddy.Module {
return new(S3)
},
}
}
func (s3 *S3) CertMagicStorage() (certmagic.Storage, error) {
return s3, nil
}
func (s3 *S3) Lock(ctx context.Context, key string) error {
s3.Logger.Info(fmt.Sprintf("Lock: %v", key))
lock := &FASMSLocker{client: s3.FASMSClient, resourceName: key}
err := lock.Lock(ctx, time.Minute)
s3.FASMSLocks[key] = lock
if err != nil {
s3.Logger.Error(fmt.Sprintf("Lock error: %v", err))
}
return err
}
func (s3 *S3) Unlock(key string) error {
if lock, exists := s3.FASMSLocks[key]; exists {
s3.Logger.Info(fmt.Sprintf("Release lock: %v", key))
err := lock.Unlock(context.Background())
delete(s3.FASMSLocks, key)
if err != nil {
return err
}
}
return nil
}
func (s3 *S3) Store(key string, value []byte) error {
key = s3.KeyPrefix(key)
s3.Logger.Info(fmt.Sprintf("Store: %v, %v bytes", key, len(value)))
_, err := s3.Client.PutObject(context.Background(), s3.Bucket, key, bytes.NewReader(value), int64(len(value)), minio.PutObjectOptions{})
return err
}
func (s3 *S3) Load(key string) ([]byte, error) {
key = s3.KeyPrefix(key)
s3.Logger.Info(fmt.Sprintf("Load: %v", key))
object, err := s3.Client.GetObject(context.Background(), s3.Bucket, key, minio.GetObjectOptions{})
if err != nil {
return nil, err
}
content, err := ioutil.ReadAll(object)
return content, err
}
func (s3 *S3) Delete(key string) error {
key = s3.KeyPrefix(key)
s3.Logger.Info(fmt.Sprintf("Delete: %v", key))
err := s3.Client.RemoveObject(context.Background(), s3.Bucket, key, minio.RemoveObjectOptions{})
return err
}
func (s3 *S3) Exists(key string) bool {
key = s3.KeyPrefix(key)
s3.Logger.Info(fmt.Sprintf("Exists: %v", key))
_, err := s3.Client.StatObject(context.Background(), s3.Bucket, key, minio.StatObjectOptions{})
return err == nil
}
func (s3 *S3) List(prefix string, recursive bool) ([]string, error) {
prefix = s3.KeyPrefix(prefix)
if !strings.HasSuffix(prefix, "/") {
prefix = prefix + "/"
}
var keys []string
objects := s3.Client.ListObjects(context.Background(), s3.Bucket, minio.ListObjectsOptions{
Prefix: prefix,
Recursive: recursive,
})
for object := range objects {
if !strings.HasSuffix(object.Key, "/") {
keys = append(keys, object.Key)
}
}
return keys, nil
}
func (s3 *S3) Stat(key string) (certmagic.KeyInfo, error) {
key = s3.KeyPrefix(key)
s3.Logger.Info(fmt.Sprintf("Stat: %v", key))
object, err := s3.Client.StatObject(context.Background(), s3.Bucket, key, minio.StatObjectOptions{})
if err != nil {
return certmagic.KeyInfo{}, nil
}
return certmagic.KeyInfo{
Key: object.Key,
Modified: object.LastModified,
Size: object.Size,
IsTerminal: strings.HasSuffix(object.Key, "/"),
}, err
}
func (s3 S3) KeyPrefix(prefix string) string {
if strings.HasPrefix(prefix, s3.Prefix) {
return prefix
} else {
return strings.Join([]string{s3.Prefix, prefix}, "/")
}
}
var _ caddy.Provisioner = (*S3)(nil)