-
Notifications
You must be signed in to change notification settings - Fork 696
Expand file tree
/
Copy pathconfdb.go
More file actions
113 lines (95 loc) · 3.3 KB
/
Copy pathconfdb.go
File metadata and controls
113 lines (95 loc) · 3.3 KB
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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2024 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 builtin
import (
"fmt"
"strings"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/confdb"
"github.com/snapcore/snapd/snap"
)
// The confdb interface can be plugged by snaps that want to access specific
// confdb views. Plugs are auto-connected if the confdb's account is the
// same as the snap's publisher.
const confdbSummary = `allows accessing a confdb through a view`
const confdbBaseDeclarationSlots = `
confdb:
allow-installation:
slot-snap-type:
- core
`
const confdbBaseDeclarationPlugs = `
confdb:
allow-auto-connection:
plug-attributes:
account: $PLUG_PUBLISHER_ID
`
// confdbInterface allows accessing a confdb through a view
type confdbInterface struct {
commonInterface
}
func (iface *confdbInterface) BeforePreparePlug(plug *snap.PlugInfo) error {
account, ok := plug.Attrs["account"].(string)
if !ok || account == "" {
return fmt.Errorf(`confdb plug must have an "account" attribute`)
}
if !asserts.IsValidAccountID(account) {
return fmt.Errorf(`confdb plug must have a valid "account" attribute: format mismatch`)
}
view, ok := plug.Attrs["view"].(string)
if !ok || view == "" {
return fmt.Errorf(`confdb plug must have a "view" attribute`)
}
if err := validateView(view); err != nil {
return fmt.Errorf(`confdb plug must have a valid "view" attribute: %w`, err)
}
// by default, snaps can read/write confdb and be notified of changes. The
// custodian role allows snaps to change, reject and persist changes made by others
role, ok := plug.Attrs["role"].(string)
if ok && role != "custodian" {
return fmt.Errorf(`optional confdb plug "role" attribute must be "custodian"`)
}
if role == "custodian" && account == "system" {
return fmt.Errorf(`snaps cannot be custodians of "system" confdb schemas`)
}
return nil
}
func validateView(view string) error {
parts := strings.Split(view, "/")
if len(parts) != 2 {
return fmt.Errorf("expected confdb and view names separated by a single '/': %s", view)
}
if !confdb.ValidConfdbName.MatchString(parts[0]) {
return fmt.Errorf("invalid confdb name: %s does not match '%s'", parts[0], confdb.ValidConfdbName.String())
}
if !confdb.ValidViewName.MatchString(parts[1]) {
return fmt.Errorf("invalid view name: %s does not match '%s'", parts[1], confdb.ValidViewName.String())
}
return nil
}
func init() {
registerIface(&confdbInterface{
commonInterface: commonInterface{
name: "confdb",
summary: confdbSummary,
baseDeclarationPlugs: confdbBaseDeclarationPlugs,
baseDeclarationSlots: confdbBaseDeclarationSlots,
implicitOnClassic: true,
implicitOnCore: true,
}})
}