Skip to content

When region is nil in signing_key/3 resulting error isn't very clear #1180

@tudborg

Description

@tudborg

In signing_key/3, if :region isn't set (or is set to nil) in config:

defp signing_key(service, datetime, config) do
["AWS4", config[:secret_access_key]]
|> hmac_sha256(date(datetime))
|> hmac_sha256(config[:region])
|> hmac_sha256(service)
|> hmac_sha256("aws4_request")
end

the resulting error is something like

** (ErlangError) Erlang error: {:badarg, {~c"mac.c", 193}, ~c"Bad text"}
    (crypto 5.7) crypto.erl:1218: :crypto.mac(:hmac, :sha256, <<50, 232, ...>>, nil)
    (ex_aws 2.6.0) lib/ex_aws/auth/signatures.ex:15: ExAws.Auth.Signatures.signing_key/3
    (ex_aws 2.6.0) lib/ex_aws/auth/signatures.ex:7: ExAws.Auth.Signatures.generate_signature_v4/4
    (ex_aws 2.6.0) lib/ex_aws/auth.ex:136: ExAws.Auth.auth_header/7
    (ex_aws 2.6.0) lib/ex_aws/auth.ex:55: ExAws.Auth.headers/6
    (ex_aws 2.6.0) lib/ex_aws/request.ex:27: ExAws.Request.request_and_retry/7
    (ex_aws 2.6.0) lib/ex_aws/operation/s3.ex:40: ExAws.Operation.ExAws.Operation.S3.perform/2
    (ex_aws_s3 2.5.8) lib/ex_aws/s3/upload.ex:58: ExAws.S3.Upload.initialize/2

which isn't super helpful.

The underlying problem is that the data argument passed to crypto.mac/4 is nil (from config[:region])
In my case, I had region: System.get_env("AWS_REGION") which wasn't set in the environment.
Which I should obviously just make sure is set.

Would you accept a patch that throws a better error?

Maybe even just

    |> hmac_sha256(Map.fetch!(config, :region))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions