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
|
package ssh
import (
"fmt"
"strconv"
"gitlab.com/gitlab-org/gitaly/internal/metadata/featureflag"
grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
log "github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitaly/internal/command"
"gitlab.com/gitlab-org/gitaly/internal/git"
"gitlab.com/gitlab-org/gitaly/internal/helper"
"gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
"gitlab.com/gitlab-org/gitaly/streamio"
)
func (s *server) SSHReceivePack(stream gitalypb.SSHService_SSHReceivePackServer) error {
req, err := stream.Recv() // First request contains only Repository, GlId, and GlUsername
if err != nil {
return helper.ErrInternal(err)
}
grpc_logrus.Extract(stream.Context()).WithFields(log.Fields{
"GlID": req.GlId,
"GlRepository": req.GlRepository,
"GlUsername": req.GlUsername,
"GitConfigOptions": req.GitConfigOptions,
}).Debug("SSHReceivePack")
if err = validateFirstReceivePackRequest(req); err != nil {
return helper.ErrInvalidArgument(err)
}
if err := sshReceivePack(stream, req); err != nil {
return helper.ErrInternal(err)
}
return nil
}
func sshReceivePack(stream gitalypb.SSHService_SSHReceivePackServer, req *gitalypb.SSHReceivePackRequest) error {
ctx := stream.Context()
stdin := streamio.NewReader(func() ([]byte, error) {
request, err := stream.Recv()
return request.GetStdin(), err
})
stdout := streamio.NewWriter(func(p []byte) error {
return stream.Send(&gitalypb.SSHReceivePackResponse{Stdout: p})
})
stderr := streamio.NewWriter(func(p []byte) error {
return stream.Send(&gitalypb.SSHReceivePackResponse{Stderr: p})
})
env := append(git.HookEnv(req), "GL_PROTOCOL=ssh")
repoPath, err := helper.GetRepoPath(req.Repository)
if err != nil {
return err
}
env = git.AddGitProtocolEnv(ctx, req, env)
env = append(env, command.GitEnv...)
env = append(env, fmt.Sprintf("%s=%s", featureflag.HooksRPCEnvVar, strconv.FormatBool(featureflag.IsEnabled(ctx, featureflag.HooksRPC))))
globalOpts := git.ReceivePackConfig()
for _, o := range req.GitConfigOptions {
globalOpts = append(globalOpts, git.ValueFlag{"-c", o})
}
cmd, err := git.SafeBareCmd(ctx, stdin, stdout, stderr, env, globalOpts, git.SubCmd{
Name: "receive-pack",
Args: []string{repoPath},
})
if err != nil {
return fmt.Errorf("start cmd: %v", err)
}
if err := cmd.Wait(); err != nil {
if status, ok := command.ExitStatus(err); ok {
return stream.Send(&gitalypb.SSHReceivePackResponse{
ExitStatus: &gitalypb.ExitStatus{Value: int32(status)},
})
}
return fmt.Errorf("cmd wait: %v", err)
}
return nil
}
func validateFirstReceivePackRequest(req *gitalypb.SSHReceivePackRequest) error {
if req.GlId == "" {
return fmt.Errorf("empty GlId")
}
if req.Stdin != nil {
return fmt.Errorf("non-empty data in first request")
}
return nil
}
|