drone/registry/app/store/database.go
Arvind Choudhary 31ec4d1069 feat: [AH-231]: Upstream features (#2733)
* [AH-231]: Updated PR comments and some artifact/image issues
* [AH-231]: Cleanup extra table
* [AH-231]: Updated versions
* [AH-231]: Lint fixed
* [AH-231]: Merge commit
* [AH-231]: Updated logic to get child manifests
* [AH-231]: Updated sleep time
* [AH-231]: Completed implementation of manifest lists
* [AH-231]: Updated manifest list flows
* [AH-231]: Temp changes
* [AH-231]: Wiring fixed
* [AH-231]: Initial commit; minor fixes
* [AH-307]: Updated lint
* fix comment
* add new method to spacestore
* feat: [AH-307]: fix after rebase with main
* [AH-307]: Removing comments
* [AH-307]: linting fixes
* feat: [AH-286]: define proto, interface and no-op reporter implementation to publish artifact events (#2657)

* feat: [AH-286]: publish artifact event - no row found is not error
* feat: [AH-286]: publish artifact event - no row found is not error
* feat: [AH-286]: publish artifact event - lint errors, move publishing event outside DB transaction
* feat: [AH-286]: publish artifact event - review comments
* feat: [AH-286]: publish artifact event - address review comments
* feat: [AH-286]: publish artifact event - keep payload generic
* feat: [AH-286]: publish artifact event - as sqlite locks DB, perform db operation outside goroutine publishing of events
* feat: [AH-286]: publish artifact event - make publishing event async
* feat: [AH-286]: publish artifact event - use api types
* feat: [AH-286]: Publish event for SSCA to trigger scans - no need to export spacePathStore
* feat: [AH-286]: Publish event for SSCA to trigger scans - send spacePath instead of parentID
* feat: [AH-286]: Publish event for SSCA to trigger scans - rename scanner as generic reporter
* feat: [AH-286]: Publish event for SSCA to trigger scans - rename scanner as generic reporter
* feat: [AH-286]: publish artifact event - reuse redis.Send()
* feat: [AH-286]: Publish event for SSCA to trigger scans - review comments
* feat: [AH-286]: Publish event for SSCA to trigger scans - remove unused interface
* feat: [AH-286]: Publish event for SSCA to trigger scans - update msg format
* feat: [AH-286]: define proto, interface and no-op reporter implementation to publish artifact events
* feat: [AH-286]: Publish event for SSCA to trigger scans - extract acctID/orgID/projectID from spacepathStore
* feat: [AH-286]: publish artifact event - remove protobuf reference, fix lint errors
* feat: [AH-286]: publish artifact event - fix msg format
* feat: [AH-286]: define proto, interface and no-op reporter implementation to publish artifact events
* feat: [AH-286]: define proto, interface and no-op reporter implementation to publish artifact events
* feat: [AH-321]: make repo form disabled for rbac (#2687)

* feat: [AH-321]: make repo form disabled for rbac
* fix wire-gen
* GC refactoring
* feat: [AH-340]: update UI as per the product feedbacks (#2685)

* feat: [AH-340]: update UI as per the product feedbacks
* feat: [AH-44]: add module data while redirecting to pipeline execution page
* feat: [AH-44]: add build pipeline details in overview cards
* feat: [AH-44]: update view for prod and non prod tag
* feat: [AH-44]: rearrange filters on artifact list apge
* feat: [AH-10]: add schema for overview cards, update artifact list, add ai search input, update api for registry artifact list and update mapping for deployments table
* feat: [AH-307]: add secretSpacePath in upstream password field while sending to BE (#2631)

* feat: [AH-307]: add secretSpacePath in upstream password field while sending to BE
* feat: [AH-299]: support new changes for artifact list page (#2630)

* feat: update har service api version
* feat: [AH-30]: integrate API schema for deployments list content
* feat: [AH-300]: update tag colors for prod and non prod tags
* feat: [AH-300]: Add Deployments table in artiface version details page
* feat: [AH-299]: support new changes for artifact list page
* feat: [AH-299]: support new changes for artifact list page
* feat: [AH-321]: support artifact registry rbac permission on UI (#2671)

* feat: [AH-321]: support artifact registry rbac permission on UI
* enable rbac (#2664)

* fix scope
* enable rbac
* feat: [AH-307]: hide code tab from version details page for both docker and helm
* feat: [AH-240]: add custom handling for enterprise auth type field
* Merge branch 'AH-307-plus-url-support-2_no_rbac' of https://git0.harness.io/l7B_kbSEQD2wjrM7PShm5w/PROD/Harness_Commons/gitness into AH-307-plus-url-support-2_no_rbac
* feat: [AH-307]: send space_ref in query param while creating registries
* lowercase rootRef
* [AH-307]: updated route
* [AH-307]: Added logs
* [AH-307]: Added logs
* feat: [AH-317]: add space_ref query param
* local
* Merge commit
* Merge commit
* Merge commit
* Added comments
* Revert changes
* Merge commit
* Merge branch 'main' of https://git0.harness.io/l7B_kbSEQD2wjrM7PShm5w/PROD/Harness_Commons/gitness into AH-307-plus-url-support-2
* Merge branch 'AH-306d' of https://git0.harness.io/l7B_kbSEQD2wjrM7PShm5w/PROD/Harness_Commons/gitness into AH-307-plus-url-support-2
* fix space path handling
* Merge branch 'main' of https://git0.harness.io/l7B_kbSEQD2wjrM7PShm5w/PROD/Harness_Commons/gitness into AH-307-plus-url-support-2
* Updated URLs to support slashes with + separator
* fix: [AH-306c]: fix anonymous flow
* fix: [AH-306c]: fix anonymous flow
* feat: [AH-307]: plus url support on UI

(cherry picked from commit 3fb6add3ce03498b6668b5f8f6d547e1acedaec4)
* [AH-307]: Added examples

(cherry picked from commit e83e41303da536f421be333be04aed09fbf75f5f)
* [AH-307]: Added Regex request rewrite support

(cherry picked from commit ed7b155256bdcd1134bc228b5705556a1233add6)
* fix: [AH-306c]: fix anonymous flow
2024-09-25 05:08:26 +00:00

472 lines
14 KiB
Go

// Copyright 2023 Harness, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package store
import (
"context"
"time"
"github.com/harness/gitness/registry/app/api/openapi/contracts/artifact"
"github.com/harness/gitness/registry/types"
"github.com/lib/pq"
"github.com/opencontainers/go-digest"
)
type MediaTypesRepository interface {
MapMediaType(ctx context.Context, mediaType string) (int64, error)
MediaTypeExists(ctx context.Context, mediaType string) (bool, error)
}
type BlobRepository interface {
FindByID(ctx context.Context, id int64) (*types.Blob, error)
FindByDigestAndRootParentID(
ctx context.Context, d digest.Digest,
rootParentID int64,
) (*types.Blob, error)
FindByDigestAndRepoID(
ctx context.Context, d digest.Digest, repoID int64,
imageName string,
) (*types.Blob, error)
CreateOrFind(ctx context.Context, b *types.Blob) (*types.Blob, error)
DeleteByID(ctx context.Context, id int64) error
ExistsBlob(
ctx context.Context, repoID int64, d digest.Digest,
image string,
) (bool, error)
}
type CleanupPolicyRepository interface {
// GetIdsByRegistryId the CleanupPolicy Ids specified by Registry Key
GetIDsByRegistryID(ctx context.Context, id int64) (ids []int64, err error)
// GetByRegistryId the CleanupPolicy specified by Registry Key
GetByRegistryID(
ctx context.Context,
id int64,
) (cleanupPolicies *[]types.CleanupPolicy, err error)
// Create a CleanupPolicy
Create(
ctx context.Context,
cleanupPolicy *types.CleanupPolicy,
) (id int64, err error)
// Delete the CleanupPolicy specified by repokey and name
Delete(ctx context.Context, id int64) (err error)
// Update the CleanupPolicy.
ModifyCleanupPolicies(
ctx context.Context,
cleanupPolicies *[]types.CleanupPolicy, ids []int64,
) error
}
type ManifestRepository interface {
// FindAll finds all manifests.
FindAll(ctx context.Context) (types.Manifests, error)
// Count counts all manifests.
Count(ctx context.Context) (int, error)
// LayerBlobs finds layer blobs associated with a manifest,
// through the `layers` relationship entity.
LayerBlobs(ctx context.Context, m *types.Manifest) (types.Blobs, error)
// References finds all manifests directly
// referenced by a manifest (if any).
References(ctx context.Context, m *types.Manifest) (types.Manifests, error)
// Create saves a new Manifest. ID value is updated in given request object
Create(ctx context.Context, m *types.Manifest) error
// CreateOrFind attempts to create a manifest. If the manifest already exists
// (same digest in the scope of a given repository)
// that record is loaded from the database into m.
// This is similar to a repositoryStore.FindManifestByDigest followed by
// a Create, but without being prone to race conditions on write
// operations between the corresponding read (FindManifestByDigest2)
// and write (Create) operations.
// Separate Find* and Create method calls should be preferred
// to this when race conditions are not a concern.
CreateOrFind(ctx context.Context, m *types.Manifest) error
AssociateLayerBlob(ctx context.Context, m *types.Manifest, b *types.Blob) error
DissociateLayerBlob(ctx context.Context, m *types.Manifest, b *types.Blob) error
Delete(ctx context.Context, registryID, id int64) error
FindManifestByDigest(
ctx context.Context, repoID int64, imageName string,
digest types.Digest,
) (*types.Manifest, error)
FindManifestByTagName(
ctx context.Context, repoID int64, imageName string,
tag string,
) (*types.Manifest, error)
FindManifestPayloadByTagName(
ctx context.Context,
parentID int64,
repoKey string,
imageName string,
version string,
) (*types.Payload, error)
GetManifestPayload(
ctx context.Context,
parentID int64,
repoKey string,
imageName string,
digest types.Digest,
) (*types.Payload, error)
Get(ctx context.Context, manifestID int64) (*types.Manifest, error)
DeleteManifest(
ctx context.Context, repoID int64,
imageName string, d digest.Digest,
) (bool, error)
ListManifestsBySubject(
ctx context.Context, repoID int64,
id int64,
) (types.Manifests, error)
ListManifestsBySubjectDigest(
ctx context.Context, repoID int64,
digest types.Digest,
) (types.Manifests, error)
}
type ManifestReferenceRepository interface {
AssociateManifest(
ctx context.Context, ml *types.Manifest,
m *types.Manifest,
) error
DissociateManifest(
ctx context.Context, ml *types.Manifest,
m *types.Manifest,
) error
}
type OCIImageIndexMappingRepository interface {
Create(ctx context.Context, ociManifest *types.OCIImageIndexMapping) error
GetAllByChildDigest(ctx context.Context, registryID int64, imageName string, childDigest types.Digest) (
[]*types.OCIImageIndexMapping, error,
)
}
type LayerRepository interface {
AssociateLayerBlob(ctx context.Context, m *types.Manifest, b *types.Blob) error
}
type TagRepository interface {
// CreateOrUpdate upsert a tag. A tag with a given name
// on a given repository may not exist (in which case it should be
// inserted), already exist and point to the same manifest
// (in which case nothing needs to be done) or already exist but
// points to a different manifest (in which case it should be updated).
CreateOrUpdate(ctx context.Context, t *types.Tag) error
LockTagByNameForUpdate(
ctx context.Context, repoID int64,
name string,
) (bool, error)
DeleteTagByName(
ctx context.Context, repoID int64,
name string,
) (bool, error)
DeleteTagByManifestID(
ctx context.Context, repoID int64,
manifestID int64,
) (bool, error)
TagsPaginated(
ctx context.Context, repoID int64, image string,
filters types.FilterParams,
) ([]*types.Tag, error)
HasTagsAfterName(
ctx context.Context, repoID int64,
filters types.FilterParams,
) (bool, error)
GetAllArtifactsByParentID(
ctx context.Context, parentID int64,
registryIDs *[]string, sortByField string,
sortByOrder string, limit int, offset int, search string,
latestVersion bool,
) (*[]types.ArtifactMetadata, error)
CountAllArtifactsByParentID(
ctx context.Context, parentID int64,
registryIDs *[]string, search string,
latestVersion bool,
) (int64, error)
GetAllArtifactsByRepo(
ctx context.Context, parentID int64, repoKey string,
sortByField string, sortByOrder string,
limit int, offset int, search string, labels []string,
) (*[]types.ArtifactMetadata, error)
GetLatestTagMetadata(
ctx context.Context,
parentID int64,
repoKey string,
imageName string,
) (*types.ArtifactMetadata, error)
GetLatestTagName(
ctx context.Context, parentID int64, repoKey string,
imageName string,
) (string, error)
GetTagMetadata(
ctx context.Context,
parentID int64,
repoKey string,
imageName string,
name string,
) (*types.TagMetadata, error)
CountAllArtifactsByRepo(
ctx context.Context, parentID int64, repoKey string,
search string, labels []string,
) (int64, error)
GetTagDetail(
ctx context.Context, repoID int64, imageName string,
name string,
) (*types.TagDetail, error)
GetLatestTag(ctx context.Context, repoID int64, imageName string) (*types.Tag, error)
GetAllTagsByRepoAndImage(
ctx context.Context,
parentID int64,
repoKey string,
image string,
sortByField string,
sortByOrder string,
limit int,
offset int,
search string,
) (*[]types.TagMetadata, error)
CountAllTagsByRepoAndImage(
ctx context.Context, parentID int64, repoKey string,
image string, search string,
) (int64, error)
FindTag(
ctx context.Context, repoID int64, imageName string,
name string,
) (*types.Tag, error)
}
// UpstreamProxyConfig holds the record of a config of upstream proxy in DB.
type UpstreamProxyConfig struct {
ID int64
RegistryID int64
Source string
URL string
AuthType string
UserName string
Password string
Token string
CreatedAt time.Time
UpdatedAt time.Time
}
type UpstreamProxyConfigRepository interface {
// Get the upstreamproxy specified by ID
Get(ctx context.Context, id int64) (upstreamProxy *types.UpstreamProxy, err error)
// GetByRepoKey gets the upstreamproxy specified by registry key
GetByRegistryIdentifier(
ctx context.Context,
parentID int64,
repoKey string,
) (upstreamProxy *types.UpstreamProxy, err error)
// GetByParentUniqueId gets the upstreamproxy specified by parent id and parent unique id
GetByParentID(ctx context.Context, parentID string) (
upstreamProxies *[]types.UpstreamProxy,
err error,
)
// Create a upstreamProxyConfig
Create(ctx context.Context, upstreamproxyRecord *types.UpstreamProxyConfig) (
id int64,
err error,
)
// Delete the upstreamProxyConfig specified by registry key
Delete(ctx context.Context, parentID int64, repoKey string) (err error)
// Update updates the upstreamproxy.
Update(ctx context.Context, upstreamproxyRecord *types.UpstreamProxyConfig) (err error)
GetAll(
ctx context.Context,
parentID int64,
packageTypes []string,
sortByField string,
sortByOrder string,
limit int,
offset int,
search string,
) (upstreamProxies *[]types.UpstreamProxy, err error)
CountAll(
ctx context.Context, parentID string, packageTypes []string,
search string,
) (count int64, err error)
}
type RegistryMetadata struct {
RegIdentifier string
Description string
PackageType artifact.PackageType
Type artifact.RegistryType
LastModified time.Time
URL string
Labels pq.StringArray
ArtifactCount int64
DownloadCount int64
Size int64
}
type RegistryRepository interface {
// Get the repository specified by ID
Get(ctx context.Context, id int64) (repository *types.Registry, err error)
// GetByName gets the repository specified by name
GetByIDIn(
ctx context.Context, parentID int64,
ids []int64,
) (registries *[]types.Registry, err error)
// GetByName gets the repository specified by parent id and name
GetByParentIDAndName(
ctx context.Context, parentID int64,
name string,
) (registry *types.Registry, err error)
GetByRootParentIDAndName(
ctx context.Context, parentID int64,
name string,
) (registry *types.Registry, err error)
// Create a repository
Create(ctx context.Context, repository *types.Registry) (id int64, err error)
// Delete the repository specified by ID
Delete(ctx context.Context, parentID int64, name string) (err error)
// Update updates the repository. Only the properties specified by "props" will be updated if it is set
Update(ctx context.Context, repository *types.Registry) (err error)
GetAll(
ctx context.Context,
parentID int64,
packageTypes []string,
sortByField string,
sortByOrder string,
limit int,
offset int,
search string,
repoType string,
) (repos *[]RegistryMetadata, err error)
CountAll(
ctx context.Context, parentID int64, packageTypes []string,
search string, repoType string,
) (count int64, err error)
FetchUpstreamProxyIDs(
ctx context.Context, repokeys []string,
parentID int64,
) (ids []int64, err error)
FetchUpstreamProxyKeys(ctx context.Context, ids []int64) (repokeys []string, err error)
}
type RegistryBlobRepository interface {
LinkBlob(
ctx context.Context, imageName string,
registry *types.Registry, blobID int64,
) error
UnlinkBlob(
ctx context.Context, imageName string,
registry *types.Registry, blobID int64,
) (bool, error)
}
type ImageRepository interface {
// Get an Artifact specified by ID
Get(ctx context.Context, id int64) (*types.Image, error)
// Get an Artifact specified by Artifact Name
GetByName(
ctx context.Context, registryID int64,
name string,
) (*types.Image, error)
// Get the Labels specified by Parent ID and Repo
GetLabelsByParentIDAndRepo(
ctx context.Context, parentID int64,
repo string, limit int, offset int,
search string,
) (labels []string, err error)
// Count the Labels specified by Parent ID and Repo
CountLabelsByParentIDAndRepo(
ctx context.Context, parentID int64,
repo, search string,
) (count int64, err error)
// Get an Artifact specified by Artifact Name
GetByRepoAndName(
ctx context.Context, parentID int64,
repo string, name string,
) (*types.Image, error)
// Create an Artifact
CreateOrUpdate(ctx context.Context, image *types.Image) error
// Update an Artifact
Update(ctx context.Context, artifact *types.Image) (err error)
}
type ArtifactRepository interface {
// Get an Artifact specified by ID
GetByName(ctx context.Context, imageID int64, version string) (*types.Artifact, error)
// Create an Artifact
CreateOrUpdate(ctx context.Context, artifact *types.Artifact) error
}
type DownloadStatRepository interface {
Create(ctx context.Context, downloadStat *types.DownloadStat) error
}
type BandwidthStatRepository interface {
Create(ctx context.Context, bandwidthStat *types.BandwidthStat) error
}
type GCBlobTaskRepository interface {
FindAll(ctx context.Context) ([]*types.GCBlobTask, error)
FindAndLockBefore(
ctx context.Context, blobID int64,
date time.Time,
) (*types.GCBlobTask, error)
Count(ctx context.Context) (int, error)
Next(ctx context.Context) (*types.GCBlobTask, error)
Reschedule(ctx context.Context, b *types.GCBlobTask, d time.Duration) error
Postpone(ctx context.Context, b *types.GCBlobTask, d time.Duration) error
IsDangling(ctx context.Context, b *types.GCBlobTask) (bool, error)
Delete(ctx context.Context, b *types.GCBlobTask) error
}
type GCManifestTaskRepository interface {
FindAll(ctx context.Context) ([]*types.GCManifestTask, error)
FindAndLock(
ctx context.Context, registryID,
manifestID int64,
) (*types.GCManifestTask, error)
FindAndLockBefore(
ctx context.Context, registryID, manifestID int64,
date time.Time,
) (*types.GCManifestTask, error)
FindAndLockNBefore(
ctx context.Context, registryID int64,
manifestIDs []int64, date time.Time,
) ([]*types.GCManifestTask, error)
Count(ctx context.Context) (int, error)
Next(ctx context.Context) (*types.GCManifestTask, error)
Postpone(ctx context.Context, b *types.GCManifestTask, d time.Duration) error
IsDangling(ctx context.Context, b *types.GCManifestTask) (bool, error)
Delete(ctx context.Context, b *types.GCManifestTask) error
DeleteManifest(ctx context.Context, registryID, id int64) (*digest.Digest, error)
}