diff options
-rw-r--r-- | internal/git/alternates/alternates.go | 19 | ||||
-rw-r--r-- | internal/helper/repo.go | 22 | ||||
-rw-r--r-- | internal/helper/repo_test.go | 156 |
3 files changed, 0 insertions, 197 deletions
diff --git a/internal/git/alternates/alternates.go b/internal/git/alternates/alternates.go index 60636da83..f74180a6d 100644 --- a/internal/git/alternates/alternates.go +++ b/internal/git/alternates/alternates.go @@ -4,27 +4,8 @@ import ( "fmt" "path/filepath" "strings" - - "gitlab.com/gitlab-org/gitaly/internal/git/repository" - "gitlab.com/gitlab-org/gitaly/internal/helper" ) -// PathAndEnv finds the disk path to a repository, and returns the -// alternate object directory environment variables encoded in the -// gitalypb.Repository instance. -// Deprecated: please use storage.Locator to define the project path and alternates.Env -// to get alternate object directory environment variables. -func PathAndEnv(repo repository.GitRepo) (string, []string, error) { - repoPath, err := helper.GetRepoPath(repo) - if err != nil { - return "", nil, err - } - - env := Env(repoPath, repo.GetGitObjectDirectory(), repo.GetGitAlternateObjectDirectories()) - - return repoPath, env, nil -} - // Env returns the alternate object directory environment variables. func Env(repoPath, objectDirectory string, alternateObjectDirectories []string) []string { var env []string diff --git a/internal/helper/repo.go b/internal/helper/repo.go index 2d61a73ec..f65f5c996 100644 --- a/internal/helper/repo.go +++ b/internal/helper/repo.go @@ -12,28 +12,6 @@ import ( "google.golang.org/grpc/status" ) -// GetRepoPath returns the full path of the repository referenced by an -// RPC Repository message. The errors returned are gRPC errors with -// relevant error codes and should be passed back to gRPC without further -// decoration. -// Deprecated: please use storage.Locator to define the project path. -func GetRepoPath(repo repository.GitRepo) (string, error) { - repoPath, err := GetPath(repo) - if err != nil { - return "", err - } - - if repoPath == "" { - return "", status.Errorf(codes.InvalidArgument, "GetRepoPath: empty repo") - } - - if storage.IsGitDirectory(repoPath) { - return repoPath, nil - } - - return "", status.Errorf(codes.NotFound, "GetRepoPath: not a git repository '%s'", repoPath) -} - // RepoPathEqual compares if two repositories are in the same location func RepoPathEqual(a, b repository.GitRepo) bool { return a.GetStorageName() == b.GetStorageName() && diff --git a/internal/helper/repo_test.go b/internal/helper/repo_test.go index ba8648ee3..8f4b3d8b6 100644 --- a/internal/helper/repo_test.go +++ b/internal/helper/repo_test.go @@ -2,14 +2,11 @@ package helper import ( "os" - "path/filepath" "testing" "github.com/stretchr/testify/assert" - "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/testhelper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" - "google.golang.org/grpc/codes" ) func TestMain(m *testing.M) { @@ -73,156 +70,3 @@ func TestRepoPathEqual(t *testing.T) { }) } } - -func TestGetRepoPath(t *testing.T) { - defer func(oldStorages []config.Storage) { - config.Config.Storages = oldStorages - }(config.Config.Storages) - - testRepo, _, cleanup := testhelper.NewTestRepo(t) - defer cleanup() - - repoPath, err := GetRepoPath(testRepo) - if err != nil { - t.Fatal(err) - } - - exampleStorages := []config.Storage{ - {Name: "default", Path: testhelper.GitlabTestStoragePath()}, - {Name: "other", Path: "/home/git/repositories2"}, - {Name: "third", Path: "/home/git/repositories3"}, - } - - testCases := []struct { - desc string - storages []config.Storage - repo *gitalypb.Repository - path string - err codes.Code - }{ - { - desc: "storages configured", - storages: exampleStorages, - repo: &gitalypb.Repository{StorageName: testRepo.GetStorageName(), RelativePath: testRepo.GetRelativePath()}, - path: repoPath, - }, - { - desc: "no storage config, storage name provided", - repo: &gitalypb.Repository{StorageName: "does not exist", RelativePath: testRepo.GetRelativePath()}, - err: codes.InvalidArgument, - }, - { - desc: "no storage config, nil repo", - err: codes.InvalidArgument, - }, - { - desc: "storage config provided, empty repo", - storages: exampleStorages, - repo: &gitalypb.Repository{}, - err: codes.InvalidArgument, - }, - { - desc: "no storage config, empty repo", - repo: &gitalypb.Repository{}, - err: codes.InvalidArgument, - }, - { - desc: "non existing repo", - storages: exampleStorages, - repo: &gitalypb.Repository{StorageName: testRepo.GetStorageName(), RelativePath: "made/up/path.git"}, - err: codes.NotFound, - }, - { - desc: "non existing storage", - storages: exampleStorages, - repo: &gitalypb.Repository{StorageName: "does not exists", RelativePath: testRepo.GetRelativePath()}, - err: codes.InvalidArgument, - }, - { - desc: "storage defined but storage dir does not exist", - storages: []config.Storage{{Name: testRepo.GetStorageName(), Path: "/does/not/exist"}}, - repo: &gitalypb.Repository{StorageName: testRepo.GetStorageName(), RelativePath: "foobar.git"}, - err: codes.Internal, - }, - { - desc: "relative path with directory traversal", - storages: exampleStorages, - repo: &gitalypb.Repository{StorageName: testRepo.GetStorageName(), RelativePath: "../bazqux.git"}, - err: codes.InvalidArgument, - }, - { - desc: "valid path with ..", - storages: exampleStorages, - repo: &gitalypb.Repository{StorageName: testRepo.GetStorageName(), RelativePath: "foo../bazqux.git"}, - err: codes.NotFound, // Because the directory doesn't exist - }, - { - desc: "relative path with sneaky directory traversal", - storages: exampleStorages, - repo: &gitalypb.Repository{StorageName: testRepo.GetStorageName(), RelativePath: "/../bazqux.git"}, - err: codes.InvalidArgument, - }, - { - desc: "relative path with traversal outside storage", - storages: exampleStorages, - repo: &gitalypb.Repository{StorageName: testRepo.GetStorageName(), RelativePath: testRepo.GetRelativePath() + "/../../../../.."}, - err: codes.InvalidArgument, - }, - { - desc: "relative path with traversal outside storage with trailing slash", - storages: exampleStorages, - repo: &gitalypb.Repository{StorageName: testRepo.GetStorageName(), RelativePath: testRepo.GetRelativePath() + "/../../../../../"}, - err: codes.InvalidArgument, - }, - { - desc: "relative path with deep traversal at the end", - storages: exampleStorages, - repo: &gitalypb.Repository{StorageName: testRepo.GetStorageName(), RelativePath: "bazqux.git/../.."}, - err: codes.InvalidArgument, - }, - } - - for _, tc := range testCases { - t.Run(tc.desc, func(t *testing.T) { - config.Config.Storages = tc.storages - path, err := GetRepoPath(tc.repo) - - if tc.err != codes.OK { - testhelper.RequireGrpcError(t, err, tc.err) - return - } - - if err != nil { - assert.NoError(t, err) - return - } - - assert.Equal(t, tc.path, path) - }) - } -} - -func assertInvalidRepoWithoutFile(t *testing.T, repo *gitalypb.Repository, repoPath, file string) { - oldRoute := filepath.Join(repoPath, file) - renamedRoute := filepath.Join(repoPath, file+"moved") - os.Rename(oldRoute, renamedRoute) - defer func() { - os.Rename(renamedRoute, oldRoute) - }() - - _, err := GetRepoPath(repo) - - testhelper.RequireGrpcError(t, err, codes.NotFound) -} - -func TestGetRepoPathWithCorruptedRepo(t *testing.T) { - testRepo, _, cleanup := testhelper.NewTestRepo(t) - defer cleanup() - - testRepoStoragePath := testhelper.GitlabTestStoragePath() - testRepoPath := filepath.Join(testRepoStoragePath, testRepo.RelativePath) - - for _, file := range []string{"objects", "refs", "HEAD"} { - assertInvalidRepoWithoutFile(t, testRepo, testRepoPath, file) - } -} |