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

config.go « config « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0f041f2a9aa47eb992c61a4ef44deed4e5890191 (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
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
package config

import (
	"fmt"
	"io"
	"io/ioutil"
	"net"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"sync"
	"time"

	"github.com/BurntSushi/toml"
	"github.com/kelseyhightower/envconfig"
	log "github.com/sirupsen/logrus"
	"gitlab.com/gitlab-org/gitaly/internal/config/auth"
	internallog "gitlab.com/gitlab-org/gitaly/internal/config/log"
	"gitlab.com/gitlab-org/gitaly/internal/config/prometheus"
	"gitlab.com/gitlab-org/gitaly/internal/config/sentry"
	"gitlab.com/gitlab-org/gitaly/internal/helper/text"
)

var (
	// Config stores the global configuration
	Config Cfg

	hooks []func(Cfg) error
)

// 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"`
	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"`
	GitlabShell                GitlabShell       `toml:"gitlab-shell"`
	Concurrency                []Concurrency     `toml:"concurrency"`
	GracefulRestartTimeout     time.Duration
	GracefulRestartTimeoutToml duration `toml:"graceful_restart_timeout"`
	InternalSocketDir          string   `toml:"internal_socket_dir"`
}

// TLS configuration
type TLS struct {
	CertPath string `toml:"certificate_path"`
	KeyPath  string `toml:"key_path"`
}

// GitlabShell contains the settings required for executing `gitlab-shell`
type GitlabShell struct {
	Dir string `toml:"dir"`
}

// Git contains the settings for the Git executable
type Git struct {
	BinPath string `toml:"bin_path"`

	CatfileCacheSize int `toml:"catfile_cache_size"`
}

// 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
type Concurrency struct {
	RPC        string `toml:"rpc"`
	MaxPerRepo int    `toml:"max_per_repo"`
}

// Load initializes the Config variable from file and the environment.
//  Environment variables take precedence over the file.
func Load(file io.Reader) error {
	Config = Cfg{}

	if _, err := toml.DecodeReader(file, &Config); err != nil {
		return fmt.Errorf("load toml: %v", err)
	}

	if err := envconfig.Process("gitaly", &Config); err != nil {
		return fmt.Errorf("envconfig: %v", err)
	}

	Config.setDefaults()

	return nil
}

// RegisterHook adds a post-validation callback. Your hook should only
// access config via the Cfg instance it gets passed. This avoids race
// conditions during testing, when the global config.Config instance gets
// updated after these hooks have run.
func RegisterHook(f func(c Cfg) error) {
	hooks = append(hooks, f)
}

// Validate checks the current Config for sanity. It also runs all hooks
// registered with RegisterHook.
func Validate() error {
	for _, err := range []error{
		validateListeners(),
		validateStorages(),
		validateToken(),
		SetGitPath(),
		validateShell(),
		ConfigureRuby(),
		validateBinDir(),
		validateInternalSocketDir(),
		validateHooks(),
	} {
		if err != nil {
			return err
		}
	}

	for _, f := range hooks {
		if err := f(Config); err != nil {
			return err
		}
	}

	return nil
}

func (c *Cfg) setDefaults() {
	c.GracefulRestartTimeout = c.GracefulRestartTimeoutToml.Duration
	if c.GracefulRestartTimeout == 0 {
		c.GracefulRestartTimeout = 1 * time.Minute
	}
}

func validateListeners() error {
	if len(Config.SocketPath) == 0 && len(Config.ListenAddr) == 0 {
		return fmt.Errorf("invalid listener config: at least one of socket_path and listen_addr must be set")
	}
	return nil
}

func validateShell() error {
	if len(Config.GitlabShell.Dir) == 0 {
		return fmt.Errorf("gitlab-shell.dir is not set")
	}

	return validateIsDirectory(Config.GitlabShell.Dir, "gitlab-shell.dir")
}

func checkExecutable(path string) error {
	fi, err := os.Stat(path)
	if err != nil {
		return err
	}

	if fi.Mode()&0755 < 0755 {
		return fmt.Errorf("not executable: %v", path)
	}

	return nil
}

type hookErrs struct {
	errors []error
}

func (h *hookErrs) Error() string {
	var errStrings []string
	for _, err := range h.errors {
		errStrings = append(errStrings, err.Error())
	}

	return strings.Join(errStrings, ", ")
}

func (h *hookErrs) Add(err error) {
	h.errors = append(h.errors, err)
}

func validateHooks() error {
	if os.Getenv("GITALY_TESTING_NO_GIT_HOOKS") == "1" {
		return nil
	}

	errs := &hookErrs{}

	for _, hookName := range []string{"pre-receive", "post-receive", "update"} {
		if err := checkExecutable(filepath.Join(Config.Ruby.Dir, "git-hooks", hookName)); err != nil {
			errs.Add(err)
			continue
		}
	}

	if len(errs.errors) > 0 {
		return errs
	}

	return nil
}

func validateIsDirectory(path, name string) error {
	s, err := os.Stat(path)
	if err != nil {
		return err
	}
	if !s.IsDir() {
		return fmt.Errorf("not a directory: %q", path)
	}

	log.WithField("dir", path).
		Debugf("%s set", name)

	return nil
}

func validateStorages() error {
	if len(Config.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 Config.Storages {
		if storage.Name == "" {
			return fmt.Errorf("empty storage name in %v", storage)
		}

		if storage.Path == "" {
			return fmt.Errorf("empty storage path in %v", storage)
		}

		if fs, err := os.Stat(storage.Path); err != nil || !fs.IsDir() {
			return fmt.Errorf("storage paths have to exist %v", storage)
		}

		stPath := filepath.Clean(storage.Path)
		for j := 0; j < i; j++ {
			other := Config.Storages[j]
			if other.Name == storage.Name {
				return fmt.Errorf("storage %q is defined more than once", storage.Name)
			}

			otherPath := filepath.Clean(other.Path)
			if stPath == otherPath {
				// This is weird but we allow it for legacy gitlab.com reasons.
				continue
			}

			if strings.HasPrefix(stPath, otherPath) || strings.HasPrefix(otherPath, stPath) {
				// If storages have the same sub directory, that is allowed
				if filepath.Dir(stPath) == filepath.Dir(otherPath) {
					continue
				}
				return fmt.Errorf("storage paths may not nest: %q and %q", storage.Name, other.Name)
			}
		}
	}

	return nil
}

// SetGitPath populates the variable GitPath with the path to the `git`
// executable. It warns if no path was specified in the configuration.
func SetGitPath() error {
	if Config.Git.BinPath != "" {
		return nil
	}

	resolvedPath, err := exec.LookPath("git")
	if err != nil {
		return err
	}

	log.WithFields(log.Fields{
		"resolvedPath": resolvedPath,
	}).Warn("git path not configured. Using default path resolution")

	Config.Git.BinPath = resolvedPath

	return nil
}

// StoragePath looks up the base path for storageName. The second boolean
// return value indicates if anything was found.
func (c Cfg) StoragePath(storageName string) (string, bool) {
	storage, ok := c.Storage(storageName)
	return storage.Path, ok
}

// Storage looks up storageName.
func (c Cfg) Storage(storageName string) (Storage, bool) {
	for _, storage := range c.Storages {
		if storage.Name == storageName {
			return storage, true
		}
	}
	return Storage{}, false
}

func validateBinDir() error {
	if err := validateIsDirectory(Config.BinDir, "bin_dir"); err != nil {
		log.WithError(err).Warn("Gitaly bin directory is not configured")
		// TODO this must become a fatal error
		return nil
	}

	var err error
	Config.BinDir, err = filepath.Abs(Config.BinDir)
	return err
}

func validateToken() error {
	if !Config.Auth.Transitioning || len(Config.Auth.Token) == 0 {
		return nil
	}

	log.Warn("Authentication is enabled but not enforced because transitioning=true. Gitaly will accept unauthenticated requests.")
	return nil
}

var (
	lazyInit                   sync.Once
	generatedInternalSocketDir string
)

func generateSocketPath() {
	// The socket path must be short-ish because listen(2) fails on long
	// socket paths. We hope/expect that ioutil.TempDir 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

	var err error
	generatedInternalSocketDir, err = ioutil.TempDir("", "gitaly-internal")
	if err != nil {
		log.Fatalf("create ruby server socket directory: %v", err)
	}
}

// InternalSocketDir will generate a temp dir for internal sockets if one is not provided in the config
func InternalSocketDir() string {
	if Config.InternalSocketDir != "" {
		return Config.InternalSocketDir
	}

	if generatedInternalSocketDir == "" {
		lazyInit.Do(generateSocketPath)
	}

	return generatedInternalSocketDir
}

// GitalyInternalSocketPath is the path to the internal gitaly socket
func GitalyInternalSocketPath() string {
	socketDir := InternalSocketDir()
	if socketDir == "" {
		panic("internal socket directory is missing")
	}

	return filepath.Join(socketDir, "internal.sock")
}

// GeneratedInternalSocketDir returns the path to the generated internal socket directory
func GeneratedInternalSocketDir() string {
	return generatedInternalSocketDir
}

func validateInternalSocketDir() error {
	if Config.InternalSocketDir == "" {
		return nil
	}

	dir := Config.InternalSocketDir

	f, err := os.Stat(dir)
	switch {
	case err != nil:
		return fmt.Errorf("InternalSocketDir: %s", err)
	case !f.IsDir():
		return fmt.Errorf("InternalSocketDir %s is not a directory", dir)
	}

	return trySocketCreation(dir)
}

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
	b, err := text.RandomHex(4)
	if err != nil {
		return err
	}

	socketPath := filepath.Join(dir, fmt.Sprintf("test-%s.sock", b))
	defer 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 {
		return fmt.Errorf("socket could not be created in %s: %s", dir, err)
	}

	return l.Close()
}