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