Bump loofah from 2.2.3 to 2.3.1 in /apps/workbench
[arvados.git] / lib / controller / railsproxy / railsproxy.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 // Package railsproxy implements Arvados APIs by proxying to the
6 // RailsAPI server on the local machine.
7 package railsproxy
8
9 import (
10         "fmt"
11         "net/http"
12         "net/url"
13         "strings"
14
15         "git.curoverse.com/arvados.git/lib/controller/rpc"
16         "git.curoverse.com/arvados.git/sdk/go/arvados"
17 )
18
19 // For now, FindRailsAPI always uses the rails API running on this
20 // node.
21 func FindRailsAPI(cluster *arvados.Cluster) (*url.URL, bool, error) {
22         var best *url.URL
23         for target := range cluster.Services.RailsAPI.InternalURLs {
24                 target := url.URL(target)
25                 best = &target
26                 if strings.HasPrefix(target.Host, "localhost:") || strings.HasPrefix(target.Host, "127.0.0.1:") || strings.HasPrefix(target.Host, "[::1]:") {
27                         break
28                 }
29         }
30         if best == nil {
31                 return nil, false, fmt.Errorf("Services.RailsAPI.InternalURLs is empty")
32         }
33         return best, cluster.TLS.Insecure, nil
34 }
35
36 func NewConn(cluster *arvados.Cluster) *rpc.Conn {
37         url, insecure, err := FindRailsAPI(cluster)
38         if err != nil {
39                 panic(err)
40         }
41         conn := rpc.NewConn(cluster.ClusterID, url, insecure, rpc.PassthroughTokenProvider)
42         // If Rails is running with force_ssl=true, this
43         // "X-Forwarded-Proto: https" header prevents it from
44         // redirecting our internal request to an invalid https URL.
45         conn.SendHeader = http.Header{"X-Forwarded-Proto": []string{"https"}}
46         return conn
47 }