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