20240: Update test to reveal bug.
[arvados.git] / lib / controller / handler.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package controller
6
7 import (
8         "context"
9         "encoding/json"
10         "errors"
11         "fmt"
12         "io/ioutil"
13         "mime"
14         "net/http"
15         "net/http/httptest"
16         "net/url"
17         "strings"
18         "sync"
19         "time"
20
21         "git.arvados.org/arvados.git/lib/controller/api"
22         "git.arvados.org/arvados.git/lib/controller/federation"
23         "git.arvados.org/arvados.git/lib/controller/localdb"
24         "git.arvados.org/arvados.git/lib/controller/railsproxy"
25         "git.arvados.org/arvados.git/lib/controller/router"
26         "git.arvados.org/arvados.git/lib/ctrlctx"
27         "git.arvados.org/arvados.git/sdk/go/arvados"
28         "git.arvados.org/arvados.git/sdk/go/ctxlog"
29         "git.arvados.org/arvados.git/sdk/go/health"
30         "git.arvados.org/arvados.git/sdk/go/httpserver"
31
32         // sqlx needs lib/pq to talk to PostgreSQL
33         _ "github.com/lib/pq"
34 )
35
36 type Handler struct {
37         Cluster           *arvados.Cluster
38         BackgroundContext context.Context
39
40         setupOnce      sync.Once
41         federation     *federation.Conn
42         handlerStack   http.Handler
43         proxy          *proxy
44         secureClient   *http.Client
45         insecureClient *http.Client
46         dbConnector    ctrlctx.DBConnector
47         limitLogCreate chan struct{}
48
49         cache map[string]*cacheEnt
50 }
51
52 func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
53         h.setupOnce.Do(h.setup)
54         if req.Method != "GET" && req.Method != "HEAD" {
55                 // http.ServeMux returns 301 with a cleaned path if
56                 // the incoming request has a double slash. Some
57                 // clients (including the Go standard library) change
58                 // the request method to GET when following a 301
59                 // redirect if the original method was not HEAD
60                 // (RFC7231 6.4.2 specifically allows this in the case
61                 // of POST). Thus "POST //foo" gets misdirected to
62                 // "GET /foo". To avoid this, eliminate double slashes
63                 // before passing the request to ServeMux.
64                 for strings.Contains(req.URL.Path, "//") {
65                         req.URL.Path = strings.Replace(req.URL.Path, "//", "/", -1)
66                 }
67         }
68         h.handlerStack.ServeHTTP(w, req)
69 }
70
71 func (h *Handler) CheckHealth() error {
72         h.setupOnce.Do(h.setup)
73         _, err := h.dbConnector.GetDB(context.TODO())
74         if err != nil {
75                 return err
76         }
77         _, _, err = railsproxy.FindRailsAPI(h.Cluster)
78         if err != nil {
79                 return err
80         }
81         if h.Cluster.API.VocabularyPath != "" {
82                 req, err := http.NewRequest("GET", "/arvados/v1/vocabulary", nil)
83                 if err != nil {
84                         return err
85                 }
86                 var resp httptest.ResponseRecorder
87                 h.handlerStack.ServeHTTP(&resp, req)
88                 if resp.Result().StatusCode != http.StatusOK {
89                         return fmt.Errorf("%d %s", resp.Result().StatusCode, resp.Result().Status)
90                 }
91         }
92         return nil
93 }
94
95 func (h *Handler) Done() <-chan struct{} {
96         return nil
97 }
98
99 func neverRedirect(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
100
101 func (h *Handler) setup() {
102         mux := http.NewServeMux()
103         healthFuncs := make(map[string]health.Func)
104
105         h.dbConnector = ctrlctx.DBConnector{PostgreSQL: h.Cluster.PostgreSQL}
106         go func() {
107                 <-h.BackgroundContext.Done()
108                 h.dbConnector.Close()
109         }()
110         oidcAuthorizer := localdb.OIDCAccessTokenAuthorizer(h.Cluster, h.dbConnector.GetDB)
111         h.federation = federation.New(h.BackgroundContext, h.Cluster, &healthFuncs, h.dbConnector.GetDB)
112         rtr := router.New(h.federation, router.Config{
113                 MaxRequestSize: h.Cluster.API.MaxRequestSize,
114                 WrapCalls: api.ComposeWrappers(
115                         ctrlctx.WrapCallsInTransactions(h.dbConnector.GetDB),
116                         oidcAuthorizer.WrapCalls,
117                         ctrlctx.WrapCallsWithAuth(h.Cluster)),
118         })
119
120         healthRoutes := health.Routes{"ping": func() error { _, err := h.dbConnector.GetDB(context.TODO()); return err }}
121         for name, f := range healthFuncs {
122                 healthRoutes[name] = f
123         }
124         mux.Handle("/_health/", &health.Handler{
125                 Token:  h.Cluster.ManagementToken,
126                 Prefix: "/_health/",
127                 Routes: healthRoutes,
128         })
129         mux.Handle("/arvados/v1/config", rtr)
130         mux.Handle("/arvados/v1/vocabulary", rtr)
131         mux.Handle("/"+arvados.EndpointUserAuthenticate.Path, rtr) // must come before .../users/
132         mux.Handle("/arvados/v1/collections", rtr)
133         mux.Handle("/arvados/v1/collections/", rtr)
134         mux.Handle("/arvados/v1/users", rtr)
135         mux.Handle("/arvados/v1/users/", rtr)
136         mux.Handle("/arvados/v1/connect/", rtr)
137         mux.Handle("/arvados/v1/container_requests", rtr)
138         mux.Handle("/arvados/v1/container_requests/", rtr)
139         mux.Handle("/arvados/v1/groups", rtr)
140         mux.Handle("/arvados/v1/groups/", rtr)
141         mux.Handle("/arvados/v1/links", rtr)
142         mux.Handle("/arvados/v1/links/", rtr)
143         mux.Handle("/login", rtr)
144         mux.Handle("/logout", rtr)
145         mux.Handle("/arvados/v1/api_client_authorizations", rtr)
146         mux.Handle("/arvados/v1/api_client_authorizations/", rtr)
147
148         hs := http.NotFoundHandler()
149         hs = prepend(hs, h.proxyRailsAPI)
150         hs = prepend(hs, h.limitLogCreateRequests)
151         hs = h.setupProxyRemoteCluster(hs)
152         hs = prepend(hs, oidcAuthorizer.Middleware)
153         mux.Handle("/", hs)
154         h.handlerStack = mux
155
156         sc := *arvados.DefaultSecureClient
157         sc.CheckRedirect = neverRedirect
158         h.secureClient = &sc
159
160         ic := *arvados.InsecureHTTPClient
161         ic.CheckRedirect = neverRedirect
162         h.insecureClient = &ic
163
164         logCreateLimit := int(float64(h.Cluster.API.MaxConcurrentRequests) * h.Cluster.API.LogCreateRequestFraction)
165         if logCreateLimit == 0 && h.Cluster.API.LogCreateRequestFraction > 0 {
166                 logCreateLimit = 1
167         }
168         h.limitLogCreate = make(chan struct{}, logCreateLimit)
169
170         h.proxy = &proxy{
171                 Name: "arvados-controller",
172         }
173         h.cache = map[string]*cacheEnt{
174                 "/discovery/v1/apis/arvados/v1/rest": &cacheEnt{validate: validateDiscoveryDoc},
175         }
176
177         go h.trashSweepWorker()
178         go h.containerLogSweepWorker()
179 }
180
181 type middlewareFunc func(http.ResponseWriter, *http.Request, http.Handler)
182
183 func prepend(next http.Handler, middleware middlewareFunc) http.Handler {
184         return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
185                 middleware(w, req, next)
186         })
187 }
188
189 func (h *Handler) localClusterRequest(req *http.Request) (*http.Response, error) {
190         urlOut, insecure, err := railsproxy.FindRailsAPI(h.Cluster)
191         if err != nil {
192                 return nil, err
193         }
194         urlOut = &url.URL{
195                 Scheme:   urlOut.Scheme,
196                 Host:     urlOut.Host,
197                 Path:     req.URL.Path,
198                 RawPath:  req.URL.RawPath,
199                 RawQuery: req.URL.RawQuery,
200         }
201         client := h.secureClient
202         if insecure {
203                 client = h.insecureClient
204         }
205         return h.proxy.Do(req, urlOut, client)
206 }
207
208 func (h *Handler) limitLogCreateRequests(w http.ResponseWriter, req *http.Request, next http.Handler) {
209         if cap(h.limitLogCreate) > 0 && req.Method == http.MethodPost && strings.HasPrefix(req.URL.Path, "/arvados/v1/logs") {
210                 select {
211                 case h.limitLogCreate <- struct{}{}:
212                         defer func() { <-h.limitLogCreate }()
213                         next.ServeHTTP(w, req)
214                 default:
215                         http.Error(w, "Excess log messages", http.StatusServiceUnavailable)
216                 }
217                 return
218         }
219         next.ServeHTTP(w, req)
220 }
221
222 // cacheEnt implements a basic stale-while-revalidate cache, suitable
223 // for the Arvados discovery document.
224 type cacheEnt struct {
225         validate     func(body []byte) error
226         mtx          sync.Mutex
227         header       http.Header
228         body         []byte
229         expireAfter  time.Time
230         refreshAfter time.Time
231         refreshLock  sync.Mutex
232 }
233
234 const (
235         cacheTTL    = 5 * time.Minute
236         cacheExpire = 24 * time.Hour
237 )
238
239 func (ent *cacheEnt) refresh(path string, do func(*http.Request) (*http.Response, error)) (http.Header, []byte, error) {
240         ent.refreshLock.Lock()
241         defer ent.refreshLock.Unlock()
242         if header, body, needRefresh := ent.response(); !needRefresh {
243                 // another goroutine refreshed successfully while we
244                 // were waiting for refreshLock
245                 return header, body, nil
246         } else if body != nil {
247                 // Cache is present, but expired. We'll try to refresh
248                 // below. Meanwhile, other refresh() calls will queue
249                 // up for refreshLock -- and we don't want them to
250                 // turn into N upstream requests, even if upstream is
251                 // failing.  (If we succeed we'll update the expiry
252                 // time again below with the real cacheTTL -- this
253                 // just takes care of the error case.)
254                 ent.mtx.Lock()
255                 ent.refreshAfter = time.Now().Add(time.Second)
256                 ent.mtx.Unlock()
257         }
258
259         ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Minute))
260         defer cancel()
261         // 0.0.0.0:0 is just a placeholder here -- do(), which is
262         // localClusterRequest(), will replace the scheme and host
263         // parts with the real proxy destination.
264         req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://0.0.0.0:0/"+path, nil)
265         if err != nil {
266                 return nil, nil, err
267         }
268         resp, err := do(req)
269         if err != nil {
270                 return nil, nil, err
271         }
272         if resp.StatusCode != http.StatusOK {
273                 return nil, nil, fmt.Errorf("HTTP status %d", resp.StatusCode)
274         }
275         body, err := ioutil.ReadAll(resp.Body)
276         if err != nil {
277                 return nil, nil, fmt.Errorf("Read error: %w", err)
278         }
279         header := http.Header{}
280         for k, v := range resp.Header {
281                 if !dropHeaders[k] && k != "X-Request-Id" {
282                         header[k] = v
283                 }
284         }
285         if ent.validate != nil {
286                 if err := ent.validate(body); err != nil {
287                         return nil, nil, err
288                 }
289         } else if mediatype, _, err := mime.ParseMediaType(header.Get("Content-Type")); err == nil && mediatype == "application/json" {
290                 if !json.Valid(body) {
291                         return nil, nil, errors.New("invalid JSON encoding in response")
292                 }
293         }
294         ent.mtx.Lock()
295         defer ent.mtx.Unlock()
296         ent.header = header
297         ent.body = body
298         ent.refreshAfter = time.Now().Add(cacheTTL)
299         ent.expireAfter = time.Now().Add(cacheExpire)
300         return ent.header, ent.body, nil
301 }
302
303 func (ent *cacheEnt) response() (http.Header, []byte, bool) {
304         ent.mtx.Lock()
305         defer ent.mtx.Unlock()
306         if ent.expireAfter.Before(time.Now()) {
307                 ent.header, ent.body, ent.refreshAfter = nil, nil, time.Time{}
308         }
309         return ent.header, ent.body, ent.refreshAfter.Before(time.Now())
310 }
311
312 func (ent *cacheEnt) ServeHTTP(ctx context.Context, w http.ResponseWriter, path string, do func(*http.Request) (*http.Response, error)) {
313         header, body, needRefresh := ent.response()
314         if body == nil {
315                 // need to fetch before we can return anything
316                 var err error
317                 header, body, err = ent.refresh(path, do)
318                 if err != nil {
319                         http.Error(w, err.Error(), http.StatusBadGateway)
320                         return
321                 }
322         } else if needRefresh {
323                 // re-fetch in background
324                 go func() {
325                         _, _, err := ent.refresh(path, do)
326                         if err != nil {
327                                 ctxlog.FromContext(ctx).WithError(err).WithField("path", path).Warn("error refreshing cache")
328                         }
329                 }()
330         }
331         for k, v := range header {
332                 w.Header()[k] = v
333         }
334         w.WriteHeader(http.StatusOK)
335         w.Write(body)
336 }
337
338 func (h *Handler) proxyRailsAPI(w http.ResponseWriter, req *http.Request, next http.Handler) {
339         if ent, ok := h.cache[req.URL.Path]; ok && req.Method == http.MethodGet {
340                 ent.ServeHTTP(req.Context(), w, req.URL.Path, h.localClusterRequest)
341                 return
342         }
343         resp, err := h.localClusterRequest(req)
344         n, err := h.proxy.ForwardResponse(w, resp, err)
345         if err != nil {
346                 httpserver.Logger(req).WithError(err).WithField("bytesCopied", n).Error("error copying response body")
347         }
348 }
349
350 // Use a localhost entry from Services.RailsAPI.InternalURLs if one is
351 // present, otherwise choose an arbitrary entry.
352 func findRailsAPI(cluster *arvados.Cluster) (*url.URL, bool, error) {
353         var best *url.URL
354         for target := range cluster.Services.RailsAPI.InternalURLs {
355                 target := url.URL(target)
356                 best = &target
357                 if strings.HasPrefix(target.Host, "localhost:") || strings.HasPrefix(target.Host, "127.0.0.1:") || strings.HasPrefix(target.Host, "[::1]:") {
358                         break
359                 }
360         }
361         if best == nil {
362                 return nil, false, fmt.Errorf("Services.RailsAPI.InternalURLs is empty")
363         }
364         return best, cluster.TLS.Insecure, nil
365 }
366
367 func validateDiscoveryDoc(body []byte) error {
368         var dd arvados.DiscoveryDocument
369         err := json.Unmarshal(body, &dd)
370         if err != nil {
371                 return fmt.Errorf("error decoding JSON response: %w", err)
372         }
373         if dd.BasePath == "" {
374                 return errors.New("error in discovery document: no value for basePath")
375         }
376         return nil
377 }