github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/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 malformedPlugInfo *snap.PlugInfo 52 malformedPlug *interfaces.ConnectedPlug 53 } 54 55 const coreDockerSlotYaml = `name: core 56 version: 0 57 type: os 58 slots: 59 docker-support: 60 network-control: 61 ` 62 63 const dockerSupportMockPlugSnapInfoYaml = `name: docker 64 version: 1.0 65 apps: 66 app: 67 command: foo 68 plugs: 69 - docker-support 70 - network-control 71 ` 72 73 const dockerSupportPrivilegedContainersMalformedMockPlugSnapInfoYaml = `name: docker 74 version: 1.0 75 plugs: 76 privileged: 77 interface: docker-support 78 privileged-containers: foobar 79 apps: 80 app: 81 command: foo 82 plugs: 83 - privileged 84 ` 85 86 const dockerSupportPrivilegedContainersFalseMockPlugSnapInfoYaml = `name: docker 87 version: 1.0 88 plugs: 89 privileged: 90 interface: docker-support 91 privileged-containers: false 92 apps: 93 app: 94 command: foo 95 plugs: 96 - privileged 97 ` 98 99 const dockerSupportPrivilegedContainersTrueMockPlugSnapInfoYaml = `name: docker 100 version: 1.0 101 plugs: 102 privileged: 103 interface: docker-support 104 privileged-containers: true 105 apps: 106 app: 107 command: foo 108 plugs: 109 - privileged 110 ` 111 112 var _ = Suite(&DockerSupportInterfaceSuite{ 113 iface: builtin.MustInterface("docker-support"), 114 }) 115 116 func (s *DockerSupportInterfaceSuite) SetUpTest(c *C) { 117 s.plug, s.plugInfo = MockConnectedPlug(c, dockerSupportMockPlugSnapInfoYaml, nil, "docker-support") 118 s.slot, s.slotInfo = MockConnectedSlot(c, coreDockerSlotYaml, nil, "docker-support") 119 s.networkCtrlPlug, s.networkCtrlPlugInfo = MockConnectedPlug(c, dockerSupportMockPlugSnapInfoYaml, nil, "network-control") 120 s.networkCtrlSlot, s.networkCtrlSlotInfo = MockConnectedSlot(c, coreDockerSlotYaml, nil, "network-control") 121 s.privContainersPlug, s.privContainersPlugInfo = MockConnectedPlug(c, dockerSupportPrivilegedContainersTrueMockPlugSnapInfoYaml, nil, "privileged") 122 s.noPrivContainersPlug, s.noPrivContainersPlugInfo = MockConnectedPlug(c, dockerSupportPrivilegedContainersFalseMockPlugSnapInfoYaml, nil, "privileged") 123 s.malformedPlug, s.malformedPlugInfo = MockConnectedPlug(c, dockerSupportPrivilegedContainersMalformedMockPlugSnapInfoYaml, nil, "privileged") 124 } 125 126 func (s *DockerSupportInterfaceSuite) TestName(c *C) { 127 c.Assert(s.iface.Name(), Equals, "docker-support") 128 } 129 130 func (s *DockerSupportInterfaceSuite) TestUsedSecuritySystems(c *C) { 131 // connected plugs have a non-nil security snippet for apparmor 132 apparmorSpec := &apparmor.Specification{} 133 c.Assert(apparmorSpec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil) 134 c.Assert(apparmorSpec.SecurityTags(), HasLen, 1) 135 136 // connected plugs have a non-nil security snippet for seccomp 137 seccompSpec := &seccomp.Specification{} 138 c.Assert(seccompSpec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil) 139 c.Assert(seccompSpec.Snippets(), HasLen, 1) 140 } 141 142 func (s *DockerSupportInterfaceSuite) TestSanitizeSlot(c *C) { 143 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.slotInfo), IsNil) 144 } 145 146 func (s *DockerSupportInterfaceSuite) TestSanitizePlug(c *C) { 147 c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil) 148 } 149 150 func (s *DockerSupportInterfaceSuite) TestSanitizePlugWithPrivilegedTrue(c *C) { 151 apparmorSpec := &apparmor.Specification{} 152 c.Assert(apparmorSpec.AddConnectedPlug(s.iface, s.privContainersPlug, s.slot), IsNil) 153 c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.docker.app"}) 154 c.Assert(apparmorSpec.SnippetForTag("snap.docker.app"), testutil.Contains, `change_profile unsafe /**,`) 155 156 seccompSpec := &seccomp.Specification{} 157 c.Assert(seccompSpec.AddConnectedPlug(s.iface, s.privContainersPlug, s.slot), IsNil) 158 c.Assert(seccompSpec.SecurityTags(), DeepEquals, []string{"snap.docker.app"}) 159 c.Check(seccompSpec.SnippetForTag("snap.docker.app"), testutil.Contains, "@unrestricted") 160 } 161 162 func (s *DockerSupportInterfaceSuite) TestSanitizePlugWithPrivilegedFalse(c *C) { 163 apparmorSpec := &apparmor.Specification{} 164 c.Assert(apparmorSpec.AddConnectedPlug(s.iface, s.noPrivContainersPlug, s.slot), IsNil) 165 c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.docker.app"}) 166 c.Assert(apparmorSpec.SnippetForTag("snap.docker.app"), Not(testutil.Contains), `change_profile unsafe /**,`) 167 168 seccompSpec := &seccomp.Specification{} 169 c.Assert(seccompSpec.AddConnectedPlug(s.iface, s.noPrivContainersPlug, s.slot), IsNil) 170 c.Assert(seccompSpec.SecurityTags(), DeepEquals, []string{"snap.docker.app"}) 171 c.Check(seccompSpec.SnippetForTag("snap.docker.app"), Not(testutil.Contains), "@unrestricted") 172 } 173 174 func (s *DockerSupportInterfaceSuite) TestSanitizePlugWithPrivilegedBad(c *C) { 175 var mockSnapYaml = `name: docker 176 version: 1.0 177 plugs: 178 privileged: 179 interface: docker-support 180 privileged-containers: bad 181 ` 182 183 info := snaptest.MockInfo(c, mockSnapYaml, nil) 184 plug := info.Plugs["privileged"] 185 c.Assert(interfaces.BeforePreparePlug(s.iface, plug), ErrorMatches, "docker-support plug requires bool with 'privileged-containers'") 186 } 187 188 func (s *DockerSupportInterfaceSuite) TestInterfaces(c *C) { 189 c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface) 190 } 191 192 func (s *DockerSupportInterfaceSuite) TestAppArmorSpec(c *C) { 193 spec := &apparmor.Specification{} 194 c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil) 195 c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.docker.app"}) 196 c.Check(spec.SnippetForTag("snap.docker.app"), testutil.Contains, "/sys/fs/cgroup/*/docker/ rw,\n") 197 c.Check(spec.UsesPtraceTrace(), Equals, true) 198 } 199 200 func (s *DockerSupportInterfaceSuite) TestSecCompSpec(c *C) { 201 spec := &seccomp.Specification{} 202 c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil) 203 c.Check(spec.SnippetForTag("snap.docker.app"), testutil.Contains, "# Calls the Docker daemon itself requires\n") 204 } 205 206 func (s *DockerSupportInterfaceSuite) TestKModSpec(c *C) { 207 spec := &kmod.Specification{} 208 c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil) 209 c.Assert(spec.Modules(), DeepEquals, map[string]bool{ 210 "overlay": true, 211 }) 212 } 213 214 func (s *DockerSupportInterfaceSuite) TestPermanentSlotAppArmorSessionNative(c *C) { 215 restore := release.MockOnClassic(false) 216 defer restore() 217 218 apparmorSpec := &apparmor.Specification{} 219 err := apparmorSpec.AddConnectedPlug(s.iface, s.plug, s.slot) 220 c.Assert(err, IsNil) 221 c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.docker.app"}) 222 223 // verify core rule present 224 c.Check(apparmorSpec.SnippetForTag("snap.docker.app"), testutil.Contains, "# /system-data/var/snap/docker/common/var-lib-docker/overlay2/$SHA/diff/\n") 225 } 226 227 func (s *DockerSupportInterfaceSuite) TestPermanentSlotAppArmorSessionClassic(c *C) { 228 restore := release.MockOnClassic(true) 229 defer restore() 230 231 apparmorSpec := &apparmor.Specification{} 232 err := apparmorSpec.AddConnectedPlug(s.iface, s.plug, s.slot) 233 c.Assert(err, IsNil) 234 c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.docker.app"}) 235 236 // verify core rule not present 237 c.Check(apparmorSpec.SnippetForTag("snap.docker.app"), Not(testutil.Contains), "# /system-data/var/snap/docker/common/var-lib-docker/overlay2/$SHA/diff/\n") 238 } 239 240 func (s *DockerSupportInterfaceSuite) TestUdevTaggingDisablingRemoveLast(c *C) { 241 // make a spec with network-control that has udev tagging 242 spec := &udev.Specification{} 243 c.Assert(spec.AddConnectedPlug(builtin.MustInterface("network-control"), s.networkCtrlPlug, s.networkCtrlSlot), IsNil) 244 c.Assert(spec.Snippets(), HasLen, 3) 245 246 // connect docker-support interface plug and ensure that the udev spec is now nil 247 c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil) 248 c.Check(spec.Snippets(), HasLen, 0) 249 } 250 251 func (s *DockerSupportInterfaceSuite) TestUdevTaggingDisablingRemoveFirst(c *C) { 252 spec := &udev.Specification{} 253 // connect docker-support interface plug which specifies 254 // controls-device-cgroup as true and ensure that the udev spec is now nil 255 c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil) 256 c.Check(spec.Snippets(), HasLen, 0) 257 258 // add network-control and ensure the spec is still nil 259 c.Assert(spec.AddConnectedPlug(builtin.MustInterface("network-control"), s.networkCtrlPlug, s.networkCtrlSlot), IsNil) 260 c.Assert(spec.Snippets(), HasLen, 0) 261 } 262 263 func (s *DockerSupportInterfaceSuite) TestServicePermanentPlugSnippets(c *C) { 264 snips, err := interfaces.PermanentPlugServiceSnippets(s.iface, s.plugInfo) 265 c.Assert(err, IsNil) 266 c.Check(snips, DeepEquals, []string{"Delegate=true"}) 267 268 // check that a malformed plug with bad attribute returns non-nil error 269 // from PermanentPlugServiceSnippets, thereby ensuring that function 270 // sanitizes plugs 271 _, err = interfaces.PermanentPlugServiceSnippets(s.iface, s.malformedPlugInfo) 272 c.Assert(err, ErrorMatches, "docker-support plug requires bool with 'privileged-containers'") 273 }