File: ssh.go

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (109 lines) | stat: -rw-r--r-- 3,262 bytes parent folder | download
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
/*
In this file we handle the Git over SSH GitLab-Shell requests
*/

// Package git handles git operations
package git

import (
	"fmt"
	"net/http"

	"gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb"

	"gitlab.com/gitlab-org/gitaly/v16/client"

	"gitlab.com/gitlab-org/gitlab/workhorse/internal/api"
	"gitlab.com/gitlab-org/gitlab/workhorse/internal/gitaly"
	"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/fail"
)

type flushWriter struct {
	http.ResponseWriter
	controller *http.ResponseController
}

func (f *flushWriter) Write(p []byte) (int, error) {
	n, err := f.ResponseWriter.Write(p)
	if err != nil {
		return n, err
	}

	return n, f.controller.Flush()
}

// SSHUploadPack handles git pull via SSH connection between GitLab-Shell and Gitaly through Workhorse
func SSHUploadPack(a *api.API) http.Handler {
	return repoPreAuthorizeHandler(a, handleSSHUploadPack)
}

// SSHReceivePack handles git push via SSH connection between GitLab-Shell and Gitaly through Workhorse
func SSHReceivePack(a *api.API) http.Handler {
	return repoPreAuthorizeHandler(a, handleSSHReceivePack)
}

func handleSSHUploadPack(w http.ResponseWriter, r *http.Request, a *api.Response) {
	controller := http.NewResponseController(w) //nolint:bodyclose // false-positive https://github.com/timakin/bodyclose/issues/52
	if err := controller.EnableFullDuplex(); err != nil {
		fail.Request(w, r, fmt.Errorf("enabling full duplex: %v", err))
		return
	}

	conn, err := gitaly.NewConnection(a.GitalyServer)
	if err != nil {
		fail.Request(w, r, fmt.Errorf("look up for gitaly connection: %v", err))
		return
	}

	registry, err := gitaly.Sidechannel()
	if err != nil {
		fail.Request(w, r, fmt.Errorf("sidechannel error: %v", err))
		return
	}

	w.WriteHeader(http.StatusOK)

	request := &gitalypb.SSHUploadPackWithSidechannelRequest{
		Repository:       &a.Repository,
		GitProtocol:      r.Header.Get("Git-Protocol"),
		GitConfigOptions: a.GitConfigOptions,
	}
	out := &flushWriter{ResponseWriter: w, controller: controller}
	_, err = client.UploadPackWithSidechannelWithResult(r.Context(), conn, registry, r.Body, out, out, request)
	if err != nil {
		fail.Request(w, r, fmt.Errorf("upload pack failed: %v", err))
		return
	}
}

func handleSSHReceivePack(w http.ResponseWriter, r *http.Request, a *api.Response) {
	controller := http.NewResponseController(w) //nolint:bodyclose // false-positive https://github.com/timakin/bodyclose/issues/52
	if err := controller.EnableFullDuplex(); err != nil {
		fail.Request(w, r, fmt.Errorf("enabling full duplex: %v", err))
		return
	}

	conn, err := gitaly.NewConnection(a.GitalyServer)
	if err != nil {
		fail.Request(w, r, fmt.Errorf("look up for gitaly connection: %v", err))
		return
	}

	w.WriteHeader(http.StatusOK)

	request := &gitalypb.SSHReceivePackRequest{
		Repository:       &a.Repository,
		GlId:             a.GL_ID,
		GlRepository:     a.GL_REPOSITORY,
		GlUsername:       a.GL_USERNAME,
		GitProtocol:      r.Header.Get("Git-Protocol"),
		GitConfigOptions: a.GitConfigOptions,
	}

	out := &flushWriter{ResponseWriter: w, controller: controller}
	_, err = client.ReceivePack(r.Context(), conn, r.Body, out, out, request)
	if err != nil {
		fail.Request(w, r, fmt.Errorf("receive pack failed: %v", err))
		return
	}
}