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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
package git
import (
"context"
"fmt"
"io"
"os"
"regexp"
"strings"
"gitlab.com/gitlab-org/gitaly/v15/internal/command"
"gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/config"
"gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/storage"
"gitlab.com/gitlab-org/gitaly/v15/internal/metadata/featureflag"
"gitlab.com/gitlab-org/gitaly/v15/internal/structerr"
"gitlab.com/gitlab-org/gitaly/v15/internal/x509"
"gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb"
"gitlab.com/gitlab-org/labkit/correlation"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
const (
// InternalGitalyURL is a special URL that indicates Gitaly wants to push to or fetch from
// another internal Gitaly instance.
InternalGitalyURL = "ssh://gitaly/internal.git"
)
var flagRegex = regexp.MustCompile(`^(-|--)[[:alnum:]]`)
// GlobalOption is an interface for all options which can be globally applied
// to git commands. This is the command-inspecific part before the actual
// command that's being run, e.g. the `-c` part in `git -c foo.bar=value
// command`.
type GlobalOption interface {
GlobalArgs() ([]string, error)
}
// Option is a git command line flag with validation logic
type Option interface {
OptionArgs() ([]string, error)
}
// ConfigPair is a GlobalOption that can be passed to Git commands to inject per-command config
// entries via the `git -c` switch.
type ConfigPair = config.GitConfig
// ConfigPairsToGitEnvironment converts the given config pairs into a set of environment variables
// that can be injected into a Git executable.
func ConfigPairsToGitEnvironment(configPairs []ConfigPair) []string {
env := make([]string, 0, len(configPairs)*2+1)
for i, configPair := range configPairs {
env = append(env,
fmt.Sprintf("GIT_CONFIG_KEY_%d=%s", i, configPair.Key),
fmt.Sprintf("GIT_CONFIG_VALUE_%d=%s", i, configPair.Value),
)
}
return append(env, fmt.Sprintf("GIT_CONFIG_COUNT=%d", len(configPairs)))
}
// Flag is a single token optional command line argument that enables or
// disables functionality (e.g. "-L")
type Flag struct {
Name string
}
// GlobalArgs returns the arguments for the given flag, which should typically
// only be the flag itself. It returns an error if the flag is not sanitary.
func (f Flag) GlobalArgs() ([]string, error) {
return f.OptionArgs()
}
// OptionArgs returns an error if the flag is not sanitary
func (f Flag) OptionArgs() ([]string, error) {
if !flagRegex.MatchString(f.Name) {
return nil, fmt.Errorf("flag %q failed regex validation: %w", f.Name, ErrInvalidArg)
}
return []string{f.Name}, nil
}
// ValueFlag is an optional command line argument that is comprised of pair of
// tokens (e.g. "-n 50")
type ValueFlag struct {
Name string
Value string
}
// GlobalArgs returns the arguments for the given value flag, which should
// typically be two arguments: the flag and its value. It returns an error if the value flag is not sanitary.
func (vf ValueFlag) GlobalArgs() ([]string, error) {
return vf.OptionArgs()
}
// OptionArgs returns an error if the flag is not sanitary
func (vf ValueFlag) OptionArgs() ([]string, error) {
if !flagRegex.MatchString(vf.Name) {
return nil, fmt.Errorf("value flag %q failed regex validation: %w", vf.Name, ErrInvalidArg)
}
return []string{vf.Name, vf.Value}, nil
}
// ConvertGlobalOptions converts a protobuf message to a CmdOpt.
func ConvertGlobalOptions(options *gitalypb.GlobalOptions) []CmdOpt {
if options != nil && options.GetLiteralPathspecs() {
return []CmdOpt{
WithEnv("GIT_LITERAL_PATHSPECS=1"),
}
}
return nil
}
// ConvertConfigOptions converts `<key>=<value>` config entries into `ConfigPairs`.
func ConvertConfigOptions(options []string) ([]ConfigPair, error) {
configPairs := make([]ConfigPair, len(options))
for i, option := range options {
configPair := strings.SplitN(option, "=", 2)
if len(configPair) != 2 {
return nil, fmt.Errorf("cannot convert invalid config key: %q", option)
}
configPairs[i] = ConfigPair{Key: configPair[0], Value: configPair[1]}
}
return configPairs, nil
}
type cmdCfg struct {
env []string
globals []GlobalOption
commandOpts []command.Option
hooksConfigured bool
worktreePath string
}
// CmdOpt is an option for running a command
type CmdOpt func(context.Context, config.Cfg, CommandFactory, *cmdCfg) error
// WithStdin sets the command's stdin. Pass `command.SetupStdin` to make the
// command suitable for `Write()`ing to.
func WithStdin(r io.Reader) CmdOpt {
return func(_ context.Context, _ config.Cfg, _ CommandFactory, c *cmdCfg) error {
c.commandOpts = append(c.commandOpts, command.WithStdin(r))
return nil
}
}
// WithSetupStdin sets up the command so that it can be `Write()`en to.
func WithSetupStdin() CmdOpt {
return func(_ context.Context, _ config.Cfg, _ CommandFactory, c *cmdCfg) error {
c.commandOpts = append(c.commandOpts, command.WithSetupStdin())
return nil
}
}
// WithStdout sets the command's stdout.
func WithStdout(w io.Writer) CmdOpt {
return func(_ context.Context, _ config.Cfg, _ CommandFactory, c *cmdCfg) error {
c.commandOpts = append(c.commandOpts, command.WithStdout(w))
return nil
}
}
// WithStderr sets the command's stderr.
func WithStderr(w io.Writer) CmdOpt {
return func(_ context.Context, _ config.Cfg, _ CommandFactory, c *cmdCfg) error {
c.commandOpts = append(c.commandOpts, command.WithStderr(w))
return nil
}
}
// WithEnv adds environment variables to the command.
func WithEnv(envs ...string) CmdOpt {
return func(_ context.Context, _ config.Cfg, _ CommandFactory, c *cmdCfg) error {
c.env = append(c.env, envs...)
return nil
}
}
// WithConfig adds git configuration entries to the command.
func WithConfig(configPairs ...ConfigPair) CmdOpt {
return func(_ context.Context, _ config.Cfg, _ CommandFactory, c *cmdCfg) error {
for _, configPair := range configPairs {
c.globals = append(c.globals, configPair)
}
return nil
}
}
// WithConfigEnv adds git configuration entries to the command's environment. This should be used
// in place of `WithConfig()` in case config entries may contain secrets which shouldn't leak e.g.
// via the process's command line.
func WithConfigEnv(configPairs ...ConfigPair) CmdOpt {
return func(_ context.Context, _ config.Cfg, _ CommandFactory, c *cmdCfg) error {
c.env = append(c.env, ConfigPairsToGitEnvironment(configPairs)...)
return nil
}
}
// WithGlobalOption adds the global options to the command. These are universal options which work
// across all git commands.
func WithGlobalOption(opts ...GlobalOption) CmdOpt {
return func(_ context.Context, _ config.Cfg, _ CommandFactory, c *cmdCfg) error {
c.globals = append(c.globals, opts...)
return nil
}
}
// WithInternalFetch returns an option which sets up git-fetch(1) to fetch from another internal
// Gitaly node.
func WithInternalFetch(req *gitalypb.SSHUploadPackRequest) CmdOpt {
return withInternalFetch(req, false)
}
// WithInternalFetchWithSidechannel returns an option which sets up git-fetch(1) to fetch from
// another internal Gitaly node. In contrast to WithInternalFetch, this will call
// SSHUploadPackWithSidechannel instead of SSHUploadPack.
func WithInternalFetchWithSidechannel(req *gitalypb.SSHUploadPackWithSidechannelRequest) CmdOpt {
return withInternalFetch(req, true)
}
type repoScopedRequest interface {
proto.Message
GetRepository() *gitalypb.Repository
}
func withInternalFetch(req repoScopedRequest, withSidechannel bool) func(ctx context.Context, cfg config.Cfg, _ CommandFactory, c *cmdCfg) error {
return func(ctx context.Context, cfg config.Cfg, _ CommandFactory, c *cmdCfg) error {
payload, err := protojson.Marshal(req)
if err != nil {
return structerr.NewInternal("marshalling payload failed: %v", err)
}
serversInfo, err := storage.ExtractGitalyServers(ctx)
if err != nil {
return structerr.NewInternal("extracting Gitaly servers: %v", err)
}
storageInfo, ok := serversInfo[req.GetRepository().GetStorageName()]
if !ok {
return structerr.NewInvalidArgument("no storage info for %q", req.GetRepository().GetStorageName())
}
if storageInfo.Address == "" {
return structerr.NewInvalidArgument("empty Gitaly address")
}
var flagsWithValue []string
for flag, value := range featureflag.FromContext(ctx) {
flagsWithValue = append(flagsWithValue, flag.FormatWithValue(value))
}
c.env = append(c.env,
fmt.Sprintf("GITALY_PAYLOAD=%s", payload),
fmt.Sprintf("GIT_SSH_COMMAND=%s %s", cfg.BinaryPath("gitaly-ssh"), "upload-pack"),
fmt.Sprintf("GITALY_ADDRESS=%s", storageInfo.Address),
fmt.Sprintf("GITALY_TOKEN=%s", storageInfo.Token),
fmt.Sprintf("GITALY_FEATUREFLAGS=%s", strings.Join(flagsWithValue, ",")),
fmt.Sprintf("CORRELATION_ID=%s", correlation.ExtractFromContextOrGenerate(ctx)),
// please see https://github.com/git/git/commit/0da0e49ba12225684b75e86a4c9344ad121652cb for mote details
"GIT_SSH_VARIANT=simple",
// Pass through the SSL_CERT_* variables that indicate which
// system certs to trust
fmt.Sprintf("%s=%s", x509.SSLCertDir, os.Getenv(x509.SSLCertDir)),
fmt.Sprintf("%s=%s", x509.SSLCertFile, os.Getenv(x509.SSLCertFile)),
)
if withSidechannel {
c.env = append(c.env, "GITALY_USE_SIDECHANNEL=1")
}
return nil
}
}
// WithFinalizer sets up the finalizer to be run when the command is being wrapped up. It will be
// called after `Wait()` has returned.
func WithFinalizer(finalizer func(context.Context, *command.Command)) CmdOpt {
return func(_ context.Context, _ config.Cfg, _ CommandFactory, c *cmdCfg) error {
c.commandOpts = append(c.commandOpts, command.WithFinalizer(finalizer))
return nil
}
}
// WithWorktree sets up the Git command to run in the given worktree path by using the `-C` switch.
func WithWorktree(worktreePath string) CmdOpt {
return func(_ context.Context, _ config.Cfg, _ CommandFactory, c *cmdCfg) error {
c.worktreePath = worktreePath
return nil
}
}
|