1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
16 "git.curoverse.com/arvados.git/sdk/go/arvados"
17 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
18 "git.curoverse.com/arvados.git/sdk/go/httpserver"
19 "git.curoverse.com/arvados.git/sdk/go/keepclient"
20 "github.com/Sirupsen/logrus"
21 check "gopkg.in/check.v1"
24 // Gocheck boilerplate
25 var _ = check.Suite(&FederationSuite{})
27 type FederationSuite struct {
29 // testServer and testHandler are the controller being tested,
31 testServer *httpserver.Server
33 // remoteServer ("zzzzz") forwards requests to the Rails API
34 // provided by the integration test environment.
35 remoteServer *httpserver.Server
36 // remoteMock ("zmock") appends each incoming request to
37 // remoteMockRequests, and returns an empty 200 response.
38 remoteMock *httpserver.Server
39 remoteMockRequests []http.Request
42 func (s *FederationSuite) SetUpTest(c *check.C) {
44 s.log.Formatter = &logrus.JSONFormatter{}
45 s.log.Out = &logWriter{c.Log}
47 s.remoteServer = newServerFromIntegrationTestEnv(c)
48 c.Assert(s.remoteServer.Start(), check.IsNil)
50 s.remoteMock = newServerFromIntegrationTestEnv(c)
51 s.remoteMock.Server.Handler = http.HandlerFunc(s.remoteMockHandler)
52 c.Assert(s.remoteMock.Start(), check.IsNil)
54 nodeProfile := arvados.NodeProfile{
55 Controller: arvados.SystemServiceInstance{Listen: ":"},
56 RailsAPI: arvados.SystemServiceInstance{Listen: ":1"}, // local reqs will error "connection refused"
58 s.testHandler = &Handler{Cluster: &arvados.Cluster{
60 PostgreSQL: integrationTestCluster().PostgreSQL,
61 NodeProfiles: map[string]arvados.NodeProfile{
64 }, NodeProfile: &nodeProfile}
65 s.testServer = newServerFromIntegrationTestEnv(c)
66 s.testServer.Server.Handler = httpserver.AddRequestIDs(httpserver.LogRequests(s.log, s.testHandler))
68 s.testHandler.Cluster.RemoteClusters = map[string]arvados.RemoteCluster{
70 Host: s.remoteServer.Addr,
75 Host: s.remoteMock.Addr,
81 c.Assert(s.testServer.Start(), check.IsNil)
83 s.remoteMockRequests = nil
86 func (s *FederationSuite) remoteMockHandler(w http.ResponseWriter, req *http.Request) {
87 s.remoteMockRequests = append(s.remoteMockRequests, *req)
90 func (s *FederationSuite) TearDownTest(c *check.C) {
91 if s.remoteServer != nil {
92 s.remoteServer.Close()
94 if s.testServer != nil {
99 func (s *FederationSuite) testRequest(req *http.Request) *http.Response {
100 resp := httptest.NewRecorder()
101 s.testServer.Server.Handler.ServeHTTP(resp, req)
105 func (s *FederationSuite) TestLocalRequest(c *check.C) {
106 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+strings.Replace(arvadostest.WorkflowWithDefinitionYAMLUUID, "zzzzz-", "zhome-", 1), nil)
107 resp := s.testRequest(req)
108 s.checkHandledLocally(c, resp)
111 func (s *FederationSuite) checkHandledLocally(c *check.C, resp *http.Response) {
112 // Our "home" controller can't handle local requests because
113 // it doesn't have its own stub/test Rails API, so we rely on
114 // "connection refused" to indicate the controller tried to
115 // proxy the request to its local Rails API.
116 c.Check(resp.StatusCode, check.Equals, http.StatusBadGateway)
117 s.checkJSONErrorMatches(c, resp, `.*connection refused`)
120 func (s *FederationSuite) TestNoAuth(c *check.C) {
121 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
122 resp := s.testRequest(req)
123 c.Check(resp.StatusCode, check.Equals, http.StatusUnauthorized)
124 s.checkJSONErrorMatches(c, resp, `Not logged in`)
127 func (s *FederationSuite) TestBadAuth(c *check.C) {
128 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
129 req.Header.Set("Authorization", "Bearer aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
130 resp := s.testRequest(req)
131 c.Check(resp.StatusCode, check.Equals, http.StatusUnauthorized)
132 s.checkJSONErrorMatches(c, resp, `Not logged in`)
135 func (s *FederationSuite) TestNoAccess(c *check.C) {
136 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
137 req.Header.Set("Authorization", "Bearer "+arvadostest.SpectatorToken)
138 resp := s.testRequest(req)
139 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
140 s.checkJSONErrorMatches(c, resp, `.*not found`)
143 func (s *FederationSuite) TestGetUnknownRemote(c *check.C) {
144 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+strings.Replace(arvadostest.WorkflowWithDefinitionYAMLUUID, "zzzzz-", "zz404-", 1), nil)
145 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
146 resp := s.testRequest(req)
147 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
148 s.checkJSONErrorMatches(c, resp, `.*no proxy available for cluster zz404`)
151 func (s *FederationSuite) TestRemoteError(c *check.C) {
152 rc := s.testHandler.Cluster.RemoteClusters["zzzzz"]
154 s.testHandler.Cluster.RemoteClusters["zzzzz"] = rc
156 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
157 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
158 resp := s.testRequest(req)
159 c.Check(resp.StatusCode, check.Equals, http.StatusBadGateway)
160 s.checkJSONErrorMatches(c, resp, `.*HTTP response to HTTPS client`)
163 func (s *FederationSuite) TestGetRemoteWorkflow(c *check.C) {
164 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
165 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
166 resp := s.testRequest(req)
167 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
168 var wf arvados.Workflow
169 c.Check(json.NewDecoder(resp.Body).Decode(&wf), check.IsNil)
170 c.Check(wf.UUID, check.Equals, arvadostest.WorkflowWithDefinitionYAMLUUID)
171 c.Check(wf.OwnerUUID, check.Equals, arvadostest.ActiveUserUUID)
174 func (s *FederationSuite) TestOptionsMethod(c *check.C) {
175 req := httptest.NewRequest("OPTIONS", "/arvados/v1/workflows/"+arvadostest.WorkflowWithDefinitionYAMLUUID, nil)
176 req.Header.Set("Origin", "https://example.com")
177 resp := s.testRequest(req)
178 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
179 body, err := ioutil.ReadAll(resp.Body)
180 c.Check(err, check.IsNil)
181 c.Check(string(body), check.Equals, "")
182 c.Check(resp.Header.Get("Access-Control-Allow-Origin"), check.Equals, "*")
183 for _, hdr := range []string{"Authorization", "Content-Type"} {
184 c.Check(resp.Header.Get("Access-Control-Allow-Headers"), check.Matches, ".*"+hdr+".*")
186 for _, method := range []string{"GET", "HEAD", "PUT", "POST", "DELETE"} {
187 c.Check(resp.Header.Get("Access-Control-Allow-Methods"), check.Matches, ".*"+method+".*")
191 func (s *FederationSuite) TestRemoteWithTokenInQuery(c *check.C) {
192 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+strings.Replace(arvadostest.WorkflowWithDefinitionYAMLUUID, "zzzzz-", "zmock-", 1)+"?api_token="+arvadostest.ActiveToken, nil)
194 c.Assert(len(s.remoteMockRequests), check.Equals, 1)
195 pr := s.remoteMockRequests[0]
196 // Token is salted and moved from query to Authorization header.
197 c.Check(pr.URL.String(), check.Not(check.Matches), `.*api_token=.*`)
198 c.Check(pr.Header.Get("Authorization"), check.Equals, "Bearer v2/zzzzz-gj3su-077z32aux8dg2s1/7fd31b61f39c0e82a4155592163218272cedacdc")
201 func (s *FederationSuite) TestLocalTokenSalted(c *check.C) {
202 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+strings.Replace(arvadostest.WorkflowWithDefinitionYAMLUUID, "zzzzz-", "zmock-", 1), nil)
203 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
205 c.Assert(len(s.remoteMockRequests), check.Equals, 1)
206 pr := s.remoteMockRequests[0]
207 // The salted token here has a "zzzzz-" UUID instead of a
208 // "ztest-" UUID because ztest's local database has the
209 // "zzzzz-" test fixtures. The "secret" part is HMAC(sha1,
210 // arvadostest.ActiveToken, "zmock") = "7fd3...".
211 c.Check(pr.Header.Get("Authorization"), check.Equals, "Bearer v2/zzzzz-gj3su-077z32aux8dg2s1/7fd31b61f39c0e82a4155592163218272cedacdc")
214 func (s *FederationSuite) TestRemoteTokenNotSalted(c *check.C) {
215 // remoteToken can be any v1 token that doesn't appear in
217 remoteToken := "abcdef00000000000000000000000000000000000000000000"
218 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+strings.Replace(arvadostest.WorkflowWithDefinitionYAMLUUID, "zzzzz-", "zmock-", 1), nil)
219 req.Header.Set("Authorization", "Bearer "+remoteToken)
221 c.Assert(len(s.remoteMockRequests), check.Equals, 1)
222 pr := s.remoteMockRequests[0]
223 c.Check(pr.Header.Get("Authorization"), check.Equals, "Bearer "+remoteToken)
226 func (s *FederationSuite) TestWorkflowCRUD(c *check.C) {
227 wf := arvados.Workflow{
228 Description: "TestCRUD",
231 body := &strings.Builder{}
232 json.NewEncoder(body).Encode(&wf)
233 req := httptest.NewRequest("POST", "/arvados/v1/workflows", strings.NewReader(url.Values{
234 "workflow": {body.String()},
236 req.Header.Set("Content-type", "application/x-www-form-urlencoded")
237 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
238 rec := httptest.NewRecorder()
239 s.remoteServer.Server.Handler.ServeHTTP(rec, req) // direct to remote -- can't proxy a create req because no uuid
241 s.checkResponseOK(c, resp)
242 json.NewDecoder(resp.Body).Decode(&wf)
245 req := httptest.NewRequest("DELETE", "/arvados/v1/workflows/"+wf.UUID, nil)
246 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
247 s.remoteServer.Server.Handler.ServeHTTP(httptest.NewRecorder(), req)
249 c.Check(wf.UUID, check.Not(check.Equals), "")
251 c.Assert(wf.ModifiedAt, check.NotNil)
252 c.Logf("wf.ModifiedAt: %v", wf.ModifiedAt)
253 c.Check(time.Since(*wf.ModifiedAt) < time.Minute, check.Equals, true)
255 for _, method := range []string{"PATCH", "PUT", "POST"} {
257 "workflow": {`{"description": "Updated with ` + method + `"}`},
259 if method == "POST" {
260 form["_method"] = []string{"PATCH"}
262 req := httptest.NewRequest(method, "/arvados/v1/workflows/"+wf.UUID, strings.NewReader(form.Encode()))
263 req.Header.Set("Content-type", "application/x-www-form-urlencoded")
264 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
265 resp := s.testRequest(req)
266 s.checkResponseOK(c, resp)
267 err := json.NewDecoder(resp.Body).Decode(&wf)
268 c.Check(err, check.IsNil)
270 c.Check(wf.Description, check.Equals, "Updated with "+method)
273 req := httptest.NewRequest("DELETE", "/arvados/v1/workflows/"+wf.UUID, nil)
274 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
275 resp := s.testRequest(req)
276 s.checkResponseOK(c, resp)
277 err := json.NewDecoder(resp.Body).Decode(&wf)
278 c.Check(err, check.IsNil)
281 req := httptest.NewRequest("GET", "/arvados/v1/workflows/"+wf.UUID, nil)
282 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
283 resp := s.testRequest(req)
284 c.Check(resp.StatusCode, check.Equals, http.StatusNotFound)
288 func (s *FederationSuite) checkResponseOK(c *check.C, resp *http.Response) {
289 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
290 if resp.StatusCode != http.StatusOK {
291 body, err := ioutil.ReadAll(resp.Body)
292 c.Logf("... response body = %q, %v\n", body, err)
296 func (s *FederationSuite) checkJSONErrorMatches(c *check.C, resp *http.Response, re string) {
297 var jresp httpserver.ErrorResponse
298 err := json.NewDecoder(resp.Body).Decode(&jresp)
299 c.Check(err, check.IsNil)
300 c.Assert(len(jresp.Errors), check.Equals, 1)
301 c.Check(jresp.Errors[0], check.Matches, re)
304 func (s *FederationSuite) TestGetRemoteCollection(c *check.C) {
305 req := httptest.NewRequest("GET", "/arvados/v1/collections/"+arvadostest.UserAgreementCollection, nil)
306 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
307 resp := s.testRequest(req)
308 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
309 var col arvados.Collection
310 c.Check(json.NewDecoder(resp.Body).Decode(&col), check.IsNil)
311 c.Check(col.UUID, check.Equals, arvadostest.UserAgreementCollection)
312 c.Check(col.ManifestText, check.Matches,
313 `\. 6a4ff0499484c6c79c95cd8c566bd25f\+249025\+Rzzzzz-[0-9a-f]{40}@[0-9a-f]{8} 0:249025:GNU_General_Public_License,_version_3.pdf
316 // Confirm the regular expression identifies other groups of hints correctly
317 c.Check(keepclient.SignedLocatorRe.FindStringSubmatch(`6a4ff0499484c6c79c95cd8c566bd25f+249025+B1+C2+A05227438989d04712ea9ca1c91b556cef01d5cc7@5ba5405b+D3+E4`),
319 []string{"6a4ff0499484c6c79c95cd8c566bd25f+249025+B1+C2+A05227438989d04712ea9ca1c91b556cef01d5cc7@5ba5405b+D3+E4",
320 "6a4ff0499484c6c79c95cd8c566bd25f",
323 "+A05227438989d04712ea9ca1c91b556cef01d5cc7@5ba5405b",
324 "05227438989d04712ea9ca1c91b556cef01d5cc7", "5ba5405b",