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

Allow clients to specify the request date on signing requests #5239

Closed
wants to merge 1 commit into from

Conversation

xeviknal-cf
Copy link

The main intent of this PR is to make the signed urls api flexible with regards to the time when it's created.

GOAL: implement what it's called Cacheable signed URLs. Using v4 presign method, the current Date is necessary. Meaning that we can't generate constant signed urls according to our needs.

example: we want to avoid certain customers to download again and again the same set of images. And we don't want to make those images publicly available. If those images have a Control-Cache: private, max-age=3600 policy, it won't be used since each time we sign URLs those will be completely different. Therefore the browser won't be able to re-use them.

original idea: https://advancedweb.hu/cacheable-s3-signed-urls/

@lucix-aws
Copy link
Contributor

We recently announced the upcoming end-of-life for SDK v1 and are no longer accepting PRs for additive features. I had meant to update the PR template stating as such, I'll do that now.

What you're trying to do here is innately possible in SDK v2 through wrapping the HTTPPresignerV4 interface:

package main

import (
	"context"
	"fmt"
	"net/http"
	"time"

	"github.com/aws/aws-sdk-go-v2/aws"
	v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/service/s3"
)

type timedPresigner struct {
	signingTime time.Time
	signer      s3.HTTPPresignerV4
}

func (t *timedPresigner) PresignHTTP(
	ctx context.Context, creds aws.Credentials, r *http.Request, payloadHash, service, region string, _ time.Time, opts ...func(*v4.SignerOptions),
) (string, http.Header, error) {
	return t.signer.PresignHTTP(ctx, creds, r, payloadHash, service, region, t.signingTime, opts...)
}

func main() {
	cfg, err := config.LoadDefaultConfig(context.Background())
	if err != nil {
		panic(err)
	}

	svc := s3.NewFromConfig(cfg)
	psvc := s3.NewPresignClient(svc, func(o *s3.PresignOptions) {
		o.Presigner = &timedPresigner{
			signingTime: time.UnixMilli(1000),
			signer:      v4.NewSigner(),
		}
	})

	req, err := psvc.PresignGetObject(context.Background(), &s3.GetObjectInput{
		Bucket: aws.String("bucket"),
		Key:    aws.String("key"),
	})
	if err != nil {
		panic(err)
	}

	fmt.Println(req.URL)
}

@lucix-aws lucix-aws closed this Apr 29, 2024
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.

None yet

2 participants