2021-06-08 15:11:18 -05:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2022-10-22 02:26:14 -05:00
|
|
|
"context"
|
2021-12-07 13:26:26 -05:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2021-06-08 15:11:18 -05:00
|
|
|
"encoding/json"
|
2021-12-17 11:34:22 -05:00
|
|
|
"errors"
|
2022-01-10 11:06:12 -05:00
|
|
|
"fmt"
|
2021-12-07 13:26:26 -05:00
|
|
|
"net/url"
|
2021-06-08 15:11:18 -05:00
|
|
|
"os"
|
|
|
|
"path"
|
2022-10-22 02:26:14 -05:00
|
|
|
"regexp"
|
2021-06-08 15:11:18 -05:00
|
|
|
"strings"
|
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
"github.com/Masterminds/semver"
|
2021-11-25 07:04:39 -05:00
|
|
|
glob "github.com/bmatcuk/doublestar/v4"
|
2022-01-10 11:06:12 -05:00
|
|
|
"github.com/containers/image/v5/docker"
|
2021-06-08 15:11:18 -05:00
|
|
|
"github.com/containers/image/v5/docker/reference"
|
2022-01-10 11:06:12 -05:00
|
|
|
"github.com/containers/image/v5/oci/layout"
|
2021-06-08 15:11:18 -05:00
|
|
|
"github.com/containers/image/v5/types"
|
2022-01-10 11:06:12 -05:00
|
|
|
guuid "github.com/gofrs/uuid"
|
2022-10-22 15:46:13 -05:00
|
|
|
godigest "github.com/opencontainers/go-digest"
|
2021-10-28 04:10:01 -05:00
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2021-12-07 13:26:26 -05:00
|
|
|
artifactspec "github.com/oras-project/artifacts-spec/specs-go/v1"
|
2022-03-07 03:45:10 -05:00
|
|
|
"github.com/sigstore/cosign/pkg/oci/static"
|
2021-12-07 13:26:26 -05:00
|
|
|
"gopkg.in/resty.v1"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2021-12-17 11:34:22 -05:00
|
|
|
zerr "zotregistry.io/zot/errors"
|
2021-12-29 10:14:56 -05:00
|
|
|
"zotregistry.io/zot/pkg/common"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/extensions/monitoring"
|
|
|
|
"zotregistry.io/zot/pkg/log"
|
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2022-09-30 12:35:16 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage/local"
|
2022-02-18 06:36:50 -05:00
|
|
|
"zotregistry.io/zot/pkg/test"
|
2021-06-08 15:11:18 -05:00
|
|
|
)
|
|
|
|
|
2021-12-07 13:26:26 -05:00
|
|
|
type ReferenceList struct {
|
2022-03-07 03:45:10 -05:00
|
|
|
References []artifactspec.Descriptor `json:"references"`
|
2021-12-07 13:26:26 -05:00
|
|
|
}
|
|
|
|
|
2022-04-21 03:09:08 -05:00
|
|
|
func TypeOf(v interface{}) string {
|
|
|
|
return fmt.Sprintf("%T", v)
|
|
|
|
}
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
// getTagFromRef returns a tagged reference from an image reference.
|
|
|
|
func getTagFromRef(ref types.ImageReference, log log.Logger) reference.Tagged {
|
|
|
|
tagged, isTagged := ref.DockerReference().(reference.Tagged)
|
|
|
|
if !isTagged {
|
|
|
|
log.Warn().Msgf("internal server error, reference %s does not have a tag, skipping", ref.DockerReference())
|
|
|
|
}
|
|
|
|
|
|
|
|
return tagged
|
|
|
|
}
|
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
// getImageTags lists all tags in a repository.
|
|
|
|
// It returns a string slice of tags and any error encountered.
|
|
|
|
func getImageTags(ctx context.Context, sysCtx *types.SystemContext, repoRef reference.Named) ([]string, error) {
|
|
|
|
dockerRef, err := docker.NewReference(reference.TagNameOnly(repoRef))
|
|
|
|
// hard to reach test case, injected error, see pkg/test/dev.go
|
|
|
|
if err = test.Error(err); err != nil {
|
|
|
|
return nil, err // Should never happen for a reference with tag and no digest
|
|
|
|
}
|
|
|
|
|
|
|
|
tags, err := docker.GetRepositoryTags(ctx, sysCtx, dockerRef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tags, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// filterTagsByRegex filters images by tag regex given in the config.
|
|
|
|
func filterTagsByRegex(tags []string, regex string, log log.Logger) ([]string, error) {
|
|
|
|
filteredTags := []string{}
|
|
|
|
|
|
|
|
if len(tags) == 0 || regex == "" {
|
|
|
|
return filteredTags, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info().Msgf("start filtering using the regular expression: %s", regex)
|
|
|
|
|
|
|
|
tagReg, err := regexp.Compile(regex)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Str("regex", regex).Msg("couldn't compile regex")
|
|
|
|
|
|
|
|
return filteredTags, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tag := range tags {
|
|
|
|
if tagReg.MatchString(tag) {
|
|
|
|
filteredTags = append(filteredTags, tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filteredTags, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// filterTagsBySemver filters tags by checking if they are semver compliant.
|
|
|
|
func filterTagsBySemver(tags []string, log log.Logger) []string {
|
|
|
|
filteredTags := []string{}
|
|
|
|
|
|
|
|
log.Info().Msg("start filtering using semver compliant rule")
|
|
|
|
|
|
|
|
for _, tag := range tags {
|
|
|
|
_, err := semver.NewVersion(tag)
|
|
|
|
if err == nil {
|
|
|
|
filteredTags = append(filteredTags, tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filteredTags
|
|
|
|
}
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
// parseRepositoryReference parses input into a reference.Named, and verifies that it names a repository, not an image.
|
|
|
|
func parseRepositoryReference(input string) (reference.Named, error) {
|
|
|
|
ref, err := reference.ParseNormalizedNamed(input)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reference.IsNameOnly(ref) {
|
2021-12-17 11:34:22 -05:00
|
|
|
return nil, zerr.ErrInvalidRepositoryName
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ref, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// filterRepos filters repos based on prefix given in the config.
|
2021-12-13 14:23:31 -05:00
|
|
|
func filterRepos(repos []string, contentList []Content, log log.Logger) map[int][]string {
|
2021-06-08 15:11:18 -05:00
|
|
|
filtered := make(map[int][]string)
|
|
|
|
|
|
|
|
for _, repo := range repos {
|
2021-12-13 14:23:31 -05:00
|
|
|
for contentID, content := range contentList {
|
2021-06-08 15:11:18 -05:00
|
|
|
var prefix string
|
2021-11-25 07:04:39 -05:00
|
|
|
// handle prefixes starting with '/'
|
2021-12-13 14:23:31 -05:00
|
|
|
if strings.HasPrefix(content.Prefix, "/") {
|
|
|
|
prefix = content.Prefix[1:]
|
2021-06-08 15:11:18 -05:00
|
|
|
} else {
|
2021-12-13 14:23:31 -05:00
|
|
|
prefix = content.Prefix
|
2021-06-08 15:11:18 -05:00
|
|
|
}
|
|
|
|
|
2021-11-25 07:04:39 -05:00
|
|
|
matched, err := glob.Match(prefix, repo)
|
|
|
|
if err != nil {
|
2022-04-21 03:09:08 -05:00
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
|
|
|
Err(err).Str("pattern",
|
2021-11-25 07:04:39 -05:00
|
|
|
prefix).Msg("error while parsing glob pattern, skipping it...")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if matched {
|
|
|
|
filtered[contentID] = append(filtered[contentID], repo)
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filtered
|
|
|
|
}
|
|
|
|
|
2022-03-10 10:39:11 -05:00
|
|
|
// findRepoContentID return the contentID that maches the localRepo path for a given RegistryConfig in the config file.
|
|
|
|
func findRepoMatchingContentID(localRepo string, contentList []Content) (int, error) {
|
|
|
|
contentID := -1
|
|
|
|
localRepo = strings.Trim(localRepo, "/")
|
|
|
|
|
|
|
|
for cID, content := range contentList {
|
|
|
|
// make sure prefix ends in "/" to extract the meta characters
|
|
|
|
prefix := strings.Trim(content.Prefix, "/") + "/"
|
|
|
|
destination := strings.Trim(content.Destination, "/")
|
|
|
|
|
|
|
|
var patternSlice []string
|
|
|
|
|
|
|
|
if content.StripPrefix {
|
|
|
|
_, metaCharacters := glob.SplitPattern(prefix)
|
|
|
|
patternSlice = append(patternSlice, destination, metaCharacters)
|
|
|
|
} else {
|
|
|
|
patternSlice = append(patternSlice, destination, prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
pattern := strings.Trim(strings.Join(patternSlice, "/"), "/")
|
|
|
|
|
|
|
|
matched, err := glob.Match(pattern, localRepo)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if matched {
|
|
|
|
contentID = cID
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if contentID == -1 {
|
|
|
|
return -1, zerr.ErrRegistryNoContent
|
|
|
|
}
|
|
|
|
|
|
|
|
return contentID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRepoSource(localRepo string, content Content) string {
|
|
|
|
localRepo = strings.Trim(localRepo, "/")
|
|
|
|
destination := strings.Trim(content.Destination, "/")
|
|
|
|
prefix := strings.Trim(content.Prefix, "/*")
|
|
|
|
|
|
|
|
var localRepoSlice []string
|
|
|
|
|
|
|
|
localRepo = strings.TrimPrefix(localRepo, destination)
|
|
|
|
localRepo = strings.Trim(localRepo, "/")
|
|
|
|
|
|
|
|
if content.StripPrefix {
|
|
|
|
localRepoSlice = append([]string{prefix}, localRepo)
|
|
|
|
} else {
|
|
|
|
localRepoSlice = []string{localRepo}
|
|
|
|
}
|
|
|
|
|
|
|
|
repoSource := strings.Join(localRepoSlice, "/")
|
|
|
|
if repoSource == "/" {
|
|
|
|
return repoSource
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Trim(repoSource, "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
// getRepoDestination returns the local storage path of the synced repo based on the specified destination.
|
|
|
|
func getRepoDestination(remoteRepo string, content Content) string {
|
|
|
|
remoteRepo = strings.Trim(remoteRepo, "/")
|
|
|
|
destination := strings.Trim(content.Destination, "/")
|
|
|
|
prefix := strings.Trim(content.Prefix, "/*")
|
|
|
|
|
|
|
|
var repoDestSlice []string
|
|
|
|
|
|
|
|
if content.StripPrefix {
|
|
|
|
remoteRepo = strings.TrimPrefix(remoteRepo, prefix)
|
|
|
|
remoteRepo = strings.Trim(remoteRepo, "/")
|
|
|
|
repoDestSlice = append(repoDestSlice, destination, remoteRepo)
|
|
|
|
} else {
|
|
|
|
repoDestSlice = append(repoDestSlice, destination, remoteRepo)
|
|
|
|
}
|
|
|
|
|
|
|
|
repoDestination := strings.Join(repoDestSlice, "/")
|
|
|
|
|
|
|
|
if repoDestination == "/" {
|
|
|
|
return "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Trim(repoDestination, "/")
|
|
|
|
}
|
|
|
|
|
2021-06-08 15:11:18 -05:00
|
|
|
// Get sync.FileCredentials from file.
|
|
|
|
func getFileCredentials(filepath string) (CredentialsFile, error) {
|
2022-09-02 07:56:02 -05:00
|
|
|
credsFile, err := os.ReadFile(filepath)
|
2021-06-08 15:11:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var creds CredentialsFile
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
err = json.Unmarshal(credsFile, &creds)
|
2021-06-08 15:11:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return creds, nil
|
|
|
|
}
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2021-12-29 10:14:56 -05:00
|
|
|
func getHTTPClient(regCfg *RegistryConfig, upstreamURL string, credentials Credentials,
|
2022-03-21 12:37:23 -05:00
|
|
|
log log.Logger,
|
|
|
|
) (*resty.Client, *url.URL, error) {
|
2021-12-07 13:26:26 -05:00
|
|
|
client := resty.New()
|
|
|
|
|
2021-12-29 10:14:56 -05:00
|
|
|
if !common.Contains(regCfg.URLs, upstreamURL) {
|
2022-03-07 03:45:10 -05:00
|
|
|
return nil, nil, zerr.ErrSyncInvalidUpstreamURL
|
2021-12-29 10:14:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
registryURL, err := url.Parse(upstreamURL)
|
2022-01-27 07:45:46 -05:00
|
|
|
if err != nil {
|
2022-04-21 03:09:08 -05:00
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
|
|
|
Err(err).Str("url", upstreamURL).Msg("couldn't parse url")
|
2022-01-27 07:45:46 -05:00
|
|
|
|
2022-03-07 03:45:10 -05:00
|
|
|
return nil, nil, err
|
2022-01-27 07:45:46 -05:00
|
|
|
}
|
|
|
|
|
2021-12-07 13:26:26 -05:00
|
|
|
if regCfg.CertDir != "" {
|
|
|
|
log.Debug().Msgf("sync: using certs directory: %s", regCfg.CertDir)
|
|
|
|
clientCert := path.Join(regCfg.CertDir, "client.cert")
|
|
|
|
clientKey := path.Join(regCfg.CertDir, "client.key")
|
|
|
|
caCertPath := path.Join(regCfg.CertDir, "ca.crt")
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
caCert, err := os.ReadFile(caCertPath)
|
2021-12-07 13:26:26 -05:00
|
|
|
if err != nil {
|
2022-04-21 03:09:08 -05:00
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
|
|
|
Err(err).Msg("couldn't read CA certificate")
|
2021-12-07 13:26:26 -05:00
|
|
|
|
2022-03-07 03:45:10 -05:00
|
|
|
return nil, nil, err
|
2021-12-07 13:26:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
caCertPool.AppendCertsFromPEM(caCert)
|
|
|
|
|
|
|
|
client.SetTLSClientConfig(&tls.Config{RootCAs: caCertPool, MinVersion: tls.VersionTLS12})
|
|
|
|
|
|
|
|
cert, err := tls.LoadX509KeyPair(clientCert, clientKey)
|
|
|
|
if err != nil {
|
2022-04-21 03:09:08 -05:00
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
|
|
|
Err(err).Msg("couldn't read certificates key pairs")
|
2021-12-07 13:26:26 -05:00
|
|
|
|
2022-03-07 03:45:10 -05:00
|
|
|
return nil, nil, err
|
2021-12-07 13:26:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
client.SetCertificates(cert)
|
|
|
|
}
|
|
|
|
|
2022-10-05 05:21:14 -05:00
|
|
|
//nolint: gosec
|
2022-01-27 07:45:46 -05:00
|
|
|
if regCfg.TLSVerify != nil && !*regCfg.TLSVerify && registryURL.Scheme == "https" {
|
2021-12-07 13:26:26 -05:00
|
|
|
client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
|
|
|
|
}
|
|
|
|
|
|
|
|
if credentials.Username != "" && credentials.Password != "" {
|
|
|
|
log.Debug().Msgf("sync: using basic auth")
|
|
|
|
client.SetBasicAuth(credentials.Username, credentials.Password)
|
|
|
|
}
|
|
|
|
|
2022-05-25 05:23:06 -05:00
|
|
|
client.SetRedirectPolicy(resty.FlexibleRedirectPolicy(httpMaxRedirectsCount))
|
|
|
|
|
2022-03-07 03:45:10 -05:00
|
|
|
return client, registryURL, nil
|
2021-12-07 13:26:26 -05:00
|
|
|
}
|
|
|
|
|
2022-10-27 11:39:59 -05:00
|
|
|
func pushSyncedLocalImage(localRepo, reference, localCachePath string,
|
2022-03-21 12:37:23 -05:00
|
|
|
imageStore storage.ImageStore, log log.Logger,
|
|
|
|
) error {
|
2022-10-27 11:39:59 -05:00
|
|
|
log.Info().Msgf("pushing synced local image %s/%s:%s to local registry", localCachePath, localRepo, reference)
|
2021-10-28 04:10:01 -05:00
|
|
|
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-06-24 08:08:47 -05:00
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
cacheImageStore := local.NewImageStore(localCachePath, false,
|
2022-11-02 17:53:08 -05:00
|
|
|
storage.DefaultGCDelay, false, false, log, metrics, nil, nil)
|
2021-10-28 04:10:01 -05:00
|
|
|
|
2022-10-27 11:39:59 -05:00
|
|
|
manifestContent, _, mediaType, err := cacheImageStore.GetImageManifest(localRepo, reference)
|
2021-10-28 04:10:01 -05:00
|
|
|
if err != nil {
|
2022-04-21 03:09:08 -05:00
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
|
|
|
Err(err).Str("dir", path.Join(cacheImageStore.RootDir(), localRepo)).
|
2022-10-27 11:39:59 -05:00
|
|
|
Msgf("couldn't find %s manifest", reference)
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-10-28 04:10:01 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-06 11:41:16 -05:00
|
|
|
// is image manifest
|
2022-11-15 01:21:49 -05:00
|
|
|
switch mediaType {
|
|
|
|
case ispec.MediaTypeImageManifest:
|
2022-10-27 11:39:59 -05:00
|
|
|
if err := copyManifest(localRepo, manifestContent, reference, cacheImageStore, imageStore, log); err != nil {
|
2022-10-06 11:41:16 -05:00
|
|
|
if errors.Is(err, zerr.ErrImageLintAnnotations) {
|
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
|
|
|
Err(err).Msg("couldn't upload manifest because of missing annotations")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2022-11-15 01:21:49 -05:00
|
|
|
case ispec.MediaTypeImageIndex:
|
|
|
|
// is image index
|
|
|
|
var indexManifest ispec.Index
|
2022-10-06 11:41:16 -05:00
|
|
|
|
2022-11-15 01:21:49 -05:00
|
|
|
if err := json.Unmarshal(manifestContent, &indexManifest); err != nil {
|
2022-10-06 11:41:16 -05:00
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
2022-11-15 01:21:49 -05:00
|
|
|
Err(err).Str("dir", path.Join(cacheImageStore.RootDir(), localRepo)).
|
|
|
|
Msg("invalid JSON")
|
2022-10-06 11:41:16 -05:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-15 01:21:49 -05:00
|
|
|
for _, manifest := range indexManifest.Manifests {
|
|
|
|
manifestBuf, err := cacheImageStore.GetBlobContent(localRepo, manifest.Digest)
|
|
|
|
if err != nil {
|
2022-10-06 11:41:16 -05:00
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
2022-11-15 01:21:49 -05:00
|
|
|
Err(err).Str("dir", path.Join(cacheImageStore.RootDir(), localRepo)).Str("digest", manifest.Digest.String()).
|
|
|
|
Msg("couldn't find manifest which is part of an image index")
|
2022-10-06 11:41:16 -05:00
|
|
|
|
2022-11-15 01:21:49 -05:00
|
|
|
return err
|
2022-10-06 11:41:16 -05:00
|
|
|
}
|
|
|
|
|
2022-11-15 01:21:49 -05:00
|
|
|
if err := copyManifest(localRepo, manifestBuf, manifest.Digest.String(),
|
|
|
|
cacheImageStore, imageStore, log); err != nil {
|
|
|
|
if errors.Is(err, zerr.ErrImageLintAnnotations) {
|
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
|
|
|
Err(err).Msg("couldn't upload manifest because of missing annotations")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2022-10-06 11:41:16 -05:00
|
|
|
}
|
|
|
|
|
2022-11-15 01:21:49 -05:00
|
|
|
_, err = imageStore.PutImageManifest(localRepo, reference, mediaType, manifestContent)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
|
|
|
Err(err).Msg("couldn't upload manifest")
|
2022-10-06 11:41:16 -05:00
|
|
|
|
2022-11-15 01:21:49 -05:00
|
|
|
return err
|
|
|
|
}
|
2022-10-06 11:41:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyManifest(localRepo string, manifestContent []byte, reference string,
|
|
|
|
cacheImageStore, imageStore storage.ImageStore, log log.Logger,
|
|
|
|
) error {
|
2021-10-28 04:10:01 -05:00
|
|
|
var manifest ispec.Manifest
|
|
|
|
|
2022-10-06 11:41:16 -05:00
|
|
|
var err error
|
|
|
|
|
2021-10-28 04:10:01 -05:00
|
|
|
if err := json.Unmarshal(manifestContent, &manifest); err != nil {
|
2022-04-21 03:09:08 -05:00
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
|
|
|
Err(err).Str("dir", path.Join(cacheImageStore.RootDir(), localRepo)).
|
2022-03-10 10:39:11 -05:00
|
|
|
Msg("invalid JSON")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-10-28 04:10:01 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, blob := range manifest.Layers {
|
2022-10-22 15:46:13 -05:00
|
|
|
err = copyBlob(localRepo, blob.Digest, blob.MediaType,
|
2022-08-19 05:38:59 -05:00
|
|
|
cacheImageStore, imageStore, log)
|
2021-10-28 04:10:01 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
err = copyBlob(localRepo, manifest.Config.Digest, manifest.Config.MediaType,
|
2022-08-19 05:38:59 -05:00
|
|
|
cacheImageStore, imageStore, log)
|
2021-10-28 04:10:01 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-06 11:41:16 -05:00
|
|
|
_, err = imageStore.PutImageManifest(localRepo, reference,
|
2022-06-24 08:08:47 -05:00
|
|
|
ispec.MediaTypeImageManifest, manifestContent)
|
2021-10-28 04:10:01 -05:00
|
|
|
if err != nil {
|
2022-04-21 03:09:08 -05:00
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
|
|
|
Err(err).Msg("couldn't upload manifest")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-10-28 04:10:01 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-07 13:26:26 -05:00
|
|
|
|
2022-08-19 05:38:59 -05:00
|
|
|
// Copy a blob from one image store to another image store.
|
2022-10-22 15:46:13 -05:00
|
|
|
func copyBlob(localRepo string, blobDigest godigest.Digest, blobMediaType string,
|
2022-08-19 05:38:59 -05:00
|
|
|
souceImageStore, destinationImageStore storage.ImageStore, log log.Logger,
|
|
|
|
) error {
|
|
|
|
if found, _, _ := destinationImageStore.CheckBlob(localRepo, blobDigest); found {
|
|
|
|
// Blob is already at destination, nothing to do
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
blobReadCloser, _, err := souceImageStore.GetBlob(localRepo, blobDigest, blobMediaType)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Str("errorType", TypeOf(err)).Err(err).
|
|
|
|
Str("dir", path.Join(souceImageStore.RootDir(), localRepo)).
|
2022-10-22 15:46:13 -05:00
|
|
|
Str("blob digest", blobDigest.String()).Str("media type", blobMediaType).
|
2022-08-19 05:38:59 -05:00
|
|
|
Msg("couldn't read blob")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer blobReadCloser.Close()
|
|
|
|
|
|
|
|
_, _, err = destinationImageStore.FullBlobUpload(localRepo, blobReadCloser, blobDigest)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Str("errorType", TypeOf(err)).Err(err).
|
2022-10-22 15:46:13 -05:00
|
|
|
Str("blob digest", blobDigest.String()).Str("media type", blobMediaType).
|
2022-08-19 05:38:59 -05:00
|
|
|
Msg("couldn't upload blob")
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-29 10:14:56 -05:00
|
|
|
// sync needs transport to be stripped to not be wrongly interpreted as an image reference
|
|
|
|
// at a non-fully qualified registry (hostname as image and port as tag).
|
|
|
|
func StripRegistryTransport(url string) string {
|
|
|
|
return strings.Replace(strings.Replace(url, "http://", "", 1), "https://", "", 1)
|
|
|
|
}
|
2022-01-10 11:06:12 -05:00
|
|
|
|
|
|
|
// get an ImageReference given the registry, repo and tag.
|
2022-10-27 11:39:59 -05:00
|
|
|
func getImageRef(registryDomain, repo, ref string) (types.ImageReference, error) {
|
2022-01-10 11:06:12 -05:00
|
|
|
repoRef, err := parseRepositoryReference(fmt.Sprintf("%s/%s", registryDomain, repo))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-27 11:39:59 -05:00
|
|
|
var namedRepoRef reference.Named
|
|
|
|
|
|
|
|
digest, ok := parseDigest(ref)
|
|
|
|
if ok {
|
|
|
|
namedRepoRef, err = reference.WithDigest(repoRef, digest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
namedRepoRef, err = reference.WithTag(repoRef, ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-10 11:06:12 -05:00
|
|
|
}
|
|
|
|
|
2022-10-27 11:39:59 -05:00
|
|
|
imageRef, err := docker.NewReference(namedRepoRef)
|
2022-01-10 11:06:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageRef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// get a local ImageReference used to temporary store one synced image.
|
2022-10-27 11:39:59 -05:00
|
|
|
func getLocalImageRef(localCachePath, repo, reference string) (types.ImageReference, error) {
|
2022-04-05 10:18:31 -05:00
|
|
|
if _, err := os.ReadDir(localCachePath); err != nil {
|
|
|
|
return nil, err
|
2022-02-18 06:36:50 -05:00
|
|
|
}
|
|
|
|
|
2022-01-10 11:06:12 -05:00
|
|
|
localRepo := path.Join(localCachePath, repo)
|
|
|
|
|
2022-10-27 11:39:59 -05:00
|
|
|
_, refIsDigest := parseDigest(reference)
|
|
|
|
|
|
|
|
if !refIsDigest {
|
|
|
|
localRepo = fmt.Sprintf("%s:%s", localRepo, reference)
|
|
|
|
}
|
|
|
|
|
|
|
|
localImageRef, err := layout.ParseReference(localRepo)
|
2022-01-10 11:06:12 -05:00
|
|
|
if err != nil {
|
2022-04-05 10:18:31 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return localImageRef, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the localCachePath with an UUID at the end. Only to be called once per repo.
|
|
|
|
func getLocalCachePath(imageStore storage.ImageStore, repo string) (string, error) {
|
|
|
|
localRepoPath := path.Join(imageStore.RootDir(), repo, SyncBlobUploadDir)
|
|
|
|
// check if SyncBlobUploadDir exists, create if not
|
|
|
|
var err error
|
|
|
|
if _, err = os.ReadDir(localRepoPath); os.IsNotExist(err) {
|
2022-09-30 12:35:16 -05:00
|
|
|
if err = os.MkdirAll(localRepoPath, local.DefaultDirPerms); err != nil {
|
2022-04-05 10:18:31 -05:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create uuid folder
|
|
|
|
uuid, err := guuid.NewV4()
|
|
|
|
// hard to reach test case, injected error, see pkg/test/dev.go
|
|
|
|
if err := test.Error(err); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
localCachePath := path.Join(localRepoPath, uuid.String())
|
|
|
|
|
|
|
|
cachedRepoPath := path.Join(localCachePath, repo)
|
2022-09-30 12:35:16 -05:00
|
|
|
if err = os.MkdirAll(cachedRepoPath, local.DefaultDirPerms); err != nil {
|
2022-04-05 10:18:31 -05:00
|
|
|
return "", err
|
2022-01-10 11:06:12 -05:00
|
|
|
}
|
|
|
|
|
2022-04-05 10:18:31 -05:00
|
|
|
return localCachePath, nil
|
2022-01-10 11:06:12 -05:00
|
|
|
}
|
2021-12-17 11:34:22 -05:00
|
|
|
|
2022-03-07 03:45:10 -05:00
|
|
|
// canSkipImage returns whether or not we already synced this image.
|
2022-10-22 15:46:13 -05:00
|
|
|
func canSkipImage(repo, tag string, digest godigest.Digest, imageStore storage.ImageStore, log log.Logger,
|
|
|
|
) (bool, error) {
|
2022-03-07 03:45:10 -05:00
|
|
|
// check image already synced
|
|
|
|
_, localImageManifestDigest, _, err := imageStore.GetImageManifest(repo, tag)
|
2021-12-17 11:34:22 -05:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, zerr.ErrRepoNotFound) || errors.Is(err, zerr.ErrManifestNotFound) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2022-04-21 03:09:08 -05:00
|
|
|
log.Error().Str("errorType", TypeOf(err)).
|
|
|
|
Err(err).Msgf("couldn't get local image %s:%s manifest", repo, tag)
|
2021-12-17 11:34:22 -05:00
|
|
|
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2022-03-07 03:45:10 -05:00
|
|
|
if localImageManifestDigest != digest {
|
|
|
|
log.Info().Msgf("upstream image %s:%s digest changed, syncing again", repo, tag)
|
2021-12-17 11:34:22 -05:00
|
|
|
|
2022-03-07 03:45:10 -05:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-10-27 11:39:59 -05:00
|
|
|
// parse a reference, return its digest and if it's valid.
|
|
|
|
func parseDigest(reference string) (godigest.Digest, bool) {
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
d, err := godigest.Parse(reference)
|
|
|
|
if err == nil {
|
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return d, ok
|
|
|
|
}
|
|
|
|
|
2022-03-07 03:45:10 -05:00
|
|
|
func manifestsEqual(manifest1, manifest2 ispec.Manifest) bool {
|
|
|
|
if manifest1.Config.Digest == manifest2.Config.Digest &&
|
|
|
|
manifest1.Config.MediaType == manifest2.Config.MediaType &&
|
2022-04-14 14:07:44 -05:00
|
|
|
manifest1.Config.Size == manifest2.Config.Size {
|
|
|
|
if descriptorsEqual(manifest1.Layers, manifest2.Layers) {
|
2022-03-07 03:45:10 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func artifactDescriptorsEqual(desc1, desc2 []artifactspec.Descriptor) bool {
|
2022-04-14 14:07:44 -05:00
|
|
|
if len(desc1) != len(desc2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for id, desc := range desc1 {
|
|
|
|
if desc.Digest != desc2[id].Digest ||
|
|
|
|
desc.Size != desc2[id].Size ||
|
|
|
|
desc.MediaType != desc2[id].MediaType ||
|
|
|
|
desc.ArtifactType != desc2[id].ArtifactType {
|
|
|
|
return false
|
2022-03-07 03:45:10 -05:00
|
|
|
}
|
2021-12-17 11:34:22 -05:00
|
|
|
}
|
|
|
|
|
2022-04-14 14:07:44 -05:00
|
|
|
return true
|
2022-03-07 03:45:10 -05:00
|
|
|
}
|
2021-12-17 11:34:22 -05:00
|
|
|
|
2022-04-14 14:07:44 -05:00
|
|
|
func descriptorsEqual(desc1, desc2 []ispec.Descriptor) bool {
|
|
|
|
if len(desc1) != len(desc2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for id, desc := range desc1 {
|
|
|
|
if desc.Digest != desc2[id].Digest ||
|
|
|
|
desc.Size != desc2[id].Size ||
|
|
|
|
desc.MediaType != desc2[id].MediaType ||
|
|
|
|
desc.Annotations[static.SignatureAnnotationKey] != desc2[id].Annotations[static.SignatureAnnotationKey] {
|
|
|
|
return false
|
2022-03-07 03:45:10 -05:00
|
|
|
}
|
2021-12-17 11:34:22 -05:00
|
|
|
}
|
|
|
|
|
2022-04-14 14:07:44 -05:00
|
|
|
return true
|
2021-12-17 11:34:22 -05:00
|
|
|
}
|