1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
24 "git.curoverse.com/arvados.git/sdk/go/arvados"
25 "git.curoverse.com/arvados.git/sdk/go/auth"
26 "git.curoverse.com/arvados.git/sdk/go/httpserver"
27 "git.curoverse.com/arvados.git/sdk/go/keepclient"
30 var pathPattern = `^/arvados/v1/%s(/([0-9a-z]{5})-%s-[0-9a-z]{15})?(.*)$`
31 var wfRe = regexp.MustCompile(fmt.Sprintf(pathPattern, "workflows", "7fd4e"))
32 var containersRe = regexp.MustCompile(fmt.Sprintf(pathPattern, "containers", "dz642"))
33 var containerRequestsRe = regexp.MustCompile(fmt.Sprintf(pathPattern, "container_requests", "xvhdp"))
34 var collectionRe = regexp.MustCompile(fmt.Sprintf(pathPattern, "collections", "4zz18"))
35 var collectionByPDHRe = regexp.MustCompile(`^/arvados/v1/collections/([0-9a-fA-F]{32}\+[0-9]+)+$`)
37 type genericFederatedRequestHandler struct {
40 matcher *regexp.Regexp
43 type collectionFederatedRequestHandler struct {
48 func (h *Handler) remoteClusterRequest(remoteID string, w http.ResponseWriter, req *http.Request, filter ResponseFilter) {
49 remote, ok := h.Cluster.RemoteClusters[remoteID]
51 err := fmt.Errorf("no proxy available for cluster %v", remoteID)
53 _, err = filter(nil, err)
56 httpserver.Error(w, err.Error(), http.StatusNotFound)
60 scheme := remote.Scheme
64 req, err := h.saltAuthToken(req, remoteID)
67 _, err = filter(nil, err)
70 httpserver.Error(w, err.Error(), http.StatusBadRequest)
78 RawPath: req.URL.RawPath,
79 RawQuery: req.URL.RawQuery,
81 client := h.secureClient
83 client = h.insecureClient
85 h.proxy.Do(w, req, urlOut, client, filter)
88 // Buffer request body, parse form parameters in request, and then
89 // replace original body with the buffer so it can be re-read by
90 // downstream proxy steps.
91 func loadParamsFromForm(req *http.Request) error {
92 var postBody *bytes.Buffer
93 if req.Body != nil && req.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {
95 if req.ContentLength > 0 {
96 cl = req.ContentLength
98 postBody = bytes.NewBuffer(make([]byte, 0, cl))
99 originalBody := req.Body
100 defer originalBody.Close()
101 req.Body = ioutil.NopCloser(io.TeeReader(req.Body, postBody))
104 err := req.ParseForm()
109 if req.Body != nil && postBody != nil {
110 req.Body = ioutil.NopCloser(postBody)
115 type multiClusterQueryResponseCollector struct {
116 responses []map[string]interface{}
122 func (c *multiClusterQueryResponseCollector) collectResponse(resp *http.Response,
123 requestError error) (newResponse *http.Response, err error) {
124 if requestError != nil {
125 c.error = requestError
129 defer resp.Body.Close()
130 var loadInto struct {
131 Kind string `json:"kind"`
132 Items []map[string]interface{} `json:"items"`
133 Errors []string `json:"errors"`
135 err = json.NewDecoder(resp.Body).Decode(&loadInto)
138 c.error = fmt.Errorf("error fetching from %v (%v): %v", c.clusterID, resp.Status, err)
141 if resp.StatusCode != http.StatusOK {
142 c.error = fmt.Errorf("error fetching from %v (%v): %v", c.clusterID, resp.Status, loadInto.Errors)
146 c.responses = loadInto.Items
147 c.kind = loadInto.Kind
152 func (h *genericFederatedRequestHandler) remoteQueryUUIDs(w http.ResponseWriter,
154 clusterID string, uuids []string) (rp []map[string]interface{}, kind string, err error) {
156 found := make(map[string]bool)
157 prev_len_uuids := len(uuids) + 1
159 // (1) there are more uuids to query
160 // (2) we're making progress - on each iteration the set of
161 // uuids we are expecting for must shrink.
162 for len(uuids) > 0 && len(uuids) < prev_len_uuids {
163 var remoteReq http.Request
164 remoteReq.Header = req.Header
165 remoteReq.Method = "POST"
166 remoteReq.URL = &url.URL{Path: req.URL.Path}
167 remoteParams := make(url.Values)
168 remoteParams.Set("_method", "GET")
169 remoteParams.Set("count", "none")
170 if req.Form.Get("select") != "" {
171 remoteParams.Set("select", req.Form.Get("select"))
173 content, err := json.Marshal(uuids)
177 remoteParams["filters"] = []string{fmt.Sprintf(`[["uuid", "in", %s]]`, content)}
178 enc := remoteParams.Encode()
179 remoteReq.Body = ioutil.NopCloser(bytes.NewBufferString(enc))
181 rc := multiClusterQueryResponseCollector{clusterID: clusterID}
183 if clusterID == h.handler.Cluster.ClusterID {
184 h.handler.localClusterRequest(w, &remoteReq,
187 h.handler.remoteClusterRequest(clusterID, w, &remoteReq,
191 return nil, "", rc.error
196 if len(rc.responses) == 0 {
197 // We got zero responses, no point in doing
202 rp = append(rp, rc.responses...)
204 // Go through the responses and determine what was
205 // returned. If there are remaining items, loop
206 // around and do another request with just the
208 for _, i := range rc.responses {
209 uuid, ok := i["uuid"].(string)
216 for _, u := range uuids {
221 prev_len_uuids = len(uuids)
228 func (h *genericFederatedRequestHandler) handleMultiClusterQuery(w http.ResponseWriter,
229 req *http.Request, clusterId *string) bool {
231 var filters [][]interface{}
232 err := json.Unmarshal([]byte(req.Form.Get("filters")), &filters)
234 httpserver.Error(w, err.Error(), http.StatusBadRequest)
238 // Split the list of uuids by prefix
239 queryClusters := make(map[string][]string)
241 for _, filter := range filters {
242 if len(filter) != 3 {
246 if lhs, ok := filter[0].(string); !ok || lhs != "uuid" {
250 op, ok := filter[1].(string)
256 if rhs, ok := filter[2].([]interface{}); ok {
257 for _, i := range rhs {
258 if u, ok := i.(string); ok {
260 queryClusters[u[0:5]] = append(queryClusters[u[0:5]], u)
265 } else if op == "=" {
266 if u, ok := filter[2].(string); ok {
268 queryClusters[u[0:5]] = append(queryClusters[u[0:5]], u)
277 if len(queryClusters) <= 1 {
278 // Query does not search for uuids across multiple
284 count := req.Form.Get("count")
285 if count != "" && count != `none` && count != `"none"` {
286 httpserver.Error(w, "Federated multi-object query must have 'count=none'", http.StatusBadRequest)
289 if req.Form.Get("limit") != "" || req.Form.Get("offset") != "" || req.Form.Get("order") != "" {
290 httpserver.Error(w, "Federated multi-object may not provide 'limit', 'offset' or 'order'.", http.StatusBadRequest)
293 if expectCount > h.handler.Cluster.RequestLimits.GetMaxItemsPerResponse() {
294 httpserver.Error(w, fmt.Sprintf("Federated multi-object request for %v objects which is more than max page size %v.",
295 expectCount, h.handler.Cluster.RequestLimits.GetMaxItemsPerResponse()), http.StatusBadRequest)
298 if req.Form.Get("select") != "" {
301 err := json.Unmarshal([]byte(req.Form.Get("select")), &selects)
303 httpserver.Error(w, err.Error(), http.StatusBadRequest)
307 for _, r := range selects {
314 httpserver.Error(w, "Federated multi-object request must include 'uuid' in 'select'", http.StatusBadRequest)
319 // Perform concurrent requests to each cluster
321 // use channel as a semaphore to limit the number of concurrent
322 // requests at a time
323 sem := make(chan bool, h.handler.Cluster.RequestLimits.GetMultiClusterRequestConcurrency())
325 wg := sync.WaitGroup{}
327 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
330 var completeResponses []map[string]interface{}
333 for k, v := range queryClusters {
339 // blocks until it can put a value into the
340 // channel (which has a max queue capacity)
343 go func(k string, v []string) {
344 rp, kn, err := h.remoteQueryUUIDs(w, req, k, v)
347 completeResponses = append(completeResponses, rp...)
350 errors = append(errors, err)
361 for _, e := range errors {
362 strerr = append(strerr, e.Error())
364 httpserver.Errors(w, strerr, http.StatusBadGateway)
368 w.Header().Set("Content-Type", "application/json")
369 w.WriteHeader(http.StatusOK)
370 itemList := make(map[string]interface{})
371 itemList["items"] = completeResponses
372 itemList["kind"] = kind
373 json.NewEncoder(w).Encode(itemList)
378 func (h *genericFederatedRequestHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
379 m := h.matcher.FindStringSubmatch(req.URL.Path)
382 if len(m) > 0 && m[2] != "" {
386 // Get form parameters from URL and form body (if POST).
387 if err := loadParamsFromForm(req); err != nil {
388 httpserver.Error(w, err.Error(), http.StatusBadRequest)
392 // Check if the parameters have an explicit cluster_id
393 if req.Form.Get("cluster_id") != "" {
394 clusterId = req.Form.Get("cluster_id")
397 // Handle the POST-as-GET special case (workaround for large
398 // GET requests that potentially exceed maximum URL length,
399 // like multi-object queries where the filter has 100s of
401 effectiveMethod := req.Method
402 if req.Method == "POST" && req.Form.Get("_method") != "" {
403 effectiveMethod = req.Form.Get("_method")
406 if effectiveMethod == "GET" &&
408 req.Form.Get("filters") != "" &&
409 h.handleMultiClusterQuery(w, req, &clusterId) {
413 if clusterId == "" || clusterId == h.handler.Cluster.ClusterID {
414 h.next.ServeHTTP(w, req)
416 h.handler.remoteClusterRequest(clusterId, w, req, nil)
420 type rewriteSignaturesClusterId struct {
425 func (rw rewriteSignaturesClusterId) rewriteSignatures(resp *http.Response, requestError error) (newResponse *http.Response, err error) {
426 if requestError != nil {
427 return resp, requestError
430 if resp.StatusCode != 200 {
434 originalBody := resp.Body
435 defer originalBody.Close()
437 var col arvados.Collection
438 err = json.NewDecoder(resp.Body).Decode(&col)
443 // rewriting signatures will make manifest text 5-10% bigger so calculate
444 // capacity accordingly
445 updatedManifest := bytes.NewBuffer(make([]byte, 0, int(float64(len(col.ManifestText))*1.1)))
448 mw := io.MultiWriter(hasher, updatedManifest)
451 scanner := bufio.NewScanner(strings.NewReader(col.ManifestText))
452 scanner.Buffer(make([]byte, 1048576), len(col.ManifestText))
454 line := scanner.Text()
455 tokens := strings.Split(line, " ")
457 return nil, fmt.Errorf("Invalid stream (<3 tokens): %q", line)
460 n, err := mw.Write([]byte(tokens[0]))
462 return nil, fmt.Errorf("Error updating manifest: %v", err)
465 for _, token := range tokens[1:] {
466 n, err = mw.Write([]byte(" "))
468 return nil, fmt.Errorf("Error updating manifest: %v", err)
472 m := keepclient.SignedLocatorRe.FindStringSubmatch(token)
474 // Rewrite the block signature to be a remote signature
475 _, err = fmt.Fprintf(updatedManifest, "%s%s%s+R%s-%s%s", m[1], m[2], m[3], rw.clusterID, m[5][2:], m[8])
477 return nil, fmt.Errorf("Error updating manifest: %v", err)
480 // for hash checking, ignore signatures
481 n, err = fmt.Fprintf(hasher, "%s%s", m[1], m[2])
483 return nil, fmt.Errorf("Error updating manifest: %v", err)
487 n, err = mw.Write([]byte(token))
489 return nil, fmt.Errorf("Error updating manifest: %v", err)
494 n, err = mw.Write([]byte("\n"))
496 return nil, fmt.Errorf("Error updating manifest: %v", err)
501 // Check that expected hash is consistent with
502 // portable_data_hash field of the returned record
503 if rw.expectHash == "" {
504 rw.expectHash = col.PortableDataHash
505 } else if rw.expectHash != col.PortableDataHash {
506 return nil, fmt.Errorf("portable_data_hash %q on returned record did not match expected hash %q ", rw.expectHash, col.PortableDataHash)
509 // Certify that the computed hash of the manifest_text matches our expectation
510 sum := hasher.Sum(nil)
511 computedHash := fmt.Sprintf("%x+%v", sum, sz)
512 if computedHash != rw.expectHash {
513 return nil, fmt.Errorf("Computed manifest_text hash %q did not match expected hash %q", computedHash, rw.expectHash)
516 col.ManifestText = updatedManifest.String()
518 newbody, err := json.Marshal(col)
523 buf := bytes.NewBuffer(newbody)
524 resp.Body = ioutil.NopCloser(buf)
525 resp.ContentLength = int64(buf.Len())
526 resp.Header.Set("Content-Length", fmt.Sprintf("%v", buf.Len()))
531 func filterLocalClusterResponse(resp *http.Response, requestError error) (newResponse *http.Response, err error) {
532 if requestError != nil {
533 return resp, requestError
536 if resp.StatusCode == 404 {
537 // Suppress returning this result, because we want to
538 // search the federation.
544 type searchRemoteClusterForPDH struct {
549 sharedContext *context.Context
555 func (s *searchRemoteClusterForPDH) filterRemoteClusterResponse(resp *http.Response, requestError error) (newResponse *http.Response, err error) {
560 // Another request already returned a response
564 if requestError != nil {
565 *s.errors = append(*s.errors, fmt.Sprintf("Request error contacting %q: %v", s.remoteID, requestError))
566 // Record the error and suppress response
570 if resp.StatusCode != 200 {
571 // Suppress returning unsuccessful result. Maybe
572 // another request will find it.
573 // TODO collect and return error responses.
574 *s.errors = append(*s.errors, fmt.Sprintf("Response from %q: %v", s.remoteID, resp.Status))
575 if resp.StatusCode != 404 {
576 // Got a non-404 error response, convert into BadGateway
577 *s.statusCode = http.StatusBadGateway
584 // This reads the response body. We don't want to hold the
585 // lock while doing this because other remote requests could
586 // also have made it to this point, and we don't want a
587 // slow response holding the lock to block a faster response
588 // that is waiting on the lock.
589 newResponse, err = rewriteSignaturesClusterId{s.remoteID, s.pdh}.rewriteSignatures(resp, nil)
594 // Another request already returned a response
599 // Suppress returning unsuccessful result. Maybe
600 // another request will be successful.
601 *s.errors = append(*s.errors, fmt.Sprintf("Error parsing response from %q: %v", s.remoteID, err))
605 // We have a successful response. Suppress/cancel all the
606 // other requests/responses.
607 *s.sentResponse = true
610 return newResponse, nil
613 func (h *collectionFederatedRequestHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
614 if req.Method != "GET" {
615 // Only handle GET requests right now
616 h.next.ServeHTTP(w, req)
620 m := collectionByPDHRe.FindStringSubmatch(req.URL.Path)
622 // Not a collection PDH GET request
623 m = collectionRe.FindStringSubmatch(req.URL.Path)
630 if clusterId != "" && clusterId != h.handler.Cluster.ClusterID {
631 // request for remote collection by uuid
632 h.handler.remoteClusterRequest(clusterId, w, req,
633 rewriteSignaturesClusterId{clusterId, ""}.rewriteSignatures)
636 // not a collection UUID request, or it is a request
637 // for a local UUID, either way, continue down the
639 h.next.ServeHTTP(w, req)
643 // Request for collection by PDH. Search the federation.
645 // First, query the local cluster.
646 if h.handler.localClusterRequest(w, req, filterLocalClusterResponse) {
650 sharedContext, cancelFunc := context.WithCancel(req.Context())
652 req = req.WithContext(sharedContext)
654 // Create a goroutine for each cluster in the
655 // RemoteClusters map. The first valid result gets
656 // returned to the client. When that happens, all
657 // other outstanding requests are cancelled or
659 sentResponse := false
661 wg := sync.WaitGroup{}
663 var errorCode int = 404
665 // use channel as a semaphore to limit the number of concurrent
666 // requests at a time
667 sem := make(chan bool, h.handler.Cluster.RequestLimits.GetMultiClusterRequestConcurrency())
669 for remoteID := range h.handler.Cluster.RemoteClusters {
670 if remoteID == h.handler.Cluster.ClusterID {
671 // No need to query local cluster again
674 // blocks until it can put a value into the
675 // channel (which has a max queue capacity)
680 search := &searchRemoteClusterForPDH{m[1], remoteID, &mtx, &sentResponse,
681 &sharedContext, cancelFunc, &errors, &errorCode}
684 h.handler.remoteClusterRequest(search.remoteID, w, req, search.filterRemoteClusterResponse)
695 // No successful responses, so return the error
696 httpserver.Errors(w, errors, errorCode)
699 func (h *Handler) setupProxyRemoteCluster(next http.Handler) http.Handler {
700 mux := http.NewServeMux()
701 mux.Handle("/arvados/v1/workflows", &genericFederatedRequestHandler{next, h, wfRe})
702 mux.Handle("/arvados/v1/workflows/", &genericFederatedRequestHandler{next, h, wfRe})
703 mux.Handle("/arvados/v1/containers", &genericFederatedRequestHandler{next, h, containersRe})
704 mux.Handle("/arvados/v1/containers/", &genericFederatedRequestHandler{next, h, containersRe})
705 mux.Handle("/arvados/v1/container_requests", &genericFederatedRequestHandler{next, h, containerRequestsRe})
706 mux.Handle("/arvados/v1/container_requests/", &genericFederatedRequestHandler{next, h, containerRequestsRe})
707 mux.Handle("/arvados/v1/collections", next)
708 mux.Handle("/arvados/v1/collections/", &collectionFederatedRequestHandler{next, h})
709 mux.Handle("/", next)
711 return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
712 parts := strings.Split(req.Header.Get("Authorization"), "/")
713 alreadySalted := (len(parts) == 3 && parts[0] == "Bearer v2" && len(parts[2]) == 40)
716 strings.Index(req.Header.Get("Via"), "arvados-controller") != -1 {
717 // The token is already salted, or this is a
718 // request from another instance of
719 // arvados-controller. In either case, we
720 // don't want to proxy this query, so just
721 // continue down the instance handler stack.
722 next.ServeHTTP(w, req)
726 mux.ServeHTTP(w, req)
732 type CurrentUser struct {
733 Authorization arvados.APIClientAuthorization
737 func (h *Handler) validateAPItoken(req *http.Request, user *CurrentUser) error {
742 return db.QueryRowContext(req.Context(), `SELECT api_client_authorizations.uuid, users.uuid FROM api_client_authorizations JOIN users on api_client_authorizations.user_id=users.id WHERE api_token=$1 AND (expires_at IS NULL OR expires_at > current_timestamp) LIMIT 1`, user.Authorization.APIToken).Scan(&user.Authorization.UUID, &user.UUID)
745 // Extract the auth token supplied in req, and replace it with a
746 // salted token for the remote cluster.
747 func (h *Handler) saltAuthToken(req *http.Request, remote string) (updatedReq *http.Request, err error) {
748 updatedReq = (&http.Request{
753 ContentLength: req.ContentLength,
755 }).WithContext(req.Context())
757 creds := auth.NewCredentials()
758 creds.LoadTokensFromHTTPRequest(updatedReq)
759 if len(creds.Tokens) == 0 && updatedReq.Header.Get("Content-Type") == "application/x-www-form-encoded" {
760 // Override ParseForm's 10MiB limit by ensuring
761 // req.Body is a *http.maxBytesReader.
762 updatedReq.Body = http.MaxBytesReader(nil, updatedReq.Body, 1<<28) // 256MiB. TODO: use MaxRequestSize from discovery doc or config.
763 if err := creds.LoadTokensFromHTTPRequestBody(updatedReq); err != nil {
766 // Replace req.Body with a buffer that re-encodes the
767 // form without api_token, in case we end up
768 // forwarding the request.
769 if updatedReq.PostForm != nil {
770 updatedReq.PostForm.Del("api_token")
772 updatedReq.Body = ioutil.NopCloser(bytes.NewBufferString(updatedReq.PostForm.Encode()))
774 if len(creds.Tokens) == 0 {
775 return updatedReq, nil
778 token, err := auth.SaltToken(creds.Tokens[0], remote)
780 log.Printf("Salting %q %q to get %q %q", creds.Tokens[0], remote, token, err)
781 if err == auth.ErrObsoleteToken {
782 // If the token exists in our own database, salt it
783 // for the remote. Otherwise, assume it was issued by
784 // the remote, and pass it through unmodified.
785 currentUser := CurrentUser{Authorization: arvados.APIClientAuthorization{APIToken: creds.Tokens[0]}}
786 err = h.validateAPItoken(req, ¤tUser)
787 if err == sql.ErrNoRows {
788 // Not ours; pass through unmodified.
789 token = currentUser.Authorization.APIToken
790 } else if err != nil {
793 // Found; make V2 version and salt it.
794 token, err = auth.SaltToken(currentUser.Authorization.TokenV2(), remote)
799 } else if err != nil {
802 updatedReq.Header = http.Header{}
803 for k, v := range req.Header {
804 if k == "Authorization" {
805 updatedReq.Header[k] = []string{"Bearer " + token}
807 updatedReq.Header[k] = v
811 log.Printf("Salted %q %q to get %q", creds.Tokens[0], remote, token)
813 // Remove api_token=... from the the query string, in case we
814 // end up forwarding the request.
815 if values, err := url.ParseQuery(updatedReq.URL.RawQuery); err != nil {
817 } else if _, ok := values["api_token"]; ok {
818 delete(values, "api_token")
819 updatedReq.URL = &url.URL{
820 Scheme: req.URL.Scheme,
823 RawPath: req.URL.RawPath,
824 RawQuery: values.Encode(),
827 return updatedReq, nil