/
session.go
93 lines (79 loc) · 1.89 KB
/
session.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
// AWS session facilities. Allow using https proxy for requests
package aws
import (
"crypto/tls"
"net/http"
"net/url"
"os"
"strings"
"github.com/golang/glog"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
)
const (
// Environment variable names
// https proxy. This allows using
// https proxy other than bash's http proxy
ENV_HTTPS_PROXY = "CF_HTTPS_PROXY"
)
var (
// TO-DO:
// Investigate region picking up from env
AWSSess = session.Must(
session.NewSessionWithOptions(
session.Options{
Config: aws.Config{
HTTPClient: GetHttpClient(),
},
SharedConfigState: session.SharedConfigEnable,
},
),
)
)
// Return a session by given a profile name
func GetSessionWithProfile(profile string) *session.Session {
return session.Must(
session.NewSessionWithOptions(
session.Options{
Profile: profile,
Config: aws.Config{
HTTPClient: GetHttpClient(),
},
SharedConfigState: session.SharedConfigEnable,
},
),
)
}
// Get http client for aws calls
func GetHttpClient() *http.Client {
// Setup tool specific https proxy if available
// This allows cfctl to use proxy other than
// standard 'https_proxy'
if proxyStr := getHTTPSProxy(); len(proxyStr) > 0 {
proxyURL, err := url.Parse(proxyStr)
if err != nil {
glog.Fatalf("Error parsing proxy URL %s", proxyStr)
}
transport := http.Transport{
Proxy: http.ProxyURL(proxyURL),
TLSClientConfig: &tls.Config{},
}
return &http.Client{
Transport: &transport,
}
}
return &http.Client{}
}
// Get https proxy string from
// environment variable
func getHTTPSProxy() string {
var proxyStr string
if len(os.Getenv(ENV_HTTPS_PROXY)) > 0 {
proxyStr = os.Getenv(ENV_HTTPS_PROXY)
}
// Override capital variable if set
if len(os.Getenv(strings.ToLower(ENV_HTTPS_PROXY))) > 0 {
proxyStr = os.Getenv(strings.ToLower(ENV_HTTPS_PROXY))
}
return proxyStr
}