Handle cloudFront bucket prefix issue
Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn)
This commit is contained in:
parent
0a2affa79f
commit
0eb599329a
2 changed files with 17 additions and 12 deletions
|
@ -8,10 +8,10 @@ import (
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/url"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/AdRoll/goamz/cloudfront"
|
"github.com/AdRoll/goamz/cloudfront"
|
||||||
|
"github.com/docker/distribution/context"
|
||||||
storagedriver "github.com/docker/distribution/registry/storage/driver"
|
storagedriver "github.com/docker/distribution/registry/storage/driver"
|
||||||
storagemiddleware "github.com/docker/distribution/registry/storage/driver/middleware"
|
storagemiddleware "github.com/docker/distribution/registry/storage/driver/middleware"
|
||||||
)
|
)
|
||||||
|
@ -90,23 +90,23 @@ func newCloudFrontStorageMiddleware(storageDriver storagedriver.StorageDriver, o
|
||||||
return &cloudFrontStorageMiddleware{StorageDriver: storageDriver, cloudfront: cf, duration: duration}, nil
|
return &cloudFrontStorageMiddleware{StorageDriver: storageDriver, cloudfront: cf, duration: duration}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// S3BucketKeyer is any type that is capable of returning the S3 bucket key
|
||||||
|
// which should be cached by AWS CloudFront.
|
||||||
|
type S3BucketKeyer interface {
|
||||||
|
S3BucketKey(path string) string
|
||||||
|
}
|
||||||
|
|
||||||
// Resolve returns an http.Handler which can serve the contents of the given
|
// Resolve returns an http.Handler which can serve the contents of the given
|
||||||
// Layer, or an error if not supported by the storagedriver.
|
// Layer, or an error if not supported by the storagedriver.
|
||||||
func (lh *cloudFrontStorageMiddleware) URLFor(path string, options map[string]interface{}) (string, error) {
|
func (lh *cloudFrontStorageMiddleware) URLFor(path string, options map[string]interface{}) (string, error) {
|
||||||
// TODO(endophage): currently only supports S3
|
// TODO(endophage): currently only supports S3
|
||||||
options["expiry"] = time.Now().Add(lh.duration)
|
keyer, ok := lh.StorageDriver.(S3BucketKeyer)
|
||||||
|
if !ok {
|
||||||
layerURLStr, err := lh.StorageDriver.URLFor(path, options)
|
context.GetLogger(context.Background()).Warn("the CloudFront middleware does not support this backend storage driver")
|
||||||
if err != nil {
|
return lh.StorageDriver.URLFor(path, options)
|
||||||
return "", err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
layerURL, err := url.Parse(layerURLStr)
|
cfURL, err := lh.cloudfront.CannedSignedURL(keyer.S3BucketKey(path), "", time.Now().Add(lh.duration))
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
cfURL, err := lh.cloudfront.CannedSignedURL(layerURL.Path, "", time.Now().Add(lh.duration))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
|
@ -695,6 +695,11 @@ func (d *driver) s3Path(path string) string {
|
||||||
return strings.TrimLeft(strings.TrimRight(d.RootDirectory, "/")+path, "/")
|
return strings.TrimLeft(strings.TrimRight(d.RootDirectory, "/")+path, "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// S3BucketKey returns the s3 bucket key for the given storage driver path.
|
||||||
|
func (d *Driver) S3BucketKey(path string) string {
|
||||||
|
return d.StorageDriver.(*driver).s3Path(path)
|
||||||
|
}
|
||||||
|
|
||||||
func parseError(path string, err error) error {
|
func parseError(path string, err error) error {
|
||||||
if s3Err, ok := err.(*s3.Error); ok && s3Err.Code == "NoSuchKey" {
|
if s3Err, ok := err.(*s3.Error); ok && s3Err.Code == "NoSuchKey" {
|
||||||
return storagedriver.PathNotFoundError{Path: path}
|
return storagedriver.PathNotFoundError{Path: path}
|
||||||
|
|
Loading…
Reference in a new issue