Repository navigation
Create new lfshttp package - #3244
Conversation
Extract more basic http-related functionality out of lfsapi and into a new package, lfshttp. Everything is currently functional aside from authorization.
Refactor the client redirect code to allow lfsapi to re-authenticate redirected requests
|
This is the first PR to handle #3194. The actual changes to the access mode management will land in a separate PR after this is merged. |
bk2204
left a comment
There was a problem hiding this comment.
Overall, this looks good. I made one suggestion, but I'd be happy with it where it stands.
| return errors.New("Not implemented") | ||
| } | ||
|
|
||
| // func TestClientRedirectReauthenticate(t *testing.T) { |
There was a problem hiding this comment.
It looks like you added this code commented out in the first commit which was uncommented in this commit. I wonder if it might be tidier if we deferred adding it until the second commit.
|
Some further information on reviewing: d101bdb is exclusively functionality extraction, moving To review this commit, I'd take a look at e6c4c63 moves some of the |
ttaylorr
left a comment
There was a problem hiding this comment.
@PastelMobileSuit Huge ❇️'s from me on this -- I'm unreasonably happy with how everything shook out between lfsapi and lfshttp.
It seems like we have a great path forward to write code in package tq that can't assume it's talking to a Git LFS server, and instead can use lfshttp directly (or choose between the two).
Thanks for your comment in #3244 (comment), it certainly helped me during the review process. In the future, do you think it would be possible to include some of that information in your commits, too? I like that we've started to include this type of historical record in the commits themselves, since it makes it easier when trying to recall why a change was made in the future to run git blame and read the last commit that touched a line (instead of opening the commit in the browser, finding the affected PR, and reading through its comments).
I think that in general the division between the commits is good. I think in general I find it difficult to review two commits that have nearly 2,000 SLOC worth of diff, but I think for changes like these there's no great atomic way to separate things out.
Anyhow, my TL;DR is: this change is great, so is the writeup you posted, and in the future I would love that same information in the commit.
👏
At present, when the GIT_CURL_VERBOSE environment variable is set to a value such as "1" or "true", Git LFS client outputs some of the details of each HTTP request and response it makes to its standard error stream. We gather the HTTP request headers to be logged in the traceRequest() method of the Client structure from our "lfshttp" package. To gather the headers, the traceRequest() method uses the DumpRequest() function from the from the "net/http/httputil" package in the Go standard library. Our use of the DumpRequest() function for this purpose dates back to commit 6fafade of PR git-lfs#366 in 2015, when it was first introduced into what was then the traceHttpRequest() function in our "lfs" package. This function gradually evolved into the current traceRequest() method through a series of PRs. First, portions of the "lfs" package were migrated into a new "httputil" package in PR git-lfs#1226. That package was later removed in PR git-lfs#1846 after a new "lfsapi" package was added in PR git-lfs#1794 with a version of the traceRequest() method very similar to the current one. Finally, in PR git-lfs#3244 we moved the method into the current "lfshttp" package. Throughout these changes, however, our use of the DumpRequest() function has remained the same. However, since the Git LFS client only acts as an HTTP client and not an HTTP server, we should instead be using the DumpRequestOut() function from the standard "httputil" package to gather the headers of an HTTP request for logging purposes. Replacing our use of the DumpRequest() function with the DumpRequestOut() function would allow us to output a number of request headers that we currently fail to log, as was noted with respect to the Accept-Encoding header in issue git-lfs#6193. Since Go version 1.6, the "httputil" package's documentation has stated that the DumpRequest() function "should only be used by servers to debug client requests", and that the DumpRequestOut() function "is like DumpRequest but for outgoing client requests": https://pkg.go.dev/net/http/[email protected]#DumpRequest https://pkg.go.dev/net/http/[email protected]#DumpRequestOut However, at the time we first adopted the use of the DumpRequest() function, the Go documentation did not include these directives, and merely noted that "DumpRequest returns the as-received wire representation of req, optionally including the request body, for debugging" and that "DumpRequestOut is like DumpRequest but includes headers that the standard http.Transport adds, such as User-Agent": https://pkg.go.dev/net/http/[email protected]#DumpRequest https://pkg.go.dev/net/http/[email protected]#DumpRequestOut It is reasonable to assume, therefore, that when PR git-lfs#366 was written its authors simply assumed that the DumpRequest() function was sufficient, given the state of the Go documentation at that time. As we would prefer to log a more complete set of client HTTP request headers, though, particular the Accept-Encoding header which the Go "net/http" package automatically adds to outgoing requests, we now update our traceRequest() method to use the DumpRequestOut() function. We then also update our Go language TestVerbose*() test functions to confirm that the Accept-Encoding request header is now captured and reported when verbose logging is enabled. Note that without the change in this PR to the use of the DumpRequestOut() function, these revised tests would fail, since the Accept-Encoding header would not appear in headers returned by the DumpRequest() function.
At present, when the GIT_CURL_VERBOSE environment variable is set to a value such as "1" or "true", the Git LFS client outputs some of the details of each HTTP request and response it makes to its standard error stream. We gather the HTTP request headers to be logged in the traceRequest() method of the Client structure from our "lfshttp" package. To gather the headers, the traceRequest() method uses the DumpRequest() function from the from the "net/http/httputil" package in the Go standard library. Our use of the DumpRequest() function for this purpose dates back to commit 6fafade of PR git-lfs#366 in 2015, when it was first introduced into what was then the traceHttpRequest() function in our "lfs" package. This function gradually evolved into the current traceRequest() method through a series of PRs. First, portions of the "lfs" package were migrated into a new "httputil" package in PR git-lfs#1226. That package was later removed in PR git-lfs#1846 after a new "lfsapi" package was added in PR git-lfs#1794 with a version of the traceRequest() method very similar to the current one. Finally, in PR git-lfs#3244 we moved the method into the current "lfshttp" package. Throughout these changes, however, our use of the DumpRequest() function has remained the same. However, since the Git LFS client only acts as an HTTP client and not an HTTP server, we should instead be using the DumpRequestOut() function from the standard "httputil" package to gather the headers of an HTTP request for logging purposes. Replacing our use of the DumpRequest() function with the DumpRequestOut() function would allow us to output a number of request headers that we currently fail to log, as was noted with respect to the Accept-Encoding header in issue git-lfs#6193. Since Go version 1.6, the "httputil" package's documentation has stated that the DumpRequest() function "should only be used by servers to debug client requests", and that the DumpRequestOut() function "is like DumpRequest but for outgoing client requests": https://pkg.go.dev/net/http/[email protected]#DumpRequest https://pkg.go.dev/net/http/[email protected]#DumpRequestOut However, at the time we first adopted the use of the DumpRequest() function, the Go documentation did not include these directives, and merely noted that "DumpRequest returns the as-received wire representation of req, optionally including the request body, for debugging" and that "DumpRequestOut is like DumpRequest but includes headers that the standard http.Transport adds, such as User-Agent": https://pkg.go.dev/net/http/[email protected]#DumpRequest https://pkg.go.dev/net/http/[email protected]#DumpRequestOut It is reasonable to assume, therefore, that when PR git-lfs#366 was written its authors simply assumed that the DumpRequest() function was sufficient, given the state of the Go documentation at that time. As we would prefer to log a more complete set of client HTTP request headers, though, particularly the Accept-Encoding header which the Go "net/http" package automatically adds to outgoing requests, we now update our traceRequest() method to use the DumpRequestOut() function. We then also update some of our Go language TestVerbose*() test functions to confirm that the Accept-Encoding request header is now captured and reported when verbose logging is enabled. Note that without the change in this PR to the use of the DumpRequestOut() function, these revised tests would fail, since the Accept-Encoding header would not appear in headers returned by the DumpRequest() function.
In commit d101bdb of PR git-lfs#3244 we extracted the portions of our "lfsapi" package specifically related to HTTP request handling into a new package named "lfshttp". As part of that process, we moved some of the initialization steps formerly performed by the NewClient() function of our "lfsapi" package into a separate NewClient() function in the "lfshttp" package. Both NewClient() functions return an "error" type as their second return value, and at the time of PR git-lfs#3244, the NewClient() function in the "lfsapi" package could encounter an error condition and return a non-nil "error" value. However, none of the initialization steps that we moved to the NewClient() function of the "lfshttp" package could encounter an error condition, so there was no specific need for the function to continue to return an "error" value. As this remains the case, we can simplify the NewClient() function in our "lfshttp" package by just eliminating its "error" return value. We then update all the callers of this function, including those in our Go test functions, as they no longer need to check for an "error" return value from the function.
In PR #1839 we first developed our "lfsapi" package, and in commit fecdc9e of that PR we introduced the getAPIClient() function to our "commands" package to ensure that we only initialize a single, global Client structure from the "lfsapi" package. The getAPIClient() function acquires a global mutex, appropriately named "global", and then checks if the "commands" package's "apiClient" global variable has been initialized or not. If not, the function calls the NewClient() function of the "lfsapi" package and assigns the newly instantiated Client structure to the "apiClient" variable. Later, in commit 2435a67 of PR #2184, we added an HTTPLogger field to the "lfsapi" package's Client structure. This field's type was that of a WriteCloser structure from the Go standard library's "io" package, and since this type has a Close() method, we also defined a Close() method for the Client structure that called the HTTPLogger field's Close() method, unless the field was nil. In the same commit in PR #2184 we then updated the Run() function in our "commands" package to call the new Close() method of the Client structure, after first calling the getAPIClient() function to ensure that the "apiClient" variable was not nil. Subsequently, in commit d16a320 of PR #2633, we introduced a closeAPIClient() function to the "lfsapi" package, which acquires the "global" mutex and then calls the "apiClient" variable's Close() method, but only if the variable is not nil. By revising the Run() function to then just call the closeAPIClient() function before returning, this change avoided the need to instantiate a Client structure only in order to close it. Note that later, in PR #3244, we refactored the "lfsapi" package and moved portions of its functionality into a new "lfshttp" package, including the HTTP logging subsystem. The Close() method of the "lfsapi" package's Client structure now just calls the Close() method of the "lfshttp" package's Client structure, and that structure is the one which contains an httpLogger field. Regardless, it remains the responsibility of the "commands" package to call the Close() method on its "apiClient" variable when necessary, in order to release any resources associated with it or its internal components. However, because the "apiClient" variable's Close() method is only called when the Run() function is about to return, the resources associated with our HTTP logging subsystem are only properly released when the Git LFS client exits normally. If the client encounters an unrecoverable error condition, it invokes one of the exit wrapper functions in our "commands" package, which call the Exit() function of the Go standard library's "os" package and therefore do not return. As a result, the Run() function is not completely executed, and the closeAPIClient() function will never be called. Likewise, if the client receives a SIGINT or SIGKILL signal, the signal handler established by our main() function in the "main" package will halt the program by calling the "os" package's Exit() function, and so the closeAPIClient() function will also not be called in this case. Fortunately, in previous commits in this PR as well as in PR #6255 we have made a series of revisions which ensure that when exiting, the Git LFS client will always call the Cleanup() function in our "commands" package, regardless of the circumstances under which the client needs to halt. We therefore now revise our "commands" package so that the closeAPIClient() function is called from the Cleanup() function rather than the Run() function. This change will guarantee that the resources associated with our HTTP logging subsystem are always properly released when the Git LFS client exits. We also take the opportunity to add a check of the "error" return value from the closeAPIClient() function, so that if it is not nil, the Cleanup() function will report the corresponding error message to the standard error file descriptor.
We define a "closingByteReader" structure with a no-op Close() method in several of our packages, including the "tools", "lfsapi", and "lfshttp" packages. In each case, the purpose of the structure is to allow us to use the structure returned by the NewReader() function of the Go standard library's "bytes" package in contexts that require a structure that implements the ReadSeekCloser interface from the standard library's "io" package. We introduced the first of these structures to our "lfsapi" package in commit 6659b9d of PR #1839. In a subsequent commit in that PR we then added a NewByteBody() factory function which initializes a "closingByteReader" structure by passing the byte slice provided as a parameter to the "bytes" package's NewReader() function. Next, in the same PR we replicated the structure, its Close() method, and the NewByteBody() function into what was at that time called our "progress" package. In PR #2749 we later moved the "progress" package's contents to our "tools" package, and then in PR #3244 we replicated the structure, its Close() method, and the NewByteBody() function a third time when we refactored portions of the "lfsapi" package into our "lfshttp" package. In our "tools" package we also provide a NewByteBodyWithCallback() factory function which calls the NewByteBody() function and uses the "closingByteReader" structure it returns as the "body" parameter in a call to the NewBodyWithCallback() function. The result is that a new BodyWithCallback structure is initialized with the byte slice passed to the NewByteBodyWithCallback() function as its underlying source of data. This NewByteBodyWithCallback() function is used exclusively in various test functions, both in our "tools" package and our "lfs" package. Ideally, though, we should not define functions or methods in our non-test source files that are intended for use only in test functions. In prior commits in this PR we updated all of the test functions which use the NewByteBodyWithCallback() function, and revised a number of them so that they now perform their checks against the methods of both a BodyWithCallback structure and the methods of a CallbackReader structure. To simplify the initialization of the CallbackReader structures, in another prior commit in this PR we introduced a NewCallbackReader() factory function, which operates in a similar manner to the NewBodyWithCallback() function except that it returns a CallbackReader structure instead of a BodyWithCallback structure. We did not, though, introduce a NewByteCallbackReader() function, as it is relatively straightforward for our tests to simply pass the return value from a call to the "bytes" package's NewReader() function to our NewCallbackReader() function. Given the marginal utility of the NewByteBodyWithCallback() function and the fact that it is only used in our test functions, we remove it now. This also helps align the structure initialization steps performed by our test functions, since they now just call the NewBodyWithCallback() and NewCallbackReader() functions, which initialize their respective structures in an equivalent manner. To bring these initialization steps into even closer alignment, we also rename the "closingByteReader" structure in the "tools" package to ClosingByteReader, and rename its factory function from NewByteBody() to NewClosingByteReader(). These changes will allow us to make use of the structure in our "lfsapi" and "lfshttp" packages, replacing the identical definitions of the "closingByteReader" structure and the related functions and methods in those packages. We defer this refactoring step to a future PR, however, as it is outside of this PR's scope. Finally, we move the definition of the ClosingByteReader structure, its Close() method, and the NewClosingByteReader() function to our "tools/iotools.go" source file, and add a new test function named TestClosingByteReaderNopClose() to the "tools/iotools_test.go" source file. This test function simply verifies that the Close() method of the ClosingByteReader structure performs no action, as we expect that it should not do so.
This PR extracts much of the lower level, http request-handling functionality out of the
lfsapipackage and into a newlfshttppackage.The first commit (d101bdb) largely just extracts files into the new package with minimal changes, and updates
lfsapiand other packages to rely on it. The second commit (e6c4c63) refactors the way redirects are handled so thatlfsapican re-authenticate on a redirect when the original request was authenticated.