forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_client.go
117 lines (99 loc) · 3.43 KB
/
http_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package net
import (
"code.google.com/p/go.net/websocket"
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/cloudfoundry/cli/cf/trace"
"net"
"net/http"
"net/http/httputil"
"net/url"
"regexp"
"strings"
"time"
"github.com/cloudfoundry/cli/cf/i18n"
)
var (
t = i18n.Init("cf/net", i18n.GetResourcesPath())
PRIVATE_DATA_PLACEHOLDER = t("[PRIVATE DATA HIDDEN]")
)
func newHttpClient(trustedCerts []tls.Certificate, disableSSL bool) *http.Client {
tr := &http.Transport{
TLSClientConfig: NewTLSConfig(trustedCerts, disableSSL),
Proxy: http.ProxyFromEnvironment,
}
return &http.Client{
Transport: tr,
CheckRedirect: PrepareRedirect,
}
}
func PrepareRedirect(req *http.Request, via []*http.Request) error {
if len(via) > 1 {
return errors.New(T("stopped after 1 redirect"))
}
prevReq := via[len(via)-1]
req.Header.Set("Authorization", prevReq.Header.Get("Authorization"))
dumpRequest(req)
return nil
}
func Sanitize(input string) (sanitized string) {
var sanitizeJson = func(propertyName string, json string) string {
regex := regexp.MustCompile(fmt.Sprintf(`"%s":\s*"[^"]*"`, propertyName))
return regex.ReplaceAllString(json, fmt.Sprintf(`"%s":"%s"`, propertyName, PRIVATE_DATA_PLACEHOLDER))
}
re := regexp.MustCompile(`(?m)^Authorization: .*`)
sanitized = re.ReplaceAllString(input, "Authorization: "+PRIVATE_DATA_PLACEHOLDER)
re = regexp.MustCompile(`password=[^&]*&`)
sanitized = re.ReplaceAllString(sanitized, "password="+PRIVATE_DATA_PLACEHOLDER+"&")
sanitized = sanitizeJson("access_token", sanitized)
sanitized = sanitizeJson("refresh_token", sanitized)
sanitized = sanitizeJson("token", sanitized)
sanitized = sanitizeJson("password", sanitized)
sanitized = sanitizeJson("oldPassword", sanitized)
return
}
func dumpRequest(req *http.Request) {
shouldDisplayBody := !strings.Contains(req.Header.Get("Content-Type"), "multipart/form-data")
dumpedRequest, err := httputil.DumpRequest(req, shouldDisplayBody)
if err != nil {
trace.Logger.Printf(T("Error dumping request\n{{.Err}}\n", map[string]interface{}{"Err": err}))
} else {
trace.Logger.Printf("\n%s [%s]\n%s\n", terminal.HeaderColor(T("REQUEST:")), time.Now().Format(time.RFC3339), Sanitize(string(dumpedRequest)))
if !shouldDisplayBody {
trace.Logger.Println(T("[MULTIPART/FORM-DATA CONTENT HIDDEN]"))
}
}
}
func dumpResponse(res *http.Response) {
dumpedResponse, err := httputil.DumpResponse(res, true)
if err != nil {
trace.Logger.Printf(T("Error dumping response\n{{.Err}}\n", map[string]interface{}{"Err": err}))
} else {
trace.Logger.Printf("\n%s [%s]\n%s\n", terminal.HeaderColor(T("RESPONSE:")), time.Now().Format(time.RFC3339), Sanitize(string(dumpedResponse)))
}
}
func WrapNetworkErrors(host string, err error) error {
var innerErr error
switch typedErr := err.(type) {
case *url.Error:
innerErr = typedErr.Err
case *websocket.DialError:
innerErr = typedErr.Err
}
if innerErr != nil {
switch typedErr := innerErr.(type) {
case x509.UnknownAuthorityError:
return errors.NewInvalidSSLCert(host, T("unknown authority"))
case x509.HostnameError:
return errors.NewInvalidSSLCert(host, T("not valid for the requested host"))
case x509.CertificateInvalidError:
return errors.NewInvalidSSLCert(host, "")
case *net.OpError:
return typedErr.Err
}
}
return errors.NewWithError(T("Error performing request"), err)
}