mirror of
https://github.com/project-zot/zot.git
synced 2024-12-16 21:56:37 -05:00
af819e7b76
* refactor(repodb): moving common utilities under pkg/meta Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com> * refactor(repodb): moved update, version components under pkg/meta - updated wrapper initialization to recieve a log object in constructor Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com> * refactor(repodb): moved repodb initialization from controller to pkg/meta/repodb Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com> --------- Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com>
272 lines
6.3 KiB
Go
272 lines
6.3 KiB
Go
package common
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"time"
|
|
|
|
godigest "github.com/opencontainers/go-digest"
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
zerr "zotregistry.io/zot/errors"
|
|
"zotregistry.io/zot/pkg/meta/repodb"
|
|
)
|
|
|
|
func UpdateManifestMeta(repoMeta repodb.RepoMetadata, manifestDigest godigest.Digest,
|
|
manifestMeta repodb.ManifestMetadata,
|
|
) repodb.RepoMetadata {
|
|
updatedRepoMeta := repoMeta
|
|
|
|
updatedStatistics := repoMeta.Statistics[manifestDigest.String()]
|
|
updatedStatistics.DownloadCount = manifestMeta.DownloadCount
|
|
updatedRepoMeta.Statistics[manifestDigest.String()] = updatedStatistics
|
|
|
|
if manifestMeta.Signatures == nil {
|
|
manifestMeta.Signatures = repodb.ManifestSignatures{}
|
|
}
|
|
|
|
updatedRepoMeta.Signatures[manifestDigest.String()] = manifestMeta.Signatures
|
|
|
|
return updatedRepoMeta
|
|
}
|
|
|
|
func SignatureAlreadyExists(signatureSlice []repodb.SignatureInfo, sm repodb.SignatureMetadata) bool {
|
|
for _, sigInfo := range signatureSlice {
|
|
if sm.SignatureDigest == sigInfo.SignatureManifestDigest {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func ReferenceIsDigest(reference string) bool {
|
|
_, err := godigest.Parse(reference)
|
|
|
|
return err == nil
|
|
}
|
|
|
|
func ValidateRepoReferenceInput(repo, reference string, manifestDigest godigest.Digest) error {
|
|
if repo == "" {
|
|
return zerr.ErrEmptyRepoName
|
|
}
|
|
|
|
if reference == "" {
|
|
return zerr.ErrEmptyTag
|
|
}
|
|
|
|
if manifestDigest == "" {
|
|
return zerr.ErrEmptyDigest
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// These constants are meant used to describe how high or low in rank a match is.
|
|
// Note that the "higher rank" relates to a lower number so ranks are sorted in a
|
|
// ascending order.
|
|
const (
|
|
lowPriority = 100
|
|
mediumPriority = 10
|
|
highPriority = 1
|
|
perfectMatchPriority = 0
|
|
)
|
|
|
|
// RankRepoName associates a rank to a given repoName given a searchText.
|
|
// The imporance of the value grows inversly proportional to the int value it has.
|
|
// For example: rank(1) > rank(10) > rank(100)...
|
|
func RankRepoName(searchText string, repoName string) int {
|
|
searchText = strings.Trim(searchText, "/")
|
|
searchTextSlice := strings.Split(searchText, "/")
|
|
repoNameSlice := strings.Split(repoName, "/")
|
|
|
|
if len(searchTextSlice) > len(repoNameSlice) {
|
|
return -1
|
|
}
|
|
|
|
if searchText == repoName {
|
|
return perfectMatchPriority
|
|
}
|
|
|
|
// searchText containst just 1 diretory name
|
|
if len(searchTextSlice) == 1 {
|
|
lastNameInRepoPath := repoNameSlice[len(repoNameSlice)-1]
|
|
|
|
// searchText: "bar" | repoName: "foo/bar" lastNameInRepoPath: "bar"
|
|
if index := strings.Index(lastNameInRepoPath, searchText); index != -1 {
|
|
return (index + 1) * highPriority
|
|
}
|
|
|
|
firstNameInRepoPath := repoNameSlice[0]
|
|
|
|
// searchText: "foo" | repoName: "foo/bar" firstNameInRepoPath: "foo"
|
|
if index := strings.Index(firstNameInRepoPath, searchText); index != -1 {
|
|
return (index + 1) * mediumPriority
|
|
}
|
|
}
|
|
|
|
foundPrefixInRepoName := true
|
|
|
|
// searchText: "foo/bar/rep" | repoName: "foo/bar/baz/repo" foundPrefixInRepoName: true
|
|
// searchText: "foo/baz/rep" | repoName: "foo/bar/baz/repo" foundPrefixInRepoName: false
|
|
for i := 0; i < len(searchTextSlice)-1; i++ {
|
|
if searchTextSlice[i] != repoNameSlice[i] {
|
|
foundPrefixInRepoName = false
|
|
|
|
break
|
|
}
|
|
}
|
|
|
|
if foundPrefixInRepoName {
|
|
lastNameInRepoPath := repoNameSlice[len(repoNameSlice)-1]
|
|
lastNameInSearchText := searchTextSlice[len(searchTextSlice)-1]
|
|
|
|
// searchText: "foo/bar/epo" | repoName: "foo/bar/baz/repo" -> Index(repo, epo) = 1
|
|
if index := strings.Index(lastNameInRepoPath, lastNameInSearchText); index != -1 {
|
|
return (index + 1) * highPriority
|
|
}
|
|
}
|
|
|
|
// searchText: "foo/bar/b" | repoName: "foo/bar/baz/repo"
|
|
if strings.HasPrefix(repoName, searchText) {
|
|
return mediumPriority
|
|
}
|
|
|
|
// searchText: "bar/ba" | repoName: "foo/bar/baz/repo"
|
|
if index := strings.Index(repoName, searchText); index != -1 {
|
|
return (index + 1) * lowPriority
|
|
}
|
|
|
|
// no match
|
|
return -1
|
|
}
|
|
|
|
func GetImageLastUpdatedTimestamp(configContent ispec.Image) time.Time {
|
|
var timeStamp *time.Time
|
|
|
|
if configContent.Created != nil && !configContent.Created.IsZero() {
|
|
return *configContent.Created
|
|
}
|
|
|
|
if len(configContent.History) != 0 {
|
|
timeStamp = configContent.History[len(configContent.History)-1].Created
|
|
}
|
|
|
|
if timeStamp == nil {
|
|
timeStamp = &time.Time{}
|
|
}
|
|
|
|
return *timeStamp
|
|
}
|
|
|
|
func CheckIsSigned(signatures repodb.ManifestSignatures) bool {
|
|
for _, signatures := range signatures {
|
|
if len(signatures) > 0 {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func GetRepoTag(searchText string) (string, string, error) {
|
|
const repoTagCount = 2
|
|
|
|
splitSlice := strings.Split(searchText, ":")
|
|
|
|
if len(splitSlice) != repoTagCount {
|
|
return "", "", zerr.ErrInvalidRepoTagFormat
|
|
}
|
|
|
|
repo := strings.TrimSpace(splitSlice[0])
|
|
tag := strings.TrimSpace(splitSlice[1])
|
|
|
|
return repo, tag, nil
|
|
}
|
|
|
|
func GetMapKeys[K comparable, V any](genericMap map[K]V) []K {
|
|
keys := make([]K, 0, len(genericMap))
|
|
|
|
for k := range genericMap {
|
|
keys = append(keys, k)
|
|
}
|
|
|
|
return keys
|
|
}
|
|
|
|
// acceptedByFilter checks that data contains at least 1 element of each filter
|
|
// criteria(os, arch) present in filter.
|
|
func AcceptedByFilter(filter repodb.Filter, data repodb.FilterData) bool {
|
|
if filter.Arch != nil {
|
|
foundArch := false
|
|
for _, arch := range filter.Arch {
|
|
foundArch = foundArch || containsString(data.ArchList, *arch)
|
|
}
|
|
|
|
if !foundArch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
if filter.Os != nil {
|
|
foundOs := false
|
|
for _, os := range filter.Os {
|
|
foundOs = foundOs || containsString(data.OsList, *os)
|
|
}
|
|
|
|
if !foundOs {
|
|
return false
|
|
}
|
|
}
|
|
|
|
if filter.HasToBeSigned != nil && *filter.HasToBeSigned != data.IsSigned {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func containsString(strSlice []string, str string) bool {
|
|
for _, val := range strSlice {
|
|
if strings.EqualFold(val, str) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func GetReferredSubject(descriptorBlob []byte) (godigest.Digest, bool) {
|
|
var manifest ispec.Manifest
|
|
|
|
err := json.Unmarshal(descriptorBlob, &manifest)
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
|
|
if manifest.Subject == nil || manifest.Subject.Digest.String() == "" {
|
|
return "", false
|
|
}
|
|
|
|
return manifest.Subject.Digest, true
|
|
}
|
|
|
|
func MatchesArtifactTypes(descriptorMediaType string, artifactTypes []string) bool {
|
|
if len(artifactTypes) == 0 {
|
|
return true
|
|
}
|
|
|
|
found := false
|
|
|
|
for _, artifactType := range artifactTypes {
|
|
if artifactType != "" && descriptorMediaType != artifactType {
|
|
continue
|
|
}
|
|
|
|
found = true
|
|
|
|
break
|
|
}
|
|
|
|
return found
|
|
}
|