diff options
author | Zeger-Jan van de Weg <git@zjvandeweg.nl> | 2018-06-28 12:04:41 +0300 |
---|---|---|
committer | Zeger-Jan van de Weg <git@zjvandeweg.nl> | 2018-06-28 12:08:34 +0300 |
commit | cae6cc387f9da12fd5a3403ea2d0e581887aeaa1 (patch) | |
tree | 2c1b5c03578bf5bb368bd04caa6b801b0306f9b3 /internal/service | |
parent | a185b5a7745d1cd3b70a7c731ca1e448662f88c6 (diff) |
Sanitize errors when importing repositories
Cleans out Gitaly paths from the errros, where they may occur. The
shared path isn't excluded, but that's done by the client, which only
has the required data to do so.
Part of: https://gitlab.com/gitlab-org/gitaly/issues/1235
Diffstat (limited to 'internal/service')
-rw-r--r-- | internal/service/repository/create_from_bundle.go | 25 | ||||
-rw-r--r-- | internal/service/repository/create_from_bundle_test.go | 27 |
2 files changed, 46 insertions, 6 deletions
diff --git a/internal/service/repository/create_from_bundle.go b/internal/service/repository/create_from_bundle.go index b935d002d..d8b1593bd 100644 --- a/internal/service/repository/create_from_bundle.go +++ b/internal/service/repository/create_from_bundle.go @@ -1,10 +1,12 @@ package repository import ( + "fmt" "io" "os" "os/exec" "path" + "strings" "gitlab.com/gitlab-org/gitaly/internal/command" "gitlab.com/gitlab-org/gitaly/internal/helper" @@ -43,18 +45,21 @@ func (s *server) CreateRepositoryFromBundle(stream pb.RepositoryService_CreateRe tmpDir, err := tempdir.New(ctx, repo) if err != nil { - return status.Errorf(codes.Internal, "CreateRepositoryFromBundle: tmp dir failed: %v", err) + cleanError := sanitizedError(tmpDir, "CreateRepositoryFromBundle: tmp dir failed: %v", err) + return status.Error(codes.Internal, cleanError) } bundlePath := path.Join(tmpDir, "repo.bundle") file, err := os.Create(bundlePath) if err != nil { - return status.Errorf(codes.Internal, "CreateRepositoryFromBundle: new bundle file failed: %v", err) + cleanError := sanitizedError(tmpDir, "CreateRepositoryFromBundle: new bundle file failed: %v", err) + return status.Error(codes.Internal, cleanError) } _, err = io.Copy(file, reader) if err != nil { - return status.Errorf(codes.Internal, "CreateRepositoryFromBundle: bundle write failed: %v", err) + cleanError := sanitizedError(tmpDir, "CreateRepositoryFromBundle: new bundle file failed: %v", err) + return status.Error(codes.Internal, cleanError) } repoPath, err := helper.GetPath(repo) @@ -71,16 +76,24 @@ func (s *server) CreateRepositoryFromBundle(stream pb.RepositoryService_CreateRe } cmd, err := command.New(ctx, exec.Command(command.GitPath(), args...), nil, nil, nil) if err != nil { - return status.Errorf(codes.Internal, "CreateRepositoryFromBundle: cmd start failed: %v", err) + cleanError := sanitizedError(repoPath, "CreateRepositoryFromBundle: cmd start failed: %v", err) + return status.Error(codes.Internal, cleanError) } if err := cmd.Wait(); err != nil { - return status.Errorf(codes.Internal, "CreateRepositoryFromBundle: cmd wait failed: %v", err) + cleanError := sanitizedError(repoPath, "CreateRepositoryFromBundle: cmd wait failed: %v", err) + return status.Error(codes.Internal, cleanError) } // CreateRepository is harmless on existing repositories with the side effect that it creates the hook symlink. if _, err := s.CreateRepository(ctx, &pb.CreateRepositoryRequest{Repository: repo}); err != nil { - return status.Errorf(codes.Internal, "CreateRepositoryFromBundle: create hooks failed: %v", err) + cleanError := sanitizedError(repoPath, "CreateRepositoryFromBundle: create hooks failed: %v", err) + return status.Error(codes.Internal, cleanError) } return stream.SendAndClose(&pb.CreateRepositoryFromBundleResponse{}) } + +func sanitizedError(path, format string, a ...interface{}) string { + str := fmt.Sprintf(format, a...) + return strings.Replace(str, path, "[REPO PATH]", -1) +} diff --git a/internal/service/repository/create_from_bundle_test.go b/internal/service/repository/create_from_bundle_test.go index 3c2cbdd20..29b0229d0 100644 --- a/internal/service/repository/create_from_bundle_test.go +++ b/internal/service/repository/create_from_bundle_test.go @@ -13,6 +13,7 @@ import ( pb "gitlab.com/gitlab-org/gitaly-proto/go" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "google.golang.org/grpc/codes" ) @@ -96,3 +97,29 @@ func TestFailedCreateRepositoryFromBundleRequestDueToValidations(t *testing.T) { _, err = stream.CloseAndRecv() testhelper.RequireGrpcError(t, err, codes.InvalidArgument) } + +func TestSanitizedError(t *testing.T) { + testCases := []struct { + path string + format string + a []interface{} + expected string + }{ + { + path: "/home/git/storage", + format: "failed to create from bundle in /home/git/storage/my-project", + expected: "failed to create from bundle in [REPO PATH]/my-project", + }, + { + path: "/home/git/storage", + format: "failed to %s in [REPO PATH]/my-project", + a: []interface{}{"create from bundle"}, + expected: "failed to create from bundle in [REPO PATH]/my-project", + }, + } + + for _, tc := range testCases { + str := sanitizedError(tc.path, tc.format, tc.a...) + assert.Equal(t, tc.expected, str) + } +} |