From eb49052f9e7edac32c4ad3f36a6a7e49f955bf87 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 12 Feb 2021 07:57:47 +0100 Subject: gitaly: Set up requested hooks bitmap With the infrastructure being in place to support executing requested hooks only, this commit converts all callsites of `NewHooksPayload()` to also pass the bitmap of hooks they want to execute. This finally fixes an issue where we could've accidentally executed an unwanted hook only because we set up `core.hooksPath` for a git command. --- cmd/gitaly-hooks/hooks_test.go | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'cmd/gitaly-hooks') diff --git a/cmd/gitaly-hooks/hooks_test.go b/cmd/gitaly-hooks/hooks_test.go index 96240ee28..b64e23cf0 100644 --- a/cmd/gitaly-hooks/hooks_test.go +++ b/cmd/gitaly-hooks/hooks_test.go @@ -43,7 +43,7 @@ func envForHooks(t testing.TB, gitlabShellDir string, repo *gitalypb.Repository, UserID: glHookValues.GLID, Username: glHookValues.GLUsername, Protocol: glHookValues.GLProtocol, - }).Env() + }, git.AllHooks).Env() require.NoError(t, err) env := append(os.Environ(), []string{ @@ -469,6 +469,7 @@ func TestHooksPostReceiveFailed(t *testing.T) { Username: glUsername, Protocol: glProtocol, }, + git.PostReceiveHook, ).Env() require.NoError(t, err) @@ -726,3 +727,38 @@ func TestGitalyHooksPackObjects(t *testing.T) { }) } } + +func TestRequestedHooks(t *testing.T) { + for hook, hookName := range map[git.Hook]string{ + git.ReferenceTransactionHook: "reference-transaction", + git.UpdateHook: "update", + git.PreReceiveHook: "pre-receive", + git.PostReceiveHook: "post-receive", + git.PackObjectsHook: "git", + } { + t.Run(hookName, func(t *testing.T) { + t.Run("unrequested hook is ignored", func(t *testing.T) { + payload, err := git.NewHooksPayload(config.Config, &gitalypb.Repository{}, nil, nil, nil, git.AllHooks&^hook).Env() + require.NoError(t, err) + + cmd := exec.Command(filepath.Join(config.Config.BinDir, "gitaly-hooks"), hookName) + cmd.Env = []string{payload} + require.NoError(t, cmd.Run()) + }) + + t.Run("requested hook runs", func(t *testing.T) { + payload, err := git.NewHooksPayload(config.Config, &gitalypb.Repository{}, nil, nil, nil, hook).Env() + require.NoError(t, err) + + cmd := exec.Command(filepath.Join(config.Config.BinDir, "gitaly-hooks"), hookName) + cmd.Env = []string{payload} + + // We simply check that there is an error here as an indicator that + // the hook logic ran. We don't care for the actual error because we + // know that in the previous testcase without the hook being + // requested, there was no error. + require.Error(t, cmd.Run(), "hook should have run and failed due to incomplete setup") + }) + }) + } +} -- cgit v1.2.3