17755: Merge branch 'main' into 17755-add-singularity-to-compute-image
[arvados.git] / lib / controller / localdb / collection_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package localdb
6
7 import (
8         "context"
9         "regexp"
10         "strconv"
11         "time"
12
13         "git.arvados.org/arvados.git/lib/config"
14         "git.arvados.org/arvados.git/lib/controller/rpc"
15         "git.arvados.org/arvados.git/sdk/go/arvados"
16         "git.arvados.org/arvados.git/sdk/go/arvadostest"
17         "git.arvados.org/arvados.git/sdk/go/auth"
18         "git.arvados.org/arvados.git/sdk/go/ctxlog"
19         check "gopkg.in/check.v1"
20 )
21
22 var _ = check.Suite(&CollectionSuite{})
23
24 type CollectionSuite struct {
25         cluster  *arvados.Cluster
26         localdb  *Conn
27         railsSpy *arvadostest.Proxy
28 }
29
30 func (s *CollectionSuite) TearDownSuite(c *check.C) {
31         // Undo any changes/additions to the user database so they
32         // don't affect subsequent tests.
33         arvadostest.ResetEnv()
34         c.Check(arvados.NewClientFromEnv().RequestAndDecode(nil, "POST", "database/reset", nil, nil), check.IsNil)
35 }
36
37 func (s *CollectionSuite) SetUpTest(c *check.C) {
38         cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
39         c.Assert(err, check.IsNil)
40         s.cluster, err = cfg.GetCluster("")
41         c.Assert(err, check.IsNil)
42         s.localdb = NewConn(s.cluster)
43         s.railsSpy = arvadostest.NewProxy(c, s.cluster.Services.RailsAPI)
44         *s.localdb.railsProxy = *rpc.NewConn(s.cluster.ClusterID, s.railsSpy.URL, true, rpc.PassthroughTokenProvider)
45 }
46
47 func (s *CollectionSuite) TearDownTest(c *check.C) {
48         s.railsSpy.Close()
49 }
50
51 func (s *CollectionSuite) TestSignatures(c *check.C) {
52         ctx := auth.NewContext(context.Background(), &auth.Credentials{Tokens: []string{arvadostest.ActiveTokenV2}})
53
54         resp, err := s.localdb.CollectionGet(ctx, arvados.GetOptions{UUID: arvadostest.FooCollection})
55         c.Check(err, check.IsNil)
56         c.Check(resp.ManifestText, check.Matches, `(?ms).* acbd[^ ]*\+3\+A[0-9a-f]+@[0-9a-f]+ 0:.*`)
57         s.checkSignatureExpiry(c, resp.ManifestText, time.Hour*24*7*2)
58
59         resp, err = s.localdb.CollectionGet(ctx, arvados.GetOptions{UUID: arvadostest.FooCollection, Select: []string{"manifest_text"}})
60         c.Check(err, check.IsNil)
61         c.Check(resp.ManifestText, check.Matches, `(?ms).* acbd[^ ]*\+3\+A[0-9a-f]+@[0-9a-f]+ 0:.*`)
62
63         lresp, err := s.localdb.CollectionList(ctx, arvados.ListOptions{Limit: -1, Filters: []arvados.Filter{{"uuid", "=", arvadostest.FooCollection}}})
64         c.Check(err, check.IsNil)
65         if c.Check(lresp.Items, check.HasLen, 1) {
66                 c.Check(lresp.Items[0].UUID, check.Equals, arvadostest.FooCollection)
67                 c.Check(lresp.Items[0].ManifestText, check.Equals, "")
68                 c.Check(lresp.Items[0].UnsignedManifestText, check.Equals, "")
69         }
70
71         lresp, err = s.localdb.CollectionList(ctx, arvados.ListOptions{Limit: -1, Filters: []arvados.Filter{{"uuid", "=", arvadostest.FooCollection}}, Select: []string{"manifest_text"}})
72         c.Check(err, check.IsNil)
73         if c.Check(lresp.Items, check.HasLen, 1) {
74                 c.Check(lresp.Items[0].ManifestText, check.Matches, `(?ms).* acbd[^ ]*\+3\+A[0-9a-f]+@[0-9a-f]+ 0:.*`)
75                 c.Check(lresp.Items[0].UnsignedManifestText, check.Equals, "")
76         }
77
78         lresp, err = s.localdb.CollectionList(ctx, arvados.ListOptions{Limit: -1, Filters: []arvados.Filter{{"uuid", "=", arvadostest.FooCollection}}, Select: []string{"unsigned_manifest_text"}})
79         c.Check(err, check.IsNil)
80         if c.Check(lresp.Items, check.HasLen, 1) {
81                 c.Check(lresp.Items[0].ManifestText, check.Equals, "")
82                 c.Check(lresp.Items[0].UnsignedManifestText, check.Matches, `(?ms).* acbd[^ ]*\+3 0:.*`)
83         }
84
85         // early trash date causes lower signature TTL (even if
86         // trash_at and is_trashed fields are unselected)
87         trashed, err := s.localdb.CollectionCreate(ctx, arvados.CreateOptions{
88                 Select: []string{"uuid", "manifest_text"},
89                 Attrs: map[string]interface{}{
90                         "manifest_text": ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo\n",
91                         "trash_at":      time.Now().UTC().Add(time.Hour),
92                 }})
93         c.Assert(err, check.IsNil)
94         s.checkSignatureExpiry(c, trashed.ManifestText, time.Hour)
95         resp, err = s.localdb.CollectionGet(ctx, arvados.GetOptions{UUID: trashed.UUID})
96         c.Assert(err, check.IsNil)
97         s.checkSignatureExpiry(c, resp.ManifestText, time.Hour)
98
99         // distant future trash date does not cause higher signature TTL
100         trashed, err = s.localdb.CollectionUpdate(ctx, arvados.UpdateOptions{
101                 UUID: trashed.UUID,
102                 Attrs: map[string]interface{}{
103                         "trash_at": time.Now().UTC().Add(time.Hour * 24 * 365),
104                 }})
105         c.Assert(err, check.IsNil)
106         s.checkSignatureExpiry(c, trashed.ManifestText, time.Hour*24*7*2)
107         resp, err = s.localdb.CollectionGet(ctx, arvados.GetOptions{UUID: trashed.UUID})
108         c.Assert(err, check.IsNil)
109         s.checkSignatureExpiry(c, resp.ManifestText, time.Hour*24*7*2)
110
111         // Make sure groups/contents doesn't return manifest_text with
112         // collections (if it did, we'd need to sign it).
113         gresp, err := s.localdb.GroupContents(ctx, arvados.GroupContentsOptions{
114                 Limit:   -1,
115                 Filters: []arvados.Filter{{"uuid", "=", arvadostest.FooCollection}},
116                 Select:  []string{"uuid", "manifest_text"},
117         })
118         c.Check(err, check.IsNil)
119         if c.Check(gresp.Items, check.HasLen, 1) {
120                 c.Check(gresp.Items[0].(map[string]interface{})["uuid"], check.Equals, arvadostest.FooCollection)
121                 c.Check(gresp.Items[0].(map[string]interface{})["manifest_text"], check.Equals, nil)
122         }
123 }
124
125 func (s *CollectionSuite) checkSignatureExpiry(c *check.C, manifestText string, expectedTTL time.Duration) {
126         m := regexp.MustCompile(`@([[:xdigit:]]+)`).FindStringSubmatch(manifestText)
127         c.Assert(m, check.HasLen, 2)
128         sigexp, err := strconv.ParseInt(m[1], 16, 64)
129         c.Assert(err, check.IsNil)
130         expectedExp := time.Now().Add(expectedTTL).Unix()
131         c.Check(sigexp > expectedExp-60, check.Equals, true)
132         c.Check(sigexp <= expectedExp, check.Equals, true)
133 }
134
135 func (s *CollectionSuite) TestSignaturesDisabled(c *check.C) {
136         s.localdb.cluster.Collections.BlobSigning = false
137         ctx := auth.NewContext(context.Background(), &auth.Credentials{Tokens: []string{arvadostest.ActiveTokenV2}})
138
139         resp, err := s.localdb.CollectionGet(ctx, arvados.GetOptions{UUID: arvadostest.FooCollection})
140         c.Check(err, check.IsNil)
141         c.Check(resp.ManifestText, check.Matches, `(?ms).* acbd[^ +]*\+3 0:.*`)
142 }