-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
376 lines (349 loc) · 11.5 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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package main
import (
"context"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"io"
"math"
"net"
"net/http"
"net/url"
"os"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/service/s3"
s3Types "github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/aws/smithy-go"
flag "github.com/stefansundin/go-zflag"
)
const version = "0.1.0"
func init() {
// Do not fail if a region is not specified anywhere
// This is only used for the first call that looks up the bucket region
if _, present := os.LookupEnv("AWS_DEFAULT_REGION"); !present {
os.Setenv("AWS_DEFAULT_REGION", "us-east-1")
}
}
func main() {
var profile, region, endpointURL, caBundle, versionId string
var noVerifySsl, noSignRequest, usePathStyle, debug, versionFlag, helpFlag bool
flag.StringVar(&profile, "profile", "", "Use a specific profile from your credential file.")
flag.StringVar(®ion, "region", "", "The region to use. Overrides config/env settings. Avoids one API call.")
flag.StringVar(&endpointURL, "endpoint-url", "", "Override the S3 endpoint URL. (for use with S3 compatible APIs)")
flag.StringVar(&caBundle, "ca-bundle", "", "The CA certificate bundle to use when verifying SSL certificates.")
flag.StringVar(&versionId, "version-id", "", "Version ID used to reference a specific version of the S3 object.")
flag.BoolVar(&noVerifySsl, "no-verify-ssl", false, "Do not verify SSL certificates.")
flag.BoolVar(&noSignRequest, "no-sign-request", false, "Do not sign requests. This does not work with Amazon S3, but may work with other S3 APIs.")
flag.BoolVar(&usePathStyle, "use-path-style", false, "Use S3 Path Style.")
flag.BoolVar(&debug, "debug", false, "Turn on debug logging.")
flag.BoolVar(&versionFlag, "version", false, "Print version number.")
flag.BoolVar(&helpFlag, "help", false, "Show this help.", flag.OptShorthand('h'))
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "s3verify version %s\n", version)
fmt.Fprintln(os.Stderr, "Copyright (C) 2022 Stefan Sundin")
fmt.Fprintln(os.Stderr, "Website: https://github.com/stefansundin/s3verify")
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "s3verify comes with ABSOLUTELY NO WARRANTY.")
fmt.Fprintln(os.Stderr, "This is free software, and you are welcome to redistribute it under certain")
fmt.Fprintln(os.Stderr, "conditions. See the GNU General Public Licence version 3 for details.")
fmt.Fprintln(os.Stderr)
fmt.Fprintf(os.Stderr, "Usage: %s [options] <LocalPath> <S3Uri>\n", os.Args[0])
fmt.Fprintln(os.Stderr, "LocalPath can be - for stdin.")
fmt.Fprintln(os.Stderr, "S3Uri must have the format s3://<bucketname>/<key>.")
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "Options:")
flag.PrintDefaults()
}
flag.Parse()
if versionFlag {
fmt.Println(version)
os.Exit(0)
} else if helpFlag {
flag.Usage()
os.Exit(0)
} else if flag.NArg() < 2 {
flag.Usage()
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "Error: LocalPath and S3Uri arguments are required!")
os.Exit(1)
} else if flag.NArg() > 2 {
flag.Usage()
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "Error: Too many positional arguments.")
os.Exit(1)
}
if endpointURL != "" {
if !strings.HasPrefix(endpointURL, "http://") && !strings.HasPrefix(endpointURL, "https://") {
fmt.Fprintln(os.Stderr, "Error: the endpoint URL must start with http:// or https://.")
os.Exit(1)
}
if !usePathStyle {
u, err := url.Parse(endpointURL)
if err != nil {
fmt.Fprintln(os.Stderr, "Error: unable to parse the endpoint URL.")
os.Exit(1)
}
hostname := u.Hostname()
if hostname == "localhost" || net.ParseIP(hostname) != nil {
if debug {
fmt.Fprintln(os.Stderr, "Detected IP address in endpoint URL. Implicitly opting in for path style.")
}
usePathStyle = true
}
}
}
localPath := flag.Arg(0)
bucket, key := parseS3Uri(flag.Arg(1))
if bucket == "" || key == "" {
fmt.Fprintln(os.Stderr, "Error: The S3Uri must have the format s3://<bucketname>/<key>")
os.Exit(1)
}
// Open the file
var f *os.File
if localPath == "-" {
f = os.Stdin
} else {
var err error
f, err = os.Open(localPath)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer f.Close()
}
// Initialize the AWS SDK
cfg, err := config.LoadDefaultConfig(
context.TODO(),
func(o *config.LoadOptions) error {
if profile != "" {
o.SharedConfigProfile = profile
}
if caBundle != "" {
f, err := os.Open(caBundle)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
o.CustomCABundle = f
}
if noVerifySsl {
o.HTTPClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
}
if debug {
var lm aws.ClientLogMode = aws.LogRequest | aws.LogResponse
o.ClientLogMode = &lm
}
return nil
},
config.WithAssumeRoleCredentialOptions(func(o *stscreds.AssumeRoleOptions) {
o.TokenProvider = mfaTokenProvider
}),
)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if !noSignRequest {
creds, _ := cfg.Credentials.Retrieve(context.TODO())
if creds.AccessKeyID == "" {
fmt.Fprintln(os.Stderr, "Warning: AWS credentials were not found. Please set up your AWS credentials.")
}
}
client := s3.NewFromConfig(cfg,
func(o *s3.Options) {
if noSignRequest {
o.Credentials = aws.AnonymousCredentials{}
}
if region != "" {
o.Region = region
}
if endpointURL != "" {
o.BaseEndpoint = aws.String(endpointURL)
}
if usePathStyle {
o.UsePathStyle = true
}
})
// Get the bucket location
if endpointURL == "" && region == "" {
bucketLocationOutput, err := client.GetBucketLocation(context.TODO(), &s3.GetBucketLocationInput{
Bucket: aws.String(bucket),
})
if err != nil {
fmt.Fprintln(os.Stderr, err)
var ae smithy.APIError
if errors.As(err, &ae) && ae.ErrorCode() == "AccessDenied" {
fmt.Fprintln(os.Stderr, "\nYou can use --region to manually specify the bucket region.")
}
os.Exit(1)
}
bucketRegion := normalizeBucketLocation(bucketLocationOutput.LocationConstraint)
if debug {
fmt.Fprintf(os.Stderr, "Bucket region: %s\n", bucketRegion)
}
client = s3.NewFromConfig(cfg, func(o *s3.Options) {
if v, ok := os.LookupEnv("AWS_USE_DUALSTACK_ENDPOINT"); !ok || v != "false" {
o.EndpointOptions.UseDualStackEndpoint = aws.DualStackEndpointStateEnabled
}
if noSignRequest {
o.Credentials = aws.AnonymousCredentials{}
}
o.Region = bucketRegion
})
}
fmt.Fprintln(os.Stderr, "Fetching S3 object information...")
if debug {
fmt.Fprintln(os.Stderr)
}
getObjectAttributesInput := &s3.GetObjectAttributesInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
ObjectAttributes: []s3Types.ObjectAttributes{
s3Types.ObjectAttributesChecksum,
s3Types.ObjectAttributesObjectParts,
s3Types.ObjectAttributesObjectSize,
},
MaxParts: aws.Int32(100000),
}
if versionId != "" {
getObjectAttributesInput.VersionId = aws.String(versionId)
}
objAttrs, err := client.GetObjectAttributes(context.TODO(), getObjectAttributesInput)
if err != nil {
if isSmithyErrorCode(err, 404) {
fmt.Fprintln(os.Stderr, "Error: The object does not exist.")
} else {
fmt.Fprintln(os.Stderr, err)
}
os.Exit(1)
}
if debug {
fmt.Fprintln(os.Stderr, string(jsonMustMarshalSortedIndent(objAttrs, "", " ")))
fmt.Fprintln(os.Stderr)
}
if objAttrs.Checksum == nil {
fmt.Fprintln(os.Stderr, "Error: This S3 object was not uploaded using the additional checksum feature. s3verify requires that the object is uploaded with this feature enabled. Please consult https://docs.aws.amazon.com/AmazonS3/latest/userguide/checking-object-integrity.html")
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "You may also find s3sha256sum useful: https://github.com/stefansundin/s3sha256sum")
os.Exit(1)
}
// Compare the file sizes if possible
if localPath != "-" {
stat, err := os.Stat(localPath)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fileSize := stat.Size()
objectSize := aws.ToInt64(objAttrs.ObjectSize)
if objectSize != fileSize {
fmt.Fprintf(os.Stderr, "Error: The size of the S3 object (%d bytes) does not match the size of the local file (%d bytes).\n", objectSize, fileSize)
os.Exit(1)
}
}
algorithm, err := getChecksumAlgorithm(objAttrs.Checksum)
if err != nil {
fmt.Fprintln(os.Stderr, "This S3 object was uploaded using an unsupported checksum algorithm. Please file an issue: https://github.com/stefansundin/s3verify")
os.Exit(1)
}
objSum, err := getChecksum(objAttrs.Checksum, algorithm)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
h, err := newHash(algorithm)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Printf("S3 object checksum: %s\n", objSum)
if objAttrs.ObjectParts == nil {
// Not a multi-part object:
_, err = io.Copy(h, f)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
sum := base64.StdEncoding.EncodeToString(h.Sum(nil))
fmt.Println()
fmt.Printf("Local file checksum: %s\n", sum)
fmt.Println()
if sum != objSum {
fmt.Println("Checksum MISMATCH! File and S3 object are NOT identical!")
os.Exit(1)
}
fmt.Println("Checksum matches! File and S3 object are identical.")
os.Exit(0)
}
// A multi-part object:
numParts := int(aws.ToInt32(objAttrs.ObjectParts.TotalPartsCount))
fmt.Printf("Object consists of %d part%s.\n", numParts, pluralize(numParts))
fmt.Println()
if numParts != len(objAttrs.ObjectParts.Parts) || aws.ToBool(objAttrs.ObjectParts.IsTruncated) {
fmt.Fprintln(os.Stderr, "This S3 object has more parts than were returned in the response. Please file an issue: https://github.com/stefansundin/s3verify")
os.Exit(1)
}
partLengthDigits := 1 + int64(math.Floor(math.Log10(float64(numParts))))
partFmtStr := fmt.Sprintf("Part %%%dd: %%s ", partLengthDigits)
var offset int64
var partNumber int32 = 1
for _, part := range objAttrs.ObjectParts.Parts {
if partNumber != aws.ToInt32(part.PartNumber) {
fmt.Fprintln(os.Stderr, "The parts of the S3 object are not sorted in the response. Please file an issue: https://github.com/stefansundin/s3verify")
os.Exit(1)
}
partSize := aws.ToInt64(part.Size)
partHash, err := newHash(algorithm)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
_, err = io.Copy(partHash, io.LimitReader(f, partSize))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
partSum := partHash.Sum(nil)
partSumEncoded := base64.StdEncoding.EncodeToString(partSum)
fmt.Printf(partFmtStr, partNumber, partSumEncoded)
partChecksum, err := getPartChecksum(&part, algorithm)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if partSumEncoded != partChecksum {
fmt.Println("FAILED")
fmt.Println()
fmt.Printf("Local file did not match part %d (bytes %d to %d).\n", partNumber, offset, offset+partSize)
os.Exit(1)
}
fmt.Println("OK")
h.Write([]byte(partSum))
offset += partSize
partNumber++
}
sum := base64.StdEncoding.EncodeToString(h.Sum(nil))
if len(sum) != len(objSum) {
// Directory buckets add the number of parts to the end of the checksum of checksums, separated with a dash
sum = fmt.Sprintf("%s-%d", sum, numParts)
}
fmt.Println()
fmt.Printf("Checksum of checksums: %s\n", sum)
fmt.Println()
if sum != objSum {
fmt.Println("Checksum MISMATCH! File and S3 object are NOT identical!")
os.Exit(1)
}
fmt.Println("Checksum matches! File and S3 object are identical.")
os.Exit(0)
}