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