Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 36 additions & 20 deletions lfsapi/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ var (
// authentication from netrc or git's credential helpers if necessary,
// supporting basic and ntlm authentication.
func (c *Client) DoWithAuth(remote string, req *http.Request) (*http.Response, error) {
return c.doWithAuth(remote, req, nil)
return c.doWithAuth(remote, req, nil, false)
}

func (c *Client) doWithAuth(remote string, req *http.Request, via []*http.Request) (*http.Response, error) {
func (c *Client) doWithAuth(remote string, req *http.Request, via []*http.Request, ignoreNetRc 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.

To make sure that I'm reading this change correctly: do you intend to mark when doWithAuth should and should not use the credentials found in ~/.netrc upon each request?

If this is the case, it sounds like we start with credentials found in ~/.netrc, and (if those fail), move on to the credential helper, and then the URL credentials, and so on.

That sounds like a reasonable plan to me. I have a few stylistic remarks regarding the new parameter to this function:

  1. Do you think we could name the boolean argument positively? For example, instead of ignoreNetrc (nit also, casing should be Netrc not NetRc), could we name it as useNetrc? I think that this makes the conditional on line L123 and L155 easier to grok. Instead of if !ignoreNetrc, it would read if useNetrc, which is IMHO easier to read.

  2. Should this argument be added at all? I usually find that adding a boolean argument to a function is a smell indicating a split in behavior change that should instead be applied via a function extraction. Do you think that this is possible with the current code structure? I'm generally wary of changing code in this file, but I think that removing the 6th return value from getCreds might be a worthwhile tradeoff for the risk.

More broadly, though, I'm wondering if this is the right way to go about this change. The credential helper that this function (c.getCreds) returns has the following interface:

type CredentialHelper interface {
        Fill(Creds) (Creds, error)
        Reject(Creds) error
        Approve(Creds) error
}

and has an implementation that tries multiple CredentialHelpers in succession until either (1) one succeeds or (2) all of them fail. That goes by the name of *CredentialHelpers. I'm wondering if we could replace:

https://github.com/git-lfs/git-lfs/blob/v2.5.1/lfsapi/auth.go#L174

with an implementation of the CredentialHelper interface that fills from a user's ~/.netrc. In this case, Accept() would be a no-op, but Reject() could indicate that further calls to Fill() for a given hostname should fail immediately. A bad ~/.netrc file could return an error anytime that it's asked to fill a request.

This way, we could return one CredentialHelper (backed by a *CredentialHelpers) that would transparently attempt the ~/.netrc first, and then cascade down through other credential helpers without having to re-call this method and juggle the extra argument.

@PastelMobileSuit PastelMobileSuit Aug 20, 2018 •

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'm with you on the credential helper approach. I think there are two really clear benefits: the code will be cleaner, and URL credentials will be prioritized above .netrc credentials, which will make us more consistent with git.

My only concern is that this change will modify auth priorities and the order in which we perform auth operations in this file. I discovered first-hand while making this PR that even a simple change in ordering can cause some weird, unexpected behaviours (for example, switching the order of the conditionals at lines 123 and 127 causes some requests to fall through to the credential helper when they should be using .netrc instead). I think we'd want to make extra sure the change was safe before it goes into a release.

req.Header = c.extraHeadersFor(req)

apiEndpoint, access, credHelper, credsURL, creds, err := c.getCreds(remote, req)
apiEndpoint, access, credHelper, credsURL, creds, fromNetRc, err := c.getCreds(remote, req, ignoreNetRc)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -58,6 +58,12 @@ func (c *Client) doWithAuth(remote string, req *http.Request, via []*http.Reques
return c.DoWithAuth(remote, req)
}
}

if fromNetRc && (res.StatusCode == 401 || res.StatusCode == 403) {
tracerx.Printf("api: netrc credentials were rejected. Trying again with new credentials...")
req.Header.Del("Authorization")
return c.doWithAuth(remote, req, nil, true)
}
}

if res != nil && res.StatusCode < 300 && res.StatusCode > 199 {
Expand Down Expand Up @@ -94,7 +100,7 @@ func (c *Client) doWithCreds(req *http.Request, credHelper CredentialHelper, cre
// 3. The Git Remote URL, which should be something like "https://git.com/repo.git"
// This URL is used for the Git Credential Helper. This way existing https
// Git remote credentials can be re-used for LFS.
func (c *Client) getCreds(remote string, req *http.Request) (Endpoint, Access, CredentialHelper, *url.URL, Creds, error) {
func (c *Client) getCreds(remote string, req *http.Request, ignoreNetRc bool) (Endpoint, Access, CredentialHelper, *url.URL, Creds, bool, error) {
ef := c.Endpoints
if ef == nil {
ef = defaultEndpointFinder
Expand All @@ -110,49 +116,59 @@ func (c *Client) getCreds(remote string, req *http.Request) (Endpoint, Access, C
access := ef.AccessFor(apiEndpoint.Url)

if access != NTLMAccess {
if requestHasAuth(req) || setAuthFromNetrc(netrcFinder, req) || access == NoneAccess {
return apiEndpoint, access, nullCreds, nil, nil, nil
if requestHasAuth(req) {
return apiEndpoint, access, nullCreds, nil, nil, false, nil
}

if !ignoreNetRc && setAuthFromNetrc(netrcFinder, req) {
return apiEndpoint, access, nullCreds, nil, nil, true, nil
}

if access == NoneAccess {
return apiEndpoint, access, nullCreds, nil, nil, false, nil
}

credsURL, err := getCredURLForAPI(ef, operation, remote, apiEndpoint, req)
if err != nil {
return apiEndpoint, access, nullCreds, nil, nil, errors.Wrap(err, "creds")
return apiEndpoint, access, nullCreds, nil, nil, false, errors.Wrap(err, "creds")
}

if credsURL == nil {
return apiEndpoint, access, nullCreds, nil, nil, nil
return apiEndpoint, access, nullCreds, nil, nil, false, nil
}

credHelper, creds, err := c.getGitCreds(ef, req, credsURL)
if err == nil {
tracerx.Printf("Filled credentials for %s", credsURL)
setRequestAuth(req, creds["username"], creds["password"])
}
return apiEndpoint, access, credHelper, credsURL, creds, err
return apiEndpoint, access, credHelper, credsURL, creds, false, err
}

// NTLM ONLY

credsURL, err := url.Parse(apiEndpoint.Url)
if err != nil {
return apiEndpoint, access, nullCreds, nil, nil, errors.Wrap(err, "creds")
return apiEndpoint, access, nullCreds, nil, nil, false, errors.Wrap(err, "creds")
}

if netrcMachine := getAuthFromNetrc(netrcFinder, req); netrcMachine != nil {
creds := Creds{
"protocol": credsURL.Scheme,
"host": credsURL.Host,
"username": netrcMachine.Login,
"password": netrcMachine.Password,
"source": "netrc",
}
if !ignoreNetRc {
if netrcMachine := getAuthFromNetrc(netrcFinder, req); netrcMachine != nil {
creds := Creds{
"protocol": credsURL.Scheme,
"host": credsURL.Host,
"username": netrcMachine.Login,
"password": netrcMachine.Password,
"source": "netrc",
}

return apiEndpoint, access, nullCreds, credsURL, creds, nil
return apiEndpoint, access, nullCreds, credsURL, creds, true, nil
}
}

// NTLM uses creds to create the session
credHelper, creds, err := c.getGitCreds(ef, req, credsURL)
return apiEndpoint, access, credHelper, credsURL, creds, err
return apiEndpoint, access, credHelper, credsURL, creds, false, err
}

func (c *Client) getGitCreds(ef EndpointFinder, req *http.Request, u *url.URL) (CredentialHelper, Creds, error) {
Expand Down
58 changes: 51 additions & 7 deletions lfsapi/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,13 @@ type getCredsExpected struct {
}

type getCredsTest struct {
Remote string
Method string
Href string
Header map[string]string
Config map[string]string
Expected getCredsExpected
Remote string
Method string
Href string
Header map[string]string
Config map[string]string
Expected getCredsExpected
SkipNetrc bool
}

func TestGetCreds(t *testing.T) {
Expand Down Expand Up @@ -364,6 +365,27 @@ func TestGetCreds(t *testing.T) {
},
},
},
"ignore ntlm with netrc": getCredsTest{
Remote: "origin",
Method: "GET",
Href: "https://netrc-host.com/repo/lfs/locks",
Config: map[string]string{
"lfs.url": "https://netrc-host.com/repo/lfs",
"lfs.https://netrc-host.com/repo/lfs.access": "ntlm",
},
Expected: getCredsExpected{
Access: NTLMAccess,
Endpoint: "https://netrc-host.com/repo/lfs",
CredsURL: "https://netrc-host.com/repo/lfs",
Creds: map[string]string{
"protocol": "https",
"host": "netrc-host.com",
"username": "netrc-host.com",
"password": "monkey",
},
},
SkipNetrc: true,
},
"custom auth": getCredsTest{
Remote: "origin",
Method: "GET",
Expand Down Expand Up @@ -395,6 +417,28 @@ func TestGetCreds(t *testing.T) {
Authorization: basicAuth("abc", "def"),
},
},
"ignore netrc": getCredsTest{
Remote: "origin",
Method: "GET",
Href: "https://netrc-host.com/repo/lfs/locks",
Config: map[string]string{
"lfs.url": "https://[email protected]/repo/lfs",
"lfs.https://netrc-host.com/repo/lfs.access": "basic",
},
Expected: getCredsExpected{
Access: BasicAccess,
Endpoint: "https://[email protected]/repo/lfs",
Authorization: basicAuth("user", "monkey"),
CredsURL: "https://[email protected]/repo/lfs",
Creds: map[string]string{
"protocol": "https",
"host": "netrc-host.com",
"username": "user",
"password": "monkey",
},
},
SkipNetrc: true,
},
"username in url": getCredsTest{
Remote: "origin",
Method: "GET",
Expand Down Expand Up @@ -572,7 +616,7 @@ func TestGetCreds(t *testing.T) {
client.Credentials = &fakeCredentialFiller{}
client.Netrc = &fakeNetrc{}
client.Endpoints = NewEndpointFinder(ctx)
endpoint, access, _, credsURL, creds, err := client.getCreds(test.Remote, req)
endpoint, access, _, credsURL, creds, _, err := client.getCreds(test.Remote, req, test.SkipNetrc)
if !assert.Nil(t, err) {
continue
}
Expand Down
2 changes: 1 addition & 1 deletion lfsapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func (c *Client) doWithRedirects(cli *http.Client, req *http.Request, remote str
// presence of the Authorization header), then recur through
// doWithAuth, retaining the requests via but only after
// authenticating the redirected request.
return c.doWithAuth(remote, redirectedReq, via)
return c.doWithAuth(remote, redirectedReq, via, false)
}
return c.doWithRedirects(cli, redirectedReq, remote, via)
}
Expand Down
12 changes: 8 additions & 4 deletions t/t-credentials.sh
Original file line number Diff line number Diff line change
Expand Up @@ -311,16 +311,20 @@ begin_test "credentials from netrc with bad password"
clone_repo "$reponame" repo3

# Need a remote named "localhost" or 127.0.0.1 in netrc will interfere with the other auth
git remote add "netrc" "$(echo $GITSERVER | sed s/127.0.0.1/localhost/)/netrctest"
gitserverhost=$(echo "$GITSERVER" | sed s/127.0.0.1/localhost/ | cut -d'/' -f3)
git remote add "netrc" "http://netrcuser:netrcpass@$gitserverhost/$reponame.git"
git lfs env

git lfs track "*.dat"
echo "push a" > a.dat
git add .gitattributes a.dat
git commit -m "add a.dat"

git push netrc master 2>&1 | tee push.log
[ "0" = "$(grep -c "Uploading LFS objects: 100% (1/1), 7 B" push.log)" ]

# netrc credentials should fail, and a subsequent request should be made with
# the URL credentials
GIT_TRACE=1 git push netrc master 2>&1 | tee push.log
[ "1" = "$(grep -c "netrc credentials were rejected" push.log)" ]
[ "1" = "$(grep -c "Uploading LFS objects: 100% (1/1), 7 B" push.log)" ]
)
end_test

Expand Down