Skip to content

Append x-amz-checksum headers when using hashing algorithm other than MD5 #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 19, 2024

Conversation

d3caf
Copy link

@d3caf d3caf commented Jan 23, 2024

While it was possible to specify an alternate hashing algorithm to ex_aws_s3, the header wasn't correctly appended to the request and was instead appended as Content-<hash algo>. AWS looks to require either Content-MD5 or x-amz-checksum-<hash algo> so this implements that behavior.

I also added some unit tests around this. Open to suggestions as to how to improve them since I was a little unsure about modifying the application env during a test.

defp get_hash_config() do
Application.get_env(:ex_aws_s3, :content_hash_algorithm) || :md5
end

# Supported hash algorithms:
# https://www.erlang.org/doc/man/crypto.html#type-hash_algorithm
@spec hash_header(atom()) :: binary()
defp hash_header(alg) when is_atom(alg), do: "content-#{to_string(alg)}"
defp hash_header(:md5), do: "content-md5"
defp hash_header(alg) when is_atom(alg), do: "x-amz-checksum-#{to_string(alg)}"

Choose a reason for hiding this comment

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

Nice, I noticed the "spec" seems to be using x-amz-checksum, seems like this will be a good addition

defp hash_header(alg) when is_atom(alg), do: "content-#{to_string(alg)}"
@spec hash_header(hash_algorithm()) :: binary()
defp hash_header(:md5), do: "content-md5"
defp hash_header(:sha), do: "x-amz-checksum-sha1"
Copy link
Author

Choose a reason for hiding this comment

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

Noticed that erlang uses sha but S3 expects sha1 so mapping that here.

@d3caf
Copy link
Author

d3caf commented Jan 30, 2024

Hey there @bernardd, would you be able to give this a look? Thanks :)

@d3caf
Copy link
Author

d3caf commented Mar 12, 2024

Hi @bernardd, just wanted to check in on this! Let me know if there is anything you need on my end. Thanks!

@bernardd
Copy link
Contributor

Hi @d3caf - sorry about taking so long to get to this. All looks good - thanks especially for the docs and tests :)

@bernardd bernardd merged commit bd5feea into ex-aws:main Mar 19, 2024
@d3caf
Copy link
Author

d3caf commented Mar 28, 2024

@bernardd sorry to bug you, but do you have an idea of when a new release will be made including this? Trying to get an idea of when I can transition off of my forked version. Happy to help prepare a release as well, if you'd like!

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.

3 participants