github.com/ethanhsieh/snapd@v0.0.0-20210615102523-3db9b8e4edc5/interfaces/policy/basedeclaration_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016-2018 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package policy_test 21 22 import ( 23 "fmt" 24 "strings" 25 26 . "gopkg.in/check.v1" 27 28 "github.com/snapcore/snapd/asserts" 29 "github.com/snapcore/snapd/interfaces" 30 "github.com/snapcore/snapd/interfaces/builtin" 31 "github.com/snapcore/snapd/interfaces/policy" 32 "github.com/snapcore/snapd/release" 33 "github.com/snapcore/snapd/snap" 34 "github.com/snapcore/snapd/snap/snaptest" 35 "github.com/snapcore/snapd/strutil" 36 "github.com/snapcore/snapd/testutil" 37 ) 38 39 type baseDeclSuite struct { 40 baseDecl *asserts.BaseDeclaration 41 restoreSanitize func() 42 } 43 44 var _ = Suite(&baseDeclSuite{}) 45 46 func (s *baseDeclSuite) SetUpSuite(c *C) { 47 s.restoreSanitize = snap.MockSanitizePlugsSlots(func(snapInfo *snap.Info) {}) 48 s.baseDecl = asserts.BuiltinBaseDeclaration() 49 } 50 51 func (s *baseDeclSuite) TearDownSuite(c *C) { 52 s.restoreSanitize() 53 } 54 55 func (s *baseDeclSuite) connectCand(c *C, iface, slotYaml, plugYaml string) *policy.ConnectCandidate { 56 if slotYaml == "" { 57 slotYaml = fmt.Sprintf(`name: slot-snap 58 version: 0 59 slots: 60 %s: 61 `, iface) 62 } 63 if plugYaml == "" { 64 plugYaml = fmt.Sprintf(`name: plug-snap 65 version: 0 66 plugs: 67 %s: 68 `, iface) 69 } 70 slotSnap := snaptest.MockInfo(c, slotYaml, nil) 71 plugSnap := snaptest.MockInfo(c, plugYaml, nil) 72 return &policy.ConnectCandidate{ 73 Plug: interfaces.NewConnectedPlug(plugSnap.Plugs[iface], nil, nil), 74 Slot: interfaces.NewConnectedSlot(slotSnap.Slots[iface], nil, nil), 75 BaseDeclaration: s.baseDecl, 76 } 77 } 78 79 func (s *baseDeclSuite) installSlotCand(c *C, iface string, snapType snap.Type, yaml string) *policy.InstallCandidate { 80 if yaml == "" { 81 yaml = fmt.Sprintf(`name: install-slot-snap 82 version: 0 83 type: %s 84 slots: 85 %s: 86 `, snapType, iface) 87 } 88 snap := snaptest.MockInfo(c, yaml, nil) 89 return &policy.InstallCandidate{ 90 Snap: snap, 91 BaseDeclaration: s.baseDecl, 92 } 93 } 94 95 func (s *baseDeclSuite) installPlugCand(c *C, iface string, snapType snap.Type, yaml string) *policy.InstallCandidate { 96 if yaml == "" { 97 yaml = fmt.Sprintf(`name: install-plug-snap 98 version: 0 99 type: %s 100 plugs: 101 %s: 102 `, snapType, iface) 103 } 104 snap := snaptest.MockInfo(c, yaml, nil) 105 return &policy.InstallCandidate{ 106 Snap: snap, 107 BaseDeclaration: s.baseDecl, 108 } 109 } 110 111 const declTempl = `type: snap-declaration 112 authority-id: canonical 113 series: 16 114 snap-name: @name@ 115 snap-id: @snapid@ 116 publisher-id: @publisher@ 117 @plugsSlots@ 118 timestamp: 2016-09-30T12:00:00Z 119 sign-key-sha3-384: Jv8_JiHiIzJVcO9M55pPdqSDWUvuhfDIBJUS-3VW7F_idjix7Ffn5qMxB21ZQuij 120 121 AXNpZw==` 122 123 func (s *baseDeclSuite) mockSnapDecl(c *C, name, snapID, publisher string, plugsSlots string) *asserts.SnapDeclaration { 124 encoded := strings.Replace(declTempl, "@name@", name, 1) 125 encoded = strings.Replace(encoded, "@snapid@", snapID, 1) 126 encoded = strings.Replace(encoded, "@publisher@", publisher, 1) 127 if plugsSlots != "" { 128 encoded = strings.Replace(encoded, "@plugsSlots@", strings.TrimSpace(plugsSlots), 1) 129 } else { 130 encoded = strings.Replace(encoded, "@plugsSlots@\n", "", 1) 131 } 132 a, err := asserts.Decode([]byte(encoded)) 133 c.Assert(err, IsNil) 134 return a.(*asserts.SnapDeclaration) 135 } 136 137 func (s *baseDeclSuite) TestAutoConnection(c *C) { 138 all := builtin.Interfaces() 139 140 // these have more complex or in flux policies and have their 141 // own separate tests 142 snowflakes := map[string]bool{ 143 "content": true, 144 "core-support": true, 145 "home": true, 146 "lxd-support": true, 147 "multipass-support": true, 148 "packagekit-control": true, 149 "snapd-control": true, 150 "dummy": true, 151 } 152 153 // these simply auto-connect, anything else doesn't 154 autoconnect := map[string]bool{ 155 "audio-playback": true, 156 "browser-support": true, 157 "desktop": true, 158 "desktop-legacy": true, 159 "gsettings": true, 160 "media-hub": true, 161 "mir": true, 162 "network": true, 163 "network-bind": true, 164 "network-status": true, 165 "online-accounts-service": true, 166 "opengl": true, 167 "optical-drive": true, 168 "screen-inhibit-control": true, 169 "ubuntu-download-manager": true, 170 "unity7": true, 171 "unity8": true, 172 "upower-observe": true, 173 "wayland": true, 174 "x11": true, 175 } 176 177 for _, iface := range all { 178 if snowflakes[iface.Name()] { 179 continue 180 } 181 expected := autoconnect[iface.Name()] 182 comm := Commentf(iface.Name()) 183 184 // check base declaration 185 cand := s.connectCand(c, iface.Name(), "", "") 186 arity, err := cand.CheckAutoConnect() 187 if expected { 188 c.Check(err, IsNil, comm) 189 c.Check(arity.SlotsPerPlugAny(), Equals, false) 190 } else { 191 c.Check(err, NotNil, comm) 192 } 193 } 194 } 195 196 func (s *baseDeclSuite) TestAutoConnectPlugSlot(c *C) { 197 all := builtin.Interfaces() 198 199 // these have more complex or in flux policies and have their 200 // own separate tests 201 snowflakes := map[string]bool{ 202 "classic-support": true, 203 "content": true, 204 "home": true, 205 "lxd-support": true, 206 // netlink-driver needs the family-name attributes to match 207 "netlink-driver": true, 208 } 209 210 for _, iface := range all { 211 if snowflakes[iface.Name()] { 212 continue 213 } 214 c.Check(iface.AutoConnect(nil, nil), Equals, true) 215 } 216 } 217 218 func (s *baseDeclSuite) TestInterimAutoConnectionHome(c *C) { 219 restore := release.MockOnClassic(true) 220 defer restore() 221 cand := s.connectCand(c, "home", "", "") 222 arity, err := cand.CheckAutoConnect() 223 c.Check(err, IsNil) 224 c.Check(arity.SlotsPerPlugAny(), Equals, false) 225 226 release.OnClassic = false 227 _, err = cand.CheckAutoConnect() 228 c.Check(err, ErrorMatches, `auto-connection denied by slot rule of interface \"home\"`) 229 } 230 231 func (s *baseDeclSuite) TestHomeReadAll(c *C) { 232 const plugYaml = `name: plug-snap 233 version: 0 234 plugs: 235 home: 236 read: all 237 ` 238 restore := release.MockOnClassic(true) 239 defer restore() 240 cand := s.connectCand(c, "home", "", plugYaml) 241 err := cand.Check() 242 c.Check(err, NotNil) 243 244 _, err = cand.CheckAutoConnect() 245 c.Check(err, NotNil) 246 247 release.OnClassic = false 248 err = cand.Check() 249 c.Check(err, NotNil) 250 251 _, err = cand.CheckAutoConnect() 252 c.Check(err, NotNil) 253 } 254 255 func (s *baseDeclSuite) TestHomeReadDefault(c *C) { 256 const plugYaml = `name: plug-snap 257 version: 0 258 plugs: 259 home: null 260 ` 261 restore := release.MockOnClassic(true) 262 defer restore() 263 cand := s.connectCand(c, "home", "", plugYaml) 264 err := cand.Check() 265 c.Check(err, IsNil) 266 267 // Same as TestInterimAutoConnectionHome() 268 arity, err := cand.CheckAutoConnect() 269 c.Check(err, IsNil) 270 c.Check(arity.SlotsPerPlugAny(), Equals, false) 271 272 release.OnClassic = false 273 err = cand.Check() 274 c.Check(err, IsNil) 275 276 // Same as TestInterimAutoConnectionHome() 277 _, err = cand.CheckAutoConnect() 278 c.Check(err, NotNil) 279 } 280 281 func (s *baseDeclSuite) TestAutoConnectionSnapdControl(c *C) { 282 cand := s.connectCand(c, "snapd-control", "", "") 283 _, err := cand.CheckAutoConnect() 284 c.Check(err, NotNil) 285 c.Assert(err, ErrorMatches, "auto-connection denied by plug rule of interface \"snapd-control\"") 286 287 plugsSlots := ` 288 plugs: 289 snapd-control: 290 allow-auto-connection: true 291 ` 292 293 lxdDecl := s.mockSnapDecl(c, "some-snap", "J60k4JY0HppjwOjW8dZdYc8obXKxujRu", "canonical", plugsSlots) 294 cand.PlugSnapDeclaration = lxdDecl 295 arity, err := cand.CheckAutoConnect() 296 c.Check(err, IsNil) 297 c.Check(arity.SlotsPerPlugAny(), Equals, false) 298 } 299 300 func (s *baseDeclSuite) TestAutoConnectionContent(c *C) { 301 // random snaps cannot connect with content 302 // (Sanitize* will now also block this) 303 cand := s.connectCand(c, "content", "", "") 304 _, err := cand.CheckAutoConnect() 305 c.Check(err, NotNil) 306 307 slotDecl1 := s.mockSnapDecl(c, "slot-snap", "slot-snap-id", "pub1", "") 308 plugDecl1 := s.mockSnapDecl(c, "plug-snap", "plug-snap-id", "pub1", "") 309 plugDecl2 := s.mockSnapDecl(c, "plug-snap", "plug-snap-id", "pub2", "") 310 311 // same publisher, same content 312 cand = s.connectCand(c, "stuff", ` 313 name: slot-snap 314 version: 0 315 slots: 316 stuff: 317 interface: content 318 content: mk1 319 `, ` 320 name: plug-snap 321 version: 0 322 plugs: 323 stuff: 324 interface: content 325 content: mk1 326 `) 327 cand.SlotSnapDeclaration = slotDecl1 328 cand.PlugSnapDeclaration = plugDecl1 329 arity, err := cand.CheckAutoConnect() 330 c.Check(err, IsNil) 331 c.Check(arity.SlotsPerPlugAny(), Equals, false) 332 333 // different publisher, same content 334 cand.SlotSnapDeclaration = slotDecl1 335 cand.PlugSnapDeclaration = plugDecl2 336 _, err = cand.CheckAutoConnect() 337 c.Check(err, NotNil) 338 339 // same publisher, different content 340 cand = s.connectCand(c, "stuff", `name: slot-snap 341 version: 0 342 slots: 343 stuff: 344 interface: content 345 content: mk1 346 `, ` 347 name: plug-snap 348 version: 0 349 plugs: 350 stuff: 351 interface: content 352 content: mk2 353 `) 354 cand.SlotSnapDeclaration = slotDecl1 355 cand.PlugSnapDeclaration = plugDecl1 356 _, err = cand.CheckAutoConnect() 357 c.Check(err, NotNil) 358 } 359 360 func (s *baseDeclSuite) TestAutoConnectionLxdSupportOverride(c *C) { 361 // by default, don't auto-connect 362 cand := s.connectCand(c, "lxd-support", "", "") 363 _, err := cand.CheckAutoConnect() 364 c.Check(err, NotNil) 365 366 plugsSlots := ` 367 plugs: 368 lxd-support: 369 allow-auto-connection: true 370 ` 371 372 lxdDecl := s.mockSnapDecl(c, "lxd", "J60k4JY0HppjwOjW8dZdYc8obXKxujRu", "canonical", plugsSlots) 373 cand.PlugSnapDeclaration = lxdDecl 374 _, err = cand.CheckAutoConnect() 375 c.Check(err, IsNil) 376 } 377 378 func (s *baseDeclSuite) TestAutoConnectionLxdSupportOverrideRevoke(c *C) { 379 cand := s.connectCand(c, "lxd-support", "", "") 380 plugsSlots := ` 381 plugs: 382 lxd-support: 383 allow-auto-connection: false 384 ` 385 386 lxdDecl := s.mockSnapDecl(c, "notlxd", "J60k4JY0HppjwOjW8dZdYc8obXKxujRu", "canonical", plugsSlots) 387 cand.PlugSnapDeclaration = lxdDecl 388 _, err := cand.CheckAutoConnect() 389 c.Check(err, NotNil) 390 c.Assert(err, ErrorMatches, "auto-connection not allowed by plug rule of interface \"lxd-support\" for \"notlxd\" snap") 391 } 392 393 func (s *baseDeclSuite) TestAutoConnectionKernelModuleControlOverride(c *C) { 394 cand := s.connectCand(c, "kernel-module-control", "", "") 395 _, err := cand.CheckAutoConnect() 396 c.Check(err, NotNil) 397 c.Assert(err, ErrorMatches, "auto-connection denied by plug rule of interface \"kernel-module-control\"") 398 399 plugsSlots := ` 400 plugs: 401 kernel-module-control: 402 allow-auto-connection: true 403 ` 404 405 snapDecl := s.mockSnapDecl(c, "some-snap", "J60k4JY0HppjwOjW8dZdYc8obXKxujRu", "canonical", plugsSlots) 406 cand.PlugSnapDeclaration = snapDecl 407 _, err = cand.CheckAutoConnect() 408 c.Check(err, IsNil) 409 } 410 411 func (s *baseDeclSuite) TestAutoConnectionDockerSupportOverride(c *C) { 412 cand := s.connectCand(c, "docker-support", "", "") 413 _, err := cand.CheckAutoConnect() 414 c.Check(err, NotNil) 415 c.Assert(err, ErrorMatches, "auto-connection denied by plug rule of interface \"docker-support\"") 416 417 plugsSlots := ` 418 plugs: 419 docker-support: 420 allow-auto-connection: true 421 ` 422 423 snapDecl := s.mockSnapDecl(c, "some-snap", "J60k4JY0HppjwOjW8dZdYc8obXKxujRu", "canonical", plugsSlots) 424 cand.PlugSnapDeclaration = snapDecl 425 _, err = cand.CheckAutoConnect() 426 c.Check(err, IsNil) 427 } 428 429 func (s *baseDeclSuite) TestAutoConnectionClassicSupportOverride(c *C) { 430 cand := s.connectCand(c, "classic-support", "", "") 431 _, err := cand.CheckAutoConnect() 432 c.Check(err, NotNil) 433 c.Assert(err, ErrorMatches, "auto-connection denied by plug rule of interface \"classic-support\"") 434 435 plugsSlots := ` 436 plugs: 437 classic-support: 438 allow-auto-connection: true 439 ` 440 441 snapDecl := s.mockSnapDecl(c, "classic", "J60k4JY0HppjwOjW8dZdYc8obXKxujRu", "canonical", plugsSlots) 442 cand.PlugSnapDeclaration = snapDecl 443 _, err = cand.CheckAutoConnect() 444 c.Check(err, IsNil) 445 } 446 447 func (s *baseDeclSuite) TestAutoConnectionKubernetesSupportOverride(c *C) { 448 cand := s.connectCand(c, "kubernetes-support", "", "") 449 _, err := cand.CheckAutoConnect() 450 c.Check(err, NotNil) 451 c.Assert(err, ErrorMatches, "auto-connection denied by plug rule of interface \"kubernetes-support\"") 452 453 plugsSlots := ` 454 plugs: 455 kubernetes-support: 456 allow-auto-connection: true 457 ` 458 459 snapDecl := s.mockSnapDecl(c, "some-snap", "J60k4JY0HppjwOjW8dZdYc8obXKxujRu", "canonical", plugsSlots) 460 cand.PlugSnapDeclaration = snapDecl 461 _, err = cand.CheckAutoConnect() 462 c.Check(err, IsNil) 463 } 464 465 func (s *baseDeclSuite) TestAutoConnectionGreengrassSupportOverride(c *C) { 466 cand := s.connectCand(c, "greengrass-support", "", "") 467 _, err := cand.CheckAutoConnect() 468 c.Check(err, NotNil) 469 c.Assert(err, ErrorMatches, "auto-connection denied by plug rule of interface \"greengrass-support\"") 470 471 plugsSlots := ` 472 plugs: 473 greengrass-support: 474 allow-auto-connection: true 475 ` 476 477 snapDecl := s.mockSnapDecl(c, "some-snap", "J60k4JY0HppjwOjW8dZdYc8obXKxujRu", "canonical", plugsSlots) 478 cand.PlugSnapDeclaration = snapDecl 479 _, err = cand.CheckAutoConnect() 480 c.Check(err, IsNil) 481 } 482 483 func (s *baseDeclSuite) TestAutoConnectionMultipassSupportOverride(c *C) { 484 cand := s.connectCand(c, "multipass-support", "", "") 485 _, err := cand.CheckAutoConnect() 486 c.Check(err, NotNil) 487 c.Assert(err, ErrorMatches, "auto-connection denied by plug rule of interface \"multipass-support\"") 488 489 plugsSlots := ` 490 plugs: 491 multipass-support: 492 allow-auto-connection: true 493 ` 494 495 snapDecl := s.mockSnapDecl(c, "multipass-snap", "J60k4JY0HppjwOjW8dZdYc8obXKxujRu", "canonical", plugsSlots) 496 cand.PlugSnapDeclaration = snapDecl 497 _, err = cand.CheckAutoConnect() 498 c.Check(err, IsNil) 499 } 500 501 func (s *baseDeclSuite) TestAutoConnectionBlockDevicesOverride(c *C) { 502 cand := s.connectCand(c, "block-devices", "", "") 503 _, err := cand.CheckAutoConnect() 504 c.Check(err, NotNil) 505 c.Assert(err, ErrorMatches, "auto-connection denied by plug rule of interface \"block-devices\"") 506 507 plugsSlots := ` 508 plugs: 509 block-devices: 510 allow-auto-connection: true 511 ` 512 513 snapDecl := s.mockSnapDecl(c, "some-snap", "J60k4JY0HppjwOjW8dZdYc8obXKxujRu", "canonical", plugsSlots) 514 cand.PlugSnapDeclaration = snapDecl 515 _, err = cand.CheckAutoConnect() 516 c.Check(err, IsNil) 517 } 518 519 func (s *baseDeclSuite) TestAutoConnectionPackagekitControlOverride(c *C) { 520 cand := s.connectCand(c, "packagekit-control", "", "") 521 _, err := cand.CheckAutoConnect() 522 c.Check(err, NotNil) 523 c.Assert(err, ErrorMatches, "auto-connection denied by plug rule of interface \"packagekit-control\"") 524 525 plugsSlots := ` 526 plugs: 527 packagekit-control: 528 allow-auto-connection: true 529 ` 530 531 snapDecl := s.mockSnapDecl(c, "some-snap", "J60k4JY0HppjwOjW8dZdYc8obXKxujRu", "canonical", plugsSlots) 532 cand.PlugSnapDeclaration = snapDecl 533 _, err = cand.CheckAutoConnect() 534 c.Check(err, IsNil) 535 } 536 537 func (s *baseDeclSuite) TestAutoConnectionOverrideMultiple(c *C) { 538 plugsSlots := ` 539 plugs: 540 network-bind: 541 allow-auto-connection: true 542 network-control: 543 allow-auto-connection: true 544 kernel-module-control: 545 allow-auto-connection: true 546 system-observe: 547 allow-auto-connection: true 548 hardware-observe: 549 allow-auto-connection: true 550 ` 551 552 snapDecl := s.mockSnapDecl(c, "some-snap", "J60k4JY0HppjwOjW8dZdYc8obXKxujRu", "canonical", plugsSlots) 553 554 all := builtin.Interfaces() 555 // these are a mixture interfaces that the snap plugs 556 plugged := map[string]bool{ 557 "network-bind": true, 558 "network-control": true, 559 "kernel-module-control": true, 560 "system-observe": true, 561 "hardware-observe": true, 562 } 563 for _, iface := range all { 564 if !plugged[iface.Name()] { 565 continue 566 } 567 568 cand := s.connectCand(c, iface.Name(), "", "") 569 cand.PlugSnapDeclaration = snapDecl 570 arity, err := cand.CheckAutoConnect() 571 c.Check(err, IsNil) 572 c.Check(arity.SlotsPerPlugAny(), Equals, false) 573 } 574 } 575 576 // describe installation rules for slots succinctly for cross-checking, 577 // if an interface is not mentioned here a slot of its type can only 578 // be installed by a core snap (and this was taken care by 579 // BeforePrepareSlot), 580 // otherwise the entry for the interface is the list of snap types it 581 // can be installed by (using the declaration naming); 582 // ATM a nil entry means even stricter rules that would need be tested 583 // separately and whose implementation is in flux for now 584 var ( 585 slotInstallation = map[string][]string{ 586 // other 587 "adb-support": {"core"}, 588 "audio-playback": {"app", "core"}, 589 "audio-record": {"app", "core"}, 590 "autopilot-introspection": {"core"}, 591 "avahi-control": {"app", "core"}, 592 "avahi-observe": {"app", "core"}, 593 "bluez": {"app", "core"}, 594 "bool-file": {"core", "gadget"}, 595 "browser-support": {"core"}, 596 "content": {"app", "gadget"}, 597 "core-support": {"core"}, 598 "cups": {"app"}, 599 "cups-control": {"app", "core"}, 600 "dbus": {"app"}, 601 "docker-support": {"core"}, 602 "desktop-launch": {"core"}, 603 "dsp": {"core", "gadget"}, 604 "dummy": {"app"}, 605 "fwupd": {"app", "core"}, 606 "gpio": {"core", "gadget"}, 607 "gpio-control": {"core"}, 608 "greengrass-support": {"core"}, 609 "hidraw": {"core", "gadget"}, 610 "i2c": {"core", "gadget"}, 611 "iio": {"core", "gadget"}, 612 "kubernetes-support": {"core"}, 613 "location-control": {"app"}, 614 "location-observe": {"app"}, 615 "lxd-support": {"core"}, 616 "maliit": {"app"}, 617 "media-hub": {"app", "core"}, 618 "mir": {"app"}, 619 "modem-manager": {"app", "core"}, 620 "mpris": {"app"}, 621 "netlink-driver": {"core", "gadget"}, 622 "network-manager": {"app", "core"}, 623 "network-manager-observe": {"app", "core"}, 624 "network-status": {"core"}, 625 "ofono": {"app", "core"}, 626 "online-accounts-service": {"app"}, 627 "power-control": {"core"}, 628 "ppp": {"core"}, 629 "pulseaudio": {"app", "core"}, 630 "pwm": {"core", "gadget"}, 631 "raw-volume": {"core", "gadget"}, 632 "serial-port": {"core", "gadget"}, 633 "spi": {"core", "gadget"}, 634 "storage-framework-service": {"app"}, 635 "thumbnailer-service": {"app"}, 636 "ubuntu-download-manager": {"app"}, 637 "udisks2": {"app", "core"}, 638 "uhid": {"core"}, 639 "uio": {"core", "gadget"}, 640 "unity8": {"app"}, 641 "unity8-calendar": {"app"}, 642 "unity8-contacts": {"app"}, 643 "upower-observe": {"app", "core"}, 644 "wayland": {"app", "core"}, 645 "x11": {"app", "core"}, 646 // snowflakes 647 "classic-support": nil, 648 "docker": nil, 649 "lxd": nil, 650 } 651 652 restrictedPlugInstallation = map[string][]string{ 653 "core-support": {"core"}, 654 } 655 656 snapTypeMap = map[string]snap.Type{ 657 "core": snap.TypeOS, 658 "app": snap.TypeApp, 659 "kernel": snap.TypeKernel, 660 "gadget": snap.TypeGadget, 661 } 662 ) 663 664 func (s *baseDeclSuite) TestSlotInstallation(c *C) { 665 all := builtin.Interfaces() 666 667 for _, iface := range all { 668 types, ok := slotInstallation[iface.Name()] 669 if !ok { // common ones, only core can install them, 670 types = []string{"core"} 671 } 672 673 if types == nil { 674 // snowflake needs to be tested specially 675 continue 676 } 677 for name, snapType := range snapTypeMap { 678 ok := strutil.ListContains(types, name) 679 ic := s.installSlotCand(c, iface.Name(), snapType, ``) 680 err := ic.Check() 681 comm := Commentf("%s by %s snap", iface.Name(), name) 682 if ok { 683 c.Check(err, IsNil, comm) 684 } else { 685 c.Check(err, NotNil, comm) 686 } 687 } 688 } 689 690 // test docker specially 691 ic := s.installSlotCand(c, "docker", snap.TypeApp, ``) 692 err := ic.Check() 693 c.Assert(err, Not(IsNil)) 694 c.Assert(err, ErrorMatches, "installation not allowed by \"docker\" slot rule of interface \"docker\"") 695 696 // test lxd specially 697 ic = s.installSlotCand(c, "lxd", snap.TypeApp, ``) 698 err = ic.Check() 699 c.Assert(err, Not(IsNil)) 700 c.Assert(err, ErrorMatches, "installation not allowed by \"lxd\" slot rule of interface \"lxd\"") 701 } 702 703 func (s *baseDeclSuite) TestPlugInstallation(c *C) { 704 all := builtin.Interfaces() 705 706 restricted := map[string]bool{ 707 "block-devices": true, 708 "classic-support": true, 709 "desktop-launch": true, 710 "dm-crypt": true, 711 "docker-support": true, 712 "greengrass-support": true, 713 "gpio-control": true, 714 "kernel-module-control": true, 715 "kubernetes-support": true, 716 "lxd-support": true, 717 "multipass-support": true, 718 "packagekit-control": true, 719 "personal-files": true, 720 "snapd-control": true, 721 "system-files": true, 722 "tee": true, 723 "uinput": true, 724 "unity8": true, 725 } 726 727 for _, iface := range all { 728 types, ok := restrictedPlugInstallation[iface.Name()] 729 // If plug installation is restricted to specific snap types we 730 // need to make sure this is really the case here. If that is not 731 // the case we continue as normal. 732 if ok { 733 for name, snapType := range snapTypeMap { 734 ok := strutil.ListContains(types, name) 735 ic := s.installPlugCand(c, iface.Name(), snapType, ``) 736 err := ic.Check() 737 comm := Commentf("%s by %s snap", iface.Name(), name) 738 if ok { 739 c.Check(err, IsNil, comm) 740 } else { 741 c.Check(err, NotNil, comm) 742 } 743 } 744 } else { 745 ic := s.installPlugCand(c, iface.Name(), snap.TypeApp, ``) 746 err := ic.Check() 747 comm := Commentf("%s", iface.Name()) 748 if restricted[iface.Name()] { 749 c.Check(err, NotNil, comm) 750 } else { 751 c.Check(err, IsNil, comm) 752 } 753 } 754 } 755 } 756 757 func (s *baseDeclSuite) TestConnection(c *C) { 758 all := builtin.Interfaces() 759 760 // connecting with these interfaces needs to be allowed on 761 // case-by-case basis 762 noconnect := map[string]bool{ 763 "content": true, 764 "cups": true, 765 "docker": true, 766 "fwupd": true, 767 "location-control": true, 768 "location-observe": true, 769 "lxd": true, 770 "maliit": true, 771 "mir": true, 772 "online-accounts-service": true, 773 "raw-volume": true, 774 "storage-framework-service": true, 775 "thumbnailer-service": true, 776 "ubuntu-download-manager": true, 777 "unity8-calendar": true, 778 "unity8-contacts": true, 779 } 780 781 for _, iface := range all { 782 expected := !noconnect[iface.Name()] 783 comm := Commentf(iface.Name()) 784 785 // check base declaration 786 cand := s.connectCand(c, iface.Name(), "", "") 787 err := cand.Check() 788 789 if expected { 790 c.Check(err, IsNil, comm) 791 } else { 792 c.Check(err, NotNil, comm) 793 } 794 } 795 } 796 797 func (s *baseDeclSuite) TestConnectionOnClassic(c *C) { 798 restore := release.MockOnClassic(false) 799 defer restore() 800 801 all := builtin.Interfaces() 802 803 // connecting with these interfaces needs to be allowed on 804 // case-by-case basis when not on classic 805 noconnect := map[string]bool{ 806 "audio-record": true, 807 "modem-manager": true, 808 "network-manager": true, 809 "ofono": true, 810 "pulseaudio": true, 811 "upower-observe": true, 812 } 813 814 for _, onClassic := range []bool{true, false} { 815 release.OnClassic = onClassic 816 for _, iface := range all { 817 if !noconnect[iface.Name()] { 818 continue 819 } 820 expected := onClassic 821 comm := Commentf(iface.Name()) 822 823 // check base declaration 824 cand := s.connectCand(c, iface.Name(), "", "") 825 err := cand.Check() 826 827 if expected { 828 c.Check(err, IsNil, comm) 829 } else { 830 c.Check(err, NotNil, comm) 831 } 832 } 833 } 834 } 835 836 func (s *baseDeclSuite) TestConnectionImplicitOnClassicOrAppSnap(c *C) { 837 restore := release.MockOnClassic(false) 838 defer restore() 839 840 all := builtin.Interfaces() 841 842 // These interfaces represent when the interface might be implicit on 843 // classic or when the interface is provided via an app snap. As such, 844 // they all share the following: 845 // 846 // - implicitOnCore: false 847 // - implicitOnClassis: true 848 // - base declaration uses: 849 // allow-installation: 850 // slot-snap-type: 851 // - app 852 // - core 853 // deny-connection: 854 // on-classic: false 855 // deny-auto-connection: true|false|unspecified 856 // 857 // connecting with these interfaces needs to be allowed on 858 // case-by-case basis when not on classic 859 ifaces := map[string]bool{ 860 "audio-playback": true, 861 "audio-record": true, 862 "cups-control": true, 863 "modem-manager": true, 864 "network-manager": true, 865 "ofono": true, 866 "pulseaudio": true, 867 "upower-observe": true, 868 } 869 870 for _, iface := range all { 871 if !ifaces[iface.Name()] { 872 continue 873 } 874 comm := Commentf(iface.Name()) 875 876 // verify the interface is setup as expected wrt 877 // implicitOnCore, implicitOnClassic, no plugs and has 878 // expected slots (ignoring AutoConnection) 879 si := interfaces.StaticInfoOf(iface) 880 c.Assert(si.ImplicitOnCore, Equals, false) 881 c.Assert(si.ImplicitOnClassic, Equals, true) 882 883 c.Assert(s.baseDecl.PlugRule(iface.Name()), IsNil) 884 885 sr := s.baseDecl.SlotRule(iface.Name()) 886 c.Assert(sr, Not(IsNil)) 887 c.Assert(sr.AllowInstallation, HasLen, 1) 888 c.Check(sr.AllowInstallation[0].SlotSnapTypes, DeepEquals, []string{"app", "core"}, comm) 889 c.Assert(sr.DenyConnection, HasLen, 1) 890 c.Check(sr.DenyConnection[0].OnClassic, DeepEquals, &asserts.OnClassicConstraint{Classic: false}, comm) 891 892 for _, onClassic := range []bool{true, false} { 893 release.OnClassic = onClassic 894 895 for _, implicit := range []bool{true, false} { 896 // When implicitOnCore is false, there is 897 // nothing to test on Core 898 if implicit && !onClassic { 899 continue 900 } 901 902 snapType := "app" 903 if implicit { 904 snapType = "os" 905 } 906 slotYaml := fmt.Sprintf(`name: slot-snap 907 version: 0 908 type: %s 909 slots: 910 %s: 911 `, snapType, iface.Name()) 912 913 // XXX: eventually 'onClassic && !implicit' but 914 // the current declaration allows connection on 915 // Core when 'type: app'. See: 916 // https://github.com/snapcore/snapd/pull/8920/files#r471678529 917 expected := onClassic 918 919 // check base declaration 920 cand := s.connectCand(c, iface.Name(), slotYaml, "") 921 err := cand.Check() 922 923 if expected { 924 c.Check(err, IsNil, comm) 925 } else { 926 c.Check(err, NotNil, comm) 927 } 928 } 929 } 930 } 931 } 932 933 func (s *baseDeclSuite) TestSanity(c *C) { 934 all := builtin.Interfaces() 935 936 // these interfaces have rules both for the slots and plugs side 937 // given how the rules work this can be delicate, 938 // listed here to make sure that was a conscious decision 939 bothSides := map[string]bool{ 940 "block-devices": true, 941 "audio-playback": true, 942 "classic-support": true, 943 "core-support": true, 944 "desktop-launch": true, 945 "dm-crypt": true, 946 "docker-support": true, 947 "greengrass-support": true, 948 "gpio-control": true, 949 "kernel-module-control": true, 950 "kubernetes-support": true, 951 "lxd-support": true, 952 "multipass-support": true, 953 "packagekit-control": true, 954 "personal-files": true, 955 "snapd-control": true, 956 "system-files": true, 957 "tee": true, 958 "udisks2": true, 959 "uinput": true, 960 "unity8": true, 961 "wayland": true, 962 } 963 964 for _, iface := range all { 965 plugRule := s.baseDecl.PlugRule(iface.Name()) 966 slotRule := s.baseDecl.SlotRule(iface.Name()) 967 if plugRule == nil && slotRule == nil { 968 c.Logf("%s is not considered in the base declaration", iface.Name()) 969 c.Fail() 970 } 971 if plugRule != nil && slotRule != nil { 972 if !bothSides[iface.Name()] { 973 c.Logf("%s have both a base declaration slot rule and plug rule, make sure that's intended and correct", iface.Name()) 974 c.Fail() 975 } 976 } 977 } 978 } 979 980 func (s *baseDeclSuite) TestConnectionContent(c *C) { 981 // we let connect explicitly as long as content matches (or is absent on both sides) 982 983 // random (Sanitize* will now also block this) 984 cand := s.connectCand(c, "content", "", "") 985 err := cand.Check() 986 c.Check(err, NotNil) 987 988 slotDecl1 := s.mockSnapDecl(c, "slot-snap", "slot-snap-id", "pub1", "") 989 plugDecl1 := s.mockSnapDecl(c, "plug-snap", "plug-snap-id", "pub1", "") 990 plugDecl2 := s.mockSnapDecl(c, "plug-snap", "plug-snap-id", "pub2", "") 991 992 // same publisher, same content 993 cand = s.connectCand(c, "stuff", `name: slot-snap 994 version: 0 995 slots: 996 stuff: 997 interface: content 998 content: mk1 999 `, ` 1000 name: plug-snap 1001 version: 0 1002 plugs: 1003 stuff: 1004 interface: content 1005 content: mk1 1006 `) 1007 cand.SlotSnapDeclaration = slotDecl1 1008 cand.PlugSnapDeclaration = plugDecl1 1009 err = cand.Check() 1010 c.Check(err, IsNil) 1011 1012 // different publisher, same content 1013 cand.SlotSnapDeclaration = slotDecl1 1014 cand.PlugSnapDeclaration = plugDecl2 1015 err = cand.Check() 1016 c.Check(err, IsNil) 1017 1018 // same publisher, different content 1019 cand = s.connectCand(c, "stuff", ` 1020 name: slot-snap 1021 version: 0 1022 slots: 1023 stuff: 1024 interface: content 1025 content: mk1 1026 `, ` 1027 name: plug-snap 1028 version: 0 1029 plugs: 1030 stuff: 1031 interface: content 1032 content: mk2 1033 `) 1034 cand.SlotSnapDeclaration = slotDecl1 1035 cand.PlugSnapDeclaration = plugDecl1 1036 err = cand.Check() 1037 c.Check(err, NotNil) 1038 } 1039 1040 func (s *baseDeclSuite) TestComposeBaseDeclaration(c *C) { 1041 decl, err := policy.ComposeBaseDeclaration(nil) 1042 c.Assert(err, IsNil) 1043 c.Assert(string(decl), testutil.Contains, ` 1044 type: base-declaration 1045 authority-id: canonical 1046 series: 16 1047 revision: 0 1048 `) 1049 } 1050 1051 func (s *baseDeclSuite) TestDoesNotPanic(c *C) { 1052 // In case there are any issues in the actual interfaces we'd get a panic 1053 // on snapd startup. This test prevents this from happing unnoticed. 1054 _, err := policy.ComposeBaseDeclaration(builtin.Interfaces()) 1055 c.Assert(err, IsNil) 1056 } 1057 1058 func (s *baseDeclSuite) TestBrowserSupportAllowSandbox(c *C) { 1059 const plugYaml = `name: plug-snap 1060 version: 0 1061 plugs: 1062 browser-support: 1063 allow-sandbox: true 1064 ` 1065 cand := s.connectCand(c, "browser-support", "", plugYaml) 1066 err := cand.Check() 1067 c.Check(err, NotNil) 1068 1069 _, err = cand.CheckAutoConnect() 1070 c.Check(err, NotNil) 1071 } 1072 1073 func (s *baseDeclSuite) TestOpticalDriveWrite(c *C) { 1074 type options struct { 1075 readonlyYamls []string 1076 writableYamls []string 1077 } 1078 1079 opts := &options{ 1080 readonlyYamls: []string{ 1081 // Non-specified "write" attribute 1082 `name: plug-snap 1083 version: 0 1084 plugs: 1085 optical-drive: null 1086 `, 1087 // Undefined "write" attribute 1088 `name: plug-snap 1089 version: 0 1090 plugs: 1091 optical-drive: {} 1092 `, 1093 // False "write" attribute 1094 `name: plug-snap 1095 version: 0 1096 plugs: 1097 optical-drive: 1098 write: false 1099 `, 1100 }, 1101 writableYamls: []string{ 1102 // True "write" attribute 1103 `name: plug-snap 1104 version: 0 1105 plugs: 1106 optical-drive: 1107 write: true 1108 `, 1109 }, 1110 } 1111 1112 checkOpticalDriveAutoConnect := func(plugYaml string, checker Checker) { 1113 cand := s.connectCand(c, "optical-drive", "", plugYaml) 1114 err := cand.Check() 1115 c.Check(err, checker) 1116 _, err = cand.CheckAutoConnect() 1117 c.Check(err, checker) 1118 } 1119 1120 for _, plugYaml := range opts.readonlyYamls { 1121 checkOpticalDriveAutoConnect(plugYaml, IsNil) 1122 } 1123 for _, plugYaml := range opts.writableYamls { 1124 checkOpticalDriveAutoConnect(plugYaml, NotNil) 1125 } 1126 } 1127 1128 func (s *baseDeclSuite) TestRawVolumeOverride(c *C) { 1129 slotYaml := `name: slot-snap 1130 type: gadget 1131 version: 0 1132 slots: 1133 raw-volume: 1134 path: /dev/mmcblk0p1 1135 ` 1136 slotSnap := snaptest.MockInfo(c, slotYaml, nil) 1137 // mock a well-formed slot snap decl with SnapID 1138 slotSnapDecl := s.mockSnapDecl(c, "slot-snap", "slotsnapidididididididididididid", "canonical", "") 1139 1140 plugYaml := `name: plug-snap 1141 version: 0 1142 plugs: 1143 raw-volume: 1144 ` 1145 plugSnap := snaptest.MockInfo(c, plugYaml, nil) 1146 1147 // no plug-side declaration 1148 cand := &policy.ConnectCandidate{ 1149 Plug: interfaces.NewConnectedPlug(plugSnap.Plugs["raw-volume"], nil, nil), 1150 Slot: interfaces.NewConnectedSlot(slotSnap.Slots["raw-volume"], nil, nil), 1151 SlotSnapDeclaration: slotSnapDecl, 1152 BaseDeclaration: s.baseDecl, 1153 } 1154 1155 err := cand.Check() 1156 c.Check(err, NotNil) 1157 c.Assert(err, ErrorMatches, "connection denied by slot rule of interface \"raw-volume\"") 1158 _, err = cand.CheckAutoConnect() 1159 c.Check(err, NotNil) 1160 c.Assert(err, ErrorMatches, "auto-connection denied by slot rule of interface \"raw-volume\"") 1161 1162 // specific plug-side declaration for connection only 1163 plugsOverride := ` 1164 plugs: 1165 raw-volume: 1166 allow-connection: 1167 slot-snap-id: 1168 - slotsnapidididididididididididid 1169 allow-auto-connection: false 1170 ` 1171 plugSnapDecl := s.mockSnapDecl(c, "plug-snap", "plugsnapidididididididididididid", "canonical", plugsOverride) 1172 cand.PlugSnapDeclaration = plugSnapDecl 1173 err = cand.Check() 1174 c.Check(err, IsNil) 1175 _, err = cand.CheckAutoConnect() 1176 c.Check(err, NotNil) 1177 c.Assert(err, ErrorMatches, "auto-connection not allowed by plug rule of interface \"raw-volume\" for \"plug-snap\" snap") 1178 1179 // specific plug-side declaration for connection and auto-connection 1180 plugsOverride = ` 1181 plugs: 1182 raw-volume: 1183 allow-connection: 1184 slot-snap-id: 1185 - slotsnapidididididididididididid 1186 allow-auto-connection: 1187 slot-snap-id: 1188 - slotsnapidididididididididididid 1189 ` 1190 plugSnapDecl = s.mockSnapDecl(c, "plug-snap", "plugsnapidididididididididididid", "canonical", plugsOverride) 1191 cand.PlugSnapDeclaration = plugSnapDecl 1192 err = cand.Check() 1193 c.Check(err, IsNil) 1194 arity, err := cand.CheckAutoConnect() 1195 c.Check(err, IsNil) 1196 c.Check(arity.SlotsPerPlugAny(), Equals, false) 1197 1198 // blanket allow for connection and auto-connection to any slotting snap 1199 plugsOverride = ` 1200 plugs: 1201 raw-volume: 1202 allow-connection: true 1203 allow-auto-connection: true 1204 ` 1205 plugSnapDecl = s.mockSnapDecl(c, "some-snap", "plugsnapidididididididididididid", "canonical", plugsOverride) 1206 cand.PlugSnapDeclaration = plugSnapDecl 1207 err = cand.Check() 1208 c.Check(err, IsNil) 1209 arity, err = cand.CheckAutoConnect() 1210 c.Check(err, IsNil) 1211 c.Check(arity.SlotsPerPlugAny(), Equals, false) 1212 } 1213 1214 func (s *baseDeclSuite) TestAutoConnectionDesktopLaunchOverride(c *C) { 1215 cand := s.connectCand(c, "desktop-launch", "", "") 1216 _, err := cand.CheckAutoConnect() 1217 c.Check(err, NotNil) 1218 c.Assert(err, ErrorMatches, "auto-connection denied by plug rule of interface \"desktop-launch\"") 1219 1220 plugsSlots := ` 1221 plugs: 1222 desktop-launch: 1223 allow-auto-connection: true 1224 ` 1225 1226 snapDecl := s.mockSnapDecl(c, "some-snap", "some-snap-with-desktop-launch-id", "canonical", plugsSlots) 1227 cand.PlugSnapDeclaration = snapDecl 1228 _, err = cand.CheckAutoConnect() 1229 c.Check(err, IsNil) 1230 }