Skip to content

S3: Add ETag support - fixes #523 - #1863

Closed
chris-redekop wants to merge 1 commit into
rclone:masterfrom
chris-redekop:523/large-file-hashes
Closed

chris-redekop wants to merge 1 commit into
rclone:masterfrom
chris-redekop:523/large-file-hashes

Conversation

@chris-redekop

@chris-redekop chris-redekop commented Nov 26, 2017 •

Copy link
Copy Markdown
Contributor

Thanks for a great utility! I have been happily using rclone for a few months now. I noticed a missing use case, which this PR addresses: as a backup operator, I want to (crypt) check the large files that rclone copies to S3. (rclone only recognizes S3's ETag as an MD5 sum for small files, and so does not check the hashes of large files.)

This PR adds full ETag support to rclone's local and S3 file systems, allowing rclone to (crypt) check large files on S3. It also removes MD5 support from the S3 file system to avoid cases where S3's MD5 algorithm might be chosen even though ETag would be the better choice.

The core algorithm is in the new s3etag.go file.

This PR also updates all of the hash-construction calls, to include file-size info. This info is required by the new S3 ETag, since the algorithm is dependent on the partition count, and ultimately on the file size.
The partition-info is not saved as metadata with the remote file: the particular partition scheme is assumed by rclone. (This means that rclone can (crypt) check the result of an rclone sync, but cannot (crypt) check a bucket that has been recopied to S3 by some tool other than rclone. The recopy will use a different partitioning scheme, resulting in different ETags.)

@chris-redekop

chris-redekop commented Nov 26, 2017 •

Copy link
Copy Markdown
Contributor Author

I notice that fstests#TestFsPutStream asserts that the hash value for a stream of unknown size is the same as the hash value for the same stream when the size is known.

Notice that, although TestFsPutStream continues to pass, its assertion is no longer always true: the new S3 ETag might use different partition sizes, which might result in different hash values, for the same stream when its size is known vs when its size is unknown.

I'm not sure--is maintaining this assertion important?

@ncw ncw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some thoughts for us to think about attached!

return false, true
}
if cryptHash != underlyingHash {
err = errors.Errorf("hashes differ (%s:%s) %q vs (%s:%s) %q", fdst.Name(), fdst.Root(), cryptHash, fsrc.Name(), fsrc.Root(), underlyingHash)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering why you swapped these over?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that these were criss-crossed during debugging: src goes with cryptHash on the left, and dst goes with underlyingHash on the right. I think? 😄

Comment thread fs/hash_test.go
fs.HashMD5: "bf13fc19e5151ac57d4252e0e0f87abe",
fs.HashSHA1: "3ab6543c08a75f292a5ecedac87ec41642d12166",
fs.HashDropbox: "214d2fcf3566e94c99ad2f59bd993daca46d8521a0c447adf4b324f53fddc0c7",
fs.HashS3ETag: "bf13fc19e5151ac57d4252e0e0f87abe0001",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be the same as HashMD5 with only one part?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might not understand your question: I think this is the same as HashMD5, with only one part! The first 32 characters are the same as HashMD5, and the last 4 characters specify that there is one part. I've implemented ETags to always be 36 characters, with the partition count always specified. (I noticed the size constants in hash.go, which led me to believe that the hash value should be fixed-length.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I was thinking is that the s3etag hash should be just the etag as returned from s3, so sometimes will have 32 characters and sometimes 36.

When it has only 32 characters it is an MD5SUM.

Comment thread s3/s3.go

// Hashes returns the supported hash sets.
func (f *Fs) Hashes() fs.HashSet {
return fs.HashSet(fs.HashMD5)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to keep the s3 module supporting MD5 as well. It is possible to tell the ETags apart so it can just return blank for non MD5 valid hashes.

So I'd suggest that the s3etag hash is either a valid MD5 hash, or an s3etag with a part number > 1.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I thought about getting the best of both ETag and MD5. The issue is that, for each file, either MD5 or ETag will be optimal, but we commit to a hash algorithm at the filesystem level. It's even a bit worse because an algorithm is chosen for a filesystem without regard to any criteria. So, I wanted to support the one hash algorithm that is the best in most cases. Based on the chart below, I chose ETag over MD5. What do you think?

Scenario MD5 ETag
Small file, rcloned to S3 X X
Large file, rcloned to S3 X
Small file, 3rd-party put to S3 X X
Large file, 3rd-party put to S3
KMS/client encrypted file
Small file, rclone checked across remotes X
Large file, rclone checked across remotes

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an alternative here, the hash handshaking could be enhanced to support file-by-file negotiation; parameterized algorithms, discovering algorithm parameters during the handshake, etc...

I'd be interested in contributing to enhanced hash handshaking; let me know if you'd like to chat!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is one thing that worries me about your proposal is that this would make rclone incompatible with other methods of transferring data. So say someone has a workflow of using cyberduck to upload stuff and rclone to retrieve it remotely, then that might break with corrupted hash errors. rclone is in use by 1,000s of people so I don't want to take that risk.

So I think your proposal as it stands needs to be behind an --s3-etag flag - if set it would use s3etags, if not then md5sums and blank for the multipart uploads.

However I think we could do better than the PartSize function... given an etag with hash+partitions and the files length, and given that all the parts are the same size (except for the last), then it is possible to uniquely determine the correct partition size to use (I managed to convince myself of this). So we should be able to check any etag which was uploaded with constant size parts. I'm unsure how we'd fit this into rclone though, so maybe what you've done behind a flag is the best of both worlds.

I'd be interested in contributing to enhanced hash handshaking; let me know if you'd like to chat!

Your ideas would be appreciated!

Comment thread s3/s3etag/s3etag.go
@@ -0,0 +1,141 @@
// Package s3etag implements the AWS ETag hash as described in
//
// http://permalink.gmane.org/gmane.comp.file-systems.s3.s3tools/583

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a slight concern that this isn't officially documented by Amazon. All AWS says about it is

If an object is created by either the Multipart Upload or Part Copy operation, the ETag is not an MD5 digest, regardless of the method of encryption.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, AWS is pretty quiet on the ETag. I share your concern. Still, the ETag as implemented works and addresses a missing use case: upload large files to S3 and (crypt) check them.

Comment thread s3/s3etag/s3etag.go

// New returns a new hash.Hash computing the S3ETag checksum.
func New(size int64) hash.Hash {
partSize := s3sizes.PartSize(size)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to check files which were uploaded by rclone just fine, but what about other programs which use a different partition size algorithm. The really needs the number of partitions passed in here so it can be read out of the etag...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, this is not a panacea, it does not support large files uploaded by a 3rd-party. Note that this is not worse than S3's MD5 today, which does not support any large files.

In terms of fully defining the ETag algo, I don't think a single filesize parameter OR the single partition-count parameter is enough. (For partition count, imagine ETagging a file twice, with slightly different partition sizes, so that the partition count is the same but the ETags are otherwise different.) To fully parameterize the ETag algorithm, we need an ordered list of partition sizes (or equivalent).

Once the algorithm is fully specified, (crypt) check would then need to fetch and use the algorithm parameters while calculating the local ETag. Passing info like this, during a check, is not currently supported by (crypt) check. (See my comment above about enhancing the hash handshaking.)

Instead, I address only the use-case you mention: upload and check with rclone. By addressing the restricted use-case, I can treat the parameters as hardcoded into rclone's PartSize method (so that they don't need to be hung on a file as metadata, nor passed around during a (crypt) check).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, this is not a panacea, it does not support large files uploaded by a 3rd-party. Note that this is not worse than S3's MD5 today, which does not support any large files.

However it will error when trying to download files uploaded by a 3rd party program :-(

In terms of fully defining the ETag algo, I don't think a single filesize parameter OR the single partition-count parameter is enough

Yes I think it need either (assume all partitions the same size, length of file, number of partitions) or (list of all partitions and size)

@chris-redekop

chris-redekop commented Nov 29, 2017 •

Copy link
Copy Markdown
Contributor Author

Thanks for the new comments and the discussion over on #523 . I'm going to think about the comments here while we continue on #523.

@chris-redekop

Copy link
Copy Markdown
Contributor Author

Dropping this PR in favour of MD5 sums, as discussed over on #523.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants