1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016-2022 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package asserts_test
import (
"crypto"
"fmt"
"time"
"golang.org/x/crypto/sha3"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/asserts/assertstest"
"github.com/snapcore/snapd/release"
)
type fetcherSuite struct {
storeSigning *assertstest.StoreStack
}
var _ = Suite(&fetcherSuite{})
func (s *fetcherSuite) SetUpTest(c *C) {
s.storeSigning = assertstest.NewStoreStack("can0nical", nil)
}
func fakeSnap(rev int) []byte {
fake := fmt.Sprintf("hsqs________________%d", rev)
return []byte(fake)
}
func fakeHash(rev int) []byte {
h := sha3.Sum384(fakeSnap(rev))
return h[:]
}
func makeDigest(rev int) string {
d, err := asserts.EncodeDigest(crypto.SHA3_384, fakeHash(rev))
if err != nil {
panic(err)
}
return string(d)
}
func (s *fetcherSuite) prereqSnapAssertions(c *C, revisions ...int) {
dev1Acct := assertstest.NewAccount(s.storeSigning, "developer1", nil, "")
err := s.storeSigning.Add(dev1Acct)
c.Assert(err, IsNil)
headers := map[string]any{
"series": "16",
"snap-id": "snap-id-1",
"snap-name": "foo",
"publisher-id": dev1Acct.AccountID(),
"timestamp": time.Now().Format(time.RFC3339),
}
snapDecl, err := s.storeSigning.Sign(asserts.SnapDeclarationType, headers, nil, "")
c.Assert(err, IsNil)
err = s.storeSigning.Add(snapDecl)
c.Assert(err, IsNil)
for _, rev := range revisions {
headers = map[string]any{
"series": "16",
"snap-id": "snap-id-1",
"snap-sha3-384": makeDigest(rev),
"snap-size": "1000",
"snap-revision": fmt.Sprintf("%d", rev),
"developer-id": dev1Acct.AccountID(),
"timestamp": time.Now().Format(time.RFC3339),
}
snapRev, err := s.storeSigning.Sign(asserts.SnapRevisionType, headers, nil, "")
c.Assert(err, IsNil)
err = s.storeSigning.Add(snapRev)
c.Assert(err, IsNil)
}
}
func (s *fetcherSuite) TestFetch(c *C) {
s.prereqSnapAssertions(c, 10)
db, err := asserts.OpenDatabase(&asserts.DatabaseConfig{
Backstore: asserts.NewMemoryBackstore(),
Trusted: s.storeSigning.Trusted,
})
c.Assert(err, IsNil)
ref := &asserts.Ref{
Type: asserts.SnapRevisionType,
PrimaryKey: []string{makeDigest(10)},
}
retrieve := func(ref *asserts.Ref) (asserts.Assertion, error) {
return ref.Resolve(s.storeSigning.Find)
}
f := asserts.NewFetcher(db, retrieve, db.Add)
err = f.Fetch(ref)
c.Assert(err, IsNil)
snapRev, err := ref.Resolve(db.Find)
c.Assert(err, IsNil)
c.Check(snapRev.(*asserts.SnapRevision).SnapRevision(), Equals, 10)
snapDecl, err := db.Find(asserts.SnapDeclarationType, map[string]string{
"series": "16",
"snap-id": "snap-id-1",
})
c.Assert(err, IsNil)
c.Check(snapDecl.(*asserts.SnapDeclaration).SnapName(), Equals, "foo")
}
func (s *fetcherSuite) TestFetchCircularReference(c *C) {
s.prereqSnapAssertions(c, 10)
db, err := asserts.OpenDatabase(&asserts.DatabaseConfig{
Backstore: asserts.NewMemoryBackstore(),
Trusted: s.storeSigning.Trusted,
})
c.Assert(err, IsNil)
ref := &asserts.Ref{
Type: asserts.SnapRevisionType,
PrimaryKey: []string{makeDigest(10)},
}
retrieve := func(ref *asserts.Ref) (asserts.Assertion, error) {
return ref.Resolve(s.storeSigning.Find)
}
f := asserts.NewFetcher(db, retrieve, db.Add)
// Mock that we refer to ourself
r := asserts.MockAssertionPrereqs(func(a asserts.Assertion) []*asserts.Ref {
return []*asserts.Ref{ref}
})
defer r()
err = f.Fetch(ref)
c.Assert(err, ErrorMatches, `circular assertions are not expected: snap-revision \(tzGsQxT_xJGzbnJ_-25Bbj_8lBHY39c5uUuQWgDTGxAEd0NALdxVaSAD59Pou_Ko;\)`)
}
func (s *fetcherSuite) TestSave(c *C) {
s.prereqSnapAssertions(c, 10)
db, err := asserts.OpenDatabase(&asserts.DatabaseConfig{
Backstore: asserts.NewMemoryBackstore(),
Trusted: s.storeSigning.Trusted,
})
c.Assert(err, IsNil)
retrieve := func(ref *asserts.Ref) (asserts.Assertion, error) {
return ref.Resolve(s.storeSigning.Find)
}
f := asserts.NewFetcher(db, retrieve, db.Add)
ref := &asserts.Ref{
Type: asserts.SnapRevisionType,
PrimaryKey: []string{makeDigest(10)},
}
rev, err := ref.Resolve(s.storeSigning.Find)
c.Assert(err, IsNil)
err = f.Save(rev)
c.Assert(err, IsNil)
snapRev, err := ref.Resolve(db.Find)
c.Assert(err, IsNil)
c.Check(snapRev.(*asserts.SnapRevision).SnapRevision(), Equals, 10)
snapDecl, err := db.Find(asserts.SnapDeclarationType, map[string]string{
"series": "16",
"snap-id": "snap-id-1",
})
c.Assert(err, IsNil)
c.Check(snapDecl.(*asserts.SnapDeclaration).SnapName(), Equals, "foo")
}
func (s *fetcherSuite) prereqValidationSetAssertion(c *C) {
vs, err := s.storeSigning.Sign(asserts.ValidationSetType, map[string]any{
"type": "validation-set",
"authority-id": "can0nical",
"series": "16",
"account-id": "can0nical",
"name": "base-set",
"sequence": "2",
"snaps": []any{
map[string]any{
"name": "pc-kernel",
"id": "123456ididididididididididididid",
"presence": "required",
"revision": "7",
},
},
"timestamp": time.Now().UTC().Format(time.RFC3339),
}, nil, "")
c.Assert(err, IsNil)
err = s.storeSigning.Add(vs)
c.Check(err, IsNil)
}
func (s *fetcherSuite) TestFetchSequence(c *C) {
s.prereqValidationSetAssertion(c)
db, err := asserts.OpenDatabase(&asserts.DatabaseConfig{
Backstore: asserts.NewMemoryBackstore(),
Trusted: s.storeSigning.Trusted,
})
c.Assert(err, IsNil)
seq := &asserts.AtSequence{
Type: asserts.ValidationSetType,
SequenceKey: []string{release.Series, "can0nical", "base-set"},
Sequence: 2,
Revision: asserts.RevisionNotKnown,
}
retrieve := func(ref *asserts.Ref) (asserts.Assertion, error) {
return ref.Resolve(s.storeSigning.Find)
}
retrieveSeq := func(seq *asserts.AtSequence) (asserts.Assertion, error) {
return seq.Resolve(s.storeSigning.Find)
}
f := asserts.NewSequenceFormingFetcher(db, retrieve, retrieveSeq, db.Add)
// Fetch the sequence, this will fetch the validation-set with sequence
// 2. After that we should be able to find the validation-set (sequence 2)
// in the DB.
err = f.FetchSequence(seq)
c.Assert(err, IsNil)
// Calling resolve works when we provide the correct sequence number. This
// will then find the assertion we just fetched
vsa, err := seq.Resolve(db.Find)
c.Assert(err, IsNil)
c.Check(vsa.(*asserts.ValidationSet).Name(), Equals, "base-set")
c.Check(vsa.(*asserts.ValidationSet).Sequence(), Equals, 2)
// Calling resolve doesn't find the assertion when another sequence number
// is provided.
seq.Sequence = 4
_, err = seq.Resolve(db.Find)
c.Assert(err, ErrorMatches, `validation-set \(4; series:16 account-id:can0nical name:base-set\) not found`)
}
func (s *fetcherSuite) TestFetchSequenceCircularReference(c *C) {
s.prereqValidationSetAssertion(c)
db, err := asserts.OpenDatabase(&asserts.DatabaseConfig{
Backstore: asserts.NewMemoryBackstore(),
Trusted: s.storeSigning.Trusted,
})
c.Assert(err, IsNil)
seq := &asserts.AtSequence{
Type: asserts.ValidationSetType,
SequenceKey: []string{release.Series, "can0nical", "base-set"},
Sequence: 2,
Revision: asserts.RevisionNotKnown,
}
retrieve := func(ref *asserts.Ref) (asserts.Assertion, error) {
return ref.Resolve(s.storeSigning.Find)
}
retrieveSeq := func(seq *asserts.AtSequence) (asserts.Assertion, error) {
return seq.Resolve(s.storeSigning.Find)
}
f := asserts.NewSequenceFormingFetcher(db, retrieve, retrieveSeq, db.Add)
// Mock that we refer to ourself
r := asserts.MockAssertionPrereqs(func(a asserts.Assertion) []*asserts.Ref {
return []*asserts.Ref{
{
Type: asserts.ValidationSetType,
PrimaryKey: []string{release.Series, "can0nical", "base-set", "2"},
},
}
})
defer r()
err = f.FetchSequence(seq)
c.Assert(err, ErrorMatches, `circular assertions are not expected: validation-set \(2; series:16 account-id:can0nical name:base-set\)`)
}
func (s *fetcherSuite) TestFetchSequenceMultipleSequencesNotSupported(c *C) {
s.prereqValidationSetAssertion(c)
db, err := asserts.OpenDatabase(&asserts.DatabaseConfig{
Backstore: asserts.NewMemoryBackstore(),
Trusted: s.storeSigning.Trusted,
})
c.Assert(err, IsNil)
seq := &asserts.AtSequence{
Type: asserts.ValidationSetType,
SequenceKey: []string{release.Series, "can0nical", "base-set"},
Sequence: 2,
Revision: asserts.RevisionNotKnown,
}
retrieve := func(ref *asserts.Ref) (asserts.Assertion, error) {
return ref.Resolve(s.storeSigning.Find)
}
retrieveSeq := func(seq *asserts.AtSequence) (asserts.Assertion, error) {
return seq.Resolve(s.storeSigning.Find)
}
f := asserts.NewSequenceFormingFetcher(db, retrieve, retrieveSeq, db.Add)
err = f.FetchSequence(seq)
c.Assert(err, IsNil)
// Fetch same validation-set, but with a different sequence. Currently the
// AtSequence.Unique() does not include the sequence number or revision, meaning
// that the first sequence we fetch is the one that will be put into the DB.
// XXX: This test is here to document the behavior. If we want it to spit an error
// or support multiple sequences of an assertion, then changes are required.
seq.Sequence = 4
err = f.FetchSequence(seq)
c.Assert(err, IsNil)
// We fetch 2 first, it should exist.
seq.Sequence = 2
vsa, err := seq.Resolve(db.Find)
c.Assert(err, IsNil)
c.Check(vsa.(*asserts.ValidationSet).Name(), Equals, "base-set")
c.Check(vsa.(*asserts.ValidationSet).Sequence(), Equals, 2)
// 4 will not exist, as 2 already was present.
seq.Sequence = 4
_, err = seq.Resolve(db.Find)
c.Assert(err, ErrorMatches, `validation-set \(4; series:16 account-id:can0nical name:base-set\) not found`)
}
func (s *fetcherSuite) TestFetcherNotCreatedUsingNewSequenceFormingFetcher(c *C) {
db, err := asserts.OpenDatabase(&asserts.DatabaseConfig{
Backstore: asserts.NewMemoryBackstore(),
Trusted: s.storeSigning.Trusted,
})
c.Assert(err, IsNil)
retrieve := func(ref *asserts.Ref) (asserts.Assertion, error) {
return ref.Resolve(s.storeSigning.Find)
}
f := asserts.NewFetcher(db, retrieve, db.Add)
c.Assert(f, NotNil)
// Cast the fetcher to a SequenceFormingFetcher, which should succeed
// since the fetcher actually implements FetchSequence.
ff := f.(asserts.SequenceFormingFetcher)
c.Assert(ff, NotNil)
// Make sure this produces an error and not a crash
err = f.(asserts.SequenceFormingFetcher).FetchSequence(nil)
c.Check(err, ErrorMatches, `cannot fetch assertion sequence point, fetcher must be created using NewSequenceFormingFetcher`)
}
|