1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
19 "git.curoverse.com/arvados.git/sdk/go/arvados"
20 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
21 check "gopkg.in/check.v1"
24 func (s *IntegrationSuite) TestCadaverHTTPAuth(c *check.C) {
25 s.testCadaver(c, arvadostest.ActiveToken, func(newUUID string) (string, string, string) {
26 r := "/c=" + arvadostest.FooAndBarFilesInDirUUID + "/"
27 w := "/c=" + newUUID + "/"
28 pdh := "/c=" + strings.Replace(arvadostest.FooAndBarFilesInDirPDH, "+", "-", -1) + "/"
33 func (s *IntegrationSuite) TestCadaverPathAuth(c *check.C) {
34 s.testCadaver(c, "", func(newUUID string) (string, string, string) {
35 r := "/c=" + arvadostest.FooAndBarFilesInDirUUID + "/t=" + arvadostest.ActiveToken + "/"
36 w := "/c=" + newUUID + "/t=" + arvadostest.ActiveToken + "/"
37 pdh := "/c=" + strings.Replace(arvadostest.FooAndBarFilesInDirPDH, "+", "-", -1) + "/t=" + arvadostest.ActiveToken + "/"
42 func (s *IntegrationSuite) testCadaver(c *check.C, password string, pathFunc func(string) (string, string, string)) {
43 testdata := []byte("the human tragedy consists in the necessity of living with the consequences of actions performed under the pressure of compulsions we do not understand")
45 tempdir, err := ioutil.TempDir("", "keep-web-test-")
46 c.Assert(err, check.IsNil)
47 defer os.RemoveAll(tempdir)
49 localfile, err := ioutil.TempFile(tempdir, "localfile")
50 c.Assert(err, check.IsNil)
51 localfile.Write(testdata)
53 emptyfile, err := ioutil.TempFile(tempdir, "emptyfile")
54 c.Assert(err, check.IsNil)
56 checkfile, err := ioutil.TempFile(tempdir, "checkfile")
57 c.Assert(err, check.IsNil)
59 var newCollection arvados.Collection
60 arv := arvados.NewClientFromEnv()
61 arv.AuthToken = arvadostest.ActiveToken
62 err = arv.RequestAndDecode(&newCollection, "POST", "/arvados/v1/collections", bytes.NewBufferString(url.Values{"collection": {"{}"}}.Encode()), nil)
63 c.Assert(err, check.IsNil)
65 readPath, writePath, pdhPath := pathFunc(newCollection.UUID)
67 matchToday := time.Now().Format("Jan +2")
69 type testcase struct {
75 for _, trial := range []testcase{
79 match: `(?ms).*dir1 *0 .*`,
84 match: `(?ms).*bar *3.*foo *3 .*`,
87 path: readPath + "_/dir1",
89 match: `(?ms).*bar *3.*foo *3 .*`,
92 path: readPath + "dir1/",
94 match: `(?ms).*bar *3.*foo +3 +Feb +\d+ +2014.*`,
98 cmd: "get emptyfile '" + checkfile.Name() + "'\n",
99 match: `(?ms).*Not Found.*`,
103 cmd: "put '" + emptyfile.Name() + "' emptyfile\n",
104 match: `(?ms).*Uploading .* succeeded.*`,
108 cmd: "get emptyfile '" + checkfile.Name() + "'\n",
109 match: `(?ms).*Downloading .* succeeded.*`,
114 cmd: "put '" + localfile.Name() + "' testfile\n",
115 match: `(?ms).*Uploading .* succeeded.*`,
119 cmd: "get testfile '" + checkfile.Name() + "'\n",
120 match: `(?ms).*succeeded.*`,
125 cmd: "move testfile newdir0/\n",
126 match: `(?ms).*Moving .* succeeded.*`,
130 cmd: "move testfile newdir0/\n",
131 match: `(?ms).*Moving .* failed.*`,
136 match: `(?ms).*newdir0.* 0 +` + matchToday + ` \d+:\d+\n.*`,
140 cmd: "move newdir0/testfile emptyfile/bogus/\n",
141 match: `(?ms).*Moving .* failed.*`,
145 cmd: "mkcol newdir1\n",
146 match: `(?ms).*Creating .* succeeded.*`,
150 cmd: "move newdir0/testfile newdir1/\n",
151 match: `(?ms).*Moving .* succeeded.*`,
155 cmd: "move newdir1 newdir1/\n",
156 match: `(?ms).*Moving .* failed.*`,
160 cmd: "get newdir1/testfile '" + checkfile.Name() + "'\n",
161 match: `(?ms).*succeeded.*`,
166 cmd: "put '" + localfile.Name() + "' newdir1/testfile1\n",
167 match: `(?ms).*Uploading .* succeeded.*`,
171 cmd: "mkcol newdir2\n",
172 match: `(?ms).*Creating .* succeeded.*`,
176 cmd: "put '" + localfile.Name() + "' newdir2/testfile2\n",
177 match: `(?ms).*Uploading .* succeeded.*`,
181 cmd: "copy newdir2/testfile2 testfile3\n",
182 match: `(?ms).*succeeded.*`,
186 cmd: "get testfile3 '" + checkfile.Name() + "'\n",
187 match: `(?ms).*succeeded.*`,
192 cmd: "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
193 match: `(?ms).*succeeded.*`,
198 cmd: "rmcol newdir2\n",
199 match: `(?ms).*Deleting collection .* succeeded.*`,
203 cmd: "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
204 match: `(?ms).*Downloading .* failed.*`,
207 path: "/c=" + arvadostest.UserAgreementCollection + "/t=" + arv.AuthToken + "/",
208 cmd: "put '" + localfile.Name() + "' foo\n",
209 match: `(?ms).*Uploading .* failed:.*403 Forbidden.*`,
213 cmd: "put '" + localfile.Name() + "' foo\n",
214 match: `(?ms).*Uploading .* failed:.*405 Method Not Allowed.*`,
218 cmd: "move foo bar\n",
219 match: `(?ms).*Moving .* failed:.*405 Method Not Allowed.*`,
223 cmd: "copy foo bar\n",
224 match: `(?ms).*Copying .* failed:.*405 Method Not Allowed.*`,
229 match: `(?ms).*Deleting .* failed:.*405 Method Not Allowed.*`,
232 c.Logf("%s %+v", "http://"+s.testServer.Addr, trial)
234 os.Remove(checkfile.Name())
236 cmd := exec.Command("cadaver", "http://"+s.testServer.Addr+trial.path)
238 // cadaver won't try username/password
239 // authentication unless the server responds
240 // 401 to an unauthenticated request, which it
241 // only does in AttachmentOnlyHost,
242 // TrustAllContent, and per-collection vhost
244 s.testServer.Config.AttachmentOnlyHost = s.testServer.Addr
246 cmd.Env = append(os.Environ(), "HOME="+tempdir)
247 f, err := os.OpenFile(filepath.Join(tempdir, ".netrc"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
248 c.Assert(err, check.IsNil)
249 _, err = fmt.Fprintf(f, "default login none password %s\n", password)
250 c.Assert(err, check.IsNil)
251 c.Assert(f.Close(), check.IsNil)
253 cmd.Stdin = bytes.NewBufferString(trial.cmd)
254 stdout, err := cmd.StdoutPipe()
255 c.Assert(err, check.Equals, nil)
256 cmd.Stderr = cmd.Stdout
260 _, err = io.Copy(&buf, stdout)
261 c.Check(err, check.Equals, nil)
263 c.Check(err, check.Equals, nil)
264 c.Check(buf.String(), check.Matches, trial.match)
266 if trial.data == nil {
269 checkfile, err = os.Open(checkfile.Name())
270 c.Assert(err, check.IsNil)
271 checkfile.Seek(0, os.SEEK_SET)
272 got, err := ioutil.ReadAll(checkfile)
273 c.Check(got, check.DeepEquals, trial.data)
274 c.Check(err, check.IsNil)