2022-03-07 03:45:10 -05:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2022-12-22 13:19:42 -05:00
|
|
|
"context"
|
2022-03-07 03:45:10 -05:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2023-03-09 13:41:48 -05:00
|
|
|
"fmt"
|
2022-03-07 03:45:10 -05:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2022-08-20 20:18:50 -05:00
|
|
|
notreg "github.com/notaryproject/notation-go/registry"
|
2022-10-22 15:46:13 -05:00
|
|
|
godigest "github.com/opencontainers/go-digest"
|
2022-03-07 03:45:10 -05:00
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2022-11-15 01:21:49 -05:00
|
|
|
oras "github.com/oras-project/artifacts-spec/specs-go/v1"
|
2023-04-27 11:35:10 -05:00
|
|
|
"github.com/sigstore/cosign/v2/pkg/oci/remote"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2022-03-07 03:45:10 -05:00
|
|
|
zerr "zotregistry.io/zot/errors"
|
2022-03-24 07:25:14 -05:00
|
|
|
"zotregistry.io/zot/pkg/api/constants"
|
2022-12-22 13:19:42 -05:00
|
|
|
"zotregistry.io/zot/pkg/common"
|
2023-02-16 01:20:28 -05:00
|
|
|
syncconf "zotregistry.io/zot/pkg/extensions/config/sync"
|
2022-03-07 03:45:10 -05:00
|
|
|
"zotregistry.io/zot/pkg/log"
|
2023-03-09 13:41:48 -05:00
|
|
|
"zotregistry.io/zot/pkg/meta/repodb"
|
2022-03-07 03:45:10 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage"
|
|
|
|
)
|
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
type signaturesCopier struct {
|
2022-12-22 13:19:42 -05:00
|
|
|
client *http.Client
|
2022-10-22 02:26:14 -05:00
|
|
|
upstreamURL url.URL
|
2023-02-16 01:20:28 -05:00
|
|
|
credentials syncconf.Credentials
|
2023-03-09 13:41:48 -05:00
|
|
|
repoDB repodb.RepoDB
|
|
|
|
storeController storage.StoreController
|
2022-10-22 02:26:14 -05:00
|
|
|
log log.Logger
|
|
|
|
}
|
|
|
|
|
2023-02-16 01:20:28 -05:00
|
|
|
func newSignaturesCopier(httpClient *http.Client, credentials syncconf.Credentials,
|
2023-03-09 13:41:48 -05:00
|
|
|
upstreamURL url.URL, repoDB repodb.RepoDB,
|
2022-10-22 02:26:14 -05:00
|
|
|
storeController storage.StoreController, log log.Logger,
|
|
|
|
) *signaturesCopier {
|
|
|
|
return &signaturesCopier{
|
|
|
|
client: httpClient,
|
2022-12-22 13:19:42 -05:00
|
|
|
credentials: credentials,
|
2022-10-22 02:26:14 -05:00
|
|
|
upstreamURL: upstreamURL,
|
2023-03-09 13:41:48 -05:00
|
|
|
repoDB: repoDB,
|
2022-10-22 02:26:14 -05:00
|
|
|
storeController: storeController,
|
|
|
|
log: log,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
func (sig *signaturesCopier) getCosignManifest(repo, digestStr string) (*ispec.Manifest, error) {
|
2022-03-21 12:37:23 -05:00
|
|
|
var cosignManifest ispec.Manifest
|
2022-03-07 03:45:10 -05:00
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
cosignTag := getCosignTagFromImageDigest(digestStr)
|
2022-03-07 03:45:10 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
getCosignManifestURL := sig.upstreamURL
|
2022-03-07 03:45:10 -05:00
|
|
|
|
|
|
|
getCosignManifestURL.Path = path.Join(getCosignManifestURL.Path, "v2", repo, "manifests", cosignTag)
|
|
|
|
|
|
|
|
getCosignManifestURL.RawQuery = getCosignManifestURL.Query().Encode()
|
|
|
|
|
2022-12-22 13:19:42 -05:00
|
|
|
_, statusCode, err := common.MakeHTTPGetRequest(sig.client, sig.credentials.Username,
|
|
|
|
sig.credentials.Password, &cosignManifest,
|
|
|
|
getCosignManifestURL.String(), ispec.MediaTypeImageManifest, sig.log)
|
2022-03-07 03:45:10 -05:00
|
|
|
if err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
if statusCode == http.StatusNotFound {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).Err(err).
|
|
|
|
Str("manifest", getCosignManifestURL.String()).Msg("couldn't find any cosign manifest")
|
2022-03-07 03:45:10 -05:00
|
|
|
|
2022-12-22 13:19:42 -05:00
|
|
|
return nil, zerr.ErrSyncReferrerNotFound
|
|
|
|
}
|
2022-03-07 03:45:10 -05:00
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).Err(err).
|
|
|
|
Str("manifest", getCosignManifestURL.String()).Msg("couldn't get cosign manifest")
|
2022-03-07 03:45:10 -05:00
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-21 12:37:23 -05:00
|
|
|
return &cosignManifest, nil
|
2022-03-07 03:45:10 -05:00
|
|
|
}
|
|
|
|
|
2023-02-13 13:43:52 -05:00
|
|
|
func (sig *signaturesCopier) getORASRefs(repo, digestStr string) (ReferenceList, error) {
|
2022-03-07 03:45:10 -05:00
|
|
|
var referrers ReferenceList
|
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
getReferrersURL := sig.upstreamURL
|
2022-03-07 03:45:10 -05:00
|
|
|
|
|
|
|
// based on manifest digest get referrers
|
2022-03-24 07:25:14 -05:00
|
|
|
getReferrersURL.Path = path.Join(getReferrersURL.Path, constants.ArtifactSpecRoutePrefix,
|
2022-10-22 15:46:13 -05:00
|
|
|
repo, "manifests", digestStr, "referrers")
|
2022-03-24 07:25:14 -05:00
|
|
|
|
2022-03-07 03:45:10 -05:00
|
|
|
getReferrersURL.RawQuery = getReferrersURL.Query().Encode()
|
|
|
|
|
2022-12-22 13:19:42 -05:00
|
|
|
_, statusCode, err := common.MakeHTTPGetRequest(sig.client, sig.credentials.Username,
|
|
|
|
sig.credentials.Password, &referrers,
|
|
|
|
getReferrersURL.String(), "application/json", sig.log)
|
2022-03-07 03:45:10 -05:00
|
|
|
if err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
if statusCode == http.StatusNotFound {
|
2023-02-13 13:43:52 -05:00
|
|
|
sig.log.Info().Err(err).Msg("couldn't find any ORAS artifact")
|
2022-03-07 03:45:10 -05:00
|
|
|
|
2022-12-22 13:19:42 -05:00
|
|
|
return referrers, zerr.ErrSyncReferrerNotFound
|
|
|
|
}
|
2022-03-07 03:45:10 -05:00
|
|
|
|
2023-02-13 13:43:52 -05:00
|
|
|
sig.log.Error().Err(err).Msg("couldn't get ORAS artifacts")
|
2022-03-07 03:45:10 -05:00
|
|
|
|
|
|
|
return referrers, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return referrers, nil
|
|
|
|
}
|
|
|
|
|
2022-11-15 01:21:49 -05:00
|
|
|
func (sig *signaturesCopier) getOCIRefs(repo, digestStr string) (ispec.Index, error) {
|
|
|
|
var index ispec.Index
|
|
|
|
|
|
|
|
getReferrersURL := sig.upstreamURL
|
|
|
|
// based on manifest digest get referrers
|
|
|
|
getReferrersURL.Path = path.Join(getReferrersURL.Path, "v2", repo, "referrers", digestStr)
|
|
|
|
|
|
|
|
getReferrersURL.RawQuery = getReferrersURL.Query().Encode()
|
|
|
|
|
2022-12-22 13:19:42 -05:00
|
|
|
_, statusCode, err := common.MakeHTTPGetRequest(sig.client, sig.credentials.Username,
|
|
|
|
sig.credentials.Password, &index,
|
|
|
|
getReferrersURL.String(), "application/json", sig.log)
|
2022-11-15 01:21:49 -05:00
|
|
|
if err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
if statusCode == http.StatusNotFound {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Info().Str("referrers", getReferrersURL.String()).Int("statusCode", statusCode).
|
|
|
|
Msg("couldn't find any oci reference from referrers, skipping")
|
2022-11-15 01:21:49 -05:00
|
|
|
|
2022-12-22 13:19:42 -05:00
|
|
|
return index, zerr.ErrSyncReferrerNotFound
|
|
|
|
}
|
2022-11-15 01:21:49 -05:00
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(zerr.ErrSyncReferrer)).Err(zerr.ErrSyncReferrer).
|
|
|
|
Str("referrers", getReferrersURL.String()).Int("statusCode", statusCode).
|
|
|
|
Msg("couldn't get oci reference from referrers, skipping")
|
2022-11-15 01:21:49 -05:00
|
|
|
|
|
|
|
return index, zerr.ErrSyncReferrer
|
|
|
|
}
|
|
|
|
|
|
|
|
return index, nil
|
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
func (sig *signaturesCopier) syncCosignSignature(localRepo, remoteRepo, digestStr string,
|
|
|
|
cosignManifest *ispec.Manifest,
|
2022-03-07 03:45:10 -05:00
|
|
|
) error {
|
2022-10-22 15:46:13 -05:00
|
|
|
cosignTag := getCosignTagFromImageDigest(digestStr)
|
2022-03-07 03:45:10 -05:00
|
|
|
|
|
|
|
// if no manifest found
|
|
|
|
if cosignManifest == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
skipCosignSig, err := sig.canSkipCosignSignature(localRepo, digestStr, cosignManifest)
|
2022-10-22 02:26:14 -05:00
|
|
|
if err != nil {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Error().Err(err).Str("repository", remoteRepo).Str("reference", digestStr).
|
|
|
|
Msg("couldn't check if the upstream image cosign signature can be skipped")
|
2022-10-22 02:26:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if skipCosignSig {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
imageStore := sig.storeController.GetImageStore(localRepo)
|
|
|
|
|
|
|
|
sig.log.Info().Msg("syncing cosign signatures")
|
2022-03-07 03:45:10 -05:00
|
|
|
|
|
|
|
for _, blob := range cosignManifest.Layers {
|
2022-11-15 01:21:49 -05:00
|
|
|
if err := syncBlob(sig, imageStore, localRepo, remoteRepo, blob.Digest); err != nil {
|
2022-03-07 03:45:10 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-15 01:21:49 -05:00
|
|
|
// sync config blob
|
|
|
|
if err := syncBlob(sig, imageStore, localRepo, remoteRepo, cosignManifest.Config.Digest); err != nil {
|
2022-03-07 03:45:10 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cosignManifestBuf, err := json.Marshal(cosignManifest)
|
|
|
|
if err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).
|
2022-04-21 03:09:08 -05:00
|
|
|
Err(err).Msg("couldn't marshal cosign manifest")
|
2022-03-07 03:45:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// push manifest
|
2022-06-24 08:08:47 -05:00
|
|
|
_, err = imageStore.PutImageManifest(localRepo, cosignTag,
|
|
|
|
ispec.MediaTypeImageManifest, cosignManifestBuf)
|
2022-03-07 03:45:10 -05:00
|
|
|
if err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).
|
2022-04-21 03:09:08 -05:00
|
|
|
Err(err).Msg("couldn't upload cosign manifest")
|
2022-03-07 03:45:10 -05:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-09 13:41:48 -05:00
|
|
|
if sig.repoDB != nil {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Debug().Str("repository", localRepo).Str("digest", digestStr).
|
|
|
|
Msg("trying to sync cosign signature for repo digest")
|
2023-03-09 13:41:48 -05:00
|
|
|
|
|
|
|
err = repodb.SetMetadataFromInput(localRepo, cosignTag, ispec.MediaTypeImageManifest,
|
|
|
|
godigest.FromBytes(cosignManifestBuf), cosignManifestBuf, sig.storeController.GetImageStore(localRepo),
|
|
|
|
sig.repoDB, sig.log)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to set metadata for cosign signature '%s@%s': %w", localRepo, digestStr, err)
|
|
|
|
}
|
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Info().Str("repository", localRepo).Str("digest", digestStr).
|
|
|
|
Msg("successfully added cosign signature to RepoDB for repo digest")
|
2023-03-09 13:41:48 -05:00
|
|
|
}
|
2022-03-07 03:45:10 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-13 13:43:52 -05:00
|
|
|
func (sig *signaturesCopier) syncORASRefs(localRepo, remoteRepo, digestStr string, referrers ReferenceList,
|
2022-03-21 12:37:23 -05:00
|
|
|
) error {
|
2022-03-07 03:45:10 -05:00
|
|
|
if len(referrers.References) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-13 13:43:52 -05:00
|
|
|
skipORASRefs, err := sig.canSkipORASRefs(localRepo, digestStr, referrers)
|
2022-11-15 01:21:49 -05:00
|
|
|
if err != nil {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Error().Err(err).Str("repository", remoteRepo).Str("reference", digestStr).
|
|
|
|
Msg("couldn't check if the upstream image ORAS artifact can be skipped")
|
2022-10-22 02:26:14 -05:00
|
|
|
}
|
|
|
|
|
2023-02-13 13:43:52 -05:00
|
|
|
if skipORASRefs {
|
2022-10-22 02:26:14 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
imageStore := sig.storeController.GetImageStore(localRepo)
|
|
|
|
|
2023-02-13 13:43:52 -05:00
|
|
|
sig.log.Info().Msg("syncing ORAS artifacts")
|
2022-03-07 03:45:10 -05:00
|
|
|
|
|
|
|
for _, ref := range referrers.References {
|
|
|
|
// get referrer manifest
|
2022-10-22 02:26:14 -05:00
|
|
|
getRefManifestURL := sig.upstreamURL
|
2022-03-07 03:45:10 -05:00
|
|
|
getRefManifestURL.Path = path.Join(getRefManifestURL.Path, "v2", remoteRepo, "manifests", ref.Digest.String())
|
|
|
|
getRefManifestURL.RawQuery = getRefManifestURL.Query().Encode()
|
|
|
|
|
2022-11-15 01:21:49 -05:00
|
|
|
var artifactManifest oras.Manifest
|
2022-03-07 03:45:10 -05:00
|
|
|
|
2022-12-22 13:19:42 -05:00
|
|
|
body, statusCode, err := common.MakeHTTPGetRequest(sig.client, sig.credentials.Username,
|
|
|
|
sig.credentials.Password, &artifactManifest,
|
|
|
|
getRefManifestURL.String(), ref.MediaType, sig.log)
|
2022-03-07 03:45:10 -05:00
|
|
|
if err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
if statusCode == http.StatusNotFound {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).Err(err).
|
|
|
|
Str("manifest", getRefManifestURL.String()).Msg("couldn't find any ORAS manifest")
|
2022-12-22 13:19:42 -05:00
|
|
|
|
|
|
|
return zerr.ErrSyncReferrerNotFound
|
|
|
|
}
|
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).Err(err).
|
|
|
|
Str("manifest", getRefManifestURL.String()).Msg("couldn't get ORAS manifest")
|
2022-03-07 03:45:10 -05:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-21 12:37:23 -05:00
|
|
|
for _, blob := range artifactManifest.Blobs {
|
2022-11-15 01:21:49 -05:00
|
|
|
if err := syncBlob(sig, imageStore, localRepo, remoteRepo, blob.Digest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = imageStore.PutImageManifest(localRepo, ref.Digest.String(),
|
2022-12-22 13:19:42 -05:00
|
|
|
oras.MediaTypeArtifactManifest, body)
|
2022-11-15 01:21:49 -05:00
|
|
|
if err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).
|
2023-02-13 13:43:52 -05:00
|
|
|
Err(err).Msg("couldn't upload ORAS manifest")
|
2022-11-15 01:21:49 -05:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2023-03-09 13:41:48 -05:00
|
|
|
|
|
|
|
// this is for notation signatures
|
|
|
|
if sig.repoDB != nil {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Debug().Str("repository", localRepo).Str("digest", digestStr).
|
|
|
|
Msg("trying to sync oras artifact for digest")
|
2023-03-09 13:41:48 -05:00
|
|
|
|
|
|
|
err = repodb.SetMetadataFromInput(localRepo, ref.Digest.String(), ref.MediaType,
|
|
|
|
ref.Digest, body, sig.storeController.GetImageStore(localRepo), sig.repoDB, sig.log)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to set metadata for oras artifact '%s@%s': %w", localRepo, digestStr, err)
|
|
|
|
}
|
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Info().Str("repository", localRepo).Str("digest", digestStr).
|
|
|
|
Msg("successfully added oras artifacts to RepoDB for digest")
|
2023-03-09 13:41:48 -05:00
|
|
|
}
|
2022-11-15 01:21:49 -05:00
|
|
|
}
|
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Info().Str("repository", localRepo).Str("digest", digestStr).
|
|
|
|
Msg("successfully synced ORAS artifacts for digest")
|
2022-11-15 01:21:49 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-07 03:45:10 -05:00
|
|
|
|
2022-11-15 01:21:49 -05:00
|
|
|
func (sig *signaturesCopier) syncOCIRefs(localRepo, remoteRepo, digestStr string, index ispec.Index,
|
|
|
|
) error {
|
|
|
|
if len(index.Manifests) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
skipOCIRefs, err := sig.canSkipOCIRefs(localRepo, digestStr, index)
|
|
|
|
if err != nil {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Error().Err(err).Str("repository", remoteRepo).Str("reference", digestStr).
|
|
|
|
Msg("couldn't check if the upstream image oci references can be skipped")
|
2022-11-15 01:21:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if skipOCIRefs {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
imageStore := sig.storeController.GetImageStore(localRepo)
|
|
|
|
|
2022-12-22 13:19:42 -05:00
|
|
|
sig.log.Info().Msg("syncing oci references")
|
2022-11-15 01:21:49 -05:00
|
|
|
|
|
|
|
for _, ref := range index.Manifests {
|
|
|
|
getRefManifestURL := sig.upstreamURL
|
|
|
|
getRefManifestURL.Path = path.Join(getRefManifestURL.Path, "v2", remoteRepo, "manifests", ref.Digest.String())
|
|
|
|
getRefManifestURL.RawQuery = getRefManifestURL.Query().Encode()
|
|
|
|
|
2022-12-22 13:19:42 -05:00
|
|
|
var artifactManifest oras.Manifest
|
|
|
|
|
2023-03-09 13:41:48 -05:00
|
|
|
OCIRefBody, statusCode, err := common.MakeHTTPGetRequest(sig.client, sig.credentials.Username,
|
2022-12-22 13:19:42 -05:00
|
|
|
sig.credentials.Password, &artifactManifest,
|
|
|
|
getRefManifestURL.String(), ref.MediaType, sig.log)
|
2022-11-15 01:21:49 -05:00
|
|
|
if err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
if statusCode == http.StatusNotFound {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).Err(err).
|
|
|
|
Str("manifest", getRefManifestURL.String()).Msg("couldn't find any oci reference manifest")
|
2022-12-22 13:19:42 -05:00
|
|
|
|
|
|
|
return zerr.ErrSyncReferrerNotFound
|
|
|
|
}
|
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).Err(err).
|
|
|
|
Str("manifest", getRefManifestURL.String()).Msg("couldn't get oci reference manifest")
|
2022-11-15 01:21:49 -05:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ref.MediaType == ispec.MediaTypeImageManifest {
|
|
|
|
// read manifest
|
|
|
|
var manifest ispec.Manifest
|
|
|
|
|
2023-03-09 13:41:48 -05:00
|
|
|
err = json.Unmarshal(OCIRefBody, &manifest)
|
2022-03-07 03:45:10 -05:00
|
|
|
if err != nil {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).Err(err).
|
|
|
|
Str("manifest", getRefManifestURL.String()).Msg("couldn't unmarshal oci reference manifest")
|
2022-03-07 03:45:10 -05:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-15 01:21:49 -05:00
|
|
|
for _, layer := range manifest.Layers {
|
|
|
|
if err := syncBlob(sig, imageStore, localRepo, remoteRepo, layer.Digest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-03-07 03:45:10 -05:00
|
|
|
|
2022-11-15 01:21:49 -05:00
|
|
|
// sync config blob
|
|
|
|
if err := syncBlob(sig, imageStore, localRepo, remoteRepo, manifest.Config.Digest); err != nil {
|
|
|
|
return err
|
2022-03-07 03:45:10 -05:00
|
|
|
}
|
2022-11-15 01:21:49 -05:00
|
|
|
} else if ref.MediaType == ispec.MediaTypeArtifactManifest {
|
|
|
|
// read manifest
|
|
|
|
var manifest ispec.Artifact
|
2022-03-07 03:45:10 -05:00
|
|
|
|
2023-03-09 13:41:48 -05:00
|
|
|
err = json.Unmarshal(OCIRefBody, &manifest)
|
2022-03-07 03:45:10 -05:00
|
|
|
if err != nil {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).Err(err).
|
|
|
|
Str("manifest", getRefManifestURL.String()).Msg("couldn't unmarshal oci reference manifest")
|
2022-03-07 03:45:10 -05:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2022-11-15 01:21:49 -05:00
|
|
|
|
|
|
|
for _, layer := range manifest.Blobs {
|
|
|
|
if err := syncBlob(sig, imageStore, localRepo, remoteRepo, layer.Digest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-03-07 03:45:10 -05:00
|
|
|
}
|
|
|
|
|
2023-03-09 13:41:48 -05:00
|
|
|
digest, err := imageStore.PutImageManifest(localRepo, ref.Digest.String(),
|
|
|
|
ref.MediaType, OCIRefBody)
|
2022-03-07 03:45:10 -05:00
|
|
|
if err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).
|
2022-11-15 01:21:49 -05:00
|
|
|
Err(err).Msg("couldn't upload oci reference manifest")
|
2022-03-07 03:45:10 -05:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2023-03-09 13:41:48 -05:00
|
|
|
|
|
|
|
if sig.repoDB != nil {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Debug().Str("repository", localRepo).Str("digest", digestStr).Msg("trying to add OCI refs for repo digest")
|
2023-03-09 13:41:48 -05:00
|
|
|
|
|
|
|
err = repodb.SetMetadataFromInput(localRepo, digestStr, ref.MediaType,
|
|
|
|
digest, OCIRefBody, sig.storeController.GetImageStore(localRepo),
|
|
|
|
sig.repoDB, sig.log)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to set metadata for OCI ref in '%s@%s': %w", localRepo, digestStr, err)
|
|
|
|
}
|
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Info().Str("repository", localRepo).Str("digest", digestStr).
|
|
|
|
Msg("successfully added OCI refs to RepoDB for digest")
|
2023-03-09 13:41:48 -05:00
|
|
|
}
|
2022-03-07 03:45:10 -05:00
|
|
|
}
|
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Info().Str("repository", localRepo).Str("digest", digestStr).
|
|
|
|
Msg("successfully synced OCI refs for digest")
|
2022-03-07 03:45:10 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-22 13:19:42 -05:00
|
|
|
func (sig *signaturesCopier) syncOCIArtifact(localRepo, remoteRepo, reference string,
|
|
|
|
ociArtifactBuf []byte,
|
|
|
|
) error {
|
|
|
|
var ociArtifact ispec.Artifact
|
|
|
|
|
|
|
|
err := json.Unmarshal(ociArtifactBuf, &ociArtifact)
|
|
|
|
if err != nil {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Error().Err(err).Str("repository", remoteRepo).Str("reference", reference).
|
|
|
|
Msg("couldn't unmarshal OCI artifact")
|
2022-12-22 13:19:42 -05:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
canSkipOCIArtifact, err := sig.canSkipOCIArtifact(localRepo, reference, ociArtifact)
|
|
|
|
if err != nil {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Error().Err(err).Str("repository", remoteRepo).Str("reference", reference).
|
|
|
|
Msg("couldn't check if OCI artifact can be skipped")
|
2022-12-22 13:19:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if canSkipOCIArtifact {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
imageStore := sig.storeController.GetImageStore(localRepo)
|
|
|
|
|
|
|
|
sig.log.Info().Msg("syncing OCI artifacts")
|
|
|
|
|
|
|
|
for _, blob := range ociArtifact.Blobs {
|
|
|
|
if err := syncBlob(sig, imageStore, localRepo, remoteRepo, blob.Digest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
artifactManifestBuf, err := json.Marshal(ociArtifact)
|
|
|
|
if err != nil {
|
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).
|
|
|
|
Err(err).Msg("couldn't marshal OCI artifact")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// push manifest
|
2023-03-09 13:41:48 -05:00
|
|
|
digest, err := imageStore.PutImageManifest(localRepo, reference,
|
2022-12-22 13:19:42 -05:00
|
|
|
ispec.MediaTypeArtifactManifest, artifactManifestBuf)
|
|
|
|
if err != nil {
|
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).
|
|
|
|
Err(err).Msg("couldn't upload OCI artifact manifest")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-09 13:41:48 -05:00
|
|
|
if sig.repoDB != nil {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Debug().Str("repository", localRepo).Str("digest", digest.String()).
|
|
|
|
Msg("trying to OCI refs for repo digest")
|
2023-03-09 13:41:48 -05:00
|
|
|
|
|
|
|
err = repodb.SetMetadataFromInput(localRepo, reference, ispec.MediaTypeArtifactManifest,
|
|
|
|
digest, artifactManifestBuf, sig.storeController.GetImageStore(localRepo),
|
|
|
|
sig.repoDB, sig.log)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to set metadata for OCI Artifact '%s@%s': %w", localRepo, digest.String(), err)
|
|
|
|
}
|
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Info().Str("repository", localRepo).Str("digest", digest.String()).
|
|
|
|
Msg("successfully added oci artifacts to RepoDB for repo digest")
|
2023-03-09 13:41:48 -05:00
|
|
|
}
|
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Info().Str("repository", localRepo).Str("tag", reference).
|
|
|
|
Msg("successfully synced OCI artifact for repo tag")
|
2022-12-22 13:19:42 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-13 13:43:52 -05:00
|
|
|
func (sig *signaturesCopier) canSkipORASRefs(localRepo, digestStr string, refs ReferenceList,
|
2022-03-21 12:37:23 -05:00
|
|
|
) (bool, error) {
|
2022-10-22 02:26:14 -05:00
|
|
|
imageStore := sig.storeController.GetImageStore(localRepo)
|
2022-10-22 15:46:13 -05:00
|
|
|
digest := godigest.Digest(digestStr)
|
2022-10-22 02:26:14 -05:00
|
|
|
|
2023-02-13 13:43:52 -05:00
|
|
|
// check oras artifacts already synced
|
2022-03-07 03:45:10 -05:00
|
|
|
if len(refs.References) > 0 {
|
2023-02-13 13:43:52 -05:00
|
|
|
localRefs, err := imageStore.GetOrasReferrers(localRepo, digest, "")
|
2022-03-07 03:45:10 -05:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, zerr.ErrManifestNotFound) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).Err(err).
|
|
|
|
Str("repository", localRepo).Str("reference", digestStr).Msg("couldn't get local ORAS artifact manifest")
|
2022-03-07 03:45:10 -05:00
|
|
|
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !artifactDescriptorsEqual(localRefs, refs.References) {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Info().Str("repository", localRepo).Str("reference", digestStr).
|
|
|
|
Msg("upstream ORAS artifacts changed, syncing again")
|
2022-03-07 03:45:10 -05:00
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Info().Str("repository", localRepo).Str("reference", digestStr).
|
|
|
|
Msg("skipping ORAS artifact, already synced")
|
2022-03-07 03:45:10 -05:00
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-12-09 14:38:00 -05:00
|
|
|
func (sig *signaturesCopier) canSkipOCIArtifact(localRepo, reference string, artifact ispec.Artifact,
|
|
|
|
) (bool, error) {
|
|
|
|
imageStore := sig.storeController.GetImageStore(localRepo)
|
|
|
|
|
|
|
|
var localArtifactManifest ispec.Artifact
|
|
|
|
|
|
|
|
localArtifactBuf, _, _, err := imageStore.GetImageManifest(localRepo, reference)
|
|
|
|
if err != nil {
|
2023-03-09 13:41:48 -05:00
|
|
|
if errors.Is(err, zerr.ErrManifestNotFound) || errors.Is(err, zerr.ErrRepoNotFound) {
|
2022-12-09 14:38:00 -05:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).Err(err).
|
|
|
|
Str("repository", localRepo).Str("reference", reference).
|
|
|
|
Msg("couldn't get local OCI artifact manifest")
|
2022-12-09 14:38:00 -05:00
|
|
|
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(localArtifactBuf, &localArtifactManifest)
|
|
|
|
if err != nil {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).Err(err).
|
|
|
|
Str("repository", localRepo).Str("reference", reference).
|
|
|
|
Msg("couldn't unmarshal local OCI artifact manifest")
|
2022-12-09 14:38:00 -05:00
|
|
|
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !artifactsEqual(localArtifactManifest, artifact) {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Info().Str("repository", localRepo).Str("reference", reference).
|
|
|
|
Msg("upstream OCI artifact changed, syncing again")
|
2022-12-09 14:38:00 -05:00
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Info().Str("repository", localRepo).Str("reference", reference).
|
|
|
|
Msg("skipping OCI artifact, already synced")
|
2022-12-09 14:38:00 -05:00
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
func (sig *signaturesCopier) canSkipCosignSignature(localRepo, digestStr string, cosignManifest *ispec.Manifest,
|
2022-03-21 12:37:23 -05:00
|
|
|
) (bool, error) {
|
2022-10-22 02:26:14 -05:00
|
|
|
imageStore := sig.storeController.GetImageStore(localRepo)
|
2022-03-07 03:45:10 -05:00
|
|
|
// check cosign signature already synced
|
|
|
|
if cosignManifest != nil {
|
|
|
|
var localCosignManifest ispec.Manifest
|
|
|
|
|
|
|
|
/* we need to use tag (cosign format: sha256-$IMAGE_TAG.sig) instead of digest to get local cosign manifest
|
|
|
|
because of an issue where cosign digests differs between upstream and downstream */
|
2022-10-22 15:46:13 -05:00
|
|
|
cosignManifestTag := getCosignTagFromImageDigest(digestStr)
|
2022-03-07 03:45:10 -05:00
|
|
|
|
2022-10-22 02:26:14 -05:00
|
|
|
localCosignManifestBuf, _, _, err := imageStore.GetImageManifest(localRepo, cosignManifestTag)
|
2022-03-07 03:45:10 -05:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, zerr.ErrManifestNotFound) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).Err(err).
|
|
|
|
Str("repository", localRepo).Str("reference", digestStr).
|
|
|
|
Msg("couldn't get local cosign manifest")
|
2022-03-07 03:45:10 -05:00
|
|
|
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(localCosignManifestBuf, &localCosignManifest)
|
|
|
|
if err != nil {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).Err(err).
|
|
|
|
Str("repository", localRepo).Str("reference", digestStr).
|
|
|
|
Msg("couldn't unmarshal local cosign signature manifest")
|
2022-03-07 03:45:10 -05:00
|
|
|
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !manifestsEqual(localCosignManifest, *cosignManifest) {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Info().Str("repository", localRepo).Str("reference", digestStr).
|
|
|
|
Msg("upstream cosign signatures changed, syncing again")
|
2022-03-07 03:45:10 -05:00
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Info().Str("repository", localRepo).Str("reference", digestStr).
|
|
|
|
Msg("skipping cosign signature, already synced")
|
2022-03-07 03:45:10 -05:00
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-11-15 01:21:49 -05:00
|
|
|
func (sig *signaturesCopier) canSkipOCIRefs(localRepo, digestStr string, index ispec.Index,
|
|
|
|
) (bool, error) {
|
|
|
|
imageStore := sig.storeController.GetImageStore(localRepo)
|
|
|
|
digest := godigest.Digest(digestStr)
|
|
|
|
|
|
|
|
// check oci references already synced
|
|
|
|
if len(index.Manifests) > 0 {
|
2023-01-26 13:13:12 -05:00
|
|
|
localRefs, err := imageStore.GetReferrers(localRepo, digest, nil)
|
2022-11-15 01:21:49 -05:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, zerr.ErrManifestNotFound) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).Err(err).
|
|
|
|
Str("repository", localRepo).Str("reference", digestStr).
|
|
|
|
Msg("couldn't get local ocireferences for manifest")
|
2022-11-15 01:21:49 -05:00
|
|
|
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !descriptorsEqual(localRefs.Manifests, index.Manifests) {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Info().Str("repository", localRepo).Str("reference", digestStr).
|
|
|
|
Msg("upstream oci references for manifest changed, syncing again")
|
2022-11-15 01:21:49 -05:00
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Info().Str("repository", localRepo).Str("reference", digestStr).
|
|
|
|
Msg("skipping oci references, already synced")
|
2022-11-15 01:21:49 -05:00
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func syncBlob(sig *signaturesCopier, imageStore storage.ImageStore, remoteRepo, localRepo string,
|
|
|
|
digest godigest.Digest,
|
|
|
|
) error {
|
|
|
|
getBlobURL := sig.upstreamURL
|
|
|
|
getBlobURL.Path = path.Join(getBlobURL.Path, "v2", remoteRepo, "blobs", digest.String())
|
|
|
|
getBlobURL.RawQuery = getBlobURL.Query().Encode()
|
|
|
|
|
2022-12-22 13:19:42 -05:00
|
|
|
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, getBlobURL.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := sig.client.Do(req)
|
2022-11-15 01:21:49 -05:00
|
|
|
if err != nil {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).Str("blob url", getBlobURL.String()).
|
|
|
|
Err(err).Msg("couldn't get blob from url")
|
2022-11-15 01:21:49 -05:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-22 13:19:42 -05:00
|
|
|
defer resp.Body.Close()
|
2022-11-15 01:21:49 -05:00
|
|
|
|
2022-12-22 13:19:42 -05:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
2023-04-27 21:44:22 -05:00
|
|
|
sig.log.Info().Str("url", getBlobURL.String()).Str("blob url", getBlobURL.String()).
|
|
|
|
Int("statusCode", resp.StatusCode).Msg("couldn't find blob from url, status code")
|
2022-11-15 01:21:49 -05:00
|
|
|
|
|
|
|
return zerr.ErrSyncReferrer
|
|
|
|
}
|
|
|
|
|
2022-12-22 13:19:42 -05:00
|
|
|
_, _, err = imageStore.FullBlobUpload(localRepo, resp.Body, digest)
|
2022-11-15 01:21:49 -05:00
|
|
|
if err != nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
sig.log.Error().Str("errorType", common.TypeOf(err)).Str("digest", digest.String()).
|
2022-11-15 01:21:49 -05:00
|
|
|
Err(err).Msg("couldn't upload blob")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-07 03:45:10 -05:00
|
|
|
// sync feature will try to pull cosign signature because for sync cosign signature is just an image
|
|
|
|
// this function will check if tag is a cosign tag.
|
|
|
|
func isCosignTag(tag string) bool {
|
2022-04-14 05:57:09 -05:00
|
|
|
if strings.HasPrefix(tag, "sha256-") && strings.HasSuffix(tag, remote.SignatureTagSuffix) {
|
2022-03-07 03:45:10 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
func getCosignTagFromImageDigest(digestStr string) string {
|
|
|
|
if !isCosignTag(digestStr) {
|
|
|
|
return strings.Replace(digestStr, ":", "-", 1) + "." + remote.SignatureTagSuffix
|
2022-03-07 03:45:10 -05:00
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
return digestStr
|
2022-03-07 03:45:10 -05:00
|
|
|
}
|
2023-02-13 13:43:52 -05:00
|
|
|
|
|
|
|
func getNotationManifestsFromOCIRefs(ociRefs ispec.Index) []ispec.Descriptor {
|
|
|
|
notaryManifests := []ispec.Descriptor{}
|
|
|
|
|
|
|
|
for _, ref := range ociRefs.Manifests {
|
|
|
|
if ref.ArtifactType == notreg.ArtifactTypeNotation {
|
|
|
|
notaryManifests = append(notaryManifests, ref)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return notaryManifests
|
|
|
|
}
|