2021-12-07 13:08:26 -05:00
|
|
|
// Copyright (c) 2015-2021 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 (
|
|
|
|
"context"
|
|
|
|
"crypto/md5"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/url"
|
2021-12-21 13:08:26 -05:00
|
|
|
"os"
|
2021-12-07 13:08:26 -05:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
endpoint, accessKey, secretKey string
|
|
|
|
bucket, prefix string
|
2021-12-21 13:08:26 -05:00
|
|
|
debug bool
|
|
|
|
versions bool
|
2022-05-26 09:02:05 -04:00
|
|
|
insecure bool
|
2021-12-07 13:08:26 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// getMD5Sum returns MD5 sum of given data.
|
|
|
|
func getMD5Sum(data []byte) []byte {
|
|
|
|
hash := md5.New()
|
|
|
|
hash.Write(data)
|
|
|
|
return hash.Sum(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.StringVar(&endpoint, "endpoint", "https://play.min.io", "S3 endpoint URL")
|
|
|
|
flag.StringVar(&accessKey, "access-key", "Q3AM3UQ867SPQQA43P2F", "S3 Access Key")
|
|
|
|
flag.StringVar(&secretKey, "secret-key", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG", "S3 Secret Key")
|
|
|
|
flag.StringVar(&bucket, "bucket", "", "Select a specific bucket")
|
|
|
|
flag.StringVar(&prefix, "prefix", "", "Select a prefix")
|
2021-12-21 13:08:26 -05:00
|
|
|
flag.BoolVar(&debug, "debug", false, "Prints HTTP network calls to S3 endpoint")
|
|
|
|
flag.BoolVar(&versions, "versions", false, "Verify all versions")
|
2022-05-26 09:02:05 -04:00
|
|
|
flag.BoolVar(&insecure, "insecure", false, "Disable TLS verification")
|
2021-12-07 13:08:26 -05:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if endpoint == "" {
|
|
|
|
log.Fatalln("Endpoint is not provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
if accessKey == "" {
|
|
|
|
log.Fatalln("Access key is not provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
if secretKey == "" {
|
|
|
|
log.Fatalln("Secret key is not provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
if bucket == "" && prefix != "" {
|
|
|
|
log.Fatalln("--prefix is specified without --bucket.")
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2022-05-26 09:02:05 -04:00
|
|
|
secure := strings.EqualFold(u.Scheme, "https")
|
|
|
|
transport, err := minio.DefaultTransport(secure)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
if insecure {
|
|
|
|
// skip TLS verification
|
|
|
|
transport.TLSClientConfig.InsecureSkipVerify = true
|
|
|
|
}
|
|
|
|
|
2021-12-07 13:08:26 -05:00
|
|
|
s3Client, err := minio.New(u.Host, &minio.Options{
|
2022-05-26 09:02:05 -04:00
|
|
|
Creds: credentials.NewStaticV4(accessKey, secretKey, ""),
|
|
|
|
Secure: secure,
|
|
|
|
Transport: transport,
|
2021-12-07 13:08:26 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-05-26 09:02:05 -04:00
|
|
|
log.Fatalln(err)
|
2021-12-07 13:08:26 -05:00
|
|
|
}
|
|
|
|
|
2021-12-21 13:08:26 -05:00
|
|
|
if debug {
|
|
|
|
s3Client.TraceOn(os.Stderr)
|
|
|
|
}
|
2021-12-07 13:08:26 -05:00
|
|
|
|
|
|
|
var buckets []string
|
|
|
|
if bucket != "" {
|
|
|
|
buckets = append(buckets, bucket)
|
|
|
|
} else {
|
|
|
|
bucketsInfo, err := s3Client.ListBuckets(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
for _, b := range bucketsInfo {
|
|
|
|
buckets = append(buckets, b.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, bucket := range buckets {
|
|
|
|
opts := minio.ListObjectsOptions{
|
|
|
|
Recursive: true,
|
|
|
|
Prefix: prefix,
|
2021-12-21 13:08:26 -05:00
|
|
|
WithVersions: versions,
|
|
|
|
WithMetadata: true,
|
2021-12-07 13:08:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// List all objects from a bucket-name with a matching prefix.
|
|
|
|
for object := range s3Client.ListObjects(context.Background(), bucket, opts) {
|
|
|
|
if object.Err != nil {
|
2022-05-30 23:58:10 -04:00
|
|
|
log.Println("LIST error:", object.Err)
|
2021-12-07 13:08:26 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if object.IsDeleteMarker {
|
2021-12-21 13:08:26 -05:00
|
|
|
log.Println("DELETE marker skipping object:", object.Key)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := object.UserMetadata["X-Amz-Server-Side-Encryption-Customer-Algorithm"]; ok {
|
|
|
|
log.Println("Objects encrypted with SSE-C do not have md5sum as ETag:", object.Key)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if v, ok := object.UserMetadata["X-Amz-Server-Side-Encryption"]; ok && v == "aws:kms" {
|
|
|
|
log.Println("Objects encrypted with SSE-KMS do not have md5sum as ETag:", object.Key)
|
2021-12-07 13:08:26 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
parts := 1
|
2021-12-15 15:07:15 -05:00
|
|
|
multipart := false
|
2021-12-07 13:08:26 -05:00
|
|
|
s := strings.Split(object.ETag, "-")
|
|
|
|
switch len(s) {
|
|
|
|
case 1:
|
|
|
|
// nothing to do
|
|
|
|
case 2:
|
|
|
|
if p, err := strconv.Atoi(s[1]); err == nil {
|
|
|
|
parts = p
|
|
|
|
} else {
|
2021-12-13 20:45:06 -05:00
|
|
|
log.Println("ETAG: wrong format:", err)
|
2021-12-07 13:08:26 -05:00
|
|
|
continue
|
|
|
|
}
|
2021-12-15 15:07:15 -05:00
|
|
|
multipart = true
|
2021-12-07 13:08:26 -05:00
|
|
|
default:
|
2021-12-13 20:45:06 -05:00
|
|
|
log.Println("Unexpected ETAG format", object.ETag)
|
|
|
|
continue
|
2021-12-07 13:08:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var partsMD5Sum [][]byte
|
2022-05-30 23:58:10 -04:00
|
|
|
var failedMD5 bool
|
2021-12-07 13:08:26 -05:00
|
|
|
for p := 1; p <= parts; p++ {
|
2021-12-21 13:08:26 -05:00
|
|
|
opts := minio.GetObjectOptions{
|
|
|
|
VersionID: object.VersionID,
|
|
|
|
PartNumber: p,
|
|
|
|
}
|
|
|
|
obj, err := s3Client.GetObject(context.Background(), bucket, object.Key, opts)
|
2021-12-07 13:08:26 -05:00
|
|
|
if err != nil {
|
2021-12-13 20:45:06 -05:00
|
|
|
log.Println("GET", bucket, object.Key, object.VersionID, "=>", err)
|
2022-05-30 23:58:10 -04:00
|
|
|
failedMD5 = true
|
|
|
|
break
|
2021-12-07 13:08:26 -05:00
|
|
|
}
|
|
|
|
h := md5.New()
|
|
|
|
if _, err := io.Copy(h, obj); err != nil {
|
2021-12-13 20:45:06 -05:00
|
|
|
log.Println("MD5 calculation error:", bucket, object.Key, object.VersionID, "=>", err)
|
2022-05-30 23:58:10 -04:00
|
|
|
failedMD5 = true
|
|
|
|
break
|
2021-12-07 13:08:26 -05:00
|
|
|
}
|
|
|
|
partsMD5Sum = append(partsMD5Sum, h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
2022-05-30 23:58:10 -04:00
|
|
|
if failedMD5 {
|
|
|
|
log.Println("CORRUPTED object:", bucket, object.Key, object.VersionID)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-12-07 13:08:26 -05:00
|
|
|
corrupted := false
|
2021-12-15 15:07:15 -05:00
|
|
|
if !multipart {
|
2021-12-07 13:08:26 -05:00
|
|
|
md5sum := fmt.Sprintf("%x", partsMD5Sum[0])
|
|
|
|
if md5sum != object.ETag {
|
|
|
|
corrupted = true
|
|
|
|
}
|
2021-12-15 15:07:15 -05:00
|
|
|
} else {
|
2021-12-07 13:08:26 -05:00
|
|
|
var totalMD5SumBytes []byte
|
|
|
|
for _, sum := range partsMD5Sum {
|
|
|
|
totalMD5SumBytes = append(totalMD5SumBytes, sum...)
|
|
|
|
}
|
|
|
|
s3MD5 := fmt.Sprintf("%x-%d", getMD5Sum(totalMD5SumBytes), parts)
|
|
|
|
if s3MD5 != object.ETag {
|
|
|
|
corrupted = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if corrupted {
|
2021-12-13 20:45:06 -05:00
|
|
|
log.Println("CORRUPTED object:", bucket, object.Key, object.VersionID)
|
|
|
|
} else {
|
2021-12-21 13:08:26 -05:00
|
|
|
log.Println("INTACT object:", bucket, object.Key, object.VersionID)
|
2021-12-07 13:08:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|