1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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"
22 var _ = check.Suite(&CollectionSuite{})
24 type CollectionSuite struct {
25 cluster *arvados.Cluster
27 railsSpy *arvadostest.Proxy
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)
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)
47 func (s *CollectionSuite) TearDownTest(c *check.C) {
51 func (s *CollectionSuite) setUpVocabulary(c *check.C, testVocabulary string) {
52 if testVocabulary == "" {
58 "labels": [{"label": "Importance"}, {"label": "Priority"}],
60 "IDVALIMPORTANCES1": { "labels": [{"label": "Critical"}, {"label": "Urgent"}, {"label": "High"}] },
61 "IDVALIMPORTANCES2": { "labels": [{"label": "Normal"}, {"label": "Moderate"}] },
62 "IDVALIMPORTANCES3": { "labels": [{"label": "Low"}] }
68 voc, err := arvados.NewVocabulary([]byte(testVocabulary), []string{})
69 c.Assert(err, check.IsNil)
70 s.cluster.API.VocabularyPath = "foo"
71 s.localdb.vocabularyCache = voc
74 func (s *CollectionSuite) TestCollectionCreateWithProperties(c *check.C) {
75 s.setUpVocabulary(c, "")
76 ctx := auth.NewContext(context.Background(), &auth.Credentials{Tokens: []string{arvadostest.ActiveTokenV2}})
80 props map[string]interface{}
83 {"Invalid prop key", map[string]interface{}{"Priority": "IDVALIMPORTANCES1"}, false},
84 {"Invalid prop value", map[string]interface{}{"IDTAGIMPORTANCES": "high"}, false},
85 {"Valid prop key & value", map[string]interface{}{"IDTAGIMPORTANCES": "IDVALIMPORTANCES1"}, true},
86 {"Empty properties", map[string]interface{}{}, true},
88 for _, tt := range tests {
89 c.Log(c.TestName()+" ", tt.name)
91 coll, err := s.localdb.CollectionCreate(ctx, arvados.CreateOptions{
92 Select: []string{"uuid", "properties"},
93 Attrs: map[string]interface{}{
94 "properties": tt.props,
97 c.Assert(err, check.IsNil)
98 c.Assert(coll.Properties, check.DeepEquals, tt.props)
100 c.Assert(err, check.NotNil)
105 func (s *CollectionSuite) TestCollectionUpdateWithProperties(c *check.C) {
106 s.setUpVocabulary(c, "")
107 ctx := auth.NewContext(context.Background(), &auth.Credentials{Tokens: []string{arvadostest.ActiveTokenV2}})
111 props map[string]interface{}
114 {"Invalid prop key", map[string]interface{}{"Priority": "IDVALIMPORTANCES1"}, false},
115 {"Invalid prop value", map[string]interface{}{"IDTAGIMPORTANCES": "high"}, false},
116 {"Valid prop key & value", map[string]interface{}{"IDTAGIMPORTANCES": "IDVALIMPORTANCES1"}, true},
117 {"Empty properties", map[string]interface{}{}, true},
119 for _, tt := range tests {
120 c.Log(c.TestName()+" ", tt.name)
121 coll, err := s.localdb.CollectionCreate(ctx, arvados.CreateOptions{})
122 c.Assert(err, check.IsNil)
123 coll, err = s.localdb.CollectionUpdate(ctx, arvados.UpdateOptions{
125 Select: []string{"uuid", "properties"},
126 Attrs: map[string]interface{}{
127 "properties": tt.props,
130 c.Assert(err, check.IsNil)
131 c.Assert(coll.Properties, check.DeepEquals, tt.props)
133 c.Assert(err, check.NotNil)
138 func (s *CollectionSuite) TestSignatures(c *check.C) {
139 ctx := auth.NewContext(context.Background(), &auth.Credentials{Tokens: []string{arvadostest.ActiveTokenV2}})
141 resp, err := s.localdb.CollectionGet(ctx, arvados.GetOptions{UUID: arvadostest.FooCollection})
142 c.Check(err, check.IsNil)
143 c.Check(resp.ManifestText, check.Matches, `(?ms).* acbd[^ ]*\+3\+A[0-9a-f]+@[0-9a-f]+ 0:.*`)
144 s.checkSignatureExpiry(c, resp.ManifestText, time.Hour*24*7*2)
146 resp, err = s.localdb.CollectionGet(ctx, arvados.GetOptions{UUID: arvadostest.FooCollection, Select: []string{"manifest_text"}})
147 c.Check(err, check.IsNil)
148 c.Check(resp.ManifestText, check.Matches, `(?ms).* acbd[^ ]*\+3\+A[0-9a-f]+@[0-9a-f]+ 0:.*`)
150 lresp, err := s.localdb.CollectionList(ctx, arvados.ListOptions{Limit: -1, Filters: []arvados.Filter{{"uuid", "=", arvadostest.FooCollection}}})
151 c.Check(err, check.IsNil)
152 if c.Check(lresp.Items, check.HasLen, 1) {
153 c.Check(lresp.Items[0].UUID, check.Equals, arvadostest.FooCollection)
154 c.Check(lresp.Items[0].ManifestText, check.Equals, "")
155 c.Check(lresp.Items[0].UnsignedManifestText, check.Equals, "")
158 lresp, err = s.localdb.CollectionList(ctx, arvados.ListOptions{Limit: -1, Filters: []arvados.Filter{{"uuid", "=", arvadostest.FooCollection}}, Select: []string{"manifest_text"}})
159 c.Check(err, check.IsNil)
160 if c.Check(lresp.Items, check.HasLen, 1) {
161 c.Check(lresp.Items[0].ManifestText, check.Matches, `(?ms).* acbd[^ ]*\+3\+A[0-9a-f]+@[0-9a-f]+ 0:.*`)
162 c.Check(lresp.Items[0].UnsignedManifestText, check.Equals, "")
165 lresp, err = s.localdb.CollectionList(ctx, arvados.ListOptions{Limit: -1, Filters: []arvados.Filter{{"uuid", "=", arvadostest.FooCollection}}, Select: []string{"unsigned_manifest_text"}})
166 c.Check(err, check.IsNil)
167 if c.Check(lresp.Items, check.HasLen, 1) {
168 c.Check(lresp.Items[0].ManifestText, check.Equals, "")
169 c.Check(lresp.Items[0].UnsignedManifestText, check.Matches, `(?ms).* acbd[^ ]*\+3 0:.*`)
172 // early trash date causes lower signature TTL (even if
173 // trash_at and is_trashed fields are unselected)
174 trashed, err := s.localdb.CollectionCreate(ctx, arvados.CreateOptions{
175 Select: []string{"uuid", "manifest_text"},
176 Attrs: map[string]interface{}{
177 "manifest_text": ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo\n",
178 "trash_at": time.Now().UTC().Add(time.Hour),
180 c.Assert(err, check.IsNil)
181 s.checkSignatureExpiry(c, trashed.ManifestText, time.Hour)
182 resp, err = s.localdb.CollectionGet(ctx, arvados.GetOptions{UUID: trashed.UUID})
183 c.Assert(err, check.IsNil)
184 s.checkSignatureExpiry(c, resp.ManifestText, time.Hour)
186 // distant future trash date does not cause higher signature TTL
187 trashed, err = s.localdb.CollectionUpdate(ctx, arvados.UpdateOptions{
189 Attrs: map[string]interface{}{
190 "trash_at": time.Now().UTC().Add(time.Hour * 24 * 365),
192 c.Assert(err, check.IsNil)
193 s.checkSignatureExpiry(c, trashed.ManifestText, time.Hour*24*7*2)
194 resp, err = s.localdb.CollectionGet(ctx, arvados.GetOptions{UUID: trashed.UUID})
195 c.Assert(err, check.IsNil)
196 s.checkSignatureExpiry(c, resp.ManifestText, time.Hour*24*7*2)
198 // Make sure groups/contents doesn't return manifest_text with
199 // collections (if it did, we'd need to sign it).
200 gresp, err := s.localdb.GroupContents(ctx, arvados.GroupContentsOptions{
202 Filters: []arvados.Filter{{"uuid", "=", arvadostest.FooCollection}},
203 Select: []string{"uuid", "manifest_text"},
206 c.Check(err, check.ErrorMatches, `.*Invalid attribute.*manifest_text.*`)
207 } else if c.Check(gresp.Items, check.HasLen, 1) {
208 c.Check(gresp.Items[0].(map[string]interface{})["uuid"], check.Equals, arvadostest.FooCollection)
209 c.Check(gresp.Items[0].(map[string]interface{})["manifest_text"], check.Equals, nil)
213 func (s *CollectionSuite) checkSignatureExpiry(c *check.C, manifestText string, expectedTTL time.Duration) {
214 m := regexp.MustCompile(`@([[:xdigit:]]+)`).FindStringSubmatch(manifestText)
215 c.Assert(m, check.HasLen, 2)
216 sigexp, err := strconv.ParseInt(m[1], 16, 64)
217 c.Assert(err, check.IsNil)
218 expectedExp := time.Now().Add(expectedTTL).Unix()
219 c.Check(sigexp > expectedExp-60, check.Equals, true)
220 c.Check(sigexp <= expectedExp, check.Equals, true)
223 func (s *CollectionSuite) TestSignaturesDisabled(c *check.C) {
224 s.localdb.cluster.Collections.BlobSigning = false
225 ctx := auth.NewContext(context.Background(), &auth.Credentials{Tokens: []string{arvadostest.ActiveTokenV2}})
227 resp, err := s.localdb.CollectionGet(ctx, arvados.GetOptions{UUID: arvadostest.FooCollection})
228 c.Check(err, check.IsNil)
229 c.Check(resp.ManifestText, check.Matches, `(?ms).* acbd[^ +]*\+3 0:.*`)