-
Notifications
You must be signed in to change notification settings - Fork 0
/
minio.go
290 lines (245 loc) · 6.24 KB
/
minio.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
package main
import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"io"
"log/slog"
"net/url"
"os"
"path/filepath"
"github.com/minio/madmin-go/v3"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/minio/minio-go/v7/pkg/lifecycle"
"github.com/rclone/rclone/backend/s3"
)
type Minio struct {
log *slog.Logger
config s3.Options
client *minio.Client
adminClient *madmin.AdminClient
}
// ListBuckets returns a list of buckets in the Minio instance.
func (m *Minio) ListBuckets(ctx context.Context) ([]string, error) {
var buckets []string
bks, err := m.client.ListBuckets(ctx)
if err != nil {
return nil, err
}
for _, bucket := range bks {
buckets = append(buckets, bucket.Name)
}
return buckets, nil
}
const (
// SourceMetadata is the name of the file that contains the additional metadata for an instance, such as IAM configuration
SourceMetadata = "metadata.tar.gz"
// fileIAM is the name of the file that contains the IAM configuration
fileIAM = "iam.zip"
// fileBuckets is the name of the file that contains the bucket metadata
fileBuckets = "buckets.zip"
// fileConfig is the name of the file that contains the Minio configuration
fileConfig = "config.txt"
)
// SourceMetadata returns the additional metadata for an instance
//
// The exported data includes:
// - IAM configuration (fileIAM)
// - Bucket metadata (fileBuckets)
// - Minio configuration (fileConfig)
//
// The exported data is stored in a tar.gz file in the temporary directory, and the path to the file is returned.
func (m *Minio) SourceMetadata(ctx context.Context) (string, error) {
dir, err := os.MkdirTemp("", "s32s3-*")
if err != nil {
return "", err
}
f, err := os.Create(filepath.Join(dir, SourceMetadata))
if err != nil {
return "", err
}
defer f.Close()
gzw := gzip.NewWriter(f)
defer gzw.Close()
archive := tar.NewWriter(gzw)
defer archive.Close()
// IAM
iam, err := m.adminClient.ExportIAM(ctx)
if err != nil {
return "", err
}
buf := bytes.NewBuffer(nil)
io.Copy(buf, iam)
iam.Close()
err = archive.WriteHeader(&tar.Header{
Name: fileIAM,
Mode: 0o644,
Size: int64(buf.Len()),
})
if err != nil {
return "", err
}
io.Copy(archive, buf)
// Buckets
buf.Reset()
buckets, err := m.adminClient.ExportBucketMetadata(ctx, "")
if err != nil {
return "", err
}
io.Copy(buf, buckets)
buckets.Close()
err = archive.WriteHeader(&tar.Header{
Name: fileBuckets,
Mode: 0o644,
Size: int64(buf.Len()),
})
if err != nil {
return "", err
}
io.Copy(archive, buf)
// OIDC
buf.Reset()
oidc, err := m.adminClient.GetConfig(ctx)
if err != nil {
return "", err
}
err = archive.WriteHeader(&tar.Header{
Name: fileConfig,
Mode: 0o644,
Size: int64(len(oidc)),
})
if err != nil {
return "", err
}
archive.Write(oidc)
return f.Name(), nil
}
// RestoreMeta restores the additional metadata for an instance, such as IAM configuration, bucket metadata, and OIDC configuration, from a tar.gz archive.
// The archive is expected to contain the following files:
// - fileIAM: IAM configuration
// - fileBuckets: Bucket metadata
// - fileConfig: OIDC configuration
func (m *Minio) RestoreMeta(ctx context.Context, meta string) error {
f, err := os.Open(meta)
if err != nil {
return err
}
defer f.Close()
gzr, err := gzip.NewReader(f)
if err != nil {
return err
}
defer gzr.Close()
archive := tar.NewReader(gzr)
for {
header, err := archive.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
switch header.Name {
case fileIAM:
if err := m.adminClient.ImportIAM(ctx, io.NopCloser(archive)); err != nil {
return err
}
case fileBuckets:
resp, err := m.adminClient.ImportBucketMetadata(ctx, "", io.NopCloser(archive))
if err != nil {
return err
}
for name, value := range resp.Buckets {
if value.Err != "" {
m.log.Error("failed to import bucket", "bucket", name, "err", value.Err)
continue
}
m.log.Info("imported bucket", "bucket", name, "value", value)
}
case fileConfig:
if err := m.adminClient.SetConfig(ctx, io.NopCloser(archive)); err != nil {
return err
}
}
}
return nil
}
type BackupBucketOptions struct {
ExpirationDays int
Bucket string
}
// AssertOrCreateBucket ensures that the specified bucket exists, and if not, creates it with versioning and a lifecycle policy to expire old versions.
// The bucket will be created with the specified expiration days for old versions. If no expiration days are provided, a default of 7 days will be used.
func (m *Minio) AssertOrCreateBucket(ctx context.Context, opt BackupBucketOptions) error {
log := m.log.With("bucket", opt.Bucket)
log.Info("checking if bucket exists")
exists, err := m.client.BucketExists(ctx, opt.Bucket)
if err != nil {
return err
}
if exists {
log.Info("found bucket")
return nil
}
log.Info("creating bucket")
err = m.client.MakeBucket(ctx, opt.Bucket, minio.MakeBucketOptions{})
if err != nil {
return err
}
if opt.ExpirationDays < 0 {
return nil
}
if opt.ExpirationDays == 0 {
opt.ExpirationDays = 7
}
log.Info("enabling versioning")
err = m.client.EnableVersioning(ctx, opt.Bucket)
if err != nil {
return err
}
log.Info("setting lifecycle")
err = m.client.SetBucketLifecycle(ctx, opt.Bucket, &lifecycle.Configuration{
Rules: []lifecycle.Rule{
{
NoncurrentVersionExpiration: lifecycle.NoncurrentVersionExpiration{
NoncurrentDays: lifecycle.ExpirationDays(opt.ExpirationDays),
},
Status: "Enabled",
},
},
})
if err != nil {
return err
}
return nil
}
func NewMinio(logger *slog.Logger, config s3.Options) (*Minio, error) {
u, err := url.Parse(config.Endpoint)
if err != nil {
return &Minio{}, err
}
creds := credentials.New(&credentials.Static{
Value: credentials.Value{
AccessKeyID: config.AccessKeyID,
SecretAccessKey: config.SecretAccessKey,
},
})
c, err := minio.New(u.Host, &minio.Options{
Creds: creds,
Region: config.Region,
Secure: u.Scheme == "https",
})
if err != nil {
return &Minio{}, err
}
ac, err := madmin.NewWithOptions(u.Host, &madmin.Options{
Creds: creds,
Secure: u.Scheme == "https",
})
if err != nil {
return &Minio{}, err
}
return &Minio{log: logger, config: config, client: c, adminClient: ac}, nil
}