18799: Force UTF-8 encoding for discovery2pydoc output
[arvados.git] / lib / controller / rpc / conn.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package rpc
6
7 import (
8         "bufio"
9         "bytes"
10         "context"
11         "crypto/tls"
12         "encoding/json"
13         "errors"
14         "fmt"
15         "io"
16         "io/ioutil"
17         "net"
18         "net/http"
19         "net/http/httputil"
20         "net/url"
21         "strconv"
22         "strings"
23         "time"
24
25         "git.arvados.org/arvados.git/sdk/go/arvados"
26         "git.arvados.org/arvados.git/sdk/go/auth"
27         "git.arvados.org/arvados.git/sdk/go/ctxlog"
28         "git.arvados.org/arvados.git/sdk/go/httpserver"
29 )
30
31 const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
32
33 type TokenProvider func(context.Context) ([]string, error)
34
35 func PassthroughTokenProvider(ctx context.Context) ([]string, error) {
36         incoming, ok := auth.FromContext(ctx)
37         if !ok {
38                 return nil, errors.New("no token provided")
39         }
40         return incoming.Tokens, nil
41 }
42
43 type Conn struct {
44         SendHeader         http.Header
45         RedactHostInErrors bool
46
47         clusterID     string
48         httpClient    http.Client
49         baseURL       url.URL
50         tokenProvider TokenProvider
51 }
52
53 func NewConn(clusterID string, url *url.URL, insecure bool, tp TokenProvider) *Conn {
54         transport := http.DefaultTransport
55         if insecure {
56                 // It's not safe to copy *http.DefaultTransport
57                 // because it has a mutex (which might be locked)
58                 // protecting a private map (which might not be nil).
59                 // So we build our own, using the Go 1.12 default
60                 // values, ignoring any changes the application has
61                 // made to http.DefaultTransport.
62                 transport = &http.Transport{
63                         DialContext: (&net.Dialer{
64                                 Timeout:   30 * time.Second,
65                                 KeepAlive: 30 * time.Second,
66                                 DualStack: true,
67                         }).DialContext,
68                         MaxIdleConns:          100,
69                         IdleConnTimeout:       90 * time.Second,
70                         TLSHandshakeTimeout:   10 * time.Second,
71                         ExpectContinueTimeout: 1 * time.Second,
72                         TLSClientConfig:       &tls.Config{InsecureSkipVerify: true},
73                 }
74         }
75         return &Conn{
76                 clusterID: clusterID,
77                 httpClient: http.Client{
78                         CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse },
79                         Transport:     transport,
80                 },
81                 baseURL:       *url,
82                 tokenProvider: tp,
83         }
84 }
85
86 func (conn *Conn) requestAndDecode(ctx context.Context, dst interface{}, ep arvados.APIEndpoint, body io.Reader, opts interface{}) error {
87         aClient := arvados.Client{
88                 Client:     &conn.httpClient,
89                 Scheme:     conn.baseURL.Scheme,
90                 APIHost:    conn.baseURL.Host,
91                 SendHeader: conn.SendHeader,
92                 // Disable auto-retry
93                 Timeout: 0,
94         }
95         tokens, err := conn.tokenProvider(ctx)
96         if err != nil {
97                 return err
98         } else if len(tokens) > 0 {
99                 ctx = arvados.ContextWithAuthorization(ctx, "Bearer "+tokens[0])
100         } else {
101                 // Use a non-empty auth string to ensure we override
102                 // any default token set on aClient -- and to avoid
103                 // having the remote prompt us to send a token by
104                 // responding 401.
105                 ctx = arvados.ContextWithAuthorization(ctx, "Bearer -")
106         }
107
108         // Encode opts to JSON and decode from there to a
109         // map[string]interface{}, so we can munge the query params
110         // using the JSON key names specified by opts' struct tags.
111         j, err := json.Marshal(opts)
112         if err != nil {
113                 return fmt.Errorf("%T: requestAndDecode: Marshal opts: %s", conn, err)
114         }
115         var params map[string]interface{}
116         dec := json.NewDecoder(bytes.NewBuffer(j))
117         dec.UseNumber()
118         err = dec.Decode(&params)
119         if err != nil {
120                 return fmt.Errorf("%T: requestAndDecode: Decode opts: %s", conn, err)
121         }
122         if attrs, ok := params["attrs"]; ok && ep.AttrsKey != "" {
123                 params[ep.AttrsKey] = attrs
124                 delete(params, "attrs")
125         }
126         if limitStr, ok := params["limit"]; ok {
127                 if limit, err := strconv.ParseInt(string(limitStr.(json.Number)), 10, 64); err == nil && limit < 0 {
128                         // Negative limit means "not specified" here, but some
129                         // servers/versions do not accept that, so we need to
130                         // remove it entirely.
131                         delete(params, "limit")
132                 }
133         }
134
135         if authinfo, ok := params["auth_info"]; ok {
136                 if tmp, ok2 := authinfo.(map[string]interface{}); ok2 {
137                         for k, v := range tmp {
138                                 if strings.HasSuffix(k, "_at") {
139                                         // Change zero times values to nil
140                                         if v, ok3 := v.(string); ok3 && (strings.HasPrefix(v, "0001-01-01T00:00:00") || v == "") {
141                                                 tmp[k] = nil
142                                         }
143                                 }
144                         }
145                 }
146         }
147
148         if len(tokens) > 1 {
149                 params["reader_tokens"] = tokens[1:]
150         }
151         path := ep.Path
152         if strings.Contains(ep.Path, "/{uuid}") {
153                 uuid, _ := params["uuid"].(string)
154                 path = strings.Replace(path, "/{uuid}", "/"+uuid, 1)
155                 delete(params, "uuid")
156         }
157         err = aClient.RequestAndDecodeContext(ctx, dst, ep.Method, path, body, params)
158         if err != nil && conn.RedactHostInErrors {
159                 redacted := strings.Replace(err.Error(), strings.TrimSuffix(conn.baseURL.String(), "/"), "//railsapi.internal", -1)
160                 if strings.HasPrefix(redacted, "request failed: ") {
161                         redacted = strings.Replace(redacted, "request failed: ", "", -1)
162                 }
163                 if redacted != err.Error() {
164                         if err, ok := err.(httpStatusError); ok {
165                                 return wrapHTTPStatusError(err, redacted)
166                         } else {
167                                 return errors.New(redacted)
168                         }
169                 }
170         }
171         return err
172 }
173
174 func (conn *Conn) BaseURL() url.URL {
175         return conn.baseURL
176 }
177
178 func (conn *Conn) ConfigGet(ctx context.Context) (json.RawMessage, error) {
179         ep := arvados.EndpointConfigGet
180         var resp json.RawMessage
181         err := conn.requestAndDecode(ctx, &resp, ep, nil, nil)
182         return resp, err
183 }
184
185 func (conn *Conn) VocabularyGet(ctx context.Context) (arvados.Vocabulary, error) {
186         ep := arvados.EndpointVocabularyGet
187         var resp arvados.Vocabulary
188         err := conn.requestAndDecode(ctx, &resp, ep, nil, nil)
189         return resp, err
190 }
191
192 func (conn *Conn) Login(ctx context.Context, options arvados.LoginOptions) (arvados.LoginResponse, error) {
193         ep := arvados.EndpointLogin
194         var resp arvados.LoginResponse
195         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
196         resp.RedirectLocation = conn.relativeToBaseURL(resp.RedirectLocation)
197         return resp, err
198 }
199
200 func (conn *Conn) Logout(ctx context.Context, options arvados.LogoutOptions) (arvados.LogoutResponse, error) {
201         ep := arvados.EndpointLogout
202         var resp arvados.LogoutResponse
203         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
204         resp.RedirectLocation = conn.relativeToBaseURL(resp.RedirectLocation)
205         return resp, err
206 }
207
208 // If the given location is a valid URL and its origin is the same as
209 // conn.baseURL, return it as a relative URL. Otherwise, return it
210 // unmodified.
211 func (conn *Conn) relativeToBaseURL(location string) string {
212         u, err := url.Parse(location)
213         if err == nil && u.Scheme == conn.baseURL.Scheme && strings.ToLower(u.Host) == strings.ToLower(conn.baseURL.Host) {
214                 u.Opaque = ""
215                 u.Scheme = ""
216                 u.User = nil
217                 u.Host = ""
218                 return u.String()
219         }
220         return location
221 }
222
223 func (conn *Conn) CollectionCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Collection, error) {
224         ep := arvados.EndpointCollectionCreate
225         var resp arvados.Collection
226         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
227         return resp, err
228 }
229
230 func (conn *Conn) CollectionUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Collection, error) {
231         ep := arvados.EndpointCollectionUpdate
232         var resp arvados.Collection
233         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
234         return resp, err
235 }
236
237 func (conn *Conn) CollectionGet(ctx context.Context, options arvados.GetOptions) (arvados.Collection, error) {
238         ep := arvados.EndpointCollectionGet
239         var resp arvados.Collection
240         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
241         return resp, err
242 }
243
244 func (conn *Conn) CollectionList(ctx context.Context, options arvados.ListOptions) (arvados.CollectionList, error) {
245         ep := arvados.EndpointCollectionList
246         var resp arvados.CollectionList
247         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
248         return resp, err
249 }
250
251 func (conn *Conn) CollectionProvenance(ctx context.Context, options arvados.GetOptions) (map[string]interface{}, error) {
252         ep := arvados.EndpointCollectionProvenance
253         var resp map[string]interface{}
254         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
255         return resp, err
256 }
257
258 func (conn *Conn) CollectionUsedBy(ctx context.Context, options arvados.GetOptions) (map[string]interface{}, error) {
259         ep := arvados.EndpointCollectionUsedBy
260         var resp map[string]interface{}
261         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
262         return resp, err
263 }
264
265 func (conn *Conn) CollectionDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Collection, error) {
266         ep := arvados.EndpointCollectionDelete
267         var resp arvados.Collection
268         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
269         return resp, err
270 }
271
272 func (conn *Conn) CollectionTrash(ctx context.Context, options arvados.DeleteOptions) (arvados.Collection, error) {
273         ep := arvados.EndpointCollectionTrash
274         var resp arvados.Collection
275         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
276         return resp, err
277 }
278
279 func (conn *Conn) CollectionUntrash(ctx context.Context, options arvados.UntrashOptions) (arvados.Collection, error) {
280         ep := arvados.EndpointCollectionUntrash
281         var resp arvados.Collection
282         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
283         return resp, err
284 }
285
286 func (conn *Conn) ContainerCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Container, error) {
287         ep := arvados.EndpointContainerCreate
288         var resp arvados.Container
289         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
290         return resp, err
291 }
292
293 func (conn *Conn) ContainerUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Container, error) {
294         ep := arvados.EndpointContainerUpdate
295         var resp arvados.Container
296         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
297         return resp, err
298 }
299
300 func (conn *Conn) ContainerPriorityUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Container, error) {
301         ep := arvados.EndpointContainerPriorityUpdate
302         var resp arvados.Container
303         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
304         return resp, err
305 }
306
307 func (conn *Conn) ContainerGet(ctx context.Context, options arvados.GetOptions) (arvados.Container, error) {
308         ep := arvados.EndpointContainerGet
309         var resp arvados.Container
310         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
311         return resp, err
312 }
313
314 func (conn *Conn) ContainerList(ctx context.Context, options arvados.ListOptions) (arvados.ContainerList, error) {
315         ep := arvados.EndpointContainerList
316         var resp arvados.ContainerList
317         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
318         return resp, err
319 }
320
321 func (conn *Conn) ContainerDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Container, error) {
322         ep := arvados.EndpointContainerDelete
323         var resp arvados.Container
324         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
325         return resp, err
326 }
327
328 func (conn *Conn) ContainerLock(ctx context.Context, options arvados.GetOptions) (arvados.Container, error) {
329         ep := arvados.EndpointContainerLock
330         var resp arvados.Container
331         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
332         return resp, err
333 }
334
335 func (conn *Conn) ContainerUnlock(ctx context.Context, options arvados.GetOptions) (arvados.Container, error) {
336         ep := arvados.EndpointContainerUnlock
337         var resp arvados.Container
338         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
339         return resp, err
340 }
341
342 func (conn *Conn) ContainerLog(ctx context.Context, options arvados.ContainerLogOptions) (resp http.Handler, err error) {
343         proxy := &httputil.ReverseProxy{
344                 Transport: conn.httpClient.Transport,
345                 Director: func(r *http.Request) {
346                         u := conn.baseURL
347                         u.Path = r.URL.Path
348                         u.RawQuery = fmt.Sprintf("no_forward=%v", options.NoForward)
349                         r.URL = &u
350                 },
351         }
352         return proxy, nil
353 }
354
355 // ContainerSSH returns a connection to the out-of-band SSH server for
356 // a running container. If the returned error is nil, the caller is
357 // responsible for closing sshconn.Conn.
358 func (conn *Conn) ContainerSSH(ctx context.Context, options arvados.ContainerSSHOptions) (sshconn arvados.ConnectionResponse, err error) {
359         u, err := conn.baseURL.Parse("/" + strings.Replace(arvados.EndpointContainerSSHCompat.Path, "{uuid}", options.UUID, -1))
360         if err != nil {
361                 err = fmt.Errorf("url.Parse: %w", err)
362                 return
363         }
364         return conn.socket(ctx, u, "ssh", url.Values{
365                 "detach_keys":    {options.DetachKeys},
366                 "login_username": {options.LoginUsername},
367                 "no_forward":     {fmt.Sprintf("%v", options.NoForward)},
368         })
369 }
370
371 // ContainerGatewayTunnel returns a connection to a yamux session on
372 // the controller. The caller should connect the returned resp.Conn to
373 // a client-side yamux session.
374 func (conn *Conn) ContainerGatewayTunnel(ctx context.Context, options arvados.ContainerGatewayTunnelOptions) (tunnelconn arvados.ConnectionResponse, err error) {
375         u, err := conn.baseURL.Parse("/" + strings.Replace(arvados.EndpointContainerGatewayTunnelCompat.Path, "{uuid}", options.UUID, -1))
376         if err != nil {
377                 err = fmt.Errorf("url.Parse: %w", err)
378                 return
379         }
380         return conn.socket(ctx, u, "tunnel", url.Values{
381                 "auth_secret": {options.AuthSecret},
382         })
383 }
384
385 // socket sets up a socket using the specified API endpoint and
386 // upgrade header.
387 func (conn *Conn) socket(ctx context.Context, u *url.URL, upgradeHeader string, postform url.Values) (connresp arvados.ConnectionResponse, err error) {
388         addr := conn.baseURL.Host
389         if strings.Index(addr, ":") < 1 || (strings.Contains(addr, "::") && addr[0] != '[') {
390                 // hostname or ::1 or 1::1
391                 addr = net.JoinHostPort(addr, "https")
392         }
393         insecure := false
394         if tlsconf := conn.httpClient.Transport.(*http.Transport).TLSClientConfig; tlsconf != nil && tlsconf.InsecureSkipVerify {
395                 insecure = true
396         }
397         netconn, err := tls.Dial("tcp", addr, &tls.Config{InsecureSkipVerify: insecure})
398         if err != nil {
399                 return connresp, fmt.Errorf("tls.Dial: %w", err)
400         }
401         defer func() {
402                 if err != nil {
403                         netconn.Close()
404                 }
405         }()
406         bufr := bufio.NewReader(netconn)
407         bufw := bufio.NewWriter(netconn)
408
409         tokens, err := conn.tokenProvider(ctx)
410         if err != nil {
411                 return connresp, err
412         } else if len(tokens) < 1 {
413                 return connresp, httpserver.ErrorWithStatus(errors.New("unauthorized"), http.StatusUnauthorized)
414         }
415         postdata := postform.Encode()
416         bufw.WriteString("POST " + u.String() + " HTTP/1.1\r\n")
417         bufw.WriteString("Authorization: Bearer " + tokens[0] + "\r\n")
418         bufw.WriteString("Host: " + u.Host + "\r\n")
419         bufw.WriteString("Upgrade: " + upgradeHeader + "\r\n")
420         bufw.WriteString("Content-Type: application/x-www-form-urlencoded\r\n")
421         fmt.Fprintf(bufw, "Content-Length: %d\r\n", len(postdata))
422         bufw.WriteString("\r\n")
423         bufw.WriteString(postdata)
424         bufw.Flush()
425         resp, err := http.ReadResponse(bufr, &http.Request{Method: "POST"})
426         if err != nil {
427                 return connresp, fmt.Errorf("http.ReadResponse: %w", err)
428         }
429         defer resp.Body.Close()
430         if resp.StatusCode != http.StatusSwitchingProtocols {
431                 ctxlog.FromContext(ctx).Infof("rpc.Conn.socket: server %s did not switch protocols, got status %s", u.String(), resp.Status)
432                 body, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 10000))
433                 var message string
434                 var errDoc httpserver.ErrorResponse
435                 if err := json.Unmarshal(body, &errDoc); err == nil {
436                         message = strings.Join(errDoc.Errors, "; ")
437                 } else {
438                         message = fmt.Sprintf("%q", body)
439                 }
440                 return connresp, fmt.Errorf("server did not provide a tunnel: %s: %s", resp.Status, message)
441         }
442         if strings.ToLower(resp.Header.Get("Upgrade")) != upgradeHeader ||
443                 strings.ToLower(resp.Header.Get("Connection")) != "upgrade" {
444                 return connresp, fmt.Errorf("bad response from server: Upgrade %q Connection %q", resp.Header.Get("Upgrade"), resp.Header.Get("Connection"))
445         }
446         connresp.Conn = netconn
447         connresp.Bufrw = &bufio.ReadWriter{Reader: bufr, Writer: bufw}
448         connresp.Header = resp.Header
449         return connresp, nil
450 }
451
452 func (conn *Conn) ContainerRequestCreate(ctx context.Context, options arvados.CreateOptions) (arvados.ContainerRequest, error) {
453         ep := arvados.EndpointContainerRequestCreate
454         var resp arvados.ContainerRequest
455         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
456         return resp, err
457 }
458
459 func (conn *Conn) ContainerRequestUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.ContainerRequest, error) {
460         ep := arvados.EndpointContainerRequestUpdate
461         var resp arvados.ContainerRequest
462         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
463         return resp, err
464 }
465
466 func (conn *Conn) ContainerRequestGet(ctx context.Context, options arvados.GetOptions) (arvados.ContainerRequest, error) {
467         ep := arvados.EndpointContainerRequestGet
468         var resp arvados.ContainerRequest
469         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
470         return resp, err
471 }
472
473 func (conn *Conn) ContainerRequestList(ctx context.Context, options arvados.ListOptions) (arvados.ContainerRequestList, error) {
474         ep := arvados.EndpointContainerRequestList
475         var resp arvados.ContainerRequestList
476         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
477         return resp, err
478 }
479
480 func (conn *Conn) ContainerRequestDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.ContainerRequest, error) {
481         ep := arvados.EndpointContainerRequestDelete
482         var resp arvados.ContainerRequest
483         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
484         return resp, err
485 }
486
487 func (conn *Conn) GroupCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Group, error) {
488         ep := arvados.EndpointGroupCreate
489         var resp arvados.Group
490         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
491         return resp, err
492 }
493
494 func (conn *Conn) GroupUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Group, error) {
495         ep := arvados.EndpointGroupUpdate
496         var resp arvados.Group
497         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
498         return resp, err
499 }
500
501 func (conn *Conn) GroupGet(ctx context.Context, options arvados.GetOptions) (arvados.Group, error) {
502         ep := arvados.EndpointGroupGet
503         var resp arvados.Group
504         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
505         return resp, err
506 }
507
508 func (conn *Conn) GroupList(ctx context.Context, options arvados.ListOptions) (arvados.GroupList, error) {
509         ep := arvados.EndpointGroupList
510         var resp arvados.GroupList
511         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
512         return resp, err
513 }
514
515 func (conn *Conn) GroupContents(ctx context.Context, options arvados.GroupContentsOptions) (arvados.ObjectList, error) {
516         ep := arvados.EndpointGroupContents
517         var resp arvados.ObjectList
518         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
519         return resp, err
520 }
521
522 func (conn *Conn) GroupShared(ctx context.Context, options arvados.ListOptions) (arvados.GroupList, error) {
523         ep := arvados.EndpointGroupShared
524         var resp arvados.GroupList
525         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
526         return resp, err
527 }
528
529 func (conn *Conn) GroupDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Group, error) {
530         ep := arvados.EndpointGroupDelete
531         var resp arvados.Group
532         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
533         return resp, err
534 }
535
536 func (conn *Conn) GroupTrash(ctx context.Context, options arvados.DeleteOptions) (arvados.Group, error) {
537         ep := arvados.EndpointGroupTrash
538         var resp arvados.Group
539         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
540         return resp, err
541 }
542
543 func (conn *Conn) GroupUntrash(ctx context.Context, options arvados.UntrashOptions) (arvados.Group, error) {
544         ep := arvados.EndpointGroupUntrash
545         var resp arvados.Group
546         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
547         return resp, err
548 }
549
550 func (conn *Conn) LinkCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Link, error) {
551         ep := arvados.EndpointLinkCreate
552         var resp arvados.Link
553         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
554         return resp, err
555 }
556
557 func (conn *Conn) LinkUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Link, error) {
558         ep := arvados.EndpointLinkUpdate
559         var resp arvados.Link
560         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
561         return resp, err
562 }
563
564 func (conn *Conn) LinkGet(ctx context.Context, options arvados.GetOptions) (arvados.Link, error) {
565         ep := arvados.EndpointLinkGet
566         var resp arvados.Link
567         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
568         return resp, err
569 }
570
571 func (conn *Conn) LinkList(ctx context.Context, options arvados.ListOptions) (arvados.LinkList, error) {
572         ep := arvados.EndpointLinkList
573         var resp arvados.LinkList
574         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
575         return resp, err
576 }
577
578 func (conn *Conn) LinkDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Link, error) {
579         ep := arvados.EndpointLinkDelete
580         var resp arvados.Link
581         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
582         return resp, err
583 }
584
585 func (conn *Conn) LogCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Log, error) {
586         ep := arvados.EndpointLogCreate
587         var resp arvados.Log
588         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
589         return resp, err
590 }
591
592 func (conn *Conn) LogUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Log, error) {
593         ep := arvados.EndpointLogUpdate
594         var resp arvados.Log
595         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
596         return resp, err
597 }
598
599 func (conn *Conn) LogGet(ctx context.Context, options arvados.GetOptions) (arvados.Log, error) {
600         ep := arvados.EndpointLogGet
601         var resp arvados.Log
602         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
603         return resp, err
604 }
605
606 func (conn *Conn) LogList(ctx context.Context, options arvados.ListOptions) (arvados.LogList, error) {
607         ep := arvados.EndpointLogList
608         var resp arvados.LogList
609         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
610         return resp, err
611 }
612
613 func (conn *Conn) LogDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Log, error) {
614         ep := arvados.EndpointLogDelete
615         var resp arvados.Log
616         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
617         return resp, err
618 }
619
620 func (conn *Conn) SpecimenCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Specimen, error) {
621         ep := arvados.EndpointSpecimenCreate
622         var resp arvados.Specimen
623         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
624         return resp, err
625 }
626
627 func (conn *Conn) SpecimenUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Specimen, error) {
628         ep := arvados.EndpointSpecimenUpdate
629         var resp arvados.Specimen
630         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
631         return resp, err
632 }
633
634 func (conn *Conn) SpecimenGet(ctx context.Context, options arvados.GetOptions) (arvados.Specimen, error) {
635         ep := arvados.EndpointSpecimenGet
636         var resp arvados.Specimen
637         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
638         return resp, err
639 }
640
641 func (conn *Conn) SpecimenList(ctx context.Context, options arvados.ListOptions) (arvados.SpecimenList, error) {
642         ep := arvados.EndpointSpecimenList
643         var resp arvados.SpecimenList
644         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
645         return resp, err
646 }
647
648 func (conn *Conn) SpecimenDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Specimen, error) {
649         ep := arvados.EndpointSpecimenDelete
650         var resp arvados.Specimen
651         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
652         return resp, err
653 }
654
655 func (conn *Conn) SysTrashSweep(ctx context.Context, options struct{}) (struct{}, error) {
656         ep := arvados.EndpointSysTrashSweep
657         var resp struct{}
658         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
659         return resp, err
660 }
661
662 func (conn *Conn) UserCreate(ctx context.Context, options arvados.CreateOptions) (arvados.User, error) {
663         ep := arvados.EndpointUserCreate
664         var resp arvados.User
665         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
666         return resp, err
667 }
668 func (conn *Conn) UserUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.User, error) {
669         ep := arvados.EndpointUserUpdate
670         var resp arvados.User
671         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
672         return resp, err
673 }
674 func (conn *Conn) UserMerge(ctx context.Context, options arvados.UserMergeOptions) (arvados.User, error) {
675         ep := arvados.EndpointUserMerge
676         var resp arvados.User
677         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
678         return resp, err
679 }
680 func (conn *Conn) UserActivate(ctx context.Context, options arvados.UserActivateOptions) (arvados.User, error) {
681         ep := arvados.EndpointUserActivate
682         var resp arvados.User
683         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
684         return resp, err
685 }
686 func (conn *Conn) UserSetup(ctx context.Context, options arvados.UserSetupOptions) (map[string]interface{}, error) {
687         ep := arvados.EndpointUserSetup
688         var resp map[string]interface{}
689         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
690         return resp, err
691 }
692 func (conn *Conn) UserUnsetup(ctx context.Context, options arvados.GetOptions) (arvados.User, error) {
693         ep := arvados.EndpointUserUnsetup
694         var resp arvados.User
695         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
696         return resp, err
697 }
698 func (conn *Conn) UserGet(ctx context.Context, options arvados.GetOptions) (arvados.User, error) {
699         ep := arvados.EndpointUserGet
700         var resp arvados.User
701         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
702         return resp, err
703 }
704 func (conn *Conn) UserGetCurrent(ctx context.Context, options arvados.GetOptions) (arvados.User, error) {
705         ep := arvados.EndpointUserGetCurrent
706         var resp arvados.User
707         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
708         return resp, err
709 }
710 func (conn *Conn) UserGetSystem(ctx context.Context, options arvados.GetOptions) (arvados.User, error) {
711         ep := arvados.EndpointUserGetSystem
712         var resp arvados.User
713         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
714         return resp, err
715 }
716 func (conn *Conn) UserList(ctx context.Context, options arvados.ListOptions) (arvados.UserList, error) {
717         ep := arvados.EndpointUserList
718         var resp arvados.UserList
719         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
720         return resp, err
721 }
722 func (conn *Conn) UserDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.User, error) {
723         ep := arvados.EndpointUserDelete
724         var resp arvados.User
725         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
726         return resp, err
727 }
728
729 func (conn *Conn) APIClientAuthorizationCurrent(ctx context.Context, options arvados.GetOptions) (arvados.APIClientAuthorization, error) {
730         ep := arvados.EndpointAPIClientAuthorizationCurrent
731         var resp arvados.APIClientAuthorization
732         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
733         return resp, err
734 }
735 func (conn *Conn) APIClientAuthorizationCreate(ctx context.Context, options arvados.CreateOptions) (arvados.APIClientAuthorization, error) {
736         ep := arvados.EndpointAPIClientAuthorizationCreate
737         var resp arvados.APIClientAuthorization
738         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
739         return resp, err
740 }
741 func (conn *Conn) APIClientAuthorizationUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.APIClientAuthorization, error) {
742         ep := arvados.EndpointAPIClientAuthorizationUpdate
743         var resp arvados.APIClientAuthorization
744         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
745         return resp, err
746 }
747 func (conn *Conn) APIClientAuthorizationDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.APIClientAuthorization, error) {
748         ep := arvados.EndpointAPIClientAuthorizationDelete
749         var resp arvados.APIClientAuthorization
750         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
751         return resp, err
752 }
753 func (conn *Conn) APIClientAuthorizationList(ctx context.Context, options arvados.ListOptions) (arvados.APIClientAuthorizationList, error) {
754         ep := arvados.EndpointAPIClientAuthorizationList
755         var resp arvados.APIClientAuthorizationList
756         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
757         return resp, err
758 }
759 func (conn *Conn) APIClientAuthorizationGet(ctx context.Context, options arvados.GetOptions) (arvados.APIClientAuthorization, error) {
760         ep := arvados.EndpointAPIClientAuthorizationGet
761         var resp arvados.APIClientAuthorization
762         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
763         return resp, err
764 }
765
766 type UserSessionAuthInfo struct {
767         UserUUID        string    `json:"user_uuid"`
768         Email           string    `json:"email"`
769         AlternateEmails []string  `json:"alternate_emails"`
770         FirstName       string    `json:"first_name"`
771         LastName        string    `json:"last_name"`
772         Username        string    `json:"username"`
773         ExpiresAt       time.Time `json:"expires_at"`
774 }
775
776 type UserSessionCreateOptions struct {
777         AuthInfo UserSessionAuthInfo `json:"auth_info"`
778         ReturnTo string              `json:"return_to"`
779 }
780
781 func (conn *Conn) UserSessionCreate(ctx context.Context, options UserSessionCreateOptions) (arvados.LoginResponse, error) {
782         ep := arvados.APIEndpoint{Method: "POST", Path: "auth/controller/callback"}
783         var resp arvados.LoginResponse
784         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
785         return resp, err
786 }
787
788 func (conn *Conn) UserBatchUpdate(ctx context.Context, options arvados.UserBatchUpdateOptions) (arvados.UserList, error) {
789         ep := arvados.EndpointUserBatchUpdate
790         var resp arvados.UserList
791         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
792         return resp, err
793 }
794
795 func (conn *Conn) UserAuthenticate(ctx context.Context, options arvados.UserAuthenticateOptions) (arvados.APIClientAuthorization, error) {
796         ep := arvados.EndpointUserAuthenticate
797         var resp arvados.APIClientAuthorization
798         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
799         return resp, err
800 }
801
802 // httpStatusError is an error with an HTTP status code that can be
803 // propagated by lib/controller/router, etc.
804 type httpStatusError interface {
805         error
806         HTTPStatus() int
807 }
808
809 // wrappedHTTPStatusError is used to augment/replace an error message
810 // while preserving the HTTP status code indicated by the original
811 // error.
812 type wrappedHTTPStatusError struct {
813         httpStatusError
814         message string
815 }
816
817 func wrapHTTPStatusError(err httpStatusError, message string) httpStatusError {
818         return wrappedHTTPStatusError{err, message}
819 }
820
821 func (err wrappedHTTPStatusError) Error() string {
822         return err.message
823 }