From 9d05482d36c13db820bddce3fc31174751f53a8e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 20 Aug 2016 18:56:51 -0400 Subject: [PATCH] HTTP getter respects netrc --- get_http.go | 5 ++++ get_http_test.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ util_test.go | 25 ++++++++++++++++ 3 files changed, 106 insertions(+) diff --git a/get_http.go b/get_http.go index 06970c58d..0cfbf28c0 100644 --- a/get_http.go +++ b/get_http.go @@ -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") diff --git a/get_http_test.go b/get_http_test.go index 417523164..2501c3593 100644 --- a/get_http_test.go +++ b/get_http_test.go @@ -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 { @@ -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 @@ -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()))) } @@ -182,3 +252,9 @@ const testHttpNoneStr = ` ` + +const testHttpNetrc = ` +machine %s +login foo +password bar +` diff --git a/util_test.go b/util_test.go index b99be8c12..a3e7d829c 100644 --- a/util_test.go +++ b/util_test.go @@ -1,7 +1,10 @@ package getter import ( + "io" + "io/ioutil" "os" + "strings" "testing" ) @@ -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) + } + } +}