Home > Net >  In Bash, I am trying to upload a file to AWS S3. Why is AWS generating a different canonical request
In Bash, I am trying to upload a file to AWS S3. Why is AWS generating a different canonical request

Time:11-04

Use Case: I am currently trying to create a bash script that will be able to upload a file to an AWS S3 bucket. This will be used across a few hundred systems that can not have the AWS CLI, or s3cmd or similar, installed, unfortunately. So I have to resort to bash. I've already generated a zsh script that works as expected for my Mac systems, but now to create on linux.

Work so far: My code works up until the point that I need to hash the canonical request:

canonical_request_hash=$(echo -n $canonical_request | openssl dgst -sha256)

When I run my code AWS returns the standard

The request signature we calculated does not match the signature you provided. Check your key and signing method.

error.

After looking through the error response, my CanonicalRequest is the same that AWS would generate, but the StringToSign has a different canonical_request_hash.

Here is my code snippet that I generate a different hash than AWS:

# Initial Canoncial Request String
canonical_request="PUT
/$file_name

host:$aws_s3_host.s3.amazonaws.com
x-amz-content-sha256:$file_sha256
x-amz-date:$now_time

host;x-amz-content-sha256;x-amz-date
$file_sha256"

# THE CANONICAL REQUEST ABOVE IS WHAT AWS PRODUCES.
###
# Generate Canonical Request Hash
canonical_request_hash=$(echo -n $canonical_request | openssl dgst -sha256)
###
# THE CANONICAL REQUEST HASH ABOVE IS DIFFERENT THAN WHAT AWS PRODUCES.


# String that will be used to generate AWS Signature
string_to_sign="AWS4-HMAC-SHA256
$now_time
$now_date/$region/s3/aws4_request
$canonical_request_hash"

I have attempted different hash commands:

sha256sum openssl dgst -sha256

and attempted echo w/ and w/o the -n option.

I tried setting the canonical_request to a file and then hashing the file. Why? No clue, just trying everything I can think of. lol

Any help would be greatly appreciated.

CodePudding user response:

When you leave the $canonical_request variable unquoted, the whitespace changes. Use

printf '%s' "$canonical_request" | openssl ...

Further reading:

  • Related