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