zotregistry.dev/zot@v1.4.4-0.20240314164342-eec277e14d20/pkg/meta/types/types.go (about)

     1  package types
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	godigest "github.com/opencontainers/go-digest"
     8  	ispec "github.com/opencontainers/image-spec/specs-go/v1"
     9  )
    10  
    11  // Used to model changes to an object after a call to the DB.
    12  type ToggleState int
    13  
    14  const (
    15  	NotChanged ToggleState = iota
    16  	Added
    17  	Removed
    18  )
    19  
    20  type (
    21  	// Currently imageMeta applied for indexes is applied for each manifest individually so imageMeta.manifests
    22  	// contains just 1 manifest.
    23  	FilterFunc         func(repoMeta RepoMeta, imageMeta ImageMeta) bool
    24  	FilterRepoNameFunc func(repo string) bool
    25  	FilterFullRepoFunc func(repoMeta RepoMeta) bool
    26  	FilterRepoTagFunc  func(repo, tag string) bool
    27  )
    28  
    29  func AcceptAllRepoNames(repo string) bool {
    30  	return true
    31  }
    32  
    33  func AcceptAllRepoMeta(repoMeta RepoMeta) bool {
    34  	return true
    35  }
    36  
    37  func AcceptAllRepoTag(repo, tag string) bool {
    38  	return true
    39  }
    40  
    41  func AcceptOnlyRepo(repo string) func(repo, tag string) bool {
    42  	return func(r, t string) bool { return repo == r }
    43  }
    44  
    45  func AcceptAllImageMeta(repoMeta RepoMeta, imageMeta ImageMeta) bool {
    46  	return true
    47  }
    48  
    49  func GetLatestImageDigests(repoMetaList []RepoMeta) []string {
    50  	digests := make([]string, 0, len(repoMetaList))
    51  
    52  	for i := range repoMetaList {
    53  		if repoMetaList[i].LastUpdatedImage != nil {
    54  			digests = append(digests, repoMetaList[i].LastUpdatedImage.Digest)
    55  		}
    56  	}
    57  
    58  	return digests
    59  }
    60  
    61  type MetaDB interface { //nolint:interfacebloat
    62  	UserDB
    63  
    64  	SetImageMeta(digest godigest.Digest, imageMeta ImageMeta) error
    65  
    66  	// SetRepoReference sets the given image data to the repo metadata.
    67  	SetRepoReference(ctx context.Context, repo string, reference string, imageMeta ImageMeta) error
    68  
    69  	// SearchRepos searches for repos given a search string
    70  	SearchRepos(ctx context.Context, searchText string) ([]RepoMeta, error)
    71  
    72  	// SearchTags searches for images(repo:tag) given a search string
    73  	SearchTags(ctx context.Context, searchText string) ([]FullImageMeta, error)
    74  
    75  	// FilterTags filters for images given a filter function
    76  	FilterTags(ctx context.Context, filterRepoTag FilterRepoTagFunc, filterFunc FilterFunc,
    77  	) ([]FullImageMeta, error)
    78  
    79  	// FilterRepos filters for repos given a filter function
    80  	FilterRepos(ctx context.Context, rankName FilterRepoNameFunc, filterFunc FilterFullRepoFunc,
    81  	) ([]RepoMeta, error)
    82  
    83  	// GetRepoMeta returns the full information about a repo
    84  	GetRepoMeta(ctx context.Context, repo string) (RepoMeta, error)
    85  
    86  	// GetFullImageMeta returns the full information about an image
    87  	GetFullImageMeta(ctx context.Context, repo string, tag string) (FullImageMeta, error)
    88  
    89  	// GetImageMeta returns the raw information about an image
    90  	GetImageMeta(digest godigest.Digest) (ImageMeta, error)
    91  
    92  	// GetMultipleRepoMeta returns a list of all repos that match the given filter.
    93  	// function
    94  	GetMultipleRepoMeta(ctx context.Context, filter func(repoMeta RepoMeta) bool) (
    95  		[]RepoMeta, error)
    96  
    97  	// AddManifestSignature adds signature metadata to a given manifest in the database
    98  	AddManifestSignature(repo string, signedManifestDigest godigest.Digest, sm SignatureMetadata) error
    99  
   100  	// DeleteSignature deletes signature metadata to a given manifest from the database
   101  	DeleteSignature(repo string, signedManifestDigest godigest.Digest, sigMeta SignatureMetadata) error
   102  
   103  	// UpdateSignaturesValidity checks and updates signatures validity of a given manifest
   104  	UpdateSignaturesValidity(ctx context.Context, repo string, manifestDigest godigest.Digest) error
   105  
   106  	// IncrementRepoStars adds 1 to the star count of an image
   107  	IncrementRepoStars(repo string) error
   108  
   109  	// DecrementRepoStars subtracts 1 from the star count of an image
   110  	DecrementRepoStars(repo string) error
   111  
   112  	// SetRepoMeta returns RepoMetadata of a repo from the database
   113  	SetRepoMeta(repo string, repoMeta RepoMeta) error
   114  
   115  	// DeleteRepoMeta
   116  	DeleteRepoMeta(repo string) error
   117  
   118  	// GetReferrersInfo returns a list of  for all referrers of the given digest that match one of the
   119  	// artifact types.
   120  	GetReferrersInfo(repo string, referredDigest godigest.Digest, artifactTypes []string) ([]ReferrerInfo, error)
   121  
   122  	// UpdateStatsOnDownload adds 1 to the download count of an image and sets the timestamp of download
   123  	UpdateStatsOnDownload(repo string, reference string) error
   124  
   125  	// FilterImageMeta returns the image data for the given digests
   126  	FilterImageMeta(ctx context.Context, digests []string) (map[ImageDigest]ImageMeta, error)
   127  
   128  	/*
   129  	   	RemoveRepoReference removes the tag from RepoMetadata if the reference is a tag,
   130  
   131  	   it also removes its corresponding digest from Statistics, Signatures and Referrers if there are no tags
   132  	   pointing to it.
   133  	   If the reference is a digest then it will remove the digest from Statistics, Signatures and Referrers only
   134  	   if there are no tags pointing to the digest, otherwise it's noop
   135  	*/
   136  	RemoveRepoReference(repo, reference string, manifestDigest godigest.Digest) error
   137  
   138  	// ResetRepoReferences resets all layout specific data (tags, signatures, referrers, etc.) but keep user and image
   139  	// specific metadata such as star count, downloads other statistics
   140  	ResetRepoReferences(repo string) error
   141  
   142  	GetRepoLastUpdated(repo string) time.Time
   143  
   144  	GetAllRepoNames() ([]string, error)
   145  
   146  	// ResetDB will delete all data in the DB
   147  	ResetDB() error
   148  
   149  	PatchDB() error
   150  
   151  	ImageTrustStore() ImageTrustStore
   152  
   153  	SetImageTrustStore(imgTrustStore ImageTrustStore)
   154  }
   155  
   156  type UserDB interface { //nolint:interfacebloat
   157  	// GetStarredRepos returns starred repos and takes current user in consideration
   158  	GetStarredRepos(ctx context.Context) ([]string, error)
   159  
   160  	// GetBookmarkedRepos returns bookmarked repos and takes current user in consideration
   161  	GetBookmarkedRepos(ctx context.Context) ([]string, error)
   162  
   163  	// ToggleStarRepo adds/removes stars on repos
   164  	ToggleStarRepo(ctx context.Context, reponame string) (ToggleState, error)
   165  
   166  	// ToggleBookmarkRepo adds/removes bookmarks on repos
   167  	ToggleBookmarkRepo(ctx context.Context, reponame string) (ToggleState, error)
   168  
   169  	// UserDB profile/api key CRUD
   170  	GetUserData(ctx context.Context) (UserData, error)
   171  
   172  	SetUserData(ctx context.Context, userData UserData) error
   173  
   174  	SetUserGroups(ctx context.Context, groups []string) error
   175  
   176  	GetUserGroups(ctx context.Context) ([]string, error)
   177  
   178  	DeleteUserData(ctx context.Context) error
   179  
   180  	GetUserAPIKeyInfo(hashedKey string) (identity string, err error)
   181  
   182  	GetUserAPIKeys(ctx context.Context) ([]APIKeyDetails, error)
   183  
   184  	AddUserAPIKey(ctx context.Context, hashedKey string, apiKeyDetails *APIKeyDetails) error
   185  
   186  	IsAPIKeyExpired(ctx context.Context, hashedKey string) (bool, error)
   187  
   188  	UpdateUserAPIKeyLastUsed(ctx context.Context, hashedKey string) error
   189  
   190  	DeleteUserAPIKey(ctx context.Context, id string) error
   191  }
   192  
   193  type (
   194  	Author     = string
   195  	ExpiryDate = time.Time
   196  	Validity   = bool
   197  )
   198  
   199  type ImageTrustStore interface {
   200  	VerifySignature(
   201  		signatureType string, rawSignature []byte, sigKey string, manifestDigest godigest.Digest, imageMeta ImageMeta,
   202  		repo string,
   203  	) (Author, ExpiryDate, Validity, error)
   204  }
   205  
   206  // ImageMeta can store all data related to a image, multiarch or simple. Used for writing imaged to MetaDB.
   207  type ImageMeta struct {
   208  	MediaType string          // MediaType refers to the image descriptor, a manifest or a index (if multiarch)
   209  	Digest    godigest.Digest // Digest refers to the image descriptor, a manifest or a index (if multiarch)
   210  	Size      int64           // Size refers to the image descriptor, a manifest or a index (if multiarch)
   211  	Index     *ispec.Index    // If the image is multiarch the Index will be non-nil
   212  	Manifests []ManifestMeta  // All manifests under the image, 1 for simple images and many for multiarch
   213  }
   214  
   215  // ManifestMeta represents all data related to an image manifests (found from the image contents itself).
   216  type ManifestMeta struct {
   217  	Size     int64
   218  	Digest   godigest.Digest
   219  	Manifest ispec.Manifest
   220  	Config   ispec.Image
   221  }
   222  
   223  type (
   224  	Tag         = string
   225  	ImageDigest = string
   226  )
   227  
   228  type RepoMeta struct {
   229  	Name string
   230  	Tags map[Tag]Descriptor
   231  
   232  	Statistics map[ImageDigest]DescriptorStatistics
   233  	Signatures map[ImageDigest]ManifestSignatures
   234  	Referrers  map[ImageDigest][]ReferrerInfo
   235  
   236  	LastUpdatedImage *LastUpdatedImage
   237  	Platforms        []ispec.Platform
   238  	Vendors          []string
   239  	Size             int64
   240  
   241  	IsStarred    bool
   242  	IsBookmarked bool
   243  	Rank         int
   244  
   245  	StarCount     int
   246  	DownloadCount int
   247  }
   248  
   249  // FullImageMeta is a condensed structure of all information needed about an image when searching MetaDB.
   250  type FullImageMeta struct {
   251  	Repo         string
   252  	Tag          string
   253  	MediaType    string
   254  	Digest       godigest.Digest
   255  	Size         int64
   256  	Index        *ispec.Index
   257  	Manifests    []FullManifestMeta
   258  	IsStarred    bool
   259  	IsBookmarked bool
   260  
   261  	Referrers  []ReferrerInfo
   262  	Statistics DescriptorStatistics
   263  	Signatures ManifestSignatures
   264  }
   265  
   266  type FullManifestMeta struct {
   267  	ManifestMeta
   268  
   269  	Referrers  []ReferrerInfo
   270  	Statistics DescriptorStatistics
   271  	Signatures ManifestSignatures
   272  }
   273  
   274  type LastUpdatedImage struct {
   275  	Descriptor
   276  	Tag         string
   277  	LastUpdated *time.Time
   278  }
   279  
   280  type ReferrerInfo struct {
   281  	Digest       string
   282  	MediaType    string
   283  	ArtifactType string
   284  	Size         int
   285  	Annotations  map[string]string
   286  }
   287  
   288  // Descriptor represents an image. Multiple images might have the same digests but different tags.
   289  type Descriptor struct {
   290  	Digest    string
   291  	MediaType string
   292  }
   293  
   294  type DescriptorStatistics struct {
   295  	DownloadCount     int
   296  	LastPullTimestamp time.Time
   297  	PushTimestamp     time.Time
   298  	PushedBy          string
   299  }
   300  
   301  type (
   302  	SignatureType = string
   303  )
   304  
   305  type ManifestSignatures map[SignatureType][]SignatureInfo
   306  
   307  type LayerInfo struct {
   308  	LayerDigest  string
   309  	LayerContent []byte
   310  	SignatureKey string
   311  	Signer       string
   312  	Date         time.Time
   313  }
   314  
   315  type SignatureInfo struct {
   316  	SignatureManifestDigest string
   317  	LayersInfo              []LayerInfo
   318  }
   319  
   320  type SignatureMetadata struct {
   321  	SignatureType   string
   322  	SignatureDigest string
   323  	SignatureTag    string
   324  	LayersInfo      []LayerInfo
   325  }
   326  
   327  type (
   328  	HashedAPIKey = string
   329  )
   330  
   331  type UserData struct {
   332  	StarredRepos    []string
   333  	BookmarkedRepos []string
   334  	Groups          []string
   335  	APIKeys         map[HashedAPIKey]APIKeyDetails
   336  }
   337  
   338  type Filter struct {
   339  	Os            []*string
   340  	Arch          []*string
   341  	HasToBeSigned *bool
   342  	IsBookmarked  *bool
   343  	IsStarred     *bool
   344  }
   345  
   346  type FilterData struct {
   347  	DownloadCount int
   348  	LastUpdated   time.Time
   349  	OsList        []string
   350  	ArchList      []string
   351  	IsSigned      bool
   352  	IsStarred     bool
   353  	IsBookmarked  bool
   354  }
   355  
   356  type APIKeyDetails struct {
   357  	CreatedAt      time.Time `json:"createdAt"`
   358  	ExpirationDate time.Time `json:"expirationDate"`
   359  	IsExpired      bool      `json:"isExpired"`
   360  	CreatorUA      string    `json:"creatorUa"`
   361  	GeneratedBy    string    `json:"generatedBy"`
   362  	LastUsed       time.Time `json:"lastUsed"`
   363  	Label          string    `json:"label"`
   364  	Scopes         []string  `json:"scopes"`
   365  	UUID           string    `json:"uuid"`
   366  }