diff --git a/cmd/gitaly-hooks/hooks_test.go b/cmd/gitaly-hooks/hooks_test.go index bdca5d353dd901407d2923f7ab6a7005e6200324..5df748688f276c7272ede0ed97b58eb2dc2f7933 100644 --- a/cmd/gitaly-hooks/hooks_test.go +++ b/cmd/gitaly-hooks/hooks_test.go @@ -123,10 +123,10 @@ func TestHooksPrePostReceive(t *testing.T) { defer cleanup() if featureSet.IsEnabled("use_gitaly_gitlabshell_config") { - config.Config.GitlabShell.GitlabURL = ts.URL - config.Config.GitlabShell.SecretFile = filepath.Join(tempGitlabShellDir, ".gitlab_shell_secret") - config.Config.GitlabShell.HTTPSettings.User = gitlabUser - config.Config.GitlabShell.HTTPSettings.Password = gitlabPassword + config.Config.Gitlab.URL = ts.URL + config.Config.Gitlab.SecretFile = filepath.Join(tempGitlabShellDir, ".gitlab_shell_secret") + config.Config.Gitlab.HTTPSettings.User = gitlabUser + config.Config.Gitlab.HTTPSettings.Password = gitlabPassword } var stderr, stdout bytes.Buffer @@ -218,7 +218,7 @@ func TestHooksUpdate(t *testing.T) { for _, featureSet := range featureSets { t.Run(fmt.Sprintf("enabled features: %v", featureSet), func(t *testing.T) { if featureSet.IsEnabled("use_gitaly_gitlabshell_config") { - config.Config.GitlabShell.CustomHooksDir = customHooksDir + config.Config.Hooks.CustomHooksDir = customHooksDir } testHooksUpdate(t, tempGitlabShellDir, socket, token, testhelper.GlHookValues{ @@ -332,8 +332,8 @@ func TestHooksPostReceiveFailed(t *testing.T) { testhelper.WriteShellSecretFile(t, tempGitlabShellDir, secretToken) config.Config.GitlabShell.Dir = tempGitlabShellDir - config.Config.GitlabShell.GitlabURL = ts.URL - config.Config.GitlabShell.SecretFile = filepath.Join(tempGitlabShellDir, ".gitlab_shell_secret") + config.Config.Gitlab.URL = ts.URL + config.Config.Gitlab.SecretFile = filepath.Join(tempGitlabShellDir, ".gitlab_shell_secret") featureSets, err := testhelper.NewFeatureSets([]string{featureflag.HooksRPC}) require.NoError(t, err) @@ -416,9 +416,7 @@ func TestHooksNotAllowed(t *testing.T) { testhelper.WriteShellSecretFile(t, tempGitlabShellDir, "the wrong token") config.Config.GitlabShell.Dir = tempGitlabShellDir - - config.Config.GitlabShell.Dir = tempGitlabShellDir - config.Config.GitlabShell.GitlabURL = ts.URL + config.Config.Gitlab.URL = ts.URL customHookOutputPath, cleanup := testhelper.WriteEnvToCustomHook(t, testRepoPath, "post-receive") defer cleanup() diff --git a/config.toml.example b/config.toml.example index aaab83c21f44a402bf9b877dc6106a3c1b13ff7b..d89f2da8a358b7b14e27415766cea969e23fbe02 100644 --- a/config.toml.example +++ b/config.toml.example @@ -86,11 +86,14 @@ dir = "/home/git/gitaly/ruby" [gitlab-shell] # The directory where gitlab-shell is installed dir = "/home/git/gitlab-shell" -secret_file = "/home/git/gitlab-shell/.gitlab_shell_secret" + +[hooks] custom_hooks_dir = "/home/git/custom_hooks" -gitlab_url = "http://localhost:8080" -[gitlab-shell.http-settings] +[gitlab] +secret_file = "/home/git/gitlab-shell/.gitlab_shell_secret" +gitlab_url = "http://localhost:8080" +[gitlab.http-settings] # read_timeout = 300 # user = someone # password = somepass diff --git a/internal/config/config.go b/internal/config/config.go index 0b31aaf0049cc27c3a62c191b7e379870854ec13..98dbef3ccfa9da8383aec232cefcaa84d7944829 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -43,7 +43,9 @@ type Cfg struct { 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"` GracefulRestartTimeout time.Duration GracefulRestartTimeoutToml duration `toml:"graceful_restart_timeout"` @@ -58,11 +60,19 @@ type TLS struct { // GitlabShell contains the settings required for executing `gitlab-shell` type GitlabShell struct { - CustomHooksDir string `toml:"custom_hooks_dir" json:"custom_hooks_dir"` - Dir string `toml:"dir" json:"dir"` - GitlabURL string `toml:"gitlab_url" json:"gitlab_url"` - HTTPSettings HTTPSettings `toml:"http-settings" json:"http_settings"` - SecretFile string `toml:"secret_file" json:"secret_file"` + 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"` + 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"` } type HTTPSettings struct { @@ -164,12 +174,12 @@ func (c *Cfg) setDefaults() { c.GracefulRestartTimeout = 1 * time.Minute } - if c.GitlabShell.SecretFile == "" { - c.GitlabShell.SecretFile = filepath.Join(c.GitlabShell.Dir, ".gitlab_shell_secret") + if c.Gitlab.SecretFile == "" { + c.Gitlab.SecretFile = filepath.Join(c.GitlabShell.Dir, ".gitlab_shell_secret") } - if c.GitlabShell.CustomHooksDir == "" { - c.GitlabShell.CustomHooksDir = filepath.Join(c.GitlabShell.Dir, "hooks") + if c.Hooks.CustomHooksDir == "" { + c.Hooks.CustomHooksDir = filepath.Join(c.GitlabShell.Dir, "hooks") } } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 82b9167966c35123f9272d05539a1f1a971dc44b..cf13aa9bf643a5e7015c3446af4e894fa72d41f8 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -647,18 +647,20 @@ func TestLoadGracefulRestartTimeout(t *testing.T) { } func TestGitlabShellDefaults(t *testing.T) { - expectedDefault := GitlabShell{ - Dir: "/dir", - SecretFile: "/dir/.gitlab_shell_secret", + gitlabShellDir := "/gitlab_shell_dir" + expectedHooks := Hooks{ CustomHooksDir: "/dir/hooks", } + expectedGitlab := Gitlab{ + SecretFile: "/dir/.gitlab_shell_secret", + } tmpFile := configFileReader(fmt.Sprintf(`[gitlab-shell] -dir = '%s'`, expectedDefault.Dir)) +dir = '%s'`, gitlabShellDir)) require.NoError(t, Load(tmpFile)) - require.Equal(t, expectedDefault.SecretFile, Config.GitlabShell.SecretFile) - require.Equal(t, expectedDefault.CustomHooksDir, Config.GitlabShell.CustomHooksDir) + require.Equal(t, expectedHooks, Config.Hooks) + require.Equal(t, expectedGitlab, Config.Gitlab) } func TestValidateInternalSocketDir(t *testing.T) { diff --git a/internal/gitlabshell/env.go b/internal/gitlabshell/env.go index 24a1f62966587524bacde9b54f238edb499c860c..decadf0eaa1fbbb88f7a52b6e9ceff91febf3ac1 100644 --- a/internal/gitlabshell/env.go +++ b/internal/gitlabshell/env.go @@ -2,6 +2,7 @@ package gitlabshell import ( "encoding/json" + "fmt" "gitlab.com/gitlab-org/gitaly/internal/config" ) @@ -26,15 +27,16 @@ type Config struct { // EnvFromConfig returns a set of environment variables from a config struct relevant to gitlab shell func EnvFromConfig(cfg config.Cfg) ([]string, error) { + fmt.Printf("\n GITLAB SHELL DIR FROM ENV: %v\n", cfg.GitlabShell.Dir) gitlabShellConfig := Config{ - CustomHooksDir: cfg.GitlabShell.CustomHooksDir, - GitlabURL: cfg.GitlabShell.GitlabURL, - HTTPSettings: cfg.GitlabShell.HTTPSettings, + CustomHooksDir: cfg.Hooks.CustomHooksDir, + GitlabURL: cfg.Gitlab.URL, + HTTPSettings: cfg.Gitlab.HTTPSettings, LogFormat: cfg.Logging.Format, LogLevel: cfg.Logging.Level, LogPath: cfg.Logging.Dir, RootPath: cfg.GitlabShell.Dir, //GITLAB_SHELL_DIR has been deprecated - SecretFile: cfg.GitlabShell.SecretFile, + SecretFile: cfg.Gitlab.SecretFile, } gitlabShellConfigString, err := json.Marshal(&gitlabShellConfig) diff --git a/internal/gitlabshell/env_test.go b/internal/gitlabshell/env_test.go index a5b9249c6d225b67410c2d750c6ed1d84b187320..9eac8f07674c1273906f12bd89e32e19c325ef89 100644 --- a/internal/gitlabshell/env_test.go +++ b/internal/gitlabshell/env_test.go @@ -29,11 +29,10 @@ func TestGitHooksConfig(t *testing.T) { config.Config.Logging.Dir = loggingDir config.Config.Logging.Level = "fatal" config.Config.Logging.Format = "my-custom-format" - - config.Config.GitlabShell = config.GitlabShell{ - CustomHooksDir: "/path/to/custom_hooks", - Dir: "../../ruby/gitlab-shell", - GitlabURL: "http://gitlaburl.com", + config.Config.GitlabShell.Dir = "../../ruby/gitlab-shell" + config.Config.Hooks.CustomHooksDir = "/path/to/custom_hooks" + config.Config.Gitlab = config.Gitlab{ + URL: "http://gitlaburl.com", HTTPSettings: config.HTTPSettings{ ReadTimeout: 100, User: "user_name", @@ -63,19 +62,19 @@ func TestGitHooksConfig(t *testing.T) { require.NoError(t, json.NewDecoder(&stdout).Decode(&rubyConfigMap)) require.Equal(t, config.Config.Logging.Level, rubyConfigMap["log_level"]) require.Equal(t, config.Config.Logging.Format, rubyConfigMap["log_format"]) - require.Equal(t, config.Config.GitlabShell.SecretFile, rubyConfigMap["secret_file"]) - require.Equal(t, config.Config.GitlabShell.CustomHooksDir, rubyConfigMap["custom_hooks_dir"]) - require.Equal(t, config.Config.GitlabShell.GitlabURL, rubyConfigMap["gitlab_url"]) + require.Equal(t, config.Config.Gitlab.SecretFile, rubyConfigMap["secret_file"]) + require.Equal(t, config.Config.Hooks.CustomHooksDir, rubyConfigMap["custom_hooks_dir"]) + require.Equal(t, config.Config.Gitlab.URL, rubyConfigMap["gitlab_url"]) // HTTP Settings httpSettings, ok := rubyConfigMap["http_settings"].(map[string]interface{}) require.True(t, ok) - require.Equal(t, float64(config.Config.GitlabShell.HTTPSettings.ReadTimeout), httpSettings["read_timeout"]) - require.Equal(t, config.Config.GitlabShell.HTTPSettings.User, httpSettings["user"]) - require.Equal(t, config.Config.GitlabShell.HTTPSettings.Password, httpSettings["password"]) - require.Equal(t, config.Config.GitlabShell.HTTPSettings.CAFile, httpSettings["ca_file"]) - require.Equal(t, config.Config.GitlabShell.HTTPSettings.CAPath, httpSettings["ca_path"]) - require.Equal(t, config.Config.GitlabShell.HTTPSettings.SelfSigned, httpSettings["self_signed_cert"]) + require.Equal(t, float64(config.Config.Gitlab.HTTPSettings.ReadTimeout), httpSettings["read_timeout"]) + require.Equal(t, config.Config.Gitlab.HTTPSettings.User, httpSettings["user"]) + require.Equal(t, config.Config.Gitlab.HTTPSettings.Password, httpSettings["password"]) + require.Equal(t, config.Config.Gitlab.HTTPSettings.CAFile, httpSettings["ca_file"]) + require.Equal(t, config.Config.Gitlab.HTTPSettings.CAPath, httpSettings["ca_path"]) + require.Equal(t, config.Config.Gitlab.HTTPSettings.SelfSigned, httpSettings["self_signed_cert"]) dir := filepath.Dir(rubyConfigMap["log_file"].(string)) require.Equal(t, config.Config.Logging.Dir, dir) diff --git a/internal/service/hooks/update.go b/internal/service/hooks/update.go index 9d0e2091b45b72ae2a1605dac12b5989c4e55f5e..5741da00c47720aef4d350bbef9c004a594764c0 100644 --- a/internal/service/hooks/update.go +++ b/internal/service/hooks/update.go @@ -83,7 +83,7 @@ func (s *server) UpdateHook(in *gitalypb.UpdateHookRequest, stream gitalypb.Hook if err != nil { return err } - executor, err := newCustomHooksExecutor(repoPath, config.Config.GitlabShell.CustomHooksDir, "update") + executor, err := newCustomHooksExecutor(repoPath, config.Config.Hooks.CustomHooksDir, "update") if err != nil { return helper.ErrInternal(err) } diff --git a/internal/service/smarthttp/receive_pack.go b/internal/service/smarthttp/receive_pack.go index 1e895e9d423cc8f190e4c7e94a2d04c04a9c699a..b6161d651eba65648af95a5b342ecbdd11e6dd90 100644 --- a/internal/service/smarthttp/receive_pack.go +++ b/internal/service/smarthttp/receive_pack.go @@ -1,9 +1,12 @@ package smarthttp import ( + "fmt" + "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus" log "github.com/sirupsen/logrus" "gitlab.com/gitlab-org/gitaly/internal/command" + "gitlab.com/gitlab-org/gitaly/internal/config" "gitlab.com/gitlab-org/gitaly/internal/git" "gitlab.com/gitlab-org/gitaly/internal/helper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" @@ -38,6 +41,7 @@ func (s *server) PostReceivePack(stream gitalypb.SmartHTTPService_PostReceivePac return stream.Send(&gitalypb.PostReceivePackResponse{Data: p}) }) + fmt.Printf("\nINSIDE PostReceiveWithTransactions: %v\n", config.Config.GitlabShell.Dir) hookEnv, err := git.ReceivePackHookEnv(ctx, req) if err != nil { return err diff --git a/internal/service/smarthttp/receive_pack_test.go b/internal/service/smarthttp/receive_pack_test.go index d60672ade30e7aa0142e86bf080013016148d671..655137590e006374d4b770a52bff2b2781bde9fc 100644 --- a/internal/service/smarthttp/receive_pack_test.go +++ b/internal/service/smarthttp/receive_pack_test.go @@ -403,8 +403,8 @@ func testPostReceivePackToHooks(t *testing.T, callRPC bool) { testhelper.WriteCustomHook(testRepoPath, "pre-receive", []byte(testhelper.CheckNewObjectExists)) - config.Config.GitlabShell.GitlabURL = ts.URL - config.Config.GitlabShell.SecretFile = filepath.Join(tempGitlabShellDir, ".gitlab_shell_secret") + config.Config.Gitlab.URL = ts.URL + config.Config.Gitlab.SecretFile = filepath.Join(tempGitlabShellDir, ".gitlab_shell_secret") defer func(override string) { hooks.Override = override @@ -462,6 +462,7 @@ func runSmartHTTPHookServiceServer(t *testing.T) (*grpc.Server, string) { } func TestPostReceiveWithTransactions(t *testing.T) { + fmt.Printf("\nSTARTING TestPostReceiveWithTransactions: %v\n", config.Config.GitlabShell.Dir) defer func(cfg config.Cfg) { config.Config = cfg }(config.Config) @@ -499,7 +500,11 @@ func TestPostReceiveWithTransactions(t *testing.T) { gitlabShellDir, cleanup := testhelper.CreateTemporaryGitlabShellDir(t) defer cleanup() + config.Config.GitlabShell.Dir = gitlabShellDir + + fmt.Printf("\nINSIDE TestPostReceiveWithTransactions: %v\n", config.Config.GitlabShell.Dir) + testhelper.WriteTemporaryGitlabShellConfigFile(t, gitlabShellDir, testhelper.GitlabShellConfig{ diff --git a/internal/service/ssh/receive_pack_test.go b/internal/service/ssh/receive_pack_test.go index 07a0016c73a0184634a20cdf0eb39c8e46521f3c..cbe2fd2be9396eb39bfa2cdf09089b668bf4e42f 100644 --- a/internal/service/ssh/receive_pack_test.go +++ b/internal/service/ssh/receive_pack_test.go @@ -243,8 +243,8 @@ func TestSSHReceivePackToHooks(t *testing.T) { testhelper.WriteTemporaryGitlabShellConfigFile(t, tempGitlabShellDir, testhelper.GitlabShellConfig{GitlabURL: ts.URL}) testhelper.WriteShellSecretFile(t, tempGitlabShellDir, secretToken) - config.Config.GitlabShell.GitlabURL = ts.URL - config.Config.GitlabShell.SecretFile = filepath.Join(tempGitlabShellDir, ".gitlab_shell_secret") + config.Config.Gitlab.URL = ts.URL + config.Config.Gitlab.SecretFile = filepath.Join(tempGitlabShellDir, ".gitlab_shell_secret") testhelper.WriteCustomHook(cloneDetails.RemoteRepoPath, "pre-receive", []byte(testhelper.CheckNewObjectExists))