forked from hashicorp/go-getter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
functions for adding netrc info to URLs
- Loading branch information
Showing
6 changed files
with
163 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package getter | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"runtime" | ||
|
||
"github.com/bgentry/go-netrc/netrc" | ||
"github.com/mitchellh/go-homedir" | ||
) | ||
|
||
// addAuthFromNetrc adds auth information to the URL from the user's | ||
// netrc file if it can be found. This will only add the auth info | ||
// if the URL doesn't already have auth info specified and the | ||
// the username is blank. | ||
func addAuthFromNetrc(u *url.URL) error { | ||
// If the URL already has auth information, do nothing | ||
if u.User != nil && u.User.Username() != "" { | ||
return nil | ||
} | ||
|
||
// Get the netrc file path | ||
path := os.Getenv("NETRC") | ||
if path == "" { | ||
filename := ".netrc" | ||
if runtime.GOOS == "windows" { | ||
filename = "_netrc" | ||
} | ||
|
||
var err error | ||
path, err = homedir.Expand("~/" + filename) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// If the file is not a file, then do nothing | ||
if fi, err := os.Stat(path); err != nil { | ||
// File doesn't exist, do nothing | ||
if os.IsNotExist(err) { | ||
return nil | ||
} | ||
|
||
// Some other error! | ||
return err | ||
} else if fi.IsDir() { | ||
// File is directory, ignore | ||
return nil | ||
} | ||
|
||
// Load up the netrc file | ||
net, err := netrc.ParseFile(path) | ||
if err != nil { | ||
return fmt.Errorf("Error parsing netrc file at %q: %s", path, err) | ||
} | ||
|
||
machine := net.FindMachine(u.Host) | ||
if machine == nil { | ||
// Machine not found, no problem | ||
return nil | ||
} | ||
|
||
// Set the user info | ||
u.User = url.UserPassword(machine.Login, machine.Password) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package getter | ||
|
||
import ( | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
func TestAddAuthFromNetrc(t *testing.T) { | ||
defer tempEnv(t, "NETRC", "./test-fixtures/netrc/basic")() | ||
|
||
u, err := url.Parse("http://example.com") | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
|
||
if err := addAuthFromNetrc(u); err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
|
||
expected := "http://foo:[email protected]" | ||
actual := u.String() | ||
if expected != actual { | ||
t.Fatalf("Mismatch: %q != %q", actual, expected) | ||
} | ||
} | ||
|
||
func TestAddAuthFromNetrc_hasAuth(t *testing.T) { | ||
defer tempEnv(t, "NETRC", "./test-fixtures/netrc/basic")() | ||
|
||
u, err := url.Parse("http://username:[email protected]") | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
|
||
expected := u.String() | ||
if err := addAuthFromNetrc(u); err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
|
||
actual := u.String() | ||
if expected != actual { | ||
t.Fatalf("Mismatch: %q != %q", actual, expected) | ||
} | ||
} | ||
|
||
func TestAddAuthFromNetrc_hasUsername(t *testing.T) { | ||
defer tempEnv(t, "NETRC", "./test-fixtures/netrc/basic")() | ||
|
||
u, err := url.Parse("http://[email protected]") | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
|
||
expected := u.String() | ||
if err := addAuthFromNetrc(u); err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
|
||
actual := u.String() | ||
if expected != actual { | ||
t.Fatalf("Mismatch: %q != %q", actual, expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
machine example.com | ||
login foo | ||
password bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package getter | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
// tempEnv sets the env var temporarily and returns a function that should | ||
// be deferred to clean it up. | ||
func tempEnv(t *testing.T, k, v string) func() { | ||
old := os.Getenv(k) | ||
|
||
// Set env | ||
if err := os.Setenv(k, v); err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
|
||
// Easy cleanup | ||
return func() { | ||
if err := os.Setenv(k, old); err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
} | ||
} |