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