File: lfstransfer.go

package info (click to toggle)
gitlab-shell 14.35.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,652 kB
  • sloc: ruby: 1,129; makefile: 583; sql: 391; sh: 384
file content (129 lines) | stat: -rw-r--r-- 3,810 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package lfstransfer

import (
	"context"
	"encoding/base64"
	"fmt"

	"github.com/charmbracelet/git-lfs-transfer/transfer"
	"gitlab.com/gitlab-org/gitlab-shell/v14/internal/command"
	"gitlab.com/gitlab-org/gitlab-shell/v14/internal/command/commandargs"
	"gitlab.com/gitlab-org/gitlab-shell/v14/internal/command/readwriter"
	"gitlab.com/gitlab-org/gitlab-shell/v14/internal/command/shared/accessverifier"
	"gitlab.com/gitlab-org/gitlab-shell/v14/internal/command/shared/disallowedcommand"
	"gitlab.com/gitlab-org/gitlab-shell/v14/internal/config"
	"gitlab.com/gitlab-org/gitlab-shell/v14/internal/gitlabnet/lfsauthenticate"
	"gitlab.com/gitlab-org/labkit/log"
)

var (
	capabilities = []string{
		"version=1",
	}
)

type Command struct {
	Config     *config.Config
	Args       *commandargs.Shell
	ReadWriter *readwriter.ReadWriter
}

func (c *Command) Execute(ctx context.Context) (context.Context, error) {
	args := c.Args.SshArgs
	if len(args) != 3 {
		return ctx, disallowedcommand.Error
	}

	// e.g. git-lfs-transfer user/repo.git download
	repo := args[1]
	operation := args[2]

	action, err := actionFromOperation(operation)
	if err != nil {
		return ctx, err
	}

	accessResponse, err := c.verifyAccess(ctx, action, repo)
	if err != nil {
		return ctx, err
	}

	ctxWithLogData := context.WithValue(ctx, "logData", command.NewLogData(
		accessResponse.Gitaly.Repo.GlProjectPath,
		accessResponse.Username,
	))

	auth, err := c.authenticate(ctx, operation, repo, accessResponse.UserId)
	if err != nil {
		return ctxWithLogData, err
	}

	logger := NewWrappedLoggerForGitLFSTransfer(ctxWithLogData)

	backend, err := NewGitlabBackend(ctxWithLogData, c.Config, c.Args, auth)
	if err != nil {
		return ctxWithLogData, err
	}

	handler := transfer.NewPktline(c.ReadWriter.In, c.ReadWriter.Out, logger)

	for _, cap := range capabilities {
		if err := handler.WritePacketText(cap); err != nil {
			log.WithContextFields(ctxWithLogData, log.Fields{"capability": cap}).WithError(err).Error("error sending capability")
		}
	}

	if err := handler.WriteFlush(); err != nil {
		log.WithContextFields(ctxWithLogData, log.Fields{}).WithError(err).Error("error flushing capabilities")
	}

	p := transfer.NewProcessor(handler, backend, logger)
	defer log.WithContextFields(ctxWithLogData, log.Fields{}).Info("done processing commands")
	switch operation {
	case transfer.DownloadOperation:
		return ctxWithLogData, p.ProcessCommands(transfer.DownloadOperation)
	case transfer.UploadOperation:
		return ctxWithLogData, p.ProcessCommands(transfer.UploadOperation)
	default:
		return ctxWithLogData, fmt.Errorf("unknown operation %q", operation)
	}
}

func actionFromOperation(operation string) (commandargs.CommandType, error) {
	var action commandargs.CommandType

	switch operation {
	case transfer.DownloadOperation:
		action = commandargs.UploadPack
	case transfer.UploadOperation:
		action = commandargs.ReceivePack
	default:
		return "", disallowedcommand.Error
	}

	return action, nil
}

func (c *Command) verifyAccess(ctx context.Context, action commandargs.CommandType, repo string) (*accessverifier.Response, error) {
	cmd := accessverifier.Command{c.Config, c.Args, c.ReadWriter}

	return cmd.Verify(ctx, action, repo)
}

func (c *Command) authenticate(ctx context.Context, operation string, repo string, userId string) (*GitlabAuthentication, error) {
	client, err := lfsauthenticate.NewClient(c.Config, c.Args)
	if err != nil {
		return nil, err
	}

	response, err := client.Authenticate(ctx, operation, repo, userId)
	if err != nil {
		return nil, err
	}

	basicAuth := fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", response.Username, response.LfsToken))))
	return &GitlabAuthentication{
		href: fmt.Sprintf("%s/info/lfs", response.RepoPath),
		auth: basicAuth,
	}, nil
}