Skip to content

Commit

Permalink
[fix]: prevent extension of S3 presigned url TTL
Browse files Browse the repository at this point in the history
  • Loading branch information
theSuess committed Dec 4, 2022
1 parent 847e7c7 commit b9fd073
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"path"
"time"

"codeberg.org/gruf/go-cache/v3"
"codeberg.org/gruf/go-cache/v3/ttl"
"codeberg.org/gruf/go-store/v2/kv"
"codeberg.org/gruf/go-store/v2/storage"
"github.com/minio/minio-go/v7"
Expand All @@ -52,7 +52,7 @@ type Driver struct {
// S3-only parameters
Proxy bool
Bucket string
PresignedCache cache.Cache[string, *url.URL]
PresignedCache *ttl.Cache[string, *url.URL]
}

// URL will return a presigned GET object URL, but only if running on S3 storage with proxying disabled.
Expand All @@ -63,8 +63,9 @@ func (d *Driver) URL(ctx context.Context, key string) *url.URL {
return nil
}

if u, ok := d.PresignedCache.Get(key); ok {
return u
// access the cache member directly to avoid extending the TTL
if u, ok := d.PresignedCache.Cache.Get(key); ok {
return u.Value
}

u, err := s3.Client().PresignedGetObject(ctx, d.Bucket, key, urlCacheTTL, url.Values{
Expand Down Expand Up @@ -139,7 +140,7 @@ func NewS3Storage() (*Driver, error) {
}

// ttl should be lower than the expiry used by S3 to avoid serving invalid URLs
presignedCache := cache.New[string, *url.URL](0, 1000, urlCacheTTL-urlCacheExpiryFrequency)
presignedCache := ttl.New[string, *url.URL](0, 1000, urlCacheTTL-urlCacheExpiryFrequency)
presignedCache.Start(urlCacheExpiryFrequency)

return &Driver{
Expand Down

0 comments on commit b9fd073

Please sign in to comment.