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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
|
package config
import (
"errors"
"fmt"
"io"
"net"
"os"
"path/filepath"
"reflect"
"strings"
"syscall"
"time"
"github.com/pelletier/go-toml"
log "github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/config/auth"
"gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/config/cgroups"
internallog "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/config/log"
"gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/config/prometheus"
"gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/config/sentry"
)
const (
// GitalyDataPrefix is the top-level directory we use to store system
// (non-user) data. We need to be careful that this path does not clash
// with any directory name that could be provided by a user. The '+'
// character is not allowed in GitLab namespaces or repositories.
GitalyDataPrefix = "+gitaly"
)
// DailyJob enables a daily task to be scheduled for specific storages
type DailyJob struct {
Hour uint `toml:"start_hour"`
Minute uint `toml:"start_minute"`
Duration Duration `toml:"duration"`
Storages []string `toml:"storages"`
// Disabled will completely disable a daily job, even in cases where a
// default schedule is implied
Disabled bool `toml:"disabled"`
}
// Cfg is a container for all config derived from config.toml.
type Cfg struct {
SocketPath string `toml:"socket_path" split_words:"true"`
ListenAddr string `toml:"listen_addr" split_words:"true"`
TLSListenAddr string `toml:"tls_listen_addr" split_words:"true"`
PrometheusListenAddr string `toml:"prometheus_listen_addr" split_words:"true"`
BinDir string `toml:"bin_dir"`
RuntimeDir string `toml:"runtime_dir"`
Git Git `toml:"git" envconfig:"git"`
Storages []Storage `toml:"storage" envconfig:"storage"`
Logging Logging `toml:"logging" envconfig:"logging"`
Prometheus prometheus.Config `toml:"prometheus"`
Auth auth.Config `toml:"auth"`
TLS TLS `toml:"tls"`
Ruby Ruby `toml:"gitaly-ruby"`
Gitlab Gitlab `toml:"gitlab"`
GitlabShell GitlabShell `toml:"gitlab-shell"`
Hooks Hooks `toml:"hooks"`
Concurrency []Concurrency `toml:"concurrency"`
RateLimiting []RateLimiting `toml:"rate_limiting"`
GracefulRestartTimeout Duration `toml:"graceful_restart_timeout"`
DailyMaintenance DailyJob `toml:"daily_maintenance"`
Cgroups cgroups.Config `toml:"cgroups"`
PackObjectsCache StreamCacheConfig `toml:"pack_objects_cache"`
}
// TLS configuration
type TLS struct {
CertPath string `toml:"certificate_path,omitempty" json:"cert_path"`
KeyPath string `toml:"key_path,omitempty" json:"key_path"`
}
// GitlabShell contains the settings required for executing `gitlab-shell`
type GitlabShell struct {
Dir string `toml:"dir" json:"dir"`
}
// Gitlab contains settings required to connect to the Gitlab api
type Gitlab struct {
URL string `toml:"url" json:"url"`
RelativeURLRoot string `toml:"relative_url_root" json:"relative_url_root"` // For UNIX sockets only
HTTPSettings HTTPSettings `toml:"http-settings" json:"http_settings"`
SecretFile string `toml:"secret_file" json:"secret_file"`
}
// Hooks contains the settings required for hooks
type Hooks struct {
CustomHooksDir string `toml:"custom_hooks_dir" json:"custom_hooks_dir"`
}
//nolint: revive,stylecheck // This is unintentionally missing documentation.
type HTTPSettings struct {
ReadTimeout int `toml:"read_timeout" json:"read_timeout"`
User string `toml:"user" json:"user"`
Password string `toml:"password" json:"password"`
CAFile string `toml:"ca_file" json:"ca_file"`
CAPath string `toml:"ca_path" json:"ca_path"`
SelfSigned bool `toml:"self_signed_cert" json:"self_signed_cert"`
}
// Git contains the settings for the Git executable
type Git struct {
UseBundledBinaries bool `toml:"use_bundled_binaries"`
BinPath string `toml:"bin_path"`
CatfileCacheSize int `toml:"catfile_cache_size"`
Config []GitConfig `toml:"config"`
IgnoreGitconfig bool `toml:"ignore_gitconfig"`
}
// GitConfig contains a key-value pair which is to be passed to git as configuration.
type GitConfig struct {
Key string `toml:"key"`
Value string `toml:"value"`
}
// Storage contains a single storage-shard
type Storage struct {
Name string
Path string
}
// Sentry is a sentry.Config. We redefine this type to a different name so
// we can embed both structs into Logging
type Sentry sentry.Config
// Logging contains the logging configuration for Gitaly
type Logging struct {
internallog.Config
Sentry
RubySentryDSN string `toml:"ruby_sentry_dsn"`
}
// Concurrency allows endpoints to be limited to a maximum concurrency per repo.
// Requests that come in after the maximum number of concurrent requests are in progress will wait
// in a queue that is bounded by MaxQueueSize.
type Concurrency struct {
// RPC is the name of the RPC to set concurrency limits for
RPC string `toml:"rpc"`
// MaxPerRepo is the maximum number of concurrent calls for a given repository
MaxPerRepo int `toml:"max_per_repo"`
// MaxQueueSize is the maximum number of requests in the queue waiting to be picked up
// after which subsequent requests will return with an error.
MaxQueueSize int `toml:"max_queue_size"`
// MaxQueueWait is the maximum time a request can remain in the concurrency queue
// waiting to be picked up by Gitaly
MaxQueueWait Duration `toml:"max_queue_wait"`
}
// RateLimiting allows endpoints to be limited to a maximum request rate per
// second. The rate limiter uses a concept of a "token bucket". In order to serve a
// request, a token is retrieved from the token bucket. The size of the token
// bucket is configured through the Burst value, while the rate at which the
// token bucket is refilled per second is configured through the RequestsPerSecond
// value.
type RateLimiting struct {
// RPC is the full name of the RPC including the service name
RPC string `toml:"rpc"`
// Interval sets the interval with which the token bucket will
// be refilled to what is configured in Burst.
Interval time.Duration `toml:"interval"`
// Burst sets the capacity of the token bucket (see above).
Burst int `toml:"burst"`
}
// StreamCacheConfig contains settings for a streamcache instance.
type StreamCacheConfig struct {
Enabled bool `toml:"enabled"` // Default: false
Dir string `toml:"dir"` // Default: <FIRST STORAGE PATH>/+gitaly/PackObjectsCache
MaxAge Duration `toml:"max_age"` // Default: 5m
}
// Load initializes the Config variable from file and the environment.
// Environment variables take precedence over the file.
func Load(file io.Reader) (Cfg, error) {
cfg := Cfg{
Prometheus: prometheus.DefaultConfig(),
}
if err := toml.NewDecoder(file).Decode(&cfg); err != nil {
return Cfg{}, fmt.Errorf("load toml: %v", err)
}
if err := cfg.setDefaults(); err != nil {
return Cfg{}, err
}
for i := range cfg.Storages {
cfg.Storages[i].Path = filepath.Clean(cfg.Storages[i].Path)
}
return cfg, nil
}
// Validate checks the current Config for sanity.
func (cfg *Cfg) Validate() error {
for _, run := range []func() error{
cfg.validateListeners,
cfg.validateStorages,
cfg.validateToken,
cfg.validateGit,
cfg.validateShell,
cfg.ConfigureRuby,
cfg.validateBinDir,
cfg.validateRuntimeDir,
cfg.validateInternalSocketDir,
cfg.validateMaintenance,
cfg.validateCgroups,
cfg.configurePackObjectsCache,
} {
if err := run(); err != nil {
return err
}
}
return nil
}
func (cfg *Cfg) setDefaults() error {
if cfg.GracefulRestartTimeout.Duration() == 0 {
cfg.GracefulRestartTimeout = Duration(time.Minute)
}
if cfg.Gitlab.SecretFile == "" {
cfg.Gitlab.SecretFile = filepath.Join(cfg.GitlabShell.Dir, ".gitlab_shell_secret")
}
if cfg.Hooks.CustomHooksDir == "" {
cfg.Hooks.CustomHooksDir = filepath.Join(cfg.GitlabShell.Dir, "hooks")
}
if cfg.RuntimeDir == "" {
// If there is no runtime directory configured we just use a temporary runtime
// directory. This may not always be an ideal choice given that it's typically
// created at `/tmp`, which may get periodically pruned if `noatime` is set.
runtimeDir, err := os.MkdirTemp("", "gitaly-")
if err != nil {
return fmt.Errorf("creating temporary runtime directory: %w", err)
}
cfg.RuntimeDir = runtimeDir
} else {
// Otherwise, we use the configured runtime directory. Note that we don't use the
// runtime directory directly, but instead create a subdirectory within it which is
// based on the process's PID. While we could use `MkdirTemp()` instead and don't
// bother with preexisting directories, the benefit of using the PID here is that we
// can determine whether the directory may still be in use by checking whether the
// PID exists. Furthermore, it allows easier debugging in case one wants to inspect
// the runtime directory of a running Gitaly node.
runtimeDir := filepath.Join(cfg.RuntimeDir, fmt.Sprintf("gitaly-%d", os.Getpid()))
if _, err := os.Stat(runtimeDir); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("statting runtime directory: %w", err)
} else if err != nil {
// If the directory exists already then it must be from an old invocation of
// Gitaly. Because we use the PID as path component we know that the old
// instance cannot exist anymore though, so it's safe to remove this
// directory now.
if err := os.RemoveAll(runtimeDir); err != nil {
return fmt.Errorf("removing old runtime directory: %w", err)
}
}
if err := os.Mkdir(runtimeDir, 0o700); err != nil {
return fmt.Errorf("creating runtime directory: %w", err)
}
cfg.RuntimeDir = runtimeDir
}
// The socket path must be short-ish because listen(2) fails on long
// socket paths. We hope/expect that os.MkdirTemp creates a directory
// that is not too deep. We need a directory, not a tempfile, because we
// will later want to set its permissions to 0700
if err := os.Mkdir(cfg.InternalSocketDir(), 0o700); err != nil {
return fmt.Errorf("create internal socket directory: %w", err)
}
if reflect.DeepEqual(cfg.DailyMaintenance, DailyJob{}) {
cfg.DailyMaintenance = defaultMaintenanceWindow(cfg.Storages)
}
if cfg.Cgroups.Mountpoint == "" {
cfg.Cgroups.Mountpoint = "/sys/fs/cgroup"
}
if cfg.Cgroups.HierarchyRoot == "" {
cfg.Cgroups.HierarchyRoot = "gitaly"
}
cfg.Cgroups.FallbackToOldVersion()
return nil
}
func (cfg *Cfg) validateListeners() error {
if len(cfg.SocketPath) == 0 && len(cfg.ListenAddr) == 0 && len(cfg.TLSListenAddr) == 0 {
return fmt.Errorf("at least one of socket_path, listen_addr or tls_listen_addr must be set")
}
return nil
}
func (cfg *Cfg) validateShell() error {
if len(cfg.GitlabShell.Dir) == 0 {
return fmt.Errorf("gitlab-shell.dir: is not set")
}
return validateIsDirectory(cfg.GitlabShell.Dir, "gitlab-shell.dir")
}
func validateIsDirectory(path, name string) error {
s, err := os.Stat(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("%s: path doesn't exist: %q", name, path)
}
return fmt.Errorf("%s: %w", name, err)
}
if !s.IsDir() {
return fmt.Errorf("%s: not a directory: %q", name, path)
}
log.WithField("dir", path).Debugf("%s set", name)
return nil
}
func (cfg *Cfg) validateStorages() error {
if len(cfg.Storages) == 0 {
return fmt.Errorf("no storage configurations found. Are you using the right format? https://gitlab.com/gitlab-org/gitaly/issues/397")
}
for i, storage := range cfg.Storages {
if storage.Name == "" {
return fmt.Errorf("empty storage name at declaration %d", i+1)
}
if storage.Path == "" {
return fmt.Errorf("empty storage path for storage %q", storage.Name)
}
fs, err := os.Stat(storage.Path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("storage path %q for storage %q doesn't exist", storage.Path, storage.Name)
}
return fmt.Errorf("storage %q: %w", storage.Name, err)
}
if !fs.IsDir() {
return fmt.Errorf("storage path %q for storage %q is not a dir", storage.Path, storage.Name)
}
for _, other := range cfg.Storages[:i] {
if other.Name == storage.Name {
return fmt.Errorf("storage %q is defined more than once", storage.Name)
}
if storage.Path == other.Path {
// This is weird but we allow it for legacy gitlab.com reasons.
continue
}
if strings.HasPrefix(storage.Path, other.Path) || strings.HasPrefix(other.Path, storage.Path) {
// If storages have the same sub directory, that is allowed
if filepath.Dir(storage.Path) == filepath.Dir(other.Path) {
continue
}
return fmt.Errorf("storage paths may not nest: %q and %q", storage.Name, other.Name)
}
}
}
return nil
}
// StoragePath looks up the base path for storageName. The second boolean
// return value indicates if anything was found.
func (cfg *Cfg) StoragePath(storageName string) (string, bool) {
storage, ok := cfg.Storage(storageName)
return storage.Path, ok
}
// Storage looks up storageName.
func (cfg *Cfg) Storage(storageName string) (Storage, bool) {
for _, storage := range cfg.Storages {
if storage.Name == storageName {
return storage, true
}
}
return Storage{}, false
}
// InternalSocketDir returns the location of the internal socket directory.
func (cfg *Cfg) InternalSocketDir() string {
return filepath.Join(cfg.RuntimeDir, "sock.d")
}
// InternalSocketPath is the path to the internal Gitaly socket.
func (cfg *Cfg) InternalSocketPath() string {
return filepath.Join(cfg.InternalSocketDir(), "intern")
}
func (cfg *Cfg) validateBinDir() error {
if len(cfg.BinDir) == 0 {
return fmt.Errorf("bin_dir: is not set")
}
if err := validateIsDirectory(cfg.BinDir, "bin_dir"); err != nil {
return err
}
var err error
cfg.BinDir, err = filepath.Abs(cfg.BinDir)
return err
}
func (cfg *Cfg) validateRuntimeDir() error {
if len(cfg.RuntimeDir) == 0 {
return fmt.Errorf("runtime_dir: is not set")
}
if err := validateIsDirectory(cfg.RuntimeDir, "runtime_dir"); err != nil {
return err
}
var err error
cfg.RuntimeDir, err = filepath.Abs(cfg.RuntimeDir)
return err
}
// validateGitConfigKey does a best-effort check whether or not a given git config key is valid. It
// does not allow for assignments in keys, which is overly strict and does not allow some valid
// keys. It does avoid misinterpretation of keys though and should catch many cases of
// misconfiguration.
func validateGitConfigKey(key string) error {
if key == "" {
return errors.New("key cannot be empty")
}
if strings.Contains(key, "=") {
return errors.New("key cannot contain assignment")
}
if !strings.Contains(key, ".") {
return errors.New("key must contain at least one section")
}
if strings.HasPrefix(key, ".") || strings.HasSuffix(key, ".") {
return errors.New("key must not start or end with a dot")
}
return nil
}
func (cfg *Cfg) validateGit() error {
for _, configPair := range cfg.Git.Config {
if err := validateGitConfigKey(configPair.Key); err != nil {
return fmt.Errorf("invalid configuration key %q: %w", configPair.Key, err)
}
if configPair.Value == "" {
return fmt.Errorf("invalid configuration value: %q", configPair.Value)
}
}
return nil
}
func (cfg *Cfg) validateToken() error {
if !cfg.Auth.Transitioning || len(cfg.Auth.Token) == 0 {
return nil
}
log.Warn("Authentication is enabled but not enforced because transitioning=true. Gitaly will accept unauthenticated requests.")
return nil
}
func (cfg *Cfg) validateInternalSocketDir() error {
if err := validateIsDirectory(cfg.InternalSocketDir(), "internal_socket_dir"); err != nil {
return err
}
if err := trySocketCreation(cfg.InternalSocketDir()); err != nil {
return fmt.Errorf("failed creating internal test socket: %w", err)
}
return nil
}
func trySocketCreation(dir string) error {
// To validate the socket can actually be created, we open and close a socket.
// Any error will be assumed persistent for when the gitaly-ruby sockets are created
// and thus fatal at boot time.
//
// There are two kinds of internal sockets we create: the internal server socket
// called "intern", and then the Ruby worker sockets called "ruby.$N", with "$N"
// being the number of the Ruby worker. Given that we typically wouldn't spawn
// hundreds of Ruby workers, the maximum internal socket path name would thus be 7
// characters long.
socketPath := filepath.Join(dir, "tsocket")
defer func() { _ = os.Remove(socketPath) }()
// Attempt to create an actual socket and not just a file to catch socket path length problems
l, err := net.Listen("unix", socketPath)
if err != nil {
var errno syscall.Errno
if errors.As(err, &errno) && errno == syscall.EINVAL {
return fmt.Errorf("%w: your socket path is likely too long, please change Gitaly's runtime directory", errno)
}
return fmt.Errorf("socket could not be created in %s: %w", dir, err)
}
return l.Close()
}
// defaultMaintenanceWindow specifies a 10 minute job that runs daily at +1200
// GMT time
func defaultMaintenanceWindow(storages []Storage) DailyJob {
storageNames := make([]string, len(storages))
for i, s := range storages {
storageNames[i] = s.Name
}
return DailyJob{
Hour: 12,
Minute: 0,
Duration: Duration(10 * time.Minute),
Storages: storageNames,
}
}
func (cfg *Cfg) validateMaintenance() error {
dm := cfg.DailyMaintenance
sNames := map[string]struct{}{}
for _, s := range cfg.Storages {
sNames[s.Name] = struct{}{}
}
for _, sName := range dm.Storages {
if _, ok := sNames[sName]; !ok {
return fmt.Errorf("daily maintenance specified storage %q does not exist in configuration", sName)
}
}
if dm.Hour > 23 {
return fmt.Errorf("daily maintenance specified hour '%d' outside range (0-23)", dm.Hour)
}
if dm.Minute > 59 {
return fmt.Errorf("daily maintenance specified minute '%d' outside range (0-59)", dm.Minute)
}
if dm.Duration.Duration() > 24*time.Hour {
return fmt.Errorf("daily maintenance specified duration %s must be less than 24 hours", dm.Duration.Duration())
}
return nil
}
func (cfg *Cfg) validateCgroups() error {
cg := cfg.Cgroups
if cg.MemoryBytes > 0 && (cg.Repositories.MemoryBytes > cg.MemoryBytes) {
return errors.New("cgroups.repositories: memory limit cannot exceed parent")
}
if cg.MemoryBytes > 0 && (cg.Repositories.CPUShares > cg.CPUShares) {
return errors.New("cgroups.repositories: cpu shares cannot exceed parent")
}
return nil
}
var (
errPackObjectsCacheNegativeMaxAge = errors.New("pack_objects_cache.max_age cannot be negative")
errPackObjectsCacheNoStorages = errors.New("pack_objects_cache: cannot pick default cache directory: no storages")
errPackObjectsCacheRelativePath = errors.New("pack_objects_cache: storage directory must be absolute path")
)
func (cfg *Cfg) configurePackObjectsCache() error {
poc := &cfg.PackObjectsCache
if !poc.Enabled {
return nil
}
if poc.MaxAge < 0 {
return errPackObjectsCacheNegativeMaxAge
}
if poc.MaxAge == 0 {
poc.MaxAge = Duration(5 * time.Minute)
}
if poc.Dir == "" {
if len(cfg.Storages) == 0 {
return errPackObjectsCacheNoStorages
}
poc.Dir = filepath.Join(cfg.Storages[0].Path, GitalyDataPrefix, "PackObjectsCache")
}
if !filepath.IsAbs(poc.Dir) {
return errPackObjectsCacheRelativePath
}
return nil
}
|