Merge branch '12690-12748-crunchstat-summary'
[arvados.git] / services / keepstore / proxy_remote_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "crypto/md5"
9         "encoding/json"
10         "fmt"
11         "net"
12         "net/http"
13         "net/http/httptest"
14         "strconv"
15         "strings"
16         "sync/atomic"
17         "time"
18
19         "git.curoverse.com/arvados.git/sdk/go/arvados"
20         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
21         "git.curoverse.com/arvados.git/sdk/go/auth"
22         "git.curoverse.com/arvados.git/sdk/go/keepclient"
23         check "gopkg.in/check.v1"
24 )
25
26 var _ = check.Suite(&ProxyRemoteSuite{})
27
28 type ProxyRemoteSuite struct {
29         cluster *arvados.Cluster
30         vm      VolumeManager
31         rtr     http.Handler
32
33         remoteClusterID      string
34         remoteBlobSigningKey []byte
35         remoteKeepLocator    string
36         remoteKeepData       []byte
37         remoteKeepproxy      *httptest.Server
38         remoteKeepRequests   int64
39         remoteAPI            *httptest.Server
40 }
41
42 func (s *ProxyRemoteSuite) remoteKeepproxyHandler(w http.ResponseWriter, r *http.Request) {
43         expectToken, err := auth.SaltToken(arvadostest.ActiveTokenV2, s.remoteClusterID)
44         if err != nil {
45                 panic(err)
46         }
47         atomic.AddInt64(&s.remoteKeepRequests, 1)
48         var token string
49         if auth := strings.Split(r.Header.Get("Authorization"), " "); len(auth) == 2 && (auth[0] == "OAuth2" || auth[0] == "Bearer") {
50                 token = auth[1]
51         }
52         if r.Method == "GET" && r.URL.Path == "/"+s.remoteKeepLocator && token == expectToken {
53                 w.Write(s.remoteKeepData)
54                 return
55         }
56         http.Error(w, "404", 404)
57 }
58
59 func (s *ProxyRemoteSuite) remoteAPIHandler(w http.ResponseWriter, r *http.Request) {
60         host, port, _ := net.SplitHostPort(strings.Split(s.remoteKeepproxy.URL, "//")[1])
61         portnum, _ := strconv.Atoi(port)
62         if r.URL.Path == "/arvados/v1/discovery/v1/rest" {
63                 json.NewEncoder(w).Encode(arvados.DiscoveryDocument{})
64                 return
65         }
66         if r.URL.Path == "/arvados/v1/keep_services/accessible" {
67                 json.NewEncoder(w).Encode(arvados.KeepServiceList{
68                         Items: []arvados.KeepService{
69                                 {
70                                         UUID:           s.remoteClusterID + "-bi6l4-proxyproxyproxy",
71                                         ServiceType:    "proxy",
72                                         ServiceHost:    host,
73                                         ServicePort:    portnum,
74                                         ServiceSSLFlag: false,
75                                 },
76                         },
77                 })
78                 return
79         }
80         http.Error(w, "404", 404)
81 }
82
83 func (s *ProxyRemoteSuite) SetUpTest(c *check.C) {
84         s.remoteClusterID = "z0000"
85         s.remoteBlobSigningKey = []byte("3b6df6fb6518afe12922a5bc8e67bf180a358bc8")
86         s.remoteKeepproxy = httptest.NewServer(http.HandlerFunc(s.remoteKeepproxyHandler))
87         s.remoteAPI = httptest.NewUnstartedServer(http.HandlerFunc(s.remoteAPIHandler))
88         s.remoteAPI.StartTLS()
89         s.cluster = arvados.IntegrationTestCluster()
90         s.cluster.RemoteClusters = map[string]arvados.RemoteCluster{
91                 s.remoteClusterID: arvados.RemoteCluster{
92                         Host:     strings.Split(s.remoteAPI.URL, "//")[1],
93                         Proxy:    true,
94                         Scheme:   "http",
95                         Insecure: true,
96                 },
97         }
98         s.vm = MakeTestVolumeManager(2)
99         KeepVM = s.vm
100         theConfig = DefaultConfig()
101         theConfig.systemAuthToken = arvadostest.DataManagerToken
102         theConfig.Start()
103         s.rtr = MakeRESTRouter(s.cluster)
104 }
105
106 func (s *ProxyRemoteSuite) TearDownTest(c *check.C) {
107         s.vm.Close()
108         KeepVM = nil
109         theConfig = DefaultConfig()
110         theConfig.Start()
111         s.remoteAPI.Close()
112         s.remoteKeepproxy.Close()
113 }
114
115 func (s *ProxyRemoteSuite) TestProxyRemote(c *check.C) {
116         data := []byte("foo bar")
117         s.remoteKeepData = data
118         locator := fmt.Sprintf("%x+%d", md5.Sum(data), len(data))
119         s.remoteKeepLocator = keepclient.SignLocator(locator, arvadostest.ActiveTokenV2, time.Now().Add(time.Minute), time.Minute, s.remoteBlobSigningKey)
120
121         path := "/" + strings.Replace(s.remoteKeepLocator, "+A", "+R"+s.remoteClusterID+"-", 1)
122
123         var req *http.Request
124         var resp *httptest.ResponseRecorder
125         tryWithToken := func(token string) {
126                 req = httptest.NewRequest("GET", path, nil)
127                 req.Header.Set("Authorization", "Bearer "+token)
128                 resp = httptest.NewRecorder()
129                 s.rtr.ServeHTTP(resp, req)
130         }
131
132         // Happy path
133         tryWithToken(arvadostest.ActiveTokenV2)
134         c.Check(s.remoteKeepRequests, check.Equals, int64(1))
135         c.Check(resp.Code, check.Equals, http.StatusOK)
136         c.Check(resp.Body.String(), check.Equals, string(data))
137
138         // Obsolete token
139         tryWithToken(arvadostest.ActiveToken)
140         c.Check(s.remoteKeepRequests, check.Equals, int64(1))
141         c.Check(resp.Code, check.Equals, http.StatusBadRequest)
142         c.Check(resp.Body.String(), check.Not(check.Equals), string(data))
143
144         // Bad token
145         tryWithToken(arvadostest.ActiveTokenV2[:len(arvadostest.ActiveTokenV2)-3] + "xxx")
146         c.Check(s.remoteKeepRequests, check.Equals, int64(2))
147         c.Check(resp.Code, check.Equals, http.StatusNotFound)
148         c.Check(resp.Body.String(), check.Not(check.Equals), string(data))
149 }