]> git.arvados.org - arvados.git/blob - lib/controller/rpc/conn.go
17209: Forward http requests to container indicated in vhost.
[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) ComputedPermissionList(ctx context.Context, options arvados.ListOptions) (arvados.ComputedPermissionList, error) {
345         ep := arvados.EndpointComputedPermissionList
346         var resp arvados.ComputedPermissionList
347         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
348         return resp, err
349 }
350
351 func (conn *Conn) ContainerCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Container, error) {
352         ep := arvados.EndpointContainerCreate
353         var resp arvados.Container
354         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
355         return resp, err
356 }
357
358 func (conn *Conn) ContainerUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Container, error) {
359         ep := arvados.EndpointContainerUpdate
360         var resp arvados.Container
361         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
362         return resp, err
363 }
364
365 func (conn *Conn) ContainerPriorityUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Container, error) {
366         ep := arvados.EndpointContainerPriorityUpdate
367         var resp arvados.Container
368         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
369         return resp, err
370 }
371
372 func (conn *Conn) ContainerGet(ctx context.Context, options arvados.GetOptions) (arvados.Container, error) {
373         ep := arvados.EndpointContainerGet
374         var resp arvados.Container
375         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
376         return resp, err
377 }
378
379 func (conn *Conn) ContainerList(ctx context.Context, options arvados.ListOptions) (arvados.ContainerList, error) {
380         ep := arvados.EndpointContainerList
381         var resp arvados.ContainerList
382         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
383         return resp, err
384 }
385
386 func (conn *Conn) ContainerDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Container, error) {
387         ep := arvados.EndpointContainerDelete
388         var resp arvados.Container
389         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
390         return resp, err
391 }
392
393 func (conn *Conn) ContainerLock(ctx context.Context, options arvados.GetOptions) (arvados.Container, error) {
394         ep := arvados.EndpointContainerLock
395         var resp arvados.Container
396         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
397         return resp, err
398 }
399
400 func (conn *Conn) ContainerUnlock(ctx context.Context, options arvados.GetOptions) (arvados.Container, error) {
401         ep := arvados.EndpointContainerUnlock
402         var resp arvados.Container
403         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
404         return resp, err
405 }
406
407 func (conn *Conn) ContainerHTTPProxy(ctx context.Context, options arvados.ContainerHTTPProxyOptions) (http.Handler, error) {
408         return nil, errors.New("not implemented")
409 }
410
411 // ContainerSSH returns a connection to the out-of-band SSH server for
412 // a running container. If the returned error is nil, the caller is
413 // responsible for closing sshconn.Conn.
414 func (conn *Conn) ContainerSSH(ctx context.Context, options arvados.ContainerSSHOptions) (sshconn arvados.ConnectionResponse, err error) {
415         u, err := conn.baseURL.Parse("/" + strings.Replace(arvados.EndpointContainerSSHCompat.Path, "{uuid}", options.UUID, -1))
416         if err != nil {
417                 err = fmt.Errorf("url.Parse: %w", err)
418                 return
419         }
420         return conn.socket(ctx, u, "ssh", url.Values{
421                 "detach_keys":    {options.DetachKeys},
422                 "login_username": {options.LoginUsername},
423                 "no_forward":     {fmt.Sprintf("%v", options.NoForward)},
424         })
425 }
426
427 // ContainerGatewayTunnel returns a connection to a yamux session on
428 // the controller. The caller should connect the returned resp.Conn to
429 // a client-side yamux session.
430 func (conn *Conn) ContainerGatewayTunnel(ctx context.Context, options arvados.ContainerGatewayTunnelOptions) (tunnelconn arvados.ConnectionResponse, err error) {
431         u, err := conn.baseURL.Parse("/" + strings.Replace(arvados.EndpointContainerGatewayTunnelCompat.Path, "{uuid}", options.UUID, -1))
432         if err != nil {
433                 err = fmt.Errorf("url.Parse: %w", err)
434                 return
435         }
436         return conn.socket(ctx, u, "tunnel", url.Values{
437                 "auth_secret": {options.AuthSecret},
438         })
439 }
440
441 // socket sets up a socket using the specified API endpoint and
442 // upgrade header.
443 func (conn *Conn) socket(ctx context.Context, u *url.URL, upgradeHeader string, postform url.Values) (connresp arvados.ConnectionResponse, err error) {
444         addr := conn.baseURL.Host
445         if strings.Index(addr, ":") < 1 || (strings.Contains(addr, "::") && addr[0] != '[') {
446                 // hostname or ::1 or 1::1
447                 addr = net.JoinHostPort(addr, "https")
448         }
449         insecure := false
450         if tlsconf := conn.httpClient.Transport.(*http.Transport).TLSClientConfig; tlsconf != nil && tlsconf.InsecureSkipVerify {
451                 insecure = true
452         }
453         netconn, err := tls.Dial("tcp", addr, &tls.Config{InsecureSkipVerify: insecure})
454         if err != nil {
455                 return connresp, fmt.Errorf("tls.Dial: %w", err)
456         }
457         defer func() {
458                 if err != nil {
459                         netconn.Close()
460                 }
461         }()
462         bufr := bufio.NewReader(netconn)
463         bufw := bufio.NewWriter(netconn)
464
465         tokens, err := conn.tokenProvider(ctx)
466         if err != nil {
467                 return connresp, err
468         } else if len(tokens) < 1 {
469                 return connresp, httpserver.ErrorWithStatus(errors.New("unauthorized"), http.StatusUnauthorized)
470         }
471         postdata := postform.Encode()
472         bufw.WriteString("POST " + u.String() + " HTTP/1.1\r\n")
473         bufw.WriteString("Authorization: Bearer " + tokens[0] + "\r\n")
474         bufw.WriteString("Host: " + u.Host + "\r\n")
475         bufw.WriteString("Upgrade: " + upgradeHeader + "\r\n")
476         bufw.WriteString("Content-Type: application/x-www-form-urlencoded\r\n")
477         fmt.Fprintf(bufw, "Content-Length: %d\r\n", len(postdata))
478         bufw.WriteString("\r\n")
479         bufw.WriteString(postdata)
480         bufw.Flush()
481         resp, err := http.ReadResponse(bufr, &http.Request{Method: "POST"})
482         if err != nil {
483                 return connresp, fmt.Errorf("http.ReadResponse: %w", err)
484         }
485         defer resp.Body.Close()
486         if resp.StatusCode != http.StatusSwitchingProtocols {
487                 ctxlog.FromContext(ctx).Infof("rpc.Conn.socket: server %s did not switch protocols, got status %s", u.String(), resp.Status)
488                 body, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 10000))
489                 var message string
490                 var errDoc httpserver.ErrorResponse
491                 if err := json.Unmarshal(body, &errDoc); err == nil {
492                         message = strings.Join(errDoc.Errors, "; ")
493                 } else {
494                         message = fmt.Sprintf("%q", body)
495                 }
496                 return connresp, httpserver.ErrorWithStatus(fmt.Errorf("server did not provide a tunnel: %s: %s", resp.Status, message), resp.StatusCode)
497         }
498         if strings.ToLower(resp.Header.Get("Upgrade")) != upgradeHeader ||
499                 strings.ToLower(resp.Header.Get("Connection")) != "upgrade" {
500                 return connresp, httpserver.ErrorWithStatus(fmt.Errorf("bad response from server: Upgrade %q Connection %q", resp.Header.Get("Upgrade"), resp.Header.Get("Connection")), http.StatusBadGateway)
501         }
502         connresp.Conn = netconn
503         connresp.Bufrw = &bufio.ReadWriter{Reader: bufr, Writer: bufw}
504         connresp.Header = resp.Header
505         return connresp, nil
506 }
507
508 func (conn *Conn) ContainerRequestCreate(ctx context.Context, options arvados.CreateOptions) (arvados.ContainerRequest, error) {
509         ep := arvados.EndpointContainerRequestCreate
510         var resp arvados.ContainerRequest
511         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
512         return resp, err
513 }
514
515 func (conn *Conn) ContainerRequestUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.ContainerRequest, error) {
516         ep := arvados.EndpointContainerRequestUpdate
517         var resp arvados.ContainerRequest
518         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
519         return resp, err
520 }
521
522 func (conn *Conn) ContainerRequestGet(ctx context.Context, options arvados.GetOptions) (arvados.ContainerRequest, error) {
523         ep := arvados.EndpointContainerRequestGet
524         var resp arvados.ContainerRequest
525         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
526         return resp, err
527 }
528
529 func (conn *Conn) ContainerRequestList(ctx context.Context, options arvados.ListOptions) (arvados.ContainerRequestList, error) {
530         ep := arvados.EndpointContainerRequestList
531         var resp arvados.ContainerRequestList
532         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
533         return resp, err
534 }
535
536 func (conn *Conn) ContainerRequestDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.ContainerRequest, error) {
537         ep := arvados.EndpointContainerRequestDelete
538         var resp arvados.ContainerRequest
539         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
540         return resp, err
541 }
542
543 func (conn *Conn) ContainerRequestContainerStatus(ctx context.Context, options arvados.GetOptions) (arvados.ContainerStatus, error) {
544         ep := arvados.EndpointContainerRequestContainerStatus
545         var resp arvados.ContainerStatus
546         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
547         return resp, err
548 }
549
550 func (conn *Conn) ContainerRequestLog(ctx context.Context, options arvados.ContainerLogOptions) (resp http.Handler, err error) {
551         proxy := &httputil.ReverseProxy{
552                 Transport: conn.httpClient.Transport,
553                 Director: func(r *http.Request) {
554                         u := conn.baseURL
555                         u.Path = r.URL.Path
556                         u.RawQuery = fmt.Sprintf("no_forward=%v", options.NoForward)
557                         r.URL = &u
558                 },
559         }
560         return proxy, nil
561 }
562
563 func (conn *Conn) GroupCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Group, error) {
564         ep := arvados.EndpointGroupCreate
565         var resp arvados.Group
566         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
567         return resp, err
568 }
569
570 func (conn *Conn) GroupUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Group, error) {
571         ep := arvados.EndpointGroupUpdate
572         var resp arvados.Group
573         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
574         return resp, err
575 }
576
577 func (conn *Conn) GroupGet(ctx context.Context, options arvados.GetOptions) (arvados.Group, error) {
578         ep := arvados.EndpointGroupGet
579         var resp arvados.Group
580         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
581         return resp, err
582 }
583
584 func (conn *Conn) GroupList(ctx context.Context, options arvados.ListOptions) (arvados.GroupList, error) {
585         ep := arvados.EndpointGroupList
586         var resp arvados.GroupList
587         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
588         return resp, err
589 }
590
591 func (conn *Conn) GroupContents(ctx context.Context, options arvados.GroupContentsOptions) (arvados.ObjectList, error) {
592         ep := arvados.EndpointGroupContents
593         var resp arvados.ObjectList
594         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
595         return resp, err
596 }
597
598 func (conn *Conn) GroupShared(ctx context.Context, options arvados.ListOptions) (arvados.GroupList, error) {
599         ep := arvados.EndpointGroupShared
600         var resp arvados.GroupList
601         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
602         return resp, err
603 }
604
605 func (conn *Conn) GroupDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Group, error) {
606         ep := arvados.EndpointGroupDelete
607         var resp arvados.Group
608         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
609         return resp, err
610 }
611
612 func (conn *Conn) GroupTrash(ctx context.Context, options arvados.DeleteOptions) (arvados.Group, error) {
613         ep := arvados.EndpointGroupTrash
614         var resp arvados.Group
615         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
616         return resp, err
617 }
618
619 func (conn *Conn) GroupUntrash(ctx context.Context, options arvados.UntrashOptions) (arvados.Group, error) {
620         ep := arvados.EndpointGroupUntrash
621         var resp arvados.Group
622         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
623         return resp, err
624 }
625
626 func (conn *Conn) LinkCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Link, error) {
627         ep := arvados.EndpointLinkCreate
628         var resp arvados.Link
629         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
630         return resp, err
631 }
632
633 func (conn *Conn) LinkUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Link, error) {
634         ep := arvados.EndpointLinkUpdate
635         var resp arvados.Link
636         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
637         return resp, err
638 }
639
640 func (conn *Conn) LinkGet(ctx context.Context, options arvados.GetOptions) (arvados.Link, error) {
641         ep := arvados.EndpointLinkGet
642         var resp arvados.Link
643         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
644         return resp, err
645 }
646
647 func (conn *Conn) LinkList(ctx context.Context, options arvados.ListOptions) (arvados.LinkList, error) {
648         ep := arvados.EndpointLinkList
649         var resp arvados.LinkList
650         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
651         return resp, err
652 }
653
654 func (conn *Conn) LinkDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Link, error) {
655         ep := arvados.EndpointLinkDelete
656         var resp arvados.Link
657         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
658         return resp, err
659 }
660
661 func (conn *Conn) LogCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Log, error) {
662         ep := arvados.EndpointLogCreate
663         var resp arvados.Log
664         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
665         return resp, err
666 }
667
668 func (conn *Conn) LogUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Log, error) {
669         ep := arvados.EndpointLogUpdate
670         var resp arvados.Log
671         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
672         return resp, err
673 }
674
675 func (conn *Conn) LogGet(ctx context.Context, options arvados.GetOptions) (arvados.Log, error) {
676         ep := arvados.EndpointLogGet
677         var resp arvados.Log
678         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
679         return resp, err
680 }
681
682 func (conn *Conn) LogList(ctx context.Context, options arvados.ListOptions) (arvados.LogList, error) {
683         ep := arvados.EndpointLogList
684         var resp arvados.LogList
685         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
686         return resp, err
687 }
688
689 func (conn *Conn) LogDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Log, error) {
690         ep := arvados.EndpointLogDelete
691         var resp arvados.Log
692         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
693         return resp, err
694 }
695
696 func (conn *Conn) SysTrashSweep(ctx context.Context, options struct{}) (struct{}, error) {
697         ep := arvados.EndpointSysTrashSweep
698         var resp struct{}
699         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
700         return resp, err
701 }
702
703 func (conn *Conn) UserCreate(ctx context.Context, options arvados.CreateOptions) (arvados.User, error) {
704         ep := arvados.EndpointUserCreate
705         var resp arvados.User
706         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
707         return resp, err
708 }
709 func (conn *Conn) UserUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.User, error) {
710         ep := arvados.EndpointUserUpdate
711         var resp arvados.User
712         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
713         return resp, err
714 }
715 func (conn *Conn) UserMerge(ctx context.Context, options arvados.UserMergeOptions) (arvados.User, error) {
716         ep := arvados.EndpointUserMerge
717         var resp arvados.User
718         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
719         return resp, err
720 }
721 func (conn *Conn) UserActivate(ctx context.Context, options arvados.UserActivateOptions) (arvados.User, error) {
722         ep := arvados.EndpointUserActivate
723         var resp arvados.User
724         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
725         return resp, err
726 }
727 func (conn *Conn) UserSetup(ctx context.Context, options arvados.UserSetupOptions) (map[string]interface{}, error) {
728         ep := arvados.EndpointUserSetup
729         var resp map[string]interface{}
730         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
731         return resp, err
732 }
733 func (conn *Conn) UserUnsetup(ctx context.Context, options arvados.GetOptions) (arvados.User, error) {
734         ep := arvados.EndpointUserUnsetup
735         var resp arvados.User
736         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
737         return resp, err
738 }
739 func (conn *Conn) UserGet(ctx context.Context, options arvados.GetOptions) (arvados.User, error) {
740         ep := arvados.EndpointUserGet
741         var resp arvados.User
742         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
743         return resp, err
744 }
745 func (conn *Conn) UserGetCurrent(ctx context.Context, options arvados.GetOptions) (arvados.User, error) {
746         ep := arvados.EndpointUserGetCurrent
747         var resp arvados.User
748         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
749         return resp, err
750 }
751 func (conn *Conn) UserGetSystem(ctx context.Context, options arvados.GetOptions) (arvados.User, error) {
752         ep := arvados.EndpointUserGetSystem
753         var resp arvados.User
754         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
755         return resp, err
756 }
757 func (conn *Conn) UserList(ctx context.Context, options arvados.ListOptions) (arvados.UserList, error) {
758         ep := arvados.EndpointUserList
759         var resp arvados.UserList
760         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
761         return resp, err
762 }
763 func (conn *Conn) UserDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.User, error) {
764         ep := arvados.EndpointUserDelete
765         var resp arvados.User
766         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
767         return resp, err
768 }
769
770 func (conn *Conn) APIClientAuthorizationCurrent(ctx context.Context, options arvados.GetOptions) (arvados.APIClientAuthorization, error) {
771         ep := arvados.EndpointAPIClientAuthorizationCurrent
772         var resp arvados.APIClientAuthorization
773         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
774         return resp, err
775 }
776 func (conn *Conn) APIClientAuthorizationCreate(ctx context.Context, options arvados.CreateOptions) (arvados.APIClientAuthorization, error) {
777         ep := arvados.EndpointAPIClientAuthorizationCreate
778         var resp arvados.APIClientAuthorization
779         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
780         return resp, err
781 }
782 func (conn *Conn) APIClientAuthorizationUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.APIClientAuthorization, error) {
783         ep := arvados.EndpointAPIClientAuthorizationUpdate
784         var resp arvados.APIClientAuthorization
785         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
786         return resp, err
787 }
788 func (conn *Conn) APIClientAuthorizationDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.APIClientAuthorization, error) {
789         ep := arvados.EndpointAPIClientAuthorizationDelete
790         var resp arvados.APIClientAuthorization
791         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
792         return resp, err
793 }
794 func (conn *Conn) APIClientAuthorizationList(ctx context.Context, options arvados.ListOptions) (arvados.APIClientAuthorizationList, error) {
795         ep := arvados.EndpointAPIClientAuthorizationList
796         var resp arvados.APIClientAuthorizationList
797         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
798         return resp, err
799 }
800 func (conn *Conn) APIClientAuthorizationGet(ctx context.Context, options arvados.GetOptions) (arvados.APIClientAuthorization, error) {
801         ep := arvados.EndpointAPIClientAuthorizationGet
802         var resp arvados.APIClientAuthorization
803         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
804         return resp, err
805 }
806
807 type UserSessionAuthInfo struct {
808         UserUUID        string    `json:"user_uuid"`
809         Email           string    `json:"email"`
810         AlternateEmails []string  `json:"alternate_emails"`
811         FirstName       string    `json:"first_name"`
812         LastName        string    `json:"last_name"`
813         Username        string    `json:"username"`
814         ExpiresAt       time.Time `json:"expires_at"`
815 }
816
817 type UserSessionCreateOptions struct {
818         AuthInfo UserSessionAuthInfo `json:"auth_info"`
819         ReturnTo string              `json:"return_to"`
820 }
821
822 func (conn *Conn) UserSessionCreate(ctx context.Context, options UserSessionCreateOptions) (arvados.LoginResponse, error) {
823         ep := arvados.APIEndpoint{Method: "POST", Path: "auth/controller/callback"}
824         var resp arvados.LoginResponse
825         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
826         return resp, err
827 }
828
829 func (conn *Conn) UserBatchUpdate(ctx context.Context, options arvados.UserBatchUpdateOptions) (arvados.UserList, error) {
830         ep := arvados.EndpointUserBatchUpdate
831         var resp arvados.UserList
832         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
833         return resp, err
834 }
835
836 func (conn *Conn) UserAuthenticate(ctx context.Context, options arvados.UserAuthenticateOptions) (arvados.APIClientAuthorization, error) {
837         ep := arvados.EndpointUserAuthenticate
838         var resp arvados.APIClientAuthorization
839         err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
840         return resp, err
841 }
842
843 // httpStatusError is an error with an HTTP status code that can be
844 // propagated by lib/controller/router, etc.
845 type httpStatusError interface {
846         error
847         HTTPStatus() int
848 }
849
850 // wrappedHTTPStatusError is used to augment/replace an error message
851 // while preserving the HTTP status code indicated by the original
852 // error.
853 type wrappedHTTPStatusError struct {
854         httpStatusError
855         message string
856 }
857
858 func wrapHTTPStatusError(err httpStatusError, message string) httpStatusError {
859         return wrappedHTTPStatusError{err, message}
860 }
861
862 func (err wrappedHTTPStatusError) Error() string {
863         return err.message
864 }