github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/interfaces/builtin/docker_support_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016-2020 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 builtin_test
    21  
    22  import (
    23  	. "gopkg.in/check.v1"
    24  
    25  	"github.com/snapcore/snapd/interfaces"
    26  	"github.com/snapcore/snapd/interfaces/apparmor"
    27  	"github.com/snapcore/snapd/interfaces/builtin"
    28  	"github.com/snapcore/snapd/interfaces/kmod"
    29  	"github.com/snapcore/snapd/interfaces/seccomp"
    30  	"github.com/snapcore/snapd/interfaces/udev"
    31  	"github.com/snapcore/snapd/release"
    32  	"github.com/snapcore/snapd/snap"
    33  	"github.com/snapcore/snapd/snap/snaptest"
    34  	"github.com/snapcore/snapd/testutil"
    35  )
    36  
    37  type DockerSupportInterfaceSuite struct {
    38  	iface                    interfaces.Interface
    39  	slotInfo                 *snap.SlotInfo
    40  	slot                     *interfaces.ConnectedSlot
    41  	plugInfo                 *snap.PlugInfo
    42  	plug                     *interfaces.ConnectedPlug
    43  	networkCtrlSlotInfo      *snap.SlotInfo
    44  	networkCtrlSlot          *interfaces.ConnectedSlot
    45  	networkCtrlPlugInfo      *snap.PlugInfo
    46  	networkCtrlPlug          *interfaces.ConnectedPlug
    47  	privContainersPlugInfo   *snap.PlugInfo
    48  	privContainersPlug       *interfaces.ConnectedPlug
    49  	noPrivContainersPlugInfo *snap.PlugInfo
    50  	noPrivContainersPlug     *interfaces.ConnectedPlug
    51  }
    52  
    53  const coreDockerSlotYaml = `name: core
    54  version: 0
    55  type: os
    56  slots:
    57    docker-support:
    58    network-control:
    59  `
    60  
    61  const dockerSupportMockPlugSnapInfoYaml = `name: docker
    62  version: 1.0
    63  apps:
    64   app:
    65    command: foo
    66    plugs: 
    67     - docker-support
    68     - network-control
    69  `
    70  
    71  const dockerSupportPrivilegedContainersFalseMockPlugSnapInfoYaml = `name: docker
    72  version: 1.0
    73  plugs:
    74   privileged:
    75    interface: docker-support
    76    privileged-containers: false
    77  apps:
    78   app:
    79    command: foo
    80    plugs:
    81    - privileged
    82  `
    83  
    84  const dockerSupportPrivilegedContainersTrueMockPlugSnapInfoYaml = `name: docker
    85  version: 1.0
    86  plugs:
    87   privileged:
    88    interface: docker-support
    89    privileged-containers: true
    90  apps:
    91   app:
    92    command: foo
    93    plugs:
    94    - privileged
    95  `
    96  
    97  var _ = Suite(&DockerSupportInterfaceSuite{
    98  	iface: builtin.MustInterface("docker-support"),
    99  })
   100  
   101  func (s *DockerSupportInterfaceSuite) SetUpTest(c *C) {
   102  	s.plug, s.plugInfo = MockConnectedPlug(c, dockerSupportMockPlugSnapInfoYaml, nil, "docker-support")
   103  	s.slot, s.slotInfo = MockConnectedSlot(c, coreDockerSlotYaml, nil, "docker-support")
   104  	s.networkCtrlPlug, s.networkCtrlPlugInfo = MockConnectedPlug(c, dockerSupportMockPlugSnapInfoYaml, nil, "network-control")
   105  	s.networkCtrlSlot, s.networkCtrlSlotInfo = MockConnectedSlot(c, coreDockerSlotYaml, nil, "network-control")
   106  	s.privContainersPlug, s.privContainersPlugInfo = MockConnectedPlug(c, dockerSupportPrivilegedContainersTrueMockPlugSnapInfoYaml, nil, "privileged")
   107  	s.noPrivContainersPlug, s.noPrivContainersPlugInfo = MockConnectedPlug(c, dockerSupportPrivilegedContainersFalseMockPlugSnapInfoYaml, nil, "privileged")
   108  }
   109  
   110  func (s *DockerSupportInterfaceSuite) TestName(c *C) {
   111  	c.Assert(s.iface.Name(), Equals, "docker-support")
   112  }
   113  
   114  func (s *DockerSupportInterfaceSuite) TestUsedSecuritySystems(c *C) {
   115  	// connected plugs have a non-nil security snippet for apparmor
   116  	apparmorSpec := &apparmor.Specification{}
   117  	c.Assert(apparmorSpec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   118  	c.Assert(apparmorSpec.SecurityTags(), HasLen, 1)
   119  
   120  	// connected plugs have a non-nil security snippet for seccomp
   121  	seccompSpec := &seccomp.Specification{}
   122  	c.Assert(seccompSpec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   123  	c.Assert(seccompSpec.Snippets(), HasLen, 1)
   124  }
   125  
   126  func (s *DockerSupportInterfaceSuite) TestSanitizeSlot(c *C) {
   127  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil)
   128  }
   129  
   130  func (s *DockerSupportInterfaceSuite) TestSanitizePlug(c *C) {
   131  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
   132  }
   133  
   134  func (s *DockerSupportInterfaceSuite) TestSanitizePlugWithPrivilegedTrue(c *C) {
   135  	apparmorSpec := &apparmor.Specification{}
   136  	c.Assert(apparmorSpec.AddConnectedPlug(s.iface, s.privContainersPlug, s.slot), IsNil)
   137  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.docker.app"})
   138  	c.Assert(apparmorSpec.SnippetForTag("snap.docker.app"), testutil.Contains, `change_profile unsafe /**,`)
   139  
   140  	seccompSpec := &seccomp.Specification{}
   141  	c.Assert(seccompSpec.AddConnectedPlug(s.iface, s.privContainersPlug, s.slot), IsNil)
   142  	c.Assert(seccompSpec.SecurityTags(), DeepEquals, []string{"snap.docker.app"})
   143  	c.Check(seccompSpec.SnippetForTag("snap.docker.app"), testutil.Contains, "@unrestricted")
   144  }
   145  
   146  func (s *DockerSupportInterfaceSuite) TestSanitizePlugWithPrivilegedFalse(c *C) {
   147  	apparmorSpec := &apparmor.Specification{}
   148  	c.Assert(apparmorSpec.AddConnectedPlug(s.iface, s.noPrivContainersPlug, s.slot), IsNil)
   149  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.docker.app"})
   150  	c.Assert(apparmorSpec.SnippetForTag("snap.docker.app"), Not(testutil.Contains), `change_profile unsafe /**,`)
   151  
   152  	seccompSpec := &seccomp.Specification{}
   153  	c.Assert(seccompSpec.AddConnectedPlug(s.iface, s.noPrivContainersPlug, s.slot), IsNil)
   154  	c.Assert(seccompSpec.SecurityTags(), DeepEquals, []string{"snap.docker.app"})
   155  	c.Check(seccompSpec.SnippetForTag("snap.docker.app"), Not(testutil.Contains), "@unrestricted")
   156  }
   157  
   158  func (s *DockerSupportInterfaceSuite) TestSanitizePlugWithPrivilegedBad(c *C) {
   159  	var mockSnapYaml = `name: docker
   160  version: 1.0
   161  plugs:
   162   privileged:
   163    interface: docker-support
   164    privileged-containers: bad
   165  `
   166  
   167  	info := snaptest.MockInfo(c, mockSnapYaml, nil)
   168  	plug := info.Plugs["privileged"]
   169  	c.Assert(interfaces.BeforePreparePlug(s.iface, plug), ErrorMatches, "docker-support plug requires bool with 'privileged-containers'")
   170  }
   171  
   172  func (s *DockerSupportInterfaceSuite) TestInterfaces(c *C) {
   173  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   174  }
   175  
   176  func (s *DockerSupportInterfaceSuite) TestAppArmorSpec(c *C) {
   177  	spec := &apparmor.Specification{}
   178  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   179  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.docker.app"})
   180  	c.Check(spec.SnippetForTag("snap.docker.app"), testutil.Contains, "/sys/fs/cgroup/*/docker/   rw,\n")
   181  	c.Check(spec.UsesPtraceTrace(), Equals, true)
   182  }
   183  
   184  func (s *DockerSupportInterfaceSuite) TestSecCompSpec(c *C) {
   185  	spec := &seccomp.Specification{}
   186  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   187  	c.Check(spec.SnippetForTag("snap.docker.app"), testutil.Contains, "# Calls the Docker daemon itself requires\n")
   188  }
   189  
   190  func (s *DockerSupportInterfaceSuite) TestKModSpec(c *C) {
   191  	spec := &kmod.Specification{}
   192  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   193  	c.Assert(spec.Modules(), DeepEquals, map[string]bool{
   194  		"overlay": true,
   195  	})
   196  }
   197  
   198  func (s *DockerSupportInterfaceSuite) TestPermanentSlotAppArmorSessionNative(c *C) {
   199  	restore := release.MockOnClassic(false)
   200  	defer restore()
   201  
   202  	apparmorSpec := &apparmor.Specification{}
   203  	err := apparmorSpec.AddConnectedPlug(s.iface, s.plug, s.slot)
   204  	c.Assert(err, IsNil)
   205  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.docker.app"})
   206  
   207  	// verify core rule present
   208  	c.Check(apparmorSpec.SnippetForTag("snap.docker.app"), testutil.Contains, "# /system-data/var/snap/docker/common/var-lib-docker/overlay2/$SHA/diff/\n")
   209  }
   210  
   211  func (s *DockerSupportInterfaceSuite) TestPermanentSlotAppArmorSessionClassic(c *C) {
   212  	restore := release.MockOnClassic(true)
   213  	defer restore()
   214  
   215  	apparmorSpec := &apparmor.Specification{}
   216  	err := apparmorSpec.AddConnectedPlug(s.iface, s.plug, s.slot)
   217  	c.Assert(err, IsNil)
   218  	c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.docker.app"})
   219  
   220  	// verify core rule not present
   221  	c.Check(apparmorSpec.SnippetForTag("snap.docker.app"), Not(testutil.Contains), "# /system-data/var/snap/docker/common/var-lib-docker/overlay2/$SHA/diff/\n")
   222  }
   223  
   224  func (s *DockerSupportInterfaceSuite) TestUdevTaggingDisablingRemoveLast(c *C) {
   225  	// make a spec with network-control that has udev tagging
   226  	spec := &udev.Specification{}
   227  	c.Assert(spec.AddConnectedPlug(builtin.MustInterface("network-control"), s.networkCtrlPlug, s.networkCtrlSlot), IsNil)
   228  	c.Assert(spec.Snippets(), HasLen, 3)
   229  
   230  	// connect docker-support interface plug and ensure that the udev spec is now nil
   231  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   232  	c.Check(spec.Snippets(), HasLen, 0)
   233  }
   234  
   235  func (s *DockerSupportInterfaceSuite) TestUdevTaggingDisablingRemoveFirst(c *C) {
   236  	spec := &udev.Specification{}
   237  	// connect docker-support interface plug which specifies
   238  	// controls-device-cgroup as true and ensure that the udev spec is now nil
   239  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
   240  	c.Check(spec.Snippets(), HasLen, 0)
   241  
   242  	// add network-control and ensure the spec is still nil
   243  	c.Assert(spec.AddConnectedPlug(builtin.MustInterface("network-control"), s.networkCtrlPlug, s.networkCtrlSlot), IsNil)
   244  	c.Assert(spec.Snippets(), HasLen, 0)
   245  }