-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
main.go
317 lines (265 loc) · 8.45 KB
/
main.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
// Copyright (c) 2015-2022 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"bytes"
"context"
"crypto/sha256"
"flag"
"fmt"
"hash"
"io"
"log"
"net/url"
"os"
"strings"
"sync"
"time"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
var (
sourceEndpoint, sourceAccessKey, sourceSecretKey string
sourceBucket, sourcePrefix string
targetEndpoint, targetAccessKey, targetSecretKey string
targetBucket, targetPrefix string
minimumObjectAge string
debug bool
insecure bool
)
func buildS3Client(endpoint, accessKey, secretKey string, insecure bool) (*minio.Client, error) {
u, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
secure := strings.EqualFold(u.Scheme, "https")
transport, err := minio.DefaultTransport(secure)
if err != nil {
return nil, err
}
if insecure {
// skip TLS verification
transport.TLSClientConfig.InsecureSkipVerify = true
}
clnt, err := minio.New(u.Host, &minio.Options{
Creds: credentials.NewStaticV4(accessKey, secretKey, ""),
Secure: secure,
Transport: transport,
})
if err != nil {
return nil, err
}
return clnt, nil
}
func main() {
flag.StringVar(&sourceEndpoint, "source-endpoint", "https://play.min.io", "S3 endpoint URL")
flag.StringVar(&sourceAccessKey, "source-access-key", "Q3AM3UQ867SPQQA43P2F", "S3 Access Key")
flag.StringVar(&sourceSecretKey, "source-secret-key", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG", "S3 Secret Key")
flag.StringVar(&sourceBucket, "source-bucket", "", "Select a specific bucket")
flag.StringVar(&sourcePrefix, "source-prefix", "", "Select a prefix")
flag.StringVar(&targetEndpoint, "target-endpoint", "https://play.min.io", "S3 endpoint URL")
flag.StringVar(&targetAccessKey, "target-access-key", "Q3AM3UQ867SPQQA43P2F", "S3 Access Key")
flag.StringVar(&targetSecretKey, "target-secret-key", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG", "S3 Secret Key")
flag.StringVar(&targetBucket, "target-bucket", "", "Select a specific bucket")
flag.StringVar(&targetPrefix, "target-prefix", "", "Select a prefix")
flag.StringVar(&minimumObjectAge, "minimum-object-age", "0s", "Ignore objects younger than the specified age")
flag.BoolVar(&debug, "debug", false, "Prints HTTP network calls to S3 endpoint")
flag.BoolVar(&insecure, "insecure", false, "Disable TLS verification")
flag.Parse()
if sourceEndpoint == "" {
log.Fatalln("source Endpoint is not provided")
}
if sourceAccessKey == "" {
log.Fatalln("source Access key is not provided")
}
if sourceSecretKey == "" {
log.Fatalln("source Secret key is not provided")
}
if sourceBucket == "" && sourcePrefix != "" {
log.Fatalln("--source-prefix is specified without --source-bucket.")
}
if targetEndpoint == "" {
log.Fatalln("target Endpoint is not provided")
}
if targetAccessKey == "" {
log.Fatalln("target Access key is not provided")
}
if targetSecretKey == "" {
log.Fatalln("target Secret key is not provided")
}
if targetBucket == "" && targetPrefix != "" {
log.Fatalln("--target-prefix is specified without --target-bucket.")
}
srcU, err := url.Parse(sourceEndpoint)
if err != nil {
log.Fatalln(err)
}
ssecure := strings.EqualFold(srcU.Scheme, "https")
stransport, err := minio.DefaultTransport(ssecure)
if err != nil {
log.Fatalln(err)
}
if insecure {
// skip TLS verification
stransport.TLSClientConfig.InsecureSkipVerify = true
}
ageDelta, err := time.ParseDuration(minimumObjectAge)
if err != nil {
log.Fatalln(err)
}
maxObjectModTime := time.Now().Add(-ageDelta)
// Next object is used to ignore new objects in the source & target
nextObject := func(ch <-chan minio.ObjectInfo) (ctnt minio.ObjectInfo, ok bool) {
for {
ctnt, ok := <-ch
if !ok {
return minio.ObjectInfo{}, false
}
if ctnt.LastModified.Before(maxObjectModTime) {
return ctnt, ok
}
}
}
sclnt, err := buildS3Client(sourceEndpoint, sourceAccessKey, sourceSecretKey, insecure)
if err != nil {
log.Fatalln(err)
}
tclnt, err := buildS3Client(targetEndpoint, targetAccessKey, targetSecretKey, insecure)
if err != nil {
log.Fatalln(err)
}
if debug {
sclnt.TraceOn(os.Stderr)
tclnt.TraceOn(os.Stderr)
}
sopts := minio.ListObjectsOptions{
Recursive: true,
Prefix: sourcePrefix,
}
topts := minio.ListObjectsOptions{
Recursive: true,
Prefix: targetPrefix,
}
srcCh := sclnt.ListObjects(context.Background(), sourceBucket, sopts)
tgtCh := tclnt.ListObjects(context.Background(), targetBucket, topts)
srcCtnt, srcOk := nextObject(srcCh)
tgtCtnt, tgtOk := nextObject(tgtCh)
var srcEOF, tgtEOF bool
srcSha256 := sha256.New()
tgtSha256 := sha256.New()
for {
srcSha256.Reset()
tgtSha256.Reset()
srcEOF = !srcOk
tgtEOF = !tgtOk
// No objects from source AND target: Finish
if srcEOF && tgtEOF {
break
}
if !srcEOF && srcCtnt.Err != nil {
log.Fatal(srcCtnt.Err)
}
if !tgtEOF && tgtCtnt.Err != nil {
log.Fatal(tgtCtnt.Err)
}
// If source doesn't have objects anymore, comparison becomes obvious
if srcEOF {
fmt.Printf("only in target: %s\n", tgtCtnt.Key)
tgtCtnt, tgtOk = <-tgtCh
continue
}
// The same for target
if tgtEOF {
fmt.Printf("only in source: %s\n", srcCtnt.Key)
srcCtnt, srcOk = nextObject(srcCh)
continue
}
if srcCtnt.Key < tgtCtnt.Key {
fmt.Printf("only in source: %s\n", srcCtnt.Key)
srcCtnt, srcOk = nextObject(srcCh)
continue
}
if srcCtnt.Key == tgtCtnt.Key {
if verifyChecksum(sclnt, srcSha256, tgtSha256, srcCtnt, tgtCtnt) {
fmt.Printf("all readable source and target: %s -> %s\n", srcCtnt.Key, tgtCtnt.Key)
}
srcCtnt, srcOk = nextObject(srcCh)
tgtCtnt, tgtOk = nextObject(tgtCh)
continue
}
fmt.Printf("only in target: %s\n", tgtCtnt.Key)
tgtCtnt, tgtOk = nextObject(tgtCh)
}
}
func verifyChecksum(sclnt *minio.Client, srcSha256, tgtSha256 hash.Hash, srcCtnt, tgtCtnt minio.ObjectInfo) (allgood bool) {
opts := minio.GetObjectOptions{}
if srcCtnt.Size != tgtCtnt.Size {
fmt.Printf("differ in size sourceSize: %d, targetSize: %d\n", srcCtnt.Size, tgtCtnt.Size)
return false
} else if srcCtnt.ContentType != tgtCtnt.ContentType {
fmt.Printf("differ in contentType source: %s, target: %s\n", srcCtnt.ContentType, tgtCtnt.ContentType)
return false
}
core := minio.Core{Client: sclnt}
sobj, _, _, err := core.GetObject(context.Background(), sourceBucket, srcCtnt.Key, opts)
if err != nil {
fmt.Printf("unreadable on source: %s (%s)\n", srcCtnt.Key, err)
return false
}
tobj, _, _, err := core.GetObject(context.Background(), targetBucket, tgtCtnt.Key, opts)
if err != nil {
fmt.Printf("unreadable on target: %s (%s)\n", tgtCtnt.Key, err)
return false
}
var sourceFailed, targetFailed bool
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
srcSize, err := io.Copy(srcSha256, sobj)
if err != nil {
fmt.Printf("unreadable on source: %s (%s)\n", srcCtnt.Key, err)
sourceFailed = true
} else if srcSize != srcCtnt.Size {
fmt.Printf("unreadable on source - size differs upon read: %s\n", srcCtnt.Key)
sourceFailed = true
}
}()
go func() {
defer wg.Done()
tgtSize, err := io.Copy(tgtSha256, tobj)
if err != nil {
fmt.Printf("unreadable on target: %s (%s)\n", tgtCtnt.Key, err)
targetFailed = true
} else if tgtSize != tgtCtnt.Size {
fmt.Printf("unreadable on target - size differs upon read: %s\n", tgtCtnt.Key)
targetFailed = true
}
}()
wg.Wait()
sobj.Close()
tobj.Close()
if !sourceFailed && !targetFailed {
ssum := srcSha256.Sum(nil)
tsum := tgtSha256.Sum(nil)
allgood = bytes.Equal(ssum, tsum)
if !allgood {
fmt.Printf("sha256 sum mismatch: %s -> Expected(%x), Found(%x)\n", srcCtnt.Key, ssum, tsum)
}
}
return allgood
}