Skip to content

Commit

Permalink
gerrit: allow setAuth to fallthrough to netrc lookup
Browse files Browse the repository at this point in the history
When git has no http cookies, the request for http cookies
will fail because git will exit(1). Ignore this failure
because the output is properly tested either way. This allows
authentication to fallthrough to the netrc lookup.

Correct the netrc lookup under windows. git reads the netrc
file as "_netrc" in the users home directory.

Add a warning at the end of the function that no authentication
was set.

Fixes golang/go#26782

Change-Id: I0ba94ff7fa4b6038d6117156dcc729ddf4616fdc
Reviewed-on: https://go-review.googlesource.com/127855
Reviewed-by: Brad Fitzpatrick <[email protected]>
  • Loading branch information
kardianos committed Aug 3, 2018
1 parent 24a3043 commit c1b72a7
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions gerrit/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -59,18 +60,25 @@ func GitCookieFileAuth(file string) Auth {
return &gitCookieFileAuth{file: file}
}

func netrcPath() string {
if runtime.GOOS == "windows" {
return filepath.Join(os.Getenv("USERPROFILE"), "_netrc")
}
return filepath.Join(os.Getenv("HOME"), ".netrc")
}

type gitCookiesAuth struct{}

func (gitCookiesAuth) setAuth(c *Client, r *http.Request) {
// First look in Git's http.cookiefile, which is where Gerrit
// now tells users to store this information.
git := exec.Command("git", "config", "http.cookiefile")
git.Stderr = os.Stderr
gitOut, err := git.Output()
if err != nil {
log.Printf("git config http.cookiefile failed: %s", err)
return
}

// Ignore a failure here, git will exit(1) if no cookies are
// present and prevent the netrc from being read below.
gitOut, _ := git.Output()

cookieFile := strings.TrimSpace(string(gitOut))
if len(cookieFile) != 0 {
auth := &gitCookieFileAuth{file: cookieFile}
Expand All @@ -90,7 +98,8 @@ func (gitCookiesAuth) setAuth(c *Client, r *http.Request) {
// used to tell users to store the information, until the passwords
// got so long that old versions of curl couldn't handle them.
host := url.Host
data, _ := ioutil.ReadFile(filepath.Join(os.Getenv("HOME"), ".netrc"))
netrc := netrcPath()
data, _ := ioutil.ReadFile(netrc)
for _, line := range strings.Split(string(data), "\n") {
if i := strings.Index(line, "#"); i >= 0 {
line = line[:i]
Expand All @@ -101,6 +110,7 @@ func (gitCookiesAuth) setAuth(c *Client, r *http.Request) {
return
}
}
log.Printf("no authentication configured for Gerrit; tried both git config http.cookiefile and %s", netrc)
}

type gitCookieFileAuth struct {
Expand Down

0 comments on commit c1b72a7

Please sign in to comment.