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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 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 ctlcmd_test
import (
"errors"
"fmt"
"os"
"time"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/asserts/assertstest"
"github.com/snapcore/snapd/asserts/sysdb"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/gadget"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/overlord"
"github.com/snapcore/snapd/overlord/assertstate"
"github.com/snapcore/snapd/overlord/assertstate/assertstatetest"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/devicestate"
"github.com/snapcore/snapd/overlord/devicestate/devicestatetest"
"github.com/snapcore/snapd/overlord/hookstate"
"github.com/snapcore/snapd/overlord/hookstate/ctlcmd"
"github.com/snapcore/snapd/overlord/hookstate/hooktest"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/overlord/storecontext"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/store/storetest"
"github.com/snapcore/snapd/sysconfig"
"github.com/snapcore/snapd/testutil"
)
type modelSuite struct {
testutil.BaseTest
o *overlord.Overlord
state *state.State
hookMgr *hookstate.HookManager
mgr *devicestate.DeviceManager
db *asserts.Database
mockHandler *hooktest.MockHandler
storeSigning *assertstest.StoreStack
brands *assertstest.SigningAccounts
newFakeStore func(storecontext.DeviceBackend) snapstate.StoreService
}
type fakeSnapStore struct {
storetest.Store
state *state.State
db asserts.RODatabase
}
var _ = Suite(&modelSuite{})
var (
brandPrivKey, _ = assertstest.GenerateKey(752)
brandPrivKey2, _ = assertstest.GenerateKey(752)
)
func (s *modelSuite) newStore(devBE storecontext.DeviceBackend) snapstate.StoreService {
return s.newFakeStore(devBE)
}
func (s *modelSuite) SetUpTest(c *C) {
s.BaseTest.SetUpTest(c)
dirs.SetRootDir(c.MkDir())
s.AddCleanup(func() { dirs.SetRootDir("") })
err := os.MkdirAll(dirs.SnapRunDir, 0755)
c.Assert(err, IsNil)
err = os.MkdirAll(dirs.SnapdStateDir(dirs.GlobalRootDir), 0755)
c.Assert(err, IsNil)
s.AddCleanup(osutil.MockMountInfo(``))
s.o = overlord.Mock()
s.state = s.o.State()
s.mockHandler = hooktest.NewMockHandler()
s.storeSigning = assertstest.NewStoreStack("canonical", nil)
s.AddCleanup(sysdb.MockGenericClassicModel(s.storeSigning.GenericClassicModel))
s.brands = assertstest.NewSigningAccounts(s.storeSigning)
s.brands.Register("my-brand", brandPrivKey, map[string]any{
"display-name": "fancy model publisher",
"validation": "certified",
})
db, err := asserts.OpenDatabase(&asserts.DatabaseConfig{
Backstore: asserts.NewMemoryBackstore(),
Trusted: s.storeSigning.Trusted,
OtherPredefined: s.storeSigning.Generic,
})
c.Assert(err, IsNil)
s.state.Lock()
assertstate.ReplaceDB(s.state, db)
s.state.Unlock()
s.AddCleanup(func() {
s.state.Lock()
assertstate.ReplaceDB(s.state, nil)
s.state.Unlock()
})
err = db.Add(s.storeSigning.StoreAccountKey(""))
c.Assert(err, IsNil)
hookMgr, err := hookstate.Manager(s.state, s.o.TaskRunner())
c.Assert(err, IsNil)
devicestate.EarlyConfig = func(*state.State, func() (sysconfig.Device, *gadget.Info, error)) error {
return nil
}
s.AddCleanup(func() { devicestate.EarlyConfig = nil })
mgr, err := devicestate.Manager(s.state, hookMgr, s.o.TaskRunner(), s.newStore)
c.Assert(err, IsNil)
s.db = db
s.hookMgr = hookMgr
s.o.AddManager(s.hookMgr)
s.mgr = mgr
s.o.AddManager(s.mgr)
s.o.AddManager(s.o.TaskRunner())
s.state.Lock()
snapstate.ReplaceStore(s.state, &fakeSnapStore{
state: s.state,
db: s.storeSigning,
})
s.state.Unlock()
s.AddCleanup(func() { s.newFakeStore = nil })
}
func (s *modelSuite) setupBrands() {
s.state.Lock()
defer s.state.Unlock()
assertstatetest.AddMany(s.state, s.brands.AccountsAndKeys("my-brand")...)
otherAcct := assertstest.NewAccount(s.storeSigning, "other-brand", map[string]any{
"account-id": "other-brand",
}, "")
assertstatetest.AddMany(s.state, otherAcct)
}
func (s *modelSuite) addSnapDeclaration(c *C, snapID, developerID, snapName string) {
declA, err := s.storeSigning.Sign(asserts.SnapDeclarationType, map[string]any{
"series": "16",
"snap-id": snapID,
"publisher-id": developerID,
"snap-name": snapName,
"timestamp": time.Now().UTC().Format(time.RFC3339),
}, nil, "")
c.Assert(err, IsNil)
err = s.db.Add(declA)
c.Assert(err, IsNil)
}
const snapGadgetYaml = `name: gadget1
type: gadget
version: 1
`
const snapBaseYaml = `name: snap1-base
type: base
version: 1
`
const snapKernelYaml = `name: kernel1
type: kernel
version: 1
`
const snapYaml = `name: snap1
base: snap1-base
version: 1
`
var snapWithSnapdControlOnlyYaml = `
name: snap1-control
version: 1
plugs:
snapd-control:
`
func (s *modelSuite) TestUnhappyModelCommandInsufficientPermissions(c *C) {
// Verify we get an error in case that we do not match any of the three
// criteria:
// - snapd-control interface
// - we are a gadget snap
// - we come from the same publisher
s.setupBrands()
// set a model assertion
s.state.Lock()
current := s.brands.Model("canonical", "pc-model", map[string]any{
"architecture": "amd64",
"kernel": "pc-kernel",
"gadget": "pc",
"base": "core18",
})
err := assertstate.Add(s.state, current)
c.Assert(err, IsNil)
devicestatetest.SetDevice(s.state, &auth.DeviceState{
Brand: "canonical",
Model: "pc-model",
})
c.Assert(err, IsNil)
task := s.state.NewTask("test-task", "my test task")
setup := &hookstate.HookSetup{Snap: "snap1", Revision: snap.R(1), Hook: "test-hook"}
mockContext, err := hookstate.NewContext(task, s.state, setup, s.mockHandler, "")
c.Assert(err, IsNil)
mockInstalledSnap(c, s.state, snapBaseYaml, "")
mockInstalledSnap(c, s.state, snapYaml, "")
s.state.Unlock()
stdout, stderr, err := ctlcmd.Run(mockContext, []string{"model"}, 0)
c.Check(err, ErrorMatches, "insufficient permissions to get model assertion for snap \"snap1\"")
c.Check(string(stdout), Equals, "")
c.Check(string(stderr), Equals, "cannot get model assertion for snap \"snap1\": must be either a gadget or a kernel snap, from the same publisher as the model or have the snapd-control interface\n")
}
func (s *modelSuite) TestHappyModelCommandIdenticalPublisher(c *C) {
// Test that verifies we can get the model assertion if we are the publisher
// of the snap that requests
s.addSnapDeclaration(c, "snap1-id", "canonical", "snap1")
s.setupBrands()
// set a model assertion
s.state.Lock()
current := s.brands.Model("canonical", "pc-model", map[string]any{
"architecture": "amd64",
"kernel": "pc-kernel",
"gadget": "pc",
"base": "core18",
})
err := assertstate.Add(s.state, current)
c.Assert(err, IsNil)
devicestatetest.SetDevice(s.state, &auth.DeviceState{
Brand: "canonical",
Model: "pc-model",
})
c.Assert(err, IsNil)
task := s.state.NewTask("test-task", "my test task")
setup := &hookstate.HookSetup{Snap: "snap1", Revision: snap.R(1), Hook: "test-hook"}
mockContext, err := hookstate.NewContext(task, s.state, setup, s.mockHandler, "")
c.Assert(err, IsNil)
mockInstalledSnap(c, s.state, snapBaseYaml, "")
mockInstalledSnap(c, s.state, snapYaml, "")
s.state.Unlock()
stdout, stderr, err := ctlcmd.Run(mockContext, []string{"model"}, 0)
// For this test we just check that no error is returned, we have other testsw
// that verifies formats for each case. So make sure that stderr is empty and that
// we get data printed on stdout.
c.Check(err, IsNil)
c.Check(len(string(stdout)) > 0, Equals, true)
c.Check(string(stderr), Equals, "")
}
func (s *modelSuite) TestHappyModelCommandSnapdControlPlug(c *C) {
// Verify that we can retrieve the model assertion in the case that we are
// not a gadget snap, or from the same publisher, but we do have the snapd-control
// interface connected.
s.setupBrands()
s.addSnapDeclaration(c, "snap1-control-id", "other-brand", "snap1-control")
// set a model assertion
s.state.Lock()
current := s.brands.Model("canonical", "pc-model", map[string]any{
"architecture": "amd64",
"kernel": "pc-kernel",
"gadget": "pc",
"base": "core18",
})
err := assertstate.Add(s.state, current)
c.Assert(err, IsNil)
devicestatetest.SetDevice(s.state, &auth.DeviceState{
Brand: "canonical",
Model: "pc-model",
})
c.Assert(err, IsNil)
task := s.state.NewTask("test-task", "my test task")
setup := &hookstate.HookSetup{Snap: "snap1-control", Revision: snap.R(1), Hook: "test-hook"}
mockContext, err := hookstate.NewContext(task, s.state, setup, s.mockHandler, "")
c.Assert(err, IsNil)
mockInstalledSnap(c, s.state, snapWithSnapdControlOnlyYaml, "")
s.state.Set("conns", map[string]any{
"snap1-control:plug core:slot": map[string]any{"interface": "snapd-control"},
})
s.state.Unlock()
stdout, stderr, err := ctlcmd.Run(mockContext, []string{"model"}, 0)
c.Check(err, IsNil)
c.Check(len(string(stdout)) > 0, Equals, true)
c.Check(string(stderr), Equals, "")
}
func (s *modelSuite) TestHappyModelCommandPublisherYaml(c *C) {
// Verify that we can read the model assertion when the snap has the same
// publisher as the model assertion.
s.addSnapDeclaration(c, "snap1-id", "canonical", "snap1")
s.setupBrands()
// set a model assertion
s.state.Lock()
current := s.brands.Model("canonical", "pc-model", map[string]any{
"architecture": "amd64",
"kernel": "pc-kernel",
"gadget": "pc",
"base": "core18",
})
err := assertstate.Add(s.state, current)
c.Assert(err, IsNil)
devicestatetest.SetDevice(s.state, &auth.DeviceState{
Brand: "canonical",
Model: "pc-model",
})
c.Assert(err, IsNil)
task := s.state.NewTask("test-task", "my test task")
setup := &hookstate.HookSetup{Snap: "snap1", Revision: snap.R(1), Hook: "test-hook"}
mockContext, err := hookstate.NewContext(task, s.state, setup, s.mockHandler, "")
c.Assert(err, IsNil)
mockInstalledSnap(c, s.state, snapYaml, "")
s.state.Unlock()
stdout, stderr, err := ctlcmd.Run(mockContext, []string{"model"}, 0)
c.Check(err, IsNil)
c.Check(string(stdout), Equals, fmt.Sprintf(`brand-id: canonical
model: pc-model
serial: -- (device not registered yet)
architecture: amd64
base: core18
gadget: pc
kernel: pc-kernel
timestamp: %s
`, current.Timestamp().Format(time.RFC3339)))
c.Check(string(stderr), Equals, "")
}
func (s *modelSuite) TestHappyModelCommandGadget(c *C) {
s.testHappyModelCommandForSnap(c, "gadget1", snapGadgetYaml)
}
func (s *modelSuite) TestHappyModelCommandKernel(c *C) {
s.testHappyModelCommandForSnap(c, "kernel1", snapKernelYaml)
}
func (s *modelSuite) testHappyModelCommandForSnap(c *C, snapName, snapYaml string) {
// This tests verifies that a snap that is a gadget can be used to
// get the model assertion, even if from a different publisher
s.addSnapDeclaration(c, snapName+"-id", "canonical", snapName)
s.setupBrands()
// set a model assertion
s.state.Lock()
current := s.brands.Model("my-brand", "pc-model", map[string]any{
"architecture": "amd64",
"kernel": "pc-kernel",
"gadget": "pc",
"base": "core18",
})
err := assertstate.Add(s.state, current)
c.Assert(err, IsNil)
devicestatetest.SetDevice(s.state, &auth.DeviceState{
Brand: "my-brand",
Model: "pc-model",
})
c.Assert(err, IsNil)
task := s.state.NewTask("test-task", "my test task")
setup := &hookstate.HookSetup{Snap: snapName, Revision: snap.R(1), Hook: "test-hook"}
mockContext, err := hookstate.NewContext(task, s.state, setup, s.mockHandler, "")
c.Assert(err, IsNil)
mockInstalledSnap(c, s.state, snapYaml, "")
s.state.Unlock()
stdout, stderr, err := ctlcmd.Run(mockContext, []string{"model"}, 0)
c.Check(err, IsNil)
c.Check(string(stdout), Equals, fmt.Sprintf(`brand-id: my-brand
model: pc-model
serial: -- (device not registered yet)
architecture: amd64
base: core18
gadget: pc
kernel: pc-kernel
timestamp: %s
`, current.Timestamp().Format(time.RFC3339)))
c.Check(string(stderr), Equals, "")
}
func (s *modelSuite) TestHappyModelCommandGadgetJson(c *C) {
s.addSnapDeclaration(c, "gadget1-id", "canonical", "gadget1")
s.setupBrands()
// set a model assertion
s.state.Lock()
current := s.brands.Model("canonical", "pc-model", map[string]any{
"architecture": "amd64",
"kernel": "pc-kernel",
"gadget": "pc",
"base": "core18",
})
err := assertstate.Add(s.state, current)
c.Assert(err, IsNil)
devicestatetest.SetDevice(s.state, &auth.DeviceState{
Brand: "canonical",
Model: "pc-model",
})
c.Assert(err, IsNil)
task := s.state.NewTask("test-task", "my test task")
setup := &hookstate.HookSetup{Snap: "gadget1", Revision: snap.R(1), Hook: "test-hook"}
mockContext, err := hookstate.NewContext(task, s.state, setup, s.mockHandler, "")
c.Assert(err, IsNil)
mockInstalledSnap(c, s.state, snapGadgetYaml, "")
s.state.Unlock()
stdout, stderr, err := ctlcmd.Run(mockContext, []string{"model", "--json"}, 0)
c.Check(err, IsNil)
c.Check(string(stdout), Equals, fmt.Sprintf(`{
"architecture": "amd64",
"base": "core18",
"brand-id": "canonical",
"gadget": "pc",
"kernel": "pc-kernel",
"model": "pc-model",
"serial": null,
"timestamp": "%s"
}`, current.Timestamp().Format(time.RFC3339)))
c.Check(string(stderr), Equals, "")
}
func (s *modelSuite) TestHappyModelCommandAssertionGadgetYaml(c *C) {
s.addSnapDeclaration(c, "gadget1-id", "canonical", "gadget1")
s.setupBrands()
// set a model assertion
s.state.Lock()
current := s.brands.Model("canonical", "pc-model", map[string]any{
"architecture": "amd64",
"kernel": "pc-kernel",
"gadget": "pc",
"base": "core18",
})
err := assertstate.Add(s.state, current)
c.Assert(err, IsNil)
devicestatetest.SetDevice(s.state, &auth.DeviceState{
Brand: "canonical",
Model: "pc-model",
})
c.Assert(err, IsNil)
task := s.state.NewTask("test-task", "my test task")
setup := &hookstate.HookSetup{Snap: "gadget1", Revision: snap.R(1), Hook: "test-hook"}
mockContext, err := hookstate.NewContext(task, s.state, setup, s.mockHandler, "")
c.Assert(err, IsNil)
mockInstalledSnap(c, s.state, snapGadgetYaml, "")
s.state.Unlock()
stdout, stderr, err := ctlcmd.Run(mockContext, []string{"model", "--assertion"}, 0)
c.Check(err, IsNil)
c.Check(string(stdout), Equals, string(asserts.Encode(current)))
c.Check(string(stderr), Equals, "")
}
func (s *modelSuite) TestHappyModelCommandAssertionGadgetJson(c *C) {
s.addSnapDeclaration(c, "gadget1-id", "canonical", "gadget1")
s.setupBrands()
// set a model assertion
s.state.Lock()
current := s.brands.Model("canonical", "pc-model", map[string]any{
"architecture": "amd64",
"kernel": "pc-kernel",
"gadget": "pc",
"base": "core18",
})
err := assertstate.Add(s.state, current)
c.Assert(err, IsNil)
devicestatetest.SetDevice(s.state, &auth.DeviceState{
Brand: "canonical",
Model: "pc-model",
})
c.Assert(err, IsNil)
task := s.state.NewTask("test-task", "my test task")
setup := &hookstate.HookSetup{Snap: "gadget1", Revision: snap.R(1), Hook: "test-hook"}
mockContext, err := hookstate.NewContext(task, s.state, setup, s.mockHandler, "")
c.Assert(err, IsNil)
mockInstalledSnap(c, s.state, snapGadgetYaml, "")
s.state.Unlock()
stdout, stderr, err := ctlcmd.Run(mockContext, []string{"model", "--assertion", "--json"}, 0)
c.Check(err, IsNil)
c.Check(string(stdout), Equals, fmt.Sprintf(`{
"headers": {
"architecture": "amd64",
"authority-id": "canonical",
"base": "core18",
"brand-id": "canonical",
"gadget": "pc",
"kernel": "pc-kernel",
"model": "pc-model",
"series": "16",
"sign-key-sha3-384": "%s",
"timestamp": "%s",
"type": "model"
}
}`, current.SignKeyID(), current.Timestamp().Format(time.RFC3339)))
c.Check(string(stderr), Equals, "")
}
func (s *modelSuite) TestRunWithoutHook(c *C) {
s.addSnapDeclaration(c, "gadget1-id", "canonical", "gadget1")
s.setupBrands()
// set a model assertion
s.state.Lock()
current := s.brands.Model("canonical", "pc-model", map[string]any{
"architecture": "amd64",
"kernel": "pc-kernel",
"gadget": "pc",
"base": "core18",
})
err := assertstate.Add(s.state, current)
c.Assert(err, IsNil)
devicestatetest.SetDevice(s.state, &auth.DeviceState{
Brand: "canonical",
Model: "pc-model",
})
c.Assert(err, IsNil)
setup := &hookstate.HookSetup{Snap: "gadget1", Revision: snap.R(1)}
mockContext, err := hookstate.NewContext(nil, s.state, setup, nil, "")
c.Assert(err, IsNil)
mockInstalledSnap(c, s.state, snapGadgetYaml, "")
s.state.Unlock()
stdout, stderr, err := ctlcmd.Run(mockContext, []string{"model", "--json"}, 0)
c.Check(err, IsNil)
c.Check(string(stdout), Equals, fmt.Sprintf(`{
"architecture": "amd64",
"base": "core18",
"brand-id": "canonical",
"gadget": "pc",
"kernel": "pc-kernel",
"model": "pc-model",
"serial": null,
"timestamp": "%s"
}`, current.Timestamp().Format(time.RFC3339)))
c.Check(string(stderr), Equals, "")
}
func (s *modelSuite) TestLongPublisherUnproven(c *C) {
snapInfo := &snap.Info{
Publisher: snap.StoreAccount{
ID: "canonical-id",
Username: "canonical",
DisplayName: "Canonical",
Validation: "unproven",
},
}
c.Assert(ctlcmd.FormatLongPublisher(snapInfo, ""), Equals, "Canonical")
}
func (s *modelSuite) TestLongPublisherStarred(c *C) {
snapInfo := &snap.Info{
Publisher: snap.StoreAccount{
ID: "canonical-id",
Username: "canonical",
DisplayName: "Canonical",
Validation: "starred",
},
}
c.Assert(ctlcmd.FormatLongPublisher(snapInfo, ""), Equals, "Canonical*")
}
func (s *modelSuite) TestLongPublisherVerified(c *C) {
snapInfo := &snap.Info{
Publisher: snap.StoreAccount{
ID: "canonical-id",
Username: "canonical",
DisplayName: "Canonical",
Validation: "verified",
},
}
c.Assert(ctlcmd.FormatLongPublisher(snapInfo, ""), Equals, "Canonical**")
}
func (s *modelSuite) signSerial(accountID, model, serial string, timestamp time.Time, extras ...map[string]any) *asserts.Serial {
encodedPubKey, _ := asserts.EncodePublicKey(brandPrivKey2.PublicKey())
headers := map[string]any{
"series": "16",
"serial": serial,
"brand-id": accountID,
"model": model,
"timestamp": timestamp.Format(time.RFC3339),
"device-key": string(encodedPubKey),
"device-key-sha3-384": brandPrivKey2.PublicKey().ID(),
}
for _, extra := range extras {
for k, v := range extra {
headers[k] = v
}
}
signer := s.brands.Signing(accountID)
serialAs, err := signer.Sign(asserts.SerialType, headers, nil, "")
if err != nil {
panic(err)
}
return serialAs.(*asserts.Serial)
}
func (s *modelSuite) TestFindSerialAssertionNone(c *C) {
s.setupBrands()
model := s.brands.Model("canonical", "pc-model", map[string]any{
"architecture": "amd64",
"kernel": "pc-kernel",
"gadget": "pc",
"base": "core18",
})
s.state.Lock()
defer s.state.Unlock()
assertstatetest.AddMany(s.state, model)
result, err := ctlcmd.FindSerialAssertion(s.state, model)
c.Assert(errors.Is(err, &asserts.NotFoundError{}), Equals, true)
c.Assert(result, IsNil)
}
func (s *modelSuite) TestFindSerialAssertionMatch(c *C) {
s.setupBrands()
model := s.brands.Model("canonical", "pc-model", map[string]any{
"architecture": "amd64",
"kernel": "pc-kernel",
"gadget": "pc",
"base": "core18",
})
serial := s.signSerial("canonical", "pc-model", "1", time.Now(), map[string]any{
"architecture": "amd64",
"kernel": "pc-kernel",
"gadget": "pc",
"base": "core18",
})
s.state.Lock()
defer s.state.Unlock()
assertstatetest.AddMany(s.state, model, serial)
result, err := ctlcmd.FindSerialAssertion(s.state, model)
c.Assert(err, IsNil)
c.Check(result.Serial(), Equals, "1")
}
func (s *modelSuite) TestFindSerialAssertionMultiple(c *C) {
// In case of multiple matches, we should return the one with the
// newest timestamp.
s.setupBrands()
now := time.Now()
tomorrow := now.AddDate(0, 0, 1)
model := s.brands.Model("canonical", "pc-model", map[string]any{
"architecture": "amd64",
"kernel": "pc-kernel",
"gadget": "pc",
"base": "core18",
})
serial := s.signSerial("canonical", "pc-model", "1", now, map[string]any{
"architecture": "amd64",
"kernel": "pc-kernel",
"gadget": "pc",
"base": "core18",
})
serialnext := s.signSerial("canonical", "pc-model", "2", tomorrow, map[string]any{
"architecture": "amd64",
"kernel": "pc-kernel",
"gadget": "pc",
"base": "core18",
})
s.state.Lock()
defer s.state.Unlock()
assertstatetest.AddMany(s.state, model, serial, serialnext)
result, err := ctlcmd.FindSerialAssertion(s.state, model)
c.Assert(err, IsNil)
c.Check(result.Timestamp(), Equals, serialnext.Timestamp())
}
|