Files
snapd/asserts/validation_set_test.go
Andrew Phelps 154d46b46c many: take into account validation sets during remodel (#13243)
This PR makes remodels take into account revision constraints from validation sets on the new model. Additionally, snaps that are marked as invalid in validation sets are checked for in the model.

* a/snapasserts: add methods for extracting more information out of ValidationSets type

* o/assertstate: add ValidationSetsFromModel function for extracting a snapasserts.ValidationSets from an asserts.Model

* o/snapstate: prevent installing/updating a snap from a local file that does not match requested revision

* o/devicestate: consider validation sets during remodeling

* tests/nested/manual: add remodel test that downgrades a snap because of a validation set

* tests/nested/manual: add remodel test that fails to remodel because of an invalid snap in a validation set

* tests/nested/manual: extend offline remodel test to also include a validation set

* tests/lib/assertions: fix timestamps on assertions

* asserts: add Key method to ValidationSet and ModelValidationSet

* o/devicestate: use new Key methods

* o/devicestate: maybe enforce validation sets during doSetModel

* o/devicestate: add test for enforcing validation sets in doSetModel

* a/snapasserts: simplify TestCanBePresent with loop

* tests/lib/assertions: add bluez snap to offline remodel test

* o/devicestate: remove done TODO

* o/snapstate: if remodeling, do not install prereq if link-snap task is present

* tests/nested/manual/remodel-offline: extend test to verify that validation sets are accounted for

* Revert "o/snapstate: if remodeling, do not install prereq if link-snap task is present"

This reverts commit 57c7725a2513df51be7ac1c06c492aaed07a6e3b.

This change is independent and will be included in another PR.

* a/snapasserts: add methods for extracting more information out of ValidationSets type

* o/assertstate: add ValidationSetsFromModel function for extracting a snapasserts.ValidationSets from an asserts.Model

* o/devicestate: add test for ValidationSetsConflictError.Is

* a/snapasserts: move methods after New function

* a/snapasserts: add test for ValidationSets.Revisions to verify ValidationSetsConflictError is returned

* o/assertstate: change ValidationSetsFromModel to take in a DeviceContext, rather than a StoreService

* o/assertstate: rename ValidationSetsModelFlags to ValidationSetsModelOptions

* o/devicestate: add type to export_test to make testing simpler

* tests: add details to new spread tests

* asserts: rename ModelValidationSet.Key and ValidationSet.Key to .SequenceName and add unit tests for them

* o/snapstate: update snap revision mismatch error message to be more clear

* o/devicestate: introduce helper for setting ValidationSets on snapstate.RevisionOptions if Revision is set

* o/devicestate: verify the parameters that fakeSequenceStore receives

* o/devicestate: fix revisions not being respected for essential snaps (and add a test for it)

* o/devicestate: extend TestRemodelUC20EssentialSnapsAlreadyInstalledAndLocal to also exercise case where a validation set requires a revision but the currently installed version is unasserted

* s/seedtest: update retrieveSeq to handle unconstrained sequence forming assertions

* a/snapasserts: add ValidationSets.Sets method

* o/assertstate: add deviceContext to ForgetValidationSet function so that change can happen during remodel

* o/devicestate: attempt to handle rollback of validation sets during failed remodel

* overlord: test for replacing conflicting validation sets during remodel

* o/assertstate: update ForgetValidationSet to take in a DeviceContext and to allow for forcing removal even if the validation set is in use by the model

* o/devicestate: roll back validation set changes on remodel failure

* o/devicestate: make sure that validation sets unrelated to the model survive a remodel

* o/devicestate: rename param in installedSnapRevisionChanged

* o/devicestate: rename field newSnapRevision to newRequiredRevision in modelSnapsForRemodel

* o/devicestate: simplify loops in checkForInvalidSnapsInModel

* o/devicestate: compare validation sets using SequenceName methods

* o/devicestate: fail remodel if we attempt to use an unasserted snap as a specific revision

* tests/nested/manual/remodel-offline: fix test to actually use validation set

* o/devicestate: create helper for creating snapstate.RevisionOptions during remodel

* o/devicestate: name param literals for clarity

* o/devicestate: invert logic to eliminate double negative

* o/devicestate: fix missed inversion of logic

* o/assertstate: update comment on ForgetValidationSetOpts.ForceForget

* overlord, o/devicestate: update remodel test to change models that contain the same validation set

* o/assertstate: test ForceForget functionality in ForgetValidationSet

* o/devicestate: rename function newRevisionOptionsForRemodel to revisionOptionsForRemodel

* o/assertstate, o/devicestate, daemon: remove unneeded DeviceContext param from ForgetValidationSet

* o/devicestate: remove println

* o/devicestate: clarify comment in rollback of adding validation sets

* o/devicestate: rename variable in enforceValidationSetsForRemodel

* o/snapstate: clarify error when attempting to install/refresh local snap with different revision than requested

* o/devicestate: naming consistency

* o/devicestate: simplify error when model is missing snap that is required in validation set

* asserts, overlord, o/devicestate: rename SequenceName to SequenceKey and prefix the series to the string that is returned
2023-12-12 10:00:48 +01:00

203 lines
7.8 KiB
Go

// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2020 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 (
"strings"
"time"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/asserts"
)
type validationSetSuite struct {
ts time.Time
tsLine string
}
var _ = Suite(&validationSetSuite{})
func (vss *validationSetSuite) SetUpSuite(c *C) {
vss.ts = time.Now().Truncate(time.Second).UTC()
vss.tsLine = "timestamp: " + vss.ts.Format(time.RFC3339) + "\n"
}
const (
validationSetExample = `type: validation-set
authority-id: brand-id1
series: 16
account-id: brand-id1
name: baz-3000-good
sequence: 2
snaps:
-
name: baz-linux
id: bazlinuxidididididididididididid
presence: optional
revision: 99
OTHER` + "TSLINE" +
"body-length: 0\n" +
"sign-key-sha3-384: Jv8_JiHiIzJVcO9M55pPdqSDWUvuhfDIBJUS-3VW7F_idjix7Ffn5qMxB21ZQuij" +
"\n\n" +
"AXNpZw=="
)
func (vss *validationSetSuite) TestDecodeOK(c *C) {
encoded := strings.Replace(validationSetExample, "TSLINE", vss.tsLine, 1)
encoded = strings.Replace(encoded, "OTHER", "", 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
c.Check(a.Type(), Equals, asserts.ValidationSetType)
_, ok := a.(asserts.SequenceMember)
c.Assert(ok, Equals, true)
valset := a.(*asserts.ValidationSet)
c.Check(valset.AuthorityID(), Equals, "brand-id1")
c.Check(valset.Timestamp(), Equals, vss.ts)
c.Check(valset.Series(), Equals, "16")
c.Check(valset.AccountID(), Equals, "brand-id1")
c.Check(valset.Name(), Equals, "baz-3000-good")
c.Check(valset.Sequence(), Equals, 2)
snaps := valset.Snaps()
c.Assert(snaps, DeepEquals, []*asserts.ValidationSetSnap{
{
Name: "baz-linux",
SnapID: "bazlinuxidididididididididididid",
Presence: asserts.PresenceOptional,
Revision: 99,
},
})
c.Check(snaps[0].SnapName(), Equals, "baz-linux")
c.Check(snaps[0].ID(), Equals, "bazlinuxidididididididididididid")
}
func (vss *validationSetSuite) TestDecodeInvalid(c *C) {
const validationSetErrPrefix = "assertion validation-set: "
encoded := strings.Replace(validationSetExample, "TSLINE", vss.tsLine, 1)
snapsStanza := encoded[strings.Index(encoded, "snaps:"):strings.Index(encoded, "timestamp:")]
invalidTests := []struct{ original, invalid, expectedErr string }{
{"series: 16\n", "", `"series" header is mandatory`},
{"series: 16\n", "series: \n", `"series" header should not be empty`},
{"account-id: brand-id1\n", "", `"account-id" header is mandatory`},
{"account-id: brand-id1\n", "account-id: \n", `"account-id" header should not be empty`},
{"account-id: brand-id1\n", "account-id: random\n", `authority-id and account-id must match, validation-set assertions are expected to be signed by the issuer account: "brand-id1" != "random"`},
{"name: baz-3000-good\n", "", `"name" header is mandatory`},
{"name: baz-3000-good\n", "name: \n", `"name" header should not be empty`},
{"name: baz-3000-good\n", "name: baz/3000/good\n", `"name" primary key header cannot contain '/'`},
{"name: baz-3000-good\n", "name: baz+3000+good\n", `"name" header contains invalid characters: "baz\+3000\+good"`},
{vss.tsLine, "timestamp: 12:30\n", `"timestamp" header is not a RFC3339 date: .*`},
{"sequence: 2\n", "", `"sequence" header is mandatory`},
{"sequence: 2\n", "sequence: one\n", `"sequence" header is not an integer: one`},
{"sequence: 2\n", "sequence: 0\n", `"sequence" must be >=1: 0`},
{"sequence: 2\n", "sequence: -1\n", `"sequence" must be >=1: -1`},
{"sequence: 2\n", "sequence: 00\n", `"sequence" header has invalid prefix zeros: 00`},
{"sequence: 2\n", "sequence: 01\n", `"sequence" header has invalid prefix zeros: 01`},
{"sequence: 2\n", "sequence: 010\n", `"sequence" header has invalid prefix zeros: 010`},
{snapsStanza, "", `"snaps" header is mandatory`},
{snapsStanza, "snaps: snap\n", `"snaps" header must be a list of maps`},
{snapsStanza, "snaps:\n - snap\n", `"snaps" header must be a list of maps`},
{"name: baz-linux\n", "other: 1\n", `"name" of snap is mandatory`},
{"name: baz-linux\n", "name: linux_2\n", `invalid snap name "linux_2"`},
{"id: bazlinuxidididididididididididid\n", "id: 2\n", `"id" of snap "baz-linux" contains invalid characters: "2"`},
{" id: bazlinuxidididididididididididid\n", "", `"id" of snap "baz-linux" is mandatory`},
{"OTHER", " -\n name: baz-linux\n id: bazlinuxidididididididididididid\n", `cannot list the same snap "baz-linux" multiple times`},
{"OTHER", " -\n name: baz-linux2\n id: bazlinuxidididididididididididid\n", `cannot specify the same snap id "bazlinuxidididididididididididid" multiple times, specified for snaps "baz-linux" and "baz-linux2"`},
{"presence: optional\n", "presence:\n - opt\n", `"presence" of snap "baz-linux" must be a string`},
{"presence: optional\n", "presence: no\n", `"presence" of snap "baz-linux" must be one of must be one of required|optional|invalid`},
{"revision: 99\n", "revision: 0\n", `"revision" of snap "baz-linux" must be >=1: 0`},
{"presence: optional\n", "presence: invalid\n", `cannot specify revision of snap "baz-linux" at the same time as stating its presence is invalid`},
}
for _, test := range invalidTests {
invalid := strings.Replace(encoded, test.original, test.invalid, 1)
invalid = strings.Replace(invalid, "OTHER", "", 1)
_, err := asserts.Decode([]byte(invalid))
c.Check(err, ErrorMatches, validationSetErrPrefix+test.expectedErr)
}
}
func (vss *validationSetSuite) TestSnapPresenceOptionalDefaultRequired(c *C) {
encoded := strings.Replace(validationSetExample, "TSLINE", vss.tsLine, 1)
encoded = strings.Replace(encoded, "OTHER", "", 1)
encoded = strings.Replace(encoded, " presence: optional\n", "", 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
c.Check(a.Type(), Equals, asserts.ValidationSetType)
valset := a.(*asserts.ValidationSet)
snaps := valset.Snaps()
c.Assert(snaps, HasLen, 1)
c.Check(snaps[0].Presence, Equals, asserts.PresenceRequired)
}
func (vss *validationSetSuite) TestSnapRevisionOptional(c *C) {
encoded := strings.Replace(validationSetExample, "TSLINE", vss.tsLine, 1)
encoded = strings.Replace(encoded, "OTHER", "", 1)
encoded = strings.Replace(encoded, " revision: 99\n", "", 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
c.Check(a.Type(), Equals, asserts.ValidationSetType)
valset := a.(*asserts.ValidationSet)
snaps := valset.Snaps()
c.Assert(snaps, HasLen, 1)
// 0 means unset
c.Check(snaps[0].Revision, Equals, 0)
}
func (vss *validationSetSuite) TestIsValidValidationSetName(c *C) {
names := []struct {
name string
valid bool
}{
{"", false},
{"abA", false},
{"-a", false},
{"1", true},
{"a", true},
{"ab", true},
{"foo1-bar0", true},
}
for i, name := range names {
c.Assert(asserts.IsValidValidationSetName(name.name), Equals, name.valid, Commentf("%d: %s", i, name.name))
}
}
func (vss *validationSetSuite) TestValidationSetSequenceKey(c *C) {
encoded := strings.Replace(validationSetExample, "TSLINE", vss.tsLine, 1)
encoded = strings.Replace(encoded, "OTHER", "", 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
_, ok := a.(asserts.SequenceMember)
c.Assert(ok, Equals, true)
valset := a.(*asserts.ValidationSet)
c.Check(valset.SequenceKey(), Equals, "16/brand-id1/baz-3000-good")
}