-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
663 lines (582 loc) · 17.4 KB
/
Copy pathgit.go
File metadata and controls
663 lines (582 loc) · 17.4 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
package web
import (
"bytes"
"compress/gzip"
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"charm.land/log/v2"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
gitb "github.com/urutau-ltd/git-cone/git"
"github.com/urutau-ltd/git-cone/pkg/access"
"github.com/urutau-ltd/git-cone/pkg/backend"
"github.com/urutau-ltd/git-cone/pkg/config"
"github.com/urutau-ltd/git-cone/pkg/git"
"github.com/urutau-ltd/git-cone/pkg/lfs"
"github.com/urutau-ltd/git-cone/pkg/proto"
"github.com/urutau-ltd/git-cone/pkg/utils"
)
// GitRoute is a route for git services.
type GitRoute struct {
method []string
handler http.HandlerFunc
path string
}
var _ http.Handler = GitRoute{}
// ServeHTTP implements http.Handler.
func (g GitRoute) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var hasMethod bool
for _, m := range g.method {
if m == r.Method {
hasMethod = true
break
}
}
if !hasMethod {
renderMethodNotAllowed(w, r)
return
}
g.handler(w, r)
}
var (
//nolint:revive
gitHttpReceiveCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "soft_serve",
Subsystem: "http",
Name: "git_receive_pack_total",
Help: "The total number of git push requests",
}, []string{"repo"})
//nolint:revive
gitHttpUploadCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "soft_serve",
Subsystem: "http",
Name: "git_upload_pack_total",
Help: "The total number of git fetch/pull requests",
}, []string{"repo", "file"})
)
func withParams(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
cfg := config.FromContext(ctx)
vars := mux.Vars(r)
repo := vars["repo"]
// Construct "file" param from path
vars["file"] = strings.TrimPrefix(r.URL.Path, "/"+repo+"/")
// Set service type
switch {
case strings.HasSuffix(r.URL.Path, git.UploadPackService.String()):
vars["service"] = git.UploadPackService.String()
case strings.HasSuffix(r.URL.Path, git.ReceivePackService.String()):
vars["service"] = git.ReceivePackService.String()
}
repo = utils.SanitizeRepo(repo)
vars["repo"] = repo
vars["dir"] = filepath.Join(cfg.DataPath, "repos", repo+".git")
// Add repo suffix (.git)
r.URL.Path = fmt.Sprintf("%s.git/%s", repo, vars["file"])
r = mux.SetURLVars(r, vars)
next.ServeHTTP(w, r)
})
}
// GitController is a router for git services.
func GitController(_ context.Context, r *mux.Router) {
basePrefix := "/{repo:.*}"
for _, route := range gitRoutes {
// NOTE: withParam must always be the outermost wrapper, otherwise the
// request vars will not be set.
r.Handle(basePrefix+route.path, withParams(withAccess(route)))
}
// Handle go-get
r.Handle(basePrefix, withParams(withAccess(http.HandlerFunc(GoGetHandler)))).Methods(http.MethodGet)
}
var gitRoutes = []GitRoute{
// Git services
// These routes don't handle authentication/authorization.
// This is handled through wrapping the handlers for each route.
// See below (withAccess).
{
method: []string{http.MethodPost},
handler: serviceRpc,
path: "/{service:(?:git-upload-archive|git-upload-pack|git-receive-pack)$}",
},
{
method: []string{http.MethodGet},
handler: getInfoRefs,
path: "/info/refs",
},
{
method: []string{http.MethodGet},
handler: getTextFile,
path: "/{_:(?:HEAD|objects/info/alternates|objects/info/http-alternates|objects/info/[^/]*)$}",
},
{
method: []string{http.MethodGet},
handler: getInfoPacks,
path: "/objects/info/packs",
},
{
method: []string{http.MethodGet},
handler: getLooseObject,
path: "/objects/{_:[0-9a-f]{2}/[0-9a-f]{38}$}",
},
{
method: []string{http.MethodGet},
handler: getPackFile,
path: "/objects/pack/{_:pack-[0-9a-f]{40}\\.pack$}",
},
{
method: []string{http.MethodGet},
handler: getIdxFile,
path: "/objects/pack/{_:pack-[0-9a-f]{40}\\.idx$}",
},
// Git LFS
{
method: []string{http.MethodPost},
handler: serviceLfsBatch,
path: "/info/lfs/objects/batch",
},
{
// Git LFS basic object handler
method: []string{http.MethodGet, http.MethodPut},
handler: serviceLfsBasic,
path: "/info/lfs/objects/basic/{oid:[0-9a-f]{64}$}",
},
{
method: []string{http.MethodPost},
handler: serviceLfsBasicVerify,
path: "/info/lfs/objects/basic/verify",
},
// Git LFS locks
{
method: []string{http.MethodPost, http.MethodGet},
handler: serviceLfsLocks,
path: "/info/lfs/locks",
},
{
method: []string{http.MethodPost},
handler: serviceLfsLocksVerify,
path: "/info/lfs/locks/verify",
},
{
method: []string{http.MethodPost},
handler: serviceLfsLocksDelete,
path: "/info/lfs/locks/{lock_id:[0-9]+}/unlock",
},
}
func askCredentials(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("WWW-Authenticate", `Basic realm="Git" charset="UTF-8", Token, Bearer`)
w.Header().Set("LFS-Authenticate", `Basic realm="Git LFS" charset="UTF-8", Token, Bearer`)
}
// withAccess handles auth.
func withAccess(next http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
cfg := config.FromContext(ctx)
logger := log.FromContext(ctx)
be := backend.FromContext(ctx)
// Store repository in context
// We're not checking for errors here because we want to allow
// repo creation on the fly.
repoName := mux.Vars(r)["repo"]
repo, _ := be.Repository(ctx, repoName)
ctx = proto.WithRepositoryContext(ctx, repo)
r = r.WithContext(ctx)
user, err := authenticate(r)
if err != nil {
switch {
case errors.Is(err, ErrInvalidToken):
case errors.Is(err, proto.ErrUserNotFound):
default:
logger.Error("failed to authenticate", "err", err)
}
}
if user == nil && !be.AllowKeyless(ctx) {
askCredentials(w, r)
renderUnauthorized(w, r)
return
}
// Store user in context
ctx = proto.WithUserContext(ctx, user)
r = r.WithContext(ctx)
if user != nil {
logger.Debug("authenticated", "username", user.Username())
}
service := git.Service(mux.Vars(r)["service"])
if service == "" {
// Get service from request params
service = getServiceType(r)
}
accessLevel := be.AccessLevelForUser(ctx, repoName, user)
ctx = access.WithContext(ctx, accessLevel)
r = r.WithContext(ctx)
file := mux.Vars(r)["file"]
// We only allow these services to proceed any other services should return 403
// - git-upload-pack
// - git-receive-pack
// - git-lfs
switch {
case service == git.ReceivePackService:
if accessLevel < access.ReadWriteAccess {
askCredentials(w, r)
renderUnauthorized(w, r)
return
}
// Create the repo if it doesn't exist.
if repo == nil {
repo, err = be.CreateRepository(ctx, repoName, user, proto.RepositoryOptions{})
if err != nil {
logger.Error("failed to create repository", "repo", repoName, "err", err)
renderInternalServerError(w, r)
return
}
ctx = proto.WithRepositoryContext(ctx, repo)
r = r.WithContext(ctx)
}
fallthrough
case service == git.UploadPackService || service == git.UploadArchiveService:
if repo == nil {
// If the repo doesn't exist, return 404
renderNotFound(w, r)
return
} else if errors.Is(err, ErrInvalidToken) || errors.Is(err, ErrInvalidPassword) {
// return 403 when bad credentials are provided
renderForbidden(w, r)
return
} else if accessLevel < access.ReadOnlyAccess {
askCredentials(w, r)
renderUnauthorized(w, r)
return
}
case strings.HasPrefix(file, "info/lfs"):
if !cfg.LFS.Enabled {
logger.Debug("LFS is not enabled, skipping")
renderNotFound(w, r)
return
}
switch {
case strings.HasPrefix(file, "info/lfs/locks"):
switch {
case strings.HasSuffix(file, "lfs/locks"), strings.HasSuffix(file, "/unlock") && r.Method == http.MethodPost:
// Create lock, list locks, and delete lock require write access
fallthrough
case strings.HasSuffix(file, "lfs/locks/verify"):
// Locks verify requires write access
// https://github.com/git-lfs/git-lfs/blob/main/docs/api/locking.md#unauthorized-response-2
if accessLevel < access.ReadWriteAccess {
renderJSON(w, http.StatusForbidden, lfs.ErrorResponse{
Message: "write access required",
})
return
}
}
case strings.HasPrefix(file, "info/lfs/objects/basic"):
switch r.Method {
case http.MethodPut:
// Basic upload
if accessLevel < access.ReadWriteAccess {
renderJSON(w, http.StatusForbidden, lfs.ErrorResponse{
Message: "write access required",
})
return
}
case http.MethodGet:
// Basic download
case http.MethodPost:
// Basic verify
}
}
if accessLevel < access.ReadOnlyAccess {
if repo == nil {
renderJSON(w, http.StatusNotFound, lfs.ErrorResponse{
Message: "repository not found",
})
} else if errors.Is(err, ErrInvalidToken) || errors.Is(err, ErrInvalidPassword) {
renderJSON(w, http.StatusForbidden, lfs.ErrorResponse{
Message: "bad credentials",
})
} else {
askCredentials(w, r)
renderJSON(w, http.StatusUnauthorized, lfs.ErrorResponse{
Message: "credentials needed",
})
}
return
}
}
switch {
case r.URL.Query().Get("go-get") == "1" && accessLevel >= access.ReadOnlyAccess:
// Allow go-get requests to passthrough.
break
case errors.Is(err, ErrInvalidToken), errors.Is(err, ErrInvalidPassword):
// return 403 when bad credentials are provided
renderForbidden(w, r)
return
case repo == nil, accessLevel < access.ReadOnlyAccess:
// Don't hint that the repo exists if the user doesn't have access
renderNotFound(w, r)
return
}
next.ServeHTTP(w, r)
}
}
//nolint:revive
func serviceRpc(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
cfg := config.FromContext(ctx)
logger := log.FromContext(ctx)
service, dir, repoName := git.Service(mux.Vars(r)["service"]), mux.Vars(r)["dir"], mux.Vars(r)["repo"]
if !isSmart(r, service) {
renderForbidden(w, r)
return
}
if service == git.ReceivePackService {
gitHttpReceiveCounter.WithLabelValues(repoName)
}
w.Header().Set("Content-Type", fmt.Sprintf("application/x-%s-result", service))
w.Header().Set("Connection", "Keep-Alive")
w.Header().Set("Transfer-Encoding", "chunked")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusOK)
version := r.Header.Get("Git-Protocol")
var stdout bytes.Buffer
cmd := git.ServiceCommand{
Stdout: &stdout,
Dir: dir,
}
switch service {
case git.UploadPackService, git.ReceivePackService:
cmd.Args = append(cmd.Args, "--stateless-rpc")
}
user := proto.UserFromContext(ctx)
cmd.Env = cfg.Environ()
cmd.Env = append(cmd.Env, []string{
"SOFT_SERVE_REPO_NAME=" + repoName,
"SOFT_SERVE_REPO_PATH=" + dir,
"SOFT_SERVE_LOG_PATH=" + filepath.Join(cfg.DataPath, "log", "hooks.log"),
}...)
if user != nil {
cmd.Env = append(cmd.Env, []string{
"SOFT_SERVE_USERNAME=" + user.Username(),
}...)
}
if len(version) != 0 {
cmd.Env = append(cmd.Env, []string{
fmt.Sprintf("GIT_PROTOCOL=%s", version),
}...)
}
var (
err error
reader io.ReadCloser
)
// Handle gzip encoding
reader = r.Body
switch r.Header.Get("Content-Encoding") {
case "gzip":
reader, err = gzip.NewReader(reader)
if err != nil {
logger.Errorf("failed to create gzip reader: %v", err)
renderInternalServerError(w, r)
return
}
defer reader.Close() //nolint: errcheck
}
cmd.Stdin = reader
cmd.Stdout = &flushResponseWriter{w}
if err := service.Handler(ctx, cmd); err != nil {
logger.Errorf("failed to handle service: %v", err)
return
}
if service == git.ReceivePackService {
if err := git.EnsureDefaultBranch(ctx, cmd.Dir); err != nil {
logger.Errorf("failed to ensure default branch: %s", err)
}
}
}
// Handle buffered output
// Useful when using proxies
type flushResponseWriter struct {
http.ResponseWriter
}
func (f *flushResponseWriter) ReadFrom(r io.Reader) (int64, error) {
flusher := http.NewResponseController(f.ResponseWriter)
var n int64
p := make([]byte, 1024)
for {
nRead, err := r.Read(p)
if err == io.EOF {
break
}
nWrite, err := f.ResponseWriter.Write(p[:nRead])
if err != nil {
return n, err
}
if nRead != nWrite {
return n, err
}
n += int64(nRead)
// ResponseWriter must support http.Flusher to handle buffered output.
if err := flusher.Flush(); err != nil {
return n, fmt.Errorf("%w: error while flush", err)
}
}
return n, nil
}
func getInfoRefs(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
cfg := config.FromContext(ctx)
dir, repoName, file := mux.Vars(r)["dir"], mux.Vars(r)["repo"], mux.Vars(r)["file"]
service := getServiceType(r)
protocol := r.Header.Get("Git-Protocol")
gitHttpUploadCounter.WithLabelValues(repoName, file).Inc()
if service != "" && (service == git.UploadPackService || service == git.ReceivePackService) {
// Smart HTTP
var refs bytes.Buffer
cmd := git.ServiceCommand{
Stdout: &refs,
Dir: dir,
Args: []string{"--stateless-rpc", "--advertise-refs"},
}
user := proto.UserFromContext(ctx)
cmd.Env = cfg.Environ()
cmd.Env = append(cmd.Env, []string{
"SOFT_SERVE_REPO_NAME=" + repoName,
"SOFT_SERVE_REPO_PATH=" + dir,
"SOFT_SERVE_LOG_PATH=" + filepath.Join(cfg.DataPath, "log", "hooks.log"),
}...)
if user != nil {
cmd.Env = append(cmd.Env, []string{
"SOFT_SERVE_USERNAME=" + user.Username(),
}...)
}
if len(protocol) != 0 {
cmd.Env = append(cmd.Env, fmt.Sprintf("GIT_PROTOCOL=%s", protocol))
}
var version int
for _, p := range strings.Split(protocol, ":") {
if strings.HasPrefix(p, "version=") {
if v, _ := strconv.Atoi(p[8:]); v > version {
version = v
}
}
}
if err := service.Handler(ctx, cmd); err != nil {
renderNotFound(w, r)
return
}
hdrNocache(w)
w.Header().Set("Content-Type", fmt.Sprintf("application/x-%s-advertisement", service))
w.WriteHeader(http.StatusOK)
if version < 2 {
git.WritePktline(w, "# service="+service.String()) //nolint: errcheck
}
w.Write(refs.Bytes()) //nolint: errcheck
} else {
// Dumb HTTP
updateServerInfo(ctx, dir) //nolint: errcheck
hdrNocache(w)
sendFile("text/plain; charset=utf-8", w, r)
}
}
func getInfoPacks(w http.ResponseWriter, r *http.Request) {
hdrCacheForever(w)
sendFile("text/plain; charset=utf-8", w, r)
}
func getLooseObject(w http.ResponseWriter, r *http.Request) {
hdrCacheForever(w)
sendFile("application/x-git-loose-object", w, r)
}
func getPackFile(w http.ResponseWriter, r *http.Request) {
hdrCacheForever(w)
sendFile("application/x-git-packed-objects", w, r)
}
func getIdxFile(w http.ResponseWriter, r *http.Request) {
hdrCacheForever(w)
sendFile("application/x-git-packed-objects-toc", w, r)
}
func getTextFile(w http.ResponseWriter, r *http.Request) {
hdrNocache(w)
sendFile("text/plain", w, r)
}
func sendFile(contentType string, w http.ResponseWriter, r *http.Request) {
dir, file := mux.Vars(r)["dir"], mux.Vars(r)["file"]
reqFile := filepath.Join(dir, file)
// Open the file first, then serve using the open handle to eliminate the
// TOCTOU window between os.Stat and the subsequent read that http.ServeFile
// would introduce by calling Lstat internally a second time.
f, err := os.Open(reqFile)
if err != nil {
if os.IsNotExist(err) {
renderNotFound(w, r)
return
}
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
defer f.Close() //nolint:errcheck
fi, err := f.Stat()
if err != nil {
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", contentType)
http.ServeContent(w, r, fi.Name(), fi.ModTime(), f)
}
func getServiceType(r *http.Request) git.Service {
service := r.FormValue("service")
if !strings.HasPrefix(service, "git-") {
return ""
}
return git.Service(service)
}
func isSmart(r *http.Request, service git.Service) bool {
contentType := r.Header.Get("Content-Type")
return strings.HasPrefix(contentType, fmt.Sprintf("application/x-%s-request", service))
}
func updateServerInfo(ctx context.Context, dir string) error {
return gitb.UpdateServerInfo(ctx, dir)
}
// HTTP error response handling functions
func renderBadRequest(w http.ResponseWriter, r *http.Request) {
renderStatus(http.StatusBadRequest)(w, r)
}
func renderMethodNotAllowed(w http.ResponseWriter, r *http.Request) {
if r.Proto == "HTTP/1.1" {
renderStatus(http.StatusMethodNotAllowed)(w, r)
} else {
renderBadRequest(w, r)
}
}
func renderNotFound(w http.ResponseWriter, r *http.Request) {
renderStatus(http.StatusNotFound)(w, r)
}
func renderUnauthorized(w http.ResponseWriter, r *http.Request) {
renderStatus(http.StatusUnauthorized)(w, r)
}
func renderForbidden(w http.ResponseWriter, r *http.Request) {
renderStatus(http.StatusForbidden)(w, r)
}
func renderInternalServerError(w http.ResponseWriter, r *http.Request) {
renderStatus(http.StatusInternalServerError)(w, r)
}
// Header writing functions
func hdrNocache(w http.ResponseWriter) {
w.Header().Set("Expires", "Fri, 01 Jan 1980 00:00:00 GMT")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Cache-Control", "no-cache, max-age=0, must-revalidate")
}
func hdrCacheForever(w http.ResponseWriter) {
now := time.Now().Unix()
expires := now + 31536000
w.Header().Set("Date", fmt.Sprintf("%d", now))
w.Header().Set("Expires", fmt.Sprintf("%d", expires))
w.Header().Set("Cache-Control", "public, max-age=31536000")
}