forked from heroku/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
130 lines (117 loc) · 2.64 KB
/
request.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
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"container/list"
"crypto/tls"
"crypto/x509"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/franela/goreq"
)
func init() {
goreq.SetConnectTimeout(15 * time.Second)
certs := getCACerts()
if certs != nil {
goreq.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{RootCAs: certs}
}
}
func useSystemCerts() bool {
e := os.Getenv("HEROKU_USE_SYSTEM_CERTS")
return e != "false" && e != "0"
}
func apiRequestBase(authToken string) *goreq.Request {
req := goreq.Request{
Uri: apiURL(),
ShowDebug: debugging,
Insecure: !shouldVerifyHost(apiURL()),
UserAgent: version(),
}
if authToken != "" {
req.AddHeader("Authorization", "Bearer "+authToken)
}
if os.Getenv("HEROKU_HEADERS") != "" {
var h map[string]string
json.Unmarshal([]byte(os.Getenv("HEROKU_HEADERS")), &h)
for k, v := range h {
req.AddHeader(k, v)
}
}
return &req
}
func apiRequest(authToken string) *goreq.Request {
req := apiRequestBase(authToken)
req.AddHeader("Accept", "application/vnd.heroku+json; version=3")
return req
}
func shouldVerifyHost(host string) bool {
return !(os.Getenv("HEROKU_SSL_VERIFY") == "disable" || strings.HasSuffix(host, "herokudev.com"))
}
func getCACerts() *x509.CertPool {
paths := list.New()
if !useSystemCerts() {
path := filepath.Join(AppDir(), "cacert.pem")
if _, err := os.Stat(path); os.IsNotExist(err) {
downloadCert(path)
}
paths.PushBack(path)
}
ssl_cert_file := os.Getenv("SSL_CERT_FILE")
if ssl_cert_file != "" {
paths.PushBack(ssl_cert_file)
}
ssl_cert_dir := os.Getenv("SSL_CERT_DIR")
if ssl_cert_dir != "" {
files, err := ioutil.ReadDir(ssl_cert_dir)
if err != nil {
Warn("Error opening " + ssl_cert_dir)
return nil
}
for _, file := range files {
path := filepath.Join(ssl_cert_dir, file.Name())
paths.PushBack(path)
}
}
if paths.Len() == 0 {
return nil
}
certs := x509.NewCertPool()
Debugln("Adding the following trusted certificate authorities")
for e := paths.Front(); e != nil; e = e.Next() {
path := e.Value.(string)
Debugln(" " + path)
data, err := ioutil.ReadFile(path)
if err != nil {
WarnIfError(err)
return nil
}
ok := certs.AppendCertsFromPEM(data)
if !ok {
Warn("Error parsing " + path)
return nil
}
}
return certs
}
func downloadCert(path string) {
f, err := os.Create(path)
if err != nil {
WarnIfError(err)
return
}
res, err := goreq.Request{
Uri: "https://cli-assets.heroku.com/cacert.pem",
ShowDebug: debugging,
}.Do()
if err != nil {
WarnIfError(err)
return
}
defer res.Body.Close()
defer f.Close()
io.Copy(f, res.Body)
}