1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.curoverse.com/arvados.git/sdk/go/arvados"
16 "git.curoverse.com/arvados.git/sdk/go/auth"
17 "git.curoverse.com/arvados.git/sdk/go/httpserver"
20 var wfRe = regexp.MustCompile(`^/arvados/v1/workflows/([0-9a-z]{5})-[^/]+$`)
22 func (h *Handler) proxyRemoteCluster(w http.ResponseWriter, req *http.Request, next http.Handler) {
23 m := wfRe.FindStringSubmatch(req.URL.Path)
24 if len(m) < 2 || m[1] == h.Cluster.ClusterID {
25 next.ServeHTTP(w, req)
29 remote, ok := h.Cluster.RemoteClusters[remoteID]
31 httpserver.Error(w, "no proxy available for cluster "+remoteID, http.StatusNotFound)
34 scheme := remote.Scheme
38 err := h.saltAuthToken(req, remoteID)
40 httpserver.Error(w, err.Error(), http.StatusBadRequest)
47 RawPath: req.URL.RawPath,
48 RawQuery: req.URL.RawQuery,
50 client := h.secureClient
52 client = h.insecureClient
54 h.proxy.Do(w, req, urlOut, client)
57 // Extract the auth token supplied in req, and replace it with a
58 // salted token for the remote cluster.
59 func (h *Handler) saltAuthToken(req *http.Request, remote string) error {
60 creds := auth.NewCredentials()
61 creds.LoadTokensFromHTTPRequest(req)
62 if len(creds.Tokens) == 0 && req.Header.Get("Content-Type") == "application/x-www-form-encoded" {
63 // Override ParseForm's 10MiB limit by ensuring
64 // req.Body is a *http.maxBytesReader.
65 req.Body = http.MaxBytesReader(nil, req.Body, 1<<28) // 256MiB. TODO: use MaxRequestSize from discovery doc or config.
66 if err := creds.LoadTokensFromHTTPRequestBody(req); err != nil {
69 // Replace req.Body with a buffer that re-encodes the
70 // form without api_token, in case we end up
71 // forwarding the request.
72 if req.PostForm != nil {
73 req.PostForm.Del("api_token")
75 req.Body = ioutil.NopCloser(bytes.NewBufferString(req.PostForm.Encode()))
77 if len(creds.Tokens) == 0 {
80 token, err := auth.SaltToken(creds.Tokens[0], remote)
81 if err == auth.ErrObsoleteToken {
82 // If the token exists in our own database, salt it
83 // for the remote. Otherwise, assume it was issued by
84 // the remote, and pass it through unmodified.
89 aca := arvados.APIClientAuthorization{APIToken: creds.Tokens[0]}
90 err = db.QueryRowContext(req.Context(), `SELECT uuid FROM api_client_authorizations WHERE api_token=$1 AND (expires_at IS NULL OR expires_at > current_timestamp) LIMIT 1`, aca.APIToken).Scan(&aca.UUID)
91 if err == sql.ErrNoRows {
92 // Not ours; pass through unmodified.
94 } else if err != nil {
97 // Found; make V2 version and salt it.
98 token, err = auth.SaltToken(aca.TokenV2(), remote)
103 } else if err != nil {
106 req.Header.Set("Authorization", "Bearer "+token)
108 // Remove api_token=... from the the query string, in case we
109 // end up forwarding the request.
110 if values, err := url.ParseQuery(req.URL.RawQuery); err != nil {
112 } else if _, ok := values["api_token"]; ok {
113 delete(values, "api_token")
114 req.URL.RawQuery = values.Encode()