| 1 | package lfsapi |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "sync" |
| 6 | |
| 7 | "github.com/git-lfs/git-lfs/v3/config" |
| 8 | "github.com/git-lfs/git-lfs/v3/creds" |
| 9 | "github.com/git-lfs/git-lfs/v3/errors" |
| 10 | "github.com/git-lfs/git-lfs/v3/lfshttp" |
| 11 | "github.com/git-lfs/git-lfs/v3/ssh" |
| 12 | "github.com/rubyist/tracerx" |
| 13 | ) |
| 14 | |
| 15 | type Client struct { |
| 16 | Endpoints EndpointFinder |
| 17 | Credentials creds.CredentialHelper |
| 18 | |
| 19 | credContext *creds.CredentialHelperContext |
| 20 | |
| 21 | client *lfshttp.Client |
| 22 | context lfshttp.Context |
| 23 | access []creds.AccessMode |
| 24 | |
| 25 | sshTransfers map[string]*ssh.SSHTransfer |
| 26 | sshTransfersMutex *sync.Mutex |
| 27 | } |
| 28 | |
| 29 | func NewClient(ctx lfshttp.Context) *Client { |
| 30 | if ctx == nil { |
| 31 | ctx = lfshttp.NewContext(nil, nil, nil) |
| 32 | } |
| 33 | |
| 34 | gitEnv := ctx.GitEnv() |
| 35 | osEnv := ctx.OSEnv() |
| 36 | |
| 37 | return &Client{ |
| 38 | Endpoints: NewEndpointFinder(ctx), |
| 39 | client: lfshttp.NewClient(ctx), |
| 40 | context: ctx, |
| 41 | credContext: creds.NewCredentialHelperContext(gitEnv, osEnv), |
| 42 | access: creds.AllAccessModes(), |
| 43 | |
| 44 | sshTransfers: make(map[string]*ssh.SSHTransfer), |
| 45 | sshTransfersMutex: &sync.Mutex{}, |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func (c *Client) Context() lfshttp.Context { |
| 50 | return c.context |
| 51 | } |
| 52 | |
| 53 | // SSHTransfer returns either an suitable transfer object or nil. For a |
| 54 | // given operation and remote, the same transfer object (or nil) will |
| 55 | // always be returned. |
| 56 | func (c *Client) SSHTransfer(operation, remote string) *ssh.SSHTransfer { |
| 57 | if len(operation) == 0 { |
| 58 | return nil |
| 59 | } |
| 60 | |
| 61 | k := fmt.Sprintf("%s.%s", operation, remote) |
| 62 | |
| 63 | c.sshTransfersMutex.Lock() |
| 64 | defer c.sshTransfersMutex.Unlock() |
| 65 | |
| 66 | sshTransfer, ok := c.sshTransfers[k] |
| 67 | if !ok { |
| 68 | var err error |
| 69 | sshTransfer, err = c.initSSHTransfer(operation, remote) |
| 70 | if err == nil { |
| 71 | c.sshTransfers[k] = sshTransfer |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return sshTransfer |
| 76 | } |
| 77 | |
| 78 | // initSSHTransfer returns either an suitable transfer object or nil if the |
| 79 | // server is not using an SSH remote or the git-lfs-transfer style of SSH |
| 80 | // remote. |
| 81 | func (c *Client) initSSHTransfer(operation, remote string) (*ssh.SSHTransfer, error) { |
| 82 | endpoint := c.Endpoints.Endpoint(operation, remote) |
| 83 | if len(endpoint.SSHMetadata.UserAndHost) == 0 { |
| 84 | return nil, nil |
| 85 | } |
| 86 | |
| 87 | uc := config.NewURLConfig(c.context.GitEnv()) |
| 88 | if val, ok := uc.Get("lfs", endpoint.OriginalUrl, "sshtransfer"); ok && val != "negotiate" && val != "always" { |
| 89 | tracerx.Printf("skipping pure SSH protocol connection by request (%s, %s)", operation, remote) |
| 90 | return nil, nil |
| 91 | } |
| 92 | |
| 93 | ctx := c.Context() |
| 94 | tracerx.Printf("attempting pure SSH protocol connection (%s, %s)", operation, remote) |
| 95 | sshTransfer, err := ssh.NewSSHTransfer(ctx.OSEnv(), ctx.GitEnv(), &endpoint.SSHMetadata, operation) |
| 96 | if err != nil { |
| 97 | tracerx.Printf("pure SSH protocol connection failed (%s, %s): %s", operation, remote, err) |
| 98 | return nil, err |
| 99 | } |
| 100 | |
| 101 | return sshTransfer, nil |
| 102 | } |
| 103 | |
| 104 | func (c *Client) closeSSHTransfers() error { |
| 105 | c.sshTransfersMutex.Lock() |
| 106 | defer c.sshTransfersMutex.Unlock() |
| 107 | |
| 108 | var multiErr error |
| 109 | for _, sshTransfer := range c.sshTransfers { |
| 110 | if sshTransfer != nil { |
| 111 | multiErr = errors.Join(multiErr, sshTransfer.Shutdown()) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | clear(c.sshTransfers) |
| 116 | |
| 117 | return multiErr |
| 118 | } |