2021-06-08 15:11:18 -05:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-07 03:45:10 -05:00
|
|
|
"errors"
|
2021-06-08 15:11:18 -05:00
|
|
|
"fmt"
|
2021-10-28 04:10:01 -05:00
|
|
|
"io"
|
2022-12-22 13:19:42 -05:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2021-06-08 15:11:18 -05:00
|
|
|
"os"
|
2021-12-02 12:45:26 -05:00
|
|
|
goSync "sync"
|
2021-06-08 15:11:18 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/containers/common/pkg/retry"
|
|
|
|
"github.com/containers/image/v5/copy"
|
|
|
|
"github.com/containers/image/v5/docker"
|
|
|
|
"github.com/containers/image/v5/docker/reference"
|
|
|
|
"github.com/containers/image/v5/signature"
|
|
|
|
"github.com/containers/image/v5/types"
|
2022-12-09 14:38:00 -05:00
|
|
|
"github.com/opencontainers/go-digest"
|
2021-06-08 15:11:18 -05:00
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2022-03-07 03:45:10 -05:00
|
|
|
zerr "zotregistry.io/zot/errors"
|
2022-02-24 15:31:36 -05:00
|
|
|
"zotregistry.io/zot/pkg/api/constants"
|
2022-12-22 13:19:42 -05:00
|
|
|
"zotregistry.io/zot/pkg/common"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/log"
|
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2022-02-18 06:36:50 -05:00
|
|
|
"zotregistry.io/zot/pkg/test"
|
2021-06-08 15:11:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-05-25 05:23:06 -05:00
|
|
|
SyncBlobUploadDir = ".sync"
|
|
|
|
httpMaxRedirectsCount = 15
|
2021-06-08 15:11:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// /v2/_catalog struct.
|
|
|
|
type catalog struct {
|
|
|
|
Repositories []string `json:"repositories"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// key is registry address.
|
|
|
|
type CredentialsFile map[string]Credentials
|
|
|
|
|
|
|
|
type Credentials struct {
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
2021-12-28 08:29:30 -05:00
|
|
|
Enable *bool
|
2021-06-08 15:11:18 -05:00
|
|
|
CredentialsFile string
|
|
|
|
Registries []RegistryConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
type RegistryConfig struct {
|
2021-12-29 10:14:56 -05:00
|
|
|
URLs []string
|
2021-06-08 15:11:18 -05:00
|
|
|
PollInterval time.Duration
|
|
|
|
Content []Content
|
|
|
|
TLSVerify *bool
|
|
|
|
OnDemand bool
|
|
|
|
CertDir string
|
2022-01-10 11:06:12 -05:00
|
|
|
MaxRetries *int
|
|
|
|
RetryDelay *time.Duration
|
2022-03-07 03:45:10 -05:00
|
|
|
OnlySigned *bool
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type Content struct {
|
2022-03-10 10:39:11 -05:00
|
|
|
Prefix string
|
|
|
|
Tags *Tags
|
|
|
|
Destination string `mapstructure:",omitempty"`
|
|
|
|
StripPrefix bool
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type Tags struct {
|
|
|
|
Regex *string
|
|
|
|
Semver *bool
|
|
|
|
}
|
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
type RepoReferences struct {
|
|
|
|
contentID int // matched registry config content
|
|
|
|
name string // repo name
|
|
|
|
imageReferences []types.ImageReference // contained images(tags)
|
|
|
|
}
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
// getUpstreamCatalog gets all repos from a registry.
|
2022-12-22 13:19:42 -05:00
|
|
|
func GetUpstreamCatalog(client *http.Client, upstreamURL, username, password string, log log.Logger) (catalog, error) { //nolint
|
2022-03-21 12:37:23 -05:00
|
|
|
var catalog catalog
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-02-24 15:31:36 -05:00
|
|
|
registryCatalogURL := fmt.Sprintf("%s%s%s", upstreamURL, constants.RoutePrefix, constants.ExtCatalogPrefix)
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-12-22 13:19:42 -05:00
|
|
|
body, statusCode, err := common.MakeHTTPGetRequest(client, username,
|
|
|
|
password, &catalog,
|
|
|
|
registryCatalogURL, "application/json", log)
|
2021-06-08 15:11:18 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Msgf("couldn't query %s, status code: %d, body: %s", registryCatalogURL,
|
2022-12-22 13:19:42 -05:00
|
|
|
statusCode, body)
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2022-03-21 12:37:23 -05:00
|
|
|
return catalog, err
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
2022-03-21 12:37:23 -05:00
|
|
|
return catalog, nil
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
// imagesToCopyFromRepos lists all images given a registry name and its repos.
|
|
|
|
func imagesToCopyFromUpstream(ctx context.Context, registryName string, repoName string,
|
|
|
|
upstreamCtx *types.SystemContext, content Content, log log.Logger,
|
|
|
|
) ([]types.ImageReference, error) {
|
|
|
|
imageRefs := []types.ImageReference{}
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
repoRef, err := parseRepositoryReference(fmt.Sprintf("%s/%s", registryName, repoName))
|
2021-06-08 15:11:18 -05:00
|
|
|
if err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
log.Error().Str("errorType", common.TypeOf(err)).
|
2022-10-22 02:26:14 -05:00
|
|
|
Err(err).Msgf("couldn't parse repository reference: %s", repoRef)
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
return imageRefs, err
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
tags, err := getImageTags(ctx, upstreamCtx, repoRef)
|
|
|
|
if err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
log.Error().Str("errorType", common.TypeOf(err)).
|
2022-10-22 02:26:14 -05:00
|
|
|
Err(err).Msgf("couldn't fetch tags for %s", repoRef)
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
return imageRefs, err
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
// filter based on tags rules
|
|
|
|
if content.Tags != nil {
|
|
|
|
if content.Tags.Regex != nil {
|
|
|
|
tags, err = filterTagsByRegex(tags, *content.Tags.Regex, log)
|
|
|
|
if err != nil {
|
|
|
|
return imageRefs, err
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
if content.Tags.Semver != nil && *content.Tags.Semver {
|
|
|
|
tags = filterTagsBySemver(tags, log)
|
|
|
|
}
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
log.Debug().Msgf("repo: %s - upstream tags to be copied: %v", repoName, tags)
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
for _, tag := range tags {
|
|
|
|
// don't copy cosign signature, containers/image doesn't support it
|
|
|
|
// we will copy it manually later
|
|
|
|
if isCosignTag(tag) {
|
|
|
|
continue
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
taggedRef, err := reference.WithTag(repoRef, tag)
|
2021-06-08 15:11:18 -05:00
|
|
|
if err != nil {
|
2022-10-22 02:26:14 -05:00
|
|
|
log.Err(err).Msgf("error creating a reference for repository %s and tag %q", repoRef.Name(), tag)
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
return imageRefs, err
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
ref, err := docker.NewReference(taggedRef)
|
2022-04-05 10:18:31 -05:00
|
|
|
if err != nil {
|
2022-10-22 02:26:14 -05:00
|
|
|
log.Err(err).Msgf("cannot obtain a valid image reference for transport %q and reference %s",
|
|
|
|
docker.Transport.Name(), taggedRef.String())
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
return imageRefs, err
|
|
|
|
}
|
2022-04-05 10:18:31 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
imageRefs = append(imageRefs, ref)
|
2022-04-05 10:18:31 -05:00
|
|
|
}
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
return imageRefs, nil
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func getCopyOptions(upstreamCtx, localCtx *types.SystemContext) copy.Options {
|
|
|
|
options := copy.Options{
|
2021-12-07 13:26:26 -05:00
|
|
|
DestinationCtx: localCtx,
|
|
|
|
SourceCtx: upstreamCtx,
|
|
|
|
ReportWriter: io.Discard,
|
|
|
|
ForceManifestMIMEType: ispec.MediaTypeImageManifest, // force only oci manifest MIME type
|
2022-10-06 11:41:16 -05:00
|
|
|
ImageListSelection: copy.CopyAllImages,
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUpstreamContext(regCfg *RegistryConfig, credentials Credentials) *types.SystemContext {
|
|
|
|
upstreamCtx := &types.SystemContext{}
|
|
|
|
upstreamCtx.DockerCertPath = regCfg.CertDir
|
|
|
|
upstreamCtx.DockerDaemonCertPath = regCfg.CertDir
|
|
|
|
|
|
|
|
if regCfg.TLSVerify != nil && *regCfg.TLSVerify {
|
|
|
|
upstreamCtx.DockerDaemonInsecureSkipTLSVerify = false
|
|
|
|
upstreamCtx.DockerInsecureSkipTLSVerify = types.NewOptionalBool(false)
|
|
|
|
} else {
|
|
|
|
upstreamCtx.DockerDaemonInsecureSkipTLSVerify = true
|
|
|
|
upstreamCtx.DockerInsecureSkipTLSVerify = types.NewOptionalBool(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
if credentials != (Credentials{}) {
|
|
|
|
upstreamCtx.DockerAuthConfig = &types.DockerAuthConfig{
|
|
|
|
Username: credentials.Username,
|
|
|
|
Password: credentials.Password,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return upstreamCtx
|
|
|
|
}
|
|
|
|
|
2022-10-05 05:21:14 -05:00
|
|
|
//nolint:gocyclo // offloading some of the functionalities from here would make the code harder to follow
|
2022-06-24 08:08:47 -05:00
|
|
|
func syncRegistry(ctx context.Context, regCfg RegistryConfig,
|
|
|
|
upstreamURL string,
|
2022-02-10 09:17:49 -05:00
|
|
|
storeController storage.StoreController, localCtx *types.SystemContext,
|
2022-03-07 03:45:10 -05:00
|
|
|
policyCtx *signature.PolicyContext, credentials Credentials,
|
2022-03-21 12:37:23 -05:00
|
|
|
retryOptions *retry.RetryOptions, log log.Logger,
|
|
|
|
) error {
|
2021-12-29 10:14:56 -05:00
|
|
|
log.Info().Msgf("syncing registry: %s", upstreamURL)
|
2021-06-08 15:11:18 -05:00
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
log.Debug().Msg("getting upstream context")
|
|
|
|
|
|
|
|
upstreamCtx := getUpstreamContext(®Cfg, credentials)
|
|
|
|
options := getCopyOptions(upstreamCtx, localCtx)
|
|
|
|
|
2022-12-22 13:19:42 -05:00
|
|
|
if !common.Contains(regCfg.URLs, upstreamURL) {
|
|
|
|
return zerr.ErrSyncInvalidUpstreamURL
|
|
|
|
}
|
|
|
|
|
|
|
|
registryURL, err := url.Parse(upstreamURL)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Str("errorType", common.TypeOf(err)).
|
|
|
|
Err(err).Str("url", upstreamURL).Msg("couldn't parse url")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
httpClient, err := common.CreateHTTPClient(*regCfg.TLSVerify, registryURL.Host, regCfg.CertDir)
|
2021-12-07 13:26:26 -05:00
|
|
|
if err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
log.Error().Str("errorType", common.TypeOf(err)).
|
|
|
|
Err(err).Msg("error while creating http client")
|
|
|
|
|
2021-12-07 13:26:26 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-07 03:45:10 -05:00
|
|
|
var catalog catalog
|
|
|
|
|
2022-02-10 09:17:49 -05:00
|
|
|
if err = retry.RetryIfNecessary(ctx, func() error {
|
2022-12-22 13:19:42 -05:00
|
|
|
catalog, err = GetUpstreamCatalog(httpClient, upstreamURL, credentials.Username, credentials.Password, log)
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
return err
|
|
|
|
}, retryOptions); err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
log.Error().Str("errorType", common.TypeOf(err)).
|
2022-04-21 03:09:08 -05:00
|
|
|
Err(err).Msg("error while getting upstream catalog, retrying...")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-25 07:04:39 -05:00
|
|
|
log.Info().Msgf("filtering %d repos based on sync prefixes", len(catalog.Repositories))
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2021-11-25 07:04:39 -05:00
|
|
|
repos := filterRepos(catalog.Repositories, regCfg.Content, log)
|
2021-06-08 15:11:18 -05:00
|
|
|
|
|
|
|
log.Info().Msgf("got repos: %v", repos)
|
|
|
|
|
2022-04-05 10:18:31 -05:00
|
|
|
upstreamAddr := StripRegistryTransport(upstreamURL)
|
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
reposReferences := []RepoReferences{}
|
2021-12-29 10:14:56 -05:00
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
for contentID, repos := range repos {
|
2022-10-22 02:26:14 -05:00
|
|
|
for _, repoName := range repos {
|
|
|
|
var imageReferences []types.ImageReference
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
if err = retry.RetryIfNecessary(ctx, func() error {
|
|
|
|
imageReferences, err = imagesToCopyFromUpstream(ctx, upstreamAddr, repoName, upstreamCtx,
|
|
|
|
regCfg.Content[contentID], log)
|
2022-04-05 10:18:31 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
return err
|
|
|
|
}, retryOptions); err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
log.Error().Str("errorType", common.TypeOf(err)).
|
2022-10-22 02:26:14 -05:00
|
|
|
Err(err).Msg("error while getting images references from upstream, retrying...")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
return err
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
reposReferences = append(reposReferences, RepoReferences{
|
|
|
|
contentID: contentID,
|
|
|
|
name: repoName,
|
|
|
|
imageReferences: imageReferences,
|
|
|
|
})
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 13:19:42 -05:00
|
|
|
sig := newSignaturesCopier(httpClient, credentials, *registryURL, storeController, log)
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
for _, repoReference := range reposReferences {
|
|
|
|
upstreamRepo := repoReference.name
|
|
|
|
content := regCfg.Content[repoReference.contentID]
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
localRepo := getRepoDestination(upstreamRepo, content)
|
2022-03-07 03:45:10 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
imageStore := storeController.GetImageStore(localRepo)
|
2022-10-05 13:03:24 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
localCachePath, err := getLocalCachePath(imageStore, localRepo)
|
|
|
|
if err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
log.Error().Str("errorType", common.TypeOf(err)).
|
2022-10-22 02:26:14 -05:00
|
|
|
Err(err).Msgf("couldn't get localCachePath for %s", localRepo)
|
2022-03-07 03:45:10 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
return err
|
|
|
|
}
|
2022-03-07 03:45:10 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
defer os.RemoveAll(localCachePath)
|
2022-03-07 03:45:10 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
for _, upstreamImageRef := range repoReference.imageReferences {
|
2022-12-09 14:38:00 -05:00
|
|
|
manifestBuf, mediaType, err := getImageRefManifest(ctx, upstreamCtx, upstreamImageRef, log)
|
2022-04-05 10:18:31 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-03-07 03:45:10 -05:00
|
|
|
}
|
|
|
|
|
2022-12-09 14:38:00 -05:00
|
|
|
upstreamImageDigest := digest.FromBytes(manifestBuf)
|
|
|
|
|
|
|
|
tag := getTagFromRef(upstreamImageRef, log).Tag()
|
|
|
|
|
|
|
|
if !isSupportedMediaType(mediaType) {
|
|
|
|
if mediaType == ispec.MediaTypeArtifactManifest {
|
2022-12-22 13:19:42 -05:00
|
|
|
err = sig.syncOCIArtifact(localRepo, upstreamRepo, tag, manifestBuf) //nolint
|
2022-12-09 14:38:00 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-04-05 10:18:31 -05:00
|
|
|
// get upstream signatures
|
2022-10-22 02:26:14 -05:00
|
|
|
cosignManifest, err := sig.getCosignManifest(upstreamRepo, upstreamImageDigest.String())
|
2022-11-15 01:21:49 -05:00
|
|
|
if err != nil && !errors.Is(err, zerr.ErrSyncReferrerNotFound) {
|
2022-04-05 10:18:31 -05:00
|
|
|
log.Error().Err(err).Msgf("couldn't get upstream image %s cosign manifest", upstreamImageRef.DockerReference())
|
2022-03-07 03:45:10 -05:00
|
|
|
|
2022-04-05 10:18:31 -05:00
|
|
|
return err
|
|
|
|
}
|
2021-12-17 11:34:22 -05:00
|
|
|
|
2023-02-13 13:43:52 -05:00
|
|
|
index, err := sig.getOCIRefs(upstreamRepo, upstreamImageDigest.String())
|
2022-11-15 01:21:49 -05:00
|
|
|
if err != nil && !errors.Is(err, zerr.ErrSyncReferrerNotFound) {
|
2023-02-13 13:43:52 -05:00
|
|
|
log.Error().Err(err).Msgf("couldn't get upstream image %s OCI references", upstreamImageRef.DockerReference())
|
2022-03-07 03:45:10 -05:00
|
|
|
|
2022-04-05 10:18:31 -05:00
|
|
|
return err
|
2022-03-07 03:45:10 -05:00
|
|
|
}
|
|
|
|
|
2022-04-05 10:18:31 -05:00
|
|
|
// check if upstream image is signed
|
2023-02-13 13:43:52 -05:00
|
|
|
if cosignManifest == nil && len(getNotationManifestsFromOCIRefs(index)) == 0 {
|
2022-04-05 10:18:31 -05:00
|
|
|
// upstream image not signed
|
|
|
|
if regCfg.OnlySigned != nil && *regCfg.OnlySigned {
|
|
|
|
// skip unsigned images
|
|
|
|
log.Info().Msgf("skipping image without signature %s", upstreamImageRef.DockerReference())
|
2022-03-07 03:45:10 -05:00
|
|
|
|
2022-04-05 10:18:31 -05:00
|
|
|
continue
|
2022-03-07 03:45:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
skipImage, err := canSkipImage(localRepo, tag, upstreamImageDigest, imageStore, log)
|
2022-03-07 03:45:10 -05:00
|
|
|
if err != nil {
|
2022-04-05 10:18:31 -05:00
|
|
|
log.Error().Err(err).Msgf("couldn't check if the upstream image %s can be skipped",
|
2022-03-07 03:45:10 -05:00
|
|
|
upstreamImageRef.DockerReference())
|
2022-04-05 10:18:31 -05:00
|
|
|
|
|
|
|
return err
|
2022-03-07 03:45:10 -05:00
|
|
|
}
|
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
if !skipImage {
|
|
|
|
// sync image
|
|
|
|
localImageRef, err := getLocalImageRef(localCachePath, localRepo, tag)
|
2022-04-05 10:18:31 -05:00
|
|
|
if err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
log.Error().Str("errorType", common.TypeOf(err)).
|
2022-10-22 02:26:14 -05:00
|
|
|
Err(err).Msgf("couldn't obtain a valid image reference for reference %s/%s:%s",
|
|
|
|
localCachePath, localRepo, tag)
|
2022-03-07 03:45:10 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
return err
|
2022-04-05 10:18:31 -05:00
|
|
|
}
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
log.Info().Msgf("copying image %s to %s", upstreamImageRef.DockerReference(), localCachePath)
|
2022-02-18 06:36:50 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
if err = retry.RetryIfNecessary(ctx, func() error {
|
|
|
|
_, err = copy.Image(ctx, policyCtx, localImageRef, upstreamImageRef, &options)
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
return err
|
|
|
|
}, retryOptions); err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
log.Error().Str("errorType", common.TypeOf(err)).
|
2022-10-22 02:26:14 -05:00
|
|
|
Err(err).Msgf("error while copying image %s to %s",
|
|
|
|
upstreamImageRef.DockerReference(), localCachePath)
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
return err
|
|
|
|
}
|
2023-02-15 14:36:50 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
// push from cache to repo
|
|
|
|
err = pushSyncedLocalImage(localRepo, tag, localCachePath, imageStore, log)
|
|
|
|
if err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
log.Error().Str("errorType", common.TypeOf(err)).
|
2022-10-22 02:26:14 -05:00
|
|
|
Err(err).Msgf("error while pushing synced cached image %s",
|
|
|
|
fmt.Sprintf("%s/%s:%s", localCachePath, localRepo, tag))
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Info().Msgf("already synced image %s, checking its signatures", upstreamImageRef.DockerReference())
|
2022-04-05 10:18:31 -05:00
|
|
|
}
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
// sync signatures
|
2022-04-05 10:18:31 -05:00
|
|
|
if err = retry.RetryIfNecessary(ctx, func() error {
|
2023-02-13 13:43:52 -05:00
|
|
|
err = sig.syncOCIRefs(localRepo, upstreamRepo, upstreamImageDigest.String(), index)
|
|
|
|
if err != nil {
|
2022-11-15 01:21:49 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-13 13:43:52 -05:00
|
|
|
refs, err := sig.getORASRefs(upstreamRepo, upstreamImageDigest.String())
|
|
|
|
if err != nil && !errors.Is(err, zerr.ErrSyncReferrerNotFound) {
|
2022-11-15 01:21:49 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-13 13:43:52 -05:00
|
|
|
err = sig.syncORASRefs(localRepo, upstreamRepo, upstreamImageDigest.String(), refs)
|
2022-10-22 02:26:14 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-05 10:18:31 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
err = sig.syncCosignSignature(localRepo, upstreamRepo, upstreamImageDigest.String(), cosignManifest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-05 10:18:31 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
return nil
|
2022-04-05 10:18:31 -05:00
|
|
|
}, retryOptions); err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
log.Error().Str("errorType", common.TypeOf(err)).
|
2022-11-15 01:21:49 -05:00
|
|
|
Err(err).Msgf("couldn't copy referrer for %s", upstreamImageRef.DockerReference())
|
2022-04-05 10:18:31 -05:00
|
|
|
}
|
2021-12-07 13:26:26 -05:00
|
|
|
}
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
2021-12-29 10:14:56 -05:00
|
|
|
log.Info().Msgf("finished syncing %s", upstreamAddr)
|
2021-06-08 15:11:18 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-28 04:10:01 -05:00
|
|
|
func getLocalContexts(log log.Logger) (*types.SystemContext, *signature.PolicyContext, error) {
|
2021-06-08 15:11:18 -05:00
|
|
|
log.Debug().Msg("getting local context")
|
|
|
|
|
|
|
|
var policy *signature.Policy
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
localCtx := &types.SystemContext{}
|
2021-12-07 13:26:26 -05:00
|
|
|
// preserve compression
|
|
|
|
localCtx.OCIAcceptUncompressedLayers = true
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2021-10-28 04:10:01 -05:00
|
|
|
// accept any image with or without signature
|
|
|
|
policy = &signature.Policy{Default: []signature.PolicyRequirement{signature.NewPRInsecureAcceptAnything()}}
|
2021-06-08 15:11:18 -05:00
|
|
|
|
|
|
|
policyContext, err := signature.NewPolicyContext(policy)
|
2022-02-18 06:36:50 -05:00
|
|
|
if err := test.Error(err); err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
log.Error().Str("errorType", common.TypeOf(err)).
|
2022-04-21 03:09:08 -05:00
|
|
|
Err(err).Msg("couldn't create policy context")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
return &types.SystemContext{}, &signature.PolicyContext{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return localCtx, policyContext, nil
|
|
|
|
}
|
|
|
|
|
2022-06-24 08:08:47 -05:00
|
|
|
func Run(ctx context.Context, cfg Config,
|
|
|
|
storeController storage.StoreController,
|
2022-03-21 12:37:23 -05:00
|
|
|
wtgrp *goSync.WaitGroup, logger log.Logger,
|
|
|
|
) error {
|
2021-06-08 15:11:18 -05:00
|
|
|
var credentialsFile CredentialsFile
|
|
|
|
|
2021-10-28 04:10:01 -05:00
|
|
|
var err error
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
if cfg.CredentialsFile != "" {
|
|
|
|
credentialsFile, err = getFileCredentials(cfg.CredentialsFile)
|
|
|
|
if err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
logger.Error().Str("errortype", common.TypeOf(err)).
|
2022-04-21 03:09:08 -05:00
|
|
|
Err(err).Msgf("couldn't get registry credentials from %s", cfg.CredentialsFile)
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-28 04:10:01 -05:00
|
|
|
localCtx, policyCtx, err := getLocalContexts(logger)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// for each upstream registry, start a go routine.
|
2021-06-08 15:11:18 -05:00
|
|
|
for _, regCfg := range cfg.Registries {
|
2021-10-25 07:05:03 -05:00
|
|
|
// if content not provided, don't run periodically sync
|
2021-11-25 07:04:39 -05:00
|
|
|
if len(regCfg.Content) == 0 {
|
2021-12-29 10:14:56 -05:00
|
|
|
logger.Info().Msgf("sync config content not configured for %v, will not run periodically sync", regCfg.URLs)
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-10-25 07:05:03 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// if pollInterval is not provided, don't run periodically sync
|
|
|
|
if regCfg.PollInterval == 0 {
|
2021-12-29 10:14:56 -05:00
|
|
|
logger.Warn().Msgf("sync config PollInterval not configured for %v, will not run periodically sync", regCfg.URLs)
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-11-25 07:04:39 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-22 04:56:55 -05:00
|
|
|
ticker := time.NewTicker(regCfg.PollInterval)
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2022-03-07 03:45:10 -05:00
|
|
|
retryOptions := &retry.RetryOptions{}
|
|
|
|
|
|
|
|
if regCfg.MaxRetries != nil {
|
|
|
|
retryOptions.MaxRetry = *regCfg.MaxRetries
|
|
|
|
if regCfg.RetryDelay != nil {
|
|
|
|
retryOptions.Delay = *regCfg.RetryDelay
|
|
|
|
}
|
|
|
|
}
|
2021-06-08 15:11:18 -05:00
|
|
|
|
2021-10-25 07:05:03 -05:00
|
|
|
// schedule each registry sync
|
2022-02-10 09:17:49 -05:00
|
|
|
go func(ctx context.Context, regCfg RegistryConfig, logger log.Logger) {
|
|
|
|
for {
|
2021-12-17 11:37:02 -05:00
|
|
|
// increment reference since will be busy, so shutdown has to wait
|
2021-12-13 14:23:31 -05:00
|
|
|
wtgrp.Add(1)
|
2021-12-17 11:37:02 -05:00
|
|
|
|
2021-12-29 10:14:56 -05:00
|
|
|
for _, upstreamURL := range regCfg.URLs {
|
|
|
|
upstreamAddr := StripRegistryTransport(upstreamURL)
|
|
|
|
// first try syncing main registry
|
2022-02-10 09:17:49 -05:00
|
|
|
if err := syncRegistry(ctx, regCfg, upstreamURL, storeController, localCtx, policyCtx,
|
2022-03-07 03:45:10 -05:00
|
|
|
credentialsFile[upstreamAddr], retryOptions, logger); err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
logger.Error().Str("errortype", common.TypeOf(err)).
|
2022-04-21 03:09:08 -05:00
|
|
|
Err(err).Str("registry", upstreamURL).
|
2022-02-10 09:17:49 -05:00
|
|
|
Msg("sync exited with error, falling back to auxiliary registries if any")
|
2021-12-29 10:14:56 -05:00
|
|
|
} else {
|
|
|
|
// if success fall back to main registry
|
|
|
|
break
|
|
|
|
}
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
2021-12-02 12:45:26 -05:00
|
|
|
// mark as done after a single sync run
|
2021-12-13 14:23:31 -05:00
|
|
|
wtgrp.Done()
|
2022-02-10 09:17:49 -05:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
ticker.Stop()
|
|
|
|
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
// run on intervals
|
|
|
|
continue
|
|
|
|
}
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
2022-09-29 15:28:39 -05:00
|
|
|
}(ctx, regCfg, logger)
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
2021-10-28 04:10:01 -05:00
|
|
|
logger.Info().Msg("finished setting up sync")
|
2021-06-08 15:11:18 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|