diff options
author | John Cai <jcai@gitlab.com> | 2022-06-03 17:44:12 +0300 |
---|---|---|
committer | John Cai <jcai@gitlab.com> | 2022-06-15 18:33:49 +0300 |
commit | e42bcdc71bcd1118b1b0a6671e33293941865aea (patch) | |
tree | 820f3ec44fd54daf112505153ebfc9aecbf8de3d | |
parent | 8171885cedaf3dda9648275ca27f34daf7af818f (diff) |
git: Add protocol, user_id, username to WithPackObjectsHookEnv
Add user_id, username, protocol to the payload we send to the hook so
that it can call the PackObjectsHookWithSidechannel with these values.
-rw-r--r-- | internal/git/hooks_options.go | 18 | ||||
-rw-r--r-- | internal/git/hooks_options_test.go | 28 | ||||
-rw-r--r-- | internal/gitaly/service/smarthttp/upload_pack.go | 2 | ||||
-rw-r--r-- | internal/gitaly/service/ssh/upload_pack.go | 2 |
4 files changed, 46 insertions, 4 deletions
diff --git a/internal/git/hooks_options.go b/internal/git/hooks_options.go index 6c29a33e7..5a24f8a9d 100644 --- a/internal/git/hooks_options.go +++ b/internal/git/hooks_options.go @@ -9,6 +9,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/git/repository" "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/v15/internal/log" + "gitlab.com/gitlab-org/gitaly/v15/internal/metadata" "gitlab.com/gitlab-org/gitaly/v15/internal/metadata/featureflag" "gitlab.com/gitlab-org/gitaly/v15/internal/transaction/txinfo" "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" @@ -50,7 +51,7 @@ func WithRefTxHook(repo repository.GitRepo) CmdOpt { } // WithPackObjectsHookEnv provides metadata for gitaly-hooks so it can act as a pack-objects hook. -func WithPackObjectsHookEnv(repo *gitalypb.Repository) CmdOpt { +func WithPackObjectsHookEnv(repo *gitalypb.Repository, protocol string) CmdOpt { return func(ctx context.Context, cfg config.Cfg, gitCmdFactory CommandFactory, cc *cmdCfg) error { if !cfg.PackObjectsCache.Enabled { return nil @@ -60,7 +61,20 @@ func WithPackObjectsHookEnv(repo *gitalypb.Repository) CmdOpt { return fmt.Errorf("missing repo: %w", ErrInvalidArg) } - if err := cc.configureHooks(ctx, repo, cfg, gitCmdFactory, nil, PackObjectsHook); err != nil { + userDetails := &UserDetails{ + Protocol: protocol, + UserID: metadata.GetValue(ctx, "user_id"), + Username: metadata.GetValue(ctx, "username"), + } + + if err := cc.configureHooks( + ctx, + repo, + cfg, + gitCmdFactory, + userDetails, + PackObjectsHook, + ); err != nil { return fmt.Errorf("pack-objects hook configuration: %w", err) } diff --git a/internal/git/hooks_options_test.go b/internal/git/hooks_options_test.go index 430c1874b..bf6da080e 100644 --- a/internal/git/hooks_options_test.go +++ b/internal/git/hooks_options_test.go @@ -8,8 +8,10 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/command" "gitlab.com/gitlab-org/gitaly/v15/internal/git" "gitlab.com/gitlab-org/gitaly/v15/internal/git/gittest" + "gitlab.com/gitlab-org/gitaly/v15/internal/metadata" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testcfg" + grpcmetadata "google.golang.org/grpc/metadata" ) func TestWithRefHook(t *testing.T) { @@ -55,3 +57,29 @@ func TestWithRefHook(t *testing.T) { }) } } + +func TestWithPackObjectsHookEnv(t *testing.T) { + cfg, repo, _ := testcfg.BuildWithRepo(t) + ctx := testhelper.Context(t) + cfg.PackObjectsCache.Enabled = true + + userID := "user-123" + username := "username" + protocol := "protocol" + + opt := git.WithPackObjectsHookEnv(repo, protocol) + subCmd := git.SubCmd{Name: "upload-pack", Args: []string{"a/b/c"}} + + ctx = grpcmetadata.AppendToOutgoingContext(ctx, "user_id", userID, "username", username) + ctx = metadata.OutgoingToIncoming(ctx) + + cmd, err := gittest.NewCommandFactory(t, cfg, git.WithSkipHooks()).New(ctx, repo, subCmd, opt) + require.NoError(t, err) + + payload, err := git.HooksPayloadFromEnv(cmd.Env()) + require.NoError(t, err) + + require.Equal(t, userID, payload.UserDetails.UserID) + require.Equal(t, username, payload.UserDetails.Username) + require.Equal(t, protocol, payload.UserDetails.Protocol) +} diff --git a/internal/gitaly/service/smarthttp/upload_pack.go b/internal/gitaly/service/smarthttp/upload_pack.go index d97cc1718..b0353da19 100644 --- a/internal/gitaly/service/smarthttp/upload_pack.go +++ b/internal/gitaly/service/smarthttp/upload_pack.go @@ -136,7 +136,7 @@ func (s *server) runUploadPack(ctx context.Context, req basicPostUploadPackReque git.WithStdin(stdin), git.WithGitProtocol(req), git.WithConfig(gitConfig...), - git.WithPackObjectsHookEnv(req.GetRepository()), + git.WithPackObjectsHookEnv(req.GetRepository(), "http"), } cmd, err := s.gitCmdFactory.NewWithoutRepo(ctx, git.SubCmd{ diff --git a/internal/gitaly/service/ssh/upload_pack.go b/internal/gitaly/service/ssh/upload_pack.go index 248524417..1b0ef278f 100644 --- a/internal/gitaly/service/ssh/upload_pack.go +++ b/internal/gitaly/service/ssh/upload_pack.go @@ -121,7 +121,7 @@ func (s *server) sshUploadPack(ctx context.Context, req sshUploadPackRequest, st commandOpts := []git.CmdOpt{ git.WithGitProtocol(req), git.WithConfig(config...), - git.WithPackObjectsHookEnv(repo), + git.WithPackObjectsHookEnv(repo, "ssh"), } var stderrBuilder strings.Builder |