forked from techknowlogick/certmagic-s3
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paths3.go
262 lines (229 loc) · 7.03 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
package s3
import (
"bytes"
"context"
"errors"
"fmt"
"io/fs"
"io/ioutil"
"strings"
"time"
"os"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/certmagic"
minio "github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"go.uber.org/zap"
)
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"`
// EncryptionKey is optional. If you do not wish to encrypt your certficates and key inside the S3 bucket, leave it empty.
EncryptionKey string `json:"encryption_key"`
iowrap IO
}
func init() {
caddy.RegisterModule(new(S3))
}
func (s3 *S3) Provision(context caddy.Context) error {
s3.Logger = context.Logger(s3)
// S3 Client
client, err := minio.New(s3.Host, &minio.Options{
Creds: credentials.NewStaticV4(s3.AccessKey, s3.SecretKey, ""),
Secure: true,
})
if err != nil {
return err
}
s3.Client = client
if len(s3.EncryptionKey) == 0 {
s3.Logger.Info("Clear text certificate storage active")
s3.iowrap = &CleartextIO{}
} else if len(s3.EncryptionKey) != 32 {
s3.Logger.Error("encryption key must have exactly 32 bytes")
return errors.New("encryption key must have exactly 32 bytes")
} else {
s3.Logger.Info("Encrypted certificate storage active")
sb := &SecretBoxIO{}
copy(sb.SecretKey[:], []byte(s3.EncryptionKey))
s3.iowrap = sb
}
return nil
}
func (s3 *S3) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "caddy.storage.s3",
New: func() caddy.Module {
return new(S3)
},
}
}
var (
LockExpiration = 2 * time.Minute
LockPollInterval = 1 * time.Second
LockTimeout = 15 * time.Second
)
func (s3 *S3) Lock(ctx context.Context, key string) error {
s3.Logger.Info(fmt.Sprintf("Lock: %v", s3.objName(key)))
var startedAt = time.Now()
for {
obj, err := s3.Client.GetObject(ctx, s3.Bucket, s3.objLockName(key), minio.GetObjectOptions{})
if err == nil {
return s3.putLockFile(ctx, 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 s3.putLockFile(ctx, key)
}
if lt.Add(LockTimeout).Before(time.Now()) {
// Existing lock file expired, overwrite.
return s3.putLockFile(ctx, key)
}
if startedAt.Add(LockTimeout).Before(time.Now()) {
return errors.New("acquiring lock failed")
}
time.Sleep(LockPollInterval)
}
}
func (s3 *S3) putLockFile(ctx context.Context, key string) error {
// Object does not exist, we're creating a lock file.
r := bytes.NewReader([]byte(time.Now().Format(time.RFC3339)))
_, err := s3.Client.PutObject(ctx, s3.Bucket, s3.objLockName(key), r, int64(r.Len()), minio.PutObjectOptions{})
return err
}
func (s3 *S3) Unlock(ctx context.Context, key string) error {
s3.Logger.Info(fmt.Sprintf("Release lock: %v", s3.objName(key)))
return s3.Client.RemoveObject(ctx, s3.Bucket, s3.objLockName(key), minio.RemoveObjectOptions{})
}
func (s3 *S3) Store(ctx context.Context, key string, value []byte) error {
r := s3.iowrap.ByteReader(value)
s3.Logger.Info(fmt.Sprintf("Store: %v, %v bytes", s3.objName(key), len(value)))
_, err := s3.Client.PutObject(ctx,
s3.Bucket,
s3.objName(key),
r,
int64(r.Len()),
minio.PutObjectOptions{},
)
return err
}
func (s3 *S3) Load(ctx context.Context, key string) ([]byte, error) {
s3.Logger.Info(fmt.Sprintf("Load from disk: %v", key))
buf, err := os.ReadFile(key)
if err != nil {
s3.Logger.Info(fmt.Sprintf("Error: %v", err))
s3.Logger.Info(fmt.Sprintf("Load from s3: %v", s3.objName(key)))
r, err := s3.Client.GetObject(ctx, s3.Bucket, s3.objName(key), minio.GetObjectOptions{})
if err != nil {
if err.Error() == "The specified key does not exist." {
return nil, fs.ErrNotExist
}
return nil, err
} else if r != nil {
// AWS (at least) doesn't return an error on key doesn't exist. We have
// to examine the empty object returned.
_, err = r.Stat()
if err != nil {
er := minio.ToErrorResponse(err)
if er.StatusCode == 404 {
return nil, fs.ErrNotExist
}
}
}
defer r.Close()
buf, err := ioutil.ReadAll(s3.iowrap.WrapReader(r))
if err != nil {
return nil, err
}
return buf, nil
}
return buf, nil
}
func (s3 *S3) Delete(ctx context.Context, key string) error {
s3.Logger.Info(fmt.Sprintf("Delete: %v", s3.objName(key)))
return s3.Client.RemoveObject(ctx, s3.Bucket, s3.objName(key), minio.RemoveObjectOptions{})
}
func (s3 *S3) Exists(ctx context.Context, key string) bool {
s3.Logger.Info(fmt.Sprintf("Exists: %v", s3.objName(key)))
_, err := s3.Client.StatObject(ctx, s3.Bucket, s3.objName(key), minio.StatObjectOptions{})
return err == nil
}
func (s3 *S3) List(ctx context.Context, prefix string, recursive bool) ([]string, error) {
var keys []string
for obj := range s3.Client.ListObjects(ctx, s3.Bucket, minio.ListObjectsOptions{
Prefix: s3.objName(""),
Recursive: true,
}) {
keys = append(keys, obj.Key)
}
return keys, nil
}
func (s3 *S3) Stat(ctx context.Context, key string) (certmagic.KeyInfo, error) {
s3.Logger.Info(fmt.Sprintf("Stat: %v", s3.objName(key)))
var ki certmagic.KeyInfo
oi, err := s3.Client.StatObject(ctx, s3.Bucket, s3.objName(key), minio.StatObjectOptions{})
if err != nil {
return ki, fs.ErrNotExist
}
ki.Key = key
ki.Size = oi.Size
ki.Modified = oi.LastModified
ki.IsTerminal = true
return ki, nil
}
func (s3 *S3) objName(key string) string {
return fmt.Sprintf("%s/%s", strings.TrimPrefix(s3.Prefix, "/"), strings.TrimPrefix(key, "/"))
}
func (s3 *S3) objLockName(key string) string {
return s3.objName(key) + ".lock"
}
// CertMagicStorage converts s to a certmagic.Storage instance.
func (s3 *S3) CertMagicStorage() (certmagic.Storage, error) {
return s3, nil
}
func (s3 *S3) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
key := d.Val()
var value string
if !d.Args(&value) {
continue
}
switch key {
case "host":
s3.Host = value
case "bucket":
s3.Bucket = value
case "access_key":
s3.AccessKey = value
case "secret_key":
s3.SecretKey = value
case "prefix":
if value != "" {
s3.Prefix = value
} else {
s3.Prefix = "caddy"
}
case "encryption_key":
s3.EncryptionKey = value
}
}
return nil
}
var (
_ caddy.Provisioner = (*S3)(nil)
_ caddy.StorageConverter = (*S3)(nil)
_ caddyfile.Unmarshaler = (*S3)(nil)
)