2015-03-03 16:57:52 +00:00
|
|
|
// Package middleware - cloudfront wrapper for storage libs
|
|
|
|
// N.B. currently only works with S3, not arbitrary sites
|
|
|
|
//
|
|
|
|
package middleware
|
2015-02-11 01:41:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/AdRoll/goamz/cloudfront"
|
2015-02-11 02:14:23 +00:00
|
|
|
storagedriver "github.com/docker/distribution/registry/storage/driver"
|
2015-03-03 16:57:52 +00:00
|
|
|
storagemiddleware "github.com/docker/distribution/registry/storage/driver/middleware"
|
2015-02-11 01:41:09 +00:00
|
|
|
)
|
|
|
|
|
2015-03-03 16:57:52 +00:00
|
|
|
// cloudFrontStorageMiddleware provides an simple implementation of layerHandler that
|
2015-02-11 01:41:09 +00:00
|
|
|
// constructs temporary signed CloudFront URLs from the storagedriver layer URL,
|
|
|
|
// then issues HTTP Temporary Redirects to this CloudFront content URL.
|
2015-03-03 16:57:52 +00:00
|
|
|
type cloudFrontStorageMiddleware struct {
|
|
|
|
storagedriver.StorageDriver
|
|
|
|
cloudfront *cloudfront.CloudFront
|
|
|
|
duration time.Duration
|
2015-02-11 01:41:09 +00:00
|
|
|
}
|
|
|
|
|
2015-03-03 16:57:52 +00:00
|
|
|
var _ storagedriver.StorageDriver = &cloudFrontStorageMiddleware{}
|
2015-02-11 01:41:09 +00:00
|
|
|
|
|
|
|
// newCloudFrontLayerHandler constructs and returns a new CloudFront
|
|
|
|
// LayerHandler implementation.
|
|
|
|
// Required options: baseurl, privatekey, keypairid
|
2015-03-03 16:57:52 +00:00
|
|
|
func newCloudFrontStorageMiddleware(storageDriver storagedriver.StorageDriver, options map[string]interface{}) (storagedriver.StorageDriver, error) {
|
2015-02-11 01:41:09 +00:00
|
|
|
base, ok := options["baseurl"]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("No baseurl provided")
|
|
|
|
}
|
|
|
|
baseURL, ok := base.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("baseurl must be a string")
|
|
|
|
}
|
|
|
|
pk, ok := options["privatekey"]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("No privatekey provided")
|
|
|
|
}
|
|
|
|
pkPath, ok := pk.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("privatekey must be a string")
|
|
|
|
}
|
|
|
|
kpid, ok := options["keypairid"]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("No keypairid provided")
|
|
|
|
}
|
|
|
|
keypairID, ok := kpid.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("keypairid must be a string")
|
|
|
|
}
|
|
|
|
|
|
|
|
pkBytes, err := ioutil.ReadFile(pkPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to read privatekey file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
block, _ := pem.Decode([]byte(pkBytes))
|
|
|
|
if block == nil {
|
|
|
|
return nil, fmt.Errorf("Failed to decode private key as an rsa private key")
|
|
|
|
}
|
|
|
|
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cf := cloudfront.New(baseURL, privateKey, keypairID)
|
|
|
|
|
|
|
|
duration := 20 * time.Minute
|
|
|
|
d, ok := options["duration"]
|
|
|
|
if ok {
|
|
|
|
switch d := d.(type) {
|
|
|
|
case time.Duration:
|
|
|
|
duration = d
|
|
|
|
case string:
|
|
|
|
dur, err := time.ParseDuration(d)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Invalid duration: %s", err)
|
|
|
|
}
|
|
|
|
duration = dur
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-03 16:57:52 +00:00
|
|
|
return &cloudFrontStorageMiddleware{StorageDriver: storageDriver, cloudfront: cf, duration: duration}, nil
|
2015-02-11 01:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve returns an http.Handler which can serve the contents of the given
|
|
|
|
// Layer, or an error if not supported by the storagedriver.
|
2015-03-03 16:57:52 +00:00
|
|
|
func (lh *cloudFrontStorageMiddleware) URLFor(path string, options map[string]interface{}) (string, error) {
|
|
|
|
// TODO(endophage): currently only supports S3
|
|
|
|
options["expiry"] = time.Now().Add(lh.duration)
|
|
|
|
|
|
|
|
layerURLStr, err := lh.StorageDriver.URLFor(path, options)
|
2015-02-11 01:41:09 +00:00
|
|
|
if err != nil {
|
2015-03-03 16:57:52 +00:00
|
|
|
return "", err
|
2015-02-11 01:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
layerURL, err := url.Parse(layerURLStr)
|
|
|
|
if err != nil {
|
2015-03-03 16:57:52 +00:00
|
|
|
return "", err
|
2015-02-11 01:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cfURL, err := lh.cloudfront.CannedSignedURL(layerURL.Path, "", time.Now().Add(lh.duration))
|
|
|
|
if err != nil {
|
2015-03-03 16:57:52 +00:00
|
|
|
return "", err
|
2015-02-11 01:41:09 +00:00
|
|
|
}
|
2015-03-03 16:57:52 +00:00
|
|
|
return cfURL, nil
|
2015-02-11 01:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// init registers the cloudfront layerHandler backend.
|
|
|
|
func init() {
|
2015-03-06 15:45:16 +00:00
|
|
|
storagemiddleware.Register("cloudfront", storagemiddleware.InitFunc(newCloudFrontStorageMiddleware))
|
2015-02-11 01:41:09 +00:00
|
|
|
}
|