-
Notifications
You must be signed in to change notification settings - Fork 29
/
s3.go
263 lines (204 loc) · 5.66 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
package certmagic_s3
import (
"bytes"
"context"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path"
"strconv"
"strings"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/certmagic"
"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"`
AccessID string `json:"access_id"`
SecretKey string `json:"secret_key"`
Prefix string `json:"prefix"`
Insecure bool `json:"insecure"`
UseIamProvider bool `json:"use_iam_provider"`
}
func init() {
caddy.RegisterModule(S3{})
}
func (s3 *S3) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
var value string
key := d.Val()
if !d.Args(&value) {
continue
}
switch key {
case "host":
s3.Host = value
case "bucket":
s3.Bucket = value
case "access_id":
s3.AccessID = value
case "secret_key":
s3.SecretKey = value
case "prefix":
s3.Prefix = value
case "insecure":
insecure, err := strconv.ParseBool(value)
if err != nil {
return d.Err("Invalid usage of insecure in s3-storage config: " + err.Error())
}
s3.Insecure = insecure
case "use_iam_provider":
boolValue, err := strconv.ParseBool(value)
if err != nil {
return d.Err("Invalid usage of use_iam_provider in s3-storage config: " + err.Error())
}
s3.UseIamProvider = boolValue
}
}
return nil
}
func (s3 *S3) Provision(ctx caddy.Context) error {
s3.logger = ctx.Logger(s3)
// Load Environment
if s3.Host == "" {
s3.Host = os.Getenv("S3_HOST")
}
if s3.Bucket == "" {
s3.Bucket = os.Getenv("S3_BUCKET")
}
if s3.AccessID == "" {
s3.AccessID = os.Getenv("S3_ACCESS_ID")
}
if s3.SecretKey == "" {
s3.SecretKey = os.Getenv("S3_SECRET_KEY")
}
if s3.Prefix == "" {
s3.Prefix = os.Getenv("S3_PREFIX")
}
if !s3.Insecure {
insecure := os.Getenv("S3_INSECURE")
if insecure != "" {
s3.Insecure, _ = strconv.ParseBool(insecure)
}
}
secure := !s3.Insecure
if !s3.UseIamProvider {
boolVal := os.Getenv("S3_USE_IAM_PROVIDER")
if boolVal != "" {
s3.UseIamProvider, _ = strconv.ParseBool(boolVal)
}
}
var creds *credentials.Credentials
if s3.UseIamProvider {
s3.logger.Info("use iam aws provider for credentials")
creds = credentials.NewIAM("")
} else {
s3.logger.Info("use secret_key and access_id for credentials")
creds = credentials.NewStaticV4(s3.AccessID, s3.SecretKey, "")
}
// S3 Client
client, err := minio.New(s3.Host, &minio.Options{
Creds: creds,
Secure: secure,
})
if err != nil {
return err
} else {
s3.Client = client
}
return nil
}
func (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 {
return nil
}
func (s3 S3) Unlock(ctx context.Context, key string) error {
return nil
}
func (s3 S3) Store(ctx context.Context, key string, value []byte) error {
key = s3.KeyPrefix(key)
length := int64(len(value))
s3.logger.Debug(fmt.Sprintf("Store: %s, %d bytes", key, length))
_, err := s3.Client.PutObject(context.Background(), s3.Bucket, key, bytes.NewReader(value), length, minio.PutObjectOptions{})
return err
}
func (s3 S3) Load(ctx context.Context, key string) ([]byte, error) {
if !s3.Exists(ctx, key) {
return nil, fs.ErrNotExist
}
key = s3.KeyPrefix(key)
s3.logger.Debug(fmt.Sprintf("Load key: %s", key))
object, err := s3.Client.GetObject(context.Background(), s3.Bucket, key, minio.GetObjectOptions{})
if err != nil {
return nil, err
}
return ioutil.ReadAll(object)
}
func (s3 S3) Delete(ctx context.Context, key string) error {
key = s3.KeyPrefix(key)
s3.logger.Debug(fmt.Sprintf("Delete key: %s", key))
return s3.Client.RemoveObject(context.Background(), s3.Bucket, key, minio.RemoveObjectOptions{})
}
func (s3 S3) Exists(ctx context.Context, key string) bool {
key = s3.KeyPrefix(key)
_, err := s3.Client.StatObject(context.Background(), s3.Bucket, key, minio.StatObjectOptions{})
exists := err == nil
s3.logger.Debug(fmt.Sprintf("Check exists: %s, %t", key, exists))
return exists
}
func (s3 S3) List(ctx context.Context, prefix string, recursive bool) ([]string, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
objects := s3.Client.ListObjects(ctx, s3.Bucket, minio.ListObjectsOptions{
Prefix: s3.KeyPrefix(prefix),
Recursive: recursive,
})
keys := make([]string, len(objects))
for object := range objects {
keys = append(keys, s3.CutKeyPrefix(object.Key))
}
return keys, nil
}
func (s3 S3) Stat(ctx context.Context, key string) (certmagic.KeyInfo, error) {
key = s3.KeyPrefix(key)
object, err := s3.Client.StatObject(context.Background(), s3.Bucket, key, minio.StatObjectOptions{})
if err != nil {
s3.logger.Error(fmt.Sprintf("Stat key: %s, error: %v", key, err))
return certmagic.KeyInfo{}, nil
}
s3.logger.Debug(fmt.Sprintf("Stat key: %s, size: %d bytes", key, object.Size))
return certmagic.KeyInfo{
Key: object.Key,
Modified: object.LastModified,
Size: object.Size,
IsTerminal: strings.HasSuffix(object.Key, "/"),
}, err
}
func (s3 S3) KeyPrefix(key string) string {
return path.Join(s3.Prefix, key)
}
func (s3 S3) CutKeyPrefix(key string) string {
cutted, _ := strings.CutPrefix(key, s3.Prefix)
return cutted
}
func (s3 S3) String() string {
return fmt.Sprintf("S3 Storage Host: %s, Bucket: %s, Prefix: %s", s3.Host, s3.Bucket, s3.Prefix)
}