From 2ec6fb8a588ece80c6eaa644f06da8c96d1152bc Mon Sep 17 00:00:00 2001 From: Pragyesh Mishra Date: Fri, 2 May 2025 18:54:52 +0000 Subject: [PATCH] [fix]: [AH-952]: fix override of files for maven artifacts (#3692) * [fix]: [AH-952]: Fixed pr check * [fix]: [AH-952]: Fixed pr check * [fix]: [AH-952]: Fixed pr check * [fix]: [AH-952]: Fixed pr check * [fix]: [AH-952]: Fixed pr check * [fix]: [AH-952]: Fixed pr check * [fix]: [AH-952]: fix override of files for maven artifacts --- cmd/gitness/wire_gen.go | 6 ++--- registry/app/api/handler/maven/base.go | 28 +++++++++++--------- registry/app/pkg/base/base.go | 7 +++++ registry/app/pkg/context.go | 14 +++++----- registry/app/pkg/maven/local.go | 24 +++++++++++++++-- registry/app/pkg/maven/wire.go | 5 +++- registry/app/pkg/python/local_helper_test.go | 5 ++++ 7 files changed, 63 insertions(+), 26 deletions(-) diff --git a/cmd/gitness/wire_gen.go b/cmd/gitness/wire_gen.go index 00f91cc8c..55400f2c0 100644 --- a/cmd/gitness/wire_gen.go +++ b/cmd/gitness/wire_gen.go @@ -528,8 +528,10 @@ func initSystem(ctx context.Context, config *types.Config) (*server.System, erro registryHelper := rpm.LocalRegistryHelperProvider(fileManager, artifactRepository) indexService := index.ProvideService(registryHelper, lockerLocker) apiHandler := router.APIHandlerProvider(registryRepository, upstreamProxyConfigRepository, fileManager, tagRepository, manifestRepository, cleanupPolicyRepository, imageRepository, storageDriver, spaceFinder, transactor, authenticator, provider, authorizer, auditService, artifactRepository, webhooksRepository, webhooksExecutionRepository, service2, spacePathStore, reporter10, downloadStatRepository, indexService, config, registryBlobRepository) + packageTagRepository := database2.ProvidePackageTagDao(db) + localBase := base.LocalBaseProvider(registryRepository, fileManager, transactor, imageRepository, artifactRepository, nodesRepository, packageTagRepository) mavenDBStore := maven.DBStoreProvider(registryRepository, imageRepository, artifactRepository, spaceStore, bandwidthStatRepository, downloadStatRepository, nodesRepository, upstreamProxyConfigRepository) - mavenLocalRegistry := maven.LocalRegistryProvider(mavenDBStore, transactor, fileManager) + mavenLocalRegistry := maven.LocalRegistryProvider(localBase, mavenDBStore, transactor, fileManager) mavenController := maven.ProvideProxyController(mavenLocalRegistry, secretService, spaceFinder) mavenRemoteRegistry := maven.RemoteRegistryProvider(mavenDBStore, transactor, mavenLocalRegistry, mavenController) controller2 := maven.ControllerProvider(mavenLocalRegistry, mavenRemoteRegistry, authorizer, mavenDBStore) @@ -540,8 +542,6 @@ func initSystem(ctx context.Context, config *types.Config) (*server.System, erro packagesHandler := api2.NewPackageHandlerProvider(registryRepository, downloadStatRepository, spaceStore, tokenStore, controller, authenticator, provider, authorizer) genericHandler := api2.NewGenericHandlerProvider(spaceStore, genericController, tokenStore, controller, authenticator, provider, authorizer, packagesHandler) handler3 := router.GenericHandlerProvider(genericHandler) - packageTagRepository := database2.ProvidePackageTagDao(db) - localBase := base.LocalBaseProvider(registryRepository, fileManager, transactor, imageRepository, artifactRepository, nodesRepository, packageTagRepository) pythonLocalRegistry := python.LocalRegistryProvider(localBase, fileManager, upstreamProxyConfigRepository, transactor, registryRepository, imageRepository, artifactRepository, provider) localRegistryHelper := python.LocalRegistryHelperProvider(pythonLocalRegistry, localBase) proxy := python.ProxyProvider(upstreamProxyConfigRepository, registryRepository, imageRepository, artifactRepository, fileManager, transactor, provider, spaceFinder, secretService, localRegistryHelper) diff --git a/registry/app/api/handler/maven/base.go b/registry/app/api/handler/maven/base.go index 089f6fd74..089551ec9 100644 --- a/registry/app/api/handler/maven/base.go +++ b/registry/app/api/handler/maven/base.go @@ -112,19 +112,23 @@ func (h *Handler) GetArtifactInfo(r *http.Request, remoteSupport bool) (pkg.Mave pathRoot := getPathRoot(r.Context()) info := &pkg.MavenArtifactInfo{ - BaseInfo: &pkg.BaseInfo{ - PathRoot: pathRoot, - RootIdentifier: rootIdentifier, - RootParentID: rootSpace.ID, - ParentID: registry.ParentID, + ArtifactInfo: &pkg.ArtifactInfo{ + BaseInfo: &pkg.BaseInfo{ + PathRoot: pathRoot, + RootIdentifier: rootIdentifier, + RootParentID: rootSpace.ID, + ParentID: registry.ParentID, + }, + Registry: *registry, + RegIdentifier: registryIdentifier, + RegistryID: registry.ID, + Image: groupID + ":" + artifactID, }, - RegIdentifier: registryIdentifier, - RegistryID: registry.ID, - GroupID: groupID, - ArtifactID: artifactID, - Version: version, - FileName: fileName, - Path: r.URL.Path, + GroupID: groupID, + ArtifactID: artifactID, + Version: version, + FileName: fileName, + Path: r.URL.Path, } log.Ctx(ctx).Info().Msgf("Dispatch: URI: %s", path) diff --git a/registry/app/pkg/base/base.go b/registry/app/pkg/base/base.go index 5fa748cdb..9fb0b52c9 100644 --- a/registry/app/pkg/base/base.go +++ b/registry/app/pkg/base/base.go @@ -80,6 +80,8 @@ type LocalBase interface { Exists(ctx context.Context, info pkg.ArtifactInfo, version string, fileName string) bool + ExistsByFilePath(ctx context.Context, registryID int64, filePath string) (bool, error) + CheckIfVersionExists(ctx context.Context, info pkg.PackageArtifactInfo) (bool, error) DeletePackage(ctx context.Context, info pkg.PackageArtifactInfo) error @@ -311,6 +313,11 @@ func (l *localBase) Exists(ctx context.Context, info pkg.ArtifactInfo, version s return exists } +func (l *localBase) ExistsByFilePath(ctx context.Context, registryID int64, filePath string) (bool, error) { + exists, _, err := l.GetSHA256ByPath(ctx, registryID, filePath) + return exists, err +} + func (l *localBase) CheckIfVersionExists(ctx context.Context, info pkg.PackageArtifactInfo) (bool, error) { artifacts, err := l.artifactDao.GetArtifactMetadata(ctx, info.BaseArtifactInfo().ParentID, info.BaseArtifactInfo().RegIdentifier, diff --git a/registry/app/pkg/context.go b/registry/app/pkg/context.go index ba3524fcc..49e9418d9 100644 --- a/registry/app/pkg/context.go +++ b/registry/app/pkg/context.go @@ -58,14 +58,12 @@ func (a *ArtifactInfo) SetRepoKey(key string) { } type MavenArtifactInfo struct { - *BaseInfo - RegIdentifier string - RegistryID int64 - GroupID string - ArtifactID string - Version string - FileName string - Path string + *ArtifactInfo + GroupID string + ArtifactID string + Version string + FileName string + Path string } type GenericArtifactInfo struct { diff --git a/registry/app/pkg/maven/local.go b/registry/app/pkg/maven/local.go index f0447e905..3804dd387 100644 --- a/registry/app/pkg/maven/local.go +++ b/registry/app/pkg/maven/local.go @@ -18,6 +18,7 @@ import ( "context" "database/sql" "encoding/json" + "fmt" "io" "net/http" "strings" @@ -26,6 +27,7 @@ import ( "github.com/harness/gitness/registry/app/dist_temp/errcode" "github.com/harness/gitness/registry/app/metadata" "github.com/harness/gitness/registry/app/pkg" + "github.com/harness/gitness/registry/app/pkg/base" "github.com/harness/gitness/registry/app/pkg/commons" "github.com/harness/gitness/registry/app/pkg/filemanager" "github.com/harness/gitness/registry/app/pkg/maven/utils" @@ -39,11 +41,13 @@ const ( ) func NewLocalRegistry( - dBStore *DBStore, tx dbtx.Transactor, - + localBase base.LocalBase, + dBStore *DBStore, + tx dbtx.Transactor, fileManager filemanager.FileManager, ) Registry { return &LocalRegistry{ + localBase: localBase, DBStore: dBStore, tx: tx, fileManager: fileManager, @@ -51,6 +55,7 @@ func NewLocalRegistry( } type LocalRegistry struct { + localBase base.LocalBase DBStore *DBStore tx dbtx.Transactor fileManager filemanager.FileManager @@ -120,6 +125,21 @@ func (r *LocalRegistry) PutArtifact(ctx context.Context, info pkg.MavenArtifactI responseHeaders *commons.ResponseHeaders, errs []error, ) { filePath := utils.GetFilePath(info) + fileExists, err := r.localBase.ExistsByFilePath(ctx, info.RegistryID, strings.TrimPrefix(filePath, "/")) + if err != nil { + return responseHeaders, []error{ + fmt.Errorf("error occurred while checking file existence for GroupID: %s, "+ + "ArtifactID: %s and Version: %s with file name: %s in registry: %s with error: %w", + info.GroupID, info.ArtifactID, info.Version, info.FileName, info.RegIdentifier, err)} + } + if fileExists { + return responseHeaders, []error{ + fmt.Errorf("file already exists for GroupID: %s, "+ + "ArtifactID: %s and Version: %s with file name: %s in registry: %s. "+ + "Try deleting this version and push it again", info.GroupID, info.ArtifactID, + info.Version, info.FileName, info.RegIdentifier)} + } + fileInfo, err := r.fileManager.UploadFile(ctx, filePath, info.RegistryID, info.RootParentID, info.RootIdentifier, nil, fileReader, info.FileName) if err != nil { diff --git a/registry/app/pkg/maven/wire.go b/registry/app/pkg/maven/wire.go index b93c6c296..08124771f 100644 --- a/registry/app/pkg/maven/wire.go +++ b/registry/app/pkg/maven/wire.go @@ -18,6 +18,7 @@ import ( "github.com/harness/gitness/app/auth/authz" "github.com/harness/gitness/app/services/refcache" corestore "github.com/harness/gitness/app/store" + "github.com/harness/gitness/registry/app/pkg/base" "github.com/harness/gitness/registry/app/pkg/filemanager" "github.com/harness/gitness/registry/app/remote/controller/proxy/maven" "github.com/harness/gitness/registry/app/store" @@ -28,12 +29,14 @@ import ( ) func LocalRegistryProvider( + localBase base.LocalBase, dBStore *DBStore, tx dbtx.Transactor, fileManager filemanager.FileManager, ) *LocalRegistry { //nolint:errcheck - return NewLocalRegistry(dBStore, + return NewLocalRegistry(localBase, + dBStore, tx, fileManager, ).(*LocalRegistry) diff --git a/registry/app/pkg/python/local_helper_test.go b/registry/app/pkg/python/local_helper_test.go index 5a215acff..3cd2ebe4d 100644 --- a/registry/app/pkg/python/local_helper_test.go +++ b/registry/app/pkg/python/local_helper_test.go @@ -119,6 +119,11 @@ func (m *MockLocalBase) Exists(ctx context.Context, info pkg.ArtifactInfo, versi return args.Bool(0) } +func (m *MockLocalBase) ExistsByFilePath(ctx context.Context, registryID int64, filePath string) (bool, error) { + args := m.Called(ctx, registryID, filePath) + return args.Bool(0), args.Error(1) +} + func (m *MockLocalBase) Download(ctx context.Context, info pkg.ArtifactInfo, version, filename string) ( *commons.ResponseHeaders, *storage.FileReader, string, error, ) {