Merge branch '15133-remove-jobs-api' refs #15133
[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         "context"
11         "errors"
12         "fmt"
13         "net/url"
14         "strings"
15
16         "git.curoverse.com/arvados.git/lib/controller/rpc"
17         "git.curoverse.com/arvados.git/sdk/go/arvados"
18         "git.curoverse.com/arvados.git/sdk/go/auth"
19 )
20
21 // For now, FindRailsAPI always uses the rails API running on this
22 // node.
23 func FindRailsAPI(cluster *arvados.Cluster) (*url.URL, bool, error) {
24         var best *url.URL
25         for target := range cluster.Services.RailsAPI.InternalURLs {
26                 target := url.URL(target)
27                 best = &target
28                 if strings.HasPrefix(target.Host, "localhost:") || strings.HasPrefix(target.Host, "127.0.0.1:") || strings.HasPrefix(target.Host, "[::1]:") {
29                         break
30                 }
31         }
32         if best == nil {
33                 return nil, false, fmt.Errorf("Services.RailsAPI.InternalURLs is empty")
34         }
35         return best, cluster.TLS.Insecure, nil
36 }
37
38 func NewConn(cluster *arvados.Cluster) *rpc.Conn {
39         url, insecure, err := FindRailsAPI(cluster)
40         if err != nil {
41                 panic(err)
42         }
43         return rpc.NewConn(cluster.ClusterID, url, insecure, provideIncomingToken)
44 }
45
46 func provideIncomingToken(ctx context.Context) ([]string, error) {
47         incoming, ok := auth.FromContext(ctx)
48         if !ok {
49                 return nil, errors.New("no token provided")
50         }
51         return incoming.Tokens, nil
52 }