Skip to content

lfsapi/tq: Have DoWithAuth() caller determine URL Access Mode - #3293

Merged
PastelMobileSuit merged 12 commits into
masterfrom
url-access
Oct 5, 2018
Merged

PastelMobileSuit merged 12 commits into
masterfrom
url-access

Conversation

@PastelMobileSuit

@PastelMobileSuit PastelMobileSuit commented Sep 28, 2018 •

Copy link
Copy Markdown
Contributor

This PR moves the responsibility to determine the URL Access Mode out of DoWithAuth() and into the hands of the caller. This removes our built-in assumption that all requests will be made against the LFS API endpoint, and allows other endpoints to be used for the access mode.

The transfer adapters generally don't make requests against the LFS API endpoint, generally making requests against a storage endpoint instead. This change means that requests coming from the transfer queue will now use that storage endpoint to determine the URL Access Mode instead of the LFS API endpoint.

This PR ended up being more complicated than expected, so there's a fair number of commits here. I've tried to commit atomic changes with descriptive commit messages, so I think it makes sense to review this one on a commit-by-commit basis.

/cc @git-lfs/core

Add a new Access type returned by the EndpointFinder. This type
contains a URL and the AccessMode associated with it. It also
contains an `Upgrade()` function, which returns a copy of the
Access object but with a new AccessMode.
Add a new Access parameter to DoWithAuth() to allow callers to
specify the URL Access Mode. This removes the assumption that
DoWithAuth() will always be used for an LFS API request. This
commit also adds DoAPIRequestWithAuth(), which provides the
functionality of DoWithAuth() while explicitly using the LFS API
endpoint for the access mode, allowing parity with the previous
DoWithAuth() functionality.
Add an allowRetry parameter to DoWithAuth(). This parameter is a
boolean, and allows any requests without an authentication header
to be retried if an authorization error was returned.

This change is required to get things working with the basic
upload adapter. Since it passes along a file reader in the
request body, and since that reader is read by the HTTP Client
in a copy operation to construct a new request, an upload request
body can't be re-used in a new request.
For tq requests, use the URL being transferred to for the access
mode instead of the LFS API endpoint. Since these endpoints are
different, this will ensure credentials aren't passed along
needlessly if, for example, the API endpoint requires
authentication and the endpoint being transferred to does not.
In the Basic Download and Basic Upload adapters, retry requests
that failed due to a lack of Authorization. This will allow these
requests to go through the normal auth flow without having to make
another batch request.
Comment thread lfsapi/auth.go
res, err := c.doWithAuth(remote, access, req, nil)

if allowRetry && errors.IsAuthError(err) {
if len(req.Header.Get("Authorization")) == 0 {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is a change to the retry conditional that previously lived in doWithAuth(), but it seemed safe to me. If a request fails with credentials then the Authorization header will be deleted before doWithAuth() returns, so this should cover that case, and so should only fire if the request had no authorization or if it used a credential helper to fill out the Authorization header.

@ttaylorr ttaylorr left a comment

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.

Nice work, @PastelMobileSuit! I think that things are looking good at a high-level. I left a few more minor comments, so perhaps you could take a look at those and then I'll do another pass looking more closely at the meat of the changes and their associated tests?

Comment thread lfsapi/endpoint_finder.go
}

// Returns a copy of an AccessMode with the mode upgraded to newMode
func (a *Access) Upgrade(newMode AccessMode) Access {

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.

Hmm. When we talked about this approach, I thought that there was more work to do in order to upgrade from one access mode to another.

I don't think it's bad at all thought that this function is short. In fact, I'd rather have a short function that introduces no mutable behavior than a short one that does (and I'd prefer that before a longer function that introduces mutable behavior).

Comment thread lfsapi/endpoint_finder.go Outdated
return Access{url: a.url, mode: newMode}
}

func (a *Access) GetMode() AccessMode {

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.

Nit: "accessors" in Go do not ordinarily contain the prefix Get(). I think that you did the right thing in making mode non-exported, since that enforces immutability. But, let's call this Mode() instead, to make it appear closer to accessing a normal, publicly exported field.

Comment thread lfsapi/endpoint_finder.go Outdated
defer e.accessMu.Unlock()

switch access {
switch access.mode {

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.

What do you think about calling the GetMode()/Mode() function here instead? I think that that's a good way to go since it will minimize the diff if we ever need to do extra computation to determine the access mode.

This same comment applies throughout the rest of this file, and its test.

Comment thread lfsapi/auth.go Outdated
operation := getReqOperation(req)
apiEndpoint := c.Endpoints.Endpoint(operation, remote)
access := c.Endpoints.AccessFor(apiEndpoint.Url)
return c.doWithAuth(remote, access, req, nil)

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.

I had a comment here that I wrote about halfway during the review that recommended that you call DoWithAuth() here to minimize any diff in the future should we complicate the body of DoWithAuth() at all later on.

But, you made that change in the subsequent commit, so I removed that comment here :-). Instead, I would recommend that in the future you make that change here (i.e., introduce this new function with a call to DoWithAuth() in this commit) rather than introduce something and then immediately change it in a future commit.

Comment thread lfsapi/auth.go Outdated
// supporting basic and ntlm authentication.
func (c *Client) DoWithAuth(remote string, access Access, req *http.Request) (*http.Response, error) {
return c.doWithAuth(remote, access, req, nil)
func (c *Client) DoWithAuth(remote string, access Access, req *http.Request, allowRetry bool) (*http.Response, error) {

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.

Nice, I think that your explanation as to why we need this new behavior variant makes sense to me. What do you think about introducing another function which encodes this behavior, rather than adding a boolean argument to this one?

This has a few of benefits in my mind: (1) is that it limits the number of parameters that we have to think about when calling a function, (2) it makes clear at the call-site what the parameter is doing (i.e., rather than seeing ..., true in a function call, we get a name that describes the behavior), and (3) it makes explicit what's going on in (2) which is really that we have two different versions of this function.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I like that idea! I actually considered it but went with the boolean because all the names I came up with at the time felt pretty unwieldy. I actually quite like the name DoWithAuthNoRetry() though, so I'll add that.

Comment thread tq/basic_upload.go
// request body into a new request, we can safely make this request
// outside of the flow of the transfer adapter, and if it fails, the
// transfer progress will be rewound at the top level
f, _ := os.OpenFile(t.Path, os.O_RDONLY, 0644)

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.

I think that we already have the file open by the time that we call this function. Can you call io.Seeker.Seek(0, io.SeekStart) on the _file_ instead of on the io.Reader` in the request?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately the issue in this case is that the underlying file has already been closed, so we can't issue a new request without re-opening it.

Rename Access.GetMode() to Access.Mode(), and use it in the
EndpointFinder implementation and tests instead of directly
accessing the `mode` member
Add a new auth function, `DoWithAuthNoRetry()`, which sends an
HTTP request with authorization but won't retry if authorization
failed.

This commit also removes the `allowRetry` parameter from
`DoWithAuth()` as a result, and that function will now
retry requests where authorization failed as before.
@PastelMobileSuit

Copy link
Copy Markdown
Contributor Author

@ttaylorr Thanks for the review! I've gone ahead and addressed your first round of feedback

@ttaylorr

ttaylorr commented Oct 4, 2018 via email

Copy link
Copy Markdown
Contributor

@bk2204

bk2204 commented Oct 4, 2018

Copy link
Copy Markdown
Member

Sure. Let me spend a few minutes looking.

@bk2204 bk2204 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.

Overall, this looks fine to me, so if @ttaylorr is okay with it, so am I. I'll let him make the final call.

I did note a couple places where we could improve the robustness of the tests, but they're not critical to fix: the test will just fail with less useful output.

Comment thread t/t-credentials.sh Outdated
grep "HTTP: 401" push.log
# Ensure we didn't make a second batch request, which means the request
# was successfully retried internally
[ ! $(grep "tq: retrying object" push.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.

If for some reason this test fails, the error here will be rather unhelpful, since we'll expand the command substitution into multiple words, even potentially inserting newlines. We could fix this if we wrote this with quotes around the command substitution, like so:

  [ ! "$(grep "tq: retrying object" push.log)" ]

The shell will do the right thing with the nested quote marks. There are also a few places below that do the same thing.

Quote the command substitutions in the last two credentials tests
so that they'll provide a more user-friendly error message if they
fail.
@PastelMobileSuit

Copy link
Copy Markdown
Contributor Author

@bk2204 Thanks for the review! I've gone ahead and updated those last two credentials tests

@ttaylorr ttaylorr left a comment

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.

This looks good to me, too. Thanks @PastelMobileSuit for your great work on this, and taking the time to answer mine and @bk2204's feedback.

As we discussed today, I think that this is an important item to ship with v2.6.0, in a couple of weeks.
But, before we do that, I think that we should do some due diligence and validate this out in the wild on popular hosted implementations of Git LFS. (I say that not because your tests aren't good (in fact, I think that they are quite good), but because it's important to verify that the behavior change in tq isn't a breaking one.)

Could you prepare a set of builds with this included (by running make release) and (1) open an issue that describes the changes here and how they will impact the transfer queue, and (2) tag @git-lfs/implementers on that issue to have them try it out? We can hold the baton for testing on GitHub.com, of course.

Once we do all of those things, I think that you should feel free to merge this. If you want to presumptively merge ahead of time, I think that that's fair, too, since all of the tests are passing in CI, so this appears ready to go. If we discover a show stopper along the way, it won't hurt to add additional patches on top of master by way of another pull request.

Thanks again, this is really great work. 👏

@PastelMobileSuit

Copy link
Copy Markdown
Contributor Author

I'm currently in the process of getting that issue put together and have built a set of preview binaries! As such I'm going to merge this as soon as CI passes, and start work on the fix for #3130

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