-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbosh.go
More file actions
195 lines (163 loc) · 3.88 KB
/
Copy pathbosh.go
File metadata and controls
195 lines (163 loc) · 3.88 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"regexp"
"strings"
"github.com/doomsday-project/doomsday/storage/uaa"
)
type client struct {
URL string
Username string
Password string
AccessToken string
RefreshToken string
SkipSSLValidation bool
isBasic bool
cache map[string]string
}
type boshInfo struct {
Auth struct {
Type string `json:"type"`
Options struct {
URL string `json:"url"`
} `json:"options"`
} `json:"user_authentication"`
}
var schemeRegex = regexp.MustCompile("^(http|https)://")
func (c client) path(path string) string {
uStr := c.URL
if !schemeRegex.MatchString(uStr) {
uStr = "https://" + uStr
}
u, err := url.Parse(uStr)
if err != nil {
return c.URL + path
}
if u.Port() == "" {
u.Host = u.Host + ":25555"
}
u.Path = path
u.RawPath = path
return u.String()
}
func (c client) basicAuthHeader() string {
return fmt.Sprintf("Basic %s",
base64.StdEncoding.EncodeToString(
[]byte(fmt.Sprintf("%s:%s", c.Username, c.Password)),
),
)
}
func (c client) accessTokenHeader() string {
return fmt.Sprintf("Bearer %s", c.AccessToken)
}
func (c *client) fetchAuthHeader() (string, error) {
if c.AccessToken != "" {
return c.accessTokenHeader(), nil
}
if c.isBasic {
c.basicAuthHeader()
}
if c.Username == "" && c.Password == "" && c.RefreshToken == "" {
return "", fmt.Errorf("No authorization options. Need to log in")
}
//Check out /info for the type of auth
req, err := http.NewRequest("GET", c.path("/info"), nil)
if err != nil {
return "", err
}
info := boshInfo{}
err = c.Do(req, "/info", &info)
if err != nil {
return "", err
}
header := ""
switch info.Auth.Type {
case "basic":
c.isBasic = true
header = c.basicAuthHeader()
case "uaa":
uaac := uaa.Client{
URL: info.Auth.Options.URL,
SkipTLSValidation: true,
}
var authResp *uaa.AuthResponse
if c.RefreshToken != "" {
log.Write("Performing refresh token grant UAA auth")
authResp, err = uaac.Refresh("bosh_cli", "", c.RefreshToken)
} else {
log.Write("Performing password grant UAA auth")
log.Write("with username `%s' and password `%s'", c.Username, c.Password)
authResp, err = uaac.Password("bosh_cli", "", c.Username, c.Password)
}
if err == nil {
c.AccessToken = authResp.AccessToken
header = c.accessTokenHeader()
}
default:
err = fmt.Errorf("Unknown auth type: `%s'", info.Auth.Type)
}
return header, err
}
func (c *client) Get(path string, output interface{}) error {
cacheBody, cacheHit := c.cache[path]
if cacheHit {
log.Write("http cache hit: %s", path)
err := json.NewDecoder(strings.NewReader(cacheBody)).Decode(output)
return err
}
log.Write("http cache miss: %s", path)
authHeader, err := c.fetchAuthHeader()
if err != nil {
return err
}
req, err := http.NewRequest("GET", c.path(path), nil)
if err != nil {
return err
}
req.Header.Set("Authorization", authHeader)
return c.Do(req, path, output)
}
func (c *client) Do(req *http.Request, path string, output interface{}) error {
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: c.SkipSSLValidation,
},
},
}
dump, err := httputil.DumpRequestOut(req, true)
if err == nil {
log.Write("%s", string(dump))
}
resp, err := client.Do(req)
if err != nil {
return err
}
dump, err = httputil.DumpResponse(resp, true)
if err == nil {
log.Write("%s", string(dump))
}
if resp.StatusCode >= 300 {
return fmt.Errorf("Non-2xx response code")
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
log.Write("Inserting to cache: %s", path)
c.cache[path] = string(bodyBytes)
if output != nil {
err := json.Unmarshal(bodyBytes, output)
if err != nil {
return err
}
}
return nil
}