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 type CurrentUser struct {
58 Authorization arvados.APIClientAuthorization
62 func (h *Handler) validateAPItoken(req *http.Request, user *CurrentUser) error {
67 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)
70 // Extract the auth token supplied in req, and replace it with a
71 // salted token for the remote cluster.
72 func (h *Handler) saltAuthToken(req *http.Request, remote string) error {
73 creds := auth.NewCredentials()
74 creds.LoadTokensFromHTTPRequest(req)
75 if len(creds.Tokens) == 0 && req.Header.Get("Content-Type") == "application/x-www-form-encoded" {
76 // Override ParseForm's 10MiB limit by ensuring
77 // req.Body is a *http.maxBytesReader.
78 req.Body = http.MaxBytesReader(nil, req.Body, 1<<28) // 256MiB. TODO: use MaxRequestSize from discovery doc or config.
79 if err := creds.LoadTokensFromHTTPRequestBody(req); err != nil {
82 // Replace req.Body with a buffer that re-encodes the
83 // form without api_token, in case we end up
84 // forwarding the request.
85 if req.PostForm != nil {
86 req.PostForm.Del("api_token")
88 req.Body = ioutil.NopCloser(bytes.NewBufferString(req.PostForm.Encode()))
90 if len(creds.Tokens) == 0 {
93 token, err := auth.SaltToken(creds.Tokens[0], remote)
94 if err == auth.ErrObsoleteToken {
95 // If the token exists in our own database, salt it
96 // for the remote. Otherwise, assume it was issued by
97 // the remote, and pass it through unmodified.
98 currentUser := CurrentUser{Authorization: arvados.APIClientAuthorization{APIToken: creds.Tokens[0]}}
99 err = h.validateAPItoken(req, ¤tUser)
100 if err == sql.ErrNoRows {
101 // Not ours; pass through unmodified.
102 token = currentUser.Authorization.APIToken
103 } else if err != nil {
106 // Found; make V2 version and salt it.
107 token, err = auth.SaltToken(currentUser.Authorization.TokenV2(), remote)
112 } else if err != nil {
115 req.Header.Set("Authorization", "Bearer "+token)
117 // Remove api_token=... from the the query string, in case we
118 // end up forwarding the request.
119 if values, err := url.ParseQuery(req.URL.RawQuery); err != nil {
121 } else if _, ok := values["api_token"]; ok {
122 delete(values, "api_token")
123 req.URL.RawQuery = values.Encode()