X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/c45edd611595cf03848cf14943d0d44bb297c786..2e727c5d2d000faa6f1d9a566dc59568f1b276fe:/lib/controller/federation_test.go diff --git a/lib/controller/federation_test.go b/lib/controller/federation_test.go index 169b1f7961..a3b198ffc9 100644 --- a/lib/controller/federation_test.go +++ b/lib/controller/federation_test.go @@ -11,6 +11,7 @@ import ( "fmt" "io" "io/ioutil" + "net" "net/http" "net/http/httptest" "net/url" @@ -18,11 +19,11 @@ import ( "strings" "time" - "git.curoverse.com/arvados.git/sdk/go/arvados" - "git.curoverse.com/arvados.git/sdk/go/arvadostest" - "git.curoverse.com/arvados.git/sdk/go/ctxlog" - "git.curoverse.com/arvados.git/sdk/go/httpserver" - "git.curoverse.com/arvados.git/sdk/go/keepclient" + "git.arvados.org/arvados.git/sdk/go/arvados" + "git.arvados.org/arvados.git/sdk/go/arvadostest" + "git.arvados.org/arvados.git/sdk/go/ctxlog" + "git.arvados.org/arvados.git/sdk/go/httpserver" + "git.arvados.org/arvados.git/sdk/go/keepclient" "github.com/sirupsen/logrus" check "gopkg.in/check.v1" ) @@ -57,20 +58,24 @@ func (s *FederationSuite) SetUpTest(c *check.C) { c.Assert(s.remoteMock.Start(), check.IsNil) cluster := &arvados.Cluster{ - ClusterID: "zhome", - PostgreSQL: integrationTestCluster().PostgreSQL, - EnableBetaController14287: enableBetaController14287, + ClusterID: "zhome", + PostgreSQL: integrationTestCluster().PostgreSQL, } cluster.TLS.Insecure = true cluster.API.MaxItemsPerResponse = 1000 cluster.API.MaxRequestAmplification = 4 + cluster.API.RequestTimeout = arvados.Duration(5 * time.Minute) + cluster.Collections.BlobSigning = true + cluster.Collections.BlobSigningKey = arvadostest.BlobSigningKey + cluster.Collections.BlobSigningTTL = arvados.Duration(time.Hour * 24 * 14) arvadostest.SetServiceURL(&cluster.Services.RailsAPI, "http://localhost:1/") arvadostest.SetServiceURL(&cluster.Services.Controller, "http://localhost:/") - s.testHandler = &Handler{Cluster: cluster} + s.testHandler = &Handler{Cluster: cluster, BackgroundContext: ctxlog.Context(context.Background(), s.log)} s.testServer = newServerFromIntegrationTestEnv(c) - s.testServer.Server.Handler = httpserver.HandlerWithContext( - ctxlog.Context(context.Background(), s.log), - httpserver.AddRequestIDs(httpserver.LogRequests(s.testHandler))) + s.testServer.Server.BaseContext = func(net.Listener) context.Context { + return ctxlog.Context(context.Background(), s.log) + } + s.testServer.Server.Handler = httpserver.AddRequestIDs(httpserver.LogRequests(s.testHandler)) cluster.RemoteClusters = map[string]arvados.RemoteCluster{ "zzzzz": { @@ -83,6 +88,9 @@ func (s *FederationSuite) SetUpTest(c *check.C) { Proxy: true, Scheme: "http", }, + "*": { + Scheme: "https", + }, } c.Assert(s.testServer.Start(), check.IsNil) @@ -348,7 +356,13 @@ func (s *FederationSuite) localServiceReturns404(c *check.C) *httpserver.Server return s.localServiceHandler(c, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { if req.URL.Path == "/arvados/v1/api_client_authorizations/current" { if req.Header.Get("Authorization") == "Bearer "+arvadostest.ActiveToken { - json.NewEncoder(w).Encode(arvados.APIClientAuthorization{UUID: arvadostest.ActiveTokenUUID, APIToken: arvadostest.ActiveToken}) + json.NewEncoder(w).Encode(arvados.APIClientAuthorization{UUID: arvadostest.ActiveTokenUUID, APIToken: arvadostest.ActiveToken, Scopes: []string{"all"}}) + } else { + w.WriteHeader(http.StatusUnauthorized) + } + } else if req.URL.Path == "/arvados/v1/users/current" { + if req.Header.Get("Authorization") == "Bearer "+arvadostest.ActiveToken { + json.NewEncoder(w).Encode(arvados.User{UUID: arvadostest.ActiveUserUUID}) } else { w.WriteHeader(http.StatusUnauthorized) } @@ -467,6 +481,10 @@ func (s *FederationSuite) TestGetRemoteCollectionByPDH(c *check.C) { func (s *FederationSuite) TestGetCollectionByPDHError(c *check.C) { defer s.localServiceReturns404(c).Close() + // zmock's normal response (200 with an empty body) would + // change the outcome from 404 to 502 + delete(s.testHandler.Cluster.RemoteClusters, "zmock") + req := httptest.NewRequest("GET", "/arvados/v1/collections/99999999999999999999999999999999+99", nil) req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken) @@ -479,6 +497,10 @@ func (s *FederationSuite) TestGetCollectionByPDHError(c *check.C) { func (s *FederationSuite) TestGetCollectionByPDHErrorBadHash(c *check.C) { defer s.localServiceReturns404(c).Close() + // zmock's normal response (200 with an empty body) would + // change the outcome + delete(s.testHandler.Cluster.RemoteClusters, "zmock") + srv2 := &httpserver.Server{ Server: http.Server{ Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { @@ -512,7 +534,7 @@ func (s *FederationSuite) TestGetCollectionByPDHErrorBadHash(c *check.C) { resp := s.testRequest(req).Result() defer resp.Body.Close() - c.Check(resp.StatusCode, check.Equals, http.StatusNotFound) + c.Check(resp.StatusCode, check.Equals, http.StatusBadGateway) } func (s *FederationSuite) TestSaltedTokenGetCollectionByPDH(c *check.C) { @@ -534,6 +556,10 @@ func (s *FederationSuite) TestSaltedTokenGetCollectionByPDH(c *check.C) { func (s *FederationSuite) TestSaltedTokenGetCollectionByPDHError(c *check.C) { arvadostest.SetServiceURL(&s.testHandler.Cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST")) + // zmock's normal response (200 with an empty body) would + // change the outcome + delete(s.testHandler.Cluster.RemoteClusters, "zmock") + req := httptest.NewRequest("GET", "/arvados/v1/collections/99999999999999999999999999999999+99", nil) req.Header.Set("Authorization", "Bearer v2/zzzzz-gj3su-077z32aux8dg2s1/282d7d172b6cfdce364c5ed12ddf7417b2d00065") resp := s.testRequest(req).Result() @@ -571,6 +597,21 @@ func (s *FederationSuite) TestUpdateRemoteContainerRequest(c *check.C) { setPri(1) // Reset fixture so side effect doesn't break other tests. } +func (s *FederationSuite) TestCreateContainerRequestBadToken(c *check.C) { + defer s.localServiceReturns404(c).Close() + // pass cluster_id via query parameter, this allows arvados-controller + // to avoid parsing the body + req := httptest.NewRequest("POST", "/arvados/v1/container_requests?cluster_id=zzzzz", + strings.NewReader(`{"container_request":{}}`)) + req.Header.Set("Authorization", "Bearer abcdefg") + req.Header.Set("Content-type", "application/json") + resp := s.testRequest(req).Result() + c.Check(resp.StatusCode, check.Equals, http.StatusForbidden) + var e map[string][]string + c.Check(json.NewDecoder(resp.Body).Decode(&e), check.IsNil) + c.Check(e["errors"], check.DeepEquals, []string{"invalid API token"}) +} + func (s *FederationSuite) TestCreateRemoteContainerRequest(c *check.C) { defer s.localServiceReturns404(c).Close() // pass cluster_id via query parameter, this allows arvados-controller @@ -596,38 +637,93 @@ func (s *FederationSuite) TestCreateRemoteContainerRequest(c *check.C) { c.Check(strings.HasPrefix(cr.UUID, "zzzzz-"), check.Equals, true) } +// getCRfromMockRequest returns a ContainerRequest with the content of the +// request sent to the remote mock. This function takes into account the +// Content-Type and acts accordingly. +func (s *FederationSuite) getCRfromMockRequest(c *check.C) arvados.ContainerRequest { + + // Body can be a json formated or something like: + // cluster_id=zmock&container_request=%7B%22command%22%3A%5B%22abc%22%5D%2C%22container_image%22%3A%22123%22%2C%22...7D + // or: + // "{\"container_request\":{\"command\":[\"abc\"],\"container_image\":\"12...Uncommitted\"}}" + + var cr arvados.ContainerRequest + data, err := ioutil.ReadAll(s.remoteMockRequests[0].Body) + c.Check(err, check.IsNil) + + if s.remoteMockRequests[0].Header.Get("Content-Type") == "application/json" { + // legacy code path sends a JSON request body + var answerCR struct { + ContainerRequest arvados.ContainerRequest `json:"container_request"` + } + c.Check(json.Unmarshal(data, &answerCR), check.IsNil) + cr = answerCR.ContainerRequest + } else if s.remoteMockRequests[0].Header.Get("Content-Type") == "application/x-www-form-urlencoded" { + // new code path sends a form-encoded request body with a JSON-encoded parameter value + decodedValue, err := url.ParseQuery(string(data)) + c.Check(err, check.IsNil) + decodedValueCR := decodedValue.Get("container_request") + c.Check(json.Unmarshal([]byte(decodedValueCR), &cr), check.IsNil) + } else { + // mock needs to have Content-Type that we can parse. + c.Fail() + } + + return cr +} + func (s *FederationSuite) TestCreateRemoteContainerRequestCheckRuntimeToken(c *check.C) { // Send request to zmock and check that outgoing request has // runtime_token set with a new random v2 token. defer s.localServiceReturns404(c).Close() - // pass cluster_id via query parameter, this allows arvados-controller - // to avoid parsing the body req := httptest.NewRequest("POST", "/arvados/v1/container_requests?cluster_id=zmock", strings.NewReader(`{ - "container_request": { - "name": "hello world", - "state": "Uncommitted", - "output_path": "/", - "container_image": "123", - "command": ["abc"] - } -} -`)) + "container_request": { + "name": "hello world", + "state": "Uncommitted", + "output_path": "/", + "container_image": "123", + "command": ["abc"] + } + } + `)) req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveTokenV2) req.Header.Set("Content-type", "application/json") + // We replace zhome with zzzzz values (RailsAPI, ClusterID, SystemRootToken) + // SystemRoot token is needed because we check the + // https://[RailsAPI]/arvados/v1/api_client_authorizations/current + // https://[RailsAPI]/arvados/v1/users/current and + // https://[RailsAPI]/auth/controller/callback arvadostest.SetServiceURL(&s.testHandler.Cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST")) s.testHandler.Cluster.ClusterID = "zzzzz" + s.testHandler.Cluster.SystemRootToken = arvadostest.SystemRootToken + s.testHandler.Cluster.API.MaxTokenLifetime = arvados.Duration(time.Hour) resp := s.testRequest(req).Result() c.Check(resp.StatusCode, check.Equals, http.StatusOK) - var cr struct { - arvados.ContainerRequest `json:"container_request"` - } - c.Check(json.NewDecoder(s.remoteMockRequests[0].Body).Decode(&cr), check.IsNil) - c.Check(strings.HasPrefix(cr.ContainerRequest.RuntimeToken, "v2/zzzzz-gj3su-"), check.Equals, true) - c.Check(cr.ContainerRequest.RuntimeToken, check.Not(check.Equals), arvadostest.ActiveTokenV2) + + cr := s.getCRfromMockRequest(c) + + // Runtime token must match zzzzz cluster + c.Check(cr.RuntimeToken, check.Matches, "v2/zzzzz-gj3su-.*") + + // RuntimeToken must be different than the Original Token we originally did the request with. + c.Check(cr.RuntimeToken, check.Not(check.Equals), arvadostest.ActiveTokenV2) + + // Runtime token should not have an expiration based on API.MaxTokenLifetime + req2 := httptest.NewRequest("GET", "/arvados/v1/api_client_authorizations/current", nil) + req2.Header.Set("Authorization", "Bearer "+cr.RuntimeToken) + req2.Header.Set("Content-type", "application/json") + resp = s.testRequest(req2).Result() + c.Check(resp.StatusCode, check.Equals, http.StatusOK) + var aca arvados.APIClientAuthorization + c.Check(json.NewDecoder(resp.Body).Decode(&aca), check.IsNil) + c.Check(aca.ExpiresAt, check.NotNil) // Time.Now()+BlobSigningTTL + t := aca.ExpiresAt + c.Check(t.After(time.Now().Add(s.testHandler.Cluster.API.MaxTokenLifetime.Duration())), check.Equals, true) + c.Check(t.Before(time.Now().Add(s.testHandler.Cluster.Collections.BlobSigningTTL.Duration())), check.Equals, true) } func (s *FederationSuite) TestCreateRemoteContainerRequestCheckSetRuntimeToken(c *check.C) { @@ -639,54 +735,25 @@ func (s *FederationSuite) TestCreateRemoteContainerRequestCheckSetRuntimeToken(c // to avoid parsing the body req := httptest.NewRequest("POST", "/arvados/v1/container_requests?cluster_id=zmock", strings.NewReader(`{ - "container_request": { - "name": "hello world", - "state": "Uncommitted", - "output_path": "/", - "container_image": "123", - "command": ["abc"], - "runtime_token": "xyz" - } -} -`)) + "container_request": { + "name": "hello world", + "state": "Uncommitted", + "output_path": "/", + "container_image": "123", + "command": ["abc"], + "runtime_token": "xyz" + } + } + `)) req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken) req.Header.Set("Content-type", "application/json") resp := s.testRequest(req).Result() c.Check(resp.StatusCode, check.Equals, http.StatusOK) - var cr struct { - arvados.ContainerRequest `json:"container_request"` - } - c.Check(json.NewDecoder(s.remoteMockRequests[0].Body).Decode(&cr), check.IsNil) - c.Check(cr.ContainerRequest.RuntimeToken, check.Equals, "xyz") -} -func (s *FederationSuite) TestCreateRemoteContainerRequestRuntimeTokenFromAuth(c *check.C) { - // Send request to zmock and check that outgoing request has - // runtime_token set using the Auth token because the user is remote. + cr := s.getCRfromMockRequest(c) - defer s.localServiceReturns404(c).Close() - // pass cluster_id via query parameter, this allows arvados-controller - // to avoid parsing the body - req := httptest.NewRequest("POST", "/arvados/v1/container_requests?cluster_id=zmock", - strings.NewReader(`{ - "container_request": { - "name": "hello world", - "state": "Uncommitted", - "output_path": "/", - "container_image": "123", - "command": ["abc"] - } -} -`)) - req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveTokenV2+"/zzzzz-dz642-parentcontainer") - req.Header.Set("Content-type", "application/json") - resp := s.testRequest(req).Result() - c.Check(resp.StatusCode, check.Equals, http.StatusOK) - var cr struct { - arvados.ContainerRequest `json:"container_request"` - } - c.Check(json.NewDecoder(s.remoteMockRequests[0].Body).Decode(&cr), check.IsNil) - c.Check(cr.ContainerRequest.RuntimeToken, check.Equals, arvadostest.ActiveTokenV2) + // After mocking around now making sure the runtime_token we sent is still there. + c.Check(cr.RuntimeToken, check.Equals, "xyz") } func (s *FederationSuite) TestCreateRemoteContainerRequestError(c *check.C) { @@ -789,7 +856,7 @@ func (s *FederationSuite) TestListMultiRemoteContainersPaged(c *check.C) { w.WriteHeader(200) w.Write([]byte(`{"kind": "arvados#containerList", "items": [{"uuid": "zhome-xvhdp-cr6queuedcontnr", "command": ["efg"]}]}`)) } - callCount += 1 + callCount++ })).Close() req := httptest.NewRequest("GET", fmt.Sprintf("/arvados/v1/containers?count=none&filters=%s", url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v", "zhome-xvhdp-cr5queuedcontnr", "zhome-xvhdp-cr6queuedcontnr"]]]`, @@ -825,7 +892,7 @@ func (s *FederationSuite) TestListMultiRemoteContainersMissing(c *check.C) { w.WriteHeader(200) w.Write([]byte(`{"kind": "arvados#containerList", "items": []}`)) } - callCount += 1 + callCount++ })).Close() req := httptest.NewRequest("GET", fmt.Sprintf("/arvados/v1/containers?count=none&filters=%s", url.QueryEscape(fmt.Sprintf(`[["uuid", "in", ["%v", "zhome-xvhdp-cr5queuedcontnr", "zhome-xvhdp-cr6queuedcontnr"]]]`,