Welcome to mirror list, hosted at ThFree Co, Russian Federation.

upload_pack.go « smarthttp « service « gitaly « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cd966ae340fadee1329d0f7ac5416897a1940aeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package smarthttp

import (
	"context"
	"crypto/sha1"
	"fmt"
	"io"

	"gitlab.com/gitlab-org/gitaly/v16/streamio"
	"google.golang.org/protobuf/proto"

	"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
	"gitlab.com/gitlab-org/gitaly/v16/internal/command"
	"gitlab.com/gitlab-org/gitaly/v16/internal/git"
	"gitlab.com/gitlab-org/gitaly/v16/internal/git/stats"
	"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/service"
	"gitlab.com/gitlab-org/gitaly/v16/internal/grpc/sidechannel"
	"gitlab.com/gitlab-org/gitaly/v16/internal/structerr"
	"gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb"
)

type postUploadPackRequest interface {
	GetRepository() *gitalypb.Repository
	GetGitConfigOptions() []string
	GetGitProtocol() string
}

func (s *server) PostUploadPackWithSidechannel(ctx context.Context, req *gitalypb.PostUploadPackWithSidechannelRequest) (*gitalypb.PostUploadPackWithSidechannelResponse, error) {
	repoPath, gitConfig, err := s.validateUploadPackRequest(ctx, req)
	if err != nil {
		return nil, structerr.NewInvalidArgument("%w", err)
	}

	conn, err := sidechannel.OpenSidechannel(ctx)
	if err != nil {
		return nil, structerr.NewInternal("open sidechannel: %w", err)
	}
	defer conn.Close()

	proxy := func(cmd *command.Command) (int64, error) {
		return io.CopyBuffer(conn, cmd, make([]byte, 64*1024))
	}
	if err := s.runUploadPack(ctx, req, repoPath, gitConfig, conn, proxy); err != nil {
		return nil, structerr.NewInternal("running upload-pack: %w", err)
	}
	if err := conn.Close(); err != nil {
		return nil, structerr.NewInternal("close sidechannel connection: %w", err)
	}

	return &gitalypb.PostUploadPackWithSidechannelResponse{}, nil
}

func (s *server) PostUploadPackV3(stream gitalypb.SmartHTTPService_PostUploadPackV3Server) error {
	ctx := stream.Context()

	var req gitalypb.PostUploadPackV3Request
	var firstRequest []byte
	// First request contains Repository only
	if err := stream.RecvMsg(&firstRequest); err != nil {
		return err
	}
	if err := proto.Unmarshal(firstRequest, &req); err != nil {
		return err
	}

	repoPath, gitConfig, err := s.validateUploadPackRequest(ctx, &req)
	if err != nil {
		return structerr.NewInvalidArgument("%w", err)
	}

	stdin := streamio.NewReader(func() ([]byte, error) {
		var stdinBuffer []byte
		err := stream.RecvMsg(&stdinBuffer)
		if err != nil {
			return nil, err
		}
		return stdinBuffer, err
	})

	var totalBytes int64
	var recentBytes int
	const maxBufferSize = 1024 * 1024
	bufferSize := 64 * 1024
	copyBuffer := make([]byte, bufferSize)
	proxy := func(cmd *command.Command) (int64, error) {
		for {
			read, err := cmd.Read(copyBuffer[recentBytes:])
			if err != nil {
				if err == io.EOF {
					if recentBytes > 0 {
						return totalBytes, stream.SendMsg(copyBuffer[:recentBytes])
					}
					return totalBytes, nil
				}
				return totalBytes, err
			}
			totalBytes += int64(read)
			recentBytes += read
			if recentBytes == bufferSize {
				if err := stream.SendMsg(copyBuffer); err != nil {
					return totalBytes, err
				}
				bufferSize *= 2
				if bufferSize > maxBufferSize {
					bufferSize = maxBufferSize
				}
				copyBuffer = make([]byte, bufferSize)
				recentBytes = 0
			}
		}
	}

	if err := s.runUploadPack(ctx, &req, repoPath, gitConfig, stdin, proxy); err != nil {
		return structerr.NewInternal("running upload-pack: %w", err)
	}

	return nil
}

type statsCollector struct {
	c       io.Closer
	statsCh chan stats.PackfileNegotiation
}

func (sc *statsCollector) finish() stats.PackfileNegotiation {
	sc.c.Close()
	return <-sc.statsCh
}

func (s *server) runStatsCollector(ctx context.Context, r io.Reader) (io.Reader, *statsCollector) {
	pr, pw := io.Pipe()
	sc := &statsCollector{
		c:       pw,
		statsCh: make(chan stats.PackfileNegotiation, 1),
	}

	go func() {
		defer close(sc.statsCh)

		stats, err := stats.ParsePackfileNegotiation(pr)
		if err != nil {
			ctxlogrus.Extract(ctx).WithError(err).Debug("failed parsing packfile negotiation")
			return
		}
		stats.UpdateMetrics(s.packfileNegotiationMetrics)

		sc.statsCh <- stats
	}()

	return io.TeeReader(r, pw), sc
}

func (s *server) validateUploadPackRequest(ctx context.Context, req postUploadPackRequest) (string, []git.ConfigPair, error) {
	repository := req.GetRepository()
	if err := service.ValidateRepository(repository); err != nil {
		return "", nil, err
	}
	repoPath, err := s.locator.GetRepoPath(repository)
	if err != nil {
		return "", nil, err
	}

	git.WarnIfTooManyBitmaps(ctx, s.locator, repository.GetStorageName(), repoPath)

	config, err := git.ConvertConfigOptions(req.GetGitConfigOptions())
	if err != nil {
		return "", nil, err
	}

	return repoPath, config, nil
}

func (s *server) runUploadPack(ctx context.Context, req postUploadPackRequest, repoPath string, gitConfig []git.ConfigPair, stdin io.Reader, proxy func(*command.Command) (int64, error)) error {
	h := sha1.New()

	stdin = io.TeeReader(stdin, h)
	stdin, collector := s.runStatsCollector(ctx, stdin)
	defer collector.finish()

	commandOpts := []git.CmdOpt{
		git.WithStdin(stdin),
		git.WithGitProtocol(req),
		git.WithConfig(gitConfig...),
		git.WithPackObjectsHookEnv(req.GetRepository(), "http"),
	}

	cmd, err := s.gitCmdFactory.New(ctx, req.GetRepository(), git.Command{
		Name:  "upload-pack",
		Flags: []git.Option{git.Flag{Name: "--stateless-rpc"}},
		Args:  []string{repoPath},
	}, commandOpts...)
	if err != nil {
		return structerr.NewUnavailable("spawning upload-pack: %w", err)
	}

	respBytes, err := proxy(cmd)
	if err != nil {
		return structerr.NewUnavailable("copying stdout from upload-pack: %w", err)
	}

	if err := cmd.Wait(); err != nil {
		stats := collector.finish()

		if _, ok := command.ExitStatus(err); ok && stats.Deepen != "" {
			// We have seen a 'deepen' message in the request. It is expected that
			// git-upload-pack has a non-zero exit status: don't treat this as an
			// error.
			return nil
		}

		return structerr.NewUnavailable("waiting for upload-pack: %w", err)
	}

	ctxlogrus.Extract(ctx).WithField("request_sha", fmt.Sprintf("%x", h.Sum(nil))).WithField("response_bytes", respBytes).Info("request details")

	return nil
}