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-01-09 00:55:40 +00:00
|
|
|
|
|
|
|
import (
|
2017-08-11 22:31:16 +00:00
|
|
|
"context"
|
2015-01-09 00:55:40 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
2023-10-24 19:49:47 +00:00
|
|
|
"net/http"
|
2016-01-22 02:17:53 +00:00
|
|
|
"net/url"
|
2022-11-02 21:55:22 +00:00
|
|
|
"os"
|
2016-01-22 02:17:53 +00:00
|
|
|
"strings"
|
2015-01-09 00:55:40 +00:00
|
|
|
"time"
|
|
|
|
|
2016-01-22 02:17:53 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/cloudfront/sign"
|
2023-10-24 17:16:58 +00:00
|
|
|
"github.com/distribution/distribution/v3/internal/dcontext"
|
2020-08-24 11:18:39 +00:00
|
|
|
storagedriver "github.com/distribution/distribution/v3/registry/storage/driver"
|
|
|
|
storagemiddleware "github.com/distribution/distribution/v3/registry/storage/driver/middleware"
|
2015-01-09 00:55:40 +00:00
|
|
|
)
|
|
|
|
|
2016-06-02 05:31:13 +00:00
|
|
|
// cloudFrontStorageMiddleware provides a simple implementation of layerHandler that
|
2015-01-09 00:55:40 +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
|
2017-11-15 01:21:36 +00:00
|
|
|
awsIPs *awsIPs
|
2016-01-22 02:17:53 +00:00
|
|
|
urlSigner *sign.URLSigner
|
|
|
|
baseURL string
|
|
|
|
duration time.Duration
|
2015-01-09 00:55:40 +00:00
|
|
|
}
|
|
|
|
|
2015-03-03 16:57:52 +00:00
|
|
|
var _ storagedriver.StorageDriver = &cloudFrontStorageMiddleware{}
|
2015-01-09 00:55:40 +00:00
|
|
|
|
|
|
|
// newCloudFrontLayerHandler constructs and returns a new CloudFront
|
|
|
|
// LayerHandler implementation.
|
2022-11-02 21:05:45 +00:00
|
|
|
//
|
|
|
|
// Required options:
|
|
|
|
//
|
|
|
|
// - baseurl
|
|
|
|
// - privatekey
|
|
|
|
// - keypairid
|
|
|
|
//
|
|
|
|
// Optional options:
|
|
|
|
//
|
|
|
|
// - ipFilteredBy
|
|
|
|
// - awsregion
|
|
|
|
// - ipfilteredby: valid value "none|aws|awsregion". "none", do not filter any IP,
|
|
|
|
// default value. "aws", only aws IP goes to S3 directly. "awsregion", only
|
|
|
|
// regions listed in awsregion options goes to S3 directly
|
|
|
|
// - awsregion: a comma separated string of AWS regions.
|
2023-10-27 21:46:09 +00:00
|
|
|
func newCloudFrontStorageMiddleware(ctx context.Context, storageDriver storagedriver.StorageDriver, options map[string]interface{}) (storagedriver.StorageDriver, error) {
|
2017-11-15 01:21:36 +00:00
|
|
|
// parse baseurl
|
2015-01-09 00:55:40 +00:00
|
|
|
base, ok := options["baseurl"]
|
|
|
|
if !ok {
|
2016-01-22 02:17:53 +00:00
|
|
|
return nil, fmt.Errorf("no baseurl provided")
|
2015-01-09 00:55:40 +00:00
|
|
|
}
|
|
|
|
baseURL, ok := base.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("baseurl must be a string")
|
|
|
|
}
|
2016-01-22 02:17:53 +00:00
|
|
|
if !strings.Contains(baseURL, "://") {
|
|
|
|
baseURL = "https://" + baseURL
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(baseURL, "/") {
|
|
|
|
baseURL += "/"
|
|
|
|
}
|
|
|
|
if _, err := url.Parse(baseURL); err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid baseurl: %v", err)
|
|
|
|
}
|
2017-11-15 01:21:36 +00:00
|
|
|
|
|
|
|
// parse privatekey to get pkPath
|
2015-01-09 00:55:40 +00:00
|
|
|
pk, ok := options["privatekey"]
|
|
|
|
if !ok {
|
2016-01-22 02:17:53 +00:00
|
|
|
return nil, fmt.Errorf("no privatekey provided")
|
2015-01-09 00:55:40 +00:00
|
|
|
}
|
|
|
|
pkPath, ok := pk.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("privatekey must be a string")
|
|
|
|
}
|
2017-11-15 01:21:36 +00:00
|
|
|
|
|
|
|
// parse keypairid
|
2015-01-09 00:55:40 +00:00
|
|
|
kpid, ok := options["keypairid"]
|
|
|
|
if !ok {
|
2016-01-22 02:17:53 +00:00
|
|
|
return nil, fmt.Errorf("no keypairid provided")
|
2015-01-09 00:55:40 +00:00
|
|
|
}
|
|
|
|
keypairID, ok := kpid.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("keypairid must be a string")
|
|
|
|
}
|
|
|
|
|
2017-11-15 01:21:36 +00:00
|
|
|
// get urlSigner from the file specified in pkPath
|
2022-11-02 21:55:22 +00:00
|
|
|
pkBytes, err := os.ReadFile(pkPath)
|
2015-01-09 00:55:40 +00:00
|
|
|
if err != nil {
|
2016-01-22 02:17:53 +00:00
|
|
|
return nil, fmt.Errorf("failed to read privatekey file: %s", err)
|
2015-01-09 00:55:40 +00:00
|
|
|
}
|
|
|
|
|
2018-08-06 21:34:15 +00:00
|
|
|
block, _ := pem.Decode(pkBytes)
|
2015-01-09 00:55:40 +00:00
|
|
|
if block == nil {
|
2016-01-22 02:17:53 +00:00
|
|
|
return nil, fmt.Errorf("failed to decode private key as an rsa private key")
|
2015-01-09 00:55:40 +00:00
|
|
|
}
|
|
|
|
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-22 02:17:53 +00:00
|
|
|
urlSigner := sign.NewURLSigner(keypairID, privateKey)
|
2015-01-09 00:55:40 +00:00
|
|
|
|
2017-11-15 01:21:36 +00:00
|
|
|
// parse duration
|
2015-01-15 22:04:16 +00:00
|
|
|
duration := 20 * time.Minute
|
2017-11-15 01:21:36 +00:00
|
|
|
if d, ok := options["duration"]; ok {
|
2015-01-15 22:04:16 +00:00
|
|
|
switch d := d.(type) {
|
|
|
|
case time.Duration:
|
|
|
|
duration = d
|
|
|
|
case string:
|
|
|
|
dur, err := time.ParseDuration(d)
|
|
|
|
if err != nil {
|
2016-01-22 02:17:53 +00:00
|
|
|
return nil, fmt.Errorf("invalid duration: %s", err)
|
2015-01-15 22:04:16 +00:00
|
|
|
}
|
|
|
|
duration = dur
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 22:53:58 +00:00
|
|
|
// parse updatefrequency
|
2017-11-15 01:21:36 +00:00
|
|
|
updateFrequency := defaultUpdateFrequency
|
2019-10-07 22:53:58 +00:00
|
|
|
// #2447 introduced a typo. Support it for backward compatibility.
|
|
|
|
if _, ok := options["updatefrenquency"]; ok {
|
|
|
|
options["updatefrequency"] = options["updatefrenquency"]
|
|
|
|
dcontext.GetLogger(context.Background()).Warn("cloudfront updatefrenquency is deprecated. Please use updatefrequency")
|
|
|
|
}
|
|
|
|
if u, ok := options["updatefrequency"]; ok {
|
2017-11-15 01:21:36 +00:00
|
|
|
switch u := u.(type) {
|
|
|
|
case time.Duration:
|
|
|
|
updateFrequency = u
|
|
|
|
case string:
|
|
|
|
updateFreq, err := time.ParseDuration(u)
|
|
|
|
if err != nil {
|
2019-10-07 22:53:58 +00:00
|
|
|
return nil, fmt.Errorf("invalid updatefrequency: %s", err)
|
2017-11-15 01:21:36 +00:00
|
|
|
}
|
2021-05-06 15:47:43 +00:00
|
|
|
updateFrequency = updateFreq
|
2017-11-15 01:21:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse iprangesurl
|
|
|
|
ipRangesURL := defaultIPRangesURL
|
|
|
|
if i, ok := options["iprangesurl"]; ok {
|
|
|
|
if iprangeurl, ok := i.(string); ok {
|
|
|
|
ipRangesURL = iprangeurl
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("iprangesurl must be a string")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse ipfilteredby
|
|
|
|
var awsIPs *awsIPs
|
2019-01-30 13:05:07 +00:00
|
|
|
if i, ok := options["ipfilteredby"]; ok {
|
|
|
|
if ipFilteredBy, ok := i.(string); ok {
|
|
|
|
switch strings.ToLower(strings.TrimSpace(ipFilteredBy)) {
|
|
|
|
case "", "none":
|
|
|
|
awsIPs = nil
|
|
|
|
case "aws":
|
2023-10-27 21:46:09 +00:00
|
|
|
awsIPs, err = newAWSIPs(ctx, ipRangesURL, updateFrequency, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-30 13:05:07 +00:00
|
|
|
case "awsregion":
|
|
|
|
var awsRegion []string
|
|
|
|
if i, ok := options["awsregion"]; ok {
|
|
|
|
if regions, ok := i.(string); ok {
|
|
|
|
for _, awsRegions := range strings.Split(regions, ",") {
|
|
|
|
awsRegion = append(awsRegion, strings.ToLower(strings.TrimSpace(awsRegions)))
|
|
|
|
}
|
2023-10-27 21:46:09 +00:00
|
|
|
awsIPs, err = newAWSIPs(ctx, ipRangesURL, updateFrequency, awsRegion)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-30 13:05:07 +00:00
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("awsRegion must be a comma separated string of valid aws regions")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("awsRegion is not defined")
|
2017-11-15 01:21:36 +00:00
|
|
|
}
|
2019-01-30 13:05:07 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("ipfilteredby only allows a string the following value: none|aws|awsregion")
|
2017-11-15 01:21:36 +00:00
|
|
|
}
|
2019-01-30 13:05:07 +00:00
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("ipfilteredby only allows a string with the following value: none|aws|awsregion")
|
2017-11-15 01:21:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:17:53 +00:00
|
|
|
return &cloudFrontStorageMiddleware{
|
|
|
|
StorageDriver: storageDriver,
|
|
|
|
urlSigner: urlSigner,
|
|
|
|
baseURL: baseURL,
|
|
|
|
duration: duration,
|
2017-11-15 01:21:36 +00:00
|
|
|
awsIPs: awsIPs,
|
2016-01-22 02:17:53 +00:00
|
|
|
}, nil
|
2015-01-09 00:55:40 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 23:23:31 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-10-24 19:49:47 +00:00
|
|
|
// RedirectURL attempts to find a url which may be used to retrieve the file at the given path.
|
2017-11-15 01:21:36 +00:00
|
|
|
// Returns an error if the file cannot be found.
|
2023-10-24 19:49:47 +00:00
|
|
|
func (lh *cloudFrontStorageMiddleware) RedirectURL(r *http.Request, path string) (string, error) {
|
2015-03-03 16:57:52 +00:00
|
|
|
// TODO(endophage): currently only supports S3
|
2015-04-06 23:23:31 +00:00
|
|
|
keyer, ok := lh.StorageDriver.(S3BucketKeyer)
|
|
|
|
if !ok {
|
2023-10-24 19:49:47 +00:00
|
|
|
dcontext.GetLogger(r.Context()).Warn("the CloudFront middleware does not support this backend storage driver")
|
|
|
|
return lh.StorageDriver.RedirectURL(r, path)
|
2015-01-09 00:55:40 +00:00
|
|
|
}
|
|
|
|
|
2023-10-24 19:49:47 +00:00
|
|
|
if eligibleForS3(r, lh.awsIPs) {
|
|
|
|
return lh.StorageDriver.RedirectURL(r, path)
|
2017-11-15 01:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get signed cloudfront url.
|
2016-01-22 02:17:53 +00:00
|
|
|
cfURL, err := lh.urlSigner.Sign(lh.baseURL+keyer.S3BucketKey(path), time.Now().Add(lh.duration))
|
2015-01-09 00:55:40 +00:00
|
|
|
if err != nil {
|
2015-03-03 16:57:52 +00:00
|
|
|
return "", err
|
2015-01-09 00:55:40 +00:00
|
|
|
}
|
2015-03-03 16:57:52 +00:00
|
|
|
return cfURL, nil
|
2015-01-09 00:55:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// init registers the cloudfront layerHandler backend.
|
|
|
|
func init() {
|
2022-11-02 21:05:45 +00:00
|
|
|
storagemiddleware.Register("cloudfront", newCloudFrontStorageMiddleware)
|
2015-01-09 00:55:40 +00:00
|
|
|
}
|