Commit 9e4f081f authored by Douwe Maan's avatar Douwe Maan

Merge branch '143-remote-user' into 'master'

Respect GL_USERNAME in Workhorse

Closes #143

See merge request gitlab-org/gitlab-workhorse!192
parents 76ead3b1 5b0f3acd
......@@ -31,8 +31,9 @@ func TestFailedCloneNoGitaly(t *testing.T) {
require.NoError(t, os.RemoveAll(scratchDir))
authBody := &api.Response{
GL_ID: "user-123",
RepoPath: repoPath(t),
GL_ID: "user-123",
GL_USERNAME: "username",
RepoPath: repoPath(t),
// This will create a failure to connect to Gitaly
GitalyServer: gitaly.Server{Address: "unix:/nonexistent"},
}
......@@ -132,6 +133,7 @@ func TestPostReceivePackProxiedToGitalySuccessfully(t *testing.T) {
apiResponse.Repository.StorageName,
apiResponse.Repository.RelativePath,
apiResponse.GL_ID,
apiResponse.GL_USERNAME,
string(testhelper.GitalyReceivePackResponseMock),
}, "\000")
......
......@@ -80,6 +80,10 @@ type Response struct {
// GL_ID is an environment variable used by gitlab-shell hooks during 'git
// push' and 'git pull'
GL_ID string
// GL_USERNAME holds gitlab username of the user who is taking the action causing hooks to be invoked
GL_USERNAME string
// GL_REPOSITORY is an environment variable used by gitlab-shell hooks during
// 'git push' and 'git pull'
GL_REPOSITORY string
......
......@@ -74,7 +74,7 @@ func newArchiveReader(ctx context.Context, repoPath string, format ArchiveFormat
a = &archiveReader{}
compressCmd, formatArg := parseArchiveFormat(format)
archiveCmd := gitCommand("", "", "git", "--git-dir="+repoPath, "archive", "--format="+formatArg, "--prefix="+archivePrefix+"/", commitId)
archiveCmd := gitCommand("git", "--git-dir="+repoPath, "archive", "--format="+formatArg, "--prefix="+archivePrefix+"/", commitId)
var archiveStdout io.ReadCloser
archiveStdout, err = archiveCmd.StdoutPipe()
......
......@@ -52,13 +52,13 @@ func handleSendBlobWithGitaly(w http.ResponseWriter, r *http.Request, params *bl
func handleSendBlobLocally(w http.ResponseWriter, r *http.Request, params *blobParams) {
log.Printf("SendBlob: sending %q for %q", params.BlobId, r.URL.Path)
sizeOutput, err := gitCommand("", "", "git", "--git-dir="+params.RepoPath, "cat-file", "-s", params.BlobId).Output()
sizeOutput, err := gitCommand("git", "--git-dir="+params.RepoPath, "cat-file", "-s", params.BlobId).Output()
if err != nil {
helper.Fail500(w, r, fmt.Errorf("SendBlob: get blob size: %v", err))
return
}
gitShowCmd := gitCommand("", "", "git", "--git-dir="+params.RepoPath, "cat-file", "blob", params.BlobId)
gitShowCmd := gitCommand("git", "--git-dir="+params.RepoPath, "cat-file", "blob", params.BlobId)
stdout, err := gitShowCmd.StdoutPipe()
if err != nil {
helper.Fail500(w, r, fmt.Errorf("SendBlob: git cat-file stdout: %v", err))
......
......@@ -2,6 +2,7 @@ package git
import (
"fmt"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/api"
"os"
"os/exec"
"syscall"
......@@ -10,7 +11,7 @@ import (
var execCommand = exec.Command
// Git subprocess helpers
func gitCommand(glId string, glRepository string, name string, args ...string) *exec.Cmd {
func gitCommandApi(a *api.Response, name string, args ...string) *exec.Cmd {
cmd := execCommand(name, args...)
// Start the command in its own process group (nice for signalling)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
......@@ -19,14 +20,20 @@ func gitCommand(glId string, glRepository string, name string, args ...string) *
fmt.Sprintf("HOME=%s", os.Getenv("HOME")),
fmt.Sprintf("PATH=%s", os.Getenv("PATH")),
fmt.Sprintf("LD_LIBRARY_PATH=%s", os.Getenv("LD_LIBRARY_PATH")),
fmt.Sprintf("GL_ID=%s", glId),
fmt.Sprintf("GL_PROTOCOL=http"),
}
if glRepository != "" {
cmd.Env = append(cmd.Env, fmt.Sprintf("GL_REPOSITORY=%s", glRepository))
if a != nil {
cmd.Env = append(cmd.Env, fmt.Sprintf("GL_ID=%s", a.GL_ID))
cmd.Env = append(cmd.Env, fmt.Sprintf("GL_USERNAME=%s", a.GL_USERNAME))
if a.GL_REPOSITORY != "" {
cmd.Env = append(cmd.Env, fmt.Sprintf("GL_REPOSITORY=%s", a.GL_REPOSITORY))
}
}
// If we don't do something with cmd.Stderr, Git errors will be lost
cmd.Stderr = os.Stderr
return cmd
}
func gitCommand(name string, args ...string) *exec.Cmd {
return gitCommandApi(nil, name, args...)
}
......@@ -28,7 +28,7 @@ func (d *diff) Inject(w http.ResponseWriter, r *http.Request, sendData string) {
log.Printf("SendDiff: sending diff between %q and %q for %q", params.ShaFrom, params.ShaTo, r.URL.Path)
gitDiffCmd := gitCommand("", "", "git", "--git-dir="+params.RepoPath, "diff", params.ShaFrom, params.ShaTo)
gitDiffCmd := gitCommand("git", "--git-dir="+params.RepoPath, "diff", params.ShaFrom, params.ShaTo)
stdout, err := gitDiffCmd.StdoutPipe()
if err != nil {
helper.Fail500(w, r, fmt.Errorf("SendDiff: create stdout pipe: %v", err))
......
......@@ -29,7 +29,7 @@ func (p *patch) Inject(w http.ResponseWriter, r *http.Request, sendData string)
log.Printf("SendPatch: sending patch between %q and %q for %q", params.ShaFrom, params.ShaTo, r.URL.Path)
gitRange := fmt.Sprintf("%s..%s", params.ShaFrom, params.ShaTo)
gitPatchCmd := gitCommand("", "", "git", "--git-dir="+params.RepoPath, "format-patch", gitRange, "--stdout")
gitPatchCmd := gitCommand("git", "--git-dir="+params.RepoPath, "format-patch", gitRange, "--stdout")
stdout, err := gitPatchCmd.StdoutPipe()
if err != nil {
......
......@@ -79,7 +79,7 @@ func startGitCommand(a *api.Response, stdin io.Reader, stdout io.Writer, action
args := []string{subCommand(action), "--stateless-rpc"}
args = append(args, options...)
args = append(args, a.RepoPath)
cmd = gitCommand(a.GL_ID, a.GL_REPOSITORY, "git", args...)
cmd = gitCommandApi(a, "git", args...)
cmd.Stdin = stdin
cmd.Stdout = stdout
......
......@@ -52,7 +52,7 @@ func handleReceivePackWithGitaly(ctx context.Context, a *api.Response, clientReq
return fmt.Errorf("smarthttp.ReceivePack: %v", err)
}
if err := smarthttp.ReceivePack(ctx, &a.Repository, a.GL_ID, a.GL_REPOSITORY, clientRequest, clientResponse); err != nil {
if err := smarthttp.ReceivePack(ctx, &a.Repository, a.GL_ID, a.GL_USERNAME, a.GL_REPOSITORY, clientRequest, clientResponse); err != nil {
return fmt.Errorf("smarthttp.ReceivePack: %v", err)
}
......
......@@ -39,7 +39,7 @@ func infoRefsReader(stream infoRefsClient) io.Reader {
})
}
func (client *SmartHTTPClient) ReceivePack(ctx context.Context, repo *pb.Repository, glId string, glRepository string, clientRequest io.Reader, clientResponse io.Writer) error {
func (client *SmartHTTPClient) ReceivePack(ctx context.Context, repo *pb.Repository, glId string, glUsername string, glRepository string, clientRequest io.Reader, clientResponse io.Writer) error {
stream, err := client.PostReceivePack(ctx)
if err != nil {
return err
......@@ -48,6 +48,7 @@ func (client *SmartHTTPClient) ReceivePack(ctx context.Context, repo *pb.Reposit
rpcRequest := &pb.PostReceivePackRequest{
Repository: repo,
GlId: glId,
GlUsername: glUsername,
GlRepository: glRepository,
}
......
......@@ -99,6 +99,7 @@ func (s *GitalyTestServer) PostReceivePack(stream pb.SmartHTTPService_PostReceiv
repo.GetStorageName(),
repo.GetRelativePath(),
req.GlId,
req.GlUsername,
}, "\000") + "\000")
// The body of the request starts in the second message
......
......@@ -722,8 +722,9 @@ func runOrFail(t *testing.T, cmd *exec.Cmd) {
func gitOkBody(t *testing.T) *api.Response {
repoPath := repoPath(t)
return &api.Response{
GL_ID: "user-123",
RepoPath: repoPath,
GL_ID: "user-123",
GL_USERNAME: "username",
RepoPath: repoPath,
Repository: pb.Repository{
StorageName: "default",
RelativePath: "foo/bar.git",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment