-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy patheoshttp.go
532 lines (446 loc) · 16.5 KB
/
eoshttp.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// Copyright 2018-2024 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
package eosgrpc
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/cs3org/reva/pkg/appctx"
"github.com/cs3org/reva/pkg/eosclient"
"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/logger"
)
// HTTPOptions to configure the Client.
type HTTPOptions struct {
// HTTP URL of the EOS MGM.
// Default is https://eos-example.org
BaseURL string
// Timeout in seconds for connecting to the service
ConnectTimeout int
// Timeout in seconds for sending a request to the service and getting a response
// Does not include redirections
RWTimeout int
// Timeout in seconds for performing an operation. Includes every redirection, retry, etc
OpTimeout int
// Max idle conns per Transport
MaxIdleConns int
// Max conns per transport per destination host
MaxConnsPerHost int
// Max idle conns per transport per destination host
MaxIdleConnsPerHost int
// TTL for an idle conn per transport
IdleConnTimeout int
// If the URL is https, then we need to configure this client
// with the usual TLS stuff
// Defaults are /etc/grid-security/hostcert.pem and /etc/grid-security/hostkey.pem
ClientCertFile string
ClientKeyFile string
// These will override the defaults, which are common system paths hardcoded
// in the go x509 implementation (why did they do that?!?!?)
// of course /etc/grid-security/certificates is NOT in those defaults!
ClientCADirs string
ClientCAFiles string
// Authkey is the key that authorizes this client to connect to the EOS HTTP service
Authkey string
}
// Init fills the basic fields.
func (opt *HTTPOptions) init() {
if opt.BaseURL == "" {
opt.BaseURL = "https://eos-example.org"
}
if opt.ConnectTimeout == 0 {
opt.ConnectTimeout = 30
}
if opt.RWTimeout == 0 {
opt.RWTimeout = 180
}
if opt.OpTimeout == 0 {
opt.OpTimeout = 360
}
if opt.MaxIdleConns == 0 {
opt.MaxIdleConns = 100
}
if opt.MaxConnsPerHost == 0 {
opt.MaxConnsPerHost = 64
}
if opt.MaxIdleConnsPerHost == 0 {
opt.MaxIdleConnsPerHost = 8
}
if opt.IdleConnTimeout == 0 {
opt.IdleConnTimeout = 30
}
if opt.ClientCertFile == "" {
opt.ClientCertFile = "/etc/grid-security/hostcert.pem"
}
if opt.ClientKeyFile == "" {
opt.ClientKeyFile = "/etc/grid-security/hostkey.pem"
}
if opt.ClientCAFiles != "" {
os.Setenv("SSL_CERT_FILE", opt.ClientCAFiles)
}
if opt.ClientCADirs != "" {
os.Setenv("SSL_CERT_DIR", opt.ClientCADirs)
} else {
os.Setenv("SSL_CERT_DIR", "/etc/grid-security/certificates")
}
}
// EOSHTTPClient performs HTTP-based tasks (e.g. upload, download)
// against a EOS management node (MGM)
// using the EOS XrdHTTP interface.
// In this module we wrap eos-related behaviour, e.g. headers or r/w retries.
type EOSHTTPClient struct {
opt *HTTPOptions
cl *http.Client
}
// NewEOSHTTPClient creates a new client with the given options.
func NewEOSHTTPClient(opt *HTTPOptions) (*EOSHTTPClient, error) {
log := logger.New().With().Int("pid", os.Getpid()).Logger()
log.Debug().Str("func", "New").Str("Creating new eoshttp client. opt: ", "'"+fmt.Sprintf("%#v", opt)+"' ").Msg("")
if opt == nil {
log.Debug().Str("opt is nil, error creating http client ", "").Msg("")
return nil, errtypes.InternalError("HTTPOptions is nil")
}
opt.init()
t := &http.Transport{
MaxIdleConns: opt.MaxIdleConns,
MaxConnsPerHost: opt.MaxConnsPerHost,
MaxIdleConnsPerHost: opt.MaxIdleConnsPerHost,
IdleConnTimeout: time.Duration(opt.IdleConnTimeout) * time.Second,
DisableCompression: true,
}
cl := &http.Client{
Transport: t,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
return &EOSHTTPClient{
opt: opt,
cl: cl,
}, nil
}
// Format a human readable line that describes a response.
func rspdesc(rsp *http.Response) string {
desc := "'" + fmt.Sprintf("%d", rsp.StatusCode) + "'" + ": '" + rsp.Status + "'"
buf := new(bytes.Buffer)
r := "<none>"
n, e := buf.ReadFrom(rsp.Body)
if e != nil {
r = "Error reading body: '" + e.Error() + "'"
} else if n > 0 {
r = buf.String()
}
desc += " - '" + r + "'"
return desc
}
func (c *EOSHTTPClient) doReq(req *http.Request, remoteuser string) (*http.Response, error) {
// Here we put the headers that are required by EOS >= 5
req.Header.Set("x-gateway-authorization", c.opt.Authkey)
req.Header.Set("x-forwarded-for", "dummy")
req.Header.Set("remote-user", remoteuser)
resp, err := c.cl.Do(req)
return resp, err
}
// If the error is not nil, take that.
// If there is an error coming from EOS, return a descriptive error.
func (c *EOSHTTPClient) getRespError(rsp *http.Response, err error) error {
if err != nil {
return err
}
if rsp.StatusCode == 0 {
return nil
}
switch rsp.StatusCode {
case 0, http.StatusOK, http.StatusCreated:
return nil
case http.StatusForbidden:
return errtypes.PermissionDenied(rspdesc(rsp))
case http.StatusNotFound:
return errtypes.NotFound(rspdesc(rsp))
case http.StatusConflict:
return errtypes.Conflict(rspdesc(rsp))
}
return errtypes.InternalError("Err from EOS: " + rspdesc(rsp))
}
// From the basepath and the file path... build an url.
func (c *EOSHTTPClient) buildFullURL(urlpath string, auth eosclient.Authorization) (string, error) {
// Prohibit malicious users from injecting a false uid/gid into the url
pos := strings.Index(urlpath, "?")
if pos >= 0 {
if strings.Index(urlpath, "eos.ruid") > pos || strings.Index(urlpath, "eos.rgid") > pos {
return "", errtypes.PermissionDenied("Illegal malicious url " + urlpath)
}
}
fullurl := strings.TrimRight(c.opt.BaseURL, "/")
fullurl += "/"
fullurl += strings.TrimLeft(urlpath, "/")
if pos < 0 {
fullurl += "?"
}
if auth.Token != "" {
fullurl += "authz=" + auth.Token
} else if auth.Role.UID != "" {
fullurl += fmt.Sprintf("eos.ruid=%s&eos.rgid=%s", auth.Role.UID, auth.Role.GID)
}
u, err := url.Parse(fullurl)
if err != nil {
return "", errtypes.PermissionDenied("Could not parse url " + urlpath)
}
final := strings.ReplaceAll(u.String(), "#", "%23")
return final, nil
}
// GETFile does an entire GET to download a full file. Returns a stream to read the content from.
func (c *EOSHTTPClient) GETFile(ctx context.Context, remoteuser string, auth eosclient.Authorization, urlpath string, stream io.WriteCloser) (io.ReadCloser, error) {
log := appctx.GetLogger(ctx)
log.Info().Str("func", "GETFile").Str("remoteuser", remoteuser).Str("uid,gid", auth.Role.UID+","+auth.Role.GID).Str("path", urlpath).Msg("")
// Now send the req and see what happens
finalurl, err := c.buildFullURL(urlpath, auth)
if err != nil {
log.Error().Str("func", "GETFile").Str("url", finalurl).Str("err", err.Error()).Msg("can't create request")
return nil, err
}
log.Debug().Str("func", "GETFile").Str("url", finalurl).Msg("")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, finalurl, nil)
if err != nil {
log.Error().Str("func", "GETFile").Str("url", finalurl).Str("err", err.Error()).Msg("can't create request")
return nil, err
}
// similar to eosbinary.go::Read()
req.Header.Set("app", "reva_eosclient::read")
ntries := 0
nredirs := 0
timebegin := time.Now().Unix()
for {
// Check for a max count of redirections or retries
// Check for a global timeout in any case
tdiff := time.Now().Unix() - timebegin
if tdiff > int64(c.opt.OpTimeout) {
log.Error().Str("func", "GETFile").Str("url", finalurl).Int64("timeout", tdiff).Int("ntries", ntries).Msg("")
return nil, errtypes.InternalError("Timeout with url" + finalurl)
}
// Execute the request. I don't like that there is no explicit timeout or buffer control on the input stream
log.Debug().Str("func", "GETFile").Str("finalurl", finalurl).Msg("sending req")
// c.doReq sets headers such as remoteuser and x-gateway-authorization
// we don't want those when using a token (i.e. ?authz=), so in this case
// we skip this and call the HTTP client directly
var resp *http.Response
if auth.Token != "" {
resp, err = c.cl.Do(req)
} else {
resp, err = c.doReq(req, remoteuser)
}
// Let's support redirections... and if we retry we have to retry at the same FST, avoid going back to the MGM
if resp != nil && (resp.StatusCode == http.StatusFound || resp.StatusCode == http.StatusTemporaryRedirect) {
// io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
loc, err := resp.Location()
if err != nil {
log.Error().Str("func", "GETFile").Str("url", finalurl).Str("err", err.Error()).Msg("can't get a new location for a redirection")
return nil, err
}
// Very special case for eos file versions
final := strings.ReplaceAll(loc.String(), "#", "%23")
req, err = http.NewRequestWithContext(ctx, http.MethodGet, final, nil)
if err != nil {
log.Error().Str("func", "GETFile").Str("url", loc.String()).Str("err", err.Error()).Msg("can't create redirected request")
return nil, err
}
req.Close = true
log.Debug().Str("func", "GETFile").Str("location", loc.String()).Msg("redirection")
nredirs++
resp = nil
err = nil
continue
}
// And get an error code (if error) that is worth propagating
e := c.getRespError(resp, err)
if e != nil {
resp.Body.Close()
if os.IsTimeout(e) {
ntries++
log.Warn().Str("func", "GETFile").Str("url", finalurl).Str("err", e.Error()).Int("try", ntries).Msg("recoverable network timeout")
continue
}
log.Error().Str("func", "GETFile").Str("url", finalurl).Str("err", e.Error()).Msg("")
return nil, e
}
log.Debug().Str("func", "GETFile").Str("url", finalurl).Str("resp:", fmt.Sprintf("%#v", resp)).Msg("")
if resp == nil {
return nil, errtypes.NotFound(fmt.Sprintf("url: %s", finalurl))
}
if stream != nil {
// Streaming versus localfile. If we have bene given a dest stream then copy the body into it
_, err = io.Copy(stream, resp.Body)
return nil, err
}
// If we have not been given a stream to write into then return our stream to read from
return resp.Body, nil
}
}
// PUTFile does an entire PUT to upload a full file, taking the data from a stream.
func (c *EOSHTTPClient) PUTFile(ctx context.Context, remoteuser string, auth eosclient.Authorization, urlpath string, stream io.ReadCloser, length int64, app string) error {
log := appctx.GetLogger(ctx)
log.Info().Str("func", "PUTFile").Str("remoteuser", remoteuser).Str("uid,gid", auth.Role.UID+","+auth.Role.GID).Str("path", urlpath).Int64("length", length).Str("app", app).Msg("")
// Now send the req and see what happens
finalurl, err := c.buildFullURL(urlpath, auth)
if err != nil {
log.Error().Str("func", "PUTFile").Str("url", finalurl).Str("err", err.Error()).Msg("can't create request")
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPut, finalurl, nil)
if err != nil {
log.Error().Str("func", "PUTFile").Str("url", finalurl).Str("err", err.Error()).Msg("can't create request")
return err
}
if app != "" {
req.Header.Set("app", app)
}
req.Close = true
ntries := 0
nredirs := 0
timebegin := time.Now().Unix()
for {
// Check for a max count of redirections or retries
// Check for a global timeout in any case
tdiff := time.Now().Unix() - timebegin
if tdiff > int64(c.opt.OpTimeout) {
log.Error().Str("func", "PUTFile").Str("url", finalurl).Int64("timeout", tdiff).Int("ntries", ntries).Msg("")
return errtypes.InternalError("Timeout with url" + finalurl)
}
// Execute the request. I don't like that there is no explicit timeout or buffer control on the input stream
log.Debug().Str("func", "PUTFile").Msg("sending req")
// c.doReq sets headers such as remoteuser and x-gateway-authorization
// we don't want those when using a token (i.e. ?authz=), so in this case
// we skip this and call the HTTP client directly
var resp *http.Response
if auth.Token != "" {
resp, err = c.cl.Do(req)
} else {
resp, err = c.doReq(req, remoteuser)
}
// Let's support redirections... and if we retry we retry at the same FST
if resp != nil && resp.StatusCode == 307 {
// io.Copy(ioutil.Discard, resp.Body)
loc, err := resp.Location()
if err != nil {
log.Error().Str("func", "PUTFile").Str("url", finalurl).Str("err", err.Error()).Msg("can't get a new location for a redirection")
return err
}
req, err = http.NewRequestWithContext(ctx, http.MethodPut, loc.String(), stream)
if err != nil {
log.Error().Str("func", "PUTFile").Str("url", loc.String()).Str("err", err.Error()).Msg("can't create redirected request")
return err
}
if length >= 0 {
log.Debug().Str("func", "PUTFile").Int64("Content-Length", length).Msg("setting header")
req.Header.Set("Content-Length", strconv.FormatInt(length, 10))
}
if err != nil {
log.Error().Str("func", "PUTFile").Str("url", loc.String()).Str("err", err.Error()).Msg("can't create redirected request")
return err
}
if length >= 0 {
log.Debug().Str("func", "PUTFile").Int64("Content-Length", length).Msg("setting header")
req.Header.Set("Content-Length", strconv.FormatInt(length, 10))
}
log.Debug().Str("func", "PUTFile").Str("location", loc.String()).Msg("redirection")
nredirs++
if (resp != nil) && (resp.Body != nil) {
resp.Body.Close()
}
resp = nil
err = nil
continue
}
// And get an error code (if error) that is worth propagating
e := c.getRespError(resp, err)
if e != nil {
if (resp != nil) && (resp.Body != nil) {
resp.Body.Close()
}
if os.IsTimeout(e) {
ntries++
log.Warn().Str("func", "PUTFile").Str("url", finalurl).Str("err", e.Error()).Int("try", ntries).Msg("recoverable network timeout")
continue
}
log.Error().Str("func", "PUTFile").Str("url", finalurl).Str("err", e.Error()).Msg("")
return e
}
log.Debug().Str("func", "PUTFile").Str("url", finalurl).Str("resp:", fmt.Sprintf("%#v", resp)).Msg("")
if resp == nil {
return errtypes.NotFound(fmt.Sprintf("url: %s", finalurl))
}
if (resp != nil) && (resp.Body != nil) {
resp.Body.Close()
}
return nil
}
}
// Head performs a HEAD req. Useful to check the server.
func (c *EOSHTTPClient) Head(ctx context.Context, remoteuser string, auth eosclient.Authorization, urlpath string) error {
log := appctx.GetLogger(ctx)
log.Info().Str("func", "Head").Str("remoteuser", remoteuser).Str("uid,gid", auth.Role.UID+","+auth.Role.GID).Str("path", urlpath).Msg("")
// Now send the req and see what happens
finalurl, err := c.buildFullURL(urlpath, auth)
if err != nil {
log.Error().Str("func", "Head").Str("url", finalurl).Str("err", err.Error()).Msg("can't create request")
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodHead, finalurl, nil)
if err != nil {
log.Error().Str("func", "Head").Str("remoteuser", remoteuser).Str("uid,gid", auth.Role.UID+","+auth.Role.GID).Str("url", finalurl).Str("err", err.Error()).Msg("can't create request")
return err
}
ntries := 0
timebegin := time.Now().Unix()
for {
tdiff := time.Now().Unix() - timebegin
if tdiff > int64(c.opt.OpTimeout) {
log.Error().Str("func", "Head").Str("url", finalurl).Int64("timeout", tdiff).Int("ntries", ntries).Msg("")
return errtypes.InternalError("Timeout with url" + finalurl)
}
// Execute the request. I don't like that there is no explicit timeout or buffer control on the input stream
resp, err := c.doReq(req, remoteuser)
// And get an error code (if error) that is worth propagating
e := c.getRespError(resp, err)
if e != nil {
if os.IsTimeout(e) {
ntries++
log.Warn().Str("func", "Head").Str("url", finalurl).Str("err", e.Error()).Int("try", ntries).Msg("recoverable network timeout")
continue
}
log.Error().Str("func", "Head").Str("url", finalurl).Str("err", e.Error()).Msg("")
return e
}
if resp != nil {
defer resp.Body.Close()
}
log.Debug().Str("func", "Head").Str("url", finalurl).Str("resp:", fmt.Sprintf("%#v", resp)).Msg("")
if resp == nil {
return errtypes.NotFound(fmt.Sprintf("url: %s", finalurl))
}
}
// return nil
}