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 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2015-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 asserts
import (
"errors"
"fmt"
"regexp"
"strings"
"unicode"
"github.com/snapcore/snapd/snap/naming"
)
// AttrMatchContext has contextual helpers for evaluating attribute constraints.
type AttrMatchContext interface {
PlugAttr(arg string) (any, error)
SlotAttr(arg string) (any, error)
PlugPublisherID() string
SlotPublisherID() string
}
const (
// feature label for on-store/on-brand/on-model
deviceScopeConstraintsFeature = "device-scope-constraints"
// feature label for plug-names/slot-names constraints
nameConstraintsFeature = "name-constraints"
)
// AttributeConstraints implements a set of constraints on the attributes of a slot or plug.
type AttributeConstraints struct {
matcher attrMatcher
}
func (ac *AttributeConstraints) feature(flabel string) bool {
return ac.matcher.feature(flabel)
}
// compileAttributeConstraints checks and compiles a mapping or list
// from the assertion format into AttributeConstraints.
func compileAttributeConstraints(constraints any) (*AttributeConstraints, error) {
cc := compileContext{
opts: &compileAttrMatcherOptions{
allowedOperations: []string{"SLOT", "PLUG"},
allowedRefs: []string{"PLUG_PUBLISHER_ID", "SLOT_PUBLISHER_ID"},
},
}
matcher, err := compileAttrMatcher(cc, constraints)
if err != nil {
return nil, err
}
return &AttributeConstraints{matcher: matcher}, nil
}
type fixedAttrMatcher struct {
result error
}
func (matcher fixedAttrMatcher) feature(flabel string) bool {
return false
}
func (matcher fixedAttrMatcher) match(apath string, v any, ctx *attrMatchingContext) error {
return matcher.result
}
var (
AlwaysMatchAttributes = &AttributeConstraints{matcher: fixedAttrMatcher{nil}}
NeverMatchAttributes = &AttributeConstraints{matcher: fixedAttrMatcher{errors.New("not allowed")}}
)
// Attrer reflects part of the Attrer interface (see interfaces.Attrer).
type Attrer interface {
Lookup(path string) (any, bool)
}
// Check checks whether attrs don't match the constraints.
func (c *AttributeConstraints) Check(attrer Attrer, helper AttrMatchContext) error {
return c.matcher.match("", attrer, &attrMatchingContext{
attrWord: "attribute",
helper: helper,
})
}
// SideArityConstraint specifies a constraint for the overall arity of
// the set of connected slots for a given plug or the set of
// connected plugs for a given slot.
// It is used to express parsed slots-per-plug and plugs-per-slot
// constraints.
// See https://forum.snapcraft.io/t/plug-slot-declaration-rules-greedy-plugs/12438
type SideArityConstraint struct {
// N can be:
// =>1
// 0 means default and is used only internally during rule
// compilation or on deny- rules where these constraints are
// not applicable
// -1 represents *, that means any (number of)
N int
}
// Any returns whether this represents the * (any number of) constraint.
func (ac SideArityConstraint) Any() bool {
return ac.N == -1
}
func compileSideArityConstraint(context *subruleContext, which string, v any) (SideArityConstraint, error) {
var a SideArityConstraint
if context.installation() || !context.allow() {
return a, fmt.Errorf("%s cannot specify a %s constraint, they apply only to allow-*connection", context, which)
}
x, ok := v.(string)
if !ok || len(x) == 0 {
return a, fmt.Errorf("%s in %s must be an integer >=1 or *", which, context)
}
if x == "*" {
return SideArityConstraint{N: -1}, nil
}
n, err := atoi(x, "%s in %s", which, context)
switch _, syntax := err.(intSyntaxError); {
case err == nil && n < 1:
fallthrough
case syntax:
return a, fmt.Errorf("%s in %s must be an integer >=1 or *", which, context)
case err != nil:
return a, err
}
return SideArityConstraint{N: n}, nil
}
type sideArityConstraintsHolder interface {
setSlotsPerPlug(SideArityConstraint)
setPlugsPerSlot(SideArityConstraint)
slotsPerPlug() SideArityConstraint
plugsPerSlot() SideArityConstraint
}
func normalizeSideArityConstraints(context *subruleContext, c sideArityConstraintsHolder) {
if !context.allow() {
return
}
any := SideArityConstraint{N: -1}
// normalized plugs-per-slot is always *
c.setPlugsPerSlot(any)
slotsPerPlug := c.slotsPerPlug()
if context.autoConnection() {
// auto-connection slots-per-plug can be any or 1
if !slotsPerPlug.Any() {
c.setSlotsPerPlug(SideArityConstraint{N: 1})
}
} else {
// connection slots-per-plug can be only any
c.setSlotsPerPlug(any)
}
}
var (
sideArityConstraints = []string{"slots-per-plug", "plugs-per-slot"}
sideArityConstraintsSetters = map[string]func(sideArityConstraintsHolder, SideArityConstraint){
"slots-per-plug": sideArityConstraintsHolder.setSlotsPerPlug,
"plugs-per-slot": sideArityConstraintsHolder.setPlugsPerSlot,
}
)
// OnClassicConstraint specifies a constraint based whether the system is classic and optional specific distros' sets.
type OnClassicConstraint struct {
Classic bool
SystemIDs []string
}
// OnCoreDesktopConstraint specifies a constraint based whether the system is core desktop.
type OnCoreDesktopConstraint struct {
CoreDesktop bool
}
type nameMatcher interface {
match(name string, special map[string]string) error
}
var (
// validates special name constraints like $INTERFACE
validSpecialNameConstraint = regexp.MustCompile(`^\$[A-Z][A-Z0-9_]*$`)
)
func compileNameMatcher(whichName string, v any) (nameMatcher, error) {
s, ok := v.(string)
if !ok {
return nil, fmt.Errorf("%s constraint entry must be a regexp or special $ value", whichName)
}
if strings.HasPrefix(s, "$") {
if !validSpecialNameConstraint.MatchString(s) {
return nil, fmt.Errorf("%s constraint entry special value %q is invalid", whichName, s)
}
return specialNameMatcher{special: s}, nil
}
if strings.IndexFunc(s, unicode.IsSpace) != -1 {
return nil, fmt.Errorf("%s constraint entry regexp contains unexpected spaces", whichName)
}
rx, err := regexp.Compile("^(" + s + ")$")
if err != nil {
return nil, fmt.Errorf("cannot compile %s constraint entry %q: %v", whichName, s, err)
}
return regexpNameMatcher{rx}, nil
}
type regexpNameMatcher struct {
*regexp.Regexp
}
func (matcher regexpNameMatcher) match(name string, special map[string]string) error {
if !matcher.Regexp.MatchString(name) {
return fmt.Errorf("%q does not match %v", name, matcher.Regexp)
}
return nil
}
type specialNameMatcher struct {
special string
}
func (matcher specialNameMatcher) match(name string, special map[string]string) error {
expected := special[matcher.special]
if expected == "" || expected != name {
return fmt.Errorf("%q does not match %v", name, matcher.special)
}
return nil
}
// NameConstraints implements a set of constraints on the names of slots or plugs.
// See https://forum.snapcraft.io/t/plug-slot-rules-plug-names-slot-names-constraints/12439
type NameConstraints struct {
matchers []nameMatcher
}
func compileNameConstraints(whichName string, constraints any) (*NameConstraints, error) {
l, ok := constraints.([]any)
if !ok {
return nil, fmt.Errorf("%s constraints must be a list of regexps and special $ values", whichName)
}
matchers := make([]nameMatcher, 0, len(l))
for _, nm := range l {
matcher, err := compileNameMatcher(whichName, nm)
if err != nil {
return nil, err
}
matchers = append(matchers, matcher)
}
return &NameConstraints{matchers: matchers}, nil
}
// Check checks whether name doesn't match the constraints.
func (nc *NameConstraints) Check(whichName, name string, special map[string]string) error {
for _, m := range nc.matchers {
if err := m.match(name, special); err == nil {
return nil
}
}
return fmt.Errorf("%s %q does not match constraints", whichName, name)
}
// rules
var (
validSnapType = regexp.MustCompile(`^(?:core|kernel|gadget|app)$`)
validDistro = regexp.MustCompile(`^[-0-9a-z._]+$`)
validPublisher = regexp.MustCompile(`^(?:[a-z0-9A-Z]{32}|[-a-z0-9]{2,28}|\$[A-Z][A-Z0-9_]*)$`) // account ids look like snap-ids or are nice identifiers, support our own special markers $MARKER
validIDConstraints = map[string]*regexp.Regexp{
"slot-snap-type": validSnapType,
"slot-snap-id": naming.ValidSnapID,
"slot-publisher-id": validPublisher,
"plug-snap-type": validSnapType,
"plug-snap-id": naming.ValidSnapID,
"plug-publisher-id": validPublisher,
}
)
func checkMapOrShortcut(v any) (m map[string]any, invert bool, err error) {
switch x := v.(type) {
case map[string]any:
return x, false, nil
case string:
switch x {
case "true":
return nil, false, nil
case "false":
return nil, true, nil
}
}
return nil, false, errors.New("unexpected type")
}
type constraintsHolder interface {
setNameConstraints(field string, cstrs *NameConstraints)
setAttributeConstraints(field string, cstrs *AttributeConstraints)
setIDConstraints(field string, cstrs []string)
setOnClassicConstraint(onClassic *OnClassicConstraint)
setOnCoreDesktopConstraint(onCoreDesktop *OnCoreDesktopConstraint)
setDeviceScopeConstraint(deviceScope *DeviceScopeConstraint)
}
func baseCompileConstraints(context *subruleContext, cDef constraintsDef, target constraintsHolder, nameConstraints, attrConstraints, idConstraints []string) error {
cMap := cDef.cMap
if cMap == nil {
fixed := AlwaysMatchAttributes // "true"
if cDef.invert { // "false"
fixed = NeverMatchAttributes
}
for _, field := range attrConstraints {
target.setAttributeConstraints(field, fixed)
}
return nil
}
defaultUsed := 0
for _, field := range nameConstraints {
v := cMap[field]
if v != nil {
nc, err := compileNameConstraints(field, v)
if err != nil {
return err
}
target.setNameConstraints(field, nc)
} else {
defaultUsed++
}
}
for _, field := range attrConstraints {
cstrs := AlwaysMatchAttributes
v := cMap[field]
if v != nil {
var err error
cstrs, err = compileAttributeConstraints(cMap[field])
if err != nil {
return fmt.Errorf("cannot compile %s in %s: %v", field, context, err)
}
} else {
defaultUsed++
}
target.setAttributeConstraints(field, cstrs)
}
for _, field := range idConstraints {
lst, err := checkStringListInMap(cMap, field, fmt.Sprintf("%s in %s", field, context), validIDConstraints[field])
if err != nil {
return err
}
if lst == nil {
defaultUsed++
}
target.setIDConstraints(field, lst)
}
for _, field := range sideArityConstraints {
v := cMap[field]
if v != nil {
c, err := compileSideArityConstraint(context, field, v)
if err != nil {
return err
}
h, ok := target.(sideArityConstraintsHolder)
if !ok {
return fmt.Errorf("internal error: side arity constraint compiled for unexpected subrule %T", target)
}
sideArityConstraintsSetters[field](h, c)
} else {
defaultUsed++
}
}
onClassic := cMap["on-classic"]
if onClassic == nil {
defaultUsed++
} else {
var c *OnClassicConstraint
switch x := onClassic.(type) {
case string:
switch x {
case "true":
c = &OnClassicConstraint{Classic: true}
case "false":
c = &OnClassicConstraint{Classic: false}
}
case []any:
lst, err := checkStringListInMap(cMap, "on-classic", fmt.Sprintf("on-classic in %s", context), validDistro)
if err != nil {
return err
}
c = &OnClassicConstraint{Classic: true, SystemIDs: lst}
}
if c == nil {
return fmt.Errorf("on-classic in %s must be 'true', 'false' or a list of operating system IDs", context)
}
target.setOnClassicConstraint(c)
}
onCoreDesktop := cMap["on-core-desktop"]
if onCoreDesktop == nil {
defaultUsed++
} else {
var c *OnCoreDesktopConstraint
switch x := onCoreDesktop.(type) {
case string:
switch x {
case "true":
c = &OnCoreDesktopConstraint{CoreDesktop: true}
case "false":
c = &OnCoreDesktopConstraint{CoreDesktop: false}
}
}
if c == nil {
return fmt.Errorf("on-core-desktop in %s must be 'true' or 'false'", context)
}
target.setOnCoreDesktopConstraint(c)
}
dsc, err := compileDeviceScopeConstraint(cMap, context.String())
if err != nil {
return err
}
if dsc == nil {
defaultUsed++
} else {
target.setDeviceScopeConstraint(dsc)
}
// checks whether defaults have been used for everything, which is not
// well-formed
// +1+1+1 accounts for defaults for missing on-classic, on-core-desktop plus missing
// on-store/on-brand/on-model
if defaultUsed == len(nameConstraints)+len(attributeConstraints)+len(idConstraints)+len(sideArityConstraints)+1+1+1 {
return fmt.Errorf("%s must specify at least one of %s, %s, %s, %s, on-classic, on-core-desktop, on-store, on-brand, on-model", context, strings.Join(nameConstraints, ", "), strings.Join(attrConstraints, ", "), strings.Join(idConstraints, ", "), strings.Join(sideArityConstraints, ", "))
}
return nil
}
type rule interface {
setConstraints(field string, cstrs []constraintsHolder)
}
type constraintsDef struct {
cMap map[string]any
invert bool
}
// subruleContext carries queryable context information about one the
// {allow,deny}-* subrules that end up compiled as
// Plug|Slot*Constraints. The information includes the parent rule,
// the introductory subrule key ({allow,deny}-*) and which alternative
// it corresponds to if any.
// The information is useful for constraints compilation now that we
// have constraints with different behavior depending on the kind of
// subrule that hosts them (e.g. slots-per-plug, plugs-per-slot).
type subruleContext struct {
// rule is the parent rule context description
rule string
// subrule is the subrule key
subrule string
// alt is which alternative this is (if > 0)
alt int
}
func (c *subruleContext) String() string {
subctxt := fmt.Sprintf("%s in %s", c.subrule, c.rule)
if c.alt != 0 {
subctxt = fmt.Sprintf("alternative %d of %s", c.alt, subctxt)
}
return subctxt
}
// allow returns whether the subrule is an allow-* subrule.
func (c *subruleContext) allow() bool {
return strings.HasPrefix(c.subrule, "allow-")
}
// installation returns whether the subrule is an *-installation subrule.
func (c *subruleContext) installation() bool {
return strings.HasSuffix(c.subrule, "-installation")
}
// autoConnection returns whether the subrule is an *-auto-connection subrule.
func (c *subruleContext) autoConnection() bool {
return strings.HasSuffix(c.subrule, "-auto-connection")
}
type subruleCompiler func(context *subruleContext, def constraintsDef) (constraintsHolder, error)
func baseCompileRule(context string, rule any, target rule, subrules []string, compilers map[string]subruleCompiler, defaultOutcome, invertedOutcome map[string]any) error {
rMap, invert, err := checkMapOrShortcut(rule)
if err != nil {
return fmt.Errorf("%s must be a map or one of the shortcuts 'true' or 'false'", context)
}
if rMap == nil {
rMap = defaultOutcome // "true"
if invert {
rMap = invertedOutcome // "false"
}
}
defaultUsed := 0
// compile and set subrules
for _, subrule := range subrules {
v := rMap[subrule]
var lst []any
alternatives := false
switch x := v.(type) {
case nil:
v = defaultOutcome[subrule]
defaultUsed++
case []any:
alternatives = true
lst = x
}
if lst == nil { // v is map or a string, checked below
lst = []any{v}
}
compiler := compilers[subrule]
if compiler == nil {
panic(fmt.Sprintf("no compiler for %s in %s", subrule, context))
}
alts := make([]constraintsHolder, len(lst))
for i, alt := range lst {
subctxt := &subruleContext{
rule: context,
subrule: subrule,
}
if alternatives {
subctxt.alt = i + 1
}
cMap, invert, err := checkMapOrShortcut(alt)
if err != nil || (cMap == nil && alternatives) {
efmt := "%s must be a map"
if !alternatives {
efmt = "%s must be a map or one of the shortcuts 'true' or 'false'"
}
return fmt.Errorf(efmt, subctxt)
}
cstrs, err := compiler(subctxt, constraintsDef{
cMap: cMap,
invert: invert,
})
if err != nil {
return err
}
alts[i] = cstrs
}
target.setConstraints(subrule, alts)
}
if defaultUsed == len(subrules) {
return fmt.Errorf("%s must specify at least one of %s", context, strings.Join(subrules, ", "))
}
return nil
}
// PlugRule holds the rule of what is allowed, wrt installation and
// connection, for a plug of a specific interface for a snap.
type PlugRule struct {
Interface string
AllowInstallation []*PlugInstallationConstraints
DenyInstallation []*PlugInstallationConstraints
AllowConnection []*PlugConnectionConstraints
DenyConnection []*PlugConnectionConstraints
AllowAutoConnection []*PlugConnectionConstraints
DenyAutoConnection []*PlugConnectionConstraints
}
func (r *PlugRule) feature(flabel string) bool {
for _, cs := range [][]*PlugInstallationConstraints{r.AllowInstallation, r.DenyInstallation} {
for _, c := range cs {
if c.feature(flabel) {
return true
}
}
}
for _, cs := range [][]*PlugConnectionConstraints{r.AllowConnection, r.DenyConnection, r.AllowAutoConnection, r.DenyAutoConnection} {
for _, c := range cs {
if c.feature(flabel) {
return true
}
}
}
return false
}
func castPlugInstallationConstraints(cstrs []constraintsHolder) (res []*PlugInstallationConstraints) {
res = make([]*PlugInstallationConstraints, len(cstrs))
for i, cstr := range cstrs {
res[i] = cstr.(*PlugInstallationConstraints)
}
return res
}
func castPlugConnectionConstraints(cstrs []constraintsHolder) (res []*PlugConnectionConstraints) {
res = make([]*PlugConnectionConstraints, len(cstrs))
for i, cstr := range cstrs {
res[i] = cstr.(*PlugConnectionConstraints)
}
return res
}
func (r *PlugRule) setConstraints(field string, cstrs []constraintsHolder) {
if len(cstrs) == 0 {
panic(fmt.Sprintf("cannot set PlugRule field %q to empty", field))
}
switch cstrs[0].(type) {
case *PlugInstallationConstraints:
switch field {
case "allow-installation":
r.AllowInstallation = castPlugInstallationConstraints(cstrs)
return
case "deny-installation":
r.DenyInstallation = castPlugInstallationConstraints(cstrs)
return
}
case *PlugConnectionConstraints:
switch field {
case "allow-connection":
r.AllowConnection = castPlugConnectionConstraints(cstrs)
return
case "deny-connection":
r.DenyConnection = castPlugConnectionConstraints(cstrs)
return
case "allow-auto-connection":
r.AllowAutoConnection = castPlugConnectionConstraints(cstrs)
return
case "deny-auto-connection":
r.DenyAutoConnection = castPlugConnectionConstraints(cstrs)
return
}
}
panic(fmt.Sprintf("cannot set PlugRule field %q with %T elements", field, cstrs[0]))
}
// PlugInstallationConstraints specifies a set of constraints on an interface plug relevant to the installation of snap.
type PlugInstallationConstraints struct {
PlugSnapTypes []string
PlugSnapIDs []string
PlugNames *NameConstraints
PlugAttributes *AttributeConstraints
OnClassic *OnClassicConstraint
OnCoreDesktop *OnCoreDesktopConstraint
DeviceScope *DeviceScopeConstraint
}
func (c *PlugInstallationConstraints) feature(flabel string) bool {
if flabel == deviceScopeConstraintsFeature {
return c.DeviceScope != nil
}
if flabel == nameConstraintsFeature {
return c.PlugNames != nil
}
return c.PlugAttributes.feature(flabel)
}
func (c *PlugInstallationConstraints) setNameConstraints(field string, cstrs *NameConstraints) {
switch field {
case "plug-names":
c.PlugNames = cstrs
default:
panic("unknown PlugInstallationConstraints field " + field)
}
}
func (c *PlugInstallationConstraints) setAttributeConstraints(field string, cstrs *AttributeConstraints) {
switch field {
case "plug-attributes":
c.PlugAttributes = cstrs
default:
panic("unknown PlugInstallationConstraints field " + field)
}
}
func (c *PlugInstallationConstraints) setIDConstraints(field string, cstrs []string) {
switch field {
case "plug-snap-type":
c.PlugSnapTypes = cstrs
case "plug-snap-id":
c.PlugSnapIDs = cstrs
default:
panic("unknown PlugInstallationConstraints field " + field)
}
}
func (c *PlugInstallationConstraints) setOnClassicConstraint(onClassic *OnClassicConstraint) {
c.OnClassic = onClassic
}
func (c *PlugInstallationConstraints) setOnCoreDesktopConstraint(onCoreDesktop *OnCoreDesktopConstraint) {
c.OnCoreDesktop = onCoreDesktop
}
func (c *PlugInstallationConstraints) setDeviceScopeConstraint(deviceScope *DeviceScopeConstraint) {
c.DeviceScope = deviceScope
}
func compilePlugInstallationConstraints(context *subruleContext, cDef constraintsDef) (constraintsHolder, error) {
plugInstCstrs := &PlugInstallationConstraints{}
// plug-snap-id is supported here mainly for symmetry with the slot case
// see discussion there
err := baseCompileConstraints(context, cDef, plugInstCstrs, []string{"plug-names"}, []string{"plug-attributes"}, []string{"plug-snap-type", "plug-snap-id"})
if err != nil {
return nil, err
}
return plugInstCstrs, nil
}
// PlugConnectionConstraints specfies a set of constraints on an
// interface plug for a snap relevant to its connection or
// auto-connection.
type PlugConnectionConstraints struct {
SlotSnapTypes []string
SlotSnapIDs []string
SlotPublisherIDs []string
PlugNames *NameConstraints
SlotNames *NameConstraints
PlugAttributes *AttributeConstraints
SlotAttributes *AttributeConstraints
// SlotsPerPlug defaults to 1 for auto-connection, can be * (any)
SlotsPerPlug SideArityConstraint
// PlugsPerSlot is always * (any) (for now)
PlugsPerSlot SideArityConstraint
OnClassic *OnClassicConstraint
OnCoreDesktop *OnCoreDesktopConstraint
DeviceScope *DeviceScopeConstraint
}
func (c *PlugConnectionConstraints) feature(flabel string) bool {
if flabel == deviceScopeConstraintsFeature {
return c.DeviceScope != nil
}
if flabel == nameConstraintsFeature {
return c.PlugNames != nil || c.SlotNames != nil
}
return c.PlugAttributes.feature(flabel) || c.SlotAttributes.feature(flabel)
}
func (c *PlugConnectionConstraints) setNameConstraints(field string, cstrs *NameConstraints) {
switch field {
case "plug-names":
c.PlugNames = cstrs
case "slot-names":
c.SlotNames = cstrs
default:
panic("unknown PlugConnectionConstraints field " + field)
}
}
func (c *PlugConnectionConstraints) setAttributeConstraints(field string, cstrs *AttributeConstraints) {
switch field {
case "plug-attributes":
c.PlugAttributes = cstrs
case "slot-attributes":
c.SlotAttributes = cstrs
default:
panic("unknown PlugConnectionConstraints field " + field)
}
}
func (c *PlugConnectionConstraints) setIDConstraints(field string, cstrs []string) {
switch field {
case "slot-snap-type":
c.SlotSnapTypes = cstrs
case "slot-snap-id":
c.SlotSnapIDs = cstrs
case "slot-publisher-id":
c.SlotPublisherIDs = cstrs
default:
panic("unknown PlugConnectionConstraints field " + field)
}
}
func (c *PlugConnectionConstraints) setSlotsPerPlug(a SideArityConstraint) {
c.SlotsPerPlug = a
}
func (c *PlugConnectionConstraints) setPlugsPerSlot(a SideArityConstraint) {
c.PlugsPerSlot = a
}
func (c *PlugConnectionConstraints) slotsPerPlug() SideArityConstraint {
return c.SlotsPerPlug
}
func (c *PlugConnectionConstraints) plugsPerSlot() SideArityConstraint {
return c.PlugsPerSlot
}
func (c *PlugConnectionConstraints) setOnClassicConstraint(onClassic *OnClassicConstraint) {
c.OnClassic = onClassic
}
func (c *PlugConnectionConstraints) setOnCoreDesktopConstraint(onCoreDesktop *OnCoreDesktopConstraint) {
c.OnCoreDesktop = onCoreDesktop
}
func (c *PlugConnectionConstraints) setDeviceScopeConstraint(deviceScope *DeviceScopeConstraint) {
c.DeviceScope = deviceScope
}
var (
nameConstraints = []string{"plug-names", "slot-names"}
attributeConstraints = []string{"plug-attributes", "slot-attributes"}
plugIDConstraints = []string{"slot-snap-type", "slot-publisher-id", "slot-snap-id"}
)
func compilePlugConnectionConstraints(context *subruleContext, cDef constraintsDef) (constraintsHolder, error) {
plugConnCstrs := &PlugConnectionConstraints{}
err := baseCompileConstraints(context, cDef, plugConnCstrs, nameConstraints, attributeConstraints, plugIDConstraints)
if err != nil {
return nil, err
}
normalizeSideArityConstraints(context, plugConnCstrs)
return plugConnCstrs, nil
}
var (
defaultOutcome = map[string]any{
"allow-installation": "true",
"allow-connection": "true",
"allow-auto-connection": "true",
"deny-installation": "false",
"deny-connection": "false",
"deny-auto-connection": "false",
}
invertedOutcome = map[string]any{
"allow-installation": "false",
"allow-connection": "false",
"allow-auto-connection": "false",
"deny-installation": "true",
"deny-connection": "true",
"deny-auto-connection": "true",
}
ruleSubrules = []string{"allow-installation", "deny-installation", "allow-connection", "deny-connection", "allow-auto-connection", "deny-auto-connection"}
)
var plugRuleCompilers = map[string]subruleCompiler{
"allow-installation": compilePlugInstallationConstraints,
"deny-installation": compilePlugInstallationConstraints,
"allow-connection": compilePlugConnectionConstraints,
"deny-connection": compilePlugConnectionConstraints,
"allow-auto-connection": compilePlugConnectionConstraints,
"deny-auto-connection": compilePlugConnectionConstraints,
}
func compilePlugRule(interfaceName string, rule any) (*PlugRule, error) {
context := fmt.Sprintf("plug rule for interface %q", interfaceName)
plugRule := &PlugRule{
Interface: interfaceName,
}
err := baseCompileRule(context, rule, plugRule, ruleSubrules, plugRuleCompilers, defaultOutcome, invertedOutcome)
if err != nil {
return nil, err
}
return plugRule, nil
}
// SlotRule holds the rule of what is allowed, wrt installation and
// connection, for a slot of a specific interface for a snap.
type SlotRule struct {
Interface string
AllowInstallation []*SlotInstallationConstraints
DenyInstallation []*SlotInstallationConstraints
AllowConnection []*SlotConnectionConstraints
DenyConnection []*SlotConnectionConstraints
AllowAutoConnection []*SlotConnectionConstraints
DenyAutoConnection []*SlotConnectionConstraints
}
func castSlotInstallationConstraints(cstrs []constraintsHolder) (res []*SlotInstallationConstraints) {
res = make([]*SlotInstallationConstraints, len(cstrs))
for i, cstr := range cstrs {
res[i] = cstr.(*SlotInstallationConstraints)
}
return res
}
func (r *SlotRule) feature(flabel string) bool {
for _, cs := range [][]*SlotInstallationConstraints{r.AllowInstallation, r.DenyInstallation} {
for _, c := range cs {
if c.feature(flabel) {
return true
}
}
}
for _, cs := range [][]*SlotConnectionConstraints{r.AllowConnection, r.DenyConnection, r.AllowAutoConnection, r.DenyAutoConnection} {
for _, c := range cs {
if c.feature(flabel) {
return true
}
}
}
return false
}
func castSlotConnectionConstraints(cstrs []constraintsHolder) (res []*SlotConnectionConstraints) {
res = make([]*SlotConnectionConstraints, len(cstrs))
for i, cstr := range cstrs {
res[i] = cstr.(*SlotConnectionConstraints)
}
return res
}
func (r *SlotRule) setConstraints(field string, cstrs []constraintsHolder) {
if len(cstrs) == 0 {
panic(fmt.Sprintf("cannot set SlotRule field %q to empty", field))
}
switch cstrs[0].(type) {
case *SlotInstallationConstraints:
switch field {
case "allow-installation":
r.AllowInstallation = castSlotInstallationConstraints(cstrs)
return
case "deny-installation":
r.DenyInstallation = castSlotInstallationConstraints(cstrs)
return
}
case *SlotConnectionConstraints:
switch field {
case "allow-connection":
r.AllowConnection = castSlotConnectionConstraints(cstrs)
return
case "deny-connection":
r.DenyConnection = castSlotConnectionConstraints(cstrs)
return
case "allow-auto-connection":
r.AllowAutoConnection = castSlotConnectionConstraints(cstrs)
return
case "deny-auto-connection":
r.DenyAutoConnection = castSlotConnectionConstraints(cstrs)
return
}
}
panic(fmt.Sprintf("cannot set SlotRule field %q with %T elements", field, cstrs[0]))
}
// SlotInstallationConstraints specifies a set of constraints on an
// interface slot relevant to the installation of snap.
type SlotInstallationConstraints struct {
SlotSnapTypes []string
SlotSnapIDs []string
SlotNames *NameConstraints
SlotAttributes *AttributeConstraints
OnClassic *OnClassicConstraint
OnCoreDesktop *OnCoreDesktopConstraint
DeviceScope *DeviceScopeConstraint
}
func (c *SlotInstallationConstraints) feature(flabel string) bool {
if flabel == deviceScopeConstraintsFeature {
return c.DeviceScope != nil
}
if flabel == nameConstraintsFeature {
return c.SlotNames != nil
}
return c.SlotAttributes.feature(flabel)
}
func (c *SlotInstallationConstraints) setNameConstraints(field string, cstrs *NameConstraints) {
switch field {
case "slot-names":
c.SlotNames = cstrs
default:
panic("unknown SlotInstallationConstraints field " + field)
}
}
func (c *SlotInstallationConstraints) setAttributeConstraints(field string, cstrs *AttributeConstraints) {
switch field {
case "slot-attributes":
c.SlotAttributes = cstrs
default:
panic("unknown SlotInstallationConstraints field " + field)
}
}
func (c *SlotInstallationConstraints) setIDConstraints(field string, cstrs []string) {
switch field {
case "slot-snap-type":
c.SlotSnapTypes = cstrs
case "slot-snap-id":
c.SlotSnapIDs = cstrs
default:
panic("unknown SlotInstallationConstraints field " + field)
}
}
func (c *SlotInstallationConstraints) setOnClassicConstraint(onClassic *OnClassicConstraint) {
c.OnClassic = onClassic
}
func (c *SlotInstallationConstraints) setOnCoreDesktopConstraint(onCoreDesktop *OnCoreDesktopConstraint) {
c.OnCoreDesktop = onCoreDesktop
}
func (c *SlotInstallationConstraints) setDeviceScopeConstraint(deviceScope *DeviceScopeConstraint) {
c.DeviceScope = deviceScope
}
func compileSlotInstallationConstraints(context *subruleContext, cDef constraintsDef) (constraintsHolder, error) {
slotInstCstrs := &SlotInstallationConstraints{}
// slot-snap-id here is mostly useful to restrict a relaxed
// base-declaration slot-snap-type constraint because the latter is used
// also for --dangerous installations. So in rare complex situations
// slot-snap-type might constraint to core and app
// but the intention is really that only system snaps should have the
// slot without a snap-declaration rule, slot-snap-id then can
// be used to limit to the known system snap snap-ids.
// This means we want app-slots to be super-privileged but we have
// slots for the interface on the system snaps as well.
// An example of this is shared-memory.
err := baseCompileConstraints(context, cDef, slotInstCstrs, []string{"slot-names"}, []string{"slot-attributes"}, []string{"slot-snap-type", "slot-snap-id"})
if err != nil {
return nil, err
}
return slotInstCstrs, nil
}
// SlotConnectionConstraints specfies a set of constraints on an
// interface slot for a snap relevant to its connection or
// auto-connection.
type SlotConnectionConstraints struct {
// SlotSnapTypes constraints on the slot side for connections
// are only useful in the base-declaration,
// as the snap-declaration is for one given snap with its type.
// So there is no (new) format iteration to cover this.
SlotSnapTypes []string
PlugSnapTypes []string
PlugSnapIDs []string
PlugPublisherIDs []string
SlotNames *NameConstraints
PlugNames *NameConstraints
SlotAttributes *AttributeConstraints
PlugAttributes *AttributeConstraints
// SlotsPerPlug defaults to 1 for auto-connection, can be * (any)
SlotsPerPlug SideArityConstraint
// PlugsPerSlot is always * (any) (for now)
PlugsPerSlot SideArityConstraint
OnClassic *OnClassicConstraint
OnCoreDesktop *OnCoreDesktopConstraint
DeviceScope *DeviceScopeConstraint
}
func (c *SlotConnectionConstraints) feature(flabel string) bool {
if flabel == deviceScopeConstraintsFeature {
return c.DeviceScope != nil
}
if flabel == nameConstraintsFeature {
return c.PlugNames != nil || c.SlotNames != nil
}
return c.PlugAttributes.feature(flabel) || c.SlotAttributes.feature(flabel)
}
func (c *SlotConnectionConstraints) setNameConstraints(field string, cstrs *NameConstraints) {
switch field {
case "plug-names":
c.PlugNames = cstrs
case "slot-names":
c.SlotNames = cstrs
default:
panic("unknown SlotConnectionConstraints field " + field)
}
}
func (c *SlotConnectionConstraints) setAttributeConstraints(field string, cstrs *AttributeConstraints) {
switch field {
case "plug-attributes":
c.PlugAttributes = cstrs
case "slot-attributes":
c.SlotAttributes = cstrs
default:
panic("unknown SlotConnectionConstraints field " + field)
}
}
func (c *SlotConnectionConstraints) setIDConstraints(field string, cstrs []string) {
switch field {
case "slot-snap-type":
c.SlotSnapTypes = cstrs
case "plug-snap-type":
c.PlugSnapTypes = cstrs
case "plug-snap-id":
c.PlugSnapIDs = cstrs
case "plug-publisher-id":
c.PlugPublisherIDs = cstrs
default:
panic("unknown SlotConnectionConstraints field " + field)
}
}
var (
slotIDConstraints = []string{"slot-snap-type", "plug-snap-type", "plug-publisher-id", "plug-snap-id"}
)
func (c *SlotConnectionConstraints) setSlotsPerPlug(a SideArityConstraint) {
c.SlotsPerPlug = a
}
func (c *SlotConnectionConstraints) setPlugsPerSlot(a SideArityConstraint) {
c.PlugsPerSlot = a
}
func (c *SlotConnectionConstraints) slotsPerPlug() SideArityConstraint {
return c.SlotsPerPlug
}
func (c *SlotConnectionConstraints) plugsPerSlot() SideArityConstraint {
return c.PlugsPerSlot
}
func (c *SlotConnectionConstraints) setOnClassicConstraint(onClassic *OnClassicConstraint) {
c.OnClassic = onClassic
}
func (c *SlotConnectionConstraints) setOnCoreDesktopConstraint(onCoreDesktop *OnCoreDesktopConstraint) {
c.OnCoreDesktop = onCoreDesktop
}
func (c *SlotConnectionConstraints) setDeviceScopeConstraint(deviceScope *DeviceScopeConstraint) {
c.DeviceScope = deviceScope
}
func compileSlotConnectionConstraints(context *subruleContext, cDef constraintsDef) (constraintsHolder, error) {
slotConnCstrs := &SlotConnectionConstraints{}
err := baseCompileConstraints(context, cDef, slotConnCstrs, nameConstraints, attributeConstraints, slotIDConstraints)
if err != nil {
return nil, err
}
normalizeSideArityConstraints(context, slotConnCstrs)
return slotConnCstrs, nil
}
var slotRuleCompilers = map[string]subruleCompiler{
"allow-installation": compileSlotInstallationConstraints,
"deny-installation": compileSlotInstallationConstraints,
"allow-connection": compileSlotConnectionConstraints,
"deny-connection": compileSlotConnectionConstraints,
"allow-auto-connection": compileSlotConnectionConstraints,
"deny-auto-connection": compileSlotConnectionConstraints,
}
func compileSlotRule(interfaceName string, rule any) (*SlotRule, error) {
context := fmt.Sprintf("slot rule for interface %q", interfaceName)
slotRule := &SlotRule{
Interface: interfaceName,
}
err := baseCompileRule(context, rule, slotRule, ruleSubrules, slotRuleCompilers, defaultOutcome, invertedOutcome)
if err != nil {
return nil, err
}
return slotRule, nil
}
|