-
Notifications
You must be signed in to change notification settings - Fork 227
/
session.go
408 lines (364 loc) · 9.87 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package proxmox
// inspired by https://github.com/openstack/golang-client/blob/master/openstack/session.go
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
)
var Debug = new(bool)
const DebugLargeBodyThreshold = 5 * 1024 * 1024
type Response struct {
Resp *http.Response
Body []byte
}
type Session struct {
httpClient *http.Client
ApiUrl string
AuthTicket string
CsrfToken string
AuthToken string // Combination of user, realm, token ID and UUID
Headers http.Header
}
func NewSession(apiUrl string, hclient *http.Client, proxyString string, tls *tls.Config) (session *Session, err error) {
if hclient == nil {
if proxyString == "" {
tr := &http.Transport{
TLSClientConfig: tls,
DisableCompression: true,
Proxy: nil,
}
hclient = &http.Client{Transport: tr}
} else {
proxyURL, err := url.ParseRequestURI(proxyString)
if err != nil {
return nil, err
}
if _, _, err := net.SplitHostPort(proxyURL.Host); err != nil {
return nil, err
} else {
// Only build a transport if we're also building the client
tr := &http.Transport{
TLSClientConfig: tls,
DisableCompression: true,
Proxy: http.ProxyURL(proxyURL),
}
hclient = &http.Client{Transport: tr}
}
}
}
session = &Session{
httpClient: hclient,
ApiUrl: apiUrl,
AuthTicket: "",
CsrfToken: "",
Headers: http.Header{},
}
return session, nil
}
func ParamsToBody(params map[string]interface{}) (body []byte) {
vals := ParamsToValuesWithEmpty(params, []string{})
body = bytes.NewBufferString(vals.Encode()).Bytes()
return
}
func ParamsToValues(params map[string]interface{}) (vals url.Values) {
vals = ParamsToValuesWithEmpty(params, []string{})
return
}
func ParamsToBodyWithEmpty(params map[string]interface{}, allowedEmpty []string) (body []byte) {
vals := ParamsToValuesWithEmpty(params, allowedEmpty)
body = bytes.NewBufferString(vals.Encode()).Bytes()
return
}
func ParamsToBodyWithAllEmpty(params map[string]interface{}) (body []byte) {
vals := ParamsToValuesWithAllEmpty(params, []string{}, true)
body = bytes.NewBufferString(vals.Encode()).Bytes()
return
}
func ParamsToValuesWithEmpty(params map[string]interface{}, allowedEmpty []string) (vals url.Values) {
return ParamsToValuesWithAllEmpty(params, allowedEmpty, false)
}
func ParamsToValuesWithAllEmpty(params map[string]interface{}, allowedEmpty []string, allowEmpty bool) (vals url.Values) {
vals = url.Values{}
for k, intrV := range params {
var v string
switch intrV := intrV.(type) {
// Convert true/false bool to 1/0 string where Proxmox API can understand it.
case bool:
if intrV {
v = "1"
} else {
v = "0"
}
case []string:
for _, v := range intrV {
vals.Add(k, fmt.Sprintf("%v", v))
}
continue
default:
v = fmt.Sprintf("%v", intrV)
}
if allowEmpty {
vals.Set(k, v)
} else if v != "" || inArray(allowedEmpty, k) {
vals.Set(k, v)
}
}
return
}
func decodeResponse(resp *http.Response, v interface{}) error {
if resp.Body == nil {
return nil
}
rbody, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error reading response body: %s", err)
}
if err = json.Unmarshal(rbody, &v); err != nil {
return err
}
return nil
}
func ResponseJSON(resp *http.Response) (jbody map[string]interface{}, err error) {
err = decodeResponse(resp, &jbody)
return jbody, err
}
// Is this needed?
func TypedResponse(resp *http.Response, v interface{}) error {
var intermediate struct {
Data struct {
Result json.RawMessage `json:"result"`
} `json:"data"`
}
err := decodeResponse(resp, &intermediate)
if err != nil {
return fmt.Errorf("error reading response envelope: %v", err)
}
if err = json.Unmarshal(intermediate.Data.Result, v); err != nil {
return fmt.Errorf("error unmarshalling result %v", err)
}
return nil
}
func (s *Session) SetAPIToken(userID, token string) {
auth := fmt.Sprintf("%s=%s", userID, token)
s.AuthToken = auth
}
func (s *Session) Login(username string, password string, otp string) (err error) {
reqUser := map[string]interface{}{"username": username, "password": password}
if otp != "" {
reqUser["otp"] = otp
}
reqbody := ParamsToBody(reqUser)
olddebug := *Debug
*Debug = false // don't share passwords in debug log
resp, err := s.Post("/access/ticket", nil, &s.Headers, &reqbody)
*Debug = olddebug
if err != nil {
return err
}
if resp == nil {
return fmt.Errorf("Login error reading response")
}
dr, _ := httputil.DumpResponse(resp, true)
jbody, err := ResponseJSON(resp)
if err != nil {
return err
}
if jbody == nil || jbody["data"] == nil {
return fmt.Errorf("invalid login response:\n-----\n%s\n-----", dr)
}
dat := jbody["data"].(map[string]interface{})
//Check if the 2FA was required
if dat["NeedTFA"] == 1.0 {
return fmt.Errorf("missing TFA code")
}
s.AuthTicket = dat["ticket"].(string)
s.CsrfToken = dat["CSRFPreventionToken"].(string)
return nil
}
func (s *Session) NewRequest(method, url string, headers *http.Header, body io.Reader) (req *http.Request, err error) {
req, err = http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
if headers != nil {
req.Header = *headers
}
if s.AuthToken != "" {
req.Header["Authorization"] = []string{"PVEAPIToken=" + s.AuthToken}
} else if s.AuthTicket != "" {
req.Header["Authorization"] = []string{"PVEAuthCookie=" + s.AuthTicket}
req.Header["CSRFPreventionToken"] = []string{s.CsrfToken}
}
return
}
func (s *Session) Do(req *http.Request) (*http.Response, error) {
// Add session headers
for k, v := range s.Headers {
req.Header[k] = v
}
if *Debug {
includeBody := req.ContentLength < DebugLargeBodyThreshold
d, _ := httputil.DumpRequestOut(req, includeBody)
if !includeBody {
d = append(d, fmt.Sprintf("<request body of %d bytes not shown>\n\n", req.ContentLength)...)
}
log.Printf(">>>>>>>>>> REQUEST:\n%v", string(d))
}
resp, err := s.httpClient.Do(req)
if err != nil {
return nil, err
}
// The response body reader needs to be closed, but lots of places call
// session.Do, and they might not be able to reliably close it themselves.
// Therefore, read the body out, close the original, then replace it with
// a NopCloser over the bytes, which does not need to be closed downsteam.
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
resp.Body.Close()
resp.Body = io.NopCloser(bytes.NewReader(respBody))
if *Debug {
includeBody := resp.ContentLength < DebugLargeBodyThreshold
dr, _ := httputil.DumpResponse(resp, includeBody)
if !includeBody {
dr = append(dr, fmt.Sprintf("<response body of %d bytes not shown>\n\n", resp.ContentLength)...)
}
log.Printf("<<<<<<<<<< RESULT:\n%v", string(dr))
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return resp, fmt.Errorf(resp.Status)
}
return resp, nil
}
// Perform a simple get to an endpoint
func (s *Session) Request(
method string,
url string,
params *url.Values,
headers *http.Header,
body *[]byte,
) (resp *http.Response, err error) {
// add params to url here
url = s.ApiUrl + url
if params != nil {
url = url + "?" + params.Encode()
}
// Get the body if one is present
var buf io.Reader
if body != nil {
buf = bytes.NewReader(*body)
}
req, err := s.NewRequest(method, url, headers, buf)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
return s.Do(req)
}
// Perform a simple get to an endpoint and unmarshal returned JSON
func (s *Session) RequestJSON(
method string,
url string,
params *url.Values,
headers *http.Header,
body interface{},
responseContainer interface{},
) (resp *http.Response, err error) {
var bodyjson []byte
if body != nil {
bodyjson, err = json.Marshal(body)
if err != nil {
return nil, err
}
}
// if headers == nil {
// headers = &http.Header{}
// headers.Add("Content-Type", "application/json")
// }
resp, err = s.Request(method, url, params, headers, &bodyjson)
if err != nil {
return resp, err
}
// err = util.CheckHTTPResponseStatusCode(resp)
// if err != nil {
// return nil, err
// }
rbody, err := io.ReadAll(resp.Body)
if err != nil {
return resp, fmt.Errorf("error reading response body")
}
if err = json.Unmarshal(rbody, &responseContainer); err != nil {
return resp, err
}
return resp, nil
}
func (s *Session) Delete(
url string,
params *url.Values,
headers *http.Header,
) (resp *http.Response, err error) {
return s.Request("DELETE", url, params, headers, nil)
}
func (s *Session) Get(
url string,
params *url.Values,
headers *http.Header,
) (resp *http.Response, err error) {
return s.Request("GET", url, params, headers, nil)
}
func (s *Session) GetJSON(
url string,
params *url.Values,
headers *http.Header,
responseContainer interface{},
) (resp *http.Response, err error) {
return s.RequestJSON("GET", url, params, headers, nil, responseContainer)
}
func (s *Session) Head(
url string,
params *url.Values,
headers *http.Header,
) (resp *http.Response, err error) {
return s.Request("HEAD", url, params, headers, nil)
}
func (s *Session) Post(
url string,
params *url.Values,
headers *http.Header,
body *[]byte,
) (resp *http.Response, err error) {
if headers == nil {
headers = &http.Header{}
headers.Add("Content-Type", "application/x-www-form-urlencoded")
}
return s.Request("POST", url, params, headers, body)
}
func (s *Session) PostJSON(
url string,
params *url.Values,
headers *http.Header,
body interface{},
responseContainer interface{},
) (resp *http.Response, err error) {
return s.RequestJSON("POST", url, params, headers, body, responseContainer)
}
func (s *Session) Put(
url string,
params *url.Values,
headers *http.Header,
body *[]byte,
) (resp *http.Response, err error) {
if headers == nil {
headers = &http.Header{}
headers.Add("Content-Type", "application/x-www-form-urlencoded")
}
return s.Request("PUT", url, params, headers, body)
}