Merge branch '16265-security-updates' into dependabot/bundler/apps/workbench/loofah...
[arvados.git] / lib / controller / integration_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package controller
6
7 import (
8         "bytes"
9         "context"
10         "io"
11         "net"
12         "net/url"
13         "os"
14         "path/filepath"
15
16         "git.arvados.org/arvados.git/lib/boot"
17         "git.arvados.org/arvados.git/lib/config"
18         "git.arvados.org/arvados.git/lib/controller/rpc"
19         "git.arvados.org/arvados.git/lib/service"
20         "git.arvados.org/arvados.git/sdk/go/arvados"
21         "git.arvados.org/arvados.git/sdk/go/arvadosclient"
22         "git.arvados.org/arvados.git/sdk/go/auth"
23         "git.arvados.org/arvados.git/sdk/go/ctxlog"
24         "git.arvados.org/arvados.git/sdk/go/keepclient"
25         check "gopkg.in/check.v1"
26 )
27
28 var _ = check.Suite(&IntegrationSuite{})
29
30 type testCluster struct {
31         super         boot.Supervisor
32         config        arvados.Config
33         controllerURL *url.URL
34 }
35
36 type IntegrationSuite struct {
37         testClusters map[string]*testCluster
38 }
39
40 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
41         if forceLegacyAPI14 {
42                 c.Skip("heavy integration tests don't run with forceLegacyAPI14")
43                 return
44         }
45
46         cwd, _ := os.Getwd()
47         s.testClusters = map[string]*testCluster{
48                 "z1111": nil,
49                 "z2222": nil,
50                 "z3333": nil,
51         }
52         hostport := map[string]string{}
53         for id := range s.testClusters {
54                 hostport[id] = func() string {
55                         // TODO: Instead of expecting random ports on
56                         // 127.0.0.11, 22, 33 to be race-safe, try
57                         // different 127.x.y.z until finding one that
58                         // isn't in use.
59                         ln, err := net.Listen("tcp", ":0")
60                         c.Assert(err, check.IsNil)
61                         ln.Close()
62                         _, port, err := net.SplitHostPort(ln.Addr().String())
63                         c.Assert(err, check.IsNil)
64                         return "127.0.0." + id[3:] + ":" + port
65                 }()
66         }
67         for id := range s.testClusters {
68                 yaml := `Clusters:
69   ` + id + `:
70     Services:
71       Controller:
72         ExternalURL: https://` + hostport[id] + `
73     TLS:
74       Insecure: true
75     Login:
76       # LoginCluster: z1111
77     SystemLogs:
78       Format: text
79     RemoteClusters:
80       z1111:
81         Host: ` + hostport["z1111"] + `
82         Scheme: https
83         Insecure: true
84         Proxy: true
85         ActivateUsers: true
86       z2222:
87         Host: ` + hostport["z2222"] + `
88         Scheme: https
89         Insecure: true
90         Proxy: true
91         ActivateUsers: true
92       z3333:
93         Host: ` + hostport["z3333"] + `
94         Scheme: https
95         Insecure: true
96         Proxy: true
97         ActivateUsers: true
98 `
99                 loader := config.NewLoader(bytes.NewBufferString(yaml), ctxlog.TestLogger(c))
100                 loader.Path = "-"
101                 loader.SkipLegacy = true
102                 loader.SkipAPICalls = true
103                 cfg, err := loader.Load()
104                 c.Assert(err, check.IsNil)
105                 s.testClusters[id] = &testCluster{
106                         super: boot.Supervisor{
107                                 SourcePath:           filepath.Join(cwd, "..", ".."),
108                                 ClusterType:          "test",
109                                 ListenHost:           "127.0.0." + id[3:],
110                                 ControllerAddr:       ":0",
111                                 OwnTemporaryDatabase: true,
112                                 Stderr:               &service.LogPrefixer{Writer: ctxlog.LogWriter(c.Log), Prefix: []byte("[" + id + "] ")},
113                         },
114                         config: *cfg,
115                 }
116                 s.testClusters[id].super.Start(context.Background(), &s.testClusters[id].config)
117         }
118         for _, tc := range s.testClusters {
119                 au, ok := tc.super.WaitReady()
120                 c.Assert(ok, check.Equals, true)
121                 u := url.URL(*au)
122                 tc.controllerURL = &u
123         }
124 }
125
126 func (s *IntegrationSuite) TearDownSuite(c *check.C) {
127         for _, c := range s.testClusters {
128                 c.super.Stop()
129         }
130 }
131
132 func (s *IntegrationSuite) conn(clusterID string) *rpc.Conn {
133         return rpc.NewConn(clusterID, s.testClusters[clusterID].controllerURL, true, rpc.PassthroughTokenProvider)
134 }
135
136 func (s *IntegrationSuite) clientsWithToken(clusterID string, token string) (context.Context, *arvados.Client, *keepclient.KeepClient) {
137         cl := s.testClusters[clusterID].config.Clusters[clusterID]
138         ctx := auth.NewContext(context.Background(), auth.NewCredentials(token))
139         ac, err := arvados.NewClientFromConfig(&cl)
140         if err != nil {
141                 panic(err)
142         }
143         ac.AuthToken = token
144         arv, err := arvadosclient.New(ac)
145         if err != nil {
146                 panic(err)
147         }
148         kc := keepclient.New(arv)
149         return ctx, ac, kc
150 }
151
152 func (s *IntegrationSuite) userClients(c *check.C, conn *rpc.Conn, rootctx context.Context, clusterID string, activate bool) (context.Context, *arvados.Client, *keepclient.KeepClient) {
153         login, err := conn.UserSessionCreate(rootctx, rpc.UserSessionCreateOptions{
154                 ReturnTo: ",https://example.com",
155                 AuthInfo: rpc.UserSessionAuthInfo{
156                         Email:     "user@example.com",
157                         FirstName: "Example",
158                         LastName:  "User",
159                         Username:  "example",
160                 },
161         })
162         c.Assert(err, check.IsNil)
163         redirURL, err := url.Parse(login.RedirectLocation)
164         c.Assert(err, check.IsNil)
165         userToken := redirURL.Query().Get("api_token")
166         c.Logf("user token: %q", userToken)
167         ctx, ac, kc := s.clientsWithToken(clusterID, userToken)
168         user, err := conn.UserGetCurrent(ctx, arvados.GetOptions{})
169         c.Assert(err, check.IsNil)
170         _, err = conn.UserSetup(rootctx, arvados.UserSetupOptions{UUID: user.UUID})
171         c.Assert(err, check.IsNil)
172         if activate {
173                 _, err = conn.UserActivate(rootctx, arvados.UserActivateOptions{UUID: user.UUID})
174                 c.Assert(err, check.IsNil)
175                 user, err = conn.UserGetCurrent(ctx, arvados.GetOptions{})
176                 c.Assert(err, check.IsNil)
177                 c.Logf("user UUID: %q", user.UUID)
178                 if !user.IsActive {
179                         c.Fatalf("failed to activate user -- %#v", user)
180                 }
181         }
182         return ctx, ac, kc
183 }
184
185 func (s *IntegrationSuite) rootClients(clusterID string) (context.Context, *arvados.Client, *keepclient.KeepClient) {
186         return s.clientsWithToken(clusterID, s.testClusters[clusterID].config.Clusters[clusterID].SystemRootToken)
187 }
188
189 func (s *IntegrationSuite) TestGetCollectionByPDH(c *check.C) {
190         conn1 := s.conn("z1111")
191         rootctx1, _, _ := s.rootClients("z1111")
192         conn3 := s.conn("z3333")
193         userctx1, ac1, kc1 := s.userClients(c, conn1, rootctx1, "z1111", true)
194
195         // Create the collection to find its PDH (but don't save it
196         // anywhere yet)
197         var coll1 arvados.Collection
198         fs1, err := coll1.FileSystem(ac1, kc1)
199         c.Assert(err, check.IsNil)
200         f, err := fs1.OpenFile("test.txt", os.O_CREATE|os.O_RDWR, 0777)
201         c.Assert(err, check.IsNil)
202         _, err = io.WriteString(f, "IntegrationSuite.TestGetCollectionByPDH")
203         c.Assert(err, check.IsNil)
204         err = f.Close()
205         c.Assert(err, check.IsNil)
206         mtxt, err := fs1.MarshalManifest(".")
207         c.Assert(err, check.IsNil)
208         pdh := arvados.PortableDataHash(mtxt)
209
210         // Looking up the PDH before saving returns 404 if cycle
211         // detection is working.
212         _, err = conn1.CollectionGet(userctx1, arvados.GetOptions{UUID: pdh})
213         c.Assert(err, check.ErrorMatches, `.*404 Not Found.*`)
214
215         // Save the collection on cluster z1111.
216         coll1, err = conn1.CollectionCreate(userctx1, arvados.CreateOptions{Attrs: map[string]interface{}{
217                 "manifest_text": mtxt,
218         }})
219         c.Assert(err, check.IsNil)
220
221         // Retrieve the collection from cluster z3333.
222         coll, err := conn3.CollectionGet(userctx1, arvados.GetOptions{UUID: pdh})
223         c.Check(err, check.IsNil)
224         c.Check(coll.PortableDataHash, check.Equals, pdh)
225 }