Skip to content
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

Presignedurl requestpayer change #2768

Merged
merged 4 commits into from
Aug 30, 2024

Conversation

bhavya2109sharma
Copy link
Contributor

@bhavya2109sharma bhavya2109sharma commented Aug 30, 2024

The AWS Go SDK V2 does not currently sign the x-amz-request-payer header when generating presigned URLs. This requires clients to manually add the header, which can lead to SignatureDoesNotMatch errors.

This PR updates the signer implementation to include the x-amz-request-payer header in the signed URL, allowing clients to use the presigned URL without needing to add the header.

Fixes : #2764

Testing:

PresignedURL before the change

https://demo-bucket.s3.us-east-1.amazonaws.com/key?
X-Amz-Credential=REDACTED/20240830/us-east-1/s3/aws4_request&
X-Amz-Date=20240830T170032Z&
X-Amz-Expires=900&
X-Amz-SignedHeaders=host;x-amz-request-payer&
x-id=GetObject&
X-Amz-Signature=REDACTED&
X-Amz-Algorithm=AWS4-HMAC-SHA256 

PresignedURL after the change

https://demo-bucket.s3.us-east-1.amazonaws.com/key?
X-Amz-Date=20240830T165708Z&
X-Amz-Expires=900&
X-Amz-Request-Payer=requester&
X-Amz-SignedHeaders=host&
x-id=GetObject&
X-Amz-Signature=REDACTED&
X-Amz-Algorithm=AWS4-HMAC-SHA256&
X-Amz-Credential=REDACTED/20240830/us-east-1/s3/aws4_request 
package main 

import (
	"context"
	"fmt"
	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/service/s3"
	"github.com/aws/aws-sdk-go-v2/service/s3/types"
	"net/http"
	"net/url"
	"regexp"
	"strings"
)

func main() {

	cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("us-east-1"),
		config.WithClientLogMode(aws.LogRequestWithBody|aws.LogResponseWithBody))

	if err != nil {
		panic(err)
	}
	client := s3.NewFromConfig(cfg)
	presigner := s3.NewPresignClient(client)

	presignedreq, err := presigner.PresignGetObject(context.TODO(), &s3.GetObjectInput{
		Bucket:       aws.String("demo-bucket"),
		Key:          aws.String("key"),
		RequestPayer: types.RequestPayerRequester,
	})

	if err != nil {
		panic(err)
	}
	req, err := http.NewRequest("GET", presignedreq.URL, nil)
	if err != nil {
		panic(err)
	}
	clientHTTP := &http.Client{}
	resp, err := clientHTTP.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	fmt.Println("HTTP Status:", resp.Status)
        //HTTP Status: 200 OK
}

@bhavya2109sharma bhavya2109sharma requested a review from a team as a code owner August 30, 2024 16:51
Copy link
Contributor

@wty-Bryant wty-Bryant left a comment

Choose a reason for hiding this comment

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

need a changelog

@RanVaknin RanVaknin merged commit 4ed838e into aws:main Aug 30, 2024
12 checks passed
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.

Presign url's require side loading request-payer header
4 participants