zotregistry.io/zot@v1.4.4-0.20231124084042-02a8ed785457/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 AcceptAllImageMeta(repoMeta RepoMeta, imageMeta ImageMeta) bool {
    42  	return true
    43  }
    44  
    45  func GetLatestImageDigests(repoMetaList []RepoMeta) []string {
    46  	digests := make([]string, 0, len(repoMetaList))
    47  
    48  	for i := range repoMetaList {
    49  		if repoMetaList[i].LastUpdatedImage != nil {
    50  			digests = append(digests, repoMetaList[i].LastUpdatedImage.Digest)
    51  		}
    52  	}
    53  
    54  	return digests
    55  }
    56  
    57  type (
    58  	ImageDigest = string
    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 information about all repositories as map[string]RepoMetadata filtered by the 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[string]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 ImageTrustStore interface {
   194  	VerifySignature(
   195  		signatureType string, rawSignature []byte, sigKey string, manifestDigest godigest.Digest, imageMeta ImageMeta,
   196  		repo string,
   197  	) (string, time.Time, bool, error)
   198  }
   199  
   200  // ImageMeta can store all data related to a image, multiarch or simple. Used for writing imaged to MetaDB.
   201  type ImageMeta struct {
   202  	MediaType string          // MediaType refers to the image descriptor, a manifest or a index (if multiarch)
   203  	Digest    godigest.Digest // Digest refers to the image descriptor, a manifest or a index (if multiarch)
   204  	Size      int64           // Size refers to the image descriptor, a manifest or a index (if multiarch)
   205  	Index     *ispec.Index    // If the image is multiarch the Index will be non-nil
   206  	Manifests []ManifestMeta  // All manifests under the image, 1 for simple images and many for multiarch
   207  }
   208  
   209  // ManifestMeta represents all data related to an image manifests (found from the image contents itself).
   210  type ManifestMeta struct {
   211  	Size     int64
   212  	Digest   godigest.Digest
   213  	Manifest ispec.Manifest
   214  	Config   ispec.Image
   215  }
   216  
   217  type RepoMeta struct {
   218  	Name string
   219  	Tags map[string]Descriptor
   220  
   221  	Statistics map[string]DescriptorStatistics
   222  	Signatures map[string]ManifestSignatures
   223  	Referrers  map[string][]ReferrerInfo
   224  
   225  	LastUpdatedImage *LastUpdatedImage
   226  	Platforms        []ispec.Platform
   227  	Vendors          []string
   228  	Size             int64
   229  
   230  	IsStarred    bool
   231  	IsBookmarked bool
   232  	Rank         int
   233  
   234  	StarCount     int
   235  	DownloadCount int
   236  }
   237  
   238  // FullImageMeta is a condensed structure of all information needed about an image when searching MetaDB.
   239  type FullImageMeta struct {
   240  	Repo         string
   241  	Tag          string
   242  	MediaType    string
   243  	Digest       godigest.Digest
   244  	Size         int64
   245  	Index        *ispec.Index
   246  	Manifests    []FullManifestMeta
   247  	IsStarred    bool
   248  	IsBookmarked bool
   249  
   250  	Referrers  []ReferrerInfo
   251  	Statistics DescriptorStatistics
   252  	Signatures ManifestSignatures
   253  }
   254  
   255  type FullManifestMeta struct {
   256  	ManifestMeta
   257  
   258  	Referrers  []ReferrerInfo
   259  	Statistics DescriptorStatistics
   260  	Signatures ManifestSignatures
   261  }
   262  
   263  type LastUpdatedImage struct {
   264  	Descriptor
   265  	Tag         string
   266  	LastUpdated *time.Time
   267  }
   268  
   269  type ReferrerInfo struct {
   270  	Digest       string
   271  	MediaType    string
   272  	ArtifactType string
   273  	Size         int
   274  	Annotations  map[string]string
   275  }
   276  
   277  // Descriptor represents an image. Multiple images might have the same digests but different tags.
   278  type Descriptor struct {
   279  	Digest    string
   280  	MediaType string
   281  }
   282  
   283  type DescriptorStatistics struct {
   284  	DownloadCount     int
   285  	LastPullTimestamp time.Time
   286  	PushTimestamp     time.Time
   287  	PushedBy          string
   288  }
   289  
   290  type ManifestSignatures map[string][]SignatureInfo
   291  
   292  type LayerInfo struct {
   293  	LayerDigest  string
   294  	LayerContent []byte
   295  	SignatureKey string
   296  	Signer       string
   297  	Date         time.Time
   298  }
   299  
   300  type SignatureInfo struct {
   301  	SignatureManifestDigest string
   302  	LayersInfo              []LayerInfo
   303  }
   304  
   305  type SignatureMetadata struct {
   306  	SignatureType   string
   307  	SignatureDigest string
   308  	SignatureTag    string
   309  	LayersInfo      []LayerInfo
   310  }
   311  
   312  type UserData struct {
   313  	StarredRepos    []string
   314  	BookmarkedRepos []string
   315  	Groups          []string
   316  	APIKeys         map[string]APIKeyDetails
   317  }
   318  
   319  type Filter struct {
   320  	Os            []*string
   321  	Arch          []*string
   322  	HasToBeSigned *bool
   323  	IsBookmarked  *bool
   324  	IsStarred     *bool
   325  }
   326  
   327  type FilterData struct {
   328  	DownloadCount int
   329  	LastUpdated   time.Time
   330  	OsList        []string
   331  	ArchList      []string
   332  	IsSigned      bool
   333  	IsStarred     bool
   334  	IsBookmarked  bool
   335  }
   336  
   337  type APIKeyDetails struct {
   338  	CreatedAt      time.Time `json:"createdAt"`
   339  	ExpirationDate time.Time `json:"expirationDate"`
   340  	IsExpired      bool      `json:"isExpired"`
   341  	CreatorUA      string    `json:"creatorUa"`
   342  	GeneratedBy    string    `json:"generatedBy"`
   343  	LastUsed       time.Time `json:"lastUsed"`
   344  	Label          string    `json:"label"`
   345  	Scopes         []string  `json:"scopes"`
   346  	UUID           string    `json:"uuid"`
   347  }