2015-07-29 18:12:01 +00:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
2017-08-11 22:31:16 +00:00
|
|
|
"context"
|
2016-02-11 02:07:28 +00:00
|
|
|
"fmt"
|
2015-07-29 18:12:01 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2016-02-11 02:07:28 +00:00
|
|
|
"sync"
|
2020-08-28 00:07:35 +00:00
|
|
|
"time"
|
2015-07-29 18:12:01 +00:00
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3"
|
|
|
|
"github.com/distribution/distribution/v3/configuration"
|
|
|
|
dcontext "github.com/distribution/distribution/v3/context"
|
|
|
|
"github.com/distribution/distribution/v3/reference"
|
|
|
|
"github.com/distribution/distribution/v3/registry/client"
|
|
|
|
"github.com/distribution/distribution/v3/registry/client/auth"
|
|
|
|
"github.com/distribution/distribution/v3/registry/client/auth/challenge"
|
|
|
|
"github.com/distribution/distribution/v3/registry/client/transport"
|
|
|
|
"github.com/distribution/distribution/v3/registry/proxy/scheduler"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver"
|
2015-07-29 18:12:01 +00:00
|
|
|
)
|
|
|
|
|
2020-08-28 00:07:35 +00:00
|
|
|
var repositoryTTL = 24 * 7 * time.Hour
|
|
|
|
|
2015-07-29 18:12:01 +00:00
|
|
|
// proxyingRegistry fetches content from a remote registry and caches it locally
|
|
|
|
type proxyingRegistry struct {
|
2016-02-11 02:07:28 +00:00
|
|
|
embedded distribution.Namespace // provides local registry functionality
|
|
|
|
scheduler *scheduler.TTLExpirationScheduler
|
2020-08-28 00:07:35 +00:00
|
|
|
ttl *time.Duration
|
2016-02-26 22:18:09 +00:00
|
|
|
remoteURL url.URL
|
2016-02-11 02:07:28 +00:00
|
|
|
authChallenger authChallenger
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRegistryPullThroughCache creates a registry acting as a pull through cache
|
|
|
|
func NewRegistryPullThroughCache(ctx context.Context, registry distribution.Namespace, driver driver.StorageDriver, config configuration.Proxy) (distribution.Namespace, error) {
|
2016-02-26 22:18:09 +00:00
|
|
|
remoteURL, err := url.Parse(config.RemoteURL)
|
2015-07-29 18:12:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
v := storage.NewVacuum(ctx, driver)
|
2016-01-27 00:42:10 +00:00
|
|
|
|
2020-08-28 00:07:35 +00:00
|
|
|
var s *scheduler.TTLExpirationScheduler
|
|
|
|
var ttl *time.Duration
|
|
|
|
if config.TTL == nil {
|
|
|
|
// Default TTL is 7 days
|
|
|
|
ttl = &repositoryTTL
|
|
|
|
} else if *config.TTL > 0 {
|
|
|
|
ttl = config.TTL
|
|
|
|
} else {
|
|
|
|
// TTL is disabled, never expire
|
|
|
|
ttl = nil
|
|
|
|
}
|
2016-01-27 00:42:10 +00:00
|
|
|
|
2020-08-28 00:07:35 +00:00
|
|
|
if ttl != nil {
|
|
|
|
s = scheduler.New(ctx, driver, "/scheduler-state.json")
|
|
|
|
s.OnBlobExpire(func(ref reference.Reference) error {
|
|
|
|
var r reference.Canonical
|
|
|
|
var ok bool
|
|
|
|
if r, ok = ref.(reference.Canonical); !ok {
|
|
|
|
return fmt.Errorf("unexpected reference type : %T", ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
repo, err := registry.Repository(ctx, r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
blobs := repo.Blobs(ctx)
|
|
|
|
|
|
|
|
// Clear the repository reference and descriptor caches
|
|
|
|
err = blobs.Delete(ctx, r.Digest())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = v.RemoveBlob(r.Digest().String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
s.OnManifestExpire(func(ref reference.Reference) error {
|
|
|
|
var r reference.Canonical
|
|
|
|
var ok bool
|
|
|
|
if r, ok = ref.(reference.Canonical); !ok {
|
|
|
|
return fmt.Errorf("unexpected reference type : %T", ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
repo, err := registry.Repository(ctx, r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
manifests, err := repo.Manifests(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = manifests.Delete(ctx, r.Digest())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
err = s.Start()
|
2016-01-27 00:42:10 +00:00
|
|
|
if err != nil {
|
2020-08-28 00:07:35 +00:00
|
|
|
return nil, err
|
2016-01-27 00:42:10 +00:00
|
|
|
}
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 23:46:51 +00:00
|
|
|
cs, err := configureAuth(config.Username, config.Password, config.RemoteURL)
|
2015-07-29 18:12:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proxyingRegistry{
|
2016-02-11 02:07:28 +00:00
|
|
|
embedded: registry,
|
|
|
|
scheduler: s,
|
2020-08-28 00:07:35 +00:00
|
|
|
ttl: ttl,
|
2016-02-26 22:18:09 +00:00
|
|
|
remoteURL: *remoteURL,
|
2016-02-11 02:07:28 +00:00
|
|
|
authChallenger: &remoteAuthChallenger{
|
2016-02-26 22:18:09 +00:00
|
|
|
remoteURL: *remoteURL,
|
2016-11-08 01:13:56 +00:00
|
|
|
cm: challenge.NewSimpleManager(),
|
2016-02-17 18:42:34 +00:00
|
|
|
cs: cs,
|
2016-02-11 02:07:28 +00:00
|
|
|
},
|
2015-07-29 18:12:01 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *proxyingRegistry) Scope() distribution.Scope {
|
|
|
|
return distribution.GlobalScope
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *proxyingRegistry) Repositories(ctx context.Context, repos []string, last string) (n int, err error) {
|
|
|
|
return pr.embedded.Repositories(ctx, repos, last)
|
|
|
|
}
|
|
|
|
|
2015-12-15 22:35:23 +00:00
|
|
|
func (pr *proxyingRegistry) Repository(ctx context.Context, name reference.Named) (distribution.Repository, error) {
|
2016-02-17 18:42:34 +00:00
|
|
|
c := pr.authChallenger
|
2016-02-11 02:07:28 +00:00
|
|
|
|
2017-08-15 01:30:46 +00:00
|
|
|
tkopts := auth.TokenHandlerOptions{
|
|
|
|
Transport: http.DefaultTransport,
|
|
|
|
Credentials: c.credentialStore(),
|
|
|
|
Scopes: []auth.Scope{
|
|
|
|
auth.RepositoryScope{
|
|
|
|
Repository: name.Name(),
|
|
|
|
Actions: []string{"pull"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Logger: dcontext.GetLogger(ctx),
|
|
|
|
}
|
|
|
|
|
2015-07-29 18:12:01 +00:00
|
|
|
tr := transport.NewTransport(http.DefaultTransport,
|
2017-08-15 01:30:46 +00:00
|
|
|
auth.NewAuthorizer(c.challengeManager(),
|
|
|
|
auth.NewTokenHandlerWithOptions(tkopts)))
|
2015-07-29 18:12:01 +00:00
|
|
|
|
|
|
|
localRepo, err := pr.embedded.Repository(ctx, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-21 04:50:15 +00:00
|
|
|
localManifests, err := localRepo.Manifests(ctx, storage.SkipLayerVerification())
|
2015-07-29 18:12:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-02 15:37:34 +00:00
|
|
|
remoteRepo, err := client.NewRepository(name, pr.remoteURL.String(), tr)
|
2015-07-29 18:12:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteManifests, err := remoteRepo.Manifests(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proxiedRepository{
|
2015-09-18 23:11:35 +00:00
|
|
|
blobStore: &proxyBlobStore{
|
2016-01-27 00:42:10 +00:00
|
|
|
localStore: localRepo.Blobs(ctx),
|
|
|
|
remoteStore: remoteRepo.Blobs(ctx),
|
|
|
|
scheduler: pr.scheduler,
|
2020-08-28 00:07:35 +00:00
|
|
|
ttl: pr.ttl,
|
2016-01-27 00:42:10 +00:00
|
|
|
repositoryName: name,
|
2016-02-11 02:07:28 +00:00
|
|
|
authChallenger: pr.authChallenger,
|
2015-07-29 18:12:01 +00:00
|
|
|
},
|
2016-01-27 00:42:10 +00:00
|
|
|
manifests: &proxyManifestStore{
|
2015-07-29 18:12:01 +00:00
|
|
|
repositoryName: name,
|
|
|
|
localManifests: localManifests, // Options?
|
|
|
|
remoteManifests: remoteManifests,
|
|
|
|
ctx: ctx,
|
|
|
|
scheduler: pr.scheduler,
|
2020-08-28 00:07:35 +00:00
|
|
|
ttl: pr.ttl,
|
2016-02-11 02:07:28 +00:00
|
|
|
authChallenger: pr.authChallenger,
|
2015-07-29 18:12:01 +00:00
|
|
|
},
|
2015-08-21 04:50:15 +00:00
|
|
|
name: name,
|
2016-01-27 00:42:10 +00:00
|
|
|
tags: &proxyTagService{
|
2016-02-11 02:07:28 +00:00
|
|
|
localTags: localRepo.Tags(ctx),
|
|
|
|
remoteTags: remoteRepo.Tags(ctx),
|
|
|
|
authChallenger: pr.authChallenger,
|
2015-08-21 04:50:15 +00:00
|
|
|
},
|
2015-07-29 18:12:01 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-01-19 22:26:15 +00:00
|
|
|
func (pr *proxyingRegistry) Blobs() distribution.BlobEnumerator {
|
|
|
|
return pr.embedded.Blobs()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *proxyingRegistry) BlobStatter() distribution.BlobStatter {
|
|
|
|
return pr.embedded.BlobStatter()
|
|
|
|
}
|
|
|
|
|
2016-02-11 02:07:28 +00:00
|
|
|
// authChallenger encapsulates a request to the upstream to establish credential challenges
|
|
|
|
type authChallenger interface {
|
|
|
|
tryEstablishChallenges(context.Context) error
|
2016-11-08 01:13:56 +00:00
|
|
|
challengeManager() challenge.Manager
|
2016-02-17 18:42:34 +00:00
|
|
|
credentialStore() auth.CredentialStore
|
2016-02-11 02:07:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type remoteAuthChallenger struct {
|
2016-02-26 22:18:09 +00:00
|
|
|
remoteURL url.URL
|
2016-02-11 02:07:28 +00:00
|
|
|
sync.Mutex
|
2016-11-08 01:13:56 +00:00
|
|
|
cm challenge.Manager
|
2016-02-17 18:42:34 +00:00
|
|
|
cs auth.CredentialStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *remoteAuthChallenger) credentialStore() auth.CredentialStore {
|
|
|
|
return r.cs
|
|
|
|
}
|
|
|
|
|
2016-11-08 01:13:56 +00:00
|
|
|
func (r *remoteAuthChallenger) challengeManager() challenge.Manager {
|
2016-02-17 18:42:34 +00:00
|
|
|
return r.cm
|
2016-02-11 02:07:28 +00:00
|
|
|
}
|
|
|
|
|
2016-02-17 18:42:34 +00:00
|
|
|
// tryEstablishChallenges will attempt to get a challenge type for the upstream if none currently exist
|
|
|
|
func (r *remoteAuthChallenger) tryEstablishChallenges(ctx context.Context) error {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
2016-02-11 02:07:28 +00:00
|
|
|
|
2016-02-26 22:18:09 +00:00
|
|
|
remoteURL := r.remoteURL
|
|
|
|
remoteURL.Path = "/v2/"
|
2016-09-13 01:58:12 +00:00
|
|
|
challenges, err := r.cm.GetChallenges(remoteURL)
|
2016-02-11 02:07:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(challenges) > 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// establish challenge type with upstream
|
2016-02-26 22:18:09 +00:00
|
|
|
if err := ping(r.cm, remoteURL.String(), challengeHeader); err != nil {
|
2016-02-11 02:07:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-11 22:31:16 +00:00
|
|
|
dcontext.GetLogger(ctx).Infof("Challenge established with upstream : %s %s", remoteURL, r.cm)
|
2016-02-11 02:07:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-29 18:12:01 +00:00
|
|
|
// proxiedRepository uses proxying blob and manifest services to serve content
|
|
|
|
// locally, or pulling it through from a remote and caching it locally if it doesn't
|
|
|
|
// already exist
|
|
|
|
type proxiedRepository struct {
|
2015-08-21 04:50:15 +00:00
|
|
|
blobStore distribution.BlobStore
|
|
|
|
manifests distribution.ManifestService
|
2015-12-15 22:35:23 +00:00
|
|
|
name reference.Named
|
2015-08-21 04:50:15 +00:00
|
|
|
tags distribution.TagService
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *proxiedRepository) Manifests(ctx context.Context, options ...distribution.ManifestServiceOption) (distribution.ManifestService, error) {
|
|
|
|
return pr.manifests, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr *proxiedRepository) Blobs(ctx context.Context) distribution.BlobStore {
|
|
|
|
return pr.blobStore
|
|
|
|
}
|
|
|
|
|
2016-02-01 21:47:34 +00:00
|
|
|
func (pr *proxiedRepository) Named() reference.Named {
|
2015-07-29 18:12:01 +00:00
|
|
|
return pr.name
|
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
func (pr *proxiedRepository) Tags(ctx context.Context) distribution.TagService {
|
|
|
|
return pr.tags
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|