Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

seedwriter: check if the optionSnap channel is same as model assert #14071

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion seed/seedwriter/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,26 @@ func (w *Writer) SetOptionsSnaps(optSnaps []*OptionsSnap) error {
if err != nil {
return fmt.Errorf("cannot use option channel for snap %q: %v", whichSnap, err)
}
if err := w.policy.checkSnapChannel(ch, whichSnap); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIU the original check is intentional and if you are using a mode with grade > dangerous, the channel is already specified in the model itself, thus it is a mistake to attempt to pass one in the command line.

Note that it is enough to pass the snap name as --snap <name> to include an optional snap. During seeding the snap will be set up to track the right channel


// Check whether the channel defined in the model assertion
// is same as the option snap(--snap=SNAP_NAME=CHANNEL)
modSnaps, err := w.modSnaps()
if err != nil {
return err
}
for _, modSnap := range modSnaps {
if sn.Name != modSnap.Name {
continue
}

if modSnap.Presence == "optional" && sn.Channel == modSnap.DefaultChannel {
continue
} else {
if err := w.policy.checkSnapChannel(ch, whichSnap); err != nil {
return err
}
}
}
}
if local {
if w.localSnaps == nil {
Expand Down
96 changes: 96 additions & 0 deletions seed/seedwriter/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3316,6 +3316,102 @@ func (s *writerSuite) TestSnapsToDownloadCore20OptionalSnaps(c *C) {
c.Check(snaps[5].SnapName(), Equals, "optional20-b")
}

func (s *writerSuite) TestSnapsToDownloadCore20OptionalSnapsWithSameChannel(c *C) {
model := s.Brands.Model("my-brand", "my-model", map[string]interface{}{
"display-name": "my model",
"architecture": "amd64",
"base": "core20",
"snaps": []interface{}{
map[string]interface{}{
"name": "pc-kernel",
"id": s.AssertedSnapID("pc-kernel"),
"type": "kernel",
"default-channel": "20",
},
map[string]interface{}{
"name": "pc",
"id": s.AssertedSnapID("pc"),
"type": "gadget",
"default-channel": "20",
},
map[string]interface{}{
"name": "optional20-a",
"id": s.AssertedSnapID("optional20-a"),
"presence": "optional",
"default-channel": "20",
},
},
})

// validity
c.Assert(model.Grade(), Equals, asserts.ModelSigned)

s.makeSnap(c, "snapd", "")
s.makeSnap(c, "core20", "")
s.makeSnap(c, "pc-kernel=20", "")
s.makeSnap(c, "pc=20", "")
s.makeSnap(c, "optional20-a", "developerid")

s.opts.Label = "20191122"
w, err := seedwriter.New(model, s.opts)
c.Assert(err, IsNil)

err = w.SetOptionsSnaps([]*seedwriter.OptionsSnap{{Name: "optional20-a", Channel: "20"}})
c.Assert(err, IsNil)

err = w.Start(s.db, s.rf)
c.Assert(err, IsNil)

snaps, err := w.SnapsToDownload()
c.Assert(err, IsNil)
c.Check(snaps, HasLen, 5)
c.Check(snaps[4].SnapName(), Equals, "optional20-a")
}

func (s *writerSuite) TestSnapsToDownloadCore20OptionalSnapsWithDifferentChannel(c *C) {
model := s.Brands.Model("my-brand", "my-model", map[string]interface{}{
"display-name": "my model",
"architecture": "amd64",
"base": "core20",
"snaps": []interface{}{
map[string]interface{}{
"name": "pc-kernel",
"id": s.AssertedSnapID("pc-kernel"),
"type": "kernel",
"default-channel": "20",
},
map[string]interface{}{
"name": "pc",
"id": s.AssertedSnapID("pc"),
"type": "gadget",
"default-channel": "20",
},
map[string]interface{}{
"name": "optional20-a",
"id": s.AssertedSnapID("optional20-a"),
"presence": "optional",
"default-channel": "20",
},
},
})

// validity
c.Assert(model.Grade(), Equals, asserts.ModelSigned)

s.makeSnap(c, "snapd", "")
s.makeSnap(c, "core20", "")
s.makeSnap(c, "pc-kernel=20", "")
s.makeSnap(c, "pc=20", "")
s.makeSnap(c, "optional20-a", "developerid")

s.opts.Label = "20191122"
w, err := seedwriter.New(model, s.opts)
c.Assert(err, IsNil)

err = w.SetOptionsSnaps([]*seedwriter.OptionsSnap{{Name: "optional20-a", Channel: "22"}})
c.Check(err, ErrorMatches, `cannot override channels, add devmode snaps, local snaps, or extra snaps with a model of grade higher than dangerous`)
}

func (s *writerSuite) TestSeedSnapsWriteMetaCore20ExtraSnaps(c *C) {
model := s.Brands.Model("my-brand", "my-model", map[string]interface{}{
"display-name": "my model",
Expand Down
Loading