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
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016-2017 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 policy
import (
"fmt"
"strings"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snap"
)
// check helpers
func checkSnapType(snapInfo *snap.Info, types []string) error {
if len(types) == 0 {
return nil
}
snapType := snapInfo.Type()
s := string(snapType)
if snapType == snap.TypeOS || snapType == snap.TypeSnapd {
// we use "core" in the assertions and we need also to
// allow for the "snapd" snap
s = "core"
}
for _, t := range types {
if t == s {
return nil
}
}
return fmt.Errorf("snap type does not match")
}
func checkID(kind, id string, ids []string, special map[string]string) error {
if len(ids) == 0 {
return nil
}
if id == "" { // unset values never match
return fmt.Errorf("%s does not match", kind)
}
for _, cand := range ids {
if strings.HasPrefix(cand, "$") {
cand = special[cand]
if cand == "" { // we ignore unknown special "ids"
continue
}
}
if id == cand {
return nil
}
}
return fmt.Errorf("%s does not match", kind)
}
func checkOnClassic(c *asserts.OnClassicConstraint) error {
if c == nil {
return nil
}
if c.Classic != release.OnClassic {
return fmt.Errorf("on-classic mismatch")
}
if c.Classic && len(c.SystemIDs) != 0 {
return checkID("operating system ID", release.ReleaseInfo.ID, c.SystemIDs, nil)
}
return nil
}
func checkOnCoreDesktop(c *asserts.OnCoreDesktopConstraint) error {
if c == nil {
return nil
}
if c.CoreDesktop != release.OnCoreDesktop {
return fmt.Errorf("on-core-desktop mismatch")
}
return nil
}
func checkDeviceScope(c *asserts.DeviceScopeConstraint, model *asserts.Model, store *asserts.Store) error {
if c == nil {
return nil
}
opts := asserts.DeviceScopeConstraintCheckOptions{
UseFriendlyStores: true,
}
return c.Check(model, store, &opts)
}
func checkNameConstraints(c *asserts.NameConstraints, iface, which, name string) error {
if c == nil {
return nil
}
special := map[string]string{
"$INTERFACE": iface,
}
return c.Check(which, name, special)
}
func checkPlugConnectionConstraints1(connc *ConnectCandidate, constraints *asserts.PlugConnectionConstraints) error {
if err := checkNameConstraints(constraints.PlugNames, connc.Plug.Interface(), "plug name", connc.Plug.Name()); err != nil {
return err
}
if err := checkNameConstraints(constraints.SlotNames, connc.Slot.Interface(), "slot name", connc.Slot.Name()); err != nil {
return err
}
if err := constraints.PlugAttributes.Check(connc.Plug, connc); err != nil {
return err
}
if err := constraints.SlotAttributes.Check(connc.Slot, connc); err != nil {
return err
}
if err := checkSnapType(connc.Slot.Snap(), constraints.SlotSnapTypes); err != nil {
return err
}
if err := checkID("snap id", connc.slotSnapID(), constraints.SlotSnapIDs, nil); err != nil {
return err
}
err := checkID("publisher id", connc.SlotPublisherID(), constraints.SlotPublisherIDs, map[string]string{
"$PLUG_PUBLISHER_ID": connc.PlugPublisherID(),
})
if err != nil {
return err
}
if err := checkOnClassic(constraints.OnClassic); err != nil {
return err
}
if err := checkOnCoreDesktop(constraints.OnCoreDesktop); err != nil {
return err
}
if err := checkDeviceScope(constraints.DeviceScope, connc.Model, connc.Store); err != nil {
return err
}
return nil
}
func checkPlugConnectionAltConstraints(connc *ConnectCandidate, altConstraints []*asserts.PlugConnectionConstraints) (*asserts.PlugConnectionConstraints, error) {
var firstErr error
// OR of constraints
for _, constraints := range altConstraints {
err := checkPlugConnectionConstraints1(connc, constraints)
if err == nil {
return constraints, nil
}
if firstErr == nil {
firstErr = err
}
}
return nil, firstErr
}
func checkSlotConnectionConstraints1(connc *ConnectCandidate, constraints *asserts.SlotConnectionConstraints) error {
if err := checkNameConstraints(constraints.PlugNames, connc.Plug.Interface(), "plug name", connc.Plug.Name()); err != nil {
return err
}
if err := checkNameConstraints(constraints.SlotNames, connc.Slot.Interface(), "slot name", connc.Slot.Name()); err != nil {
return err
}
if err := constraints.PlugAttributes.Check(connc.Plug, connc); err != nil {
return err
}
if err := constraints.SlotAttributes.Check(connc.Slot, connc); err != nil {
return err
}
if err := checkSnapType(connc.Slot.Snap(), constraints.SlotSnapTypes); err != nil {
return err
}
if err := checkSnapType(connc.Plug.Snap(), constraints.PlugSnapTypes); err != nil {
return err
}
if err := checkID("snap id", connc.plugSnapID(), constraints.PlugSnapIDs, nil); err != nil {
return err
}
err := checkID("publisher id", connc.PlugPublisherID(), constraints.PlugPublisherIDs, map[string]string{
"$SLOT_PUBLISHER_ID": connc.SlotPublisherID(),
})
if err != nil {
return err
}
if err := checkOnClassic(constraints.OnClassic); err != nil {
return err
}
if err := checkOnCoreDesktop(constraints.OnCoreDesktop); err != nil {
return err
}
if err := checkDeviceScope(constraints.DeviceScope, connc.Model, connc.Store); err != nil {
return err
}
return nil
}
func checkSlotConnectionAltConstraints(connc *ConnectCandidate, altConstraints []*asserts.SlotConnectionConstraints) (*asserts.SlotConnectionConstraints, error) {
var firstErr error
// OR of constraints
for _, constraints := range altConstraints {
err := checkSlotConnectionConstraints1(connc, constraints)
if err == nil {
return constraints, nil
}
if firstErr == nil {
firstErr = err
}
}
return nil, firstErr
}
func checkSnapTypeSlotInstallationConstraints1(slot *snap.SlotInfo, constraints *asserts.SlotInstallationConstraints) error {
if err := checkSnapType(slot.Snap, constraints.SlotSnapTypes); err != nil {
return err
}
if err := checkOnClassic(constraints.OnClassic); err != nil {
return err
}
if err := checkOnCoreDesktop(constraints.OnCoreDesktop); err != nil {
return err
}
return nil
}
func checkMinimalSlotInstallationAltConstraints(slot *snap.SlotInfo, altConstraints []*asserts.SlotInstallationConstraints) (bool, error) {
var firstErr error
var hasSnapTypeConstraints bool
// OR of constraints
for _, constraints := range altConstraints {
if constraints.OnClassic == nil && constraints.OnCoreDesktop == nil && len(constraints.SlotSnapTypes) == 0 {
continue
}
hasSnapTypeConstraints = true
err := checkSnapTypeSlotInstallationConstraints1(slot, constraints)
if err == nil {
return true, nil
}
if firstErr == nil {
firstErr = err
}
}
return hasSnapTypeConstraints, firstErr
}
func checkSlotInstallationConstraints1(ic *InstallCandidate, slot *snap.SlotInfo, constraints *asserts.SlotInstallationConstraints) error {
if err := checkNameConstraints(constraints.SlotNames, slot.Interface, "slot name", slot.Name); err != nil {
return err
}
// TODO: allow evaluated attr constraints here too?
if err := constraints.SlotAttributes.Check(slot, nil); err != nil {
return err
}
if err := checkSnapType(slot.Snap, constraints.SlotSnapTypes); err != nil {
return err
}
if err := checkID("snap id", ic.snapID(), constraints.SlotSnapIDs, nil); err != nil {
return err
}
if err := checkOnClassic(constraints.OnClassic); err != nil {
return err
}
if err := checkOnCoreDesktop(constraints.OnCoreDesktop); err != nil {
return err
}
if err := checkDeviceScope(constraints.DeviceScope, ic.Model, ic.Store); err != nil {
return err
}
return nil
}
func checkSlotInstallationAltConstraints(ic *InstallCandidate, slot *snap.SlotInfo, altConstraints []*asserts.SlotInstallationConstraints) error {
var firstErr error
// OR of constraints
for _, constraints := range altConstraints {
err := checkSlotInstallationConstraints1(ic, slot, constraints)
if err == nil {
return nil
}
if firstErr == nil {
firstErr = err
}
}
return firstErr
}
func checkPlugInstallationConstraints1(ic *InstallCandidate, plug *snap.PlugInfo, constraints *asserts.PlugInstallationConstraints) error {
if err := checkNameConstraints(constraints.PlugNames, plug.Interface, "plug name", plug.Name); err != nil {
return err
}
// TODO: allow evaluated attr constraints here too?
if err := constraints.PlugAttributes.Check(plug, nil); err != nil {
return err
}
if err := checkSnapType(plug.Snap, constraints.PlugSnapTypes); err != nil {
return err
}
if err := checkID("snap id", ic.snapID(), constraints.PlugSnapIDs, nil); err != nil {
return err
}
if err := checkOnClassic(constraints.OnClassic); err != nil {
return err
}
if err := checkOnCoreDesktop(constraints.OnCoreDesktop); err != nil {
return err
}
if err := checkDeviceScope(constraints.DeviceScope, ic.Model, ic.Store); err != nil {
return err
}
return nil
}
func checkPlugInstallationAltConstraints(ic *InstallCandidate, plug *snap.PlugInfo, altConstraints []*asserts.PlugInstallationConstraints) error {
var firstErr error
// OR of constraints
for _, constraints := range altConstraints {
err := checkPlugInstallationConstraints1(ic, plug, constraints)
if err == nil {
return nil
}
if firstErr == nil {
firstErr = err
}
}
return firstErr
}
// sideArity carries relevant arity constraints for successful
// allow-auto-connection rules. It implements policy.SideArity.
// ATM only slots-per-plug might have an interesting non-default
// value.
// See: https://forum.snapcraft.io/t/plug-slot-declaration-rules-greedy-plugs/12438
type sideArity struct {
slotsPerPlug asserts.SideArityConstraint
}
func (a sideArity) SlotsPerPlugOne() bool {
return a.slotsPerPlug.N == 1
}
func (a sideArity) SlotsPerPlugAny() bool {
return a.slotsPerPlug.Any()
}
|