Search code examples
amazon-web-servicesgoamazon-s3aws-sdkaws-sdk-go

Signature Does Not Match 403 error when signing a URL via aws-sdk-go


I followed instructions on this issue https://github.com/aws/aws-sdk-go/issues/467 which clearly documented how to create a pre-signed url for a PUT request. The goal is to presign the url, so I can directly upload images from the browser safely

key and secret are of course my current credentials that work with direct PutObject requests via the SDK

creds := credentials.NewStaticCredentials("key", "secret", "")

cfg := aws.NewConfig().WithRegion("us-west-2").WithCredentials(creds)
srv := s3.New(session.New(), cfg)

params := &s3.PutObjectInput{
    Bucket: aws.String("my-bucket"),
    Key:    aws.String("/local/test/filename"), 
}
req, _ := srv.PutObjectRequest(params)
url, err := req.Presign(15 * time.Hour)
if err != nil {
    fmt.Println("error signing request", err)
}

fmt.Println("URL", url)

```

I then take that URL and make a curl request. I get this response

<?xml version="1.0" encoding="UTF-8"?>
<Error>
    <Code>SignatureDoesNotMatch</Code>
    <Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>
    <AWSAccessKeyId>redacted</AWSAccessKeyId>
    <StringToSign>redacted</StringToSign>
    <SignatureProvided>redacted</SignatureProvided>
    <StringToSignBytes>redacted</StringToSignBytes>
    <CanonicalRequest>PUT
/local/test/filename
X-Amz-Algorithm=AWS4-HMAC-SHA256&amp;X-Amz-Credential=redacted%2Fus-west-2%2Fs3%2F%20aws4_request&amp;X-Amz-Date=20161129T012909Z&amp;X-Amz-Expires=54000&amp;X-Amz-SignedHeaders=host
host:redacted.s3-us-west-2.amazonaws.com

host
UNSIGNED-PAYLOAD</CanonicalRequest>
    <CanonicalRequestBytes>redacted</CanonicalRequestBytes>
    <RequestId>redacted</RequestId>
    <HostId>redacted</HostId>
</Error>

any ideas why the presigned URL is providing me a signature that supposedly does not match? Again these same credentials are currently working for direct PutObject commands on my server


Solution

  • my Bucket policy was not properly configured. I had to cross reference my key/secret with the IAM policy and make sure they were listed in the "principals" section of the policy

    https://aws.amazon.com/blogs/security/iam-policies-and-bucket-policies-and-acls-oh-my-controlling-access-to-s3-resources/