Skip to content

Allow configuring the SSH protocol - #5555

Merged
bk2204 merged 2 commits into
git-lfs:mainfrom
bk2204:remote-ssh-protocol
Nov 8, 2023
Merged

bk2204 merged 2 commits into
git-lfs:mainfrom
bk2204:remote-ssh-protocol

Conversation

@bk2204

@bk2204 bk2204 commented Oct 25, 2023

Copy link
Copy Markdown
Member

Some people have very slow SSH round trips and the extra time to attempt a failed git-lfs-transfer operation is bothersome. Let's add an option to allow users to use either the pure SSH protocol or the older hybrid protocol based on the URL in question to let them choose the right value if they know what it should be.

@bk2204
bk2204 force-pushed the remote-ssh-protocol branch 2 times, most recently from e701654 to 74183aa Compare October 27, 2023 15:02
@bk2204
bk2204 marked this pull request as ready for review October 27, 2023 16:57
@bk2204
bk2204 requested a review from a team as a code owner October 27, 2023 16:57

@chrisd8088 chrisd8088 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great enhancement for people who want to simplify their setups and avoid the SSH "probe" for full transfer protocol support on the server side.

My one main comment is whether we might profit by trying to align the configuration option's name and possible settings a bit more with some of our existing configuration terminology, and maybe splitting it into two Boolean options (which avoids a whole set of naming problems!) I kind of like that latter approach, I think, but I'm open to a range of alternatives. Mostly I just thought we should avoid an accidental misalignment with lfs.gitProtocol and could perhaps also find some simpler terms to describe the valid options of the new setting.

Comment thread docs/man/git-lfs-config.adoc Outdated
https://git-scm.com/docs/git-config#git-config-httplturlgt. To set this
value per-host:
`git config --global lfs.https://github.com/.locksverify [true|false]`.
* `lfs.<url>.sshprotocol`

@chrisd8088 chrisd8088 Oct 27, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One request I might make here is to employ the format we use elsewhere in the git-lfs-config(5) man page (admittedly, quite inconsistently, something I hope to address in a PR soon) and indicate that lfs.sshProtocol is also valid as a global fallback, e.g.:

* `lfs.sshProtocol` / `lfs.<url>.sshProtocol`

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread docs/man/git-lfs-config.adoc Outdated
https://git-scm.com/docs/git-config#git-config-httplturlgt. To set this
value per-host:
`git config --global lfs.https://github.com/.locksverify [true|false]`.
* `lfs.<url>.sshprotocol`

@chrisd8088 chrisd8088 Oct 29, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it happens, I've been slowly working on some revised and expanded documentation for many of our config settings, and in the process have written some for the currently undocumented lfs.gitProtocol setting, which takes values like http or https; the intent is that whatever is specified (with the default being https) is then substituted if the original URL begins with the git:// scheme. So it's a full protocol substitution.

This new setting has a different intent, which isn't to change from the SSH protocol to something else entirely, but rather to adjust "how much" SSH to use, as it were. So I wonder if we could use a different term here other than "protocol", although I appreciate the challenge in coming up with something clear and succinct.

One alternative which occurred to me is something like lfs.sshTransferProtocol or lfs.sshAdapterProtocol; another, which I maybe like better, might be lfs.sshTransfer, which aligns with the lfs.customTransfer.* settings.

Following from that last idea, I then wondered if it might be reasonable to allow as the possible values some simpler terms such as always, never, and negotiate (the default), with the goal of making the configuration settings more readable to the lay user.

I also toyed with the idea of having two Boolean settings instead of a single ternary one, maybe something like:

  • lfs.sshTransfer - if false, git-lfs-authenticate is always used; otherwise, git-lfs-transfer may be used (default: true)
  • lfs.sshTransfer.negotiate - only meaningful if lfs.sshTransfer is true, in which case it indicates whether or not to try git-lfs-transfer before falling back to git-lfs-authenticate (default: true)

That would avoid the need to come up with any terms at all for the settings, which I kind of appreciate, because naming things is hard.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On reflection, lfs.sshTransfer.negotiate is a bit ambiguous and maybe suggests some kind of negotiation of the underlying SSH protocol or the LFS transfer protocol over SSH, and neither is the case (particularly not the former).

What about something like lfs.sshTransfer.httpFallback or lfs.sshTransfer.basicTransferFallback, which might make it really clear what's being enabled/disabled by a Boolean value?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having a single tri-state option is better here. I'll take lfs.sshTransfer with always, never, and negotiate.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, thanks!

Comment thread t/t-locks.sh Outdated
grep "lfs-ssh-echo.*git-lfs-authenticate /$reponame download" trace.log

GIT_TRACE=1 git -c lfs."$sshurl".sshprotocol=git-lfs-authenticate lfs locks --path "f.dat" 2>trace.log | tee locks.log
cat trace.log

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cat and the ones in the subsequent test could probably be removed; I wondered if they were left over from the test construction and debugging process.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed them.

Comment thread lfshttp/client.go

uc := config.NewURLConfig(c.gitEnv)
if val, ok := uc.Get("lfs", e.OriginalUrl, "sshprotocol"); ok && val != "auto" && val != "git-lfs-authenticate" {
tracerx.Printf("skipping SSH-HTTPS hybrid protocol connection by request")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for adding this. While adding pure-SSH support to GitLab, I've had to take care to make sure git-lfs isn't falling back to git-lfs-authenticate in the event that git-lfs-transfer fails, resulting in me not noticing that my GitLab git-lfs-transfer code doesn't actually work. It would be nice to have a way to ensure that only git-lfs-transfer is ever attempted.

When we compute the contents of an Endpoint struct, we don't preserve
the URL from which we computed the value in the first place.  For
example, if we use an SSH URL, we don't preserve the SSH URL anywhere
for further use.  Let's do so, since we'll use it in a future commit.

Note that for anything but an SSH URL, this is the same value as the URL
field.
Some people have very slow SSH round trips and the extra time to attempt
a failed git-lfs-transfer operation is bothersome.  Let's add an option
to allow users to use either the pure SSH protocol or the older hybrid
protocol based on the URL in question to let them choose the right value
if they know what it should be.
@bk2204
bk2204 force-pushed the remote-ssh-protocol branch from 74183aa to 3ee2684 Compare November 6, 2023 16:28
@bk2204

bk2204 commented Nov 6, 2023

Copy link
Copy Markdown
Member Author

This looks green and is ready for another review when it's convenient.

@bk2204
bk2204 merged commit df9b79d into git-lfs:main Nov 8, 2023
@bk2204
bk2204 deleted the remote-ssh-protocol branch November 8, 2023 13:37
chrisd8088 added a commit that referenced this pull request Oct 22, 2026
In commit 72795a3 of PR #404
we expanded Git LFS client's support for SSH configurations,
including remotes configured using SSH URLs (i.e., URLs with
an "ssh:" scheme) and those configured using an scp(1)-style
format (i.e., "<user>@<host>:<path>").

One specific feature we introduced in that commit is that of a
fallback HTTP URL which we construct from the SSH configuration
of a Git remote.  The Git LFS client sends a "git-lfs-authenticate"
command to the SSH remote and expects a JSON response which may
contain either an HTTP URL or a set of HTTP credentials, or both.

If the JSON response to a "git-lfs-authenticate" remote command
includes an HTTP URL, then the client uses any provided credentials
with that URL.  Otherwise, the client makes requests to the fallback
URL that was constructed from the SSH configuration.

However, the Git LFS client constructs the fallback URL from a given
SSH remote configuration without properly encoding the URL's path
component.  As a result, if an SSH URL or scp(1)-style SSH remote
configuration contains characters which should be URL-encoded, these
are included in the fallback URL as raw characters rather than as
percent-encoded characters.

The primary consequence of this behaviour is that the client may
attempt to make a request to an invalid HTTP URL if the remote
"git-lfs-authenticate" command either does not succeed or does not
return an HTTP URL.

In addition, even if the remote "git-lfs-authenticate" command does
return a valid HTTP URL, the client will still attempt to use the
invalid fallback URL to fetch credentials for the API endpoint,
should they be required.  If the first HTTP response from the API
endpoint has a 401 status code, the client will then attempt to
invoke a local Git credential helper program to provide the
necessary credentials.  The helper program requires the host and
path of the remote system as input values, so the getCreds() method
of the Client structure in our "lfsapi" package calls the
getCredURLForAPI() function to provide the appropriate URL,
passing the fallback URL that was constructed for the API endpoint
as one of the function's parameters.

The getCredURLForAPI() function first checks whether the scheme and
host components of the given API endpoint's URL match those of the
actual request URL and, if not, returns the request URL rather than
the endpoint's URL.  This helps ensure that credentials for one
remote system are never sent to another system.  To determine the
scheme and host components of the given endpoint URL, the
getCredURLForAPI() function passes it to the Parse() function of
the "net/url" package from the Go standard library.  When the
URL contains invalid characters, the Parse() function returns an
error, which is then reported by the Git LFS client before it
exits without performing any further operations.

To resolve these problems, we revise the EndpointFromSshUrl()
function in our "lfshttp" package so that when it constructs a
fallback HTTP URL from an SSH URL, it uses a URL-encoded version
of the SSH URL's path component in the fallback URL.

In particular, the fallback URL's path is formed by re-encoding
the URL-decoded path from the SSH URL.  The decoded path is used
in the repository name argument of the remote commands sent via SSH
by the Git LFS client, following Git's own handling of SSH URLs.
This decoding converts any percent-encoded path separators (i.e.,
"%2F" or "%2f" in place of a "/" character) into full path
separators, as is true for Git as well:

  https://github.com/git/git/blob/v2.56.0/connect.c#L1467
  https://github.com/git/git/blob/v2.56.0/connect.c#L1107
  https://github.com/git/git/blob/v2.56.0/connect.c#L1489

When constructing the fallback HTTPS URL, we want to preserve the
lack of distinction in the SSH repository path argument between path
separators that were originally raw and those that were URL-encoded.
For this reason, we do not directly call the EscapedPath() method
of the URL structure whose Path field contains the decoded path of
the original SSH URL, because under some circumstances it may return
percent-encoded path separators from the original URL.  Instead, we
construct a new, anonymous URL structure with the decoded path as
its Path field and then call the EscapedPath() method of the new
structure.

The difference between these two approaches stems from the
inconsistent handling of percent-encoded path separators in the
methods of the URL structure type from the "net/url" package of
the Go standard library.  So long as the original URL's path is
considered to have a valid encoding, it is returned unaltered by
the EscapedPath() method.  This includes many cases in which path
separator characters are percent-encoded, so if we called the
EscapedPath() method directly, then in these instances the path
separators would be decoded in the SSH repository path argument
but would remain percent-encoded in the fallback URL.

However, when the original URL path is deemed to be improperly
encoded by the validEncoded() internal function in the "net/url"
package, the EscapedPath() method returns an encoded version of
the value in the URL structure's Path field rather than returning
the original path stored in the structure's RawPath field.  In
these cases, all the path separators from the Path field will be
returned in raw form, regardless of whether or not they were
percent-encoded in the original URL.  For reference, see:

  golang/go#73549
  golang/go#14815 (comment)
  https://github.com/golang/go/blob/go1.27.0/src/net/url/url.go#L678-L724

As we wish to always retain the raw path separators in the
URL-decoded path of the SSH repository name argument when we
construct a fallback HTTPS URL, we explicitly induce the latter
behaviour of the EscapedPath() method when constructing the
fallback URL.  To do this, we initialize an anonymous URL structure
with a defined Path field but an empty RawPath field, which forces
the EscapedPath() method to always encode the value in the Path
field.  We then use the result in the Url field of the Endpoint
structure created by the EndpointFromSshUrl() function.

Note, though, that the OriginalUrl field of the Endpoint structure
created by the EndpointFromSshUrl() function does not actually
contain the original SSH URL, and so the field's value may differ
from that URL due to the "net/url" package's inconsistency in
handling percent-encoded path separators.  The EndpointFromSshUrl()
function calls the String() method of the "net/url" package's URL
structure type, which in turn calls the EscapedPath() method to
generate the path of the URL the String() method returns.  Hence,
when the original URL's path contains both percent-encoded path
separators and certain unencoded raw characters, the path of the
URL returned by the String() method will differ from the path of
the original URL.

  https://github.com/golang/go/blob/go1.27.0/src/net/url/url.go#L839

The OriginalUrl field is only used to read the value of an
"lfs.<url>.sshTransfer" configuration option, should one exist.
(We introduced the field for that purpose in commit
2c04884 of PR #5555.)  We should
likely refactor our API endpoint URL parsing code so the OriginalUrl
field is populated with an unaltered copy of the original URL,
but that change falls outside of the scope of the present work
and so we defer it to a future PR.

To confirm that our revisions to the EndpointFromSshUrl() function
behave as we expect and that the URL constructed by the function is
always properly encoded, we expand the set of test cases checked by
the TestEndpointParsing() Go test function in our "lfsapi" package.
These test cases cover a range of possible SSH remote configurations,
including URLs with "ssh:", "git+ssh:", and "ssh+git:" schemes as
well as a configuration with an scp(1)-style format.

Note that the configuration in the scp(1)-style format, which
we term a "bare" SSH configuration, is first parsed by the
EndpointFromBareSshUrl() function in our "lfshttp" package.
This constructs an SSH URL and passes it to the EndpointFromSshUrl()
function, unless the "net/url" package's Parse() function returns an
error while parsing the constructed URL.  As a consequence, we do
not support the same range of characters in scp(1)-style format SSH
configurations which we now fully support in SSH URL configurations.

In addition to our expanded Go test cases, we also add a new shell
integration test to accompany the two tests we introduced recently
in PR #6286.  These tests all simulate the condition in which an
SSH remote is configured but the "git-lfs-authenticate" command is
not available.  Since the changes in PR #6286, we now expect that
in these circumstances the Git LFS client will proceed to make an
HTTP request to the fallback URL it constructed from the
configured SSH URL.

Like the two existing tests, our new "SSH git-lfs-authenticate
unavailable (fallback to HTTPS, URL-encoded newline)" test also
invokes the ssh_authenticate_unavailable_test() function, which
then establishes the requested test conditions and confirms that
the Git LFS client behaves as expected.  We update this function so
that it recognizes the "ssh-unavailable-newline" repository name
passed by our new test and inserts a percent-encoded line feed (LF)
character into the URL used to configure the SSH remote.

We also update our "lfs-ssh-echo" test utility to recognize the
repository name argument sent by the Git LFS client during our new
test and exit with status code 127.  This argument contains a line
feed character which appears in raw form because the repository
name argument of the "git-lfs-authenticate" remote command is not
URL-encoded.

Finally, we revise the ssh_authenticate_unavailable_test() function
so that in the case of our new test, it checks only the first portion
of the trace log message which records the contents of the remote SSH
command, because the log message is truncated by the raw line feed
character in the repository name argument.  We expect to revise the
format of these trace log messages in subsequent commits, at which
time we can expand the ssh_authenticate_unavailable_test() function
to also check the second portion of the log message with the
remainder of the repository name argument following the line feed
character.

In subsequent commit, we also expect to add another shell test
which will validate the behaviour of the Git LFS client when a
URL-encoded line feed character appears in an SSH URL.  In the
case of this test, the "lfs-ssh-echo" utility will not exit with
a non-zero status code, as it does during the new test we are
adding in this commit.  Instead, the utility will simulate a
valid JSON response to the "git-lfs-authenticate" remote command.
Under these conditions, the client should proceed to make HTTP
requests (using URLs with percent-encoded LF characters) and
ultimately successfully upload an object.  However, this future
test will only be possible if the client does not fail when it
attempts to fetch credentials for the HTTP requests, as it would
without the changes from this commit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants