Skip to content

Commit 9e42df5

Browse files
Add insecure flag for http getter
1 parent e2a8659 commit 9e42df5

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

client.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ type Client struct {
6767
// By default a no op progress listener is used.
6868
ProgressListener ProgressTracker
6969

70+
// Insecure controls whether a client verifies the server's
71+
// certificate chain and host name. If Insecure is true, crypto/tls
72+
// accepts any certificate presented by the server and any host name in that
73+
// certificate. In this mode, TLS is susceptible to machine-in-the-middle
74+
// attacks unless custom verification is used. This should be used only for
75+
// testing or in combination with VerifyConnection or VerifyPeerCertificate.
76+
// This is identical to tls.Config.InsecureSkipVerify.
77+
Insecure bool
78+
7079
Options []ClientOption
7180
}
7281

@@ -289,7 +298,7 @@ func (c *Client) Get() error {
289298
// if we're specifying a subdir.
290299
err := g.Get(dst, u)
291300
if err != nil {
292-
err = fmt.Errorf("error downloading '%s': %s", src, err)
301+
err = fmt.Errorf("error downloading '%s': %s", u.Redacted(), err)
293302
return err
294303
}
295304
}

client_option_insecure.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package getter
2+
3+
// WithInsecure allows for a user to avoid
4+
// checking certificates (not recommended).
5+
// For example, when connecting on HTTPS where an
6+
// invalid certificate is presented.
7+
// User assumes all risk.
8+
// Not all getters have support for insecure mode yet.
9+
func WithInsecure() func(*Client) error {
10+
return func(c *Client) error {
11+
c.Insecure = true
12+
return nil
13+
}
14+
}

cmd/go-getter/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
func main() {
1515
modeRaw := flag.String("mode", "any", "get mode (any, file, dir)")
1616
progress := flag.Bool("progress", false, "display terminal progress")
17+
insecure := flag.Bool("insecure", false, "do not verify server's certificate chain (not recommended)")
1718
flag.Parse()
1819
args := flag.Args()
1920
if len(args) < 2 {
@@ -46,6 +47,11 @@ func main() {
4647
opts = append(opts, getter.WithProgress(defaultProgressBar))
4748
}
4849

50+
if *insecure {
51+
log.Println("WARNING: Using Insecure TLS transport!")
52+
opts = append(opts, getter.WithInsecure())
53+
}
54+
4955
ctx, cancel := context.WithCancel(context.Background())
5056
// Build the client
5157
client := &getter.Client{

get_http.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package getter
22

33
import (
44
"context"
5+
"crypto/tls"
56
"encoding/xml"
67
"fmt"
78
"io"
@@ -11,6 +12,7 @@ import (
1112
"path/filepath"
1213
"strings"
1314

15+
"github.com/hashicorp/go-cleanhttp"
1416
safetemp "github.com/hashicorp/go-safetemp"
1517
)
1618

@@ -74,6 +76,11 @@ func (g *HttpGetter) Get(dst string, u *url.URL) error {
7476

7577
if g.Client == nil {
7678
g.Client = httpClient
79+
if g.client != nil && g.client.Insecure {
80+
insecureTransport := cleanhttp.DefaultTransport()
81+
insecureTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
82+
g.Client.Transport = insecureTransport
83+
}
7784
}
7885

7986
// Add terraform-get to the parameter.
@@ -157,6 +164,11 @@ func (g *HttpGetter) GetFile(dst string, src *url.URL) error {
157164

158165
if g.Client == nil {
159166
g.Client = httpClient
167+
if g.client != nil && g.client.Insecure {
168+
insecureTransport := cleanhttp.DefaultTransport()
169+
insecureTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
170+
g.Client.Transport = insecureTransport
171+
}
160172
}
161173

162174
var currentFileSize int64

0 commit comments

Comments
 (0)