Repository navigation
Improved SSH support: non-bare urls, custom ports, and GIT_SSH/plink - #404
Conversation
|
Can you merge the latest master now that #403 has been merged? Rebase if you like. |
There was a problem hiding this comment.
I'm not a fan of this giant method. I'd prefer to have something that calls internal functions:
endpoint := Endpoint{Url: urlstr}
u, err := url.Parse(urlstr)
if err != nil {
// right now just {user}@{host}:{path}, but maybe others one day?
processSshRemote(urlstr)
}
switch u.Scheme {
case "http", "https": processHttpUrl(u)
case "ssh": processSshUrl(u)
default: endpoint.Url = ENDPOINT_URL_UNKNOWN
}Note: feel free to override my suggestions on method or variable names.
Minor nitpick: I tend to use rawurl. I don't like having type related suffixes on variable names. Maybe it's from my experience using hungarian notation in VB/Access code :)
There was a problem hiding this comment.
OK - rebased and will refactor a little in next commit.
You did VB/Access too eh? We should commiserate over a beer one day ;)
There was a problem hiding this comment.
I'd prefer early returns.
if u.Scheme != "" || u.Path == "" {
return u
}
// This might be a bare SSH URL
// Turn it into ssh:// for simplicity of extraction later
parts := strings.Split(u.Path, ":")
if len(parts) < 2 {
return u
}
// Treat presence of ':' as a bare URL
var newPath string
if len(parts) > 2 { // port included; really should only ever be 3 parts
newPath = fmt.Sprintf("%v:%v", parts[0], strings.Join(parts[1:], "/"))
} else {
newPath = strings.Join(parts, "/")
}
newrawurl := fmt.Sprintf("ssh://%v", newPath)
newu, err := url.Parse(newrawurl)
if err != nil {
return u
}
return newuThere was a problem hiding this comment.
Another possibility is a switch statement:
var newPath string
switch {
case len(parts) < 2:
return u
case len(parts) == 2:
newPath = strings.Join(parts, "/")
default:
// port included; really should only ever be 3 parts
newPath = fmt.Sprintf("%v:%v", parts[0], strings.Join(parts[1:], "/"))
}There was a problem hiding this comment.
I definitely prefer early returns vs multiple levels of nesting. 👍
|
Without this change, you can not use SSH-connections multiplexing on Windows. |
|
This PR doesn't look ready, so it likely won't make it in. We can release v0.5.3 whenever we want though. |
|
AFAIK the only thing that's "not ready" about it is stylistic preferences? |
|
Yes, and a merge conflict with master. |
|
OK, that must be new in the last day or two. Will come back to this next week. |
|
Are we doing ssh multiplexing in this PR? If so, has it been benchmarked with large numbers of large files? It is my understanding that ssh multiplexing starts to fall over when connections with large amounts of data are involved, though I am admittedly unfamiliar with plink. |
|
There's also another patch I'm holding back because of insufficient testing. We can roll v0.5.3 when these two are ready. I'm anxious to get v0.5.2 out because it's been way too long since the last release. @rubyist: We may be able to punt on that concern for now until we're reviewing the SSH API. |
All this PR does is support ssh:// url forms, custom ports, and the GIT_SSH env var. Its performance characteristics are identical to master since the ssh calls themselves happen exactly the same way, all it does is support cases that would previously fail. My other PR on full SSH support opens only Config.ConcurrentTransfers() SSH connections to run the API over and pipes everything through those. |
|
@sinbad 👍 that sounds OK then. My concerns were around using something like ssh's ControlMaster (or equivalents in plink, etc), using ConcurrentTransfers() connections with the transfer queue should be just fine. |
Yeah I did consider adding another config parameter that could override ConcurrentTransfers() for SSH connections, using a default lower limit but for simplicity I didn't, and the default 3 should be OK for all but the most totalitarian server admin. |
I make little benchmark on Windows/Linux with our project repository #376 (4231 LFS files, working copy size: 3.6G, original repository size: ~50G). Local gigabit network environment. On Linux: This changes enable using single SSH connection for all On Windows: A similar function is in Putty (plink.exe), but its use needs support GIT_SSH environment variable (this PR). The effect of multiplex under Windows, I did not measure. |
* Full SSH urls e.g. ssh://user@host/repo * Custom ports in both bare and full urls * GIT_SSH environment for alternate ssh clients * Explicit support for plink.exe and TortoisePlink.exe
|
Rebased on master & added early-outs, should be ready for inclusion in 0.5.3 now. |
There was a problem hiding this comment.
Should we really return an empty Endpoint in case of err != nil? Why not Endpoint{Url: rawurl}?
There was a problem hiding this comment.
Pretty much anything will go through the default: route, but for consistency with the other routes I've returned EndpointUrlUnknown in that case. You have to throw something pretty whacky at url.Parse for it to return an error (none of the tests with bare strings manage it).
…t EndpointUrlUnknown
Refactor some of the code in #404
Backport #404 for v0.5.x: Improved SSH support: non-bare urls, custom ports, and GIT_SSH/plink
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.
Depends on #403 (included in PR)