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
|
package lfsauthenticate
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"gitlab.com/gitlab-org/labkit/log"
"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"
)
const (
downloadOperation = "download"
uploadOperation = "upload"
)
type Command struct {
Config *config.Config
Args *commandargs.Shell
ReadWriter *readwriter.ReadWriter
}
type PayloadHeader struct {
Auth string `json:"Authorization"`
}
type Payload struct {
Header PayloadHeader `json:"header"`
Href string `json:"href"`
ExpiresIn int `json:"expires_in,omitempty"`
}
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-authenticate 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
}
logData := command.NewLogData(
accessResponse.Gitaly.Repo.GlProjectPath,
accessResponse.Username,
)
ctxWithLogData := context.WithValue(ctx, "logData", logData)
payload, err := c.authenticate(ctx, operation, repo, accessResponse.UserId)
if err != nil {
// return nothing just like Ruby's GitlabShell#lfs_authenticate does
log.WithContextFields(
ctx,
log.Fields{"operation": operation, "repo": repo, "user_id": accessResponse.UserId},
).WithError(err).Debug("lfsauthenticate: execute: LFS authentication failed")
return ctxWithLogData, nil
}
fmt.Fprintf(c.ReadWriter.Out, "%s\n", payload)
return ctxWithLogData, nil
}
func actionFromOperation(operation string) (commandargs.CommandType, error) {
var action commandargs.CommandType
switch operation {
case downloadOperation:
action = commandargs.UploadPack
case 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, userId string) ([]byte, 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 := base64.StdEncoding.EncodeToString([]byte(response.Username + ":" + response.LfsToken))
payload := &Payload{
Header: PayloadHeader{Auth: "Basic " + basicAuth},
Href: response.RepoPath + "/info/lfs",
ExpiresIn: response.ExpiresIn,
}
return json.Marshal(payload)
}
|