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