Skip to content

Commit

Permalink
HTTP getter respects netrc
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Aug 20, 2016
1 parent bb6b083 commit 9d05482
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
5 changes: 5 additions & 0 deletions get_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ func (g *HttpGetter) Get(dst string, u *url.URL) error {
var newU url.URL = *u
u = &newU

// Add auth from netrc if we can
if err := addAuthFromNetrc(u); err != nil {
return err
}

// Add terraform-get to the parameter.
q := u.Query()
q.Add("terraform-get", "1")
Expand Down
76 changes: 76 additions & 0 deletions get_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,60 @@ func TestHttpGetter_file(t *testing.T) {
assertContents(t, dst, "Hello\n")
}

func TestHttpGetter_auth(t *testing.T) {
ln := testHttpServer(t)
defer ln.Close()

g := new(HttpGetter)
dst := tempDir(t)

var u url.URL
u.Scheme = "http"
u.Host = ln.Addr().String()
u.Path = "/meta-auth"
u.User = url.UserPassword("foo", "bar")

// Get it!
if err := g.Get(dst, &u); err != nil {
t.Fatalf("err: %s", err)
}

// Verify the main file exists
mainPath := filepath.Join(dst, "main.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
}

func TestHttpGetter_authNetrc(t *testing.T) {
ln := testHttpServer(t)
defer ln.Close()

g := new(HttpGetter)
dst := tempDir(t)

var u url.URL
u.Scheme = "http"
u.Host = ln.Addr().String()
u.Path = "/meta"

// Write the netrc file
path, closer := tempFileContents(t, fmt.Sprintf(testHttpNetrc, ln.Addr().String()))
defer closer()
defer tempEnv(t, "NETRC", path)()

// Get it!
if err := g.Get(dst, &u); err != nil {
t.Fatalf("err: %s", err)
}

// Verify the main file exists
mainPath := filepath.Join(dst, "main.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
}

func testHttpServer(t *testing.T) net.Listener {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
Expand All @@ -138,6 +192,7 @@ func testHttpServer(t *testing.T) net.Listener {
mux.HandleFunc("/file", testHttpHandlerFile)
mux.HandleFunc("/header", testHttpHandlerHeader)
mux.HandleFunc("/meta", testHttpHandlerMeta)
mux.HandleFunc("/meta-auth", testHttpHandlerMetaAuth)
mux.HandleFunc("/meta-subdir", testHttpHandlerMetaSubdir)

var server http.Server
Expand All @@ -160,6 +215,21 @@ func testHttpHandlerMeta(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf(testHttpMetaStr, testModuleURL("basic").String())))
}

func testHttpHandlerMetaAuth(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
if !ok {
w.WriteHeader(401)
return
}

if user != "foo" || pass != "bar" {
w.WriteHeader(401)
return
}

w.Write([]byte(fmt.Sprintf(testHttpMetaStr, testModuleURL("basic").String())))
}

func testHttpHandlerMetaSubdir(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf(testHttpMetaStr, testModuleURL("basic//subdir").String())))
}
Expand All @@ -182,3 +252,9 @@ const testHttpNoneStr = `
</head>
</html>
`

const testHttpNetrc = `
machine %s
login foo
password bar
`
25 changes: 25 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package getter

import (
"io"
"io/ioutil"
"os"
"strings"
"testing"
)

Expand All @@ -22,3 +25,25 @@ func tempEnv(t *testing.T, k, v string) func() {
}
}
}

// tempFileContents writes a temporary file and returns the path and a function
// to clean it up.
func tempFileContents(t *testing.T, contents string) (string, func()) {
tf, err := ioutil.TempFile("", "getter")
if err != nil {
t.Fatalf("err: %s", err)
}

if _, err := io.Copy(tf, strings.NewReader(contents)); err != nil {
t.Fatalf("err: %s", err)
}

tf.Close()

path := tf.Name()
return path, func() {
if err := os.Remove(path); err != nil {
t.Fatalf("err: %s", err)
}
}
}

0 comments on commit 9d05482

Please sign in to comment.