mirror of
https://github.com/harness/drone.git
synced 2025-05-03 12:12:22 +08:00

* fix * Merge branch 'main' into AH-307-plus-url-support-2_no_rbac * resolve PR comments * resolve PR comments * resolve PR comments * feat: [AH-346]: new api changes for version list and digest list (#2726) * feat: [AH-346]: new api changes for version list and digest list * resolve pr comments * resolve pr comments * feat: [AH-346]: new api yaml integration (#2716) * feat: [AH-346]: new api yaml integration * Merge branch 'main' of https://git0.harness.io/l7B_kbSEQD2wjrM7PShm5w/PROD/Harness_Commons/gitness into AH-307-plus-url-support-2_no_rbac * fix wire check * fix lint issues * fix: [AH-357]: migrations * changes for global artifact listing (#2708) * changes for global artifact listing * Merge branch 'main' into AH-307-plus-url-support-2_no_rbac * merged main * Merge branch 'main' into AH-307-plus-url-support-2_no_rbac * [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
410 lines
10 KiB
Go
410 lines
10 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 metadata
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
"net/url"
|
|
"path"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
api "github.com/harness/gitness/registry/app/api/openapi/contracts/artifact"
|
|
"github.com/harness/gitness/registry/app/pkg/commons"
|
|
"github.com/harness/gitness/types"
|
|
"github.com/harness/gitness/types/enum"
|
|
|
|
"github.com/dustin/go-humanize"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
var registrySort = []string{
|
|
"identifier",
|
|
"lastModified",
|
|
"registrySize",
|
|
"artifactsCount",
|
|
"downloadsCount",
|
|
}
|
|
|
|
const (
|
|
RepositoryResource = "repository"
|
|
ArtifactResource = "artifact"
|
|
ArtifactVersionResource = "artifactversion"
|
|
RegistryIdentifierErrorMsg = "registry name should be 1~255 characters long with lower case characters, numbers " +
|
|
"and ._- and must be start with numbers or characters"
|
|
RegexIdentifierPattern = "^[a-z0-9]+(?:[._-][a-z0-9]+)*$"
|
|
)
|
|
|
|
var RegistrySortMap = map[string]string{
|
|
"identifier": "name",
|
|
"lastModified": "updated_at",
|
|
"registrySize": "size",
|
|
"artifactsCount": "artifact_count",
|
|
"downloadsCount": "download_count",
|
|
"createdAt": "created_at",
|
|
}
|
|
|
|
var artifactSort = []string{
|
|
"repoKey",
|
|
"name",
|
|
"lastModified",
|
|
"downloadsCount",
|
|
}
|
|
|
|
var artifactSortMap = map[string]string{
|
|
"repoKey": "name",
|
|
"lastModified": "updated_at",
|
|
"name": "image_name",
|
|
"downloadsCount": "image_name",
|
|
"createdAt": "created_at",
|
|
}
|
|
|
|
var artifactVersionSort = []string{
|
|
"name",
|
|
"size",
|
|
"pullCommand",
|
|
"downloadsCount",
|
|
"lastModified",
|
|
}
|
|
|
|
var artifactVersionSortMap = map[string]string{
|
|
"name": "name",
|
|
"size": "name",
|
|
"pullCommand": "name",
|
|
"downloadsCount": "name",
|
|
"lastModified": "updated_at",
|
|
"createdAt": "created_at",
|
|
}
|
|
|
|
var validRepositoryTypes = []string{
|
|
string(api.RegistryTypeUPSTREAM),
|
|
string(api.RegistryTypeVIRTUAL),
|
|
}
|
|
|
|
var validPackageTypes = []string{
|
|
string(api.PackageTypeDOCKER),
|
|
string(api.PackageTypeHELM),
|
|
string(api.PackageTypeMAVEN),
|
|
}
|
|
|
|
var validUpstreamSources = []string{
|
|
string(api.UpstreamConfigSourceCustom),
|
|
string(api.UpstreamConfigSourceDockerhub),
|
|
}
|
|
|
|
func ValidatePackageTypes(packageTypes []string) error {
|
|
if commons.IsEmpty(packageTypes) || IsPackageTypesValid(packageTypes) {
|
|
return nil
|
|
}
|
|
return errors.New("invalid package type")
|
|
}
|
|
|
|
func ValidatePackageType(packageType string) error {
|
|
if len(packageType) == 0 || IsPackageTypeValid(packageType) {
|
|
return nil
|
|
}
|
|
return errors.New("invalid package type")
|
|
}
|
|
|
|
func ValidatePackageTypeChange(fromDB, newPackage string) error {
|
|
if len(fromDB) > 0 && len(newPackage) > 0 && fromDB == newPackage {
|
|
return nil
|
|
}
|
|
return errors.New("package type change is not allowed")
|
|
}
|
|
|
|
func ValidateRepoTypeChange(fromDB, newRepo string) error {
|
|
if len(fromDB) > 0 && len(newRepo) > 0 && fromDB == newRepo {
|
|
return nil
|
|
}
|
|
return errors.New("registry type change is not allowed")
|
|
}
|
|
|
|
func ValidateIdentifierChange(fromDB, newIdentifier string) error {
|
|
if len(fromDB) > 0 && len(newIdentifier) > 0 && fromDB == newIdentifier {
|
|
return nil
|
|
}
|
|
return errors.New("registry identifier change is not allowed")
|
|
}
|
|
|
|
func ValidateIdentifier(identifier string) error {
|
|
if len(identifier) == 0 {
|
|
return errors.New(RegistryIdentifierErrorMsg)
|
|
}
|
|
|
|
matched, err := regexp.MatchString(RegexIdentifierPattern, identifier)
|
|
if err != nil || !matched {
|
|
return errors.New(RegistryIdentifierErrorMsg)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ValidateUpstream(config *api.RegistryConfig) error {
|
|
upstreamConfig, err := config.AsUpstreamConfig()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !commons.IsEmpty(config.Type) && config.Type == api.RegistryTypeUPSTREAM &&
|
|
*upstreamConfig.Source != api.UpstreamConfigSourceDockerhub {
|
|
if commons.IsEmpty(upstreamConfig.Url) {
|
|
return errors.New("URL is required for upstream repository")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ValidateRepoType(repoType string) error {
|
|
if len(repoType) == 0 || IsRepoTypeValid(repoType) {
|
|
return nil
|
|
}
|
|
return errors.New("invalid repository type")
|
|
}
|
|
|
|
func ValidateUpstreamSource(source string) error {
|
|
if len(source) == 0 || IsUpstreamSourceValid(source) {
|
|
return nil
|
|
}
|
|
return errors.New("invalid upstream proxy source")
|
|
}
|
|
|
|
func IsRepoTypeValid(repoType string) bool {
|
|
for _, item := range validRepositoryTypes {
|
|
if item == repoType {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func IsUpstreamSourceValid(source string) bool {
|
|
for _, item := range validUpstreamSources {
|
|
if item == source {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func IsPackageTypeValid(packageType string) bool {
|
|
for _, item := range validPackageTypes {
|
|
if item == packageType {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func IsPackageTypesValid(packageTypes []string) bool {
|
|
for _, item := range packageTypes {
|
|
if !IsPackageTypeValid(item) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func GetTimeInMs(t time.Time) string {
|
|
return fmt.Sprint(t.UnixMilli())
|
|
}
|
|
|
|
func GetErrorResponse(code int, message string) *api.Error {
|
|
return &api.Error{
|
|
Code: fmt.Sprint(code),
|
|
Message: message,
|
|
}
|
|
}
|
|
|
|
func GetSortByOrder(sortOrder string) string {
|
|
defaultSortOrder := "ASC"
|
|
decreasingSortOrder := "DESC"
|
|
if len(sortOrder) == 0 {
|
|
return defaultSortOrder
|
|
}
|
|
if sortOrder == decreasingSortOrder {
|
|
return decreasingSortOrder
|
|
}
|
|
return defaultSortOrder
|
|
}
|
|
|
|
func sortKey(slice []string, target string) string {
|
|
for _, item := range slice {
|
|
if item == target {
|
|
return item
|
|
}
|
|
}
|
|
return "createdAt"
|
|
}
|
|
|
|
func GetSortByField(sortByField string, resource string) string {
|
|
switch resource {
|
|
case RepositoryResource:
|
|
sortkey := sortKey(registrySort, sortByField)
|
|
return RegistrySortMap[sortkey]
|
|
case ArtifactResource:
|
|
sortkey := sortKey(artifactSort, sortByField)
|
|
return artifactSortMap[sortkey]
|
|
case ArtifactVersionResource:
|
|
sortkey := sortKey(artifactVersionSort, sortByField)
|
|
return artifactVersionSortMap[sortkey]
|
|
}
|
|
return "created_at"
|
|
}
|
|
|
|
func GetPageLimit(pageSize *api.PageSize) int {
|
|
defaultPageSize := 10
|
|
if pageSize != nil {
|
|
return int(*pageSize)
|
|
}
|
|
return defaultPageSize
|
|
}
|
|
|
|
func GetOffset(pageSize *api.PageSize, pageNumber *api.PageNumber) int {
|
|
defaultOffset := 0
|
|
if pageSize == nil || pageNumber == nil {
|
|
return defaultOffset
|
|
}
|
|
if *pageNumber == 0 {
|
|
return 0
|
|
}
|
|
return (int(*pageSize)) * int(*pageNumber)
|
|
}
|
|
|
|
func GetPageNumber(pageNumber *api.PageNumber) int64 {
|
|
defaultPageNumber := int64(1)
|
|
if pageNumber == nil {
|
|
return defaultPageNumber
|
|
}
|
|
return int64(*pageNumber)
|
|
}
|
|
|
|
func GetSuccessResponse() *api.Success {
|
|
return &api.Success{
|
|
Status: api.StatusSUCCESS,
|
|
}
|
|
}
|
|
|
|
func GetPageCount(count int64, pageSize int) int64 {
|
|
return int64(math.Ceil(float64(count) / float64(pageSize)))
|
|
}
|
|
|
|
func GetImageSize(size string) string {
|
|
sizeVal, _ := strconv.ParseInt(size, 10, 64)
|
|
return GetSize(sizeVal)
|
|
}
|
|
|
|
func GetSize(sizeVal int64) string {
|
|
humanReadable := humanize.Bytes(uint64(sizeVal))
|
|
return humanReadable
|
|
}
|
|
|
|
func GetRegRef(parentRef string, regIdentifier string) (string, error) {
|
|
result := ""
|
|
if commons.IsEmpty(parentRef) || commons.IsEmpty(regIdentifier) {
|
|
return result, errors.New("parentRef or regIdentifier is empty")
|
|
}
|
|
return parentRef + "/" + regIdentifier, nil
|
|
}
|
|
|
|
func GetRepoURL(rootIdentifier, registry string, registryURL string) string {
|
|
parsedURL, err := url.Parse(registryURL)
|
|
if err != nil {
|
|
log.Error().Err(err).Msgf("Error parsing URL: %s", registryURL)
|
|
return ""
|
|
}
|
|
parsedURL.Path = path.Join(parsedURL.Path, strings.ToLower(rootIdentifier), registry)
|
|
return parsedURL.String()
|
|
}
|
|
|
|
func GetRepoURLWithoutProtocol(rootIdentifier string, registry string, registryURL string) string {
|
|
repoURL := GetRepoURL(rootIdentifier, registry, registryURL)
|
|
parsedURL, err := url.Parse(repoURL)
|
|
if err != nil {
|
|
log.Error().Stack().Err(err).Msg("Error parsing URL: ")
|
|
return ""
|
|
}
|
|
|
|
return parsedURL.Host + parsedURL.Path
|
|
}
|
|
|
|
func GetTagURL(rootIdentifier string, artifact string, version string, registry string, registryURL string) string {
|
|
url := GetRepoURL(rootIdentifier, registry, registryURL)
|
|
url += "/" + artifact + "/"
|
|
url += version
|
|
return url
|
|
}
|
|
|
|
func GetPullCommand(
|
|
rootIdentifier string, registry string, image string, tag string,
|
|
packageType string, registryURL string,
|
|
) string {
|
|
if packageType == "DOCKER" {
|
|
return GetDockerPullCommand(rootIdentifier, registry, image, tag, registryURL)
|
|
} else if packageType == "HELM" {
|
|
return GetHelmPullCommand(rootIdentifier, registry, image, tag, registryURL)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func GetDockerPullCommand(
|
|
rootIdentifier string, registry string, image string,
|
|
tag string, registryURL string,
|
|
) string {
|
|
return "docker pull " + GetRepoURLWithoutProtocol(rootIdentifier, registry, registryURL) + "/" + image + ":" + tag
|
|
}
|
|
|
|
func GetHelmPullCommand(rootIdentifier string, registry string, image string, tag string, registryURL string) string {
|
|
return "helm install " + GetRepoURLWithoutProtocol(rootIdentifier, registry, registryURL) + "/" + image + ":" + tag
|
|
}
|
|
|
|
// CleanURLPath removes leading and trailing spaces and trailing slashes from the given URL string.
|
|
func CleanURLPath(input *string) {
|
|
if input == nil {
|
|
return
|
|
}
|
|
// Parse the input to URL
|
|
u, err := url.Parse(*input)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// Clean the path by removing trailing slashes and spaces
|
|
cleanedPath := strings.TrimRight(strings.TrimSpace(u.Path), "/")
|
|
|
|
// Update the URL path in the original input string
|
|
u.Path = cleanedPath
|
|
|
|
// Update the input string with the cleaned URL string representation
|
|
*input = u.String()
|
|
}
|
|
|
|
func GetPermissionChecks(
|
|
space *types.Space,
|
|
registryIdentifier string,
|
|
permission enum.Permission,
|
|
) []types.PermissionCheck {
|
|
var permissionChecks []types.PermissionCheck
|
|
permissionCheck := &types.PermissionCheck{
|
|
Scope: types.Scope{SpacePath: space.Path},
|
|
Resource: types.Resource{Type: enum.ResourceTypeRegistry, Identifier: registryIdentifier},
|
|
Permission: permission,
|
|
}
|
|
permissionChecks = append(permissionChecks, *permissionCheck)
|
|
return permissionChecks
|
|
}
|